LLVM 24.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"
12#include "llvm/ADT/Statistic.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Twine.h"
16#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCCodeView.h"
19#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCDwarf.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCFixup.h"
23#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCSFrame.h"
26#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCValue.h"
31#include "llvm/Support/Debug.h"
34#include "llvm/Support/LEB128.h"
36#include <cassert>
37#include <cstdint>
38#include <tuple>
39#include <utility>
40
41using namespace llvm;
42
43namespace llvm {
44class MCSubtargetInfo;
45}
46
47#define DEBUG_TYPE "assembler"
48
49namespace {
50namespace stats {
51
52STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
53STATISTIC(EmittedRelaxableFragments,
54 "Number of emitted assembler fragments - relaxable");
55STATISTIC(EmittedDataFragments,
56 "Number of emitted assembler fragments - data");
57STATISTIC(EmittedAlignFragments,
58 "Number of emitted assembler fragments - align");
59STATISTIC(EmittedFillFragments,
60 "Number of emitted assembler fragments - fill");
61STATISTIC(EmittedNopsFragments, "Number of emitted assembler fragments - nops");
62STATISTIC(EmittedOrgFragments, "Number of emitted assembler fragments - org");
63STATISTIC(Fixups, "Number of fixups");
64STATISTIC(FixupEvalForRelax, "Number of fixup evaluations for relaxation");
65STATISTIC(ObjectBytes, "Number of emitted object file bytes");
66STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
67STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
68
69} // end namespace stats
70} // end anonymous namespace
71
72// FIXME FIXME FIXME: There are number of places in this file where we convert
73// what is a 64-bit assembler value used for computation into a value in the
74// object file, which may truncate it. We should detect that truncation where
75// invalid and report errors back.
76
77/* *** */
78
80 std::unique_ptr<MCAsmBackend> Backend,
81 std::unique_ptr<MCCodeEmitter> Emitter,
82 std::unique_ptr<MCObjectWriter> Writer)
83 : Context(Context), Backend(std::move(Backend)),
84 Emitter(std::move(Emitter)), Writer(std::move(Writer)) {
85 if (this->Backend)
86 this->Backend->setAssembler(this);
87 if (this->Writer)
88 this->Writer->setAssembler(this);
89}
90
92 HasLayout = false;
93 HasFinalLayout = false;
94 RelaxAll = false;
95 BundleAlign.reset();
96 Sections.clear();
97 Symbols.clear();
98 ThumbFuncs.clear();
99
100 // reset objects owned by us
101 if (getBackendPtr())
102 getBackendPtr()->reset();
103 if (getEmitterPtr())
104 getEmitterPtr()->reset();
105 if (Writer)
106 Writer->reset();
107}
108
110 if (Section.isRegistered())
111 return false;
112 Sections.push_back(&Section);
113 Section.setIsRegistered(true);
114 return true;
115}
116
117bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
118 if (ThumbFuncs.count(Symbol))
119 return true;
120
121 if (!Symbol->isVariable())
122 return false;
123
124 const MCExpr *Expr = Symbol->getVariableValue();
125
126 MCValue V;
127 if (!Expr->evaluateAsRelocatable(V, nullptr))
128 return false;
129
130 if (V.getSubSym() || V.getSpecifier())
131 return false;
132
133 auto *Sym = V.getAddSym();
134 if (!Sym || V.getSpecifier())
135 return false;
136
137 if (!isThumbFunc(Sym))
138 return false;
139
140 ThumbFuncs.insert(Symbol); // Cache it.
141 return true;
142}
143
144bool MCAssembler::evaluateFixup(const MCFragment &F, MCFixup &Fixup,
146 bool RecordReloc, uint8_t *Data) const {
147 if (RecordReloc)
148 ++stats::Fixups;
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 Value = 0;
158 if (!Expr->evaluateAsRelocatable(Target, this)) {
159 reportError(Fixup.getLoc(), "expected relocatable expression");
160 return true;
161 }
162
163 bool IsResolved = false;
164 if (auto State = getBackend().evaluateFixup(F, Fixup, Target, Value)) {
165 IsResolved = *State;
166 } else {
167 const MCSymbol *Add = Target.getAddSym();
168 const MCSymbol *Sub = Target.getSubSym();
169 Value += Target.getConstant();
170 if (Add && Add->isDefined())
172 if (Sub && Sub->isDefined())
174
175 if (Fixup.isPCRel()) {
176 Value -= getFragmentOffset(F) + Fixup.getOffset();
177 // During relaxation, F's offset is already updated but forward reference
178 // targets are stale. Add Stretch so that the displacement equals
179 // target_old - source_old, preventing premature relaxation.
180 if (Stretch) {
181 assert(!RecordReloc &&
182 "Stretch should only be applied during relaxation");
183 MCFragment *AF = Add ? Add->getFragment() : nullptr;
184 if (AF && AF->getLayoutOrder() > F.getLayoutOrder())
185 Value += Stretch;
186 MCFragment *SF = Sub ? Sub->getFragment() : nullptr;
187 if (SF && SF->getLayoutOrder() > F.getLayoutOrder())
188 Value -= Stretch;
189 }
190 if (Add && !Sub && !Add->isUndefined() && !Add->isAbsolute()) {
192 *Add, F, false, true);
193 }
194 } else {
195 IsResolved = Target.isAbsolute();
196 }
197 }
198
199 if (!RecordReloc)
200 return IsResolved;
201
202 if (IsResolved && mc::isRelocRelocation(Fixup.getKind()))
203 IsResolved = false;
204 getBackend().applyFixup(F, Fixup, Target, Data, Value, IsResolved);
205 return true;
206}
207
209 assert(getBackendPtr() && "Requires assembler backend");
210 switch (F.getKind()) {
220 return F.getSize();
221 case MCFragment::FT_Fill: {
222 auto &FF = static_cast<const MCFillFragment &>(F);
223 int64_t NumValues = 0;
224 if (!FF.getNumValues().evaluateKnownAbsolute(NumValues, *this)) {
225 recordError(FF.getLoc(), "expected assembly-time absolute expression");
226 return 0;
227 }
228 int64_t Size = NumValues * FF.getValueSize();
229 if (Size < 0) {
230 recordError(FF.getLoc(), "invalid number of bytes");
231 return 0;
232 }
233 return Size;
234 }
235
237 return F.getSize();
238
240 return cast<MCNopsFragment>(F).getNumBytes();
241
243 return cast<MCBoundaryAlignFragment>(F).getSize();
244
246 return 4;
247
248 case MCFragment::FT_Org: {
251 if (!OF.getOffset().evaluateAsValue(Value, *this)) {
252 recordError(OF.getLoc(), "expected assembly-time absolute expression");
253 return 0;
254 }
255
256 uint64_t FragmentOffset = getFragmentOffset(OF);
257 int64_t TargetLocation = Value.getConstant();
258 if (const auto *SA = Value.getAddSym()) {
259 uint64_t Val;
260 if (!getSymbolOffset(*SA, Val)) {
261 recordError(OF.getLoc(), "expected absolute expression");
262 return 0;
263 }
264 TargetLocation += Val;
265 }
266 int64_t Size = TargetLocation - FragmentOffset;
267 if (Size < 0 || Size >= 0x40000000) {
268 recordError(OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
269 "' (at offset '" + Twine(FragmentOffset) +
270 "')");
271 return 0;
272 }
273 return Size;
274 }
275 }
276
277 llvm_unreachable("invalid fragment kind");
278}
279
280// Simple getSymbolOffset helper for the non-variable case.
281static bool getLabelOffset(const MCAssembler &Asm, const MCSymbol &S,
282 bool ReportError, uint64_t &Val) {
283 if (!S.getFragment()) {
284 if (ReportError)
285 reportFatalUsageError("cannot evaluate undefined symbol '" + S.getName() +
286 "'");
287 return false;
288 }
289 Val = Asm.getFragmentOffset(*S.getFragment()) + S.getOffset();
290 return true;
291}
292
293static bool getSymbolOffsetImpl(const MCAssembler &Asm, const MCSymbol &S,
294 bool ReportError, uint64_t &Val) {
295 if (!S.isVariable())
296 return getLabelOffset(Asm, S, ReportError, Val);
297
298 // If SD is a variable, evaluate it.
301 reportFatalUsageError("cannot evaluate equated symbol '" + S.getName() +
302 "'");
303
304 uint64_t Offset = Target.getConstant();
305
306 const MCSymbol *A = Target.getAddSym();
307 if (A) {
308 uint64_t ValA;
309 // FIXME: On most platforms, `Target`'s component symbols are labels from
310 // having been simplified during evaluation, but on Mach-O they can be
311 // variables due to PR19203. This, and the line below for `B` can be
312 // restored to call `getLabelOffset` when PR19203 is fixed.
313 if (!getSymbolOffsetImpl(Asm, *A, ReportError, ValA))
314 return false;
315 Offset += ValA;
316 }
317
318 const MCSymbol *B = Target.getSubSym();
319 if (B) {
320 uint64_t ValB;
321 if (!getSymbolOffsetImpl(Asm, *B, ReportError, ValB))
322 return false;
323 Offset -= ValB;
324 }
325
326 Val = Offset;
327 return true;
328}
329
331 return getSymbolOffsetImpl(*this, S, false, Val);
332}
333
335 uint64_t Val;
336 getSymbolOffsetImpl(*this, S, true, Val);
337 return Val;
338}
339
340const MCSymbol *MCAssembler::getBaseSymbol(const MCSymbol &Symbol) const {
341 assert(HasLayout);
342 if (!Symbol.isVariable())
343 return &Symbol;
344
345 const MCExpr *Expr = Symbol.getVariableValue();
347 if (!Expr->evaluateAsValue(Value, *this)) {
348 reportError(Expr->getLoc(), "expression could not be evaluated");
349 return nullptr;
350 }
351
352 const MCSymbol *SymB = Value.getSubSym();
353 if (SymB) {
354 reportError(Expr->getLoc(),
355 Twine("symbol '") + SymB->getName() +
356 "' could not be evaluated in a subtraction expression");
357 return nullptr;
358 }
359
360 const MCSymbol *A = Value.getAddSym();
361 if (!A)
362 return nullptr;
363
364 const MCSymbol &ASym = *A;
365 if (ASym.isCommon()) {
366 reportError(Expr->getLoc(), "Common symbol '" + ASym.getName() +
367 "' cannot be used in assignment expr");
368 return nullptr;
369 }
370
371 return &ASym;
372}
373
375 const MCFragment &F = *Sec.curFragList()->Tail;
376 assert(HasLayout && F.getKind() == MCFragment::FT_Data);
377 return getFragmentOffset(F) + F.getSize();
378}
379
381 // Virtual sections have no file size.
382 if (Sec.isBssSection())
383 return 0;
384 return getSectionAddressSize(Sec);
385}
386
388 bool Changed = !Symbol.isRegistered();
389 if (Changed) {
390 Symbol.setIsRegistered(true);
391 Symbols.push_back(&Symbol);
392 }
393 return Changed;
394}
395
396void MCAssembler::addRelocDirective(RelocDirective RD) {
397 relocDirectives.push_back(RD);
398}
399
400/// Write \p NumBytes of NOPs at \p Offset in chunks of at most \p MaxNopSize.
401/// When bundling is enabled, no chunk crosses a bundle boundary.
402static void writeControlledNops(raw_ostream &OS, const MCAssembler &Asm,
403 uint64_t NumBytes, uint64_t Offset,
404 uint64_t MaxNopSize,
405 const MCSubtargetInfo *STI) {
406 while (NumBytes) {
407 uint64_t Size = std::min(NumBytes, MaxNopSize);
408 if (Asm.isBundlingEnabled()) {
409 uint64_t BundleSize = Asm.getBundleAlign().value();
410 Size = std::min(Size, BundleSize - (Offset & (BundleSize - 1)));
411 }
412 assert(Size && "try to emit zero-sized NOP");
413 if (!Asm.getBackend().writeNopData(OS, Size, STI))
414 reportFatalInternalError("unable to write nop sequence of " +
415 Twine(Size) + " bytes");
416 NumBytes -= Size;
417 Offset += Size;
418 }
419}
420
421/// Write the fragment \p F to the output file.
422static void writeFragment(raw_ostream &OS, const MCAssembler &Asm,
423 const MCFragment &F) {
424 // FIXME: Embed in fragments instead?
425 uint64_t FragmentSize = Asm.computeFragmentSize(F);
426
427 llvm::endianness Endian = Asm.getBackend().Endian;
428
429 // This variable (and its dummy usage) is to participate in the assert at
430 // the end of the function.
431 uint64_t Start = OS.tell();
432 (void) Start;
433
434 ++stats::EmittedFragments;
435
436 switch (F.getKind()) {
445 if (F.getKind() == MCFragment::FT_Data)
446 ++stats::EmittedDataFragments;
447 else if (F.getKind() == MCFragment::FT_Relaxable)
448 ++stats::EmittedRelaxableFragments;
449 const auto &EF = cast<MCFragment>(F);
450 OS << StringRef(EF.getContents().data(), EF.getContents().size());
451 OS << StringRef(EF.getVarContents().data(), EF.getVarContents().size());
452 } break;
453
455 ++stats::EmittedAlignFragments;
456 OS << StringRef(F.getContents().data(), F.getContents().size());
457 assert(F.getAlignFillLen() &&
458 "Invalid virtual align in concrete fragment!");
459
460 uint64_t Count = (FragmentSize - F.getFixedSize()) / F.getAlignFillLen();
461 assert((FragmentSize - F.getFixedSize()) % F.getAlignFillLen() == 0 &&
462 "computeFragmentSize computed size is incorrect");
463
464 // In the nops mode, call the backend hook to write `Count` nops.
465 if (F.hasAlignEmitNops()) {
466 writeControlledNops(OS, Asm, Count,
467 Asm.getFragmentOffset(F) + F.getFixedSize(), Count,
468 F.getSubtargetInfo());
469 } else {
470 // Otherwise, write out in multiples of the value size.
471 for (uint64_t i = 0; i != Count; ++i) {
472 switch (F.getAlignFillLen()) {
473 default:
474 llvm_unreachable("Invalid size!");
475 case 1:
476 OS << char(F.getAlignFill());
477 break;
478 case 2:
479 support::endian::write<uint16_t>(OS, F.getAlignFill(), Endian);
480 break;
481 case 4:
482 support::endian::write<uint32_t>(OS, F.getAlignFill(), Endian);
483 break;
484 case 8:
485 support::endian::write<uint64_t>(OS, F.getAlignFill(), Endian);
486 break;
487 }
488 }
489 }
490 } break;
491
493 OS << StringRef(F.getContents().data(), F.getContents().size());
494 uint64_t PadSize = FragmentSize - F.getContents().size();
495 if (F.getPrefAlignEmitNops()) {
496 if (!Asm.getBackend().writeNopData(OS, PadSize, F.getSubtargetInfo()))
497 reportFatalInternalError("unable to write nop sequence of " +
498 Twine(PadSize) + " bytes");
499 } else if (F.getPrefAlignFill() == 0) {
500 OS.write_zeros(PadSize);
501 } else {
502 char B = char(F.getPrefAlignFill());
503 for (uint64_t I = 0; I < PadSize; ++I)
504 OS << B;
505 }
506 break;
507 }
508
509 case MCFragment::FT_Fill: {
510 ++stats::EmittedFillFragments;
512 uint64_t V = FF.getValue();
513 unsigned VSize = FF.getValueSize();
514 const unsigned MaxChunkSize = 16;
515 char Data[MaxChunkSize];
516 assert(0 < VSize && VSize <= MaxChunkSize && "Illegal fragment fill size");
517 // Duplicate V into Data as byte vector to reduce number of
518 // writes done. As such, do endian conversion here.
519 for (unsigned I = 0; I != VSize; ++I) {
520 unsigned index = Endian == llvm::endianness::little ? I : (VSize - I - 1);
521 Data[I] = uint8_t(V >> (index * 8));
522 }
523 for (unsigned I = VSize; I < MaxChunkSize; ++I)
524 Data[I] = Data[I - VSize];
525
526 // Set to largest multiple of VSize in Data.
527 const unsigned NumPerChunk = MaxChunkSize / VSize;
528 // Set ChunkSize to largest multiple of VSize in Data
529 const unsigned ChunkSize = VSize * NumPerChunk;
530
531 // Do copies by chunk.
532 StringRef Ref(Data, ChunkSize);
533 for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I)
534 OS << Ref;
535
536 // do remainder if needed.
537 unsigned TrailingCount = FragmentSize % ChunkSize;
538 if (TrailingCount)
539 OS.write(Data, TrailingCount);
540 break;
541 }
542
543 case MCFragment::FT_Nops: {
544 ++stats::EmittedNopsFragments;
546
547 int64_t NumBytes = NF.getNumBytes();
548 int64_t ControlledNopLength = NF.getControlledNopLength();
549 int64_t MaximumNopLength =
550 Asm.getBackend().getMaximumNopSize(*NF.getSubtargetInfo());
551
552 assert(NumBytes > 0 && "Expected positive NOPs fragment size");
553 assert(ControlledNopLength >= 0 && "Expected non-negative NOP size");
554
555 if (ControlledNopLength > MaximumNopLength) {
556 Asm.reportError(NF.getLoc(), "illegal NOP size " +
557 std::to_string(ControlledNopLength) +
558 ". (expected within [0, " +
559 std::to_string(MaximumNopLength) + "])");
560 // Clamp the NOP length as reportError does not stop the execution
561 // immediately.
562 ControlledNopLength = MaximumNopLength;
563 }
564
565 // Use maximum value if the size of each NOP is not specified
566 if (!ControlledNopLength)
567 ControlledNopLength = MaximumNopLength;
568
569 writeControlledNops(OS, Asm, (uint64_t)NumBytes, Asm.getFragmentOffset(NF),
570 (uint64_t)ControlledNopLength, NF.getSubtargetInfo());
571 break;
572 }
573
576 writeControlledNops(OS, Asm, FragmentSize, Asm.getFragmentOffset(BF),
577 FragmentSize, BF.getSubtargetInfo());
578 break;
579 }
580
584 break;
585 }
586
587 case MCFragment::FT_Org: {
588 ++stats::EmittedOrgFragments;
590
591 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
592 OS << char(OF.getValue());
593
594 break;
595 }
596
597 }
598
599 assert(OS.tell() - Start == FragmentSize &&
600 "The stream should advance by fragment size");
601}
602
604 const MCSection *Sec) const {
605 assert(getBackendPtr() && "Expected assembler backend");
606
607 if (Sec->isBssSection()) {
608 assert(getSectionFileSize(*Sec) == 0 && "Invalid size for section!");
609
610 // Ensure no fixups or non-zero bytes are written to BSS sections, catching
611 // errors in both input assembly code and MCStreamer API usage. Location is
612 // not tracked for efficiency.
613 auto Fn = [](char c) { return c != 0; };
614 for (const MCFragment &F : *Sec) {
615 bool HasNonZero = false;
616 switch (F.getKind()) {
617 default:
618 reportFatalInternalError("BSS section '" + Sec->getName() +
619 "' contains invalid fragment");
620 break;
623 HasNonZero =
624 any_of(F.getContents(), Fn) || any_of(F.getVarContents(), Fn);
625 break;
627 // Disallowed for API usage. AsmParser changes non-zero fill values to
628 // 0.
629 assert(F.getAlignFill() == 0 && "Invalid align in virtual section!");
630 break;
632 assert(!F.getPrefAlignEmitNops() && F.getPrefAlignFill() == 0 &&
633 "Invalid align in BSS");
634 break;
636 HasNonZero = cast<MCFillFragment>(F).getValue() != 0;
637 break;
639 HasNonZero = cast<MCOrgFragment>(F).getValue() != 0;
640 break;
641 }
642 if (HasNonZero) {
643 reportError(SMLoc(), "BSS section '" + Sec->getName() +
644 "' cannot have non-zero bytes");
645 break;
646 }
647 if (F.getFixups().size() || F.getVarFixups().size()) {
649 "BSS section '" + Sec->getName() + "' cannot have fixups");
650 break;
651 }
652 }
653
654 return;
655 }
656
657 uint64_t Start = OS.tell();
658 (void)Start;
659
660 for (const MCFragment &F : *Sec)
661 writeFragment(OS, *this, F);
662
664 assert(getContext().hadError() ||
665 OS.tell() - Start == getSectionAddressSize(*Sec));
666}
667
669 assert(getBackendPtr() && "Expected assembler backend");
670 DEBUG_WITH_TYPE("mc-dump-pre", {
671 errs() << "assembler backend - pre-layout\n--\n";
672 dump();
673 });
674
675 // Assign section ordinals.
676 unsigned SectionIndex = 0;
677 for (MCSection &Sec : *this) {
678 Sec.setOrdinal(SectionIndex++);
679
680 // Chain together fragments from all subsections.
681 if (Sec.Subsections.size() > 1) {
682 MCFragment Dummy;
683 MCFragment *Tail = &Dummy;
684 for (auto &[_, List] : Sec.Subsections) {
685 assert(List.Head);
686 Tail->Next = List.Head;
687 Tail = List.Tail;
688 }
689 Sec.Subsections.clear();
690 Sec.Subsections.push_back({0u, {Dummy.getNext(), Tail}});
691 Sec.CurFragList = &Sec.Subsections[0].second;
692
693 unsigned FragmentIndex = 0;
694 for (MCFragment &Frag : Sec)
695 Frag.setLayoutOrder(FragmentIndex++);
696 }
697 }
698
699 // Layout until everything fits.
700 this->HasLayout = true;
701 for (MCSection &Sec : *this)
702 layoutSection(Sec);
703 unsigned FirstStable = Sections.size();
704 while ((FirstStable = relaxOnce(FirstStable)) > 0)
705 if (getContext().hadError())
706 return;
707
708 // Some targets might want to adjust fragment offsets. If so, perform another
709 // layout iteration.
710 if (getBackend().finishLayout())
711 for (MCSection &Sec : *this)
712 layoutSection(Sec);
713
715
716 DEBUG_WITH_TYPE("mc-dump", {
717 errs() << "assembler backend - final-layout\n--\n";
718 dump(); });
719
720 // Allow the object writer a chance to perform post-layout binding (for
721 // example, to set the index fields in the symbol data).
723
724 // Fragment sizes are finalized. For RISC-V linker relaxation, this flag
725 // helps check whether a PC-relative fixup is fully resolved.
726 this->HasFinalLayout = true;
727
728 // Stores the current .reloc group for each fragment.
729 //
730 // A .reloc group is a consecutive sequence of .reloc relocations that have
731 // an offset <= the first relocation's offset. A relocation with offset > the
732 // first relocation's offset starts a new group. Relocation groups are
733 // inserted in offset order using the offset of the first relocation, but the
734 // source ordering of relocations within the group is preserved.
736 auto DrainRelocGroup = [](MCFragment *F, std::vector<MCFixup> &Group) {
737 F->insertRelocFixups(Group);
738 Group.clear();
739 };
740
741 // Resolve .reloc offsets and add fixups.
742 for (auto &PF : relocDirectives) {
743 MCValue Res;
744 auto &O = PF.Offset;
745 if (!O.evaluateAsValue(Res, *this)) {
746 getContext().reportError(O.getLoc(), ".reloc offset is not relocatable");
747 continue;
748 }
749 auto *Sym = Res.getAddSym();
750 auto *F = Sym ? Sym->getFragment() : nullptr;
751 auto *Sec = F ? F->getParent() : nullptr;
752 if (Res.getSubSym() || !Sec) {
753 getContext().reportError(O.getLoc(),
754 ".reloc offset is not relative to a section");
755 continue;
756 }
757
758 uint64_t Offset = Sym ? Sym->getOffset() + Res.getConstant() : 0;
759 auto Fixup = MCFixup::create(Offset, PF.Expr, PF.Kind);
760 auto &Group = RelocGroups[F];
761 if (!Group.empty() && Group[0].getOffset() < Offset)
762 DrainRelocGroup(F, Group);
763 Group.push_back(Fixup);
764 }
765
766 for (auto &[F, Group] : RelocGroups)
767 DrainRelocGroup(F, Group);
768
769 // Evaluate and apply the fixups, generating relocation entries as necessary.
770 for (MCSection &Sec : *this) {
771 for (MCFragment &F : Sec) {
772 // Process fragments with fixups here.
773 auto Contents = F.getContents();
774 for (MCFixup &Fixup : F.getFixups()) {
775 uint64_t FixedValue;
778 Fixup.getOffset() <= F.getFixedSize());
779 auto *Data =
780 reinterpret_cast<uint8_t *>(Contents.data() + Fixup.getOffset());
781 evaluateFixup(F, Fixup, Target, FixedValue,
782 /*RecordReloc=*/true, Data);
783 }
784 // In the variable part, fixup offsets are relative to the fixed part's
785 // start.
786 for (MCFixup &Fixup : F.getVarFixups()) {
787 uint64_t FixedValue;
790 (Fixup.getOffset() >= F.getFixedSize() &&
791 Fixup.getOffset() <= F.getSize()));
792 auto *Data = reinterpret_cast<uint8_t *>(
793 F.getVarContents().data() + (Fixup.getOffset() - F.getFixedSize()));
794 evaluateFixup(F, Fixup, Target, FixedValue,
795 /*RecordReloc=*/true, Data);
796 }
797 }
798 }
799}
800
802 layout();
803
804 // Write the object file if there is no error. The output would be discarded
805 // anyway, and this avoids wasting time writing large files (e.g. when testing
806 // fixup overflow with `.space 0x80000000`).
807 if (!getContext().hadError())
808 stats::ObjectBytes += getWriter().writeObject();
809
810 HasLayout = false;
811 assert(PendingErrors.empty());
812}
813
814void MCAssembler::relaxAlign(MCFragment &F) {
815 uint64_t Offset = F.Offset + F.getFixedSize();
816 unsigned Size = offsetToAlignment(Offset, F.getAlignment());
817 bool AlignFixup = false;
818 if (F.hasAlignEmitNops()) {
819 AlignFixup = getBackend().relaxAlign(F, Size);
820 if (!AlignFixup)
821 while (Size % getBackend().getMinimumNopSize())
822 Size += F.getAlignment().value();
823 }
824 if (!AlignFixup && Size > F.getAlignMaxBytesToEmit())
825 Size = 0;
826 F.VarContentStart = F.getFixedSize();
827 F.VarContentEnd = F.VarContentStart + Size;
828 if (F.VarContentEnd > F.getParent()->ContentStorage.size())
829 F.getParent()->ContentStorage.resize(F.VarContentEnd);
830}
831
832// Compute the body size by walking forward from F to the End symbol and
833// summing fragment sizes. This avoids depending on stale layout offsets.
834void MCAssembler::relaxPrefAlign(MCFragment &F) {
835 uint64_t RawStart = F.Offset + F.getFixedSize();
836 const MCSymbol &End = F.getPrefAlignEnd();
837 if (!End.getFragment() || End.getFragment()->getParent() != F.getParent()) {
838 recordError(SMLoc(), ".prefalign end symbol '" + End.getName() +
839 "' must be in the current section");
840 return;
841 }
842 const MCFragment *EndFrag = End.getFragment();
843 if (EndFrag->getLayoutOrder() <= F.getLayoutOrder())
844 return;
845 uint64_t BodySize = End.getOffset();
846 for (auto *Cur = F.getNext(); Cur != EndFrag; Cur = Cur->getNext())
847 BodySize += computeFragmentSize(*Cur);
848 // Intervening FT_Align's padding depends on where this prefalign lands, so
849 // `BodySize` depends on this prefalign's own padding and may not reach a
850 // fixed point. Break the cycle with a monotone value.
851 Align NewAlign =
852 std::min(Align(llvm::bit_ceil(BodySize)), F.getPrefAlignPreferred());
853 NewAlign = std::max(NewAlign, F.getPrefAlignComputed());
854 F.setPrefAlignComputed(NewAlign);
855 uint64_t NewPadSize = offsetToAlignment(RawStart, NewAlign);
856 F.VarContentStart = F.getFixedSize();
857 F.VarContentEnd = F.VarContentStart + NewPadSize;
858 if (F.VarContentEnd > F.getParent()->ContentStorage.size())
859 F.getParent()->ContentStorage.resize(F.VarContentEnd);
860 // Update the maximum alignment on the current section if necessary, similar
861 // to MCObjectStreamer::emitValueToAlignment.
862 F.getParent()->ensureMinAlignment(NewAlign);
863}
864
865bool MCAssembler::fixupNeedsRelaxation(const MCFragment &F,
866 const MCFixup &Fixup) const {
867 ++stats::FixupEvalForRelax;
868 MCValue Target;
869 uint64_t Value;
870 bool Resolved = evaluateFixup(F, const_cast<MCFixup &>(Fixup), Target, Value,
871 /*RecordReloc=*/false, {});
873 Resolved);
874}
875
876void MCAssembler::relaxInstruction(MCFragment &F) {
878 "Expected CodeEmitter defined for relaxInstruction");
879 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
880 // are intentionally pushing out inst fragments, or because we relaxed a
881 // previous instruction to one that doesn't need relaxation.
882 if (!getBackend().mayNeedRelaxation(F.getOpcode(), F.getOperands(),
883 *F.getSubtargetInfo()))
884 return;
885
886 bool DoRelax = false;
887 for (const MCFixup &Fixup : F.getVarFixups())
888 if ((DoRelax = fixupNeedsRelaxation(F, Fixup)))
889 break;
890 if (!DoRelax)
891 return;
892
893 ++stats::RelaxedInstructions;
894
895 // TODO Refactor relaxInstruction to accept MCFragment and remove
896 // `setInst`.
897 MCInst Relaxed = F.getInst();
898 getBackend().relaxInstruction(Relaxed, *F.getSubtargetInfo());
899
900 // Encode the new instruction.
901 F.setInst(Relaxed);
904 getEmitter().encodeInstruction(Relaxed, Data, Fixups, *F.getSubtargetInfo());
905 F.setVarContents(Data);
906 F.setVarFixups(Fixups);
907}
908
909void MCAssembler::relaxLEB(MCFragment &F) {
910 unsigned PadTo = F.getVarSize();
911 int64_t Value;
912 F.clearVarFixups();
913 // Use evaluateKnownAbsolute for Mach-O as a hack: .subsections_via_symbols
914 // requires that .uleb128 A-B is foldable where A and B reside in different
915 // fragments. This is used by __gcc_except_table.
917 ? F.getLEBValue().evaluateKnownAbsolute(Value, *this)
918 : F.getLEBValue().evaluateAsAbsolute(Value, *this);
919 if (!Abs) {
920 bool Relaxed, UseZeroPad;
921 std::tie(Relaxed, UseZeroPad) = getBackend().relaxLEB128(F, Value);
922 if (!Relaxed) {
923 reportError(F.getLEBValue().getLoc(),
924 Twine(F.isLEBSigned() ? ".s" : ".u") +
925 "leb128 expression is not absolute");
926 F.setLEBValue(MCConstantExpr::create(0, Context));
927 }
928 uint8_t Tmp[10]; // maximum size: ceil(64/7)
929 PadTo = std::max(PadTo, encodeULEB128(uint64_t(Value), Tmp));
930 if (UseZeroPad)
931 Value = 0;
932 }
933 uint8_t Data[16];
934 size_t Size = 0;
935 // The compiler can generate EH table assembly that is impossible to assemble
936 // without either adding padding to an LEB fragment or adding extra padding
937 // to a later alignment fragment. To accommodate such tables, relaxation can
938 // only increase an LEB fragment size here, not decrease it. See PR35809.
939 if (F.isLEBSigned())
940 Size = encodeSLEB128(Value, Data, PadTo);
941 else
942 Size = encodeULEB128(Value, Data, PadTo);
943 F.setVarContents({reinterpret_cast<char *>(Data), Size});
944}
945
946/// Check if the branch crosses the boundary.
947///
948/// \param StartAddr start address of the fused/unfused branch.
949/// \param Size size of the fused/unfused branch.
950/// \param BoundaryAlignment alignment requirement of the branch.
951/// \returns true if the branch cross the boundary.
952static bool mayCrossBoundary(uint64_t StartAddr, uint64_t Size,
953 Align BoundaryAlignment) {
954 uint64_t EndAddr = StartAddr + Size;
955 return (StartAddr >> Log2(BoundaryAlignment)) !=
956 ((EndAddr - 1) >> Log2(BoundaryAlignment));
957}
958
959/// Check if the branch is against the boundary.
960///
961/// \param StartAddr start address of the fused/unfused branch.
962/// \param Size size of the fused/unfused branch.
963/// \param BoundaryAlignment alignment requirement of the branch.
964/// \returns true if the branch is against the boundary.
966 Align BoundaryAlignment) {
967 uint64_t EndAddr = StartAddr + Size;
968 return (EndAddr & (BoundaryAlignment.value() - 1)) == 0;
969}
970
971/// Check if the branch needs padding.
972///
973/// \param StartAddr start address of the fused/unfused branch.
974/// \param Size size of the fused/unfused branch.
975/// \param BoundaryAlignment alignment requirement of the branch.
976/// \returns true if the branch needs padding.
977static bool needPadding(uint64_t StartAddr, uint64_t Size,
978 Align BoundaryAlignment) {
979 return mayCrossBoundary(StartAddr, Size, BoundaryAlignment) ||
980 isAgainstBoundary(StartAddr, Size, BoundaryAlignment);
981}
982
983/// Compute the padding size to boundary-align the fragments BF is responsible
984/// for.
986 const MCBoundaryAlignFragment &BF) {
987 assert(BF.getLastFragment() && "the fragment range to align must be known");
988
989 uint64_t AlignedOffset = Asm.getFragmentOffset(BF);
990 uint64_t AlignedSize = 0;
991 for (const MCFragment *F = BF.getNext();; F = F->getNext()) {
992 AlignedSize += Asm.computeFragmentSize(*F);
993 if (F == BF.getLastFragment())
994 break;
995 }
996
997 Align BoundaryAlignment = BF.getAlignment();
998
999 if (!Asm.isBundlingEnabled())
1000 return needPadding(AlignedOffset, AlignedSize, BoundaryAlignment)
1001 ? offsetToAlignment(AlignedOffset, BoundaryAlignment)
1002 : 0U;
1003 if (BF.isAlignToEnd())
1004 return offsetToAlignment(AlignedOffset + AlignedSize, BoundaryAlignment);
1005
1006 // For bundle alignment, we only pad instructions that cross the boundary.
1007 return mayCrossBoundary(AlignedOffset, AlignedSize, BoundaryAlignment)
1008 ? offsetToAlignment(AlignedOffset, BoundaryAlignment)
1009 : 0U;
1010}
1011
1012void MCAssembler::relaxBoundaryAlign(MCBoundaryAlignFragment &BF) {
1013 // BoundaryAlignFragment that doesn't need to align any fragment should not be
1014 // relaxed.
1015 if (!BF.getLastFragment())
1016 return;
1017
1018 uint64_t NewSize = computeBoundaryAlignSize(*this, BF);
1019 if (NewSize == BF.getSize())
1020 return;
1021 BF.setSize(NewSize);
1022}
1023
1024void MCAssembler::relaxDwarfLineAddr(MCFragment &F) {
1025 if (getBackend().relaxDwarfLineAddr(F))
1026 return;
1027
1028 MCContext &Context = getContext();
1029 int64_t AddrDelta;
1030 bool Abs = F.getDwarfAddrDelta().evaluateKnownAbsolute(AddrDelta, *this);
1031 assert(Abs && "We created a line delta with an invalid expression");
1032 (void)Abs;
1033 SmallVector<char, 8> Data;
1035 F.getDwarfLineDelta(), AddrDelta, Data);
1036 F.setVarContents(Data);
1037 F.clearVarFixups();
1038}
1039
1040void MCAssembler::relaxDwarfCallFrameFragment(MCFragment &F) {
1041 if (getBackend().relaxDwarfCFA(F))
1042 return;
1043
1044 MCContext &Context = getContext();
1045 int64_t Value;
1046 bool Abs = F.getDwarfAddrDelta().evaluateAsAbsolute(Value, *this);
1047 if (!Abs) {
1048 reportError(F.getDwarfAddrDelta().getLoc(),
1049 "invalid CFI advance_loc expression");
1050 F.setDwarfAddrDelta(MCConstantExpr::create(0, Context));
1051 return;
1052 }
1053
1054 SmallVector<char, 8> Data;
1056 F.setVarContents(Data);
1057 F.clearVarFixups();
1058}
1059
1060void MCAssembler::relaxSFrameFragment(MCFragment &F) {
1061 assert(F.getKind() == MCFragment::FT_SFrame);
1062 MCContext &C = getContext();
1063 int64_t Value;
1064 bool Abs = F.getSFrameAddrDelta().evaluateAsAbsolute(Value, *this);
1065 if (!Abs) {
1066 C.reportError(F.getSFrameAddrDelta().getLoc(),
1067 "invalid CFI advance_loc expression in sframe");
1068 F.setSFrameAddrDelta(MCConstantExpr::create(0, C));
1069 return;
1070 }
1071
1073 MCSFrameEmitter::encodeFuncOffset(Context, Value, Data, F.getSFrameFDE());
1074 F.setVarContents(Data);
1075 F.clearVarFixups();
1076}
1077
1078void MCAssembler::relaxFragment(MCFragment &F) {
1079 switch (F.getKind()) {
1080 default:
1081 return;
1083 relaxAlign(F);
1084 break;
1086 // Bundling emits every instruction as relaxable, so FT_Relaxable is
1087 // expected with RelaxAll mode once bundling is enabled.
1089 "Did not expect a FT_Relaxable in RelaxAll mode");
1090 relaxInstruction(F);
1091 break;
1092 case MCFragment::FT_LEB:
1093 relaxLEB(F);
1094 break;
1096 relaxDwarfLineAddr(F);
1097 break;
1099 relaxDwarfCallFrameFragment(F);
1100 break;
1102 relaxSFrameFragment(F);
1103 break;
1105 relaxBoundaryAlign(static_cast<MCBoundaryAlignFragment &>(F));
1106 break;
1108 relaxPrefAlign(F);
1109 break;
1112 *this, static_cast<MCCVInlineLineTableFragment &>(F));
1113 break;
1116 *this, static_cast<MCCVDefRangeFragment &>(F));
1117 break;
1118 }
1119}
1120
1121void MCAssembler::layoutSection(MCSection &Sec) {
1122 uint64_t Offset = 0;
1123 for (MCFragment &F : Sec) {
1124 F.Offset = Offset;
1125 if (F.getKind() == MCFragment::FT_Align)
1126 relaxAlign(F);
1128 }
1129}
1130
1131// Fused relaxation and layout: a single forward pass that updates each
1132// fragment's offset before processing it, so upstream size changes are
1133// immediately visible.
1134unsigned MCAssembler::relaxOnce(unsigned FirstStable) {
1135 uint64_t MaxIterations = 0;
1136 PendingErrors.clear();
1137 unsigned Res = 0;
1138 for (unsigned I = 0; I != FirstStable; ++I) {
1139 auto &Sec = *Sections[I];
1140 uint64_t Iters = 0;
1141 for (;;) {
1142 bool Changed = false;
1143 uint64_t Offset = 0;
1144 for (MCFragment &F : Sec) {
1145 if (F.Offset != Offset)
1146 Changed = true;
1147 Stretch = Offset - F.Offset;
1148 F.Offset = Offset;
1149 if (F.getKind() != MCFragment::FT_Data)
1150 relaxFragment(F);
1152 }
1153 ++Iters;
1154
1155 if (!Changed)
1156 break;
1157 // If any fragment changed size, it might impact the layout of subsequent
1158 // sections. Therefore, we must re-evaluate all sections.
1159 FirstStable = Sections.size();
1160 Res = I;
1161 // Assume each iteration finalizes at least one extra fragment. If the
1162 // layout does not converge after N+1 iterations, bail out.
1163 if (Iters > Sec.curFragList()->Tail->getLayoutOrder())
1164 break;
1165 }
1166 MaxIterations = std::max(MaxIterations, Iters);
1167 }
1168 stats::RelaxationSteps += MaxIterations;
1169 Stretch = 0;
1170 // The subsequent relaxOnce call only needs to visit Sections [0,Res) if no
1171 // change occurred.
1172 return Res;
1173}
1174
1177}
1178
1180 PendingErrors.emplace_back(Loc, Msg.str());
1181}
1182
1184 for (auto &Err : PendingErrors)
1185 reportError(Err.first, Err.second);
1186 PendingErrors.clear();
1187}
1188
1189#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1191 raw_ostream &OS = errs();
1193 // Scan symbols and build a map of fragments to their corresponding symbols.
1194 // For variable symbols, we don't want to call their getFragment, which might
1195 // modify `Fragment`.
1196 for (const MCSymbol &Sym : symbols())
1197 if (!Sym.isVariable())
1198 if (auto *F = Sym.getFragment())
1199 FragToSyms.try_emplace(F).first->second.push_back(&Sym);
1200
1201 OS << "Sections:[";
1202 for (const MCSection &Sec : *this) {
1203 OS << '\n';
1204 Sec.dump(&FragToSyms);
1205 }
1206 OS << "\n]\n";
1207}
1208#endif
1209
1211 if (auto *E = getValue())
1212 return E->getLoc();
1213 return {};
1214}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:678
#define _
static void writeControlledNops(raw_ostream &OS, const MCAssembler &Asm, uint64_t NumBytes, uint64_t Offset, uint64_t MaxNopSize, const MCSubtargetInfo *STI)
Write NumBytes of NOPs at Offset in chunks of at most MaxNopSize.
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 uint64_t computeBoundaryAlignSize(const MCAssembler &Asm, const MCBoundaryAlignFragment &BF)
Compute the padding size to boundary-align the fragments BF is responsible for.
static bool getLabelOffset(const MCAssembler &Asm, const MCSymbol &S, bool ReportError, uint64_t &Val)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
PowerPC TLS Dynamic Call Fixup
if(PassOpts->AAPipeline)
const char * Msg
This file defines the SmallVector class.
static Split data
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:171
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
LLVM_ABI void encodeInlineLineTable(const MCAssembler &Asm, MCCVInlineLineTableFragment &F)
Encodes the binary annotations once we have a layout.
LLVM_ABI void encodeDefRange(const MCAssembler &Asm, MCCVDefRangeFragment &F)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:299
virtual void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const
Relax the instruction in the given fragment to the next wider instruction.
virtual bool relaxAlign(MCFragment &F, unsigned &Size)
virtual std::pair< bool, bool > relaxLEB128(MCFragment &, int64_t &Value) const
virtual bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &, const MCValue &, uint64_t, bool Resolved) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
virtual void reset()
lifetime management
virtual void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved)=0
MCContext & getContext() const
LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
LLVM_ABI uint64_t getSectionAddressSize(const MCSection &Sec) const
LLVM_ABI void Finish()
Finish - Do final processing and write the object to the output stream.
bool isBundlingEnabled() const
LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const
LLVM_ABI void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
iterator_range< pointee_iterator< SmallVector< const MCSymbol *, 0 >::const_iterator > > symbols() const
LLVM_ABI void dump() const
LLVM_ABI void layout()
MCObjectWriter & getWriter() const
MCCodeEmitter * getEmitterPtr() const
LLVM_ABI void addRelocDirective(RelocDirective RD)
bool getRelaxAll() const
MCCodeEmitter & getEmitter() const
LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const
LLVM_ABI MCAssembler(MCContext &Context, std::unique_ptr< MCAsmBackend > Backend, std::unique_ptr< MCCodeEmitter > Emitter, std::unique_ptr< MCObjectWriter > Writer)
Construct a new assembler instance.
LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
MCAsmBackend & getBackend() const
LLVM_ABI bool registerSection(MCSection &Section)
LLVM_ABI void flushPendingErrors() const
LLVM_ABI uint64_t computeFragmentSize(const MCFragment &F) const
Compute the effective fragment size.
LLVM_ABI const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
MCAsmBackend * getBackendPtr() const
LLVM_ABI uint64_t getSectionFileSize(const MCSection &Sec) const
LLVM_ABI void reset()
Reuse an assembler instance.
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
uint64_t getFragmentOffset(const MCFragment &F) const
MCDwarfLineTableParams getDWARFLinetableParams() const
Represents required padding such that a particular other set of fragments does not cross a particular...
Definition MCSection.h:539
void setSize(uint64_t Value)
Definition MCSection.h:559
const MCFragment * getLastFragment() const
Definition MCSection.h:567
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.
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI CodeViewContext & getCVContext()
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
static LLVM_ABI void encodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, SmallVectorImpl< char > &OS)
Definition MCDwarf.cpp:2238
static LLVM_ABI 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:744
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
LLVM_ABI 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:453
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Definition MCExpr.cpp:450
SMLoc getLoc() const
Definition MCExpr.h:86
uint8_t getValueSize() const
Definition MCSection.h:405
uint64_t getValue() const
Definition MCSection.h:404
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
const MCExpr * getValue() const
Definition MCFixup.h:101
LLVM_ABI SMLoc getLoc() const
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:86
unsigned getLayoutOrder() const
Definition MCSection.h:186
MCSection * getParent() const
Definition MCSection.h:181
MCFragment * getNext() const
Definition MCSection.h:177
const MCSubtargetInfo * getSubtargetInfo() const
Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
Definition MCSection.h:197
int64_t getControlledNopLength() const
Definition MCSection.h:433
int64_t getNumBytes() const
Definition MCSection.h:432
SMLoc getLoc() const
Definition MCSection.h:435
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const
bool getSubsectionsViaSymbols() const
virtual void executePostLayoutBinding()
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
virtual uint64_t writeObject()=0
Write the object file and returns the number of bytes written.
static LLVM_ABI void encodeFuncOffset(MCContext &C, uint64_t Offset, SmallVectorImpl< char > &Out, MCFragment *FDEFrag)
Definition MCSFrame.cpp:618
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:580
bool isBssSection() const
Check whether this section is "virtual", that is has no actual object file contents.
Definition MCSection.h:697
void dump(DenseMap< const MCFragment *, SmallVector< const MCSymbol *, 0 > > *FragToSyms=nullptr) const
Definition MCSection.cpp:36
void setOrdinal(unsigned Value)
Definition MCSection.h:674
FragList * curFragList() const
Definition MCSection.h:688
Generic base class for all target subtargets.
Represents a symbol table index fragment.
Definition MCSection.h:467
const MCSymbol * getSymbol() const
Definition MCSection.h:473
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
bool isCommon() const
Is this a 'common' symbol.
Definition MCSymbol.h:343
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition MCSymbol.h:267
uint32_t getIndex() const
Get the (implementation defined) index.
Definition MCSymbol.h:280
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Definition MCSymbol.h:270
MCFragment * getFragment() const
Definition MCSymbol.h:345
uint64_t getOffset() const
Definition MCSymbol.h:289
const MCSymbol * getAddSym() const
Definition MCValue.h:49
int64_t getConstant() const
Definition MCValue.h:44
const MCSymbol * getSubSym() const
Definition MCValue.h:51
Represents a location in source code.
Definition SMLoc.h:22
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool isRelocRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:135
@ Resolved
Queried, materialization begun.
Definition Core.h:549
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:96
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
T bit_ceil(T Value)
Returns the smallest integral power of two no smaller than Value if Value is nonzero.
Definition bit.h:362
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:173
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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:186
LLVM_ABI 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.
Definition ModRef.h:32
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
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:1917
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:24
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
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:79
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
endianness
Definition bit.h:71
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77