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