|
11 | 11 | #include "llvm/ADT/StringExtras.h"
|
12 | 12 | #include "llvm/ADT/Twine.h"
|
13 | 13 | #include "llvm/Config/llvm-config.h"
|
14 |
| -#include "llvm/MC/MCAsmLayout.h" |
15 |
| -#include "llvm/MC/MCAssembler.h" |
16 | 14 | #include "llvm/MC/MCContext.h"
|
17 |
| -#include "llvm/MC/MCExpr.h" |
18 | 15 | #include "llvm/MC/MCFixup.h"
|
19 | 16 | #include "llvm/MC/MCSection.h"
|
20 | 17 | #include "llvm/MC/MCSectionMachO.h"
|
21 | 18 | #include "llvm/MC/MCSymbol.h"
|
22 |
| -#include "llvm/MC/MCValue.h" |
23 | 19 | #include "llvm/Support/Casting.h"
|
24 | 20 | #include "llvm/Support/Compiler.h"
|
25 | 21 | #include "llvm/Support/ErrorHandling.h"
|
|
30 | 26 |
|
31 | 27 | using namespace llvm;
|
32 | 28 |
|
33 |
| -MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) { |
34 |
| - // Compute the section layout order. Virtual sections must go last. |
35 |
| - for (MCSection &Sec : Asm) |
36 |
| - if (!Sec.isVirtualSection()) |
37 |
| - SectionOrder.push_back(&Sec); |
38 |
| - for (MCSection &Sec : Asm) |
39 |
| - if (Sec.isVirtualSection()) |
40 |
| - SectionOrder.push_back(&Sec); |
41 |
| -} |
42 |
| - |
43 |
| -// Simple getSymbolOffset helper for the non-variable case. |
44 |
| -static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S, |
45 |
| - bool ReportError, uint64_t &Val) { |
46 |
| - if (!S.getFragment()) { |
47 |
| - if (ReportError) |
48 |
| - report_fatal_error("unable to evaluate offset to undefined symbol '" + |
49 |
| - S.getName() + "'"); |
50 |
| - return false; |
51 |
| - } |
52 |
| - Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset(); |
53 |
| - return true; |
54 |
| -} |
55 |
| - |
56 |
| -static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S, |
57 |
| - bool ReportError, uint64_t &Val) { |
58 |
| - if (!S.isVariable()) |
59 |
| - return getLabelOffset(Layout, S, ReportError, Val); |
60 |
| - |
61 |
| - // If SD is a variable, evaluate it. |
62 |
| - MCValue Target; |
63 |
| - if (!S.getVariableValue()->evaluateAsValue(Target, Layout)) |
64 |
| - report_fatal_error("unable to evaluate offset for variable '" + |
65 |
| - S.getName() + "'"); |
66 |
| - |
67 |
| - uint64_t Offset = Target.getConstant(); |
68 |
| - |
69 |
| - const MCSymbolRefExpr *A = Target.getSymA(); |
70 |
| - if (A) { |
71 |
| - uint64_t ValA; |
72 |
| - // FIXME: On most platforms, `Target`'s component symbols are labels from |
73 |
| - // having been simplified during evaluation, but on Mach-O they can be |
74 |
| - // variables due to PR19203. This, and the line below for `B` can be |
75 |
| - // restored to call `getLabelOffset` when PR19203 is fixed. |
76 |
| - if (!getSymbolOffsetImpl(Layout, A->getSymbol(), ReportError, ValA)) |
77 |
| - return false; |
78 |
| - Offset += ValA; |
79 |
| - } |
80 |
| - |
81 |
| - const MCSymbolRefExpr *B = Target.getSymB(); |
82 |
| - if (B) { |
83 |
| - uint64_t ValB; |
84 |
| - if (!getSymbolOffsetImpl(Layout, B->getSymbol(), ReportError, ValB)) |
85 |
| - return false; |
86 |
| - Offset -= ValB; |
87 |
| - } |
88 |
| - |
89 |
| - Val = Offset; |
90 |
| - return true; |
91 |
| -} |
92 |
| - |
93 |
| -bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const { |
94 |
| - return getSymbolOffsetImpl(*this, S, false, Val); |
95 |
| -} |
96 |
| - |
97 |
| -uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const { |
98 |
| - uint64_t Val; |
99 |
| - getSymbolOffsetImpl(*this, S, true, Val); |
100 |
| - return Val; |
101 |
| -} |
102 |
| - |
103 |
| -const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const { |
104 |
| - if (!Symbol.isVariable()) |
105 |
| - return &Symbol; |
106 |
| - |
107 |
| - const MCExpr *Expr = Symbol.getVariableValue(); |
108 |
| - MCValue Value; |
109 |
| - if (!Expr->evaluateAsValue(Value, *this)) { |
110 |
| - Assembler.getContext().reportError( |
111 |
| - Expr->getLoc(), "expression could not be evaluated"); |
112 |
| - return nullptr; |
113 |
| - } |
114 |
| - |
115 |
| - const MCSymbolRefExpr *RefB = Value.getSymB(); |
116 |
| - if (RefB) { |
117 |
| - Assembler.getContext().reportError( |
118 |
| - Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() + |
119 |
| - "' could not be evaluated in a subtraction expression"); |
120 |
| - return nullptr; |
121 |
| - } |
122 |
| - |
123 |
| - const MCSymbolRefExpr *A = Value.getSymA(); |
124 |
| - if (!A) |
125 |
| - return nullptr; |
126 |
| - |
127 |
| - const MCSymbol &ASym = A->getSymbol(); |
128 |
| - const MCAssembler &Asm = getAssembler(); |
129 |
| - if (ASym.isCommon()) { |
130 |
| - Asm.getContext().reportError(Expr->getLoc(), |
131 |
| - "Common symbol '" + ASym.getName() + |
132 |
| - "' cannot be used in assignment expr"); |
133 |
| - return nullptr; |
134 |
| - } |
135 |
| - |
136 |
| - return &ASym; |
137 |
| -} |
138 |
| - |
139 |
| -uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const { |
140 |
| - // The size is the last fragment's end offset. |
141 |
| - const MCFragment &F = *Sec->curFragList()->Tail; |
142 |
| - return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F); |
143 |
| -} |
144 |
| - |
145 |
| -uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const { |
146 |
| - // Virtual sections have no file size. |
147 |
| - if (Sec->isVirtualSection()) |
148 |
| - return 0; |
149 |
| - |
150 |
| - // Otherwise, the file size is the same as the address space size. |
151 |
| - return getSectionAddressSize(Sec); |
152 |
| -} |
153 |
| - |
154 |
| -/* *** */ |
155 |
| - |
156 | 29 | MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)
|
157 | 30 | : Kind(Kind), HasInstructions(HasInstructions), LinkerRelaxable(false) {}
|
158 | 31 |
|
|
0 commit comments