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/SmallPtrSet.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/ilist.h"
18 #include "llvm/ADT/ilist_node.h"
19 #include "llvm/ADT/iterator.h"
20 #include "llvm/MC/MCDirectives.h"
21 #include "llvm/MC/MCFixup.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/DataTypes.h"
29 #include <vector> // FIXME: Shouldn't be needed.
42 class MCSubtargetInfo;
46 class MCFragment : public ilist_node<MCFragment> {
47 friend class MCAsmLayout;
49 MCFragment(const MCFragment &) = delete;
50 void operator=(const MCFragment &) = delete;
56 FT_CompactEncodedInst,
68 /// Parent - The data for the section this fragment is in.
69 MCSectionData *Parent;
71 /// Atom - The atom this fragment is in, as represented by it's defining
75 /// \name Assembler Backend Data
78 // FIXME: This could all be kept private to the assembler implementation.
80 /// Offset - The offset of this fragment in its section. This is ~0 until
84 /// LayoutOrder - The layout order of this fragment.
90 MCFragment(FragmentType Kind, MCSectionData *Parent = nullptr);
95 virtual ~MCFragment();
97 FragmentType getKind() const { return Kind; }
99 MCSectionData *getParent() const { return Parent; }
100 void setParent(MCSectionData *Value) { Parent = Value; }
102 const MCSymbol *getAtom() const { return Atom; }
103 void setAtom(const MCSymbol *Value) { Atom = Value; }
105 unsigned getLayoutOrder() const { return LayoutOrder; }
106 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
108 /// \brief Does this fragment have instructions emitted into it? By default
109 /// this is false, but specific fragment types may set it to true.
110 virtual bool hasInstructions() const { return false; }
112 /// \brief Should this fragment be placed at the end of an aligned bundle?
113 virtual bool alignToBundleEnd() const { return false; }
114 virtual void setAlignToBundleEnd(bool V) {}
116 /// \brief Get the padding size that must be inserted before this fragment.
117 /// Used for bundling. By default, no padding is inserted.
118 /// Note that padding size is restricted to 8 bits. This is an optimization
119 /// to reduce the amount of space used for each fragment. In practice, larger
120 /// padding should never be required.
121 virtual uint8_t getBundlePadding() const { return 0; }
123 /// \brief Set the padding size for this fragment. By default it's a no-op,
124 /// and only some fragments have a meaningful implementation.
125 virtual void setBundlePadding(uint8_t N) {}
130 /// Interface implemented by fragments that contain encoded instructions and/or
133 class MCEncodedFragment : public MCFragment {
134 virtual void anchor();
136 uint8_t BundlePadding;
139 MCEncodedFragment(MCFragment::FragmentType FType, MCSectionData *SD = nullptr)
140 : MCFragment(FType, SD), BundlePadding(0) {}
141 ~MCEncodedFragment() override;
143 virtual SmallVectorImpl<char> &getContents() = 0;
144 virtual const SmallVectorImpl<char> &getContents() const = 0;
146 uint8_t getBundlePadding() const override { return BundlePadding; }
148 void setBundlePadding(uint8_t N) override { BundlePadding = N; }
150 static bool classof(const MCFragment *F) {
151 MCFragment::FragmentType Kind = F->getKind();
155 case MCFragment::FT_Relaxable:
156 case MCFragment::FT_CompactEncodedInst:
157 case MCFragment::FT_Data:
163 /// Interface implemented by fragments that contain encoded instructions and/or
164 /// data and also have fixups registered.
166 class MCEncodedFragmentWithFixups : public MCEncodedFragment {
167 void anchor() override;
170 MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
171 MCSectionData *SD = nullptr)
172 : MCEncodedFragment(FType, SD) {}
174 ~MCEncodedFragmentWithFixups() override;
176 typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
177 typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
179 virtual SmallVectorImpl<MCFixup> &getFixups() = 0;
180 virtual const SmallVectorImpl<MCFixup> &getFixups() const = 0;
182 virtual fixup_iterator fixup_begin() = 0;
183 virtual const_fixup_iterator fixup_begin() const = 0;
184 virtual fixup_iterator fixup_end() = 0;
185 virtual const_fixup_iterator fixup_end() const = 0;
187 static bool classof(const MCFragment *F) {
188 MCFragment::FragmentType Kind = F->getKind();
189 return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
193 /// Fragment for data and encoded instructions.
195 class MCDataFragment : public MCEncodedFragmentWithFixups {
196 void anchor() override;
198 /// \brief Does this fragment contain encoded instructions anywhere in it?
199 bool HasInstructions;
201 /// \brief Should this fragment be aligned to the end of a bundle?
202 bool AlignToBundleEnd;
204 SmallVector<char, 32> Contents;
206 /// Fixups - The list of fixups in this fragment.
207 SmallVector<MCFixup, 4> Fixups;
210 MCDataFragment(MCSectionData *SD = nullptr)
211 : MCEncodedFragmentWithFixups(FT_Data, SD), HasInstructions(false),
212 AlignToBundleEnd(false) {}
214 SmallVectorImpl<char> &getContents() override { return Contents; }
215 const SmallVectorImpl<char> &getContents() const override { return Contents; }
217 SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
219 const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
221 bool hasInstructions() const override { return HasInstructions; }
222 virtual void setHasInstructions(bool V) { HasInstructions = V; }
224 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
225 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
227 fixup_iterator fixup_begin() override { return Fixups.begin(); }
228 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
230 fixup_iterator fixup_end() override { return Fixups.end(); }
231 const_fixup_iterator fixup_end() const override { return Fixups.end(); }
233 static bool classof(const MCFragment *F) {
234 return F->getKind() == MCFragment::FT_Data;
238 /// This is a compact (memory-size-wise) fragment for holding an encoded
239 /// instruction (non-relaxable) that has no fixups registered. When applicable,
240 /// it can be used instead of MCDataFragment and lead to lower memory
243 class MCCompactEncodedInstFragment : public MCEncodedFragment {
244 void anchor() override;
246 /// \brief Should this fragment be aligned to the end of a bundle?
247 bool AlignToBundleEnd;
249 SmallVector<char, 4> Contents;
252 MCCompactEncodedInstFragment(MCSectionData *SD = nullptr)
253 : MCEncodedFragment(FT_CompactEncodedInst, SD), AlignToBundleEnd(false) {}
255 bool hasInstructions() const override { return true; }
257 SmallVectorImpl<char> &getContents() override { return Contents; }
258 const SmallVectorImpl<char> &getContents() const override { return Contents; }
260 bool alignToBundleEnd() const override { return AlignToBundleEnd; }
261 void setAlignToBundleEnd(bool V) override { AlignToBundleEnd = V; }
263 static bool classof(const MCFragment *F) {
264 return F->getKind() == MCFragment::FT_CompactEncodedInst;
268 /// A relaxable fragment holds on to its MCInst, since it may need to be
269 /// relaxed during the assembler layout and relaxation stage.
271 class MCRelaxableFragment : public MCEncodedFragmentWithFixups {
272 void anchor() override;
274 /// Inst - The instruction this is a fragment for.
277 /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
278 /// Keep a copy instead of a reference to make sure that updates to STI
279 /// in the assembler are not seen here.
280 const MCSubtargetInfo STI;
282 /// Contents - Binary data for the currently encoded instruction.
283 SmallVector<char, 8> Contents;
285 /// Fixups - The list of fixups in this fragment.
286 SmallVector<MCFixup, 1> Fixups;
289 MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
290 MCSectionData *SD = nullptr)
291 : MCEncodedFragmentWithFixups(FT_Relaxable, SD), Inst(Inst), STI(STI) {}
293 SmallVectorImpl<char> &getContents() override { return Contents; }
294 const SmallVectorImpl<char> &getContents() const override { return Contents; }
296 const MCInst &getInst() const { return Inst; }
297 void setInst(const MCInst &Value) { Inst = Value; }
299 const MCSubtargetInfo &getSubtargetInfo() { return STI; }
301 SmallVectorImpl<MCFixup> &getFixups() override { return Fixups; }
303 const SmallVectorImpl<MCFixup> &getFixups() const override { return Fixups; }
305 bool hasInstructions() const override { return true; }
307 fixup_iterator fixup_begin() override { return Fixups.begin(); }
308 const_fixup_iterator fixup_begin() const override { return Fixups.begin(); }
310 fixup_iterator fixup_end() override { return Fixups.end(); }
311 const_fixup_iterator fixup_end() const override { return Fixups.end(); }
313 static bool classof(const MCFragment *F) {
314 return F->getKind() == MCFragment::FT_Relaxable;
318 class MCAlignFragment : public MCFragment {
319 virtual void anchor();
321 /// Alignment - The alignment to ensure, in bytes.
324 /// Value - Value to use for filling padding bytes.
327 /// ValueSize - The size of the integer (in bytes) of \p Value.
330 /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
331 /// cannot be satisfied in this width then this fragment is ignored.
332 unsigned MaxBytesToEmit;
334 /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
335 /// of using the provided value. The exact interpretation of this flag is
336 /// target dependent.
340 MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
341 unsigned MaxBytesToEmit, MCSectionData *SD = nullptr)
342 : MCFragment(FT_Align, SD), Alignment(Alignment), Value(Value),
343 ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit), EmitNops(false) {}
348 unsigned getAlignment() const { return Alignment; }
350 int64_t getValue() const { return Value; }
352 unsigned getValueSize() const { return ValueSize; }
354 unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
356 bool hasEmitNops() const { return EmitNops; }
357 void setEmitNops(bool Value) { EmitNops = Value; }
361 static bool classof(const MCFragment *F) {
362 return F->getKind() == MCFragment::FT_Align;
366 class MCFillFragment : public MCFragment {
367 virtual void anchor();
369 /// Value - Value to use for filling bytes.
372 /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
373 /// this is a virtual fill fragment.
376 /// Size - The number of bytes to insert.
380 MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
381 MCSectionData *SD = nullptr)
382 : MCFragment(FT_Fill, SD), Value(Value), ValueSize(ValueSize),
384 assert((!ValueSize || (Size % ValueSize) == 0) &&
385 "Fill size must be a multiple of the value size!");
391 int64_t getValue() const { return Value; }
393 unsigned getValueSize() const { return ValueSize; }
395 uint64_t getSize() const { return Size; }
399 static bool classof(const MCFragment *F) {
400 return F->getKind() == MCFragment::FT_Fill;
404 class MCOrgFragment : public MCFragment {
405 virtual void anchor();
407 /// Offset - The offset this fragment should start at.
408 const MCExpr *Offset;
410 /// Value - Value to use for filling bytes.
414 MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSectionData *SD = nullptr)
415 : MCFragment(FT_Org, SD), Offset(&Offset), Value(Value) {}
420 const MCExpr &getOffset() const { return *Offset; }
422 uint8_t getValue() const { return Value; }
426 static bool classof(const MCFragment *F) {
427 return F->getKind() == MCFragment::FT_Org;
431 class MCLEBFragment : public MCFragment {
432 virtual void anchor();
434 /// Value - The value this fragment should contain.
437 /// IsSigned - True if this is a sleb128, false if uleb128.
440 SmallString<8> Contents;
443 MCLEBFragment(const MCExpr &Value_, bool IsSigned_,
444 MCSectionData *SD = nullptr)
445 : MCFragment(FT_LEB, SD), Value(&Value_), IsSigned(IsSigned_) {
446 Contents.push_back(0);
452 const MCExpr &getValue() const { return *Value; }
454 bool isSigned() const { return IsSigned; }
456 SmallString<8> &getContents() { return Contents; }
457 const SmallString<8> &getContents() const { return Contents; }
461 static bool classof(const MCFragment *F) {
462 return F->getKind() == MCFragment::FT_LEB;
466 class MCDwarfLineAddrFragment : public MCFragment {
467 virtual void anchor();
469 /// LineDelta - the value of the difference between the two line numbers
470 /// between two .loc dwarf directives.
473 /// AddrDelta - The expression for the difference of the two symbols that
474 /// make up the address delta between two .loc dwarf directives.
475 const MCExpr *AddrDelta;
477 SmallString<8> Contents;
480 MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
481 MCSectionData *SD = nullptr)
482 : MCFragment(FT_Dwarf, SD), LineDelta(LineDelta), AddrDelta(&AddrDelta) {
483 Contents.push_back(0);
489 int64_t getLineDelta() const { return LineDelta; }
491 const MCExpr &getAddrDelta() const { return *AddrDelta; }
493 SmallString<8> &getContents() { return Contents; }
494 const SmallString<8> &getContents() const { return Contents; }
498 static bool classof(const MCFragment *F) {
499 return F->getKind() == MCFragment::FT_Dwarf;
503 class MCDwarfCallFrameFragment : public MCFragment {
504 virtual void anchor();
506 /// AddrDelta - The expression for the difference of the two symbols that
507 /// make up the address delta between two .cfi_* dwarf directives.
508 const MCExpr *AddrDelta;
510 SmallString<8> Contents;
513 MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSectionData *SD = nullptr)
514 : MCFragment(FT_DwarfFrame, SD), AddrDelta(&AddrDelta) {
515 Contents.push_back(0);
521 const MCExpr &getAddrDelta() const { return *AddrDelta; }
523 SmallString<8> &getContents() { return Contents; }
524 const SmallString<8> &getContents() const { return Contents; }
528 static bool classof(const MCFragment *F) {
529 return F->getKind() == MCFragment::FT_DwarfFrame;
533 // FIXME: Should this be a separate class, or just merged into MCSection? Since
534 // we anticipate the fast path being through an MCAssembler, the only reason to
535 // keep it out is for API abstraction.
536 class MCSectionData : public ilist_node<MCSectionData> {
537 friend class MCAsmLayout;
539 MCSectionData(const MCSectionData &) = delete;
540 void operator=(const MCSectionData &) = delete;
543 typedef iplist<MCFragment> FragmentListType;
545 typedef FragmentListType::const_iterator const_iterator;
546 typedef FragmentListType::iterator iterator;
548 typedef FragmentListType::const_reverse_iterator const_reverse_iterator;
549 typedef FragmentListType::reverse_iterator reverse_iterator;
551 /// \brief Express the state of bundle locked groups while emitting code.
552 enum BundleLockStateType {
555 BundleLockedAlignToEnd
559 FragmentListType Fragments;
562 /// LayoutOrder - The index of this section in the layout order.
563 unsigned LayoutOrder;
565 /// \brief Keeping track of bundle-locked state.
566 BundleLockStateType BundleLockState;
568 /// \brief Current nesting depth of bundle_lock directives.
569 unsigned BundleLockNestingDepth;
571 /// \brief We've seen a bundle_lock directive but not its first instruction
573 bool BundleGroupBeforeFirstInst;
575 /// \name Assembler Backend Data
578 // FIXME: This could all be kept private to the assembler implementation.
580 /// HasInstructions - Whether this section has had instructions emitted into
582 unsigned HasInstructions : 1;
584 /// Mapping from subsection number to insertion point for subsection numbers
585 /// below that number.
586 SmallVector<std::pair<unsigned, MCFragment *>, 1> SubsectionFragmentMap;
591 // Only for use as sentinel.
593 MCSectionData(MCSection &Section, MCAssembler *A = nullptr);
595 MCSection &getSection() const { return *Section; }
597 bool hasInstructions() const { return HasInstructions; }
598 void setHasInstructions(bool Value) { HasInstructions = Value; }
600 unsigned getLayoutOrder() const { return LayoutOrder; }
601 void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
603 /// \name Fragment Access
606 const FragmentListType &getFragmentList() const { return Fragments; }
607 FragmentListType &getFragmentList() { return Fragments; }
609 iterator begin() { return Fragments.begin(); }
610 const_iterator begin() const { return Fragments.begin(); }
612 iterator end() { return Fragments.end(); }
613 const_iterator end() const { return Fragments.end(); }
615 reverse_iterator rbegin() { return Fragments.rbegin(); }
616 const_reverse_iterator rbegin() const { return Fragments.rbegin(); }
618 reverse_iterator rend() { return Fragments.rend(); }
619 const_reverse_iterator rend() const { return Fragments.rend(); }
621 size_t size() const { return Fragments.size(); }
623 bool empty() const { return Fragments.empty(); }
625 iterator getSubsectionInsertionPoint(unsigned Subsection);
627 bool isBundleLocked() const { return BundleLockState != NotBundleLocked; }
629 BundleLockStateType getBundleLockState() const { return BundleLockState; }
631 void setBundleLockState(BundleLockStateType NewState);
633 bool isBundleGroupBeforeFirstInst() const {
634 return BundleGroupBeforeFirstInst;
637 void setBundleGroupBeforeFirstInst(bool IsFirst) {
638 BundleGroupBeforeFirstInst = IsFirst;
646 // FIXME: This really doesn't belong here. See comments below.
647 struct IndirectSymbolData {
649 MCSectionData *SectionData;
652 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
654 struct DataRegionData {
655 // This enum should be kept in sync w/ the mach-o definition in
656 // llvm/Object/MachOFormat.h.
657 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
663 friend class MCAsmLayout;
666 typedef iplist<MCSectionData> SectionDataListType;
667 typedef std::vector<const MCSymbol *> SymbolDataListType;
669 typedef SectionDataListType::const_iterator const_iterator;
670 typedef SectionDataListType::iterator iterator;
672 typedef pointee_iterator<SymbolDataListType::const_iterator>
673 const_symbol_iterator;
674 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
676 typedef iterator_range<symbol_iterator> symbol_range;
677 typedef iterator_range<const_symbol_iterator> const_symbol_range;
679 typedef std::vector<std::string> FileNameVectorType;
680 typedef FileNameVectorType::const_iterator const_file_name_iterator;
682 typedef std::vector<IndirectSymbolData>::const_iterator
683 const_indirect_symbol_iterator;
684 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
686 typedef std::vector<DataRegionData>::const_iterator
687 const_data_region_iterator;
688 typedef std::vector<DataRegionData>::iterator data_region_iterator;
690 /// MachO specific deployment target version info.
691 // A Major version of 0 indicates that no version information was supplied
692 // and so the corresponding load command should not be emitted.
694 MCVersionMinType Kind;
698 } VersionMinInfoType;
701 MCAssembler(const MCAssembler &) = delete;
702 void operator=(const MCAssembler &) = delete;
706 MCAsmBackend &Backend;
708 MCCodeEmitter &Emitter;
710 MCObjectWriter &Writer;
714 iplist<MCSectionData> Sections;
716 SymbolDataListType Symbols;
718 DenseSet<const MCSymbol *> LocalsUsedInReloc;
720 /// The map of sections to their associated assembler backend data.
722 // FIXME: Avoid this indirection?
723 DenseMap<const MCSection *, MCSectionData *> SectionMap;
725 std::vector<IndirectSymbolData> IndirectSymbols;
727 std::vector<DataRegionData> DataRegions;
729 /// The list of linker options to propagate into the object file.
730 std::vector<std::vector<std::string>> LinkerOptions;
732 /// List of declared file names
733 FileNameVectorType FileNames;
735 /// The set of function symbols for which a .thumb_func directive has
738 // FIXME: We really would like this in target specific code rather than
739 // here. Maybe when the relocation stuff moves to target specific,
740 // this can go with it? The streamer would need some target specific
742 mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
744 /// \brief The bundle alignment size currently set in the assembler.
746 /// By default it's 0, which means bundling is disabled.
747 unsigned BundleAlignSize;
749 unsigned RelaxAll : 1;
750 unsigned SubsectionsViaSymbols : 1;
752 /// ELF specific e_header flags
753 // It would be good if there were an MCELFAssembler class to hold this.
754 // ELF header flags are used both by the integrated and standalone assemblers.
755 // Access to the flags is necessary in cases where assembler directives affect
756 // which flags to be set.
757 unsigned ELFHeaderEFlags;
759 /// Used to communicate Linker Optimization Hint information between
760 /// the Streamer and the .o writer
761 MCLOHContainer LOHContainer;
763 VersionMinInfoType VersionMinInfo;
766 /// Evaluate a fixup to a relocatable expression and the value which should be
767 /// placed into the fixup.
769 /// \param Layout The layout to use for evaluation.
770 /// \param Fixup The fixup to evaluate.
771 /// \param DF The fragment the fixup is inside.
772 /// \param Target [out] On return, the relocatable expression the fixup
774 /// \param Value [out] On return, the value of the fixup as currently laid
776 /// \return Whether the fixup value was fully resolved. This is true if the
777 /// \p Value result is fixed, otherwise the value may change due to
779 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
780 const MCFragment *DF, MCValue &Target,
781 uint64_t &Value) const;
783 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
784 /// (increased in size, in order to hold its value correctly).
785 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
786 const MCAsmLayout &Layout) const;
788 /// Check whether the given fragment needs relaxation.
789 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
790 const MCAsmLayout &Layout) const;
792 /// \brief Perform one layout iteration and return true if any offsets
794 bool layoutOnce(MCAsmLayout &Layout);
796 /// \brief Perform one layout iteration of the given section and return true
797 /// if any offsets were adjusted.
798 bool layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD);
800 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
802 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
804 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
805 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
806 MCDwarfCallFrameFragment &DF);
808 /// finishLayout - Finalize a layout, including fragment lowering.
809 void finishLayout(MCAsmLayout &Layout);
811 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
812 MCFragment &F, const MCFixup &Fixup);
815 void addLocalUsedInReloc(const MCSymbol &Sym);
816 bool isLocalUsedInReloc(const MCSymbol &Sym) const;
818 /// Compute the effective fragment size assuming it is laid out at the given
819 /// \p SectionAddress and \p FragmentOffset.
820 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
821 const MCFragment &F) const;
823 /// Find the symbol which defines the atom containing the given symbol, or
824 /// null if there is no such symbol.
825 const MCSymbol *getAtom(const MCSymbol &S) const;
827 /// Check whether a particular symbol is visible to the linker and is required
828 /// in the symbol table, or whether it can be discarded by the assembler. This
829 /// also effects whether the assembler treats the label as potentially
830 /// defining a separate atom.
831 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
833 /// Emit the section contents using the given object writer.
834 void writeSectionData(const MCSectionData *Section,
835 const MCAsmLayout &Layout) const;
837 /// Check whether a given symbol has been flagged with .thumb_func.
838 bool isThumbFunc(const MCSymbol *Func) const;
840 /// Flag a function symbol as the target of a .thumb_func directive.
841 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
843 /// ELF e_header flags
844 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
845 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
847 /// MachO deployment target version information.
848 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
849 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
851 VersionMinInfo.Kind = Kind;
852 VersionMinInfo.Major = Major;
853 VersionMinInfo.Minor = Minor;
854 VersionMinInfo.Update = Update;
858 /// Construct a new assembler instance.
860 /// \param OS The stream to output to.
862 // FIXME: How are we going to parameterize this? Two obvious options are stay
863 // concrete and require clients to pass in a target like object. The other
864 // option is to make this abstract, and have targets provide concrete
865 // implementations as we do with AsmParser.
866 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
867 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
871 /// Reuse an assembler instance
875 MCContext &getContext() const { return Context; }
877 MCAsmBackend &getBackend() const { return Backend; }
879 MCCodeEmitter &getEmitter() const { return Emitter; }
881 MCObjectWriter &getWriter() const { return Writer; }
883 /// Finish - Do final processing and write the object to the output stream.
884 /// \p Writer is used for custom object writer (as the MCJIT does),
885 /// if not specified it is automatically created from backend.
888 // FIXME: This does not belong here.
889 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
890 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
892 bool getRelaxAll() const { return RelaxAll; }
893 void setRelaxAll(bool Value) { RelaxAll = Value; }
895 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
897 unsigned getBundleAlignSize() const { return BundleAlignSize; }
899 void setBundleAlignSize(unsigned Size) {
900 assert((Size == 0 || !(Size & (Size - 1))) &&
901 "Expect a power-of-two bundle align size");
902 BundleAlignSize = Size;
905 /// \name Section List Access
908 const SectionDataListType &getSectionList() const { return Sections; }
909 SectionDataListType &getSectionList() { return Sections; }
911 iterator begin() { return Sections.begin(); }
912 const_iterator begin() const { return Sections.begin(); }
914 iterator end() { return Sections.end(); }
915 const_iterator end() const { return Sections.end(); }
917 size_t size() const { return Sections.size(); }
920 /// \name Symbol List Access
922 symbol_iterator symbol_begin() { return Symbols.begin(); }
923 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
925 symbol_iterator symbol_end() { return Symbols.end(); }
926 const_symbol_iterator symbol_end() const { return Symbols.end(); }
928 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
929 const_symbol_range symbols() const {
930 return make_range(symbol_begin(), symbol_end());
933 size_t symbol_size() const { return Symbols.size(); }
936 /// \name Indirect Symbol List Access
939 // FIXME: This is a total hack, this should not be here. Once things are
940 // factored so that the streamer has direct access to the .o writer, it can
942 std::vector<IndirectSymbolData> &getIndirectSymbols() {
943 return IndirectSymbols;
946 indirect_symbol_iterator indirect_symbol_begin() {
947 return IndirectSymbols.begin();
949 const_indirect_symbol_iterator indirect_symbol_begin() const {
950 return IndirectSymbols.begin();
953 indirect_symbol_iterator indirect_symbol_end() {
954 return IndirectSymbols.end();
956 const_indirect_symbol_iterator indirect_symbol_end() const {
957 return IndirectSymbols.end();
960 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
963 /// \name Linker Option List Access
966 std::vector<std::vector<std::string>> &getLinkerOptions() {
967 return LinkerOptions;
971 /// \name Data Region List Access
974 // FIXME: This is a total hack, this should not be here. Once things are
975 // factored so that the streamer has direct access to the .o writer, it can
977 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
979 data_region_iterator data_region_begin() { return DataRegions.begin(); }
980 const_data_region_iterator data_region_begin() const {
981 return DataRegions.begin();
984 data_region_iterator data_region_end() { return DataRegions.end(); }
985 const_data_region_iterator data_region_end() const {
986 return DataRegions.end();
989 size_t data_region_size() const { return DataRegions.size(); }
992 /// \name Data Region List Access
995 // FIXME: This is a total hack, this should not be here. Once things are
996 // factored so that the streamer has direct access to the .o writer, it can
998 MCLOHContainer &getLOHContainer() { return LOHContainer; }
999 const MCLOHContainer &getLOHContainer() const {
1000 return const_cast<MCAssembler *>(this)->getLOHContainer();
1003 /// \name Backend Data Access
1006 MCSectionData &getSectionData(const MCSection &Section) const {
1007 MCSectionData *Entry = SectionMap.lookup(&Section);
1008 assert(Entry && "Missing section data!");
1012 MCSectionData &getOrCreateSectionData(MCSection &Section,
1013 bool *Created = nullptr) {
1014 MCSectionData *&Entry = SectionMap[&Section];
1019 Entry = new MCSectionData(Section, this);
1024 bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }
1026 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
1027 return const_cast<MCSymbolData &>(
1028 static_cast<const MCAssembler &>(*this).getSymbolData(Symbol));
1031 const MCSymbolData &getSymbolData(const MCSymbol &Symbol) const {
1032 return Symbol.getData();
1035 MCSymbolData &getOrCreateSymbolData(const MCSymbol &Symbol,
1036 bool *Created = nullptr) {
1038 *Created = !hasSymbolData(Symbol);
1039 if (!hasSymbolData(Symbol)) {
1040 Symbol.initializeData();
1041 Symbols.push_back(&Symbol);
1043 return Symbol.getData();
1046 const_file_name_iterator file_names_begin() const {
1047 return FileNames.begin();
1050 const_file_name_iterator file_names_end() const { return FileNames.end(); }
1052 void addFileName(StringRef FileName) {
1053 if (std::find(file_names_begin(), file_names_end(), FileName) ==
1055 FileNames.push_back(FileName);
1058 /// \brief Write the necessary bundle padding to the given object writer.
1059 /// Expects a fragment \p F containing instructions and its size \p FSize.
1060 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
1061 MCObjectWriter *OW) const;
1068 /// \brief Compute the amount of padding required before the fragment \p F to
1069 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
1070 /// its section and \p FSize is the fragment's size.
1071 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
1072 uint64_t FOffset, uint64_t FSize);
1074 } // end namespace llvm