1 //===- lib/MC/MCAssembler.cpp - Assembler Backend 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/MCAssembler.h"
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCAsmLayout.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCDwarf.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCFixupKindInfo.h"
22 #include "llvm/MC/MCObjectWriter.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCValue.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/LEB128.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <tuple>
33 using namespace llvm;
34
35 #define DEBUG_TYPE "assembler"
36
37 namespace {
38 namespace stats {
39 STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
40 STATISTIC(EmittedRelaxableFragments,
41 "Number of emitted assembler fragments - relaxable");
42 STATISTIC(EmittedDataFragments,
43 "Number of emitted assembler fragments - data");
44 STATISTIC(EmittedCompactEncodedInstFragments,
45 "Number of emitted assembler fragments - compact encoded inst");
46 STATISTIC(EmittedAlignFragments,
47 "Number of emitted assembler fragments - align");
48 STATISTIC(EmittedFillFragments,
49 "Number of emitted assembler fragments - fill");
50 STATISTIC(EmittedOrgFragments,
51 "Number of emitted assembler fragments - org");
52 STATISTIC(evaluateFixup, "Number of evaluated fixups");
53 STATISTIC(FragmentLayouts, "Number of fragment layouts");
54 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
55 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
56 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
57 }
58 }
59
60 // FIXME FIXME FIXME: There are number of places in this file where we convert
61 // what is a 64-bit assembler value used for computation into a value in the
62 // object file, which may truncate it. We should detect that truncation where
63 // invalid and report errors back.
64
65 /* *** */
66
MCAsmLayout(MCAssembler & Asm)67 MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
68 : Assembler(Asm), LastValidFragment()
69 {
70 // Compute the section layout order. Virtual sections must go last.
71 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
72 if (!it->getSection().isVirtualSection())
73 SectionOrder.push_back(&*it);
74 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
75 if (it->getSection().isVirtualSection())
76 SectionOrder.push_back(&*it);
77 }
78
isFragmentValid(const MCFragment * F) const79 bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
80 const MCSectionData &SD = *F->getParent();
81 const MCFragment *LastValid = LastValidFragment.lookup(&SD);
82 if (!LastValid)
83 return false;
84 assert(LastValid->getParent() == F->getParent());
85 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
86 }
87
invalidateFragmentsFrom(MCFragment * F)88 void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
89 // If this fragment wasn't already valid, we don't need to do anything.
90 if (!isFragmentValid(F))
91 return;
92
93 // Otherwise, reset the last valid fragment to the previous fragment
94 // (if this is the first fragment, it will be NULL).
95 const MCSectionData &SD = *F->getParent();
96 LastValidFragment[&SD] = F->getPrevNode();
97 }
98
ensureValid(const MCFragment * F) const99 void MCAsmLayout::ensureValid(const MCFragment *F) const {
100 MCSectionData &SD = *F->getParent();
101
102 MCFragment *Cur = LastValidFragment[&SD];
103 if (!Cur)
104 Cur = &*SD.begin();
105 else
106 Cur = Cur->getNextNode();
107
108 // Advance the layout position until the fragment is valid.
109 while (!isFragmentValid(F)) {
110 assert(Cur && "Layout bookkeeping error");
111 const_cast<MCAsmLayout*>(this)->layoutFragment(Cur);
112 Cur = Cur->getNextNode();
113 }
114 }
115
getFragmentOffset(const MCFragment * F) const116 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
117 ensureValid(F);
118 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
119 return F->Offset;
120 }
121
122 // Simple getSymbolOffset helper for the non-varibale case.
getLabelOffset(const MCAsmLayout & Layout,const MCSymbolData & SD,bool ReportError,uint64_t & Val)123 static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbolData &SD,
124 bool ReportError, uint64_t &Val) {
125 if (!SD.getFragment()) {
126 if (ReportError)
127 report_fatal_error("unable to evaluate offset to undefined symbol '" +
128 SD.getSymbol().getName() + "'");
129 return false;
130 }
131 Val = Layout.getFragmentOffset(SD.getFragment()) + SD.getOffset();
132 return true;
133 }
134
getSymbolOffsetImpl(const MCAsmLayout & Layout,const MCSymbolData * SD,bool ReportError,uint64_t & Val)135 static bool getSymbolOffsetImpl(const MCAsmLayout &Layout,
136 const MCSymbolData *SD, bool ReportError,
137 uint64_t &Val) {
138 const MCSymbol &S = SD->getSymbol();
139
140 if (!S.isVariable())
141 return getLabelOffset(Layout, *SD, ReportError, Val);
142
143 // If SD is a variable, evaluate it.
144 MCValue Target;
145 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, &Layout, nullptr))
146 report_fatal_error("unable to evaluate offset for variable '" +
147 S.getName() + "'");
148
149 uint64_t Offset = Target.getConstant();
150
151 const MCAssembler &Asm = Layout.getAssembler();
152
153 const MCSymbolRefExpr *A = Target.getSymA();
154 if (A) {
155 uint64_t ValA;
156 if (!getLabelOffset(Layout, Asm.getSymbolData(A->getSymbol()), ReportError,
157 ValA))
158 return false;
159 Offset += ValA;
160 }
161
162 const MCSymbolRefExpr *B = Target.getSymB();
163 if (B) {
164 uint64_t ValB;
165 if (!getLabelOffset(Layout, Asm.getSymbolData(B->getSymbol()), ReportError,
166 ValB))
167 return false;
168 Offset -= ValB;
169 }
170
171 Val = Offset;
172 return true;
173 }
174
getSymbolOffset(const MCSymbolData * SD,uint64_t & Val) const175 bool MCAsmLayout::getSymbolOffset(const MCSymbolData *SD, uint64_t &Val) const {
176 return getSymbolOffsetImpl(*this, SD, false, Val);
177 }
178
getSymbolOffset(const MCSymbolData * SD) const179 uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
180 uint64_t Val;
181 getSymbolOffsetImpl(*this, SD, true, Val);
182 return Val;
183 }
184
getBaseSymbol(const MCSymbol & Symbol) const185 const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
186 if (!Symbol.isVariable())
187 return &Symbol;
188
189 const MCExpr *Expr = Symbol.getVariableValue();
190 MCValue Value;
191 if (!Expr->evaluateAsValue(Value, *this))
192 llvm_unreachable("Invalid Expression");
193
194 const MCSymbolRefExpr *RefB = Value.getSymB();
195 if (RefB)
196 Assembler.getContext().FatalError(
197 SMLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
198 "' could not be evaluated in a subtraction expression");
199
200 const MCSymbolRefExpr *A = Value.getSymA();
201 if (!A)
202 return nullptr;
203
204 const MCSymbol &ASym = A->getSymbol();
205 const MCAssembler &Asm = getAssembler();
206 const MCSymbolData &ASD = Asm.getSymbolData(ASym);
207 if (ASD.isCommon()) {
208 // FIXME: we should probably add a SMLoc to MCExpr.
209 Asm.getContext().FatalError(SMLoc(),
210 "Common symbol " + ASym.getName() +
211 " cannot be used in assignment expr");
212 }
213
214 return &ASym;
215 }
216
getSectionAddressSize(const MCSectionData * SD) const217 uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
218 // The size is the last fragment's end offset.
219 const MCFragment &F = SD->getFragmentList().back();
220 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
221 }
222
getSectionFileSize(const MCSectionData * SD) const223 uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
224 // Virtual sections have no file size.
225 if (SD->getSection().isVirtualSection())
226 return 0;
227
228 // Otherwise, the file size is the same as the address space size.
229 return getSectionAddressSize(SD);
230 }
231
computeBundlePadding(const MCAssembler & Assembler,const MCFragment * F,uint64_t FOffset,uint64_t FSize)232 uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
233 const MCFragment *F,
234 uint64_t FOffset, uint64_t FSize) {
235 uint64_t BundleSize = Assembler.getBundleAlignSize();
236 assert(BundleSize > 0 &&
237 "computeBundlePadding should only be called if bundling is enabled");
238 uint64_t BundleMask = BundleSize - 1;
239 uint64_t OffsetInBundle = FOffset & BundleMask;
240 uint64_t EndOfFragment = OffsetInBundle + FSize;
241
242 // There are two kinds of bundling restrictions:
243 //
244 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
245 // *end* on a bundle boundary.
246 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
247 // would, add padding until the end of the bundle so that the fragment
248 // will start in a new one.
249 if (F->alignToBundleEnd()) {
250 // Three possibilities here:
251 //
252 // A) The fragment just happens to end at a bundle boundary, so we're good.
253 // B) The fragment ends before the current bundle boundary: pad it just
254 // enough to reach the boundary.
255 // C) The fragment ends after the current bundle boundary: pad it until it
256 // reaches the end of the next bundle boundary.
257 //
258 // Note: this code could be made shorter with some modulo trickery, but it's
259 // intentionally kept in its more explicit form for simplicity.
260 if (EndOfFragment == BundleSize)
261 return 0;
262 else if (EndOfFragment < BundleSize)
263 return BundleSize - EndOfFragment;
264 else { // EndOfFragment > BundleSize
265 return 2 * BundleSize - EndOfFragment;
266 }
267 } else if (EndOfFragment > BundleSize)
268 return BundleSize - OffsetInBundle;
269 else
270 return 0;
271 }
272
273 /* *** */
274
MCFragment()275 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
276 }
277
~MCFragment()278 MCFragment::~MCFragment() {
279 }
280
MCFragment(FragmentType Kind,MCSectionData * Parent)281 MCFragment::MCFragment(FragmentType Kind, MCSectionData *Parent)
282 : Kind(Kind), Parent(Parent), Atom(nullptr), Offset(~UINT64_C(0)) {
283 if (Parent)
284 Parent->getFragmentList().push_back(this);
285 }
286
287 /* *** */
288
~MCEncodedFragment()289 MCEncodedFragment::~MCEncodedFragment() {
290 }
291
292 /* *** */
293
~MCEncodedFragmentWithFixups()294 MCEncodedFragmentWithFixups::~MCEncodedFragmentWithFixups() {
295 }
296
297 /* *** */
298
MCSectionData()299 MCSectionData::MCSectionData() : Section(nullptr) {}
300
MCSectionData(const MCSection & Section,MCAssembler * A)301 MCSectionData::MCSectionData(const MCSection &Section, MCAssembler *A)
302 : Section(&Section), Ordinal(~UINT32_C(0)), Alignment(1),
303 BundleLockState(NotBundleLocked), BundleLockNestingDepth(0),
304 BundleGroupBeforeFirstInst(false), HasInstructions(false) {
305 if (A)
306 A->getSectionList().push_back(this);
307 }
308
309 MCSectionData::iterator
getSubsectionInsertionPoint(unsigned Subsection)310 MCSectionData::getSubsectionInsertionPoint(unsigned Subsection) {
311 if (Subsection == 0 && SubsectionFragmentMap.empty())
312 return end();
313
314 SmallVectorImpl<std::pair<unsigned, MCFragment *> >::iterator MI =
315 std::lower_bound(SubsectionFragmentMap.begin(), SubsectionFragmentMap.end(),
316 std::make_pair(Subsection, (MCFragment *)nullptr));
317 bool ExactMatch = false;
318 if (MI != SubsectionFragmentMap.end()) {
319 ExactMatch = MI->first == Subsection;
320 if (ExactMatch)
321 ++MI;
322 }
323 iterator IP;
324 if (MI == SubsectionFragmentMap.end())
325 IP = end();
326 else
327 IP = MI->second;
328 if (!ExactMatch && Subsection != 0) {
329 // The GNU as documentation claims that subsections have an alignment of 4,
330 // although this appears not to be the case.
331 MCFragment *F = new MCDataFragment();
332 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
333 getFragmentList().insert(IP, F);
334 F->setParent(this);
335 }
336
337 return IP;
338 }
339
setBundleLockState(BundleLockStateType NewState)340 void MCSectionData::setBundleLockState(BundleLockStateType NewState) {
341 if (NewState == NotBundleLocked) {
342 if (BundleLockNestingDepth == 0) {
343 report_fatal_error("Mismatched bundle_lock/unlock directives");
344 }
345 if (--BundleLockNestingDepth == 0) {
346 BundleLockState = NotBundleLocked;
347 }
348 return;
349 }
350
351 // If any of the directives is an align_to_end directive, the whole nested
352 // group is align_to_end. So don't downgrade from align_to_end to just locked.
353 if (BundleLockState != BundleLockedAlignToEnd) {
354 BundleLockState = NewState;
355 }
356 ++BundleLockNestingDepth;
357 }
358
359 /* *** */
360
MCSymbolData()361 MCSymbolData::MCSymbolData() : Symbol(nullptr) {}
362
MCSymbolData(const MCSymbol & Symbol,MCFragment * Fragment,uint64_t Offset,MCAssembler * A)363 MCSymbolData::MCSymbolData(const MCSymbol &Symbol, MCFragment *Fragment,
364 uint64_t Offset, MCAssembler *A)
365 : Symbol(&Symbol), Fragment(Fragment), Offset(Offset), SymbolSize(nullptr),
366 CommonAlign(-1U), Flags(0), Index(0) {
367 if (A)
368 A->getSymbolList().push_back(this);
369 }
370
371 /* *** */
372
MCAssembler(MCContext & Context_,MCAsmBackend & Backend_,MCCodeEmitter & Emitter_,MCObjectWriter & Writer_,raw_ostream & OS_)373 MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
374 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
375 raw_ostream &OS_)
376 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
377 OS(OS_), BundleAlignSize(0), RelaxAll(false),
378 SubsectionsViaSymbols(false), ELFHeaderEFlags(0) {
379 VersionMinInfo.Major = 0; // Major version == 0 for "none specified"
380 }
381
~MCAssembler()382 MCAssembler::~MCAssembler() {
383 }
384
reset()385 void MCAssembler::reset() {
386 Sections.clear();
387 Symbols.clear();
388 SectionMap.clear();
389 SymbolMap.clear();
390 IndirectSymbols.clear();
391 DataRegions.clear();
392 LinkerOptions.clear();
393 FileNames.clear();
394 ThumbFuncs.clear();
395 BundleAlignSize = 0;
396 RelaxAll = false;
397 SubsectionsViaSymbols = false;
398 ELFHeaderEFlags = 0;
399 LOHContainer.reset();
400 VersionMinInfo.Major = 0;
401
402 // reset objects owned by us
403 getBackend().reset();
404 getEmitter().reset();
405 getWriter().reset();
406 getLOHContainer().reset();
407 }
408
isThumbFunc(const MCSymbol * Symbol) const409 bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
410 if (ThumbFuncs.count(Symbol))
411 return true;
412
413 if (!Symbol->isVariable())
414 return false;
415
416 // FIXME: It looks like gas supports some cases of the form "foo + 2". It
417 // is not clear if that is a bug or a feature.
418 const MCExpr *Expr = Symbol->getVariableValue();
419 const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr);
420 if (!Ref)
421 return false;
422
423 if (Ref->getKind() != MCSymbolRefExpr::VK_None)
424 return false;
425
426 const MCSymbol &Sym = Ref->getSymbol();
427 if (!isThumbFunc(&Sym))
428 return false;
429
430 ThumbFuncs.insert(Symbol); // Cache it.
431 return true;
432 }
433
addLocalUsedInReloc(const MCSymbol & Sym)434 void MCAssembler::addLocalUsedInReloc(const MCSymbol &Sym) {
435 assert(Sym.isTemporary());
436 LocalsUsedInReloc.insert(&Sym);
437 }
438
isLocalUsedInReloc(const MCSymbol & Sym) const439 bool MCAssembler::isLocalUsedInReloc(const MCSymbol &Sym) const {
440 assert(Sym.isTemporary());
441 return LocalsUsedInReloc.count(&Sym);
442 }
443
isSymbolLinkerVisible(const MCSymbol & Symbol) const444 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
445 // Non-temporary labels should always be visible to the linker.
446 if (!Symbol.isTemporary())
447 return true;
448
449 // Absolute temporary labels are never visible.
450 if (!Symbol.isInSection())
451 return false;
452
453 if (isLocalUsedInReloc(Symbol))
454 return true;
455
456 return false;
457 }
458
getAtom(const MCSymbolData * SD) const459 const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
460 // Linker visible symbols define atoms.
461 if (isSymbolLinkerVisible(SD->getSymbol()))
462 return SD;
463
464 // Absolute and undefined symbols have no defining atom.
465 if (!SD->getFragment())
466 return nullptr;
467
468 // Non-linker visible symbols in sections which can't be atomized have no
469 // defining atom.
470 if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
471 SD->getFragment()->getParent()->getSection()))
472 return nullptr;
473
474 // Otherwise, return the atom for the containing fragment.
475 return SD->getFragment()->getAtom();
476 }
477
evaluateFixup(const MCAsmLayout & Layout,const MCFixup & Fixup,const MCFragment * DF,MCValue & Target,uint64_t & Value) const478 bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
479 const MCFixup &Fixup, const MCFragment *DF,
480 MCValue &Target, uint64_t &Value) const {
481 ++stats::evaluateFixup;
482
483 // FIXME: This code has some duplication with RecordRelocation. We should
484 // probably merge the two into a single callback that tries to evaluate a
485 // fixup and records a relocation if one is needed.
486 const MCExpr *Expr = Fixup.getValue();
487 if (!Expr->EvaluateAsRelocatable(Target, &Layout, &Fixup))
488 getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
489
490 bool IsPCRel = Backend.getFixupKindInfo(
491 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
492
493 bool IsResolved;
494 if (IsPCRel) {
495 if (Target.getSymB()) {
496 IsResolved = false;
497 } else if (!Target.getSymA()) {
498 IsResolved = false;
499 } else {
500 const MCSymbolRefExpr *A = Target.getSymA();
501 const MCSymbol &SA = A->getSymbol();
502 if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
503 IsResolved = false;
504 } else {
505 const MCSymbolData &DataA = getSymbolData(SA);
506 IsResolved = getWriter().IsSymbolRefDifferenceFullyResolvedImpl(
507 *this, DataA, nullptr, *DF, false, true);
508 }
509 }
510 } else {
511 IsResolved = Target.isAbsolute();
512 }
513
514 Value = Target.getConstant();
515
516 if (const MCSymbolRefExpr *A = Target.getSymA()) {
517 const MCSymbol &Sym = A->getSymbol();
518 if (Sym.isDefined())
519 Value += Layout.getSymbolOffset(&getSymbolData(Sym));
520 }
521 if (const MCSymbolRefExpr *B = Target.getSymB()) {
522 const MCSymbol &Sym = B->getSymbol();
523 if (Sym.isDefined())
524 Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
525 }
526
527
528 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
529 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
530 assert((ShouldAlignPC ? IsPCRel : true) &&
531 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
532
533 if (IsPCRel) {
534 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
535
536 // A number of ARM fixups in Thumb mode require that the effective PC
537 // address be determined as the 32-bit aligned version of the actual offset.
538 if (ShouldAlignPC) Offset &= ~0x3;
539 Value -= Offset;
540 }
541
542 // Let the backend adjust the fixup value if necessary, including whether
543 // we need a relocation.
544 Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
545 IsResolved);
546
547 return IsResolved;
548 }
549
computeFragmentSize(const MCAsmLayout & Layout,const MCFragment & F) const550 uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
551 const MCFragment &F) const {
552 switch (F.getKind()) {
553 case MCFragment::FT_Data:
554 case MCFragment::FT_Relaxable:
555 case MCFragment::FT_CompactEncodedInst:
556 return cast<MCEncodedFragment>(F).getContents().size();
557 case MCFragment::FT_Fill:
558 return cast<MCFillFragment>(F).getSize();
559
560 case MCFragment::FT_LEB:
561 return cast<MCLEBFragment>(F).getContents().size();
562
563 case MCFragment::FT_Align: {
564 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
565 unsigned Offset = Layout.getFragmentOffset(&AF);
566 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
567 // If we are padding with nops, force the padding to be larger than the
568 // minimum nop size.
569 if (Size > 0 && AF.hasEmitNops()) {
570 while (Size % getBackend().getMinimumNopSize())
571 Size += AF.getAlignment();
572 }
573 if (Size > AF.getMaxBytesToEmit())
574 return 0;
575 return Size;
576 }
577
578 case MCFragment::FT_Org: {
579 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
580 int64_t TargetLocation;
581 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
582 report_fatal_error("expected assembly-time absolute expression");
583
584 // FIXME: We need a way to communicate this error.
585 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
586 int64_t Size = TargetLocation - FragmentOffset;
587 if (Size < 0 || Size >= 0x40000000)
588 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
589 "' (at offset '" + Twine(FragmentOffset) + "')");
590 return Size;
591 }
592
593 case MCFragment::FT_Dwarf:
594 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
595 case MCFragment::FT_DwarfFrame:
596 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
597 }
598
599 llvm_unreachable("invalid fragment kind");
600 }
601
layoutFragment(MCFragment * F)602 void MCAsmLayout::layoutFragment(MCFragment *F) {
603 MCFragment *Prev = F->getPrevNode();
604
605 // We should never try to recompute something which is valid.
606 assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
607 // We should never try to compute the fragment layout if its predecessor
608 // isn't valid.
609 assert((!Prev || isFragmentValid(Prev)) &&
610 "Attempt to compute fragment before its predecessor!");
611
612 ++stats::FragmentLayouts;
613
614 // Compute fragment offset and size.
615 if (Prev)
616 F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
617 else
618 F->Offset = 0;
619 LastValidFragment[F->getParent()] = F;
620
621 // If bundling is enabled and this fragment has instructions in it, it has to
622 // obey the bundling restrictions. With padding, we'll have:
623 //
624 //
625 // BundlePadding
626 // |||
627 // -------------------------------------
628 // Prev |##########| F |
629 // -------------------------------------
630 // ^
631 // |
632 // F->Offset
633 //
634 // The fragment's offset will point to after the padding, and its computed
635 // size won't include the padding.
636 //
637 // When the -mc-relax-all flag is used, we optimize bundling by writting the
638 // bundle padding directly into fragments when the instructions are emitted
639 // inside the streamer.
640 //
641 if (Assembler.isBundlingEnabled() && !Assembler.getRelaxAll() &&
642 F->hasInstructions()) {
643 assert(isa<MCEncodedFragment>(F) &&
644 "Only MCEncodedFragment implementations have instructions");
645 uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
646
647 if (FSize > Assembler.getBundleAlignSize())
648 report_fatal_error("Fragment can't be larger than a bundle size");
649
650 uint64_t RequiredBundlePadding = computeBundlePadding(Assembler, F,
651 F->Offset, FSize);
652 if (RequiredBundlePadding > UINT8_MAX)
653 report_fatal_error("Padding cannot exceed 255 bytes");
654 F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
655 F->Offset += RequiredBundlePadding;
656 }
657 }
658
659 /// \brief Write the contents of a fragment to the given object writer. Expects
660 /// a MCEncodedFragment.
writeFragmentContents(const MCFragment & F,MCObjectWriter * OW)661 static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) {
662 const MCEncodedFragment &EF = cast<MCEncodedFragment>(F);
663 OW->WriteBytes(EF.getContents());
664 }
665
writeFragmentPadding(const MCFragment & F,uint64_t FSize,MCObjectWriter * OW) const666 void MCAssembler::writeFragmentPadding(const MCFragment &F, uint64_t FSize,
667 MCObjectWriter *OW) const {
668 // Should NOP padding be written out before this fragment?
669 unsigned BundlePadding = F.getBundlePadding();
670 if (BundlePadding > 0) {
671 assert(isBundlingEnabled() &&
672 "Writing bundle padding with disabled bundling");
673 assert(F.hasInstructions() &&
674 "Writing bundle padding for a fragment without instructions");
675
676 unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
677 if (F.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
678 // If the padding itself crosses a bundle boundary, it must be emitted
679 // in 2 pieces, since even nop instructions must not cross boundaries.
680 // v--------------v <- BundleAlignSize
681 // v---------v <- BundlePadding
682 // ----------------------------
683 // | Prev |####|####| F |
684 // ----------------------------
685 // ^-------------------^ <- TotalLength
686 unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
687 if (!getBackend().writeNopData(DistanceToBoundary, OW))
688 report_fatal_error("unable to write NOP sequence of " +
689 Twine(DistanceToBoundary) + " bytes");
690 BundlePadding -= DistanceToBoundary;
691 }
692 if (!getBackend().writeNopData(BundlePadding, OW))
693 report_fatal_error("unable to write NOP sequence of " +
694 Twine(BundlePadding) + " bytes");
695 }
696 }
697
698 /// \brief Write the fragment \p F to the output file.
writeFragment(const MCAssembler & Asm,const MCAsmLayout & Layout,const MCFragment & F)699 static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
700 const MCFragment &F) {
701 MCObjectWriter *OW = &Asm.getWriter();
702
703 // FIXME: Embed in fragments instead?
704 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
705
706 Asm.writeFragmentPadding(F, FragmentSize, OW);
707
708 // This variable (and its dummy usage) is to participate in the assert at
709 // the end of the function.
710 uint64_t Start = OW->getStream().tell();
711 (void) Start;
712
713 ++stats::EmittedFragments;
714
715 switch (F.getKind()) {
716 case MCFragment::FT_Align: {
717 ++stats::EmittedAlignFragments;
718 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
719 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
720
721 uint64_t Count = FragmentSize / AF.getValueSize();
722
723 // FIXME: This error shouldn't actually occur (the front end should emit
724 // multiple .align directives to enforce the semantics it wants), but is
725 // severe enough that we want to report it. How to handle this?
726 if (Count * AF.getValueSize() != FragmentSize)
727 report_fatal_error("undefined .align directive, value size '" +
728 Twine(AF.getValueSize()) +
729 "' is not a divisor of padding size '" +
730 Twine(FragmentSize) + "'");
731
732 // See if we are aligning with nops, and if so do that first to try to fill
733 // the Count bytes. Then if that did not fill any bytes or there are any
734 // bytes left to fill use the Value and ValueSize to fill the rest.
735 // If we are aligning with nops, ask that target to emit the right data.
736 if (AF.hasEmitNops()) {
737 if (!Asm.getBackend().writeNopData(Count, OW))
738 report_fatal_error("unable to write nop sequence of " +
739 Twine(Count) + " bytes");
740 break;
741 }
742
743 // Otherwise, write out in multiples of the value size.
744 for (uint64_t i = 0; i != Count; ++i) {
745 switch (AF.getValueSize()) {
746 default: llvm_unreachable("Invalid size!");
747 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
748 case 2: OW->Write16(uint16_t(AF.getValue())); break;
749 case 4: OW->Write32(uint32_t(AF.getValue())); break;
750 case 8: OW->Write64(uint64_t(AF.getValue())); break;
751 }
752 }
753 break;
754 }
755
756 case MCFragment::FT_Data:
757 ++stats::EmittedDataFragments;
758 writeFragmentContents(F, OW);
759 break;
760
761 case MCFragment::FT_Relaxable:
762 ++stats::EmittedRelaxableFragments;
763 writeFragmentContents(F, OW);
764 break;
765
766 case MCFragment::FT_CompactEncodedInst:
767 ++stats::EmittedCompactEncodedInstFragments;
768 writeFragmentContents(F, OW);
769 break;
770
771 case MCFragment::FT_Fill: {
772 ++stats::EmittedFillFragments;
773 const MCFillFragment &FF = cast<MCFillFragment>(F);
774
775 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
776
777 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
778 switch (FF.getValueSize()) {
779 default: llvm_unreachable("Invalid size!");
780 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
781 case 2: OW->Write16(uint16_t(FF.getValue())); break;
782 case 4: OW->Write32(uint32_t(FF.getValue())); break;
783 case 8: OW->Write64(uint64_t(FF.getValue())); break;
784 }
785 }
786 break;
787 }
788
789 case MCFragment::FT_LEB: {
790 const MCLEBFragment &LF = cast<MCLEBFragment>(F);
791 OW->WriteBytes(LF.getContents());
792 break;
793 }
794
795 case MCFragment::FT_Org: {
796 ++stats::EmittedOrgFragments;
797 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
798
799 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
800 OW->Write8(uint8_t(OF.getValue()));
801
802 break;
803 }
804
805 case MCFragment::FT_Dwarf: {
806 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
807 OW->WriteBytes(OF.getContents());
808 break;
809 }
810 case MCFragment::FT_DwarfFrame: {
811 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
812 OW->WriteBytes(CF.getContents());
813 break;
814 }
815 }
816
817 assert(OW->getStream().tell() - Start == FragmentSize &&
818 "The stream should advance by fragment size");
819 }
820
writeSectionData(const MCSectionData * SD,const MCAsmLayout & Layout) const821 void MCAssembler::writeSectionData(const MCSectionData *SD,
822 const MCAsmLayout &Layout) const {
823 // Ignore virtual sections.
824 if (SD->getSection().isVirtualSection()) {
825 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
826
827 // Check that contents are only things legal inside a virtual section.
828 for (MCSectionData::const_iterator it = SD->begin(),
829 ie = SD->end(); it != ie; ++it) {
830 switch (it->getKind()) {
831 default: llvm_unreachable("Invalid fragment in virtual section!");
832 case MCFragment::FT_Data: {
833 // Check that we aren't trying to write a non-zero contents (or fixups)
834 // into a virtual section. This is to support clients which use standard
835 // directives to fill the contents of virtual sections.
836 const MCDataFragment &DF = cast<MCDataFragment>(*it);
837 assert(DF.fixup_begin() == DF.fixup_end() &&
838 "Cannot have fixups in virtual section!");
839 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
840 if (DF.getContents()[i]) {
841 if (auto *ELFSec = dyn_cast<const MCSectionELF>(&SD->getSection()))
842 report_fatal_error("non-zero initializer found in section '" +
843 ELFSec->getSectionName() + "'");
844 else
845 report_fatal_error("non-zero initializer found in virtual section");
846 }
847 break;
848 }
849 case MCFragment::FT_Align:
850 // Check that we aren't trying to write a non-zero value into a virtual
851 // section.
852 assert((cast<MCAlignFragment>(it)->getValueSize() == 0 ||
853 cast<MCAlignFragment>(it)->getValue() == 0) &&
854 "Invalid align in virtual section!");
855 break;
856 case MCFragment::FT_Fill:
857 assert((cast<MCFillFragment>(it)->getValueSize() == 0 ||
858 cast<MCFillFragment>(it)->getValue() == 0) &&
859 "Invalid fill in virtual section!");
860 break;
861 }
862 }
863
864 return;
865 }
866
867 uint64_t Start = getWriter().getStream().tell();
868 (void)Start;
869
870 for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end();
871 it != ie; ++it)
872 writeFragment(*this, Layout, *it);
873
874 assert(getWriter().getStream().tell() - Start ==
875 Layout.getSectionAddressSize(SD));
876 }
877
handleFixup(const MCAsmLayout & Layout,MCFragment & F,const MCFixup & Fixup)878 std::pair<uint64_t, bool> MCAssembler::handleFixup(const MCAsmLayout &Layout,
879 MCFragment &F,
880 const MCFixup &Fixup) {
881 // Evaluate the fixup.
882 MCValue Target;
883 uint64_t FixedValue;
884 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
885 MCFixupKindInfo::FKF_IsPCRel;
886 if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
887 // The fixup was unresolved, we need a relocation. Inform the object
888 // writer of the relocation, and give it an opportunity to adjust the
889 // fixup value if need be.
890 getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, IsPCRel,
891 FixedValue);
892 }
893 return std::make_pair(FixedValue, IsPCRel);
894 }
895
Finish()896 void MCAssembler::Finish() {
897 DEBUG_WITH_TYPE("mc-dump", {
898 llvm::errs() << "assembler backend - pre-layout\n--\n";
899 dump(); });
900
901 // Create the layout object.
902 MCAsmLayout Layout(*this);
903
904 // Create dummy fragments and assign section ordinals.
905 unsigned SectionIndex = 0;
906 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
907 // Create dummy fragments to eliminate any empty sections, this simplifies
908 // layout.
909 if (it->getFragmentList().empty())
910 new MCDataFragment(it);
911
912 it->setOrdinal(SectionIndex++);
913 }
914
915 // Assign layout order indices to sections and fragments.
916 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
917 MCSectionData *SD = Layout.getSectionOrder()[i];
918 SD->setLayoutOrder(i);
919
920 unsigned FragmentIndex = 0;
921 for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end();
922 iFrag != iFragEnd; ++iFrag)
923 iFrag->setLayoutOrder(FragmentIndex++);
924 }
925
926 // Layout until everything fits.
927 while (layoutOnce(Layout))
928 continue;
929
930 DEBUG_WITH_TYPE("mc-dump", {
931 llvm::errs() << "assembler backend - post-relaxation\n--\n";
932 dump(); });
933
934 // Finalize the layout, including fragment lowering.
935 finishLayout(Layout);
936
937 DEBUG_WITH_TYPE("mc-dump", {
938 llvm::errs() << "assembler backend - final-layout\n--\n";
939 dump(); });
940
941 uint64_t StartOffset = OS.tell();
942
943 // Allow the object writer a chance to perform post-layout binding (for
944 // example, to set the index fields in the symbol data).
945 getWriter().ExecutePostLayoutBinding(*this, Layout);
946
947 // Evaluate and apply the fixups, generating relocation entries as necessary.
948 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
949 for (MCSectionData::iterator it2 = it->begin(),
950 ie2 = it->end(); it2 != ie2; ++it2) {
951 MCEncodedFragmentWithFixups *F =
952 dyn_cast<MCEncodedFragmentWithFixups>(it2);
953 if (F) {
954 for (MCEncodedFragmentWithFixups::fixup_iterator it3 = F->fixup_begin(),
955 ie3 = F->fixup_end(); it3 != ie3; ++it3) {
956 MCFixup &Fixup = *it3;
957 uint64_t FixedValue;
958 bool IsPCRel;
959 std::tie(FixedValue, IsPCRel) = handleFixup(Layout, *F, Fixup);
960 getBackend().applyFixup(Fixup, F->getContents().data(),
961 F->getContents().size(), FixedValue, IsPCRel);
962 }
963 }
964 }
965 }
966
967 // Write the object file.
968 getWriter().WriteObject(*this, Layout);
969
970 stats::ObjectBytes += OS.tell() - StartOffset;
971 }
972
fixupNeedsRelaxation(const MCFixup & Fixup,const MCRelaxableFragment * DF,const MCAsmLayout & Layout) const973 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
974 const MCRelaxableFragment *DF,
975 const MCAsmLayout &Layout) const {
976 // If we cannot resolve the fixup value, it requires relaxation.
977 MCValue Target;
978 uint64_t Value;
979 if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
980 return true;
981
982 return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
983 }
984
fragmentNeedsRelaxation(const MCRelaxableFragment * F,const MCAsmLayout & Layout) const985 bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
986 const MCAsmLayout &Layout) const {
987 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
988 // are intentionally pushing out inst fragments, or because we relaxed a
989 // previous instruction to one that doesn't need relaxation.
990 if (!getBackend().mayNeedRelaxation(F->getInst()))
991 return false;
992
993 for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(),
994 ie = F->fixup_end(); it != ie; ++it)
995 if (fixupNeedsRelaxation(*it, F, Layout))
996 return true;
997
998 return false;
999 }
1000
relaxInstruction(MCAsmLayout & Layout,MCRelaxableFragment & F)1001 bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
1002 MCRelaxableFragment &F) {
1003 if (!fragmentNeedsRelaxation(&F, Layout))
1004 return false;
1005
1006 ++stats::RelaxedInstructions;
1007
1008 // FIXME-PERF: We could immediately lower out instructions if we can tell
1009 // they are fully resolved, to avoid retesting on later passes.
1010
1011 // Relax the fragment.
1012
1013 MCInst Relaxed;
1014 getBackend().relaxInstruction(F.getInst(), Relaxed);
1015
1016 // Encode the new instruction.
1017 //
1018 // FIXME-PERF: If it matters, we could let the target do this. It can
1019 // probably do so more efficiently in many cases.
1020 SmallVector<MCFixup, 4> Fixups;
1021 SmallString<256> Code;
1022 raw_svector_ostream VecOS(Code);
1023 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups, F.getSubtargetInfo());
1024 VecOS.flush();
1025
1026 // Update the fragment.
1027 F.setInst(Relaxed);
1028 F.getContents() = Code;
1029 F.getFixups() = Fixups;
1030
1031 return true;
1032 }
1033
relaxLEB(MCAsmLayout & Layout,MCLEBFragment & LF)1034 bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
1035 uint64_t OldSize = LF.getContents().size();
1036 int64_t Value;
1037 bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout);
1038 if (!Abs)
1039 report_fatal_error("sleb128 and uleb128 expressions must be absolute");
1040 SmallString<8> &Data = LF.getContents();
1041 Data.clear();
1042 raw_svector_ostream OSE(Data);
1043 if (LF.isSigned())
1044 encodeSLEB128(Value, OSE);
1045 else
1046 encodeULEB128(Value, OSE);
1047 OSE.flush();
1048 return OldSize != LF.getContents().size();
1049 }
1050
relaxDwarfLineAddr(MCAsmLayout & Layout,MCDwarfLineAddrFragment & DF)1051 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
1052 MCDwarfLineAddrFragment &DF) {
1053 MCContext &Context = Layout.getAssembler().getContext();
1054 uint64_t OldSize = DF.getContents().size();
1055 int64_t AddrDelta;
1056 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
1057 assert(Abs && "We created a line delta with an invalid expression");
1058 (void) Abs;
1059 int64_t LineDelta;
1060 LineDelta = DF.getLineDelta();
1061 SmallString<8> &Data = DF.getContents();
1062 Data.clear();
1063 raw_svector_ostream OSE(Data);
1064 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OSE);
1065 OSE.flush();
1066 return OldSize != Data.size();
1067 }
1068
relaxDwarfCallFrameFragment(MCAsmLayout & Layout,MCDwarfCallFrameFragment & DF)1069 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
1070 MCDwarfCallFrameFragment &DF) {
1071 MCContext &Context = Layout.getAssembler().getContext();
1072 uint64_t OldSize = DF.getContents().size();
1073 int64_t AddrDelta;
1074 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
1075 assert(Abs && "We created call frame with an invalid expression");
1076 (void) Abs;
1077 SmallString<8> &Data = DF.getContents();
1078 Data.clear();
1079 raw_svector_ostream OSE(Data);
1080 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE);
1081 OSE.flush();
1082 return OldSize != Data.size();
1083 }
1084
layoutSectionOnce(MCAsmLayout & Layout,MCSectionData & SD)1085 bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) {
1086 // Holds the first fragment which needed relaxing during this layout. It will
1087 // remain NULL if none were relaxed.
1088 // When a fragment is relaxed, all the fragments following it should get
1089 // invalidated because their offset is going to change.
1090 MCFragment *FirstRelaxedFragment = nullptr;
1091
1092 // Attempt to relax all the fragments in the section.
1093 for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) {
1094 // Check if this is a fragment that needs relaxation.
1095 bool RelaxedFrag = false;
1096 switch(I->getKind()) {
1097 default:
1098 break;
1099 case MCFragment::FT_Relaxable:
1100 assert(!getRelaxAll() &&
1101 "Did not expect a MCRelaxableFragment in RelaxAll mode");
1102 RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
1103 break;
1104 case MCFragment::FT_Dwarf:
1105 RelaxedFrag = relaxDwarfLineAddr(Layout,
1106 *cast<MCDwarfLineAddrFragment>(I));
1107 break;
1108 case MCFragment::FT_DwarfFrame:
1109 RelaxedFrag =
1110 relaxDwarfCallFrameFragment(Layout,
1111 *cast<MCDwarfCallFrameFragment>(I));
1112 break;
1113 case MCFragment::FT_LEB:
1114 RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
1115 break;
1116 }
1117 if (RelaxedFrag && !FirstRelaxedFragment)
1118 FirstRelaxedFragment = I;
1119 }
1120 if (FirstRelaxedFragment) {
1121 Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
1122 return true;
1123 }
1124 return false;
1125 }
1126
layoutOnce(MCAsmLayout & Layout)1127 bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
1128 ++stats::RelaxationSteps;
1129
1130 bool WasRelaxed = false;
1131 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1132 MCSectionData &SD = *it;
1133 while (layoutSectionOnce(Layout, SD))
1134 WasRelaxed = true;
1135 }
1136
1137 return WasRelaxed;
1138 }
1139
finishLayout(MCAsmLayout & Layout)1140 void MCAssembler::finishLayout(MCAsmLayout &Layout) {
1141 // The layout is done. Mark every fragment as valid.
1142 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
1143 Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
1144 }
1145 }
1146
1147 // Debugging methods
1148
1149 namespace llvm {
1150
operator <<(raw_ostream & OS,const MCFixup & AF)1151 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
1152 OS << "<MCFixup" << " Offset:" << AF.getOffset()
1153 << " Value:" << *AF.getValue()
1154 << " Kind:" << AF.getKind() << ">";
1155 return OS;
1156 }
1157
1158 }
1159
1160 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump()1161 void MCFragment::dump() {
1162 raw_ostream &OS = llvm::errs();
1163
1164 OS << "<";
1165 switch (getKind()) {
1166 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
1167 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
1168 case MCFragment::FT_CompactEncodedInst:
1169 OS << "MCCompactEncodedInstFragment"; break;
1170 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
1171 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
1172 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
1173 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
1174 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
1175 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
1176 }
1177
1178 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
1179 << " Offset:" << Offset
1180 << " HasInstructions:" << hasInstructions()
1181 << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">";
1182
1183 switch (getKind()) {
1184 case MCFragment::FT_Align: {
1185 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
1186 if (AF->hasEmitNops())
1187 OS << " (emit nops)";
1188 OS << "\n ";
1189 OS << " Alignment:" << AF->getAlignment()
1190 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
1191 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
1192 break;
1193 }
1194 case MCFragment::FT_Data: {
1195 const MCDataFragment *DF = cast<MCDataFragment>(this);
1196 OS << "\n ";
1197 OS << " Contents:[";
1198 const SmallVectorImpl<char> &Contents = DF->getContents();
1199 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
1200 if (i) OS << ",";
1201 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1202 }
1203 OS << "] (" << Contents.size() << " bytes)";
1204
1205 if (DF->fixup_begin() != DF->fixup_end()) {
1206 OS << ",\n ";
1207 OS << " Fixups:[";
1208 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
1209 ie = DF->fixup_end(); it != ie; ++it) {
1210 if (it != DF->fixup_begin()) OS << ",\n ";
1211 OS << *it;
1212 }
1213 OS << "]";
1214 }
1215 break;
1216 }
1217 case MCFragment::FT_CompactEncodedInst: {
1218 const MCCompactEncodedInstFragment *CEIF =
1219 cast<MCCompactEncodedInstFragment>(this);
1220 OS << "\n ";
1221 OS << " Contents:[";
1222 const SmallVectorImpl<char> &Contents = CEIF->getContents();
1223 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
1224 if (i) OS << ",";
1225 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1226 }
1227 OS << "] (" << Contents.size() << " bytes)";
1228 break;
1229 }
1230 case MCFragment::FT_Fill: {
1231 const MCFillFragment *FF = cast<MCFillFragment>(this);
1232 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
1233 << " Size:" << FF->getSize();
1234 break;
1235 }
1236 case MCFragment::FT_Relaxable: {
1237 const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
1238 OS << "\n ";
1239 OS << " Inst:";
1240 F->getInst().dump_pretty(OS);
1241 break;
1242 }
1243 case MCFragment::FT_Org: {
1244 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
1245 OS << "\n ";
1246 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
1247 break;
1248 }
1249 case MCFragment::FT_Dwarf: {
1250 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
1251 OS << "\n ";
1252 OS << " AddrDelta:" << OF->getAddrDelta()
1253 << " LineDelta:" << OF->getLineDelta();
1254 break;
1255 }
1256 case MCFragment::FT_DwarfFrame: {
1257 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
1258 OS << "\n ";
1259 OS << " AddrDelta:" << CF->getAddrDelta();
1260 break;
1261 }
1262 case MCFragment::FT_LEB: {
1263 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
1264 OS << "\n ";
1265 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
1266 break;
1267 }
1268 }
1269 OS << ">";
1270 }
1271
dump()1272 void MCSectionData::dump() {
1273 raw_ostream &OS = llvm::errs();
1274
1275 OS << "<MCSectionData";
1276 OS << " Alignment:" << getAlignment()
1277 << " Fragments:[\n ";
1278 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1279 if (it != begin()) OS << ",\n ";
1280 it->dump();
1281 }
1282 OS << "]>";
1283 }
1284
dump() const1285 void MCSymbolData::dump() const {
1286 raw_ostream &OS = llvm::errs();
1287
1288 OS << "<MCSymbolData Symbol:" << getSymbol()
1289 << " Fragment:" << getFragment();
1290 if (!isCommon())
1291 OS << " Offset:" << getOffset();
1292 OS << " Flags:" << getFlags() << " Index:" << getIndex();
1293 if (isCommon())
1294 OS << " (common, size:" << getCommonSize()
1295 << " align: " << getCommonAlignment() << ")";
1296 if (isExternal())
1297 OS << " (external)";
1298 if (isPrivateExtern())
1299 OS << " (private extern)";
1300 OS << ">";
1301 }
1302
dump()1303 void MCAssembler::dump() {
1304 raw_ostream &OS = llvm::errs();
1305
1306 OS << "<MCAssembler\n";
1307 OS << " Sections:[\n ";
1308 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1309 if (it != begin()) OS << ",\n ";
1310 it->dump();
1311 }
1312 OS << "],\n";
1313 OS << " Symbols:[";
1314
1315 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1316 if (it != symbol_begin()) OS << ",\n ";
1317 it->dump();
1318 }
1319 OS << "]>\n";
1320 }
1321 #endif
1322
1323 // anchors for MC*Fragment vtables
anchor()1324 void MCEncodedFragment::anchor() { }
anchor()1325 void MCEncodedFragmentWithFixups::anchor() { }
anchor()1326 void MCDataFragment::anchor() { }
anchor()1327 void MCCompactEncodedInstFragment::anchor() { }
anchor()1328 void MCRelaxableFragment::anchor() { }
anchor()1329 void MCAlignFragment::anchor() { }
anchor()1330 void MCFillFragment::anchor() { }
anchor()1331 void MCOrgFragment::anchor() { }
anchor()1332 void MCLEBFragment::anchor() { }
anchor()1333 void MCDwarfLineAddrFragment::anchor() { }
anchor()1334 void MCDwarfCallFrameFragment::anchor() { }
1335