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.
44 class MCSubtargetInfo;
48 class MCFragment : public ilist_node<MCFragment> {
49 friend class MCAsmLayout;
51 MCFragment(const MCFragment &) = delete;
52 void operator=(const MCFragment &) = delete;
58 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 // FIXME: This really doesn't belong here. See comments below.
536 struct IndirectSymbolData {
538 MCSectionData *SectionData;
541 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
543 struct DataRegionData {
544 // This enum should be kept in sync w/ the mach-o definition in
545 // llvm/Object/MachOFormat.h.
546 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
552 friend class MCAsmLayout;
555 typedef SetVector<MCSection *> SectionListType;
556 typedef std::vector<const MCSymbol *> SymbolDataListType;
558 typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
559 typedef pointee_iterator<SectionListType::iterator> iterator;
561 typedef pointee_iterator<SymbolDataListType::const_iterator>
562 const_symbol_iterator;
563 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
565 typedef iterator_range<symbol_iterator> symbol_range;
566 typedef iterator_range<const_symbol_iterator> const_symbol_range;
568 typedef std::vector<std::string> FileNameVectorType;
569 typedef FileNameVectorType::const_iterator const_file_name_iterator;
571 typedef std::vector<IndirectSymbolData>::const_iterator
572 const_indirect_symbol_iterator;
573 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
575 typedef std::vector<DataRegionData>::const_iterator
576 const_data_region_iterator;
577 typedef std::vector<DataRegionData>::iterator data_region_iterator;
579 /// MachO specific deployment target version info.
580 // A Major version of 0 indicates that no version information was supplied
581 // and so the corresponding load command should not be emitted.
583 MCVersionMinType Kind;
587 } VersionMinInfoType;
590 MCAssembler(const MCAssembler &) = delete;
591 void operator=(const MCAssembler &) = delete;
595 MCAsmBackend &Backend;
597 MCCodeEmitter &Emitter;
599 MCObjectWriter &Writer;
603 SectionListType Sections;
605 SymbolDataListType Symbols;
607 DenseSet<const MCSymbol *> LocalsUsedInReloc;
609 std::vector<IndirectSymbolData> IndirectSymbols;
611 std::vector<DataRegionData> DataRegions;
613 /// The list of linker options to propagate into the object file.
614 std::vector<std::vector<std::string>> LinkerOptions;
616 /// List of declared file names
617 FileNameVectorType FileNames;
619 /// The set of function symbols for which a .thumb_func directive has
622 // FIXME: We really would like this in target specific code rather than
623 // here. Maybe when the relocation stuff moves to target specific,
624 // this can go with it? The streamer would need some target specific
626 mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
628 /// \brief The bundle alignment size currently set in the assembler.
630 /// By default it's 0, which means bundling is disabled.
631 unsigned BundleAlignSize;
633 unsigned RelaxAll : 1;
634 unsigned SubsectionsViaSymbols : 1;
636 /// ELF specific e_header flags
637 // It would be good if there were an MCELFAssembler class to hold this.
638 // ELF header flags are used both by the integrated and standalone assemblers.
639 // Access to the flags is necessary in cases where assembler directives affect
640 // which flags to be set.
641 unsigned ELFHeaderEFlags;
643 /// Used to communicate Linker Optimization Hint information between
644 /// the Streamer and the .o writer
645 MCLOHContainer LOHContainer;
647 VersionMinInfoType VersionMinInfo;
650 /// Evaluate a fixup to a relocatable expression and the value which should be
651 /// placed into the fixup.
653 /// \param Layout The layout to use for evaluation.
654 /// \param Fixup The fixup to evaluate.
655 /// \param DF The fragment the fixup is inside.
656 /// \param Target [out] On return, the relocatable expression the fixup
658 /// \param Value [out] On return, the value of the fixup as currently laid
660 /// \return Whether the fixup value was fully resolved. This is true if the
661 /// \p Value result is fixed, otherwise the value may change due to
663 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
664 const MCFragment *DF, MCValue &Target,
665 uint64_t &Value) const;
667 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
668 /// (increased in size, in order to hold its value correctly).
669 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
670 const MCAsmLayout &Layout) const;
672 /// Check whether the given fragment needs relaxation.
673 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
674 const MCAsmLayout &Layout) const;
676 /// \brief Perform one layout iteration and return true if any offsets
678 bool layoutOnce(MCAsmLayout &Layout);
680 /// \brief Perform one layout iteration of the given section and return true
681 /// if any offsets were adjusted.
682 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
684 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
686 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
688 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
689 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
690 MCDwarfCallFrameFragment &DF);
692 /// finishLayout - Finalize a layout, including fragment lowering.
693 void finishLayout(MCAsmLayout &Layout);
695 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
696 MCFragment &F, const MCFixup &Fixup);
699 void addLocalUsedInReloc(const MCSymbol &Sym);
700 bool isLocalUsedInReloc(const MCSymbol &Sym) const;
702 /// Compute the effective fragment size assuming it is laid out at the given
703 /// \p SectionAddress and \p FragmentOffset.
704 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
705 const MCFragment &F) const;
707 /// Find the symbol which defines the atom containing the given symbol, or
708 /// null if there is no such symbol.
709 const MCSymbol *getAtom(const MCSymbol &S) const;
711 /// Check whether a particular symbol is visible to the linker and is required
712 /// in the symbol table, or whether it can be discarded by the assembler. This
713 /// also effects whether the assembler treats the label as potentially
714 /// defining a separate atom.
715 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
717 /// Emit the section contents using the given object writer.
718 void writeSectionData(const MCSectionData *Section,
719 const MCAsmLayout &Layout) const;
721 /// Check whether a given symbol has been flagged with .thumb_func.
722 bool isThumbFunc(const MCSymbol *Func) const;
724 /// Flag a function symbol as the target of a .thumb_func directive.
725 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
727 /// ELF e_header flags
728 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
729 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
731 /// MachO deployment target version information.
732 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
733 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
735 VersionMinInfo.Kind = Kind;
736 VersionMinInfo.Major = Major;
737 VersionMinInfo.Minor = Minor;
738 VersionMinInfo.Update = Update;
742 /// Construct a new assembler instance.
744 /// \param OS The stream to output to.
746 // FIXME: How are we going to parameterize this? Two obvious options are stay
747 // concrete and require clients to pass in a target like object. The other
748 // option is to make this abstract, and have targets provide concrete
749 // implementations as we do with AsmParser.
750 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
751 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
755 /// Reuse an assembler instance
759 MCContext &getContext() const { return Context; }
761 MCAsmBackend &getBackend() const { return Backend; }
763 MCCodeEmitter &getEmitter() const { return Emitter; }
765 MCObjectWriter &getWriter() const { return Writer; }
767 /// Finish - Do final processing and write the object to the output stream.
768 /// \p Writer is used for custom object writer (as the MCJIT does),
769 /// if not specified it is automatically created from backend.
772 // FIXME: This does not belong here.
773 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
774 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
776 bool getRelaxAll() const { return RelaxAll; }
777 void setRelaxAll(bool Value) { RelaxAll = Value; }
779 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
781 unsigned getBundleAlignSize() const { return BundleAlignSize; }
783 void setBundleAlignSize(unsigned Size) {
784 assert((Size == 0 || !(Size & (Size - 1))) &&
785 "Expect a power-of-two bundle align size");
786 BundleAlignSize = Size;
789 /// \name Section List Access
792 iterator begin() { return Sections.begin(); }
793 const_iterator begin() const { return Sections.begin(); }
795 iterator end() { return Sections.end(); }
796 const_iterator end() const { return Sections.end(); }
798 size_t size() const { return Sections.size(); }
801 /// \name Symbol List Access
803 symbol_iterator symbol_begin() { return Symbols.begin(); }
804 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
806 symbol_iterator symbol_end() { return Symbols.end(); }
807 const_symbol_iterator symbol_end() const { return Symbols.end(); }
809 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
810 const_symbol_range symbols() const {
811 return make_range(symbol_begin(), symbol_end());
814 size_t symbol_size() const { return Symbols.size(); }
817 /// \name Indirect Symbol List Access
820 // FIXME: This is a total hack, this should not be here. Once things are
821 // factored so that the streamer has direct access to the .o writer, it can
823 std::vector<IndirectSymbolData> &getIndirectSymbols() {
824 return IndirectSymbols;
827 indirect_symbol_iterator indirect_symbol_begin() {
828 return IndirectSymbols.begin();
830 const_indirect_symbol_iterator indirect_symbol_begin() const {
831 return IndirectSymbols.begin();
834 indirect_symbol_iterator indirect_symbol_end() {
835 return IndirectSymbols.end();
837 const_indirect_symbol_iterator indirect_symbol_end() const {
838 return IndirectSymbols.end();
841 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
844 /// \name Linker Option List Access
847 std::vector<std::vector<std::string>> &getLinkerOptions() {
848 return LinkerOptions;
852 /// \name Data Region List Access
855 // FIXME: This is a total hack, this should not be here. Once things are
856 // factored so that the streamer has direct access to the .o writer, it can
858 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
860 data_region_iterator data_region_begin() { return DataRegions.begin(); }
861 const_data_region_iterator data_region_begin() const {
862 return DataRegions.begin();
865 data_region_iterator data_region_end() { return DataRegions.end(); }
866 const_data_region_iterator data_region_end() const {
867 return DataRegions.end();
870 size_t data_region_size() const { return DataRegions.size(); }
873 /// \name Data Region List Access
876 // FIXME: This is a total hack, this should not be here. Once things are
877 // factored so that the streamer has direct access to the .o writer, it can
879 MCLOHContainer &getLOHContainer() { return LOHContainer; }
880 const MCLOHContainer &getLOHContainer() const {
881 return const_cast<MCAssembler *>(this)->getLOHContainer();
884 /// \name Backend Data Access
887 MCSectionData &getSectionData(MCSection &Section) {
888 assert(Sections.count(&Section) && "Unknown Seciton");
889 return Section.getSectionData();
892 const MCSectionData &getSectionData(const MCSection &Section) const {
893 return const_cast<MCAssembler *>(this)
894 ->getSectionData(const_cast<MCSection &>(Section));
897 MCSectionData &getOrCreateSectionData(MCSection &Section,
898 bool *Created = nullptr) {
899 bool C = Sections.insert(&Section);
902 return Section.getSectionData();
905 bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }
907 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
908 return const_cast<MCSymbolData &>(
909 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
912 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
913 return Symbol.getData();
916 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
917 bool *Created = nullptr) {
919 *Created = !hasSymbolData(Symbol);
920 if (!hasSymbolData(Symbol)) {
921 Symbol.initializeData();
922 Symbols.push_back(&Symbol);
924 return Symbol.getData();
927 const_file_name_iterator file_names_begin() const {
928 return FileNames.begin();
931 const_file_name_iterator file_names_end() const { return FileNames.end(); }
933 void addFileName(StringRef FileName) {
934 if (std::find(file_names_begin(), file_names_end(), FileName) ==
936 FileNames.push_back(FileName);
939 /// \brief Write the necessary bundle padding to the given object writer.
940 /// Expects a fragment \p F containing instructions and its size \p FSize.
941 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
942 MCObjectWriter *OW) const;
949 /// \brief Compute the amount of padding required before the fragment \p F to
950 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
951 /// its section and \p FSize is the fragment's size.
952 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
953 uint64_t FOffset, uint64_t FSize);
955 } // end namespace llvm