LLVM 19.0.0git
MCAssembler.cpp
Go to the documentation of this file.
1//===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/Statistic.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
17#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCAsmLayout.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"
26#include "llvm/MC/MCFragment.h"
27#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCSymbol.h"
31#include "llvm/MC/MCValue.h"
34#include "llvm/Support/Debug.h"
37#include "llvm/Support/LEB128.h"
39#include <cassert>
40#include <cstdint>
41#include <tuple>
42#include <utility>
43
44using namespace llvm;
45
46namespace llvm {
47class MCSubtargetInfo;
48}
49
50#define DEBUG_TYPE "assembler"
51
52namespace {
53namespace stats {
54
55STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
56STATISTIC(EmittedRelaxableFragments,
57 "Number of emitted assembler fragments - relaxable");
58STATISTIC(EmittedDataFragments,
59 "Number of emitted assembler fragments - data");
60STATISTIC(EmittedCompactEncodedInstFragments,
61 "Number of emitted assembler fragments - compact encoded inst");
62STATISTIC(EmittedAlignFragments,
63 "Number of emitted assembler fragments - align");
64STATISTIC(EmittedFillFragments,
65 "Number of emitted assembler fragments - fill");
66STATISTIC(EmittedNopsFragments, "Number of emitted assembler fragments - nops");
67STATISTIC(EmittedOrgFragments, "Number of emitted assembler fragments - org");
68STATISTIC(evaluateFixup, "Number of evaluated fixups");
69STATISTIC(FragmentLayouts, "Number of fragment layouts");
70STATISTIC(ObjectBytes, "Number of emitted object file bytes");
71STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
72STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
73
74} // end namespace stats
75} // end anonymous namespace
76
77// FIXME FIXME FIXME: There are number of places in this file where we convert
78// what is a 64-bit assembler value used for computation into a value in the
79// object file, which may truncate it. We should detect that truncation where
80// invalid and report errors back.
81
82/* *** */
83
85 std::unique_ptr<MCAsmBackend> Backend,
86 std::unique_ptr<MCCodeEmitter> Emitter,
87 std::unique_ptr<MCObjectWriter> Writer)
88 : Context(Context), Backend(std::move(Backend)),
89 Emitter(std::move(Emitter)), Writer(std::move(Writer)),
90 BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false),
91 IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) {
92 VersionInfo.Major = 0; // Major version == 0 for "none specified"
93 DarwinTargetVariantVersionInfo.Major = 0;
94}
95
97
99 Sections.clear();
100 Symbols.clear();
101 IndirectSymbols.clear();
102 DataRegions.clear();
103 LinkerOptions.clear();
104 FileNames.clear();
105 ThumbFuncs.clear();
106 BundleAlignSize = 0;
107 RelaxAll = false;
108 SubsectionsViaSymbols = false;
109 IncrementalLinkerCompatible = false;
110 ELFHeaderEFlags = 0;
111 LOHContainer.reset();
112 VersionInfo.Major = 0;
113 VersionInfo.SDKVersion = VersionTuple();
114 DarwinTargetVariantVersionInfo.Major = 0;
115 DarwinTargetVariantVersionInfo.SDKVersion = VersionTuple();
116
117 // reset objects owned by us
118 if (getBackendPtr())
119 getBackendPtr()->reset();
120 if (getEmitterPtr())
121 getEmitterPtr()->reset();
122 if (getWriterPtr())
123 getWriterPtr()->reset();
125}
126
128 if (Section.isRegistered())
129 return false;
130 Sections.push_back(&Section);
131 Section.setIsRegistered(true);
132 return true;
133}
134
135bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
136 if (ThumbFuncs.count(Symbol))
137 return true;
138
139 if (!Symbol->isVariable())
140 return false;
141
142 const MCExpr *Expr = Symbol->getVariableValue();
143
144 MCValue V;
145 if (!Expr->evaluateAsRelocatable(V, nullptr, nullptr))
146 return false;
147
148 if (V.getSymB() || V.getRefKind() != MCSymbolRefExpr::VK_None)
149 return false;
150
151 const MCSymbolRefExpr *Ref = V.getSymA();
152 if (!Ref)
153 return false;
154
155 if (Ref->getKind() != MCSymbolRefExpr::VK_None)
156 return false;
157
158 const MCSymbol &Sym = Ref->getSymbol();
159 if (!isThumbFunc(&Sym))
160 return false;
161
162 ThumbFuncs.insert(Symbol); // Cache it.
163 return true;
164}
165
167 // Non-temporary labels should always be visible to the linker.
168 if (!Symbol.isTemporary())
169 return true;
170
171 if (Symbol.isUsedInReloc())
172 return true;
173
174 return false;
175}
176
177const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const {
178 // Linker visible symbols define atoms.
180 return &S;
181
182 // Absolute and undefined symbols have no defining atom.
183 if (!S.isInSection())
184 return nullptr;
185
186 // Non-linker visible symbols in sections which can't be atomized have no
187 // defining atom.
188 if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
189 *S.getFragment()->getParent()))
190 return nullptr;
191
192 // Otherwise, return the atom for the containing fragment.
193 return S.getFragment()->getAtom();
194}
195
196bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
197 const MCFragment *DF, MCValue &Target,
198 const MCSubtargetInfo *STI, uint64_t &Value,
199 bool &WasForced) const {
200 ++stats::evaluateFixup;
201
202 // FIXME: This code has some duplication with recordRelocation. We should
203 // probably merge the two into a single callback that tries to evaluate a
204 // fixup and records a relocation if one is needed.
205
206 // On error claim to have completely evaluated the fixup, to prevent any
207 // further processing from being done.
208 const MCExpr *Expr = Fixup.getValue();
209 MCContext &Ctx = getContext();
210 Value = 0;
211 WasForced = false;
212 if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) {
213 Ctx.reportError(Fixup.getLoc(), "expected relocatable expression");
214 return true;
215 }
216 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
217 if (RefB->getKind() != MCSymbolRefExpr::VK_None) {
218 Ctx.reportError(Fixup.getLoc(),
219 "unsupported subtraction of qualified symbol");
220 return true;
221 }
222 }
223
224 assert(getBackendPtr() && "Expected assembler backend");
225 bool IsTarget = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags &
227
228 if (IsTarget)
229 return getBackend().evaluateTargetFixup(*this, Layout, Fixup, DF, Target,
230 STI, Value, WasForced);
231
232 unsigned FixupFlags = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags;
233 bool IsPCRel = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags &
235
236 bool IsResolved = false;
237 if (IsPCRel) {
238 if (Target.getSymB()) {
239 IsResolved = false;
240 } else if (!Target.getSymA()) {
241 IsResolved = false;
242 } else {
243 const MCSymbolRefExpr *A = Target.getSymA();
244 const MCSymbol &SA = A->getSymbol();
245 if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
246 IsResolved = false;
247 } else if (auto *Writer = getWriterPtr()) {
248 IsResolved = (FixupFlags & MCFixupKindInfo::FKF_Constant) ||
249 Writer->isSymbolRefDifferenceFullyResolvedImpl(
250 *this, SA, *DF, false, true);
251 }
252 }
253 } else {
254 IsResolved = Target.isAbsolute();
255 }
256
257 Value = Target.getConstant();
258
259 if (const MCSymbolRefExpr *A = Target.getSymA()) {
260 const MCSymbol &Sym = A->getSymbol();
261 if (Sym.isDefined())
262 Value += Layout.getSymbolOffset(Sym);
263 }
264 if (const MCSymbolRefExpr *B = Target.getSymB()) {
265 const MCSymbol &Sym = B->getSymbol();
266 if (Sym.isDefined())
267 Value -= Layout.getSymbolOffset(Sym);
268 }
269
270 bool ShouldAlignPC = getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
272 assert((ShouldAlignPC ? IsPCRel : true) &&
273 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
274
275 if (IsPCRel) {
276 uint64_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
277
278 // A number of ARM fixups in Thumb mode require that the effective PC
279 // address be determined as the 32-bit aligned version of the actual offset.
280 if (ShouldAlignPC) Offset &= ~0x3;
281 Value -= Offset;
282 }
283
284 // Let the backend force a relocation if needed.
285 if (IsResolved &&
286 getBackend().shouldForceRelocation(*this, Fixup, Target, STI)) {
287 IsResolved = false;
288 WasForced = true;
289 }
290
291 // A linker relaxation target may emit ADD/SUB relocations for A-B+C. Let
292 // recordRelocation handle non-VK_None cases like A@plt-B+C.
293 if (!IsResolved && Target.getSymA() && Target.getSymB() &&
294 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_None &&
295 getBackend().handleAddSubRelocations(Layout, *DF, Fixup, Target, Value))
296 return true;
297
298 return IsResolved;
299}
300
302 const MCFragment &F) const {
303 assert(getBackendPtr() && "Requires assembler backend");
304 switch (F.getKind()) {
306 return cast<MCDataFragment>(F).getContents().size();
308 return cast<MCRelaxableFragment>(F).getContents().size();
310 return cast<MCCompactEncodedInstFragment>(F).getContents().size();
311 case MCFragment::FT_Fill: {
312 auto &FF = cast<MCFillFragment>(F);
313 int64_t NumValues = 0;
314 if (!FF.getNumValues().evaluateKnownAbsolute(NumValues, Layout)) {
315 getContext().reportError(FF.getLoc(),
316 "expected assembly-time absolute expression");
317 return 0;
318 }
319 int64_t Size = NumValues * FF.getValueSize();
320 if (Size < 0) {
321 getContext().reportError(FF.getLoc(), "invalid number of bytes");
322 return 0;
323 }
324 return Size;
325 }
326
328 return cast<MCNopsFragment>(F).getNumBytes();
329
331 return cast<MCLEBFragment>(F).getContents().size();
332
334 return cast<MCBoundaryAlignFragment>(F).getSize();
335
337 return 4;
338
340 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
341 unsigned Offset = Layout.getFragmentOffset(&AF);
342 unsigned Size = offsetToAlignment(Offset, AF.getAlignment());
343
344 // Insert extra Nops for code alignment if the target define
345 // shouldInsertExtraNopBytesForCodeAlign target hook.
346 if (AF.getParent()->useCodeAlign() && AF.hasEmitNops() &&
347 getBackend().shouldInsertExtraNopBytesForCodeAlign(AF, Size))
348 return Size;
349
350 // If we are padding with nops, force the padding to be larger than the
351 // minimum nop size.
352 if (Size > 0 && AF.hasEmitNops()) {
353 while (Size % getBackend().getMinimumNopSize())
354 Size += AF.getAlignment().value();
355 }
356 if (Size > AF.getMaxBytesToEmit())
357 return 0;
358 return Size;
359 }
360
361 case MCFragment::FT_Org: {
362 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
364 if (!OF.getOffset().evaluateAsValue(Value, Layout)) {
366 "expected assembly-time absolute expression");
367 return 0;
368 }
369
370 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
371 int64_t TargetLocation = Value.getConstant();
372 if (const MCSymbolRefExpr *A = Value.getSymA()) {
373 uint64_t Val;
374 if (!Layout.getSymbolOffset(A->getSymbol(), Val)) {
375 getContext().reportError(OF.getLoc(), "expected absolute expression");
376 return 0;
377 }
378 TargetLocation += Val;
379 }
380 int64_t Size = TargetLocation - FragmentOffset;
381 if (Size < 0 || Size >= 0x40000000) {
383 OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
384 "' (at offset '" + Twine(FragmentOffset) + "')");
385 return 0;
386 }
387 return Size;
388 }
389
391 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
393 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
395 return cast<MCCVInlineLineTableFragment>(F).getContents().size();
397 return cast<MCCVDefRangeFragment>(F).getContents().size();
399 return cast<MCPseudoProbeAddrFragment>(F).getContents().size();
401 llvm_unreachable("Should not have been added");
402 }
403
404 llvm_unreachable("invalid fragment kind");
405}
406
408 MCFragment *Prev = F->getPrevNode();
409
410 // We should never try to recompute something which is valid.
411 assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
412 // We should never try to compute the fragment layout if its predecessor
413 // isn't valid.
414 assert((!Prev || isFragmentValid(Prev)) &&
415 "Attempt to compute fragment before its predecessor!");
416
417 assert(!F->IsBeingLaidOut && "Already being laid out!");
418 F->IsBeingLaidOut = true;
419
420 ++stats::FragmentLayouts;
421
422 // Compute fragment offset and size.
423 if (Prev)
424 F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
425 else
426 F->Offset = 0;
427 F->IsBeingLaidOut = false;
428 LastValidFragment[F->getParent()] = F;
429
430 // If bundling is enabled and this fragment has instructions in it, it has to
431 // obey the bundling restrictions. With padding, we'll have:
432 //
433 //
434 // BundlePadding
435 // |||
436 // -------------------------------------
437 // Prev |##########| F |
438 // -------------------------------------
439 // ^
440 // |
441 // F->Offset
442 //
443 // The fragment's offset will point to after the padding, and its computed
444 // size won't include the padding.
445 //
446 // When the -mc-relax-all flag is used, we optimize bundling by writting the
447 // padding directly into fragments when the instructions are emitted inside
448 // the streamer. When the fragment is larger than the bundle size, we need to
449 // ensure that it's bundle aligned. This means that if we end up with
450 // multiple fragments, we must emit bundle padding between fragments.
451 //
452 // ".align N" is an example of a directive that introduces multiple
453 // fragments. We could add a special case to handle ".align N" by emitting
454 // within-fragment padding (which would produce less padding when N is less
455 // than the bundle size), but for now we don't.
456 //
457 if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
458 assert(isa<MCEncodedFragment>(F) &&
459 "Only MCEncodedFragment implementations have instructions");
460 MCEncodedFragment *EF = cast<MCEncodedFragment>(F);
461 uint64_t FSize = Assembler.computeFragmentSize(*this, *EF);
462
463 if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize())
464 report_fatal_error("Fragment can't be larger than a bundle size");
465
466 uint64_t RequiredBundlePadding =
467 computeBundlePadding(Assembler, EF, EF->Offset, FSize);
468 if (RequiredBundlePadding > UINT8_MAX)
469 report_fatal_error("Padding cannot exceed 255 bytes");
470 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
471 EF->Offset += RequiredBundlePadding;
472 }
473}
474
476 bool Changed = !Symbol.isRegistered();
477 if (Changed) {
478 Symbol.setIsRegistered(true);
479 Symbols.push_back(&Symbol);
480 }
481 return Changed;
482}
483
485 const MCEncodedFragment &EF,
486 uint64_t FSize) const {
487 assert(getBackendPtr() && "Expected assembler backend");
488 // Should NOP padding be written out before this fragment?
489 unsigned BundlePadding = EF.getBundlePadding();
490 if (BundlePadding > 0) {
492 "Writing bundle padding with disabled bundling");
493 assert(EF.hasInstructions() &&
494 "Writing bundle padding for a fragment without instructions");
495
496 unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
497 const MCSubtargetInfo *STI = EF.getSubtargetInfo();
498 if (EF.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
499 // If the padding itself crosses a bundle boundary, it must be emitted
500 // in 2 pieces, since even nop instructions must not cross boundaries.
501 // v--------------v <- BundleAlignSize
502 // v---------v <- BundlePadding
503 // ----------------------------
504 // | Prev |####|####| F |
505 // ----------------------------
506 // ^-------------------^ <- TotalLength
507 unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
508 if (!getBackend().writeNopData(OS, DistanceToBoundary, STI))
509 report_fatal_error("unable to write NOP sequence of " +
510 Twine(DistanceToBoundary) + " bytes");
511 BundlePadding -= DistanceToBoundary;
512 }
513 if (!getBackend().writeNopData(OS, BundlePadding, STI))
514 report_fatal_error("unable to write NOP sequence of " +
515 Twine(BundlePadding) + " bytes");
516 }
517}
518
519/// Write the fragment \p F to the output file.
520static void writeFragment(raw_ostream &OS, const MCAssembler &Asm,
521 const MCAsmLayout &Layout, const MCFragment &F) {
522 // FIXME: Embed in fragments instead?
523 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
524
525 llvm::endianness Endian = Asm.getBackend().Endian;
526
527 if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(&F))
528 Asm.writeFragmentPadding(OS, *EF, FragmentSize);
529
530 // This variable (and its dummy usage) is to participate in the assert at
531 // the end of the function.
532 uint64_t Start = OS.tell();
533 (void) Start;
534
535 ++stats::EmittedFragments;
536
537 switch (F.getKind()) {
539 ++stats::EmittedAlignFragments;
540 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
541 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
542
543 uint64_t Count = FragmentSize / AF.getValueSize();
544
545 // FIXME: This error shouldn't actually occur (the front end should emit
546 // multiple .align directives to enforce the semantics it wants), but is
547 // severe enough that we want to report it. How to handle this?
548 if (Count * AF.getValueSize() != FragmentSize)
549 report_fatal_error("undefined .align directive, value size '" +
550 Twine(AF.getValueSize()) +
551 "' is not a divisor of padding size '" +
552 Twine(FragmentSize) + "'");
553
554 // See if we are aligning with nops, and if so do that first to try to fill
555 // the Count bytes. Then if that did not fill any bytes or there are any
556 // bytes left to fill use the Value and ValueSize to fill the rest.
557 // If we are aligning with nops, ask that target to emit the right data.
558 if (AF.hasEmitNops()) {
559 if (!Asm.getBackend().writeNopData(OS, Count, AF.getSubtargetInfo()))
560 report_fatal_error("unable to write nop sequence of " +
561 Twine(Count) + " bytes");
562 break;
563 }
564
565 // Otherwise, write out in multiples of the value size.
566 for (uint64_t i = 0; i != Count; ++i) {
567 switch (AF.getValueSize()) {
568 default: llvm_unreachable("Invalid size!");
569 case 1: OS << char(AF.getValue()); break;
570 case 2:
571 support::endian::write<uint16_t>(OS, AF.getValue(), Endian);
572 break;
573 case 4:
574 support::endian::write<uint32_t>(OS, AF.getValue(), Endian);
575 break;
576 case 8:
577 support::endian::write<uint64_t>(OS, AF.getValue(), Endian);
578 break;
579 }
580 }
581 break;
582 }
583
585 ++stats::EmittedDataFragments;
586 OS << cast<MCDataFragment>(F).getContents();
587 break;
588
590 ++stats::EmittedRelaxableFragments;
591 OS << cast<MCRelaxableFragment>(F).getContents();
592 break;
593
595 ++stats::EmittedCompactEncodedInstFragments;
596 OS << cast<MCCompactEncodedInstFragment>(F).getContents();
597 break;
598
599 case MCFragment::FT_Fill: {
600 ++stats::EmittedFillFragments;
601 const MCFillFragment &FF = cast<MCFillFragment>(F);
602 uint64_t V = FF.getValue();
603 unsigned VSize = FF.getValueSize();
604 const unsigned MaxChunkSize = 16;
605 char Data[MaxChunkSize];
606 assert(0 < VSize && VSize <= MaxChunkSize && "Illegal fragment fill size");
607 // Duplicate V into Data as byte vector to reduce number of
608 // writes done. As such, do endian conversion here.
609 for (unsigned I = 0; I != VSize; ++I) {
610 unsigned index = Endian == llvm::endianness::little ? I : (VSize - I - 1);
611 Data[I] = uint8_t(V >> (index * 8));
612 }
613 for (unsigned I = VSize; I < MaxChunkSize; ++I)
614 Data[I] = Data[I - VSize];
615
616 // Set to largest multiple of VSize in Data.
617 const unsigned NumPerChunk = MaxChunkSize / VSize;
618 // Set ChunkSize to largest multiple of VSize in Data
619 const unsigned ChunkSize = VSize * NumPerChunk;
620
621 // Do copies by chunk.
622 StringRef Ref(Data, ChunkSize);
623 for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I)
624 OS << Ref;
625
626 // do remainder if needed.
627 unsigned TrailingCount = FragmentSize % ChunkSize;
628 if (TrailingCount)
629 OS.write(Data, TrailingCount);
630 break;
631 }
632
633 case MCFragment::FT_Nops: {
634 ++stats::EmittedNopsFragments;
635 const MCNopsFragment &NF = cast<MCNopsFragment>(F);
636
637 int64_t NumBytes = NF.getNumBytes();
638 int64_t ControlledNopLength = NF.getControlledNopLength();
639 int64_t MaximumNopLength =
640 Asm.getBackend().getMaximumNopSize(*NF.getSubtargetInfo());
641
642 assert(NumBytes > 0 && "Expected positive NOPs fragment size");
643 assert(ControlledNopLength >= 0 && "Expected non-negative NOP size");
644
645 if (ControlledNopLength > MaximumNopLength) {
646 Asm.getContext().reportError(NF.getLoc(),
647 "illegal NOP size " +
648 std::to_string(ControlledNopLength) +
649 ". (expected within [0, " +
650 std::to_string(MaximumNopLength) + "])");
651 // Clamp the NOP length as reportError does not stop the execution
652 // immediately.
653 ControlledNopLength = MaximumNopLength;
654 }
655
656 // Use maximum value if the size of each NOP is not specified
657 if (!ControlledNopLength)
658 ControlledNopLength = MaximumNopLength;
659
660 while (NumBytes) {
661 uint64_t NumBytesToEmit =
662 (uint64_t)std::min(NumBytes, ControlledNopLength);
663 assert(NumBytesToEmit && "try to emit empty NOP instruction");
664 if (!Asm.getBackend().writeNopData(OS, NumBytesToEmit,
665 NF.getSubtargetInfo())) {
666 report_fatal_error("unable to write nop sequence of the remaining " +
667 Twine(NumBytesToEmit) + " bytes");
668 break;
669 }
670 NumBytes -= NumBytesToEmit;
671 }
672 break;
673 }
674
675 case MCFragment::FT_LEB: {
676 const MCLEBFragment &LF = cast<MCLEBFragment>(F);
677 OS << LF.getContents();
678 break;
679 }
680
682 const MCBoundaryAlignFragment &BF = cast<MCBoundaryAlignFragment>(F);
683 if (!Asm.getBackend().writeNopData(OS, FragmentSize, BF.getSubtargetInfo()))
684 report_fatal_error("unable to write nop sequence of " +
685 Twine(FragmentSize) + " bytes");
686 break;
687 }
688
690 const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F);
691 support::endian::write<uint32_t>(OS, SF.getSymbol()->getIndex(), Endian);
692 break;
693 }
694
695 case MCFragment::FT_Org: {
696 ++stats::EmittedOrgFragments;
697 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
698
699 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
700 OS << char(OF.getValue());
701
702 break;
703 }
704
706 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
707 OS << OF.getContents();
708 break;
709 }
711 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
712 OS << CF.getContents();
713 break;
714 }
716 const auto &OF = cast<MCCVInlineLineTableFragment>(F);
717 OS << OF.getContents();
718 break;
719 }
721 const auto &DRF = cast<MCCVDefRangeFragment>(F);
722 OS << DRF.getContents();
723 break;
724 }
726 const MCPseudoProbeAddrFragment &PF = cast<MCPseudoProbeAddrFragment>(F);
727 OS << PF.getContents();
728 break;
729 }
731 llvm_unreachable("Should not have been added");
732 }
733
734 assert(OS.tell() - Start == FragmentSize &&
735 "The stream should advance by fragment size");
736}
737
739 const MCAsmLayout &Layout) const {
740 assert(getBackendPtr() && "Expected assembler backend");
741
742 // Ignore virtual sections.
743 if (Sec->isVirtualSection()) {
744 assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!");
745
746 // Check that contents are only things legal inside a virtual section.
747 for (const MCFragment &F : *Sec) {
748 switch (F.getKind()) {
749 default: llvm_unreachable("Invalid fragment in virtual section!");
750 case MCFragment::FT_Data: {
751 // Check that we aren't trying to write a non-zero contents (or fixups)
752 // into a virtual section. This is to support clients which use standard
753 // directives to fill the contents of virtual sections.
754 const MCDataFragment &DF = cast<MCDataFragment>(F);
755 if (DF.fixup_begin() != DF.fixup_end())
756 getContext().reportError(SMLoc(), Sec->getVirtualSectionKind() +
757 " section '" + Sec->getName() +
758 "' cannot have fixups");
759 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
760 if (DF.getContents()[i]) {
762 Sec->getVirtualSectionKind() +
763 " section '" + Sec->getName() +
764 "' cannot have non-zero initializers");
765 break;
766 }
767 break;
768 }
770 // Check that we aren't trying to write a non-zero value into a virtual
771 // section.
772 assert((cast<MCAlignFragment>(F).getValueSize() == 0 ||
773 cast<MCAlignFragment>(F).getValue() == 0) &&
774 "Invalid align in virtual section!");
775 break;
777 assert((cast<MCFillFragment>(F).getValue() == 0) &&
778 "Invalid fill in virtual section!");
779 break;
781 break;
782 }
783 }
784
785 return;
786 }
787
788 uint64_t Start = OS.tell();
789 (void)Start;
790
791 for (const MCFragment &F : *Sec)
792 writeFragment(OS, *this, Layout, F);
793
794 assert(getContext().hadError() ||
795 OS.tell() - Start == Layout.getSectionAddressSize(Sec));
796}
797
798std::tuple<MCValue, uint64_t, bool>
799MCAssembler::handleFixup(const MCAsmLayout &Layout, MCFragment &F,
800 const MCFixup &Fixup, const MCSubtargetInfo *STI) {
801 // Evaluate the fixup.
803 uint64_t FixedValue;
804 bool WasForced;
805 bool IsResolved =
806 evaluateFixup(Layout, Fixup, &F, Target, STI, FixedValue, WasForced);
807 if (!IsResolved) {
808 // The fixup was unresolved, we need a relocation. Inform the object
809 // writer of the relocation, and give it an opportunity to adjust the
810 // fixup value if need be.
811 getWriter().recordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
812 }
813 return std::make_tuple(Target, FixedValue, IsResolved);
814}
815
817 assert(getBackendPtr() && "Expected assembler backend");
818 DEBUG_WITH_TYPE("mc-dump", {
819 errs() << "assembler backend - pre-layout\n--\n";
820 dump(); });
821
822 // Create dummy fragments and assign section ordinals.
823 unsigned SectionIndex = 0;
824 for (MCSection &Sec : *this) {
825 // Create dummy fragments to eliminate any empty sections, this simplifies
826 // layout.
827 if (Sec.getFragmentList().empty())
828 new MCDataFragment(&Sec);
829
830 Sec.setOrdinal(SectionIndex++);
831 }
832
833 // Assign layout order indices to sections and fragments.
834 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
835 MCSection *Sec = Layout.getSectionOrder()[i];
836 Sec->setLayoutOrder(i);
837
838 unsigned FragmentIndex = 0;
839 for (MCFragment &Frag : *Sec)
840 Frag.setLayoutOrder(FragmentIndex++);
841 }
842
843 // Layout until everything fits.
844 while (layoutOnce(Layout)) {
845 if (getContext().hadError())
846 return;
847 // Size of fragments in one section can depend on the size of fragments in
848 // another. If any fragment has changed size, we have to re-layout (and
849 // as a result possibly further relax) all.
850 for (MCSection &Sec : *this)
851 Layout.invalidateFragmentsFrom(&*Sec.begin());
852 }
853
854 DEBUG_WITH_TYPE("mc-dump", {
855 errs() << "assembler backend - post-relaxation\n--\n";
856 dump(); });
857
858 // Finalize the layout, including fragment lowering.
859 finishLayout(Layout);
860
861 DEBUG_WITH_TYPE("mc-dump", {
862 errs() << "assembler backend - final-layout\n--\n";
863 dump(); });
864
865 // Allow the object writer a chance to perform post-layout binding (for
866 // example, to set the index fields in the symbol data).
867 getWriter().executePostLayoutBinding(*this, Layout);
868
869 // Evaluate and apply the fixups, generating relocation entries as necessary.
870 for (MCSection &Sec : *this) {
871 for (MCFragment &Frag : Sec) {
872 ArrayRef<MCFixup> Fixups;
873 MutableArrayRef<char> Contents;
874 const MCSubtargetInfo *STI = nullptr;
875
876 // Process MCAlignFragment and MCEncodedFragmentWithFixups here.
877 switch (Frag.getKind()) {
878 default:
879 continue;
881 MCAlignFragment &AF = cast<MCAlignFragment>(Frag);
882 // Insert fixup type for code alignment if the target define
883 // shouldInsertFixupForCodeAlign target hook.
884 if (Sec.useCodeAlign() && AF.hasEmitNops())
885 getBackend().shouldInsertFixupForCodeAlign(*this, Layout, AF);
886 continue;
887 }
888 case MCFragment::FT_Data: {
889 MCDataFragment &DF = cast<MCDataFragment>(Frag);
890 Fixups = DF.getFixups();
891 Contents = DF.getContents();
892 STI = DF.getSubtargetInfo();
893 assert(!DF.hasInstructions() || STI != nullptr);
894 break;
895 }
897 MCRelaxableFragment &RF = cast<MCRelaxableFragment>(Frag);
898 Fixups = RF.getFixups();
899 Contents = RF.getContents();
900 STI = RF.getSubtargetInfo();
901 assert(!RF.hasInstructions() || STI != nullptr);
902 break;
903 }
905 MCCVDefRangeFragment &CF = cast<MCCVDefRangeFragment>(Frag);
906 Fixups = CF.getFixups();
907 Contents = CF.getContents();
908 break;
909 }
911 MCDwarfLineAddrFragment &DF = cast<MCDwarfLineAddrFragment>(Frag);
912 Fixups = DF.getFixups();
913 Contents = DF.getContents();
914 break;
915 }
917 MCDwarfCallFrameFragment &DF = cast<MCDwarfCallFrameFragment>(Frag);
918 Fixups = DF.getFixups();
919 Contents = DF.getContents();
920 break;
921 }
922 case MCFragment::FT_LEB: {
923 auto &LF = cast<MCLEBFragment>(Frag);
924 Fixups = LF.getFixups();
925 Contents = LF.getContents();
926 break;
927 }
929 MCPseudoProbeAddrFragment &PF = cast<MCPseudoProbeAddrFragment>(Frag);
930 Fixups = PF.getFixups();
931 Contents = PF.getContents();
932 break;
933 }
934 }
935 for (const MCFixup &Fixup : Fixups) {
936 uint64_t FixedValue;
937 bool IsResolved;
939 std::tie(Target, FixedValue, IsResolved) =
940 handleFixup(Layout, Frag, Fixup, STI);
941 getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue,
942 IsResolved, STI);
943 }
944 }
945 }
946}
947
949 // Create the layout object.
950 MCAsmLayout Layout(*this);
951 layout(Layout);
952
953 // Write the object file.
954 stats::ObjectBytes += getWriter().writeObject(*this, Layout);
955}
956
957bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
958 const MCRelaxableFragment *DF,
959 const MCAsmLayout &Layout) const {
960 assert(getBackendPtr() && "Expected assembler backend");
963 bool WasForced;
964 bool Resolved = evaluateFixup(Layout, Fixup, DF, Target,
965 DF->getSubtargetInfo(), Value, WasForced);
966 if (Target.getSymA() &&
967 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8 &&
968 Fixup.getKind() == FK_Data_1)
969 return false;
971 Layout, WasForced);
972}
973
974bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
975 const MCAsmLayout &Layout) const {
976 assert(getBackendPtr() && "Expected assembler backend");
977 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
978 // are intentionally pushing out inst fragments, or because we relaxed a
979 // previous instruction to one that doesn't need relaxation.
980 if (!getBackend().mayNeedRelaxation(F->getInst(), *F->getSubtargetInfo()))
981 return false;
982
983 for (const MCFixup &Fixup : F->getFixups())
984 if (fixupNeedsRelaxation(Fixup, F, Layout))
985 return true;
986
987 return false;
988}
989
990bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
993 "Expected CodeEmitter defined for relaxInstruction");
994 if (!fragmentNeedsRelaxation(&F, Layout))
995 return false;
996
997 ++stats::RelaxedInstructions;
998
999 // FIXME-PERF: We could immediately lower out instructions if we can tell
1000 // they are fully resolved, to avoid retesting on later passes.
1001
1002 // Relax the fragment.
1003
1004 MCInst Relaxed = F.getInst();
1005 getBackend().relaxInstruction(Relaxed, *F.getSubtargetInfo());
1006
1007 // Encode the new instruction.
1008 F.setInst(Relaxed);
1009 F.getFixups().clear();
1010 F.getContents().clear();
1011 getEmitter().encodeInstruction(Relaxed, F.getContents(), F.getFixups(),
1012 *F.getSubtargetInfo());
1013 return true;
1014}
1015
1016bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
1017 const unsigned OldSize = static_cast<unsigned>(LF.getContents().size());
1018 unsigned PadTo = OldSize;
1019 int64_t Value;
1021 LF.getFixups().clear();
1022 // Use evaluateKnownAbsolute for Mach-O as a hack: .subsections_via_symbols
1023 // requires that .uleb128 A-B is foldable where A and B reside in different
1024 // fragments. This is used by __gcc_except_table.
1025 bool Abs = getSubsectionsViaSymbols()
1026 ? LF.getValue().evaluateKnownAbsolute(Value, Layout)
1027 : LF.getValue().evaluateAsAbsolute(Value, Layout);
1028 if (!Abs) {
1029 bool Relaxed, UseZeroPad;
1030 std::tie(Relaxed, UseZeroPad) = getBackend().relaxLEB128(LF, Layout, Value);
1031 if (!Relaxed) {
1033 Twine(LF.isSigned() ? ".s" : ".u") +
1034 "leb128 expression is not absolute");
1035 LF.setValue(MCConstantExpr::create(0, Context));
1036 }
1037 uint8_t Tmp[10]; // maximum size: ceil(64/7)
1038 PadTo = std::max(PadTo, encodeULEB128(uint64_t(Value), Tmp));
1039 if (UseZeroPad)
1040 Value = 0;
1041 }
1042 Data.clear();
1044 // The compiler can generate EH table assembly that is impossible to assemble
1045 // without either adding padding to an LEB fragment or adding extra padding
1046 // to a later alignment fragment. To accommodate such tables, relaxation can
1047 // only increase an LEB fragment size here, not decrease it. See PR35809.
1048 if (LF.isSigned())
1049 encodeSLEB128(Value, OSE, PadTo);
1050 else
1051 encodeULEB128(Value, OSE, PadTo);
1052 return OldSize != LF.getContents().size();
1053}
1054
1055/// Check if the branch crosses the boundary.
1056///
1057/// \param StartAddr start address of the fused/unfused branch.
1058/// \param Size size of the fused/unfused branch.
1059/// \param BoundaryAlignment alignment requirement of the branch.
1060/// \returns true if the branch cross the boundary.
1062 Align BoundaryAlignment) {
1063 uint64_t EndAddr = StartAddr + Size;
1064 return (StartAddr >> Log2(BoundaryAlignment)) !=
1065 ((EndAddr - 1) >> Log2(BoundaryAlignment));
1066}
1067
1068/// Check if the branch is against the boundary.
1069///
1070/// \param StartAddr start address of the fused/unfused branch.
1071/// \param Size size of the fused/unfused branch.
1072/// \param BoundaryAlignment alignment requirement of the branch.
1073/// \returns true if the branch is against the boundary.
1075 Align BoundaryAlignment) {
1076 uint64_t EndAddr = StartAddr + Size;
1077 return (EndAddr & (BoundaryAlignment.value() - 1)) == 0;
1078}
1079
1080/// Check if the branch needs padding.
1081///
1082/// \param StartAddr start address of the fused/unfused branch.
1083/// \param Size size of the fused/unfused branch.
1084/// \param BoundaryAlignment alignment requirement of the branch.
1085/// \returns true if the branch needs padding.
1086static bool needPadding(uint64_t StartAddr, uint64_t Size,
1087 Align BoundaryAlignment) {
1088 return mayCrossBoundary(StartAddr, Size, BoundaryAlignment) ||
1089 isAgainstBoundary(StartAddr, Size, BoundaryAlignment);
1090}
1091
1092bool MCAssembler::relaxBoundaryAlign(MCAsmLayout &Layout,
1094 // BoundaryAlignFragment that doesn't need to align any fragment should not be
1095 // relaxed.
1096 if (!BF.getLastFragment())
1097 return false;
1098
1099 uint64_t AlignedOffset = Layout.getFragmentOffset(&BF);
1100 uint64_t AlignedSize = 0;
1101 for (const MCFragment *F = BF.getLastFragment(); F != &BF;
1102 F = F->getPrevNode())
1103 AlignedSize += computeFragmentSize(Layout, *F);
1104
1105 Align BoundaryAlignment = BF.getAlignment();
1106 uint64_t NewSize = needPadding(AlignedOffset, AlignedSize, BoundaryAlignment)
1107 ? offsetToAlignment(AlignedOffset, BoundaryAlignment)
1108 : 0U;
1109 if (NewSize == BF.getSize())
1110 return false;
1111 BF.setSize(NewSize);
1112 Layout.invalidateFragmentsFrom(&BF);
1113 return true;
1114}
1115
1116bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
1118
1119 bool WasRelaxed;
1120 if (getBackend().relaxDwarfLineAddr(DF, Layout, WasRelaxed))
1121 return WasRelaxed;
1122
1123 MCContext &Context = Layout.getAssembler().getContext();
1124 uint64_t OldSize = DF.getContents().size();
1125 int64_t AddrDelta;
1126 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
1127 assert(Abs && "We created a line delta with an invalid expression");
1128 (void)Abs;
1129 int64_t LineDelta;
1130 LineDelta = DF.getLineDelta();
1131 SmallVectorImpl<char> &Data = DF.getContents();
1132 Data.clear();
1133 DF.getFixups().clear();
1134
1136 AddrDelta, Data);
1137 return OldSize != Data.size();
1138}
1139
1140bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
1142 bool WasRelaxed;
1143 if (getBackend().relaxDwarfCFA(DF, Layout, WasRelaxed))
1144 return WasRelaxed;
1145
1147 int64_t Value;
1148 bool Abs = DF.getAddrDelta().evaluateAsAbsolute(Value, Layout);
1149 if (!Abs) {
1150 getContext().reportError(DF.getAddrDelta().getLoc(),
1151 "invalid CFI advance_loc expression");
1152 DF.setAddrDelta(MCConstantExpr::create(0, Context));
1153 return false;
1154 }
1155
1156 SmallVectorImpl<char> &Data = DF.getContents();
1157 uint64_t OldSize = Data.size();
1158 Data.clear();
1159 DF.getFixups().clear();
1160
1162 return OldSize != Data.size();
1163}
1164
1165bool MCAssembler::relaxCVInlineLineTable(MCAsmLayout &Layout,
1167 unsigned OldSize = F.getContents().size();
1169 return OldSize != F.getContents().size();
1170}
1171
1172bool MCAssembler::relaxCVDefRange(MCAsmLayout &Layout,
1174 unsigned OldSize = F.getContents().size();
1176 return OldSize != F.getContents().size();
1177}
1178
1179bool MCAssembler::relaxPseudoProbeAddr(MCAsmLayout &Layout,
1181 uint64_t OldSize = PF.getContents().size();
1182 int64_t AddrDelta;
1183 bool Abs = PF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
1184 assert(Abs && "We created a pseudo probe with an invalid expression");
1185 (void)Abs;
1187 Data.clear();
1189 PF.getFixups().clear();
1190
1191 // AddrDelta is a signed integer
1192 encodeSLEB128(AddrDelta, OSE, OldSize);
1193 return OldSize != Data.size();
1194}
1195
1196bool MCAssembler::relaxFragment(MCAsmLayout &Layout, MCFragment &F) {
1197 switch(F.getKind()) {
1198 default:
1199 return false;
1201 assert(!getRelaxAll() &&
1202 "Did not expect a MCRelaxableFragment in RelaxAll mode");
1203 return relaxInstruction(Layout, cast<MCRelaxableFragment>(F));
1205 return relaxDwarfLineAddr(Layout, cast<MCDwarfLineAddrFragment>(F));
1207 return relaxDwarfCallFrameFragment(Layout,
1208 cast<MCDwarfCallFrameFragment>(F));
1209 case MCFragment::FT_LEB:
1210 return relaxLEB(Layout, cast<MCLEBFragment>(F));
1212 return relaxBoundaryAlign(Layout, cast<MCBoundaryAlignFragment>(F));
1214 return relaxCVInlineLineTable(Layout, cast<MCCVInlineLineTableFragment>(F));
1216 return relaxCVDefRange(Layout, cast<MCCVDefRangeFragment>(F));
1218 return relaxPseudoProbeAddr(Layout, cast<MCPseudoProbeAddrFragment>(F));
1219 }
1220}
1221
1222bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) {
1223 // Holds the first fragment which needed relaxing during this layout. It will
1224 // remain NULL if none were relaxed.
1225 // When a fragment is relaxed, all the fragments following it should get
1226 // invalidated because their offset is going to change.
1227 MCFragment *FirstRelaxedFragment = nullptr;
1228
1229 // Attempt to relax all the fragments in the section.
1230 for (MCFragment &Frag : Sec) {
1231 // Check if this is a fragment that needs relaxation.
1232 bool RelaxedFrag = relaxFragment(Layout, Frag);
1233 if (RelaxedFrag && !FirstRelaxedFragment)
1234 FirstRelaxedFragment = &Frag;
1235 }
1236 if (FirstRelaxedFragment) {
1237 Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
1238 return true;
1239 }
1240 return false;
1241}
1242
1243bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
1244 ++stats::RelaxationSteps;
1245
1246 bool WasRelaxed = false;
1247 for (MCSection &Sec : *this) {
1248 while (layoutSectionOnce(Layout, Sec))
1249 WasRelaxed = true;
1250 }
1251
1252 return WasRelaxed;
1253}
1254
1255void MCAssembler::finishLayout(MCAsmLayout &Layout) {
1256 assert(getBackendPtr() && "Expected assembler backend");
1257 // The layout is done. Mark every fragment as valid.
1258 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
1259 MCSection &Section = *Layout.getSectionOrder()[i];
1260 Layout.getFragmentOffset(&*Section.getFragmentList().rbegin());
1261 computeFragmentSize(Layout, *Section.getFragmentList().rbegin());
1262 }
1263 getBackend().finishLayout(*this, Layout);
1264}
1265
1266#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1268 raw_ostream &OS = errs();
1269
1270 OS << "<MCAssembler\n";
1271 OS << " Sections:[\n ";
1272 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
1273 if (it != begin()) OS << ",\n ";
1274 it->dump();
1275 }
1276 OS << "],\n";
1277 OS << " Symbols:[";
1278
1279 for (const_symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1280 if (it != symbol_begin()) OS << ",\n ";
1281 OS << "(";
1282 it->dump();
1283 OS << ", Index:" << it->getIndex() << ", ";
1284 OS << ")";
1285 }
1286 OS << "]>\n";
1287}
1288#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:537
dxil DXContainer Global Emitter
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static void writeFragment(raw_ostream &OS, const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment &F)
Write the fragment F to the output file.
static bool needPadding(uint64_t StartAddr, uint64_t Size, Align BoundaryAlignment)
Check if the branch needs padding.
static bool mayCrossBoundary(uint64_t StartAddr, uint64_t Size, Align BoundaryAlignment)
Check if the branch crosses the boundary.
static bool isAgainstBoundary(uint64_t StartAddr, uint64_t Size, Align BoundaryAlignment)
Check if the branch is against the boundary.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMContext & Context
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
endianness Endian
raw_pwrite_stream & OS
This file defines the SmallString class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
void encodeInlineLineTable(MCAsmLayout &Layout, MCCVInlineLineTableFragment &F)
Encodes the binary annotations once we have a layout.
Definition: MCCodeView.cpp:485
void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F)
Definition: MCCodeView.cpp:622
int64_t getValue() const
Definition: MCFragment.h:328
Align getAlignment() const
Definition: MCFragment.h:326
unsigned getMaxBytesToEmit() const
Definition: MCFragment.h:332
bool hasEmitNops() const
Definition: MCFragment.h:334
unsigned getValueSize() const
Definition: MCFragment.h:330
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCFragment.h:340
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, const MCAsmLayout &Layout, MCAlignFragment &AF)
Hook which indicates if the target requires a fixup to be generated when handling an align directive ...
Definition: MCAsmBackend.h:119
virtual void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const
Relax the instruction in the given fragment to the next wider instruction.
Definition: MCAsmBackend.h:186
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:228
virtual bool evaluateTargetFixup(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFixup &Fixup, const MCFragment *DF, const MCValue &Target, const MCSubtargetInfo *STI, uint64_t &Value, bool &WasForced)
Definition: MCAsmBackend.h:125
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:73
virtual std::pair< bool, bool > relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, int64_t &Value) const
Definition: MCAsmBackend.h:202
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
void invalidateFragmentsFrom(MCFragment *F)
Invalidate the fragments starting with F because it has been resized.
Definition: MCFragment.cpp:70
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:198
llvm::SmallVectorImpl< MCSection * > & getSectionOrder()
Definition: MCAsmLayout.h:69
uint64_t getSectionFileSize(const MCSection *Sec) const
Get the data size of the given section, as emitted to the object file.
Definition: MCFragment.cpp:204
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
Definition: MCFragment.cpp:152
void layoutFragment(MCFragment *Fragment)
Perform layout for a single fragment, assuming that the previous fragment has already been laid out c...
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:96
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
MCContext & getContext() const
Definition: MCAssembler.h:326
void Finish()
Finish - Do final processing and write the object to the output stream.
void writeSectionData(raw_ostream &OS, const MCSection *Section, const MCAsmLayout &Layout) const
Emit the section contents to OS.
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:367
bool isBundlingEnabled() const
Definition: MCAssembler.h:365
symbol_iterator symbol_begin()
Definition: MCAssembler.h:389
bool getSubsectionsViaSymbols() const
Definition: MCAssembler.h:352
void dump() const
void layout(MCAsmLayout &Layout)
MCObjectWriter * getWriterPtr() const
Definition: MCAssembler.h:332
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:338
uint64_t computeFragmentSize(const MCAsmLayout &Layout, const MCFragment &F) const
Compute the effective fragment size assuming it is laid out at the given SectionAddress and FragmentO...
MCCodeEmitter * getEmitterPtr() const
Definition: MCAssembler.h:330
bool getRelaxAll() const
Definition: MCAssembler.h:362
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:336
iterator end()
Definition: MCAssembler.h:381
MCAssembler(MCContext &Context, std::unique_ptr< MCAsmBackend > Backend, std::unique_ptr< MCCodeEmitter > Emitter, std::unique_ptr< MCObjectWriter > Writer)
Construct a new assembler instance.
Definition: MCAssembler.cpp:84
iterator begin()
Definition: MCAssembler.h:378
bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:334
bool registerSection(MCSection &Section)
const MCSymbol * getAtom(const MCSymbol &S) const
Find the symbol which defines the atom containing the given symbol, or null if there is no such symbo...
MCAsmBackend * getBackendPtr() const
Definition: MCAssembler.h:328
bool isSymbolLinkerVisible(const MCSymbol &SD) const
Check whether a particular symbol is visible to the linker and is required in the symbol table,...
MCLOHContainer & getLOHContainer()
Definition: MCAssembler.h:465
symbol_iterator symbol_end()
Definition: MCAssembler.h:392
void reset()
Reuse an assembler instance.
Definition: MCAssembler.cpp:98
bool registerSymbol(const MCSymbol &Symbol)
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:340
void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F, uint64_t FSize) const
Write the necessary bundle padding to OS.
Represents required padding such that a particular other set of fragments does not cross a particular...
Definition: MCFragment.h:580
uint64_t getSize() const
Definition: MCFragment.h:598
void setSize(uint64_t Value)
Definition: MCFragment.h:599
const MCFragment * getLastFragment() const
Definition: MCFragment.h:604
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCFragment.h:610
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:550
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition: MCFragment.h:517
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
virtual void reset()
Lifetime management.
Definition: MCCodeEmitter.h:31
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:81
bool hadError()
Definition: MCContext.h:854
CodeViewContext & getCVContext()
Definition: MCContext.cpp:1018
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1073
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
static void encodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, SmallVectorImpl< char > &OS)
Definition: MCDwarf.cpp:1941
static void encode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, SmallVectorImpl< char > &OS)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Definition: MCDwarf.cpp:688
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:197
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:223
Interface implemented by fragments that contain encoded instructions and/or data.
Definition: MCFragment.h:125
const MCSubtargetInfo * getSubtargetInfo() const
Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
Definition: MCFragment.h:173
void setBundlePadding(uint8_t N)
Set the padding size for this fragment.
Definition: MCFragment.h:169
uint8_t getBundlePadding() const
Get the padding size that must be inserted before this fragment.
Definition: MCFragment.h:165
bool alignToBundleEnd() const
Should this fragment be placed at the end of an aligned bundle?
Definition: MCFragment.h:157
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const
Definition: MCExpr.cpp:572
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:814
bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const
Try to evaluate the expression to the form (a - b + constant) where neither a nor b are variables.
Definition: MCExpr.cpp:822
SMLoc getLoc() const
Definition: MCExpr.h:82
uint8_t getValueSize() const
Definition: MCFragment.h:364
uint64_t getValue() const
Definition: MCFragment.h:363
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
const MCSymbol * getAtom() const
Definition: MCFragment.h:99
MCSection * getParent() const
Definition: MCFragment.h:96
bool hasInstructions() const
Does this fragment have instructions emitted into it? By default this is false, but specific fragment...
Definition: MCFragment.h:107
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
bool isSigned() const
Definition: MCFragment.h:448
const MCExpr & getValue() const
Definition: MCFragment.h:445
void setValue(const MCExpr *Expr)
Definition: MCFragment.h:446
int64_t getControlledNopLength() const
Definition: MCFragment.h:393
int64_t getNumBytes() const
Definition: MCFragment.h:392
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCFragment.h:397
SMLoc getLoc() const
Definition: MCFragment.h:395
virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Write the object file and returns the number of bytes written.
virtual void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
virtual void reset()
lifetime management
virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
Record a relocation entry.
SMLoc getLoc() const
Definition: MCFragment.h:424
uint8_t getValue() const
Definition: MCFragment.h:422
const MCExpr & getOffset() const
Definition: MCFragment.h:420
const MCExpr & getAddrDelta() const
Definition: MCFragment.h:627
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:274
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
void setLayoutOrder(unsigned Value)
Definition: MCSection.h:153
MCSection::FragmentListType & getFragmentList()
Definition: MCSection.h:172
virtual bool isVirtualSection() const =0
Check whether this section is "virtual", that is has no actual object file contents.
void setOrdinal(unsigned Value)
Definition: MCSection.h:150
virtual bool useCodeAlign() const =0
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
iterator begin()
Definition: MCSection.h:185
Generic base class for all target subtargets.
Represents a symbol table index fragment.
Definition: MCFragment.h:500
const MCSymbol * getSymbol()
Definition: MCFragment.h:507
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:254
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:259
uint32_t getIndex() const
Get the (implementation defined) index.
Definition: MCSymbol.h:316
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:397
This represents an "assembler immediate".
Definition: MCValue.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
Represents a location in source code.
Definition: SMLoc.h:23
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:150
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:690
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCEncodedFragment *F, uint64_t FOffset, uint64_t FSize)
Compute the amount of padding required before the fragment F to obey bundling restrictions,...
Definition: MCFragment.cpp:213
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: Alignment.h:197
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
endianness
Definition: bit.h:70
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
@ FKF_IsTarget
Should this fixup be evaluated in a target dependent manner?
@ FKF_IsAlignedDownTo32Bits
Should this fixup kind force a 4-byte aligned effective PC value?
@ FKF_Constant
This fixup kind should be resolved if defined.
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
unsigned Flags
Flags describing additional information on this fixup kind.
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:324