LLVM 20.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"
19#include "llvm/MC/MCCodeView.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCDwarf.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCFixup.h"
25#include "llvm/MC/MCFragment.h"
26#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCSymbol.h"
30#include "llvm/MC/MCValue.h"
33#include "llvm/Support/Debug.h"
36#include "llvm/Support/LEB128.h"
38#include <cassert>
39#include <cstdint>
40#include <tuple>
41#include <utility>
42
43using namespace llvm;
44
45namespace llvm {
46class MCSubtargetInfo;
47}
48
49#define DEBUG_TYPE "assembler"
50
51namespace {
52namespace stats {
53
54STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
55STATISTIC(EmittedRelaxableFragments,
56 "Number of emitted assembler fragments - relaxable");
57STATISTIC(EmittedDataFragments,
58 "Number of emitted assembler fragments - data");
59STATISTIC(EmittedCompactEncodedInstFragments,
60 "Number of emitted assembler fragments - compact encoded inst");
61STATISTIC(EmittedAlignFragments,
62 "Number of emitted assembler fragments - align");
63STATISTIC(EmittedFillFragments,
64 "Number of emitted assembler fragments - fill");
65STATISTIC(EmittedNopsFragments, "Number of emitted assembler fragments - nops");
66STATISTIC(EmittedOrgFragments, "Number of emitted assembler fragments - org");
67STATISTIC(evaluateFixup, "Number of evaluated fixups");
68STATISTIC(ObjectBytes, "Number of emitted object file bytes");
69STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
70STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
71
72} // end namespace stats
73} // end anonymous namespace
74
75// FIXME FIXME FIXME: There are number of places in this file where we convert
76// what is a 64-bit assembler value used for computation into a value in the
77// object file, which may truncate it. We should detect that truncation where
78// invalid and report errors back.
79
80/* *** */
81
83 std::unique_ptr<MCAsmBackend> Backend,
84 std::unique_ptr<MCCodeEmitter> Emitter,
85 std::unique_ptr<MCObjectWriter> Writer)
86 : Context(Context), Backend(std::move(Backend)),
87 Emitter(std::move(Emitter)), Writer(std::move(Writer)) {}
88
90 RelaxAll = false;
91 Sections.clear();
92 Symbols.clear();
93 ThumbFuncs.clear();
94 BundleAlignSize = 0;
95
96 // reset objects owned by us
97 if (getBackendPtr())
99 if (getEmitterPtr())
100 getEmitterPtr()->reset();
101 if (Writer)
102 Writer->reset();
103}
104
106 if (Section.isRegistered())
107 return false;
108 assert(Section.curFragList()->Head && "allocInitialFragment not called");
109 Sections.push_back(&Section);
110 Section.setIsRegistered(true);
111 return true;
112}
113
114bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
115 if (ThumbFuncs.count(Symbol))
116 return true;
117
118 if (!Symbol->isVariable())
119 return false;
120
121 const MCExpr *Expr = Symbol->getVariableValue();
122
123 MCValue V;
124 if (!Expr->evaluateAsRelocatable(V, nullptr, nullptr))
125 return false;
126
127 if (V.getSymB() || V.getRefKind() != MCSymbolRefExpr::VK_None)
128 return false;
129
130 const MCSymbolRefExpr *Ref = V.getSymA();
131 if (!Ref)
132 return false;
133
134 if (Ref->getKind() != MCSymbolRefExpr::VK_None)
135 return false;
136
137 const MCSymbol &Sym = Ref->getSymbol();
138 if (!isThumbFunc(&Sym))
139 return false;
140
141 ThumbFuncs.insert(Symbol); // Cache it.
142 return true;
143}
144
145bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
146 MCValue &Target, const MCSubtargetInfo *STI,
147 uint64_t &Value, bool &WasForced) const {
148 ++stats::evaluateFixup;
149
150 // FIXME: This code has some duplication with recordRelocation. We should
151 // probably merge the two into a single callback that tries to evaluate a
152 // fixup and records a relocation if one is needed.
153
154 // On error claim to have completely evaluated the fixup, to prevent any
155 // further processing from being done.
156 const MCExpr *Expr = Fixup.getValue();
157 MCContext &Ctx = getContext();
158 Value = 0;
159 WasForced = false;
160 if (!Expr->evaluateAsRelocatable(Target, this, &Fixup)) {
161 Ctx.reportError(Fixup.getLoc(), "expected relocatable expression");
162 return true;
163 }
164 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
165 if (RefB->getKind() != MCSymbolRefExpr::VK_None) {
166 Ctx.reportError(Fixup.getLoc(),
167 "unsupported subtraction of qualified symbol");
168 return true;
169 }
170 }
171
172 assert(getBackendPtr() && "Expected assembler backend");
173 bool IsTarget = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags &
175
176 if (IsTarget)
177 return getBackend().evaluateTargetFixup(*this, Fixup, DF, Target, STI,
178 Value, WasForced);
179
180 unsigned FixupFlags = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags;
181 bool IsPCRel = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags &
183
184 bool IsResolved = false;
185 if (IsPCRel) {
186 if (Target.getSymB()) {
187 IsResolved = false;
188 } else if (!Target.getSymA()) {
189 IsResolved = false;
190 } else {
191 const MCSymbolRefExpr *A = Target.getSymA();
192 const MCSymbol &SA = A->getSymbol();
193 if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
194 IsResolved = false;
195 } else {
196 IsResolved = (FixupFlags & MCFixupKindInfo::FKF_Constant) ||
198 *this, SA, *DF, false, true);
199 }
200 }
201 } else {
202 IsResolved = Target.isAbsolute();
203 }
204
205 Value = Target.getConstant();
206
207 if (const MCSymbolRefExpr *A = Target.getSymA()) {
208 const MCSymbol &Sym = A->getSymbol();
209 if (Sym.isDefined())
211 }
212 if (const MCSymbolRefExpr *B = Target.getSymB()) {
213 const MCSymbol &Sym = B->getSymbol();
214 if (Sym.isDefined())
216 }
217
218 bool ShouldAlignPC = getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
220 assert((ShouldAlignPC ? IsPCRel : true) &&
221 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
222
223 if (IsPCRel) {
224 uint64_t Offset = getFragmentOffset(*DF) + Fixup.getOffset();
225
226 // A number of ARM fixups in Thumb mode require that the effective PC
227 // address be determined as the 32-bit aligned version of the actual offset.
228 if (ShouldAlignPC) Offset &= ~0x3;
229 Value -= Offset;
230 }
231
232 // Let the backend force a relocation if needed.
233 if (IsResolved &&
234 getBackend().shouldForceRelocation(*this, Fixup, Target, STI)) {
235 IsResolved = false;
236 WasForced = true;
237 }
238
239 // A linker relaxation target may emit ADD/SUB relocations for A-B+C. Let
240 // recordRelocation handle non-VK_None cases like A@plt-B+C.
241 if (!IsResolved && Target.getSymA() && Target.getSymB() &&
242 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_None &&
243 getBackend().handleAddSubRelocations(*this, *DF, Fixup, Target, Value))
244 return true;
245
246 return IsResolved;
247}
248
250 assert(getBackendPtr() && "Requires assembler backend");
251 switch (F.getKind()) {
253 return cast<MCDataFragment>(F).getContents().size();
255 return cast<MCRelaxableFragment>(F).getContents().size();
257 return cast<MCCompactEncodedInstFragment>(F).getContents().size();
258 case MCFragment::FT_Fill: {
259 auto &FF = cast<MCFillFragment>(F);
260 int64_t NumValues = 0;
261 if (!FF.getNumValues().evaluateKnownAbsolute(NumValues, *this)) {
262 getContext().reportError(FF.getLoc(),
263 "expected assembly-time absolute expression");
264 return 0;
265 }
266 int64_t Size = NumValues * FF.getValueSize();
267 if (Size < 0) {
268 getContext().reportError(FF.getLoc(), "invalid number of bytes");
269 return 0;
270 }
271 return Size;
272 }
273
275 return cast<MCNopsFragment>(F).getNumBytes();
276
278 return cast<MCLEBFragment>(F).getContents().size();
279
281 return cast<MCBoundaryAlignFragment>(F).getSize();
282
284 return 4;
285
287 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
288 unsigned Offset = getFragmentOffset(AF);
289 unsigned Size = offsetToAlignment(Offset, AF.getAlignment());
290
291 // Insert extra Nops for code alignment if the target define
292 // shouldInsertExtraNopBytesForCodeAlign target hook.
293 if (AF.getParent()->useCodeAlign() && AF.hasEmitNops() &&
294 getBackend().shouldInsertExtraNopBytesForCodeAlign(AF, Size))
295 return Size;
296
297 // If we are padding with nops, force the padding to be larger than the
298 // minimum nop size.
299 if (Size > 0 && AF.hasEmitNops()) {
300 while (Size % getBackend().getMinimumNopSize())
301 Size += AF.getAlignment().value();
302 }
303 if (Size > AF.getMaxBytesToEmit())
304 return 0;
305 return Size;
306 }
307
308 case MCFragment::FT_Org: {
309 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
311 if (!OF.getOffset().evaluateAsValue(Value, *this)) {
313 "expected assembly-time absolute expression");
314 return 0;
315 }
316
317 uint64_t FragmentOffset = getFragmentOffset(OF);
318 int64_t TargetLocation = Value.getConstant();
319 if (const MCSymbolRefExpr *A = Value.getSymA()) {
320 uint64_t Val;
321 if (!getSymbolOffset(A->getSymbol(), Val)) {
322 getContext().reportError(OF.getLoc(), "expected absolute expression");
323 return 0;
324 }
325 TargetLocation += Val;
326 }
327 int64_t Size = TargetLocation - FragmentOffset;
328 if (Size < 0 || Size >= 0x40000000) {
330 OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
331 "' (at offset '" + Twine(FragmentOffset) + "')");
332 return 0;
333 }
334 return Size;
335 }
336
338 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
340 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
342 return cast<MCCVInlineLineTableFragment>(F).getContents().size();
344 return cast<MCCVDefRangeFragment>(F).getContents().size();
346 return cast<MCPseudoProbeAddrFragment>(F).getContents().size();
348 llvm_unreachable("Should not have been added");
349 }
350
351 llvm_unreachable("invalid fragment kind");
352}
353
354// Compute the amount of padding required before the fragment \p F to
355// obey bundling restrictions, where \p FOffset is the fragment's offset in
356// its section and \p FSize is the fragment's size.
357static uint64_t computeBundlePadding(unsigned BundleSize,
358 const MCEncodedFragment *F,
359 uint64_t FOffset, uint64_t FSize) {
360 uint64_t OffsetInBundle = FOffset & (BundleSize - 1);
361 uint64_t EndOfFragment = OffsetInBundle + FSize;
362
363 // There are two kinds of bundling restrictions:
364 //
365 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
366 // *end* on a bundle boundary.
367 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
368 // would, add padding until the end of the bundle so that the fragment
369 // will start in a new one.
370 if (F->alignToBundleEnd()) {
371 // Three possibilities here:
372 //
373 // A) The fragment just happens to end at a bundle boundary, so we're good.
374 // B) The fragment ends before the current bundle boundary: pad it just
375 // enough to reach the boundary.
376 // C) The fragment ends after the current bundle boundary: pad it until it
377 // reaches the end of the next bundle boundary.
378 //
379 // Note: this code could be made shorter with some modulo trickery, but it's
380 // intentionally kept in its more explicit form for simplicity.
381 if (EndOfFragment == BundleSize)
382 return 0;
383 else if (EndOfFragment < BundleSize)
384 return BundleSize - EndOfFragment;
385 else { // EndOfFragment > BundleSize
386 return 2 * BundleSize - EndOfFragment;
387 }
388 } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
389 return BundleSize - OffsetInBundle;
390 else
391 return 0;
392}
393
395 // If bundling is enabled and this fragment has instructions in it, it has to
396 // obey the bundling restrictions. With padding, we'll have:
397 //
398 //
399 // BundlePadding
400 // |||
401 // -------------------------------------
402 // Prev |##########| F |
403 // -------------------------------------
404 // ^
405 // |
406 // F->Offset
407 //
408 // The fragment's offset will point to after the padding, and its computed
409 // size won't include the padding.
410 //
411 // ".align N" is an example of a directive that introduces multiple
412 // fragments. We could add a special case to handle ".align N" by emitting
413 // within-fragment padding (which would produce less padding when N is less
414 // than the bundle size), but for now we don't.
415 //
416 assert(isa<MCEncodedFragment>(F) &&
417 "Only MCEncodedFragment implementations have instructions");
418 MCEncodedFragment *EF = cast<MCEncodedFragment>(F);
419 uint64_t FSize = computeFragmentSize(*EF);
420
421 if (FSize > getBundleAlignSize())
422 report_fatal_error("Fragment can't be larger than a bundle size");
423
424 uint64_t RequiredBundlePadding =
425 computeBundlePadding(getBundleAlignSize(), EF, EF->Offset, FSize);
426 if (RequiredBundlePadding > UINT8_MAX)
427 report_fatal_error("Padding cannot exceed 255 bytes");
428 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
429 EF->Offset += RequiredBundlePadding;
430 if (auto *DF = dyn_cast_or_null<MCDataFragment>(Prev))
431 if (DF->getContents().empty())
432 DF->Offset = EF->Offset;
433}
434
435// Simple getSymbolOffset helper for the non-variable case.
436static bool getLabelOffset(const MCAssembler &Asm, const MCSymbol &S,
437 bool ReportError, uint64_t &Val) {
438 if (!S.getFragment()) {
439 if (ReportError)
440 report_fatal_error("unable to evaluate offset to undefined symbol '" +
441 S.getName() + "'");
442 return false;
443 }
444 Val = Asm.getFragmentOffset(*S.getFragment()) + S.getOffset();
445 return true;
446}
447
448static bool getSymbolOffsetImpl(const MCAssembler &Asm, const MCSymbol &S,
449 bool ReportError, uint64_t &Val) {
450 if (!S.isVariable())
451 return getLabelOffset(Asm, S, ReportError, Val);
452
453 // If SD is a variable, evaluate it.
456 report_fatal_error("unable to evaluate offset for variable '" +
457 S.getName() + "'");
458
459 uint64_t Offset = Target.getConstant();
460
461 const MCSymbolRefExpr *A = Target.getSymA();
462 if (A) {
463 uint64_t ValA;
464 // FIXME: On most platforms, `Target`'s component symbols are labels from
465 // having been simplified during evaluation, but on Mach-O they can be
466 // variables due to PR19203. This, and the line below for `B` can be
467 // restored to call `getLabelOffset` when PR19203 is fixed.
468 if (!getSymbolOffsetImpl(Asm, A->getSymbol(), ReportError, ValA))
469 return false;
470 Offset += ValA;
471 }
472
473 const MCSymbolRefExpr *B = Target.getSymB();
474 if (B) {
475 uint64_t ValB;
476 if (!getSymbolOffsetImpl(Asm, B->getSymbol(), ReportError, ValB))
477 return false;
478 Offset -= ValB;
479 }
480
481 Val = Offset;
482 return true;
483}
484
486 return getSymbolOffsetImpl(*this, S, false, Val);
487}
488
490 uint64_t Val;
491 getSymbolOffsetImpl(*this, S, true, Val);
492 return Val;
493}
494
495const MCSymbol *MCAssembler::getBaseSymbol(const MCSymbol &Symbol) const {
496 assert(HasLayout);
497 if (!Symbol.isVariable())
498 return &Symbol;
499
500 const MCExpr *Expr = Symbol.getVariableValue();
502 if (!Expr->evaluateAsValue(Value, *this)) {
503 getContext().reportError(Expr->getLoc(),
504 "expression could not be evaluated");
505 return nullptr;
506 }
507
508 const MCSymbolRefExpr *RefB = Value.getSymB();
509 if (RefB) {
511 Expr->getLoc(),
512 Twine("symbol '") + RefB->getSymbol().getName() +
513 "' could not be evaluated in a subtraction expression");
514 return nullptr;
515 }
516
517 const MCSymbolRefExpr *A = Value.getSymA();
518 if (!A)
519 return nullptr;
520
521 const MCSymbol &ASym = A->getSymbol();
522 if (ASym.isCommon()) {
523 getContext().reportError(Expr->getLoc(),
524 "Common symbol '" + ASym.getName() +
525 "' cannot be used in assignment expr");
526 return nullptr;
527 }
528
529 return &ASym;
530}
531
533 assert(HasLayout);
534 // The size is the last fragment's end offset.
535 const MCFragment &F = *Sec.curFragList()->Tail;
537}
538
540 // Virtual sections have no file size.
541 if (Sec.isVirtualSection())
542 return 0;
543 return getSectionAddressSize(Sec);
544}
545
547 bool Changed = !Symbol.isRegistered();
548 if (Changed) {
549 Symbol.setIsRegistered(true);
550 Symbols.push_back(&Symbol);
551 }
552 return Changed;
553}
554
556 const MCEncodedFragment &EF,
557 uint64_t FSize) const {
558 assert(getBackendPtr() && "Expected assembler backend");
559 // Should NOP padding be written out before this fragment?
560 unsigned BundlePadding = EF.getBundlePadding();
561 if (BundlePadding > 0) {
563 "Writing bundle padding with disabled bundling");
564 assert(EF.hasInstructions() &&
565 "Writing bundle padding for a fragment without instructions");
566
567 unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
568 const MCSubtargetInfo *STI = EF.getSubtargetInfo();
569 if (EF.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
570 // If the padding itself crosses a bundle boundary, it must be emitted
571 // in 2 pieces, since even nop instructions must not cross boundaries.
572 // v--------------v <- BundleAlignSize
573 // v---------v <- BundlePadding
574 // ----------------------------
575 // | Prev |####|####| F |
576 // ----------------------------
577 // ^-------------------^ <- TotalLength
578 unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
579 if (!getBackend().writeNopData(OS, DistanceToBoundary, STI))
580 report_fatal_error("unable to write NOP sequence of " +
581 Twine(DistanceToBoundary) + " bytes");
582 BundlePadding -= DistanceToBoundary;
583 }
584 if (!getBackend().writeNopData(OS, BundlePadding, STI))
585 report_fatal_error("unable to write NOP sequence of " +
586 Twine(BundlePadding) + " bytes");
587 }
588}
589
590/// Write the fragment \p F to the output file.
591static void writeFragment(raw_ostream &OS, const MCAssembler &Asm,
592 const MCFragment &F) {
593 // FIXME: Embed in fragments instead?
594 uint64_t FragmentSize = Asm.computeFragmentSize(F);
595
596 llvm::endianness Endian = Asm.getBackend().Endian;
597
598 if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(&F))
599 Asm.writeFragmentPadding(OS, *EF, FragmentSize);
600
601 // This variable (and its dummy usage) is to participate in the assert at
602 // the end of the function.
603 uint64_t Start = OS.tell();
604 (void) Start;
605
606 ++stats::EmittedFragments;
607
608 switch (F.getKind()) {
610 ++stats::EmittedAlignFragments;
611 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
612 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
613
614 uint64_t Count = FragmentSize / AF.getValueSize();
615
616 // FIXME: This error shouldn't actually occur (the front end should emit
617 // multiple .align directives to enforce the semantics it wants), but is
618 // severe enough that we want to report it. How to handle this?
619 if (Count * AF.getValueSize() != FragmentSize)
620 report_fatal_error("undefined .align directive, value size '" +
621 Twine(AF.getValueSize()) +
622 "' is not a divisor of padding size '" +
623 Twine(FragmentSize) + "'");
624
625 // See if we are aligning with nops, and if so do that first to try to fill
626 // the Count bytes. Then if that did not fill any bytes or there are any
627 // bytes left to fill use the Value and ValueSize to fill the rest.
628 // If we are aligning with nops, ask that target to emit the right data.
629 if (AF.hasEmitNops()) {
630 if (!Asm.getBackend().writeNopData(OS, Count, AF.getSubtargetInfo()))
631 report_fatal_error("unable to write nop sequence of " +
632 Twine(Count) + " bytes");
633 break;
634 }
635
636 // Otherwise, write out in multiples of the value size.
637 for (uint64_t i = 0; i != Count; ++i) {
638 switch (AF.getValueSize()) {
639 default: llvm_unreachable("Invalid size!");
640 case 1: OS << char(AF.getValue()); break;
641 case 2:
642 support::endian::write<uint16_t>(OS, AF.getValue(), Endian);
643 break;
644 case 4:
645 support::endian::write<uint32_t>(OS, AF.getValue(), Endian);
646 break;
647 case 8:
648 support::endian::write<uint64_t>(OS, AF.getValue(), Endian);
649 break;
650 }
651 }
652 break;
653 }
654
656 ++stats::EmittedDataFragments;
657 OS << cast<MCDataFragment>(F).getContents();
658 break;
659
661 ++stats::EmittedRelaxableFragments;
662 OS << cast<MCRelaxableFragment>(F).getContents();
663 break;
664
666 ++stats::EmittedCompactEncodedInstFragments;
667 OS << cast<MCCompactEncodedInstFragment>(F).getContents();
668 break;
669
670 case MCFragment::FT_Fill: {
671 ++stats::EmittedFillFragments;
672 const MCFillFragment &FF = cast<MCFillFragment>(F);
673 uint64_t V = FF.getValue();
674 unsigned VSize = FF.getValueSize();
675 const unsigned MaxChunkSize = 16;
676 char Data[MaxChunkSize];
677 assert(0 < VSize && VSize <= MaxChunkSize && "Illegal fragment fill size");
678 // Duplicate V into Data as byte vector to reduce number of
679 // writes done. As such, do endian conversion here.
680 for (unsigned I = 0; I != VSize; ++I) {
681 unsigned index = Endian == llvm::endianness::little ? I : (VSize - I - 1);
682 Data[I] = uint8_t(V >> (index * 8));
683 }
684 for (unsigned I = VSize; I < MaxChunkSize; ++I)
685 Data[I] = Data[I - VSize];
686
687 // Set to largest multiple of VSize in Data.
688 const unsigned NumPerChunk = MaxChunkSize / VSize;
689 // Set ChunkSize to largest multiple of VSize in Data
690 const unsigned ChunkSize = VSize * NumPerChunk;
691
692 // Do copies by chunk.
693 StringRef Ref(Data, ChunkSize);
694 for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I)
695 OS << Ref;
696
697 // do remainder if needed.
698 unsigned TrailingCount = FragmentSize % ChunkSize;
699 if (TrailingCount)
700 OS.write(Data, TrailingCount);
701 break;
702 }
703
704 case MCFragment::FT_Nops: {
705 ++stats::EmittedNopsFragments;
706 const MCNopsFragment &NF = cast<MCNopsFragment>(F);
707
708 int64_t NumBytes = NF.getNumBytes();
709 int64_t ControlledNopLength = NF.getControlledNopLength();
710 int64_t MaximumNopLength =
711 Asm.getBackend().getMaximumNopSize(*NF.getSubtargetInfo());
712
713 assert(NumBytes > 0 && "Expected positive NOPs fragment size");
714 assert(ControlledNopLength >= 0 && "Expected non-negative NOP size");
715
716 if (ControlledNopLength > MaximumNopLength) {
717 Asm.getContext().reportError(NF.getLoc(),
718 "illegal NOP size " +
719 std::to_string(ControlledNopLength) +
720 ". (expected within [0, " +
721 std::to_string(MaximumNopLength) + "])");
722 // Clamp the NOP length as reportError does not stop the execution
723 // immediately.
724 ControlledNopLength = MaximumNopLength;
725 }
726
727 // Use maximum value if the size of each NOP is not specified
728 if (!ControlledNopLength)
729 ControlledNopLength = MaximumNopLength;
730
731 while (NumBytes) {
732 uint64_t NumBytesToEmit =
733 (uint64_t)std::min(NumBytes, ControlledNopLength);
734 assert(NumBytesToEmit && "try to emit empty NOP instruction");
735 if (!Asm.getBackend().writeNopData(OS, NumBytesToEmit,
736 NF.getSubtargetInfo())) {
737 report_fatal_error("unable to write nop sequence of the remaining " +
738 Twine(NumBytesToEmit) + " bytes");
739 break;
740 }
741 NumBytes -= NumBytesToEmit;
742 }
743 break;
744 }
745
746 case MCFragment::FT_LEB: {
747 const MCLEBFragment &LF = cast<MCLEBFragment>(F);
748 OS << LF.getContents();
749 break;
750 }
751
753 const MCBoundaryAlignFragment &BF = cast<MCBoundaryAlignFragment>(F);
754 if (!Asm.getBackend().writeNopData(OS, FragmentSize, BF.getSubtargetInfo()))
755 report_fatal_error("unable to write nop sequence of " +
756 Twine(FragmentSize) + " bytes");
757 break;
758 }
759
761 const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F);
762 support::endian::write<uint32_t>(OS, SF.getSymbol()->getIndex(), Endian);
763 break;
764 }
765
766 case MCFragment::FT_Org: {
767 ++stats::EmittedOrgFragments;
768 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
769
770 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
771 OS << char(OF.getValue());
772
773 break;
774 }
775
777 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
778 OS << OF.getContents();
779 break;
780 }
782 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
783 OS << CF.getContents();
784 break;
785 }
787 const auto &OF = cast<MCCVInlineLineTableFragment>(F);
788 OS << OF.getContents();
789 break;
790 }
792 const auto &DRF = cast<MCCVDefRangeFragment>(F);
793 OS << DRF.getContents();
794 break;
795 }
797 const MCPseudoProbeAddrFragment &PF = cast<MCPseudoProbeAddrFragment>(F);
798 OS << PF.getContents();
799 break;
800 }
802 llvm_unreachable("Should not have been added");
803 }
804
805 assert(OS.tell() - Start == FragmentSize &&
806 "The stream should advance by fragment size");
807}
808
810 const MCSection *Sec) const {
811 assert(getBackendPtr() && "Expected assembler backend");
812
813 // Ignore virtual sections.
814 if (Sec->isVirtualSection()) {
815 assert(getSectionFileSize(*Sec) == 0 && "Invalid size for section!");
816
817 // Check that contents are only things legal inside a virtual section.
818 for (const MCFragment &F : *Sec) {
819 switch (F.getKind()) {
820 default: llvm_unreachable("Invalid fragment in virtual section!");
821 case MCFragment::FT_Data: {
822 // Check that we aren't trying to write a non-zero contents (or fixups)
823 // into a virtual section. This is to support clients which use standard
824 // directives to fill the contents of virtual sections.
825 const MCDataFragment &DF = cast<MCDataFragment>(F);
826 if (DF.fixup_begin() != DF.fixup_end())
827 getContext().reportError(SMLoc(), Sec->getVirtualSectionKind() +
828 " section '" + Sec->getName() +
829 "' cannot have fixups");
830 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
831 if (DF.getContents()[i]) {
833 Sec->getVirtualSectionKind() +
834 " section '" + Sec->getName() +
835 "' cannot have non-zero initializers");
836 break;
837 }
838 break;
839 }
841 // Check that we aren't trying to write a non-zero value into a virtual
842 // section.
843 assert((cast<MCAlignFragment>(F).getValueSize() == 0 ||
844 cast<MCAlignFragment>(F).getValue() == 0) &&
845 "Invalid align in virtual section!");
846 break;
848 assert((cast<MCFillFragment>(F).getValue() == 0) &&
849 "Invalid fill in virtual section!");
850 break;
852 break;
853 }
854 }
855
856 return;
857 }
858
859 uint64_t Start = OS.tell();
860 (void)Start;
861
862 for (const MCFragment &F : *Sec)
863 writeFragment(OS, *this, F);
864
865 assert(getContext().hadError() ||
866 OS.tell() - Start == getSectionAddressSize(*Sec));
867}
868
869std::tuple<MCValue, uint64_t, bool>
870MCAssembler::handleFixup(MCFragment &F, const MCFixup &Fixup,
871 const MCSubtargetInfo *STI) {
872 // Evaluate the fixup.
874 uint64_t FixedValue;
875 bool WasForced;
876 bool IsResolved =
877 evaluateFixup(Fixup, &F, Target, STI, FixedValue, WasForced);
878 if (!IsResolved) {
879 // The fixup was unresolved, we need a relocation. Inform the object
880 // writer of the relocation, and give it an opportunity to adjust the
881 // fixup value if need be.
882 getWriter().recordRelocation(*this, &F, Fixup, Target, FixedValue);
883 }
884 return std::make_tuple(Target, FixedValue, IsResolved);
885}
886
888 assert(getBackendPtr() && "Expected assembler backend");
889 DEBUG_WITH_TYPE("mc-dump", {
890 errs() << "assembler backend - pre-layout\n--\n";
891 dump(); });
892
893 // Assign section ordinals.
894 unsigned SectionIndex = 0;
895 for (MCSection &Sec : *this) {
896 Sec.setOrdinal(SectionIndex++);
897
898 // Chain together fragments from all subsections.
899 if (Sec.Subsections.size() > 1) {
900 MCDummyFragment Dummy;
901 MCFragment *Tail = &Dummy;
902 for (auto &[_, List] : Sec.Subsections) {
903 assert(List.Head);
904 Tail->Next = List.Head;
905 Tail = List.Tail;
906 }
907 Sec.Subsections.clear();
908 Sec.Subsections.push_back({0u, {Dummy.getNext(), Tail}});
909 Sec.CurFragList = &Sec.Subsections[0].second;
910
911 unsigned FragmentIndex = 0;
912 for (MCFragment &Frag : Sec)
913 Frag.setLayoutOrder(FragmentIndex++);
914 }
915 }
916
917 // Layout until everything fits.
918 this->HasLayout = true;
919 for (MCSection &Sec : *this)
920 layoutSection(Sec);
921 while (layoutOnce()) {
922 }
923
924 DEBUG_WITH_TYPE("mc-dump", {
925 errs() << "assembler backend - post-relaxation\n--\n";
926 dump(); });
927
928 // Some targets might want to adjust fragment offsets. If so, perform another
929 // layout loop.
930 if (getBackend().finishLayout(*this))
931 for (MCSection &Sec : *this)
932 layoutSection(Sec);
933
934 DEBUG_WITH_TYPE("mc-dump", {
935 errs() << "assembler backend - final-layout\n--\n";
936 dump(); });
937
938 // Allow the object writer a chance to perform post-layout binding (for
939 // example, to set the index fields in the symbol data).
941
942 // Evaluate and apply the fixups, generating relocation entries as necessary.
943 for (MCSection &Sec : *this) {
944 for (MCFragment &Frag : Sec) {
945 ArrayRef<MCFixup> Fixups;
946 MutableArrayRef<char> Contents;
947 const MCSubtargetInfo *STI = nullptr;
948
949 // Process MCAlignFragment and MCEncodedFragmentWithFixups here.
950 switch (Frag.getKind()) {
951 default:
952 continue;
954 MCAlignFragment &AF = cast<MCAlignFragment>(Frag);
955 // Insert fixup type for code alignment if the target define
956 // shouldInsertFixupForCodeAlign target hook.
957 if (Sec.useCodeAlign() && AF.hasEmitNops())
959 continue;
960 }
961 case MCFragment::FT_Data: {
962 MCDataFragment &DF = cast<MCDataFragment>(Frag);
963 Fixups = DF.getFixups();
964 Contents = DF.getContents();
965 STI = DF.getSubtargetInfo();
966 assert(!DF.hasInstructions() || STI != nullptr);
967 break;
968 }
970 MCRelaxableFragment &RF = cast<MCRelaxableFragment>(Frag);
971 Fixups = RF.getFixups();
972 Contents = RF.getContents();
973 STI = RF.getSubtargetInfo();
974 assert(!RF.hasInstructions() || STI != nullptr);
975 break;
976 }
978 MCCVDefRangeFragment &CF = cast<MCCVDefRangeFragment>(Frag);
979 Fixups = CF.getFixups();
980 Contents = CF.getContents();
981 break;
982 }
984 MCDwarfLineAddrFragment &DF = cast<MCDwarfLineAddrFragment>(Frag);
985 Fixups = DF.getFixups();
986 Contents = DF.getContents();
987 break;
988 }
990 MCDwarfCallFrameFragment &DF = cast<MCDwarfCallFrameFragment>(Frag);
991 Fixups = DF.getFixups();
992 Contents = DF.getContents();
993 break;
994 }
995 case MCFragment::FT_LEB: {
996 auto &LF = cast<MCLEBFragment>(Frag);
997 Fixups = LF.getFixups();
998 Contents = LF.getContents();
999 break;
1000 }
1002 MCPseudoProbeAddrFragment &PF = cast<MCPseudoProbeAddrFragment>(Frag);
1003 Fixups = PF.getFixups();
1004 Contents = PF.getContents();
1005 break;
1006 }
1007 }
1008 for (const MCFixup &Fixup : Fixups) {
1009 uint64_t FixedValue;
1010 bool IsResolved;
1012 std::tie(Target, FixedValue, IsResolved) =
1013 handleFixup(Frag, Fixup, STI);
1014 getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue,
1015 IsResolved, STI);
1016 }
1017 }
1018 }
1019}
1020
1022 layout();
1023
1024 // Write the object file.
1025 stats::ObjectBytes += getWriter().writeObject(*this);
1026
1027 HasLayout = false;
1028}
1029
1030bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
1031 const MCRelaxableFragment *DF) const {
1032 assert(getBackendPtr() && "Expected assembler backend");
1035 bool WasForced;
1036 bool Resolved = evaluateFixup(Fixup, DF, Target, DF->getSubtargetInfo(),
1037 Value, WasForced);
1038 if (Target.getSymA() &&
1039 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8 &&
1040 Fixup.getKind() == FK_Data_1)
1041 return false;
1042 return getBackend().fixupNeedsRelaxationAdvanced(*this, Fixup, Resolved,
1043 Value, DF, WasForced);
1044}
1045
1046bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F) const {
1047 assert(getBackendPtr() && "Expected assembler backend");
1048 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
1049 // are intentionally pushing out inst fragments, or because we relaxed a
1050 // previous instruction to one that doesn't need relaxation.
1051 if (!getBackend().mayNeedRelaxation(F->getInst(), *F->getSubtargetInfo()))
1052 return false;
1053
1054 for (const MCFixup &Fixup : F->getFixups())
1055 if (fixupNeedsRelaxation(Fixup, F))
1056 return true;
1057
1058 return false;
1059}
1060
1061bool MCAssembler::relaxInstruction(MCRelaxableFragment &F) {
1063 "Expected CodeEmitter defined for relaxInstruction");
1064 if (!fragmentNeedsRelaxation(&F))
1065 return false;
1066
1067 ++stats::RelaxedInstructions;
1068
1069 // FIXME-PERF: We could immediately lower out instructions if we can tell
1070 // they are fully resolved, to avoid retesting on later passes.
1071
1072 // Relax the fragment.
1073
1074 MCInst Relaxed = F.getInst();
1075 getBackend().relaxInstruction(Relaxed, *F.getSubtargetInfo());
1076
1077 // Encode the new instruction.
1078 F.setInst(Relaxed);
1079 F.getFixups().clear();
1080 F.getContents().clear();
1081 getEmitter().encodeInstruction(Relaxed, F.getContents(), F.getFixups(),
1082 *F.getSubtargetInfo());
1083 return true;
1084}
1085
1086bool MCAssembler::relaxLEB(MCLEBFragment &LF) {
1087 const unsigned OldSize = static_cast<unsigned>(LF.getContents().size());
1088 unsigned PadTo = OldSize;
1089 int64_t Value;
1091 LF.getFixups().clear();
1092 // Use evaluateKnownAbsolute for Mach-O as a hack: .subsections_via_symbols
1093 // requires that .uleb128 A-B is foldable where A and B reside in different
1094 // fragments. This is used by __gcc_except_table.
1095 bool Abs = getWriter().getSubsectionsViaSymbols()
1096 ? LF.getValue().evaluateKnownAbsolute(Value, *this)
1097 : LF.getValue().evaluateAsAbsolute(Value, *this);
1098 if (!Abs) {
1099 bool Relaxed, UseZeroPad;
1100 std::tie(Relaxed, UseZeroPad) = getBackend().relaxLEB128(*this, LF, Value);
1101 if (!Relaxed) {
1103 Twine(LF.isSigned() ? ".s" : ".u") +
1104 "leb128 expression is not absolute");
1105 LF.setValue(MCConstantExpr::create(0, Context));
1106 }
1107 uint8_t Tmp[10]; // maximum size: ceil(64/7)
1108 PadTo = std::max(PadTo, encodeULEB128(uint64_t(Value), Tmp));
1109 if (UseZeroPad)
1110 Value = 0;
1111 }
1112 Data.clear();
1114 // The compiler can generate EH table assembly that is impossible to assemble
1115 // without either adding padding to an LEB fragment or adding extra padding
1116 // to a later alignment fragment. To accommodate such tables, relaxation can
1117 // only increase an LEB fragment size here, not decrease it. See PR35809.
1118 if (LF.isSigned())
1119 encodeSLEB128(Value, OSE, PadTo);
1120 else
1121 encodeULEB128(Value, OSE, PadTo);
1122 return OldSize != LF.getContents().size();
1123}
1124
1125/// Check if the branch crosses the boundary.
1126///
1127/// \param StartAddr start address of the fused/unfused branch.
1128/// \param Size size of the fused/unfused branch.
1129/// \param BoundaryAlignment alignment requirement of the branch.
1130/// \returns true if the branch cross the boundary.
1132 Align BoundaryAlignment) {
1133 uint64_t EndAddr = StartAddr + Size;
1134 return (StartAddr >> Log2(BoundaryAlignment)) !=
1135 ((EndAddr - 1) >> Log2(BoundaryAlignment));
1136}
1137
1138/// Check if the branch is against the boundary.
1139///
1140/// \param StartAddr start address of the fused/unfused branch.
1141/// \param Size size of the fused/unfused branch.
1142/// \param BoundaryAlignment alignment requirement of the branch.
1143/// \returns true if the branch is against the boundary.
1145 Align BoundaryAlignment) {
1146 uint64_t EndAddr = StartAddr + Size;
1147 return (EndAddr & (BoundaryAlignment.value() - 1)) == 0;
1148}
1149
1150/// Check if the branch needs padding.
1151///
1152/// \param StartAddr start address of the fused/unfused branch.
1153/// \param Size size of the fused/unfused branch.
1154/// \param BoundaryAlignment alignment requirement of the branch.
1155/// \returns true if the branch needs padding.
1156static bool needPadding(uint64_t StartAddr, uint64_t Size,
1157 Align BoundaryAlignment) {
1158 return mayCrossBoundary(StartAddr, Size, BoundaryAlignment) ||
1159 isAgainstBoundary(StartAddr, Size, BoundaryAlignment);
1160}
1161
1162bool MCAssembler::relaxBoundaryAlign(MCBoundaryAlignFragment &BF) {
1163 // BoundaryAlignFragment that doesn't need to align any fragment should not be
1164 // relaxed.
1165 if (!BF.getLastFragment())
1166 return false;
1167
1168 uint64_t AlignedOffset = getFragmentOffset(BF);
1169 uint64_t AlignedSize = 0;
1170 for (const MCFragment *F = BF.getNext();; F = F->getNext()) {
1171 AlignedSize += computeFragmentSize(*F);
1172 if (F == BF.getLastFragment())
1173 break;
1174 }
1175
1176 Align BoundaryAlignment = BF.getAlignment();
1177 uint64_t NewSize = needPadding(AlignedOffset, AlignedSize, BoundaryAlignment)
1178 ? offsetToAlignment(AlignedOffset, BoundaryAlignment)
1179 : 0U;
1180 if (NewSize == BF.getSize())
1181 return false;
1182 BF.setSize(NewSize);
1183 return true;
1184}
1185
1186bool MCAssembler::relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF) {
1187 bool WasRelaxed;
1188 if (getBackend().relaxDwarfLineAddr(*this, DF, WasRelaxed))
1189 return WasRelaxed;
1190
1191 MCContext &Context = getContext();
1192 uint64_t OldSize = DF.getContents().size();
1193 int64_t AddrDelta;
1194 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, *this);
1195 assert(Abs && "We created a line delta with an invalid expression");
1196 (void)Abs;
1197 int64_t LineDelta;
1198 LineDelta = DF.getLineDelta();
1199 SmallVectorImpl<char> &Data = DF.getContents();
1200 Data.clear();
1201 DF.getFixups().clear();
1202
1204 AddrDelta, Data);
1205 return OldSize != Data.size();
1206}
1207
1208bool MCAssembler::relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF) {
1209 bool WasRelaxed;
1210 if (getBackend().relaxDwarfCFA(*this, DF, WasRelaxed))
1211 return WasRelaxed;
1212
1213 MCContext &Context = getContext();
1214 int64_t Value;
1215 bool Abs = DF.getAddrDelta().evaluateAsAbsolute(Value, *this);
1216 if (!Abs) {
1217 getContext().reportError(DF.getAddrDelta().getLoc(),
1218 "invalid CFI advance_loc expression");
1219 DF.setAddrDelta(MCConstantExpr::create(0, Context));
1220 return false;
1221 }
1222
1223 SmallVectorImpl<char> &Data = DF.getContents();
1224 uint64_t OldSize = Data.size();
1225 Data.clear();
1226 DF.getFixups().clear();
1227
1229 return OldSize != Data.size();
1230}
1231
1232bool MCAssembler::relaxCVInlineLineTable(MCCVInlineLineTableFragment &F) {
1233 unsigned OldSize = F.getContents().size();
1235 return OldSize != F.getContents().size();
1236}
1237
1238bool MCAssembler::relaxCVDefRange(MCCVDefRangeFragment &F) {
1239 unsigned OldSize = F.getContents().size();
1241 return OldSize != F.getContents().size();
1242}
1243
1244bool MCAssembler::relaxPseudoProbeAddr(MCPseudoProbeAddrFragment &PF) {
1245 uint64_t OldSize = PF.getContents().size();
1246 int64_t AddrDelta;
1247 bool Abs = PF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, *this);
1248 assert(Abs && "We created a pseudo probe with an invalid expression");
1249 (void)Abs;
1251 Data.clear();
1253 PF.getFixups().clear();
1254
1255 // AddrDelta is a signed integer
1256 encodeSLEB128(AddrDelta, OSE, OldSize);
1257 return OldSize != Data.size();
1258}
1259
1260bool MCAssembler::relaxFragment(MCFragment &F) {
1261 switch(F.getKind()) {
1262 default:
1263 return false;
1265 assert(!getRelaxAll() &&
1266 "Did not expect a MCRelaxableFragment in RelaxAll mode");
1267 return relaxInstruction(cast<MCRelaxableFragment>(F));
1269 return relaxDwarfLineAddr(cast<MCDwarfLineAddrFragment>(F));
1271 return relaxDwarfCallFrameFragment(cast<MCDwarfCallFrameFragment>(F));
1272 case MCFragment::FT_LEB:
1273 return relaxLEB(cast<MCLEBFragment>(F));
1275 return relaxBoundaryAlign(cast<MCBoundaryAlignFragment>(F));
1277 return relaxCVInlineLineTable(cast<MCCVInlineLineTableFragment>(F));
1279 return relaxCVDefRange(cast<MCCVDefRangeFragment>(F));
1281 return relaxPseudoProbeAddr(cast<MCPseudoProbeAddrFragment>(F));
1282 }
1283}
1284
1285void MCAssembler::layoutSection(MCSection &Sec) {
1286 MCFragment *Prev = nullptr;
1287 uint64_t Offset = 0;
1288 for (MCFragment &F : Sec) {
1289 F.Offset = Offset;
1291 if (F.hasInstructions()) {
1292 layoutBundle(Prev, &F);
1293 Offset = F.Offset;
1294 }
1295 Prev = &F;
1296 }
1298 }
1299}
1300
1301bool MCAssembler::layoutOnce() {
1302 ++stats::RelaxationSteps;
1303
1304 // Size of fragments in one section can depend on the size of fragments in
1305 // another. If any fragment has changed size, we have to re-layout (and
1306 // as a result possibly further relax) all.
1307 bool ChangedAny = false;
1308 for (MCSection &Sec : *this) {
1309 for (;;) {
1310 bool Changed = false;
1311 for (MCFragment &F : Sec)
1312 if (relaxFragment(F))
1313 Changed = true;
1314 ChangedAny |= Changed;
1315 if (!Changed)
1316 break;
1317 layoutSection(Sec);
1318 }
1319 }
1320 return ChangedAny;
1321}
1322
1323#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1325 raw_ostream &OS = errs();
1326
1327 OS << "<MCAssembler\n";
1328 OS << " Sections:[\n ";
1329 bool First = true;
1330 for (const MCSection &Sec : *this) {
1331 if (First)
1332 First = false;
1333 else
1334 OS << ",\n ";
1335 Sec.dump();
1336 }
1337 OS << "],\n";
1338 OS << " Symbols:[";
1339
1340 First = true;
1341 for (const MCSymbol &Sym : symbols()) {
1342 if (First)
1343 First = false;
1344 else
1345 OS << ",\n ";
1346 OS << "(";
1347 Sym.dump();
1348 OS << ", Index:" << Sym.getIndex() << ", ";
1349 OS << ")";
1350 }
1351 OS << "]>\n";
1352}
1353#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:241
#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
#define _
static uint64_t computeBundlePadding(unsigned BundleSize, const MCEncodedFragment *F, uint64_t FOffset, uint64_t FSize)
static bool getSymbolOffsetImpl(const MCAssembler &Asm, const MCSymbol &S, bool ReportError, uint64_t &Val)
static bool needPadding(uint64_t StartAddr, uint64_t Size, Align BoundaryAlignment)
Check if the branch needs padding.
static void writeFragment(raw_ostream &OS, const MCAssembler &Asm, const MCFragment &F)
Write the fragment F to the output file.
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.
static bool getLabelOffset(const MCAssembler &Asm, const MCSymbol &S, bool ReportError, uint64_t &Val)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
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(const MCAssembler &Asm, MCCVInlineLineTableFragment &F)
Encodes the binary annotations once we have a layout.
Definition: MCCodeView.cpp:484
void encodeDefRange(const MCAssembler &Asm, MCCVDefRangeFragment &F)
Definition: MCCodeView.cpp:621
int64_t getValue() const
Definition: MCFragment.h:315
Align getAlignment() const
Definition: MCFragment.h:313
unsigned getMaxBytesToEmit() const
Definition: MCFragment.h:319
bool hasEmitNops() const
Definition: MCFragment.h:321
unsigned getValueSize() const
Definition: MCFragment.h:317
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCFragment.h:327
virtual bool finishLayout(const MCAssembler &Asm) const
Definition: MCAsmBackend.h:222
virtual void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const
Relax the instruction in the given fragment to the next wider instruction.
Definition: MCAsmBackend.h:177
virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, MCAlignFragment &AF)
Hook which indicates if the target requires a fixup to be generated when handling an align directive ...
Definition: MCAsmBackend.h:111
virtual bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm, const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
virtual bool evaluateTargetFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCFragment *DF, const MCValue &Target, const MCSubtargetInfo *STI, uint64_t &Value, bool &WasForced)
Definition: MCAsmBackend.h:116
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:65
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual std::pair< bool, bool > relaxLEB128(const MCAssembler &Asm, MCLEBFragment &LF, int64_t &Value) const
Definition: MCAsmBackend.h:195
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...
MCContext & getContext() const
Definition: MCAssembler.h:182
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
uint64_t getSectionAddressSize(const MCSection &Sec) const
void Finish()
Finish - Do final processing and write the object to the output stream.
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:210
bool isBundlingEnabled() const
Definition: MCAssembler.h:208
void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
void dump() const
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:192
MCCodeEmitter * getEmitterPtr() const
Definition: MCAssembler.h:186
void layoutBundle(MCFragment *Prev, MCFragment *F) const
bool getRelaxAll() const
Definition: MCAssembler.h:205
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:190
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:82
bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:188
bool registerSection(MCSection &Section)
uint64_t computeFragmentSize(const MCFragment &F) const
Compute the effective fragment size.
const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
MCAsmBackend * getBackendPtr() const
Definition: MCAssembler.h:184
iterator_range< pointee_iterator< typename SmallVector< const MCSymbol *, 0 >::const_iterator > > symbols() const
Definition: MCAssembler.h:223
uint64_t getSectionFileSize(const MCSection &Sec) const
void reset()
Reuse an assembler instance.
Definition: MCAssembler.cpp:89
bool registerSymbol(const MCSymbol &Symbol)
uint64_t getFragmentOffset(const MCFragment &F) const
Definition: MCAssembler.h:153
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:194
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:563
uint64_t getSize() const
Definition: MCFragment.h:580
void setSize(uint64_t Value)
Definition: MCFragment.h:581
const MCFragment * getLastFragment() const
Definition: MCFragment.h:586
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCFragment.h:592
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:533
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition: MCFragment.h:501
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:193
Context object for machine code objects.
Definition: MCContext.h:83
CodeViewContext & getCVContext()
Definition: MCContext.cpp:1012
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1067
Fragment for data and encoded instructions.
Definition: MCFragment.h:231
static void encodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, SmallVectorImpl< char > &OS)
Definition: MCDwarf.cpp:1907
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:689
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:188
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:212
Interface implemented by fragments that contain encoded instructions and/or data.
Definition: MCFragment.h:118
const MCSubtargetInfo * getSubtargetInfo() const
Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
Definition: MCFragment.h:165
void setBundlePadding(uint8_t N)
Set the padding size for this fragment.
Definition: MCFragment.h:161
uint8_t getBundlePadding() const
Get the padding size that must be inserted before this fragment.
Definition: MCFragment.h:157
bool alignToBundleEnd() const
Should this fragment be placed at the end of an aligned bundle?
Definition: MCFragment.h:149
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
bool evaluateAsValue(MCValue &Res, const MCAssembler &Asm) const
Try to evaluate the expression to the form (a - b + constant) where neither a nor b are variables.
Definition: MCExpr.cpp:794
bool evaluateKnownAbsolute(int64_t &Res, const MCAssembler &Asm) const
Aggressive variant of evaluateAsRelocatable when relocations are unavailable (e.g.
Definition: MCExpr.cpp:565
bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:789
SMLoc getLoc() const
Definition: MCExpr.h:79
uint8_t getValueSize() const
Definition: MCFragment.h:351
uint64_t getValue() const
Definition: MCFragment.h:350
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
MCSection * getParent() const
Definition: MCFragment.h:93
MCFragment * getNext() const
Definition: MCFragment.h:89
bool hasInstructions() const
Does this fragment have instructions emitted into it? By default this is false, but specific fragment...
Definition: MCFragment.h:103
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
bool isSigned() const
Definition: MCFragment.h:433
const MCExpr & getValue() const
Definition: MCFragment.h:430
void setValue(const MCExpr *Expr)
Definition: MCFragment.h:431
int64_t getControlledNopLength() const
Definition: MCFragment.h:380
int64_t getNumBytes() const
Definition: MCFragment.h:379
const MCSubtargetInfo * getSubtargetInfo() const
Definition: MCFragment.h:384
SMLoc getLoc() const
Definition: MCFragment.h:382
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const
virtual void executePostLayoutBinding(MCAssembler &Asm)
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
bool getSubsectionsViaSymbols() const
virtual uint64_t writeObject(MCAssembler &Asm)=0
Write the object file and returns the number of bytes written.
virtual void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
Record a relocation entry.
SMLoc getLoc() const
Definition: MCFragment.h:409
uint8_t getValue() const
Definition: MCFragment.h:407
const MCExpr & getOffset() const
Definition: MCFragment.h:405
const MCExpr & getAddrDelta() const
Definition: MCFragment.h:609
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:261
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
void dump() const
Definition: MCSection.cpp:72
void setOrdinal(unsigned Value)
Definition: MCSection.h:154
bool isVirtualSection() const
Check whether this section is "virtual", that is has no actual object file contents.
Definition: MCSection.h:193
virtual bool useCodeAlign() const =0
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
FragList * curFragList() const
Definition: MCSection.h:176
Generic base class for all target subtargets.
Represents a symbol table index fragment.
Definition: MCFragment.h:484
const MCSymbol * getSymbol()
Definition: MCFragment.h:491
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
const MCSymbol & getSymbol() const
Definition: MCExpr.h:406
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:305
bool isCommon() const
Is this a 'common' symbol.
Definition: MCSymbol.h:387
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:300
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
uint64_t getOffset() const
Definition: MCSymbol.h:327
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
void push_back(const T &Elt)
Definition: SmallVector.h:426
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
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:147
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition: CallingConv.h:76
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
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.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
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.