1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCAssembler.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCCodeView.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDwarf.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCFixup.h"
26 #include "llvm/MC/MCFixupKindInfo.h"
27 #include "llvm/MC/MCFragment.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCObjectWriter.h"
30 #include "llvm/MC/MCSection.h"
31 #include "llvm/MC/MCSectionELF.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCValue.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/LEB128.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/raw_ostream.h"
48 #define DEBUG_TYPE "assembler"
53 STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
54 STATISTIC(EmittedRelaxableFragments,
55 "Number of emitted assembler fragments - relaxable");
56 STATISTIC(EmittedDataFragments,
57 "Number of emitted assembler fragments - data");
58 STATISTIC(EmittedCompactEncodedInstFragments,
59 "Number of emitted assembler fragments - compact encoded inst");
60 STATISTIC(EmittedAlignFragments,
61 "Number of emitted assembler fragments - align");
62 STATISTIC(EmittedFillFragments,
63 "Number of emitted assembler fragments - fill");
64 STATISTIC(EmittedOrgFragments,
65 "Number of emitted assembler fragments - org");
66 STATISTIC(evaluateFixup, "Number of evaluated fixups");
67 STATISTIC(FragmentLayouts, "Number of fragment layouts");
68 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
69 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
70 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
71 STATISTIC(PaddingFragmentsRelaxations,
72 "Number of Padding Fragments relaxations");
73 STATISTIC(PaddingFragmentsBytes,
74 "Total size of all padding from adding Fragments");
76 } // end namespace stats
77 } // end anonymous namespace
79 // FIXME FIXME FIXME: There are number of places in this file where we convert
80 // what is a 64-bit assembler value used for computation into a value in the
81 // object file, which may truncate it. We should detect that truncation where
82 // invalid and report errors back.
86 MCAssembler::MCAssembler(MCContext &Context, MCAsmBackend &Backend,
87 MCCodeEmitter &Emitter, MCObjectWriter &Writer)
88 : Context(Context), Backend(Backend), Emitter(Emitter), Writer(Writer),
89 BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false),
90 IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) {
91 VersionInfo.Major = 0; // Major version == 0 for "none specified"
94 MCAssembler::~MCAssembler() = default;
96 void MCAssembler::reset() {
99 IndirectSymbols.clear();
101 LinkerOptions.clear();
106 SubsectionsViaSymbols = false;
107 IncrementalLinkerCompatible = false;
109 LOHContainer.reset();
110 VersionInfo.Major = 0;
112 // reset objects owned by us
113 getBackend().reset();
114 getEmitter().reset();
116 getLOHContainer().reset();
119 bool MCAssembler::registerSection(MCSection &Section) {
120 if (Section.isRegistered())
122 Sections.push_back(&Section);
123 Section.setIsRegistered(true);
127 bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
128 if (ThumbFuncs.count(Symbol))
131 if (!Symbol->isVariable())
134 const MCExpr *Expr = Symbol->getVariableValue();
137 if (!Expr->evaluateAsRelocatable(V, nullptr, nullptr))
140 if (V.getSymB() || V.getRefKind() != MCSymbolRefExpr::VK_None)
143 const MCSymbolRefExpr *Ref = V.getSymA();
147 if (Ref->getKind() != MCSymbolRefExpr::VK_None)
150 const MCSymbol &Sym = Ref->getSymbol();
151 if (!isThumbFunc(&Sym))
154 ThumbFuncs.insert(Symbol); // Cache it.
158 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
159 // Non-temporary labels should always be visible to the linker.
160 if (!Symbol.isTemporary())
163 // Absolute temporary labels are never visible.
164 if (!Symbol.isInSection())
167 if (Symbol.isUsedInReloc())
173 const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const {
174 // Linker visible symbols define atoms.
175 if (isSymbolLinkerVisible(S))
178 // Absolute and undefined symbols have no defining atom.
179 if (!S.isInSection())
182 // Non-linker visible symbols in sections which can't be atomized have no
184 if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
185 *S.getFragment()->getParent()))
188 // Otherwise, return the atom for the containing fragment.
189 return S.getFragment()->getAtom();
192 bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
193 const MCFixup &Fixup, const MCFragment *DF,
194 MCValue &Target, uint64_t &Value) const {
195 ++stats::evaluateFixup;
197 // FIXME: This code has some duplication with recordRelocation. We should
198 // probably merge the two into a single callback that tries to evaluate a
199 // fixup and records a relocation if one is needed.
201 // On error claim to have completely evaluated the fixup, to prevent any
202 // further processing from being done.
203 const MCExpr *Expr = Fixup.getValue();
204 MCContext &Ctx = getContext();
206 if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) {
207 Ctx.reportError(Fixup.getLoc(), "expected relocatable expression");
210 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
211 if (RefB->getKind() != MCSymbolRefExpr::VK_None) {
212 Ctx.reportError(Fixup.getLoc(),
213 "unsupported subtraction of qualified symbol");
218 bool IsPCRel = Backend.getFixupKindInfo(
219 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
223 if (Target.getSymB()) {
225 } else if (!Target.getSymA()) {
228 const MCSymbolRefExpr *A = Target.getSymA();
229 const MCSymbol &SA = A->getSymbol();
230 if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
233 IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl(
234 *this, SA, *DF, false, true);
238 IsResolved = Target.isAbsolute();
241 Value = Target.getConstant();
243 if (const MCSymbolRefExpr *A = Target.getSymA()) {
244 const MCSymbol &Sym = A->getSymbol();
246 Value += Layout.getSymbolOffset(Sym);
248 if (const MCSymbolRefExpr *B = Target.getSymB()) {
249 const MCSymbol &Sym = B->getSymbol();
251 Value -= Layout.getSymbolOffset(Sym);
254 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
255 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
256 assert((ShouldAlignPC ? IsPCRel : true) &&
257 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
260 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
262 // A number of ARM fixups in Thumb mode require that the effective PC
263 // address be determined as the 32-bit aligned version of the actual offset.
264 if (ShouldAlignPC) Offset &= ~0x3;
268 // Let the backend force a relocation if needed.
269 if (IsResolved && Backend.shouldForceRelocation(*this, Fixup, Target))
275 uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
276 const MCFragment &F) const {
277 switch (F.getKind()) {
278 case MCFragment::FT_Data:
279 return cast<MCDataFragment>(F).getContents().size();
280 case MCFragment::FT_Relaxable:
281 return cast<MCRelaxableFragment>(F).getContents().size();
282 case MCFragment::FT_CompactEncodedInst:
283 return cast<MCCompactEncodedInstFragment>(F).getContents().size();
284 case MCFragment::FT_Fill: {
285 auto &FF = cast<MCFillFragment>(F);
287 if (!FF.getSize().evaluateAsAbsolute(Size, Layout))
288 getContext().reportError(FF.getLoc(),
289 "expected assembly-time absolute expression");
291 getContext().reportError(FF.getLoc(), "invalid number of bytes");
297 case MCFragment::FT_LEB:
298 return cast<MCLEBFragment>(F).getContents().size();
300 case MCFragment::FT_Padding:
301 return cast<MCPaddingFragment>(F).getSize();
303 case MCFragment::FT_SymbolId:
306 case MCFragment::FT_Align: {
307 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
308 unsigned Offset = Layout.getFragmentOffset(&AF);
309 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
310 // If we are padding with nops, force the padding to be larger than the
312 if (Size > 0 && AF.hasEmitNops()) {
313 while (Size % getBackend().getMinimumNopSize())
314 Size += AF.getAlignment();
316 if (Size > AF.getMaxBytesToEmit())
321 case MCFragment::FT_Org: {
322 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
324 if (!OF.getOffset().evaluateAsValue(Value, Layout)) {
325 getContext().reportError(OF.getLoc(),
326 "expected assembly-time absolute expression");
330 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
331 int64_t TargetLocation = Value.getConstant();
332 if (const MCSymbolRefExpr *A = Value.getSymA()) {
334 if (!Layout.getSymbolOffset(A->getSymbol(), Val)) {
335 getContext().reportError(OF.getLoc(), "expected absolute expression");
338 TargetLocation += Val;
340 int64_t Size = TargetLocation - FragmentOffset;
341 if (Size < 0 || Size >= 0x40000000) {
342 getContext().reportError(
343 OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
344 "' (at offset '" + Twine(FragmentOffset) + "')");
350 case MCFragment::FT_Dwarf:
351 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
352 case MCFragment::FT_DwarfFrame:
353 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
354 case MCFragment::FT_CVInlineLines:
355 return cast<MCCVInlineLineTableFragment>(F).getContents().size();
356 case MCFragment::FT_CVDefRange:
357 return cast<MCCVDefRangeFragment>(F).getContents().size();
358 case MCFragment::FT_Dummy:
359 llvm_unreachable("Should not have been added");
362 llvm_unreachable("invalid fragment kind");
365 void MCAsmLayout::layoutFragment(MCFragment *F) {
366 MCFragment *Prev = F->getPrevNode();
368 // We should never try to recompute something which is valid.
369 assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
370 // We should never try to compute the fragment layout if its predecessor
372 assert((!Prev || isFragmentValid(Prev)) &&
373 "Attempt to compute fragment before its predecessor!");
375 ++stats::FragmentLayouts;
377 // Compute fragment offset and size.
379 F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
382 LastValidFragment[F->getParent()] = F;
384 // If bundling is enabled and this fragment has instructions in it, it has to
385 // obey the bundling restrictions. With padding, we'll have:
390 // -------------------------------------
391 // Prev |##########| F |
392 // -------------------------------------
397 // The fragment's offset will point to after the padding, and its computed
398 // size won't include the padding.
400 // When the -mc-relax-all flag is used, we optimize bundling by writting the
401 // padding directly into fragments when the instructions are emitted inside
402 // the streamer. When the fragment is larger than the bundle size, we need to
403 // ensure that it's bundle aligned. This means that if we end up with
404 // multiple fragments, we must emit bundle padding between fragments.
406 // ".align N" is an example of a directive that introduces multiple
407 // fragments. We could add a special case to handle ".align N" by emitting
408 // within-fragment padding (which would produce less padding when N is less
409 // than the bundle size), but for now we don't.
411 if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
412 assert(isa<MCEncodedFragment>(F) &&
413 "Only MCEncodedFragment implementations have instructions");
414 uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
416 if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize())
417 report_fatal_error("Fragment can't be larger than a bundle size");
419 uint64_t RequiredBundlePadding = computeBundlePadding(Assembler, F,
421 if (RequiredBundlePadding > UINT8_MAX)
422 report_fatal_error("Padding cannot exceed 255 bytes");
423 F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
424 F->Offset += RequiredBundlePadding;
428 void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) {
429 bool New = !Symbol.isRegistered();
433 Symbol.setIsRegistered(true);
434 Symbols.push_back(&Symbol);
438 void MCAssembler::writeFragmentPadding(const MCFragment &F, uint64_t FSize,
439 MCObjectWriter *OW) const {
440 // Should NOP padding be written out before this fragment?
441 unsigned BundlePadding = F.getBundlePadding();
442 if (BundlePadding > 0) {
443 assert(isBundlingEnabled() &&
444 "Writing bundle padding with disabled bundling");
445 assert(F.hasInstructions() &&
446 "Writing bundle padding for a fragment without instructions");
448 unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
449 if (F.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
450 // If the padding itself crosses a bundle boundary, it must be emitted
451 // in 2 pieces, since even nop instructions must not cross boundaries.
452 // v--------------v <- BundleAlignSize
453 // v---------v <- BundlePadding
454 // ----------------------------
455 // | Prev |####|####| F |
456 // ----------------------------
457 // ^-------------------^ <- TotalLength
458 unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
459 if (!getBackend().writeNopData(DistanceToBoundary, OW))
460 report_fatal_error("unable to write NOP sequence of " +
461 Twine(DistanceToBoundary) + " bytes");
462 BundlePadding -= DistanceToBoundary;
464 if (!getBackend().writeNopData(BundlePadding, OW))
465 report_fatal_error("unable to write NOP sequence of " +
466 Twine(BundlePadding) + " bytes");
470 /// \brief Write the fragment \p F to the output file.
471 static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
472 const MCFragment &F) {
473 MCObjectWriter *OW = &Asm.getWriter();
475 // FIXME: Embed in fragments instead?
476 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
478 Asm.writeFragmentPadding(F, FragmentSize, OW);
480 // This variable (and its dummy usage) is to participate in the assert at
481 // the end of the function.
482 uint64_t Start = OW->getStream().tell();
485 ++stats::EmittedFragments;
487 switch (F.getKind()) {
488 case MCFragment::FT_Align: {
489 ++stats::EmittedAlignFragments;
490 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
491 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
493 uint64_t Count = FragmentSize / AF.getValueSize();
495 // FIXME: This error shouldn't actually occur (the front end should emit
496 // multiple .align directives to enforce the semantics it wants), but is
497 // severe enough that we want to report it. How to handle this?
498 if (Count * AF.getValueSize() != FragmentSize)
499 report_fatal_error("undefined .align directive, value size '" +
500 Twine(AF.getValueSize()) +
501 "' is not a divisor of padding size '" +
502 Twine(FragmentSize) + "'");
504 // See if we are aligning with nops, and if so do that first to try to fill
505 // the Count bytes. Then if that did not fill any bytes or there are any
506 // bytes left to fill use the Value and ValueSize to fill the rest.
507 // If we are aligning with nops, ask that target to emit the right data.
508 if (AF.hasEmitNops()) {
509 if (!Asm.getBackend().writeNopData(Count, OW))
510 report_fatal_error("unable to write nop sequence of " +
511 Twine(Count) + " bytes");
515 // Otherwise, write out in multiples of the value size.
516 for (uint64_t i = 0; i != Count; ++i) {
517 switch (AF.getValueSize()) {
518 default: llvm_unreachable("Invalid size!");
519 case 1: OW->write8 (uint8_t (AF.getValue())); break;
520 case 2: OW->write16(uint16_t(AF.getValue())); break;
521 case 4: OW->write32(uint32_t(AF.getValue())); break;
522 case 8: OW->write64(uint64_t(AF.getValue())); break;
528 case MCFragment::FT_Data:
529 ++stats::EmittedDataFragments;
530 OW->writeBytes(cast<MCDataFragment>(F).getContents());
533 case MCFragment::FT_Relaxable:
534 ++stats::EmittedRelaxableFragments;
535 OW->writeBytes(cast<MCRelaxableFragment>(F).getContents());
538 case MCFragment::FT_CompactEncodedInst:
539 ++stats::EmittedCompactEncodedInstFragments;
540 OW->writeBytes(cast<MCCompactEncodedInstFragment>(F).getContents());
543 case MCFragment::FT_Fill: {
544 ++stats::EmittedFillFragments;
545 const MCFillFragment &FF = cast<MCFillFragment>(F);
546 uint8_t V = FF.getValue();
547 const unsigned MaxChunkSize = 16;
548 char Data[MaxChunkSize];
550 for (unsigned I = 1; I < MaxChunkSize; ++I)
553 uint64_t Size = FragmentSize;
554 for (unsigned ChunkSize = MaxChunkSize; ChunkSize; ChunkSize /= 2) {
555 StringRef Ref(Data, ChunkSize);
556 for (uint64_t I = 0, E = Size / ChunkSize; I != E; ++I)
558 Size = Size % ChunkSize;
563 case MCFragment::FT_LEB: {
564 const MCLEBFragment &LF = cast<MCLEBFragment>(F);
565 OW->writeBytes(LF.getContents());
569 case MCFragment::FT_Padding: {
570 if (!Asm.getBackend().writeNopData(FragmentSize, OW))
571 report_fatal_error("unable to write nop sequence of " +
572 Twine(FragmentSize) + " bytes");
576 case MCFragment::FT_SymbolId: {
577 const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F);
578 OW->write32(SF.getSymbol()->getIndex());
582 case MCFragment::FT_Org: {
583 ++stats::EmittedOrgFragments;
584 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
586 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
587 OW->write8(uint8_t(OF.getValue()));
592 case MCFragment::FT_Dwarf: {
593 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
594 OW->writeBytes(OF.getContents());
597 case MCFragment::FT_DwarfFrame: {
598 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
599 OW->writeBytes(CF.getContents());
602 case MCFragment::FT_CVInlineLines: {
603 const auto &OF = cast<MCCVInlineLineTableFragment>(F);
604 OW->writeBytes(OF.getContents());
607 case MCFragment::FT_CVDefRange: {
608 const auto &DRF = cast<MCCVDefRangeFragment>(F);
609 OW->writeBytes(DRF.getContents());
612 case MCFragment::FT_Dummy:
613 llvm_unreachable("Should not have been added");
616 assert(OW->getStream().tell() - Start == FragmentSize &&
617 "The stream should advance by fragment size");
620 void MCAssembler::writeSectionData(const MCSection *Sec,
621 const MCAsmLayout &Layout) const {
622 // Ignore virtual sections.
623 if (Sec->isVirtualSection()) {
624 assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!");
626 // Check that contents are only things legal inside a virtual section.
627 for (const MCFragment &F : *Sec) {
628 switch (F.getKind()) {
629 default: llvm_unreachable("Invalid fragment in virtual section!");
630 case MCFragment::FT_Data: {
631 // Check that we aren't trying to write a non-zero contents (or fixups)
632 // into a virtual section. This is to support clients which use standard
633 // directives to fill the contents of virtual sections.
634 const MCDataFragment &DF = cast<MCDataFragment>(F);
635 if (DF.fixup_begin() != DF.fixup_end())
636 report_fatal_error("cannot have fixups in virtual section!");
637 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
638 if (DF.getContents()[i]) {
639 if (auto *ELFSec = dyn_cast<const MCSectionELF>(Sec))
640 report_fatal_error("non-zero initializer found in section '" +
641 ELFSec->getSectionName() + "'");
643 report_fatal_error("non-zero initializer found in virtual section");
647 case MCFragment::FT_Align:
648 // Check that we aren't trying to write a non-zero value into a virtual
650 assert((cast<MCAlignFragment>(F).getValueSize() == 0 ||
651 cast<MCAlignFragment>(F).getValue() == 0) &&
652 "Invalid align in virtual section!");
654 case MCFragment::FT_Fill:
655 assert((cast<MCFillFragment>(F).getValue() == 0) &&
656 "Invalid fill in virtual section!");
664 uint64_t Start = getWriter().getStream().tell();
667 for (const MCFragment &F : *Sec)
668 writeFragment(*this, Layout, F);
670 assert(getWriter().getStream().tell() - Start ==
671 Layout.getSectionAddressSize(Sec));
674 std::tuple<MCValue, uint64_t, bool>
675 MCAssembler::handleFixup(const MCAsmLayout &Layout, MCFragment &F,
676 const MCFixup &Fixup) {
677 // Evaluate the fixup.
680 bool IsResolved = evaluateFixup(Layout, Fixup, &F, Target, FixedValue);
682 // The fixup was unresolved, we need a relocation. Inform the object
683 // writer of the relocation, and give it an opportunity to adjust the
684 // fixup value if need be.
685 getWriter().recordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
687 return std::make_tuple(Target, FixedValue, IsResolved);
690 void MCAssembler::layout(MCAsmLayout &Layout) {
691 DEBUG_WITH_TYPE("mc-dump", {
692 errs() << "assembler backend - pre-layout\n--\n";
695 // Create dummy fragments and assign section ordinals.
696 unsigned SectionIndex = 0;
697 for (MCSection &Sec : *this) {
698 // Create dummy fragments to eliminate any empty sections, this simplifies
700 if (Sec.getFragmentList().empty())
701 new MCDataFragment(&Sec);
703 Sec.setOrdinal(SectionIndex++);
706 // Assign layout order indices to sections and fragments.
707 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
708 MCSection *Sec = Layout.getSectionOrder()[i];
709 Sec->setLayoutOrder(i);
711 unsigned FragmentIndex = 0;
712 for (MCFragment &Frag : *Sec)
713 Frag.setLayoutOrder(FragmentIndex++);
716 // Layout until everything fits.
717 while (layoutOnce(Layout))
718 if (getContext().hadError())
721 DEBUG_WITH_TYPE("mc-dump", {
722 errs() << "assembler backend - post-relaxation\n--\n";
725 // Finalize the layout, including fragment lowering.
726 finishLayout(Layout);
728 DEBUG_WITH_TYPE("mc-dump", {
729 errs() << "assembler backend - final-layout\n--\n";
732 // Allow the object writer a chance to perform post-layout binding (for
733 // example, to set the index fields in the symbol data).
734 getWriter().executePostLayoutBinding(*this, Layout);
736 // Evaluate and apply the fixups, generating relocation entries as necessary.
737 for (MCSection &Sec : *this) {
738 for (MCFragment &Frag : Sec) {
739 // Data and relaxable fragments both have fixups. So only process
741 // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups
742 // being templated makes this tricky.
743 if (isa<MCEncodedFragment>(&Frag) &&
744 isa<MCCompactEncodedInstFragment>(&Frag))
746 if (!isa<MCEncodedFragment>(&Frag) && !isa<MCCVDefRangeFragment>(&Frag))
748 ArrayRef<MCFixup> Fixups;
749 MutableArrayRef<char> Contents;
750 if (auto *FragWithFixups = dyn_cast<MCDataFragment>(&Frag)) {
751 Fixups = FragWithFixups->getFixups();
752 Contents = FragWithFixups->getContents();
753 } else if (auto *FragWithFixups = dyn_cast<MCRelaxableFragment>(&Frag)) {
754 Fixups = FragWithFixups->getFixups();
755 Contents = FragWithFixups->getContents();
756 } else if (auto *FragWithFixups = dyn_cast<MCCVDefRangeFragment>(&Frag)) {
757 Fixups = FragWithFixups->getFixups();
758 Contents = FragWithFixups->getContents();
760 llvm_unreachable("Unknown fragment with fixups!");
761 for (const MCFixup &Fixup : Fixups) {
765 std::tie(Target, FixedValue, IsResolved) =
766 handleFixup(Layout, Frag, Fixup);
767 getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue,
774 void MCAssembler::Finish() {
775 // Create the layout object.
776 MCAsmLayout Layout(*this);
779 raw_ostream &OS = getWriter().getStream();
780 uint64_t StartOffset = OS.tell();
782 // Write the object file.
783 getWriter().writeObject(*this, Layout);
785 stats::ObjectBytes += OS.tell() - StartOffset;
788 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
789 const MCRelaxableFragment *DF,
790 const MCAsmLayout &Layout) const {
793 bool Resolved = evaluateFixup(Layout, Fixup, DF, Target, Value);
794 if (Target.getSymA() &&
795 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8 &&
796 Fixup.getKind() == FK_Data_1)
798 return getBackend().fixupNeedsRelaxationAdvanced(Fixup, Resolved, Value, DF,
802 bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
803 const MCAsmLayout &Layout) const {
804 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
805 // are intentionally pushing out inst fragments, or because we relaxed a
806 // previous instruction to one that doesn't need relaxation.
807 if (!getBackend().mayNeedRelaxation(F->getInst()))
810 for (const MCFixup &Fixup : F->getFixups())
811 if (fixupNeedsRelaxation(Fixup, F, Layout))
817 bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
818 MCRelaxableFragment &F) {
819 if (!fragmentNeedsRelaxation(&F, Layout))
822 ++stats::RelaxedInstructions;
824 // FIXME-PERF: We could immediately lower out instructions if we can tell
825 // they are fully resolved, to avoid retesting on later passes.
827 // Relax the fragment.
830 getBackend().relaxInstruction(F.getInst(), F.getSubtargetInfo(), Relaxed);
832 // Encode the new instruction.
834 // FIXME-PERF: If it matters, we could let the target do this. It can
835 // probably do so more efficiently in many cases.
836 SmallVector<MCFixup, 4> Fixups;
837 SmallString<256> Code;
838 raw_svector_ostream VecOS(Code);
839 getEmitter().encodeInstruction(Relaxed, VecOS, Fixups, F.getSubtargetInfo());
841 // Update the fragment.
843 F.getContents() = Code;
844 F.getFixups() = Fixups;
849 bool MCAssembler::relaxPaddingFragment(MCAsmLayout &Layout,
850 MCPaddingFragment &PF) {
851 uint64_t OldSize = PF.getSize();
852 if (!getBackend().relaxFragment(&PF, Layout))
854 uint64_t NewSize = PF.getSize();
856 ++stats::PaddingFragmentsRelaxations;
857 stats::PaddingFragmentsBytes += NewSize;
858 stats::PaddingFragmentsBytes -= OldSize;
862 bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
863 uint64_t OldSize = LF.getContents().size();
865 bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout);
867 report_fatal_error("sleb128 and uleb128 expressions must be absolute");
868 SmallString<8> &Data = LF.getContents();
870 raw_svector_ostream OSE(Data);
872 encodeSLEB128(Value, OSE);
874 encodeULEB128(Value, OSE);
875 return OldSize != LF.getContents().size();
878 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
879 MCDwarfLineAddrFragment &DF) {
880 MCContext &Context = Layout.getAssembler().getContext();
881 uint64_t OldSize = DF.getContents().size();
883 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
884 assert(Abs && "We created a line delta with an invalid expression");
887 LineDelta = DF.getLineDelta();
888 SmallString<8> &Data = DF.getContents();
890 raw_svector_ostream OSE(Data);
891 MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta,
893 return OldSize != Data.size();
896 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
897 MCDwarfCallFrameFragment &DF) {
898 MCContext &Context = Layout.getAssembler().getContext();
899 uint64_t OldSize = DF.getContents().size();
901 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
902 assert(Abs && "We created call frame with an invalid expression");
904 SmallString<8> &Data = DF.getContents();
906 raw_svector_ostream OSE(Data);
907 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE);
908 return OldSize != Data.size();
911 bool MCAssembler::relaxCVInlineLineTable(MCAsmLayout &Layout,
912 MCCVInlineLineTableFragment &F) {
913 unsigned OldSize = F.getContents().size();
914 getContext().getCVContext().encodeInlineLineTable(Layout, F);
915 return OldSize != F.getContents().size();
918 bool MCAssembler::relaxCVDefRange(MCAsmLayout &Layout,
919 MCCVDefRangeFragment &F) {
920 unsigned OldSize = F.getContents().size();
921 getContext().getCVContext().encodeDefRange(Layout, F);
922 return OldSize != F.getContents().size();
925 bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) {
926 // Holds the first fragment which needed relaxing during this layout. It will
927 // remain NULL if none were relaxed.
928 // When a fragment is relaxed, all the fragments following it should get
929 // invalidated because their offset is going to change.
930 MCFragment *FirstRelaxedFragment = nullptr;
932 // Attempt to relax all the fragments in the section.
933 for (MCSection::iterator I = Sec.begin(), IE = Sec.end(); I != IE; ++I) {
934 // Check if this is a fragment that needs relaxation.
935 bool RelaxedFrag = false;
936 switch(I->getKind()) {
939 case MCFragment::FT_Relaxable:
940 assert(!getRelaxAll() &&
941 "Did not expect a MCRelaxableFragment in RelaxAll mode");
942 RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
944 case MCFragment::FT_Dwarf:
945 RelaxedFrag = relaxDwarfLineAddr(Layout,
946 *cast<MCDwarfLineAddrFragment>(I));
948 case MCFragment::FT_DwarfFrame:
950 relaxDwarfCallFrameFragment(Layout,
951 *cast<MCDwarfCallFrameFragment>(I));
953 case MCFragment::FT_LEB:
954 RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
956 case MCFragment::FT_Padding:
957 RelaxedFrag = relaxPaddingFragment(Layout, *cast<MCPaddingFragment>(I));
959 case MCFragment::FT_CVInlineLines:
961 relaxCVInlineLineTable(Layout, *cast<MCCVInlineLineTableFragment>(I));
963 case MCFragment::FT_CVDefRange:
964 RelaxedFrag = relaxCVDefRange(Layout, *cast<MCCVDefRangeFragment>(I));
967 if (RelaxedFrag && !FirstRelaxedFragment)
968 FirstRelaxedFragment = &*I;
970 if (FirstRelaxedFragment) {
971 Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
977 bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
978 ++stats::RelaxationSteps;
980 bool WasRelaxed = false;
981 for (iterator it = begin(), ie = end(); it != ie; ++it) {
982 MCSection &Sec = *it;
983 while (layoutSectionOnce(Layout, Sec))
990 void MCAssembler::finishLayout(MCAsmLayout &Layout) {
991 // The layout is done. Mark every fragment as valid.
992 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
993 MCSection &Section = *Layout.getSectionOrder()[i];
994 Layout.getFragmentOffset(&*Section.rbegin());
995 computeFragmentSize(Layout, *Section.rbegin());
997 getBackend().finishLayout(*this, Layout);