1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/MC/MCDirectives.h"
22 #include "llvm/MC/MCFixup.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCLinkerOptimizationHint.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/DataTypes.h"
31 #include <vector> // FIXME: Shouldn't be needed.
43 class MCSubtargetInfo;
47 class MCFragment : public ilist_node<MCFragment> {
48 friend class MCAsmLayout;
50 MCFragment(const MCFragment &) = delete;
51 void operator=(const MCFragment &) = delete;
57 FT_CompactEncodedInst,
70 /// The data for the section this fragment is in.
73 /// Atom - The atom this fragment is in, as represented by it's defining
77 /// \name Assembler Backend Data
80 // FIXME: This could all be kept private to the assembler implementation.
82 /// Offset - The offset of this fragment in its section. This is ~0 until
86 /// LayoutOrder - The layout order of this fragment.
92 MCFragment(FragmentType Kind, MCSection *Parent = nullptr);
97 virtual ~MCFragment();
99 FragmentType getKind() const { return Kind; }
101 MCSection *getParent() const { return Parent; }
102 void setParent(MCSection *Value) { Parent = Value; }
104 const MCSymbol *getAtom() const { return Atom; }
105 void setAtom(const MCSymbol *Value) { Atom = Value; }
107 unsigned getLayoutOrder() const { return LayoutOrder; }
108 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
110 /// \brief Does this fragment have instructions emitted into it? By default
111 /// this is false, but specific fragment types may set it to true.
112 virtual bool hasInstructions() const { return false; }
114 /// \brief Should this fragment be placed at the end of an aligned bundle?
115 virtual bool alignToBundleEnd() const { return false; }
116 virtual void setAlignToBundleEnd(bool V) {}
118 /// \brief Get the padding size that must be inserted before this fragment.
119 /// Used for bundling. By default, no padding is inserted.
120 /// Note that padding size is restricted to 8 bits. This is an optimization
121 /// to reduce the amount of space used for each fragment. In practice, larger
122 /// padding should never be required.
123 virtual uint8_t getBundlePadding() const { return 0; }
125 /// \brief Set the padding size for this fragment. By default it's a no-op,
126 /// and only some fragments have a meaningful implementation.
127 virtual void setBundlePadding(uint8_t N) {}
132 /// Interface implemented by fragments that contain encoded instructions and/or
135 class MCEncodedFragment : public MCFragment {
136 virtual void anchor();
138 uint8_t BundlePadding;
141 MCEncodedFragment(MCFragment::FragmentType FType, MCSection *Sec = nullptr)
142 : MCFragment(FType, Sec), BundlePadding(0) {}
143 ~MCEncodedFragment() override;
145 virtual SmallVectorImpl<char> &getContents() = 0;
146 virtual const SmallVectorImpl<char> &getContents() const = 0;
148 uint8_t getBundlePadding() const override { return BundlePadding; }
150 void setBundlePadding(uint8_t N) override { BundlePadding = N; }
152 static bool classof(const MCFragment *F) {
153 MCFragment::FragmentType Kind = F->getKind();
157 case MCFragment::FT_Relaxable:
158 case MCFragment::FT_CompactEncodedInst:
159 case MCFragment::FT_Data:
165 /// Interface implemented by fragments that contain encoded instructions and/or
166 /// data and also have fixups registered.
168 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
169 void anchor() override;
172 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
173 MCSection *Sec = nullptr)
174 : MCEncodedFragment(FType, Sec) {}
176 ~MCEncodedFragmentWithFixups() override;
178 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
179 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
181 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
182 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
184 virtual fixup_iterator fixup_begin() = 0;
185 virtual const_fixup_iterator fixup_begin() const = 0;
186 virtual fixup_iterator fixup_end() = 0;
187 virtual const_fixup_iterator fixup_end() const = 0;
189 static bool classof(const MCFragment *F) {
190 MCFragment::FragmentType Kind = F->getKind();
191 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
195 /// Fragment for data and encoded instructions.
197 class MCDataFragment : public MCEncodedFragmentWithFixups {
198 void anchor() override;
200 /// \brief Does this fragment contain encoded instructions anywhere in it?
201 bool HasInstructions;
203 /// \brief Should this fragment be aligned to the end of a bundle?
204 bool AlignToBundleEnd;
206 SmallVector<char, 32> Contents;
208 /// Fixups - The list of fixups in this fragment.
209 SmallVector<MCFixup, 4> Fixups;
212 MCDataFragment(MCSection *Sec = nullptr)
213 : MCEncodedFragmentWithFixups(FT_Data, Sec), HasInstructions(false),
214 AlignToBundleEnd(false) {}
216 SmallVectorImpl<char> &getContents() override { return Contents; }
217 const SmallVectorImpl<char> &getContents() const override { return Contents; }
219 SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
221 const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
223 bool hasInstructions() const override { return HasInstructions; }
224 virtual void setHasInstructions(bool V) { HasInstructions = V; }
226 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
227 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
229 fixup_iterator fixup_begin() override { return Fixups.begin(); }
230 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
232 fixup_iterator fixup_end() override { return Fixups.end(); }
233 const_fixup_iterator fixup_end() const override { return Fixups.end(); }
235 static bool classof(const MCFragment *F) {
236 return F->getKind() == MCFragment::FT_Data;
240 /// This is a compact (memory-size-wise) fragment for holding an encoded
241 /// instruction (non-relaxable) that has no fixups registered. When applicable,
242 /// it can be used instead of MCDataFragment and lead to lower memory
245 class MCCompactEncodedInstFragment : public MCEncodedFragment {
246 void anchor() override;
248 /// \brief Should this fragment be aligned to the end of a bundle?
249 bool AlignToBundleEnd;
251 SmallVector<char, 4> Contents;
254 MCCompactEncodedInstFragment(MCSection *Sec = nullptr)
255 : MCEncodedFragment(FT_CompactEncodedInst, Sec), AlignToBundleEnd(false) {
258 bool hasInstructions() const override { return true; }
260 SmallVectorImpl<char> &getContents() override { return Contents; }
261 const SmallVectorImpl<char> &getContents() const override { return Contents; }
263 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
264 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
266 static bool classof(const MCFragment *F) {
267 return F->getKind() == MCFragment::FT_CompactEncodedInst;
271 /// A relaxable fragment holds on to its MCInst, since it may need to be
272 /// relaxed during the assembler layout and relaxation stage.
274 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
275 void anchor() override;
277 /// Inst - The instruction this is a fragment for.
280 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
281 /// Keep a copy instead of a reference to make sure that updates to STI
282 /// in the assembler are not seen here.
283 const MCSubtargetInfo STI;
285 /// Contents - Binary data for the currently encoded instruction.
286 SmallVector<char, 8> Contents;
288 /// Fixups - The list of fixups in this fragment.
289 SmallVector<MCFixup, 1> Fixups;
292 MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
293 MCSection *Sec = nullptr)
294 : MCEncodedFragmentWithFixups(FT_Relaxable, Sec), Inst(Inst), STI(STI) {}
296 SmallVectorImpl<char> &getContents() override { return Contents; }
297 const SmallVectorImpl<char> &getContents() const override { return Contents; }
299 const MCInst &getInst() const { return Inst; }
300 void setInst(const MCInst &Value) { Inst = Value; }
302 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
304 SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
306 const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
308 bool hasInstructions() const override { return true; }
310 fixup_iterator fixup_begin() override { return Fixups.begin(); }
311 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
313 fixup_iterator fixup_end() override { return Fixups.end(); }
314 const_fixup_iterator fixup_end() const override { return Fixups.end(); }
316 static bool classof(const MCFragment *F) {
317 return F->getKind() == MCFragment::FT_Relaxable;
321 class MCAlignFragment : public MCFragment {
322 virtual void anchor();
324 /// Alignment - The alignment to ensure, in bytes.
327 /// Value - Value to use for filling padding bytes.
330 /// ValueSize - The size of the integer (in bytes) of \p Value.
333 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
334 /// cannot be satisfied in this width then this fragment is ignored.
335 unsigned MaxBytesToEmit;
337 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
338 /// of using the provided value. The exact interpretation of this flag is
339 /// target dependent.
343 MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
344 unsigned MaxBytesToEmit, MCSection *Sec = nullptr)
345 : MCFragment(FT_Align, Sec), Alignment(Alignment), Value(Value),
346 ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit), EmitNops(false) {}
351 unsigned getAlignment() const { return Alignment; }
353 int64_t getValue() const { return Value; }
355 unsigned getValueSize() const { return ValueSize; }
357 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
359 bool hasEmitNops() const { return EmitNops; }
360 void setEmitNops(bool Value) { EmitNops = Value; }
364 static bool classof(const MCFragment *F) {
365 return F->getKind() == MCFragment::FT_Align;
369 class MCFillFragment : public MCFragment {
370 virtual void anchor();
372 /// Value - Value to use for filling bytes.
375 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
376 /// this is a virtual fill fragment.
379 /// Size - The number of bytes to insert.
383 MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
384 MCSection *Sec = nullptr)
385 : MCFragment(FT_Fill, Sec), Value(Value), ValueSize(ValueSize),
387 assert((!ValueSize || (Size % ValueSize) == 0) &&
388 "Fill size must be a multiple of the value size!");
394 int64_t getValue() const { return Value; }
396 unsigned getValueSize() const { return ValueSize; }
398 uint64_t getSize() const { return Size; }
402 static bool classof(const MCFragment *F) {
403 return F->getKind() == MCFragment::FT_Fill;
407 class MCOrgFragment : public MCFragment {
408 virtual void anchor();
410 /// Offset - The offset this fragment should start at.
411 const MCExpr *Offset;
413 /// Value - Value to use for filling bytes.
417 MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSection *Sec = nullptr)
418 : MCFragment(FT_Org, Sec), Offset(&Offset), Value(Value) {}
423 const MCExpr &getOffset() const { return *Offset; }
425 uint8_t getValue() const { return Value; }
429 static bool classof(const MCFragment *F) {
430 return F->getKind() == MCFragment::FT_Org;
434 class MCLEBFragment : public MCFragment {
435 virtual void anchor();
437 /// Value - The value this fragment should contain.
440 /// IsSigned - True if this is a sleb128, false if uleb128.
443 SmallString<8> Contents;
446 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr)
447 : MCFragment(FT_LEB, Sec), Value(&Value_), IsSigned(IsSigned_) {
448 Contents.push_back(0);
454 const MCExpr &getValue() const { return *Value; }
456 bool isSigned() const { return IsSigned; }
458 SmallString<8> &getContents() { return Contents; }
459 const SmallString<8> &getContents() const { return Contents; }
463 static bool classof(const MCFragment *F) {
464 return F->getKind() == MCFragment::FT_LEB;
468 class MCDwarfLineAddrFragment : public MCFragment {
469 virtual void anchor();
471 /// LineDelta - the value of the difference between the two line numbers
472 /// between two .loc dwarf directives.
475 /// AddrDelta - The expression for the difference of the two symbols that
476 /// make up the address delta between two .loc dwarf directives.
477 const MCExpr *AddrDelta;
479 SmallString<8> Contents;
482 MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
483 MCSection *Sec = nullptr)
484 : MCFragment(FT_Dwarf, Sec), LineDelta(LineDelta), AddrDelta(&AddrDelta) {
485 Contents.push_back(0);
491 int64_t getLineDelta() const { return LineDelta; }
493 const MCExpr &getAddrDelta() const { return *AddrDelta; }
495 SmallString<8> &getContents() { return Contents; }
496 const SmallString<8> &getContents() const { return Contents; }
500 static bool classof(const MCFragment *F) {
501 return F->getKind() == MCFragment::FT_Dwarf;
505 class MCDwarfCallFrameFragment : public MCFragment {
506 virtual void anchor();
508 /// AddrDelta - The expression for the difference of the two symbols that
509 /// make up the address delta between two .cfi_* dwarf directives.
510 const MCExpr *AddrDelta;
512 SmallString<8> Contents;
515 MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr)
516 : MCFragment(FT_DwarfFrame, Sec), AddrDelta(&AddrDelta) {
517 Contents.push_back(0);
523 const MCExpr &getAddrDelta() const { return *AddrDelta; }
525 SmallString<8> &getContents() { return Contents; }
526 const SmallString<8> &getContents() const { return Contents; }
530 static bool classof(const MCFragment *F) {
531 return F->getKind() == MCFragment::FT_DwarfFrame;
535 class MCSafeSEHFragment : public MCFragment {
536 virtual void anchor();
541 MCSafeSEHFragment(const MCSymbol *Sym, MCSection *Sec = nullptr)
542 : MCFragment(FT_SafeSEH, Sec), Sym(Sym) {}
547 const MCSymbol *getSymbol() { return Sym; }
548 const MCSymbol *getSymbol() const { return Sym; }
552 static bool classof(const MCFragment *F) {
553 return F->getKind() == MCFragment::FT_SafeSEH;
557 // FIXME: This really doesn't belong here. See comments below.
558 struct IndirectSymbolData {
563 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
565 struct DataRegionData {
566 // This enum should be kept in sync w/ the mach-o definition in
567 // llvm/Object/MachOFormat.h.
568 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
574 friend class MCAsmLayout;
577 typedef SetVector<MCSection *> SectionListType;
578 typedef std::vector<const MCSymbol *> SymbolDataListType;
580 typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
581 typedef pointee_iterator<SectionListType::iterator> iterator;
583 typedef pointee_iterator<SymbolDataListType::const_iterator>
584 const_symbol_iterator;
585 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
587 typedef iterator_range<symbol_iterator> symbol_range;
588 typedef iterator_range<const_symbol_iterator> const_symbol_range;
590 typedef std::vector<IndirectSymbolData>::const_iterator
591 const_indirect_symbol_iterator;
592 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
594 typedef std::vector<DataRegionData>::const_iterator
595 const_data_region_iterator;
596 typedef std::vector<DataRegionData>::iterator data_region_iterator;
598 /// MachO specific deployment target version info.
599 // A Major version of 0 indicates that no version information was supplied
600 // and so the corresponding load command should not be emitted.
602 MCVersionMinType Kind;
606 } VersionMinInfoType;
609 MCAssembler(const MCAssembler &) = delete;
610 void operator=(const MCAssembler &) = delete;
614 MCAsmBackend &Backend;
616 MCCodeEmitter &Emitter;
618 MCObjectWriter &Writer;
622 SectionListType Sections;
624 SymbolDataListType Symbols;
626 DenseSet<const MCSymbol *> LocalsUsedInReloc;
628 std::vector<IndirectSymbolData> IndirectSymbols;
630 std::vector<DataRegionData> DataRegions;
632 /// The list of linker options to propagate into the object file.
633 std::vector<std::vector<std::string>> LinkerOptions;
635 /// List of declared file names
636 std::vector<std::string> FileNames;
638 /// The set of function symbols for which a .thumb_func directive has
641 // FIXME: We really would like this in target specific code rather than
642 // here. Maybe when the relocation stuff moves to target specific,
643 // this can go with it? The streamer would need some target specific
645 mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
647 /// \brief The bundle alignment size currently set in the assembler.
649 /// By default it's 0, which means bundling is disabled.
650 unsigned BundleAlignSize;
652 unsigned RelaxAll : 1;
653 unsigned SubsectionsViaSymbols : 1;
655 /// ELF specific e_header flags
656 // It would be good if there were an MCELFAssembler class to hold this.
657 // ELF header flags are used both by the integrated and standalone assemblers.
658 // Access to the flags is necessary in cases where assembler directives affect
659 // which flags to be set.
660 unsigned ELFHeaderEFlags;
662 /// Used to communicate Linker Optimization Hint information between
663 /// the Streamer and the .o writer
664 MCLOHContainer LOHContainer;
666 VersionMinInfoType VersionMinInfo;
669 /// Evaluate a fixup to a relocatable expression and the value which should be
670 /// placed into the fixup.
672 /// \param Layout The layout to use for evaluation.
673 /// \param Fixup The fixup to evaluate.
674 /// \param DF The fragment the fixup is inside.
675 /// \param Target [out] On return, the relocatable expression the fixup
677 /// \param Value [out] On return, the value of the fixup as currently laid
679 /// \return Whether the fixup value was fully resolved. This is true if the
680 /// \p Value result is fixed, otherwise the value may change due to
682 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
683 const MCFragment *DF, MCValue &Target,
684 uint64_t &Value) const;
686 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
687 /// (increased in size, in order to hold its value correctly).
688 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
689 const MCAsmLayout &Layout) const;
691 /// Check whether the given fragment needs relaxation.
692 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
693 const MCAsmLayout &Layout) const;
695 /// \brief Perform one layout iteration and return true if any offsets
697 bool layoutOnce(MCAsmLayout &Layout);
699 /// \brief Perform one layout iteration of the given section and return true
700 /// if any offsets were adjusted.
701 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
703 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
705 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
707 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
708 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
709 MCDwarfCallFrameFragment &DF);
711 /// finishLayout - Finalize a layout, including fragment lowering.
712 void finishLayout(MCAsmLayout &Layout);
714 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
715 MCFragment &F, const MCFixup &Fixup);
718 void addLocalUsedInReloc(const MCSymbol &Sym);
719 bool isLocalUsedInReloc(const MCSymbol &Sym) const;
721 /// Compute the effective fragment size assuming it is laid out at the given
722 /// \p SectionAddress and \p FragmentOffset.
723 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
724 const MCFragment &F) const;
726 /// Find the symbol which defines the atom containing the given symbol, or
727 /// null if there is no such symbol.
728 const MCSymbol *getAtom(const MCSymbol &S) const;
730 /// Check whether a particular symbol is visible to the linker and is required
731 /// in the symbol table, or whether it can be discarded by the assembler. This
732 /// also effects whether the assembler treats the label as potentially
733 /// defining a separate atom.
734 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
736 /// Emit the section contents using the given object writer.
737 void writeSectionData(const MCSection *Section,
738 const MCAsmLayout &Layout) const;
740 /// Check whether a given symbol has been flagged with .thumb_func.
741 bool isThumbFunc(const MCSymbol *Func) const;
743 /// Flag a function symbol as the target of a .thumb_func directive.
744 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
746 /// ELF e_header flags
747 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
748 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
750 /// MachO deployment target version information.
751 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
752 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
754 VersionMinInfo.Kind = Kind;
755 VersionMinInfo.Major = Major;
756 VersionMinInfo.Minor = Minor;
757 VersionMinInfo.Update = Update;
761 /// Construct a new assembler instance.
763 /// \param OS The stream to output to.
765 // FIXME: How are we going to parameterize this? Two obvious options are stay
766 // concrete and require clients to pass in a target like object. The other
767 // option is to make this abstract, and have targets provide concrete
768 // implementations as we do with AsmParser.
769 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
770 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
774 /// Reuse an assembler instance
778 MCContext &getContext() const { return Context; }
780 MCAsmBackend &getBackend() const { return Backend; }
782 MCCodeEmitter &getEmitter() const { return Emitter; }
784 MCObjectWriter &getWriter() const { return Writer; }
786 /// Finish - Do final processing and write the object to the output stream.
787 /// \p Writer is used for custom object writer (as the MCJIT does),
788 /// if not specified it is automatically created from backend.
791 // FIXME: This does not belong here.
792 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
793 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
795 bool getRelaxAll() const { return RelaxAll; }
796 void setRelaxAll(bool Value) { RelaxAll = Value; }
798 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
800 unsigned getBundleAlignSize() const { return BundleAlignSize; }
802 void setBundleAlignSize(unsigned Size) {
803 assert((Size == 0 || !(Size & (Size - 1))) &&
804 "Expect a power-of-two bundle align size");
805 BundleAlignSize = Size;
808 /// \name Section List Access
811 iterator begin() { return Sections.begin(); }
812 const_iterator begin() const { return Sections.begin(); }
814 iterator end() { return Sections.end(); }
815 const_iterator end() const { return Sections.end(); }
817 size_t size() const { return Sections.size(); }
820 /// \name Symbol List Access
822 symbol_iterator symbol_begin() { return Symbols.begin(); }
823 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
825 symbol_iterator symbol_end() { return Symbols.end(); }
826 const_symbol_iterator symbol_end() const { return Symbols.end(); }
828 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
829 const_symbol_range symbols() const {
830 return make_range(symbol_begin(), symbol_end());
833 size_t symbol_size() const { return Symbols.size(); }
836 /// \name Indirect Symbol List Access
839 // FIXME: This is a total hack, this should not be here. Once things are
840 // factored so that the streamer has direct access to the .o writer, it can
842 std::vector<IndirectSymbolData> &getIndirectSymbols() {
843 return IndirectSymbols;
846 indirect_symbol_iterator indirect_symbol_begin() {
847 return IndirectSymbols.begin();
849 const_indirect_symbol_iterator indirect_symbol_begin() const {
850 return IndirectSymbols.begin();
853 indirect_symbol_iterator indirect_symbol_end() {
854 return IndirectSymbols.end();
856 const_indirect_symbol_iterator indirect_symbol_end() const {
857 return IndirectSymbols.end();
860 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
863 /// \name Linker Option List Access
866 std::vector<std::vector<std::string>> &getLinkerOptions() {
867 return LinkerOptions;
871 /// \name Data Region List Access
874 // FIXME: This is a total hack, this should not be here. Once things are
875 // factored so that the streamer has direct access to the .o writer, it can
877 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
879 data_region_iterator data_region_begin() { return DataRegions.begin(); }
880 const_data_region_iterator data_region_begin() const {
881 return DataRegions.begin();
884 data_region_iterator data_region_end() { return DataRegions.end(); }
885 const_data_region_iterator data_region_end() const {
886 return DataRegions.end();
889 size_t data_region_size() const { return DataRegions.size(); }
892 /// \name Data Region List Access
895 // FIXME: This is a total hack, this should not be here. Once things are
896 // factored so that the streamer has direct access to the .o writer, it can
898 MCLOHContainer &getLOHContainer() { return LOHContainer; }
899 const MCLOHContainer &getLOHContainer() const {
900 return const_cast<MCAssembler *>(this)->getLOHContainer();
903 /// \name Backend Data Access
906 bool registerSection(MCSection &Section) { return Sections.insert(&Section); }
908 bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }
910 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr) {
912 *Created = !hasSymbolData(Symbol);
913 if (!hasSymbolData(Symbol)) {
914 Symbol.initializeData();
915 Symbols.push_back(&Symbol);
919 ArrayRef<std::string> getFileNames() { return FileNames; }
921 void addFileName(StringRef FileName) {
922 if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
924 FileNames.push_back(FileName);
927 /// \brief Write the necessary bundle padding to the given object writer.
928 /// Expects a fragment \p F containing instructions and its size \p FSize.
929 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
930 MCObjectWriter *OW) const;
937 /// \brief Compute the amount of padding required before the fragment \p F to
938 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
939 /// its section and \p FSize is the fragment's size.
940 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
941 uint64_t FOffset, uint64_t FSize);
943 } // end namespace llvm