1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCAssembler.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCCodeView.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCFixup.h"
25 #include "llvm/MC/MCFixupKindInfo.h"
26 #include "llvm/MC/MCFragment.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSectionELF.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/LEB128.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/raw_ostream.h"
47 #define DEBUG_TYPE "assembler"
52 STATISTIC(EmittedFragments
, "Number of emitted assembler fragments - total");
53 STATISTIC(EmittedRelaxableFragments
,
54 "Number of emitted assembler fragments - relaxable");
55 STATISTIC(EmittedDataFragments
,
56 "Number of emitted assembler fragments - data");
57 STATISTIC(EmittedCompactEncodedInstFragments
,
58 "Number of emitted assembler fragments - compact encoded inst");
59 STATISTIC(EmittedAlignFragments
,
60 "Number of emitted assembler fragments - align");
61 STATISTIC(EmittedFillFragments
,
62 "Number of emitted assembler fragments - fill");
63 STATISTIC(EmittedOrgFragments
,
64 "Number of emitted assembler fragments - org");
65 STATISTIC(evaluateFixup
, "Number of evaluated fixups");
66 STATISTIC(FragmentLayouts
, "Number of fragment layouts");
67 STATISTIC(ObjectBytes
, "Number of emitted object file bytes");
68 STATISTIC(RelaxationSteps
, "Number of assembler layout and relaxation steps");
69 STATISTIC(RelaxedInstructions
, "Number of relaxed instructions");
70 STATISTIC(PaddingFragmentsRelaxations
,
71 "Number of Padding Fragments relaxations");
72 STATISTIC(PaddingFragmentsBytes
,
73 "Total size of all padding from adding Fragments");
75 } // end namespace stats
76 } // end anonymous namespace
78 // FIXME FIXME FIXME: There are number of places in this file where we convert
79 // what is a 64-bit assembler value used for computation into a value in the
80 // object file, which may truncate it. We should detect that truncation where
81 // invalid and report errors back.
85 MCAssembler::MCAssembler(MCContext
&Context
,
86 std::unique_ptr
<MCAsmBackend
> Backend
,
87 std::unique_ptr
<MCCodeEmitter
> Emitter
,
88 std::unique_ptr
<MCObjectWriter
> Writer
)
89 : Context(Context
), Backend(std::move(Backend
)),
90 Emitter(std::move(Emitter
)), Writer(std::move(Writer
)),
91 BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false),
92 IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) {
93 VersionInfo
.Major
= 0; // Major version == 0 for "none specified"
96 MCAssembler::~MCAssembler() = default;
98 void MCAssembler::reset() {
101 IndirectSymbols
.clear();
103 LinkerOptions
.clear();
108 SubsectionsViaSymbols
= false;
109 IncrementalLinkerCompatible
= false;
111 LOHContainer
.reset();
112 VersionInfo
.Major
= 0;
113 VersionInfo
.SDKVersion
= VersionTuple();
115 // reset objects owned by us
117 getBackendPtr()->reset();
119 getEmitterPtr()->reset();
121 getWriterPtr()->reset();
122 getLOHContainer().reset();
125 bool MCAssembler::registerSection(MCSection
&Section
) {
126 if (Section
.isRegistered())
128 Sections
.push_back(&Section
);
129 Section
.setIsRegistered(true);
133 bool MCAssembler::isThumbFunc(const MCSymbol
*Symbol
) const {
134 if (ThumbFuncs
.count(Symbol
))
137 if (!Symbol
->isVariable())
140 const MCExpr
*Expr
= Symbol
->getVariableValue();
143 if (!Expr
->evaluateAsRelocatable(V
, nullptr, nullptr))
146 if (V
.getSymB() || V
.getRefKind() != MCSymbolRefExpr::VK_None
)
149 const MCSymbolRefExpr
*Ref
= V
.getSymA();
153 if (Ref
->getKind() != MCSymbolRefExpr::VK_None
)
156 const MCSymbol
&Sym
= Ref
->getSymbol();
157 if (!isThumbFunc(&Sym
))
160 ThumbFuncs
.insert(Symbol
); // Cache it.
164 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol
&Symbol
) const {
165 // Non-temporary labels should always be visible to the linker.
166 if (!Symbol
.isTemporary())
169 // Absolute temporary labels are never visible.
170 if (!Symbol
.isInSection())
173 if (Symbol
.isUsedInReloc())
179 const MCSymbol
*MCAssembler::getAtom(const MCSymbol
&S
) const {
180 // Linker visible symbols define atoms.
181 if (isSymbolLinkerVisible(S
))
184 // Absolute and undefined symbols have no defining atom.
185 if (!S
.isInSection())
188 // Non-linker visible symbols in sections which can't be atomized have no
190 if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
191 *S
.getFragment()->getParent()))
194 // Otherwise, return the atom for the containing fragment.
195 return S
.getFragment()->getAtom();
198 bool MCAssembler::evaluateFixup(const MCAsmLayout
&Layout
,
199 const MCFixup
&Fixup
, const MCFragment
*DF
,
200 MCValue
&Target
, uint64_t &Value
,
201 bool &WasForced
) const {
202 ++stats::evaluateFixup
;
204 // FIXME: This code has some duplication with recordRelocation. We should
205 // probably merge the two into a single callback that tries to evaluate a
206 // fixup and records a relocation if one is needed.
208 // On error claim to have completely evaluated the fixup, to prevent any
209 // further processing from being done.
210 const MCExpr
*Expr
= Fixup
.getValue();
211 MCContext
&Ctx
= getContext();
214 if (!Expr
->evaluateAsRelocatable(Target
, &Layout
, &Fixup
)) {
215 Ctx
.reportError(Fixup
.getLoc(), "expected relocatable expression");
218 if (const MCSymbolRefExpr
*RefB
= Target
.getSymB()) {
219 if (RefB
->getKind() != MCSymbolRefExpr::VK_None
) {
220 Ctx
.reportError(Fixup
.getLoc(),
221 "unsupported subtraction of qualified symbol");
226 assert(getBackendPtr() && "Expected assembler backend");
227 bool IsPCRel
= getBackendPtr()->getFixupKindInfo(Fixup
.getKind()).Flags
&
228 MCFixupKindInfo::FKF_IsPCRel
;
230 bool IsResolved
= false;
232 if (Target
.getSymB()) {
234 } else if (!Target
.getSymA()) {
237 const MCSymbolRefExpr
*A
= Target
.getSymA();
238 const MCSymbol
&SA
= A
->getSymbol();
239 if (A
->getKind() != MCSymbolRefExpr::VK_None
|| SA
.isUndefined()) {
241 } else if (auto *Writer
= getWriterPtr()) {
242 IsResolved
= Writer
->isSymbolRefDifferenceFullyResolvedImpl(
243 *this, SA
, *DF
, false, true);
247 IsResolved
= Target
.isAbsolute();
250 Value
= Target
.getConstant();
252 if (const MCSymbolRefExpr
*A
= Target
.getSymA()) {
253 const MCSymbol
&Sym
= A
->getSymbol();
255 Value
+= Layout
.getSymbolOffset(Sym
);
257 if (const MCSymbolRefExpr
*B
= Target
.getSymB()) {
258 const MCSymbol
&Sym
= B
->getSymbol();
260 Value
-= Layout
.getSymbolOffset(Sym
);
263 bool ShouldAlignPC
= getBackend().getFixupKindInfo(Fixup
.getKind()).Flags
&
264 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits
;
265 assert((ShouldAlignPC
? IsPCRel
: true) &&
266 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
269 uint32_t Offset
= Layout
.getFragmentOffset(DF
) + Fixup
.getOffset();
271 // A number of ARM fixups in Thumb mode require that the effective PC
272 // address be determined as the 32-bit aligned version of the actual offset.
273 if (ShouldAlignPC
) Offset
&= ~0x3;
277 // Let the backend force a relocation if needed.
278 if (IsResolved
&& getBackend().shouldForceRelocation(*this, Fixup
, Target
)) {
286 uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout
&Layout
,
287 const MCFragment
&F
) const {
288 assert(getBackendPtr() && "Requires assembler backend");
289 switch (F
.getKind()) {
290 case MCFragment::FT_Data
:
291 return cast
<MCDataFragment
>(F
).getContents().size();
292 case MCFragment::FT_Relaxable
:
293 return cast
<MCRelaxableFragment
>(F
).getContents().size();
294 case MCFragment::FT_CompactEncodedInst
:
295 return cast
<MCCompactEncodedInstFragment
>(F
).getContents().size();
296 case MCFragment::FT_Fill
: {
297 auto &FF
= cast
<MCFillFragment
>(F
);
298 int64_t NumValues
= 0;
299 if (!FF
.getNumValues().evaluateAsAbsolute(NumValues
, Layout
)) {
300 getContext().reportError(FF
.getLoc(),
301 "expected assembly-time absolute expression");
304 int64_t Size
= NumValues
* FF
.getValueSize();
306 getContext().reportError(FF
.getLoc(), "invalid number of bytes");
312 case MCFragment::FT_LEB
:
313 return cast
<MCLEBFragment
>(F
).getContents().size();
315 case MCFragment::FT_Padding
:
316 return cast
<MCPaddingFragment
>(F
).getSize();
318 case MCFragment::FT_SymbolId
:
321 case MCFragment::FT_Align
: {
322 const MCAlignFragment
&AF
= cast
<MCAlignFragment
>(F
);
323 unsigned Offset
= Layout
.getFragmentOffset(&AF
);
324 unsigned Size
= OffsetToAlignment(Offset
, AF
.getAlignment());
326 // Insert extra Nops for code alignment if the target define
327 // shouldInsertExtraNopBytesForCodeAlign target hook.
328 if (AF
.getParent()->UseCodeAlign() && AF
.hasEmitNops() &&
329 getBackend().shouldInsertExtraNopBytesForCodeAlign(AF
, Size
))
332 // If we are padding with nops, force the padding to be larger than the
334 if (Size
> 0 && AF
.hasEmitNops()) {
335 while (Size
% getBackend().getMinimumNopSize())
336 Size
+= AF
.getAlignment();
338 if (Size
> AF
.getMaxBytesToEmit())
343 case MCFragment::FT_Org
: {
344 const MCOrgFragment
&OF
= cast
<MCOrgFragment
>(F
);
346 if (!OF
.getOffset().evaluateAsValue(Value
, Layout
)) {
347 getContext().reportError(OF
.getLoc(),
348 "expected assembly-time absolute expression");
352 uint64_t FragmentOffset
= Layout
.getFragmentOffset(&OF
);
353 int64_t TargetLocation
= Value
.getConstant();
354 if (const MCSymbolRefExpr
*A
= Value
.getSymA()) {
356 if (!Layout
.getSymbolOffset(A
->getSymbol(), Val
)) {
357 getContext().reportError(OF
.getLoc(), "expected absolute expression");
360 TargetLocation
+= Val
;
362 int64_t Size
= TargetLocation
- FragmentOffset
;
363 if (Size
< 0 || Size
>= 0x40000000) {
364 getContext().reportError(
365 OF
.getLoc(), "invalid .org offset '" + Twine(TargetLocation
) +
366 "' (at offset '" + Twine(FragmentOffset
) + "')");
372 case MCFragment::FT_Dwarf
:
373 return cast
<MCDwarfLineAddrFragment
>(F
).getContents().size();
374 case MCFragment::FT_DwarfFrame
:
375 return cast
<MCDwarfCallFrameFragment
>(F
).getContents().size();
376 case MCFragment::FT_CVInlineLines
:
377 return cast
<MCCVInlineLineTableFragment
>(F
).getContents().size();
378 case MCFragment::FT_CVDefRange
:
379 return cast
<MCCVDefRangeFragment
>(F
).getContents().size();
380 case MCFragment::FT_Dummy
:
381 llvm_unreachable("Should not have been added");
384 llvm_unreachable("invalid fragment kind");
387 void MCAsmLayout::layoutFragment(MCFragment
*F
) {
388 MCFragment
*Prev
= F
->getPrevNode();
390 // We should never try to recompute something which is valid.
391 assert(!isFragmentValid(F
) && "Attempt to recompute a valid fragment!");
392 // We should never try to compute the fragment layout if its predecessor
394 assert((!Prev
|| isFragmentValid(Prev
)) &&
395 "Attempt to compute fragment before its predecessor!");
397 ++stats::FragmentLayouts
;
399 // Compute fragment offset and size.
401 F
->Offset
= Prev
->Offset
+ getAssembler().computeFragmentSize(*this, *Prev
);
404 LastValidFragment
[F
->getParent()] = F
;
406 // If bundling is enabled and this fragment has instructions in it, it has to
407 // obey the bundling restrictions. With padding, we'll have:
412 // -------------------------------------
413 // Prev |##########| F |
414 // -------------------------------------
419 // The fragment's offset will point to after the padding, and its computed
420 // size won't include the padding.
422 // When the -mc-relax-all flag is used, we optimize bundling by writting the
423 // padding directly into fragments when the instructions are emitted inside
424 // the streamer. When the fragment is larger than the bundle size, we need to
425 // ensure that it's bundle aligned. This means that if we end up with
426 // multiple fragments, we must emit bundle padding between fragments.
428 // ".align N" is an example of a directive that introduces multiple
429 // fragments. We could add a special case to handle ".align N" by emitting
430 // within-fragment padding (which would produce less padding when N is less
431 // than the bundle size), but for now we don't.
433 if (Assembler
.isBundlingEnabled() && F
->hasInstructions()) {
434 assert(isa
<MCEncodedFragment
>(F
) &&
435 "Only MCEncodedFragment implementations have instructions");
436 MCEncodedFragment
*EF
= cast
<MCEncodedFragment
>(F
);
437 uint64_t FSize
= Assembler
.computeFragmentSize(*this, *EF
);
439 if (!Assembler
.getRelaxAll() && FSize
> Assembler
.getBundleAlignSize())
440 report_fatal_error("Fragment can't be larger than a bundle size");
442 uint64_t RequiredBundlePadding
=
443 computeBundlePadding(Assembler
, EF
, EF
->Offset
, FSize
);
444 if (RequiredBundlePadding
> UINT8_MAX
)
445 report_fatal_error("Padding cannot exceed 255 bytes");
446 EF
->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding
));
447 EF
->Offset
+= RequiredBundlePadding
;
451 void MCAssembler::registerSymbol(const MCSymbol
&Symbol
, bool *Created
) {
452 bool New
= !Symbol
.isRegistered();
456 Symbol
.setIsRegistered(true);
457 Symbols
.push_back(&Symbol
);
461 void MCAssembler::writeFragmentPadding(raw_ostream
&OS
,
462 const MCEncodedFragment
&EF
,
463 uint64_t FSize
) const {
464 assert(getBackendPtr() && "Expected assembler backend");
465 // Should NOP padding be written out before this fragment?
466 unsigned BundlePadding
= EF
.getBundlePadding();
467 if (BundlePadding
> 0) {
468 assert(isBundlingEnabled() &&
469 "Writing bundle padding with disabled bundling");
470 assert(EF
.hasInstructions() &&
471 "Writing bundle padding for a fragment without instructions");
473 unsigned TotalLength
= BundlePadding
+ static_cast<unsigned>(FSize
);
474 if (EF
.alignToBundleEnd() && TotalLength
> getBundleAlignSize()) {
475 // If the padding itself crosses a bundle boundary, it must be emitted
476 // in 2 pieces, since even nop instructions must not cross boundaries.
477 // v--------------v <- BundleAlignSize
478 // v---------v <- BundlePadding
479 // ----------------------------
480 // | Prev |####|####| F |
481 // ----------------------------
482 // ^-------------------^ <- TotalLength
483 unsigned DistanceToBoundary
= TotalLength
- getBundleAlignSize();
484 if (!getBackend().writeNopData(OS
, DistanceToBoundary
))
485 report_fatal_error("unable to write NOP sequence of " +
486 Twine(DistanceToBoundary
) + " bytes");
487 BundlePadding
-= DistanceToBoundary
;
489 if (!getBackend().writeNopData(OS
, BundlePadding
))
490 report_fatal_error("unable to write NOP sequence of " +
491 Twine(BundlePadding
) + " bytes");
495 /// Write the fragment \p F to the output file.
496 static void writeFragment(raw_ostream
&OS
, const MCAssembler
&Asm
,
497 const MCAsmLayout
&Layout
, const MCFragment
&F
) {
498 // FIXME: Embed in fragments instead?
499 uint64_t FragmentSize
= Asm
.computeFragmentSize(Layout
, F
);
501 support::endianness Endian
= Asm
.getBackend().Endian
;
503 if (const MCEncodedFragment
*EF
= dyn_cast
<MCEncodedFragment
>(&F
))
504 Asm
.writeFragmentPadding(OS
, *EF
, FragmentSize
);
506 // This variable (and its dummy usage) is to participate in the assert at
507 // the end of the function.
508 uint64_t Start
= OS
.tell();
511 ++stats::EmittedFragments
;
513 switch (F
.getKind()) {
514 case MCFragment::FT_Align
: {
515 ++stats::EmittedAlignFragments
;
516 const MCAlignFragment
&AF
= cast
<MCAlignFragment
>(F
);
517 assert(AF
.getValueSize() && "Invalid virtual align in concrete fragment!");
519 uint64_t Count
= FragmentSize
/ AF
.getValueSize();
521 // FIXME: This error shouldn't actually occur (the front end should emit
522 // multiple .align directives to enforce the semantics it wants), but is
523 // severe enough that we want to report it. How to handle this?
524 if (Count
* AF
.getValueSize() != FragmentSize
)
525 report_fatal_error("undefined .align directive, value size '" +
526 Twine(AF
.getValueSize()) +
527 "' is not a divisor of padding size '" +
528 Twine(FragmentSize
) + "'");
530 // See if we are aligning with nops, and if so do that first to try to fill
531 // the Count bytes. Then if that did not fill any bytes or there are any
532 // bytes left to fill use the Value and ValueSize to fill the rest.
533 // If we are aligning with nops, ask that target to emit the right data.
534 if (AF
.hasEmitNops()) {
535 if (!Asm
.getBackend().writeNopData(OS
, Count
))
536 report_fatal_error("unable to write nop sequence of " +
537 Twine(Count
) + " bytes");
541 // Otherwise, write out in multiples of the value size.
542 for (uint64_t i
= 0; i
!= Count
; ++i
) {
543 switch (AF
.getValueSize()) {
544 default: llvm_unreachable("Invalid size!");
545 case 1: OS
<< char(AF
.getValue()); break;
547 support::endian::write
<uint16_t>(OS
, AF
.getValue(), Endian
);
550 support::endian::write
<uint32_t>(OS
, AF
.getValue(), Endian
);
553 support::endian::write
<uint64_t>(OS
, AF
.getValue(), Endian
);
560 case MCFragment::FT_Data
:
561 ++stats::EmittedDataFragments
;
562 OS
<< cast
<MCDataFragment
>(F
).getContents();
565 case MCFragment::FT_Relaxable
:
566 ++stats::EmittedRelaxableFragments
;
567 OS
<< cast
<MCRelaxableFragment
>(F
).getContents();
570 case MCFragment::FT_CompactEncodedInst
:
571 ++stats::EmittedCompactEncodedInstFragments
;
572 OS
<< cast
<MCCompactEncodedInstFragment
>(F
).getContents();
575 case MCFragment::FT_Fill
: {
576 ++stats::EmittedFillFragments
;
577 const MCFillFragment
&FF
= cast
<MCFillFragment
>(F
);
578 uint64_t V
= FF
.getValue();
579 unsigned VSize
= FF
.getValueSize();
580 const unsigned MaxChunkSize
= 16;
581 char Data
[MaxChunkSize
];
582 // Duplicate V into Data as byte vector to reduce number of
583 // writes done. As such, do endian conversion here.
584 for (unsigned I
= 0; I
!= VSize
; ++I
) {
585 unsigned index
= Endian
== support::little
? I
: (VSize
- I
- 1);
586 Data
[I
] = uint8_t(V
>> (index
* 8));
588 for (unsigned I
= VSize
; I
< MaxChunkSize
; ++I
)
589 Data
[I
] = Data
[I
- VSize
];
591 // Set to largest multiple of VSize in Data.
592 const unsigned NumPerChunk
= MaxChunkSize
/ VSize
;
593 // Set ChunkSize to largest multiple of VSize in Data
594 const unsigned ChunkSize
= VSize
* NumPerChunk
;
596 // Do copies by chunk.
597 StringRef
Ref(Data
, ChunkSize
);
598 for (uint64_t I
= 0, E
= FragmentSize
/ ChunkSize
; I
!= E
; ++I
)
601 // do remainder if needed.
602 unsigned TrailingCount
= FragmentSize
% ChunkSize
;
604 OS
.write(Data
, TrailingCount
);
608 case MCFragment::FT_LEB
: {
609 const MCLEBFragment
&LF
= cast
<MCLEBFragment
>(F
);
610 OS
<< LF
.getContents();
614 case MCFragment::FT_Padding
: {
615 if (!Asm
.getBackend().writeNopData(OS
, FragmentSize
))
616 report_fatal_error("unable to write nop sequence of " +
617 Twine(FragmentSize
) + " bytes");
621 case MCFragment::FT_SymbolId
: {
622 const MCSymbolIdFragment
&SF
= cast
<MCSymbolIdFragment
>(F
);
623 support::endian::write
<uint32_t>(OS
, SF
.getSymbol()->getIndex(), Endian
);
627 case MCFragment::FT_Org
: {
628 ++stats::EmittedOrgFragments
;
629 const MCOrgFragment
&OF
= cast
<MCOrgFragment
>(F
);
631 for (uint64_t i
= 0, e
= FragmentSize
; i
!= e
; ++i
)
632 OS
<< char(OF
.getValue());
637 case MCFragment::FT_Dwarf
: {
638 const MCDwarfLineAddrFragment
&OF
= cast
<MCDwarfLineAddrFragment
>(F
);
639 OS
<< OF
.getContents();
642 case MCFragment::FT_DwarfFrame
: {
643 const MCDwarfCallFrameFragment
&CF
= cast
<MCDwarfCallFrameFragment
>(F
);
644 OS
<< CF
.getContents();
647 case MCFragment::FT_CVInlineLines
: {
648 const auto &OF
= cast
<MCCVInlineLineTableFragment
>(F
);
649 OS
<< OF
.getContents();
652 case MCFragment::FT_CVDefRange
: {
653 const auto &DRF
= cast
<MCCVDefRangeFragment
>(F
);
654 OS
<< DRF
.getContents();
657 case MCFragment::FT_Dummy
:
658 llvm_unreachable("Should not have been added");
661 assert(OS
.tell() - Start
== FragmentSize
&&
662 "The stream should advance by fragment size");
665 void MCAssembler::writeSectionData(raw_ostream
&OS
, const MCSection
*Sec
,
666 const MCAsmLayout
&Layout
) const {
667 assert(getBackendPtr() && "Expected assembler backend");
669 // Ignore virtual sections.
670 if (Sec
->isVirtualSection()) {
671 assert(Layout
.getSectionFileSize(Sec
) == 0 && "Invalid size for section!");
673 // Check that contents are only things legal inside a virtual section.
674 for (const MCFragment
&F
: *Sec
) {
675 switch (F
.getKind()) {
676 default: llvm_unreachable("Invalid fragment in virtual section!");
677 case MCFragment::FT_Data
: {
678 // Check that we aren't trying to write a non-zero contents (or fixups)
679 // into a virtual section. This is to support clients which use standard
680 // directives to fill the contents of virtual sections.
681 const MCDataFragment
&DF
= cast
<MCDataFragment
>(F
);
682 if (DF
.fixup_begin() != DF
.fixup_end())
683 report_fatal_error("cannot have fixups in virtual section!");
684 for (unsigned i
= 0, e
= DF
.getContents().size(); i
!= e
; ++i
)
685 if (DF
.getContents()[i
]) {
686 if (auto *ELFSec
= dyn_cast
<const MCSectionELF
>(Sec
))
687 report_fatal_error("non-zero initializer found in section '" +
688 ELFSec
->getSectionName() + "'");
690 report_fatal_error("non-zero initializer found in virtual section");
694 case MCFragment::FT_Align
:
695 // Check that we aren't trying to write a non-zero value into a virtual
697 assert((cast
<MCAlignFragment
>(F
).getValueSize() == 0 ||
698 cast
<MCAlignFragment
>(F
).getValue() == 0) &&
699 "Invalid align in virtual section!");
701 case MCFragment::FT_Fill
:
702 assert((cast
<MCFillFragment
>(F
).getValue() == 0) &&
703 "Invalid fill in virtual section!");
711 uint64_t Start
= OS
.tell();
714 for (const MCFragment
&F
: *Sec
)
715 writeFragment(OS
, *this, Layout
, F
);
717 assert(OS
.tell() - Start
== Layout
.getSectionAddressSize(Sec
));
720 std::tuple
<MCValue
, uint64_t, bool>
721 MCAssembler::handleFixup(const MCAsmLayout
&Layout
, MCFragment
&F
,
722 const MCFixup
&Fixup
) {
723 // Evaluate the fixup.
727 bool IsResolved
= evaluateFixup(Layout
, Fixup
, &F
, Target
, FixedValue
,
730 // The fixup was unresolved, we need a relocation. Inform the object
731 // writer of the relocation, and give it an opportunity to adjust the
732 // fixup value if need be.
733 if (Target
.getSymA() && Target
.getSymB() &&
734 getBackend().requiresDiffExpressionRelocations()) {
735 // The fixup represents the difference between two symbols, which the
736 // backend has indicated must be resolved at link time. Split up the fixup
737 // into two relocations, one for the add, and one for the sub, and emit
738 // both of these. The constant will be associated with the add half of the
740 MCFixup FixupAdd
= MCFixup::createAddFor(Fixup
);
742 MCValue::get(Target
.getSymA(), nullptr, Target
.getConstant());
743 getWriter().recordRelocation(*this, Layout
, &F
, FixupAdd
, TargetAdd
,
745 MCFixup FixupSub
= MCFixup::createSubFor(Fixup
);
746 MCValue TargetSub
= MCValue::get(Target
.getSymB());
747 getWriter().recordRelocation(*this, Layout
, &F
, FixupSub
, TargetSub
,
750 getWriter().recordRelocation(*this, Layout
, &F
, Fixup
, Target
,
754 return std::make_tuple(Target
, FixedValue
, IsResolved
);
757 void MCAssembler::layout(MCAsmLayout
&Layout
) {
758 assert(getBackendPtr() && "Expected assembler backend");
759 DEBUG_WITH_TYPE("mc-dump", {
760 errs() << "assembler backend - pre-layout\n--\n";
763 // Create dummy fragments and assign section ordinals.
764 unsigned SectionIndex
= 0;
765 for (MCSection
&Sec
: *this) {
766 // Create dummy fragments to eliminate any empty sections, this simplifies
768 if (Sec
.getFragmentList().empty())
769 new MCDataFragment(&Sec
);
771 Sec
.setOrdinal(SectionIndex
++);
774 // Assign layout order indices to sections and fragments.
775 for (unsigned i
= 0, e
= Layout
.getSectionOrder().size(); i
!= e
; ++i
) {
776 MCSection
*Sec
= Layout
.getSectionOrder()[i
];
777 Sec
->setLayoutOrder(i
);
779 unsigned FragmentIndex
= 0;
780 for (MCFragment
&Frag
: *Sec
)
781 Frag
.setLayoutOrder(FragmentIndex
++);
784 // Layout until everything fits.
785 while (layoutOnce(Layout
))
786 if (getContext().hadError())
789 DEBUG_WITH_TYPE("mc-dump", {
790 errs() << "assembler backend - post-relaxation\n--\n";
793 // Finalize the layout, including fragment lowering.
794 finishLayout(Layout
);
796 DEBUG_WITH_TYPE("mc-dump", {
797 errs() << "assembler backend - final-layout\n--\n";
800 // Allow the object writer a chance to perform post-layout binding (for
801 // example, to set the index fields in the symbol data).
802 getWriter().executePostLayoutBinding(*this, Layout
);
804 // Evaluate and apply the fixups, generating relocation entries as necessary.
805 for (MCSection
&Sec
: *this) {
806 for (MCFragment
&Frag
: Sec
) {
807 // Data and relaxable fragments both have fixups. So only process
809 // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups
810 // being templated makes this tricky.
811 if (isa
<MCEncodedFragment
>(&Frag
) &&
812 isa
<MCCompactEncodedInstFragment
>(&Frag
))
814 if (!isa
<MCEncodedFragment
>(&Frag
) && !isa
<MCCVDefRangeFragment
>(&Frag
) &&
815 !isa
<MCAlignFragment
>(&Frag
))
817 ArrayRef
<MCFixup
> Fixups
;
818 MutableArrayRef
<char> Contents
;
819 const MCSubtargetInfo
*STI
= nullptr;
820 if (auto *FragWithFixups
= dyn_cast
<MCDataFragment
>(&Frag
)) {
821 Fixups
= FragWithFixups
->getFixups();
822 Contents
= FragWithFixups
->getContents();
823 STI
= FragWithFixups
->getSubtargetInfo();
824 assert(!FragWithFixups
->hasInstructions() || STI
!= nullptr);
825 } else if (auto *FragWithFixups
= dyn_cast
<MCRelaxableFragment
>(&Frag
)) {
826 Fixups
= FragWithFixups
->getFixups();
827 Contents
= FragWithFixups
->getContents();
828 STI
= FragWithFixups
->getSubtargetInfo();
829 assert(!FragWithFixups
->hasInstructions() || STI
!= nullptr);
830 } else if (auto *FragWithFixups
= dyn_cast
<MCCVDefRangeFragment
>(&Frag
)) {
831 Fixups
= FragWithFixups
->getFixups();
832 Contents
= FragWithFixups
->getContents();
833 } else if (auto *FragWithFixups
= dyn_cast
<MCDwarfLineAddrFragment
>(&Frag
)) {
834 Fixups
= FragWithFixups
->getFixups();
835 Contents
= FragWithFixups
->getContents();
836 } else if (auto *AF
= dyn_cast
<MCAlignFragment
>(&Frag
)) {
837 // Insert fixup type for code alignment if the target define
838 // shouldInsertFixupForCodeAlign target hook.
839 if (Sec
.UseCodeAlign() && AF
->hasEmitNops()) {
840 getBackend().shouldInsertFixupForCodeAlign(*this, Layout
, *AF
);
844 llvm_unreachable("Unknown fragment with fixups!");
845 for (const MCFixup
&Fixup
: Fixups
) {
849 std::tie(Target
, FixedValue
, IsResolved
) =
850 handleFixup(Layout
, Frag
, Fixup
);
851 getBackend().applyFixup(*this, Fixup
, Target
, Contents
, FixedValue
,
858 void MCAssembler::Finish() {
859 // Create the layout object.
860 MCAsmLayout
Layout(*this);
863 // Write the object file.
864 stats::ObjectBytes
+= getWriter().writeObject(*this, Layout
);
867 bool MCAssembler::fixupNeedsRelaxation(const MCFixup
&Fixup
,
868 const MCRelaxableFragment
*DF
,
869 const MCAsmLayout
&Layout
) const {
870 assert(getBackendPtr() && "Expected assembler backend");
874 bool Resolved
= evaluateFixup(Layout
, Fixup
, DF
, Target
, Value
, WasForced
);
875 if (Target
.getSymA() &&
876 Target
.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8
&&
877 Fixup
.getKind() == FK_Data_1
)
879 return getBackend().fixupNeedsRelaxationAdvanced(Fixup
, Resolved
, Value
, DF
,
883 bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment
*F
,
884 const MCAsmLayout
&Layout
) const {
885 assert(getBackendPtr() && "Expected assembler backend");
886 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
887 // are intentionally pushing out inst fragments, or because we relaxed a
888 // previous instruction to one that doesn't need relaxation.
889 if (!getBackend().mayNeedRelaxation(F
->getInst(), *F
->getSubtargetInfo()))
892 for (const MCFixup
&Fixup
: F
->getFixups())
893 if (fixupNeedsRelaxation(Fixup
, F
, Layout
))
899 bool MCAssembler::relaxInstruction(MCAsmLayout
&Layout
,
900 MCRelaxableFragment
&F
) {
901 assert(getEmitterPtr() &&
902 "Expected CodeEmitter defined for relaxInstruction");
903 if (!fragmentNeedsRelaxation(&F
, Layout
))
906 ++stats::RelaxedInstructions
;
908 // FIXME-PERF: We could immediately lower out instructions if we can tell
909 // they are fully resolved, to avoid retesting on later passes.
911 // Relax the fragment.
914 getBackend().relaxInstruction(F
.getInst(), *F
.getSubtargetInfo(), Relaxed
);
916 // Encode the new instruction.
918 // FIXME-PERF: If it matters, we could let the target do this. It can
919 // probably do so more efficiently in many cases.
920 SmallVector
<MCFixup
, 4> Fixups
;
921 SmallString
<256> Code
;
922 raw_svector_ostream
VecOS(Code
);
923 getEmitter().encodeInstruction(Relaxed
, VecOS
, Fixups
, *F
.getSubtargetInfo());
925 // Update the fragment.
927 F
.getContents() = Code
;
928 F
.getFixups() = Fixups
;
933 bool MCAssembler::relaxPaddingFragment(MCAsmLayout
&Layout
,
934 MCPaddingFragment
&PF
) {
935 assert(getBackendPtr() && "Expected assembler backend");
936 uint64_t OldSize
= PF
.getSize();
937 if (!getBackend().relaxFragment(&PF
, Layout
))
939 uint64_t NewSize
= PF
.getSize();
941 ++stats::PaddingFragmentsRelaxations
;
942 stats::PaddingFragmentsBytes
+= NewSize
;
943 stats::PaddingFragmentsBytes
-= OldSize
;
947 bool MCAssembler::relaxLEB(MCAsmLayout
&Layout
, MCLEBFragment
&LF
) {
948 uint64_t OldSize
= LF
.getContents().size();
950 bool Abs
= LF
.getValue().evaluateKnownAbsolute(Value
, Layout
);
952 report_fatal_error("sleb128 and uleb128 expressions must be absolute");
953 SmallString
<8> &Data
= LF
.getContents();
955 raw_svector_ostream
OSE(Data
);
956 // The compiler can generate EH table assembly that is impossible to assemble
957 // without either adding padding to an LEB fragment or adding extra padding
958 // to a later alignment fragment. To accommodate such tables, relaxation can
959 // only increase an LEB fragment size here, not decrease it. See PR35809.
961 encodeSLEB128(Value
, OSE
, OldSize
);
963 encodeULEB128(Value
, OSE
, OldSize
);
964 return OldSize
!= LF
.getContents().size();
967 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout
&Layout
,
968 MCDwarfLineAddrFragment
&DF
) {
969 MCContext
&Context
= Layout
.getAssembler().getContext();
970 uint64_t OldSize
= DF
.getContents().size();
973 if (getBackend().requiresDiffExpressionRelocations())
974 Abs
= DF
.getAddrDelta().evaluateAsAbsolute(AddrDelta
, Layout
);
976 Abs
= DF
.getAddrDelta().evaluateKnownAbsolute(AddrDelta
, Layout
);
977 assert(Abs
&& "We created a line delta with an invalid expression");
980 LineDelta
= DF
.getLineDelta();
981 SmallVectorImpl
<char> &Data
= DF
.getContents();
983 raw_svector_ostream
OSE(Data
);
984 DF
.getFixups().clear();
987 MCDwarfLineAddr::Encode(Context
, getDWARFLinetableParams(), LineDelta
,
992 bool SetDelta
= MCDwarfLineAddr::FixedEncode(Context
,
993 getDWARFLinetableParams(),
994 LineDelta
, AddrDelta
,
995 OSE
, &Offset
, &Size
);
996 // Add Fixups for address delta or new address.
997 const MCExpr
*FixupExpr
;
999 FixupExpr
= &DF
.getAddrDelta();
1001 const MCBinaryExpr
*ABE
= cast
<MCBinaryExpr
>(&DF
.getAddrDelta());
1002 FixupExpr
= ABE
->getLHS();
1004 DF
.getFixups().push_back(
1005 MCFixup::create(Offset
, FixupExpr
,
1006 MCFixup::getKindForSize(Size
, false /*isPCRel*/)));
1009 return OldSize
!= Data
.size();
1012 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout
&Layout
,
1013 MCDwarfCallFrameFragment
&DF
) {
1014 MCContext
&Context
= Layout
.getAssembler().getContext();
1015 uint64_t OldSize
= DF
.getContents().size();
1017 bool Abs
= DF
.getAddrDelta().evaluateKnownAbsolute(AddrDelta
, Layout
);
1018 assert(Abs
&& "We created call frame with an invalid expression");
1020 SmallString
<8> &Data
= DF
.getContents();
1022 raw_svector_ostream
OSE(Data
);
1023 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context
, AddrDelta
, OSE
);
1024 return OldSize
!= Data
.size();
1027 bool MCAssembler::relaxCVInlineLineTable(MCAsmLayout
&Layout
,
1028 MCCVInlineLineTableFragment
&F
) {
1029 unsigned OldSize
= F
.getContents().size();
1030 getContext().getCVContext().encodeInlineLineTable(Layout
, F
);
1031 return OldSize
!= F
.getContents().size();
1034 bool MCAssembler::relaxCVDefRange(MCAsmLayout
&Layout
,
1035 MCCVDefRangeFragment
&F
) {
1036 unsigned OldSize
= F
.getContents().size();
1037 getContext().getCVContext().encodeDefRange(Layout
, F
);
1038 return OldSize
!= F
.getContents().size();
1041 bool MCAssembler::layoutSectionOnce(MCAsmLayout
&Layout
, MCSection
&Sec
) {
1042 // Holds the first fragment which needed relaxing during this layout. It will
1043 // remain NULL if none were relaxed.
1044 // When a fragment is relaxed, all the fragments following it should get
1045 // invalidated because their offset is going to change.
1046 MCFragment
*FirstRelaxedFragment
= nullptr;
1048 // Attempt to relax all the fragments in the section.
1049 for (MCSection::iterator I
= Sec
.begin(), IE
= Sec
.end(); I
!= IE
; ++I
) {
1050 // Check if this is a fragment that needs relaxation.
1051 bool RelaxedFrag
= false;
1052 switch(I
->getKind()) {
1055 case MCFragment::FT_Relaxable
:
1056 assert(!getRelaxAll() &&
1057 "Did not expect a MCRelaxableFragment in RelaxAll mode");
1058 RelaxedFrag
= relaxInstruction(Layout
, *cast
<MCRelaxableFragment
>(I
));
1060 case MCFragment::FT_Dwarf
:
1061 RelaxedFrag
= relaxDwarfLineAddr(Layout
,
1062 *cast
<MCDwarfLineAddrFragment
>(I
));
1064 case MCFragment::FT_DwarfFrame
:
1066 relaxDwarfCallFrameFragment(Layout
,
1067 *cast
<MCDwarfCallFrameFragment
>(I
));
1069 case MCFragment::FT_LEB
:
1070 RelaxedFrag
= relaxLEB(Layout
, *cast
<MCLEBFragment
>(I
));
1072 case MCFragment::FT_Padding
:
1073 RelaxedFrag
= relaxPaddingFragment(Layout
, *cast
<MCPaddingFragment
>(I
));
1075 case MCFragment::FT_CVInlineLines
:
1077 relaxCVInlineLineTable(Layout
, *cast
<MCCVInlineLineTableFragment
>(I
));
1079 case MCFragment::FT_CVDefRange
:
1080 RelaxedFrag
= relaxCVDefRange(Layout
, *cast
<MCCVDefRangeFragment
>(I
));
1083 if (RelaxedFrag
&& !FirstRelaxedFragment
)
1084 FirstRelaxedFragment
= &*I
;
1086 if (FirstRelaxedFragment
) {
1087 Layout
.invalidateFragmentsFrom(FirstRelaxedFragment
);
1093 bool MCAssembler::layoutOnce(MCAsmLayout
&Layout
) {
1094 ++stats::RelaxationSteps
;
1096 bool WasRelaxed
= false;
1097 for (iterator it
= begin(), ie
= end(); it
!= ie
; ++it
) {
1098 MCSection
&Sec
= *it
;
1099 while (layoutSectionOnce(Layout
, Sec
))
1106 void MCAssembler::finishLayout(MCAsmLayout
&Layout
) {
1107 assert(getBackendPtr() && "Expected assembler backend");
1108 // The layout is done. Mark every fragment as valid.
1109 for (unsigned int i
= 0, n
= Layout
.getSectionOrder().size(); i
!= n
; ++i
) {
1110 MCSection
&Section
= *Layout
.getSectionOrder()[i
];
1111 Layout
.getFragmentOffset(&*Section
.rbegin());
1112 computeFragmentSize(Layout
, *Section
.rbegin());
1114 getBackend().finishLayout(*this, Layout
);
1117 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1118 LLVM_DUMP_METHOD
void MCAssembler::dump() const{
1119 raw_ostream
&OS
= errs();
1121 OS
<< "<MCAssembler\n";
1122 OS
<< " Sections:[\n ";
1123 for (const_iterator it
= begin(), ie
= end(); it
!= ie
; ++it
) {
1124 if (it
!= begin()) OS
<< ",\n ";
1130 for (const_symbol_iterator it
= symbol_begin(), ie
= symbol_end(); it
!= ie
; ++it
) {
1131 if (it
!= symbol_begin()) OS
<< ",\n ";
1134 OS
<< ", Index:" << it
->getIndex() << ", ";