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,
69 /// The data for the section this fragment is in.
72 /// Atom - The atom this fragment is in, as represented by it's defining
76 /// \name Assembler Backend Data
79 // FIXME: This could all be kept private to the assembler implementation.
81 /// Offset - The offset of this fragment in its section. This is ~0 until
85 /// LayoutOrder - The layout order of this fragment.
91 MCFragment(FragmentType Kind, MCSection *Parent = nullptr);
96 virtual ~MCFragment();
98 FragmentType getKind() const { return Kind; }
100 MCSection *getParent() const { return Parent; }
101 void setParent(MCSection *Value) { Parent = Value; }
103 const MCSymbol *getAtom() const { return Atom; }
104 void setAtom(const MCSymbol *Value) { Atom = Value; }
106 unsigned getLayoutOrder() const { return LayoutOrder; }
107 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
109 /// \brief Does this fragment have instructions emitted into it? By default
110 /// this is false, but specific fragment types may set it to true.
111 virtual bool hasInstructions() const { return false; }
113 /// \brief Should this fragment be placed at the end of an aligned bundle?
114 virtual bool alignToBundleEnd() const { return false; }
115 virtual void setAlignToBundleEnd(bool V) {}
117 /// \brief Get the padding size that must be inserted before this fragment.
118 /// Used for bundling. By default, no padding is inserted.
119 /// Note that padding size is restricted to 8 bits. This is an optimization
120 /// to reduce the amount of space used for each fragment. In practice, larger
121 /// padding should never be required.
122 virtual uint8_t getBundlePadding() const { return 0; }
124 /// \brief Set the padding size for this fragment. By default it's a no-op,
125 /// and only some fragments have a meaningful implementation.
126 virtual void setBundlePadding(uint8_t N) {}
131 /// Interface implemented by fragments that contain encoded instructions and/or
134 class MCEncodedFragment : public MCFragment {
135 virtual void anchor();
137 uint8_t BundlePadding;
140 MCEncodedFragment(MCFragment::FragmentType FType, MCSection *Sec = nullptr)
141 : MCFragment(FType, Sec), BundlePadding(0) {}
142 ~MCEncodedFragment() override;
144 virtual SmallVectorImpl<char> &getContents() = 0;
145 virtual const SmallVectorImpl<char> &getContents() const = 0;
147 uint8_t getBundlePadding() const override { return BundlePadding; }
149 void setBundlePadding(uint8_t N) override { BundlePadding = N; }
151 static bool classof(const MCFragment *F) {
152 MCFragment::FragmentType Kind = F->getKind();
156 case MCFragment::FT_Relaxable:
157 case MCFragment::FT_CompactEncodedInst:
158 case MCFragment::FT_Data:
164 /// Interface implemented by fragments that contain encoded instructions and/or
165 /// data and also have fixups registered.
167 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
168 void anchor() override;
171 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
172 MCSection *Sec = nullptr)
173 : MCEncodedFragment(FType, Sec) {}
175 ~MCEncodedFragmentWithFixups() override;
177 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
178 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
180 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
181 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
183 virtual fixup_iterator fixup_begin() = 0;
184 virtual const_fixup_iterator fixup_begin() const = 0;
185 virtual fixup_iterator fixup_end() = 0;
186 virtual const_fixup_iterator fixup_end() const = 0;
188 static bool classof(const MCFragment *F) {
189 MCFragment::FragmentType Kind = F->getKind();
190 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
194 /// Fragment for data and encoded instructions.
196 class MCDataFragment : public MCEncodedFragmentWithFixups {
197 void anchor() override;
199 /// \brief Does this fragment contain encoded instructions anywhere in it?
200 bool HasInstructions;
202 /// \brief Should this fragment be aligned to the end of a bundle?
203 bool AlignToBundleEnd;
205 SmallVector<char, 32> Contents;
207 /// Fixups - The list of fixups in this fragment.
208 SmallVector<MCFixup, 4> Fixups;
211 MCDataFragment(MCSection *Sec = nullptr)
212 : MCEncodedFragmentWithFixups(FT_Data, Sec), HasInstructions(false),
213 AlignToBundleEnd(false) {}
215 SmallVectorImpl<char> &getContents() override { return Contents; }
216 const SmallVectorImpl<char> &getContents() const override { return Contents; }
218 SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
220 const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
222 bool hasInstructions() const override { return HasInstructions; }
223 virtual void setHasInstructions(bool V) { HasInstructions = V; }
225 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
226 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
228 fixup_iterator fixup_begin() override { return Fixups.begin(); }
229 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
231 fixup_iterator fixup_end() override { return Fixups.end(); }
232 const_fixup_iterator fixup_end() const override { return Fixups.end(); }
234 static bool classof(const MCFragment *F) {
235 return F->getKind() == MCFragment::FT_Data;
239 /// This is a compact (memory-size-wise) fragment for holding an encoded
240 /// instruction (non-relaxable) that has no fixups registered. When applicable,
241 /// it can be used instead of MCDataFragment and lead to lower memory
244 class MCCompactEncodedInstFragment : public MCEncodedFragment {
245 void anchor() override;
247 /// \brief Should this fragment be aligned to the end of a bundle?
248 bool AlignToBundleEnd;
250 SmallVector<char, 4> Contents;
253 MCCompactEncodedInstFragment(MCSection *Sec = nullptr)
254 : MCEncodedFragment(FT_CompactEncodedInst, Sec), AlignToBundleEnd(false) {
257 bool hasInstructions() const override { return true; }
259 SmallVectorImpl<char> &getContents() override { return Contents; }
260 const SmallVectorImpl<char> &getContents() const override { return Contents; }
262 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
263 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
265 static bool classof(const MCFragment *F) {
266 return F->getKind() == MCFragment::FT_CompactEncodedInst;
270 /// A relaxable fragment holds on to its MCInst, since it may need to be
271 /// relaxed during the assembler layout and relaxation stage.
273 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
274 void anchor() override;
276 /// Inst - The instruction this is a fragment for.
279 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
280 /// Keep a copy instead of a reference to make sure that updates to STI
281 /// in the assembler are not seen here.
282 const MCSubtargetInfo STI;
284 /// Contents - Binary data for the currently encoded instruction.
285 SmallVector<char, 8> Contents;
287 /// Fixups - The list of fixups in this fragment.
288 SmallVector<MCFixup, 1> Fixups;
291 MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
292 MCSection *Sec = nullptr)
293 : MCEncodedFragmentWithFixups(FT_Relaxable, Sec), Inst(Inst), STI(STI) {}
295 SmallVectorImpl<char> &getContents() override { return Contents; }
296 const SmallVectorImpl<char> &getContents() const override { return Contents; }
298 const MCInst &getInst() const { return Inst; }
299 void setInst(const MCInst &Value) { Inst = Value; }
301 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
303 SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
305 const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
307 bool hasInstructions() const override { return true; }
309 fixup_iterator fixup_begin() override { return Fixups.begin(); }
310 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
312 fixup_iterator fixup_end() override { return Fixups.end(); }
313 const_fixup_iterator fixup_end() const override { return Fixups.end(); }
315 static bool classof(const MCFragment *F) {
316 return F->getKind() == MCFragment::FT_Relaxable;
320 class MCAlignFragment : public MCFragment {
321 virtual void anchor();
323 /// Alignment - The alignment to ensure, in bytes.
326 /// Value - Value to use for filling padding bytes.
329 /// ValueSize - The size of the integer (in bytes) of \p Value.
332 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
333 /// cannot be satisfied in this width then this fragment is ignored.
334 unsigned MaxBytesToEmit;
336 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
337 /// of using the provided value. The exact interpretation of this flag is
338 /// target dependent.
342 MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
343 unsigned MaxBytesToEmit, MCSection *Sec = nullptr)
344 : MCFragment(FT_Align, Sec), Alignment(Alignment), Value(Value),
345 ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit), EmitNops(false) {}
350 unsigned getAlignment() const { return Alignment; }
352 int64_t getValue() const { return Value; }
354 unsigned getValueSize() const { return ValueSize; }
356 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
358 bool hasEmitNops() const { return EmitNops; }
359 void setEmitNops(bool Value) { EmitNops = Value; }
363 static bool classof(const MCFragment *F) {
364 return F->getKind() == MCFragment::FT_Align;
368 class MCFillFragment : public MCFragment {
369 virtual void anchor();
371 /// Value - Value to use for filling bytes.
374 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
375 /// this is a virtual fill fragment.
378 /// Size - The number of bytes to insert.
382 MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
383 MCSection *Sec = nullptr)
384 : MCFragment(FT_Fill, Sec), Value(Value), ValueSize(ValueSize),
386 assert((!ValueSize || (Size % ValueSize) == 0) &&
387 "Fill size must be a multiple of the value size!");
393 int64_t getValue() const { return Value; }
395 unsigned getValueSize() const { return ValueSize; }
397 uint64_t getSize() const { return Size; }
401 static bool classof(const MCFragment *F) {
402 return F->getKind() == MCFragment::FT_Fill;
406 class MCOrgFragment : public MCFragment {
407 virtual void anchor();
409 /// Offset - The offset this fragment should start at.
410 const MCExpr *Offset;
412 /// Value - Value to use for filling bytes.
416 MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSection *Sec = nullptr)
417 : MCFragment(FT_Org, Sec), Offset(&Offset), Value(Value) {}
422 const MCExpr &getOffset() const { return *Offset; }
424 uint8_t getValue() const { return Value; }
428 static bool classof(const MCFragment *F) {
429 return F->getKind() == MCFragment::FT_Org;
433 class MCLEBFragment : public MCFragment {
434 virtual void anchor();
436 /// Value - The value this fragment should contain.
439 /// IsSigned - True if this is a sleb128, false if uleb128.
442 SmallString<8> Contents;
445 MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr)
446 : MCFragment(FT_LEB, Sec), Value(&Value_), IsSigned(IsSigned_) {
447 Contents.push_back(0);
453 const MCExpr &getValue() const { return *Value; }
455 bool isSigned() const { return IsSigned; }
457 SmallString<8> &getContents() { return Contents; }
458 const SmallString<8> &getContents() const { return Contents; }
462 static bool classof(const MCFragment *F) {
463 return F->getKind() == MCFragment::FT_LEB;
467 class MCDwarfLineAddrFragment : public MCFragment {
468 virtual void anchor();
470 /// LineDelta - the value of the difference between the two line numbers
471 /// between two .loc dwarf directives.
474 /// AddrDelta - The expression for the difference of the two symbols that
475 /// make up the address delta between two .loc dwarf directives.
476 const MCExpr *AddrDelta;
478 SmallString<8> Contents;
481 MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
482 MCSection *Sec = nullptr)
483 : MCFragment(FT_Dwarf, Sec), LineDelta(LineDelta), AddrDelta(&AddrDelta) {
484 Contents.push_back(0);
490 int64_t getLineDelta() const { return LineDelta; }
492 const MCExpr &getAddrDelta() const { return *AddrDelta; }
494 SmallString<8> &getContents() { return Contents; }
495 const SmallString<8> &getContents() const { return Contents; }
499 static bool classof(const MCFragment *F) {
500 return F->getKind() == MCFragment::FT_Dwarf;
504 class MCDwarfCallFrameFragment : public MCFragment {
505 virtual void anchor();
507 /// AddrDelta - The expression for the difference of the two symbols that
508 /// make up the address delta between two .cfi_* dwarf directives.
509 const MCExpr *AddrDelta;
511 SmallString<8> Contents;
514 MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr)
515 : MCFragment(FT_DwarfFrame, Sec), AddrDelta(&AddrDelta) {
516 Contents.push_back(0);
522 const MCExpr &getAddrDelta() const { return *AddrDelta; }
524 SmallString<8> &getContents() { return Contents; }
525 const SmallString<8> &getContents() const { return Contents; }
529 static bool classof(const MCFragment *F) {
530 return F->getKind() == MCFragment::FT_DwarfFrame;
534 // FIXME: This really doesn't belong here. See comments below.
535 struct IndirectSymbolData {
540 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
542 struct DataRegionData {
543 // This enum should be kept in sync w/ the mach-o definition in
544 // llvm/Object/MachOFormat.h.
545 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
551 friend class MCAsmLayout;
554 typedef SetVector<MCSection *> SectionListType;
555 typedef std::vector<const MCSymbol *> SymbolDataListType;
557 typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
558 typedef pointee_iterator<SectionListType::iterator> iterator;
560 typedef pointee_iterator<SymbolDataListType::const_iterator>
561 const_symbol_iterator;
562 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
564 typedef iterator_range<symbol_iterator> symbol_range;
565 typedef iterator_range<const_symbol_iterator> const_symbol_range;
567 typedef std::vector<IndirectSymbolData>::const_iterator
568 const_indirect_symbol_iterator;
569 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
571 typedef std::vector<DataRegionData>::const_iterator
572 const_data_region_iterator;
573 typedef std::vector<DataRegionData>::iterator data_region_iterator;
575 /// MachO specific deployment target version info.
576 // A Major version of 0 indicates that no version information was supplied
577 // and so the corresponding load command should not be emitted.
579 MCVersionMinType Kind;
583 } VersionMinInfoType;
586 MCAssembler(const MCAssembler &) = delete;
587 void operator=(const MCAssembler &) = delete;
591 MCAsmBackend &Backend;
593 MCCodeEmitter &Emitter;
595 MCObjectWriter &Writer;
599 SectionListType Sections;
601 SymbolDataListType Symbols;
603 DenseSet<const MCSymbol *> LocalsUsedInReloc;
605 std::vector<IndirectSymbolData> IndirectSymbols;
607 std::vector<DataRegionData> DataRegions;
609 /// The list of linker options to propagate into the object file.
610 std::vector<std::vector<std::string>> LinkerOptions;
612 /// List of declared file names
613 std::vector<std::string> FileNames;
615 /// The set of function symbols for which a .thumb_func directive has
618 // FIXME: We really would like this in target specific code rather than
619 // here. Maybe when the relocation stuff moves to target specific,
620 // this can go with it? The streamer would need some target specific
622 mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
624 /// \brief The bundle alignment size currently set in the assembler.
626 /// By default it's 0, which means bundling is disabled.
627 unsigned BundleAlignSize;
629 unsigned RelaxAll : 1;
630 unsigned SubsectionsViaSymbols : 1;
632 /// ELF specific e_header flags
633 // It would be good if there were an MCELFAssembler class to hold this.
634 // ELF header flags are used both by the integrated and standalone assemblers.
635 // Access to the flags is necessary in cases where assembler directives affect
636 // which flags to be set.
637 unsigned ELFHeaderEFlags;
639 /// Used to communicate Linker Optimization Hint information between
640 /// the Streamer and the .o writer
641 MCLOHContainer LOHContainer;
643 VersionMinInfoType VersionMinInfo;
646 /// Evaluate a fixup to a relocatable expression and the value which should be
647 /// placed into the fixup.
649 /// \param Layout The layout to use for evaluation.
650 /// \param Fixup The fixup to evaluate.
651 /// \param DF The fragment the fixup is inside.
652 /// \param Target [out] On return, the relocatable expression the fixup
654 /// \param Value [out] On return, the value of the fixup as currently laid
656 /// \return Whether the fixup value was fully resolved. This is true if the
657 /// \p Value result is fixed, otherwise the value may change due to
659 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
660 const MCFragment *DF, MCValue &Target,
661 uint64_t &Value) const;
663 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
664 /// (increased in size, in order to hold its value correctly).
665 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
666 const MCAsmLayout &Layout) const;
668 /// Check whether the given fragment needs relaxation.
669 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
670 const MCAsmLayout &Layout) const;
672 /// \brief Perform one layout iteration and return true if any offsets
674 bool layoutOnce(MCAsmLayout &Layout);
676 /// \brief Perform one layout iteration of the given section and return true
677 /// if any offsets were adjusted.
678 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
680 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
682 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
684 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
685 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
686 MCDwarfCallFrameFragment &DF);
688 /// finishLayout - Finalize a layout, including fragment lowering.
689 void finishLayout(MCAsmLayout &Layout);
691 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
692 MCFragment &F, const MCFixup &Fixup);
695 void addLocalUsedInReloc(const MCSymbol &Sym);
696 bool isLocalUsedInReloc(const MCSymbol &Sym) const;
698 /// Compute the effective fragment size assuming it is laid out at the given
699 /// \p SectionAddress and \p FragmentOffset.
700 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
701 const MCFragment &F) const;
703 /// Find the symbol which defines the atom containing the given symbol, or
704 /// null if there is no such symbol.
705 const MCSymbol *getAtom(const MCSymbol &S) const;
707 /// Check whether a particular symbol is visible to the linker and is required
708 /// in the symbol table, or whether it can be discarded by the assembler. This
709 /// also effects whether the assembler treats the label as potentially
710 /// defining a separate atom.
711 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
713 /// Emit the section contents using the given object writer.
714 void writeSectionData(const MCSection *Section,
715 const MCAsmLayout &Layout) const;
717 /// Check whether a given symbol has been flagged with .thumb_func.
718 bool isThumbFunc(const MCSymbol *Func) const;
720 /// Flag a function symbol as the target of a .thumb_func directive.
721 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
723 /// ELF e_header flags
724 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
725 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
727 /// MachO deployment target version information.
728 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
729 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
731 VersionMinInfo.Kind = Kind;
732 VersionMinInfo.Major = Major;
733 VersionMinInfo.Minor = Minor;
734 VersionMinInfo.Update = Update;
738 /// Construct a new assembler instance.
740 /// \param OS The stream to output to.
742 // FIXME: How are we going to parameterize this? Two obvious options are stay
743 // concrete and require clients to pass in a target like object. The other
744 // option is to make this abstract, and have targets provide concrete
745 // implementations as we do with AsmParser.
746 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
747 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
751 /// Reuse an assembler instance
755 MCContext &getContext() const { return Context; }
757 MCAsmBackend &getBackend() const { return Backend; }
759 MCCodeEmitter &getEmitter() const { return Emitter; }
761 MCObjectWriter &getWriter() const { return Writer; }
763 /// Finish - Do final processing and write the object to the output stream.
764 /// \p Writer is used for custom object writer (as the MCJIT does),
765 /// if not specified it is automatically created from backend.
768 // FIXME: This does not belong here.
769 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
770 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
772 bool getRelaxAll() const { return RelaxAll; }
773 void setRelaxAll(bool Value) { RelaxAll = Value; }
775 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
777 unsigned getBundleAlignSize() const { return BundleAlignSize; }
779 void setBundleAlignSize(unsigned Size) {
780 assert((Size == 0 || !(Size & (Size - 1))) &&
781 "Expect a power-of-two bundle align size");
782 BundleAlignSize = Size;
785 /// \name Section List Access
788 iterator begin() { return Sections.begin(); }
789 const_iterator begin() const { return Sections.begin(); }
791 iterator end() { return Sections.end(); }
792 const_iterator end() const { return Sections.end(); }
794 size_t size() const { return Sections.size(); }
797 /// \name Symbol List Access
799 symbol_iterator symbol_begin() { return Symbols.begin(); }
800 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
802 symbol_iterator symbol_end() { return Symbols.end(); }
803 const_symbol_iterator symbol_end() const { return Symbols.end(); }
805 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
806 const_symbol_range symbols() const {
807 return make_range(symbol_begin(), symbol_end());
810 size_t symbol_size() const { return Symbols.size(); }
813 /// \name Indirect Symbol List Access
816 // FIXME: This is a total hack, this should not be here. Once things are
817 // factored so that the streamer has direct access to the .o writer, it can
819 std::vector<IndirectSymbolData> &getIndirectSymbols() {
820 return IndirectSymbols;
823 indirect_symbol_iterator indirect_symbol_begin() {
824 return IndirectSymbols.begin();
826 const_indirect_symbol_iterator indirect_symbol_begin() const {
827 return IndirectSymbols.begin();
830 indirect_symbol_iterator indirect_symbol_end() {
831 return IndirectSymbols.end();
833 const_indirect_symbol_iterator indirect_symbol_end() const {
834 return IndirectSymbols.end();
837 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
840 /// \name Linker Option List Access
843 std::vector<std::vector<std::string>> &getLinkerOptions() {
844 return LinkerOptions;
848 /// \name Data Region List Access
851 // FIXME: This is a total hack, this should not be here. Once things are
852 // factored so that the streamer has direct access to the .o writer, it can
854 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
856 data_region_iterator data_region_begin() { return DataRegions.begin(); }
857 const_data_region_iterator data_region_begin() const {
858 return DataRegions.begin();
861 data_region_iterator data_region_end() { return DataRegions.end(); }
862 const_data_region_iterator data_region_end() const {
863 return DataRegions.end();
866 size_t data_region_size() const { return DataRegions.size(); }
869 /// \name Data Region List Access
872 // FIXME: This is a total hack, this should not be here. Once things are
873 // factored so that the streamer has direct access to the .o writer, it can
875 MCLOHContainer &getLOHContainer() { return LOHContainer; }
876 const MCLOHContainer &getLOHContainer() const {
877 return const_cast<MCAssembler *>(this)->getLOHContainer();
880 /// \name Backend Data Access
883 bool registerSection(MCSection &Section) { return Sections.insert(&Section); }
885 bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }
887 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
888 return const_cast<MCSymbolData &>(
889 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
892 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
893 return Symbol.getData();
896 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
897 bool *Created = nullptr) {
899 *Created = !hasSymbolData(Symbol);
900 if (!hasSymbolData(Symbol)) {
901 Symbol.initializeData();
902 Symbols.push_back(&Symbol);
904 return Symbol.getData();
907 ArrayRef<std::string> getFileNames() { return FileNames; }
909 void addFileName(StringRef FileName) {
910 if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
912 FileNames.push_back(FileName);
915 /// \brief Write the necessary bundle padding to the given object writer.
916 /// Expects a fragment \p F containing instructions and its size \p FSize.
917 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
918 MCObjectWriter *OW) const;
925 /// \brief Compute the amount of padding required before the fragment \p F to
926 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
927 /// its section and \p FSize is the fragment's size.
928 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
929 uint64_t FOffset, uint64_t FSize);
931 } // end namespace llvm