blob: 638d91033363c7a3571f8893db9675afbd0a742a [file] [log] [blame]
Chandler Carruth8d736232015-12-29 09:06:16 +00001//===- lib/MC/MCFragment.cpp - Assembler Fragment Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCFragment.h"
11#include "llvm/ADT/StringExtras.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/MC/MCAsmBackend.h"
14#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCAsmLayout.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDwarf.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCFixupKindInfo.h"
20#include "llvm/MC/MCSection.h"
21#include "llvm/MC/MCSectionELF.h"
22#include "llvm/MC/MCSymbol.h"
23#include "llvm/MC/MCValue.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/LEB128.h"
26#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Support/raw_ostream.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000028using namespace llvm;
29
30MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
31 : Assembler(Asm), LastValidFragment()
32 {
33 // Compute the section layout order. Virtual sections must go last.
34 for (MCSection &Sec : Asm)
35 if (!Sec.isVirtualSection())
36 SectionOrder.push_back(&Sec);
37 for (MCSection &Sec : Asm)
38 if (Sec.isVirtualSection())
39 SectionOrder.push_back(&Sec);
40}
41
42bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
43 const MCSection *Sec = F->getParent();
44 const MCFragment *LastValid = LastValidFragment.lookup(Sec);
45 if (!LastValid)
46 return false;
47 assert(LastValid->getParent() == Sec);
48 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
49}
50
51void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
52 // If this fragment wasn't already valid, we don't need to do anything.
53 if (!isFragmentValid(F))
54 return;
55
56 // Otherwise, reset the last valid fragment to the previous fragment
57 // (if this is the first fragment, it will be NULL).
58 LastValidFragment[F->getParent()] = F->getPrevNode();
59}
60
61void MCAsmLayout::ensureValid(const MCFragment *F) const {
62 MCSection *Sec = F->getParent();
63 MCSection::iterator I;
64 if (MCFragment *Cur = LastValidFragment[Sec])
65 I = ++MCSection::iterator(Cur);
66 else
67 I = Sec->begin();
68
69 // Advance the layout position until the fragment is valid.
70 while (!isFragmentValid(F)) {
71 assert(I != Sec->end() && "Layout bookkeeping error");
72 const_cast<MCAsmLayout *>(this)->layoutFragment(&*I);
73 ++I;
74 }
75}
76
77uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
78 ensureValid(F);
79 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
80 return F->Offset;
81}
82
83// Simple getSymbolOffset helper for the non-varibale case.
84static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
85 bool ReportError, uint64_t &Val) {
86 if (!S.getFragment()) {
87 if (ReportError)
88 report_fatal_error("unable to evaluate offset to undefined symbol '" +
89 S.getName() + "'");
90 return false;
91 }
92 Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
93 return true;
94}
95
96static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
97 bool ReportError, uint64_t &Val) {
98 if (!S.isVariable())
99 return getLabelOffset(Layout, S, ReportError, Val);
100
101 // If SD is a variable, evaluate it.
102 MCValue Target;
103 if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
104 report_fatal_error("unable to evaluate offset for variable '" +
105 S.getName() + "'");
106
107 uint64_t Offset = Target.getConstant();
108
109 const MCSymbolRefExpr *A = Target.getSymA();
110 if (A) {
111 uint64_t ValA;
112 if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA))
113 return false;
114 Offset += ValA;
115 }
116
117 const MCSymbolRefExpr *B = Target.getSymB();
118 if (B) {
119 uint64_t ValB;
120 if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB))
121 return false;
122 Offset -= ValB;
123 }
124
125 Val = Offset;
126 return true;
127}
128
129bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
130 return getSymbolOffsetImpl(*this, S, false, Val);
131}
132
133uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
134 uint64_t Val;
135 getSymbolOffsetImpl(*this, S, true, Val);
136 return Val;
137}
138
139const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
140 if (!Symbol.isVariable())
141 return &Symbol;
142
143 const MCExpr *Expr = Symbol.getVariableValue();
144 MCValue Value;
145 if (!Expr->evaluateAsValue(Value, *this)) {
146 Assembler.getContext().reportError(
147 SMLoc(), "expression could not be evaluated");
148 return nullptr;
149 }
150
151 const MCSymbolRefExpr *RefB = Value.getSymB();
152 if (RefB) {
153 Assembler.getContext().reportError(
154 SMLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
155 "' could not be evaluated in a subtraction expression");
156 return nullptr;
157 }
158
159 const MCSymbolRefExpr *A = Value.getSymA();
160 if (!A)
161 return nullptr;
162
163 const MCSymbol &ASym = A->getSymbol();
164 const MCAssembler &Asm = getAssembler();
165 if (ASym.isCommon()) {
166 // FIXME: we should probably add a SMLoc to MCExpr.
167 Asm.getContext().reportError(SMLoc(),
168 "Common symbol '" + ASym.getName() +
169 "' cannot be used in assignment expr");
170 return nullptr;
171 }
172
173 return &ASym;
174}
175
176uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
177 // The size is the last fragment's end offset.
178 const MCFragment &F = Sec->getFragmentList().back();
179 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
180}
181
182uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
183 // Virtual sections have no file size.
184 if (Sec->isVirtualSection())
185 return 0;
186
187 // Otherwise, the file size is the same as the address space size.
188 return getSectionAddressSize(Sec);
189}
190
191uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
192 const MCFragment *F,
193 uint64_t FOffset, uint64_t FSize) {
194 uint64_t BundleSize = Assembler.getBundleAlignSize();
195 assert(BundleSize > 0 &&
196 "computeBundlePadding should only be called if bundling is enabled");
197 uint64_t BundleMask = BundleSize - 1;
198 uint64_t OffsetInBundle = FOffset & BundleMask;
199 uint64_t EndOfFragment = OffsetInBundle + FSize;
200
201 // There are two kinds of bundling restrictions:
202 //
203 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
204 // *end* on a bundle boundary.
205 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
206 // would, add padding until the end of the bundle so that the fragment
207 // will start in a new one.
208 if (F->alignToBundleEnd()) {
209 // Three possibilities here:
210 //
211 // A) The fragment just happens to end at a bundle boundary, so we're good.
212 // B) The fragment ends before the current bundle boundary: pad it just
213 // enough to reach the boundary.
214 // C) The fragment ends after the current bundle boundary: pad it until it
215 // reaches the end of the next bundle boundary.
216 //
217 // Note: this code could be made shorter with some modulo trickery, but it's
218 // intentionally kept in its more explicit form for simplicity.
219 if (EndOfFragment == BundleSize)
220 return 0;
221 else if (EndOfFragment < BundleSize)
222 return BundleSize - EndOfFragment;
223 else { // EndOfFragment > BundleSize
224 return 2 * BundleSize - EndOfFragment;
225 }
226 } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
227 return BundleSize - OffsetInBundle;
228 else
229 return 0;
230}
231
232/* *** */
233
234void ilist_node_traits<MCFragment>::deleteNode(MCFragment *V) {
235 V->destroy();
236}
237
Chandler Carruth8d736232015-12-29 09:06:16 +0000238MCFragment::~MCFragment() { }
239
240MCFragment::MCFragment(FragmentType Kind, bool HasInstructions,
241 uint8_t BundlePadding, MCSection *Parent)
242 : Kind(Kind), HasInstructions(HasInstructions), AlignToBundleEnd(false),
243 BundlePadding(BundlePadding), Parent(Parent), Atom(nullptr),
244 Offset(~UINT64_C(0)) {
245 if (Parent && !isDummy())
246 Parent->getFragmentList().push_back(this);
247}
248
249void MCFragment::destroy() {
250 // First check if we are the sentinal.
251 if (Kind == FragmentType(~0)) {
252 delete this;
253 return;
254 }
255
256 switch (Kind) {
257 case FT_Align:
258 delete cast<MCAlignFragment>(this);
259 return;
260 case FT_Data:
261 delete cast<MCDataFragment>(this);
262 return;
263 case FT_CompactEncodedInst:
264 delete cast<MCCompactEncodedInstFragment>(this);
265 return;
266 case FT_Fill:
267 delete cast<MCFillFragment>(this);
268 return;
269 case FT_Relaxable:
270 delete cast<MCRelaxableFragment>(this);
271 return;
272 case FT_Org:
273 delete cast<MCOrgFragment>(this);
274 return;
275 case FT_Dwarf:
276 delete cast<MCDwarfLineAddrFragment>(this);
277 return;
278 case FT_DwarfFrame:
279 delete cast<MCDwarfCallFrameFragment>(this);
280 return;
281 case FT_LEB:
282 delete cast<MCLEBFragment>(this);
283 return;
284 case FT_SafeSEH:
285 delete cast<MCSafeSEHFragment>(this);
286 return;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000287 case FT_CVInlineLines:
288 delete cast<MCCVInlineLineTableFragment>(this);
289 return;
David Majnemer408b5e62016-02-05 01:55:49 +0000290 case FT_CVDefRange:
291 delete cast<MCCVDefRangeFragment>(this);
292 return;
Chandler Carruth8d736232015-12-29 09:06:16 +0000293 case FT_Dummy:
294 delete cast<MCDummyFragment>(this);
295 return;
296 }
297}
298
299/* *** */
300
301// Debugging methods
302
303namespace llvm {
304
305raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
306 OS << "<MCFixup" << " Offset:" << AF.getOffset()
307 << " Value:" << *AF.getValue()
308 << " Kind:" << AF.getKind() << ">";
309 return OS;
310}
311
312}
313
Yaron Kereneb2a2542016-01-29 20:50:44 +0000314LLVM_DUMP_METHOD void MCFragment::dump() {
Chandler Carruth8d736232015-12-29 09:06:16 +0000315 raw_ostream &OS = llvm::errs();
316
317 OS << "<";
318 switch (getKind()) {
319 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
320 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
321 case MCFragment::FT_CompactEncodedInst:
322 OS << "MCCompactEncodedInstFragment"; break;
323 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
324 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
325 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
326 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
327 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
328 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
329 case MCFragment::FT_SafeSEH: OS << "MCSafeSEHFragment"; break;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000330 case MCFragment::FT_CVInlineLines: OS << "MCCVInlineLineTableFragment"; break;
David Majnemer408b5e62016-02-05 01:55:49 +0000331 case MCFragment::FT_CVDefRange: OS << "MCCVDefRangeTableFragment"; break;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000332 case MCFragment::FT_Dummy: OS << "MCDummyFragment"; break;
Chandler Carruth8d736232015-12-29 09:06:16 +0000333 }
334
335 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
336 << " Offset:" << Offset
337 << " HasInstructions:" << hasInstructions()
338 << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">";
339
340 switch (getKind()) {
341 case MCFragment::FT_Align: {
342 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
343 if (AF->hasEmitNops())
344 OS << " (emit nops)";
345 OS << "\n ";
346 OS << " Alignment:" << AF->getAlignment()
347 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
348 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
349 break;
350 }
351 case MCFragment::FT_Data: {
352 const MCDataFragment *DF = cast<MCDataFragment>(this);
353 OS << "\n ";
354 OS << " Contents:[";
355 const SmallVectorImpl<char> &Contents = DF->getContents();
356 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
357 if (i) OS << ",";
358 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
359 }
360 OS << "] (" << Contents.size() << " bytes)";
361
362 if (DF->fixup_begin() != DF->fixup_end()) {
363 OS << ",\n ";
364 OS << " Fixups:[";
365 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
366 ie = DF->fixup_end(); it != ie; ++it) {
367 if (it != DF->fixup_begin()) OS << ",\n ";
368 OS << *it;
369 }
370 OS << "]";
371 }
372 break;
373 }
374 case MCFragment::FT_CompactEncodedInst: {
375 const MCCompactEncodedInstFragment *CEIF =
376 cast<MCCompactEncodedInstFragment>(this);
377 OS << "\n ";
378 OS << " Contents:[";
379 const SmallVectorImpl<char> &Contents = CEIF->getContents();
380 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
381 if (i) OS << ",";
382 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
383 }
384 OS << "] (" << Contents.size() << " bytes)";
385 break;
386 }
387 case MCFragment::FT_Fill: {
388 const MCFillFragment *FF = cast<MCFillFragment>(this);
Rafael Espindola1a7e8b42016-01-19 16:57:08 +0000389 OS << " Value:" << FF->getValue() << " Size:" << FF->getSize();
Chandler Carruth8d736232015-12-29 09:06:16 +0000390 break;
391 }
392 case MCFragment::FT_Relaxable: {
393 const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
394 OS << "\n ";
395 OS << " Inst:";
396 F->getInst().dump_pretty(OS);
397 break;
398 }
399 case MCFragment::FT_Org: {
400 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
401 OS << "\n ";
402 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
403 break;
404 }
405 case MCFragment::FT_Dwarf: {
406 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
407 OS << "\n ";
408 OS << " AddrDelta:" << OF->getAddrDelta()
409 << " LineDelta:" << OF->getLineDelta();
410 break;
411 }
412 case MCFragment::FT_DwarfFrame: {
413 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
414 OS << "\n ";
415 OS << " AddrDelta:" << CF->getAddrDelta();
416 break;
417 }
418 case MCFragment::FT_LEB: {
419 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
420 OS << "\n ";
421 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
422 break;
423 }
424 case MCFragment::FT_SafeSEH: {
425 const MCSafeSEHFragment *F = cast<MCSafeSEHFragment>(this);
426 OS << "\n ";
427 OS << " Sym:" << F->getSymbol();
428 break;
429 }
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000430 case MCFragment::FT_CVInlineLines: {
431 const auto *F = cast<MCCVInlineLineTableFragment>(this);
432 OS << "\n ";
433 OS << " Sym:" << *F->getFnStartSym();
434 break;
435 }
David Majnemer408b5e62016-02-05 01:55:49 +0000436 case MCFragment::FT_CVDefRange: {
437 const auto *F = cast<MCCVDefRangeFragment>(this);
438 OS << "\n ";
439 for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
440 F->getRanges()) {
441 OS << " RangeStart:" << RangeStartEnd.first;
442 OS << " RangeEnd:" << RangeStartEnd.second;
443 }
444 break;
445 }
Chandler Carruth8d736232015-12-29 09:06:16 +0000446 case MCFragment::FT_Dummy:
447 break;
448 }
449 OS << ">";
450}
451
Yaron Kereneb2a2542016-01-29 20:50:44 +0000452LLVM_DUMP_METHOD void MCAssembler::dump() {
Chandler Carruth8d736232015-12-29 09:06:16 +0000453 raw_ostream &OS = llvm::errs();
454
455 OS << "<MCAssembler\n";
456 OS << " Sections:[\n ";
457 for (iterator it = begin(), ie = end(); it != ie; ++it) {
458 if (it != begin()) OS << ",\n ";
459 it->dump();
460 }
461 OS << "],\n";
462 OS << " Symbols:[";
463
464 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
465 if (it != symbol_begin()) OS << ",\n ";
466 OS << "(";
467 it->dump();
468 OS << ", Index:" << it->getIndex() << ", ";
469 OS << ")";
470 }
471 OS << "]>\n";
472}