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/ArrayRef.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/iterator.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCFixup.h"
22 #include "llvm/MC/MCFragment.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSymbol.h"
43 // FIXME: This really doesn't belong here. See comments below.
44 struct IndirectSymbolData {
49 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
51 struct DataRegionData {
52 // This enum should be kept in sync w/ the mach-o definition in
53 // llvm/Object/MachOFormat.h.
54 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
60 friend class MCAsmLayout;
63 using SectionListType = std::vector<MCSection *>;
64 using SymbolDataListType = std::vector<const MCSymbol *>;
66 using const_iterator = pointee_iterator<SectionListType::const_iterator>;
67 using iterator = pointee_iterator<SectionListType::iterator>;
69 using const_symbol_iterator =
70 pointee_iterator<SymbolDataListType::const_iterator>;
71 using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
73 using symbol_range = iterator_range<symbol_iterator>;
74 using const_symbol_range = iterator_range<const_symbol_iterator>;
76 using const_indirect_symbol_iterator =
77 std::vector<IndirectSymbolData>::const_iterator;
78 using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
80 using const_data_region_iterator =
81 std::vector<DataRegionData>::const_iterator;
82 using data_region_iterator = std::vector<DataRegionData>::iterator;
84 /// MachO specific deployment target version info.
85 // A Major version of 0 indicates that no version information was supplied
86 // and so the corresponding load command should not be emitted.
87 using VersionMinInfoType = struct {
88 MCVersionMinType Kind;
97 MCAsmBackend &Backend;
99 MCCodeEmitter &Emitter;
101 MCObjectWriter &Writer;
103 SectionListType Sections;
105 SymbolDataListType Symbols;
107 std::vector<IndirectSymbolData> IndirectSymbols;
109 std::vector<DataRegionData> DataRegions;
111 /// The list of linker options to propagate into the object file.
112 std::vector<std::vector<std::string>> LinkerOptions;
114 /// List of declared file names
115 std::vector<std::string> FileNames;
117 MCDwarfLineTableParams LTParams;
119 /// The set of function symbols for which a .thumb_func directive has
122 // FIXME: We really would like this in target specific code rather than
123 // here. Maybe when the relocation stuff moves to target specific,
124 // this can go with it? The streamer would need some target specific
126 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
128 /// \brief The bundle alignment size currently set in the assembler.
130 /// By default it's 0, which means bundling is disabled.
131 unsigned BundleAlignSize;
134 bool SubsectionsViaSymbols : 1;
135 bool IncrementalLinkerCompatible : 1;
137 /// ELF specific e_header flags
138 // It would be good if there were an MCELFAssembler class to hold this.
139 // ELF header flags are used both by the integrated and standalone assemblers.
140 // Access to the flags is necessary in cases where assembler directives affect
141 // which flags to be set.
142 unsigned ELFHeaderEFlags;
144 /// Used to communicate Linker Optimization Hint information between
145 /// the Streamer and the .o writer
146 MCLOHContainer LOHContainer;
148 VersionMinInfoType VersionMinInfo;
150 /// Evaluate a fixup to a relocatable expression and the value which should be
151 /// placed into the fixup.
153 /// \param Layout The layout to use for evaluation.
154 /// \param Fixup The fixup to evaluate.
155 /// \param DF The fragment the fixup is inside.
156 /// \param Target [out] On return, the relocatable expression the fixup
158 /// \param Value [out] On return, the value of the fixup as currently laid
160 /// \return Whether the fixup value was fully resolved. This is true if the
161 /// \p Value result is fixed, otherwise the value may change due to
163 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
164 const MCFragment *DF, MCValue &Target,
165 uint64_t &Value) const;
167 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
168 /// (increased in size, in order to hold its value correctly).
169 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
170 const MCAsmLayout &Layout) const;
172 /// Check whether the given fragment needs relaxation.
173 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
174 const MCAsmLayout &Layout) const;
176 /// \brief Perform one layout iteration and return true if any offsets
178 bool layoutOnce(MCAsmLayout &Layout);
180 /// \brief Perform one layout iteration of the given section and return true
181 /// if any offsets were adjusted.
182 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
184 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
186 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
188 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
189 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
190 MCDwarfCallFrameFragment &DF);
191 bool relaxCVInlineLineTable(MCAsmLayout &Layout,
192 MCCVInlineLineTableFragment &DF);
193 bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
195 /// finishLayout - Finalize a layout, including fragment lowering.
196 void finishLayout(MCAsmLayout &Layout);
198 std::tuple<MCValue, uint64_t, bool>
199 handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
202 /// Construct a new assembler instance.
204 // FIXME: How are we going to parameterize this? Two obvious options are stay
205 // concrete and require clients to pass in a target like object. The other
206 // option is to make this abstract, and have targets provide concrete
207 // implementations as we do with AsmParser.
208 MCAssembler(MCContext &Context, MCAsmBackend &Backend,
209 MCCodeEmitter &Emitter, MCObjectWriter &Writer);
210 MCAssembler(const MCAssembler &) = delete;
211 MCAssembler &operator=(const MCAssembler &) = delete;
214 /// Compute the effective fragment size assuming it is laid out at the given
215 /// \p SectionAddress and \p FragmentOffset.
216 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
217 const MCFragment &F) const;
219 /// Find the symbol which defines the atom containing the given symbol, or
220 /// null if there is no such symbol.
221 const MCSymbol *getAtom(const MCSymbol &S) const;
223 /// Check whether a particular symbol is visible to the linker and is required
224 /// in the symbol table, or whether it can be discarded by the assembler. This
225 /// also effects whether the assembler treats the label as potentially
226 /// defining a separate atom.
227 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
229 /// Emit the section contents using the given object writer.
230 void writeSectionData(const MCSection *Section,
231 const MCAsmLayout &Layout) const;
233 /// Check whether a given symbol has been flagged with .thumb_func.
234 bool isThumbFunc(const MCSymbol *Func) const;
236 /// Flag a function symbol as the target of a .thumb_func directive.
237 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
239 /// ELF e_header flags
240 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
241 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
243 /// MachO deployment target version information.
244 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
245 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
247 VersionMinInfo.Kind = Kind;
248 VersionMinInfo.Major = Major;
249 VersionMinInfo.Minor = Minor;
250 VersionMinInfo.Update = Update;
253 /// Reuse an assembler instance
257 MCContext &getContext() const { return Context; }
259 MCAsmBackend &getBackend() const { return Backend; }
261 MCCodeEmitter &getEmitter() const { return Emitter; }
263 MCObjectWriter &getWriter() const { return Writer; }
265 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
266 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
268 /// Finish - Do final processing and write the object to the output stream.
269 /// \p Writer is used for custom object writer (as the MCJIT does),
270 /// if not specified it is automatically created from backend.
273 // Layout all section and prepare them for emission.
274 void layout(MCAsmLayout &Layout);
276 // FIXME: This does not belong here.
277 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
278 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
280 bool isIncrementalLinkerCompatible() const {
281 return IncrementalLinkerCompatible;
283 void setIncrementalLinkerCompatible(bool Value) {
284 IncrementalLinkerCompatible = Value;
287 bool getRelaxAll() const { return RelaxAll; }
288 void setRelaxAll(bool Value) { RelaxAll = Value; }
290 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
292 unsigned getBundleAlignSize() const { return BundleAlignSize; }
294 void setBundleAlignSize(unsigned Size) {
295 assert((Size == 0 || !(Size & (Size - 1))) &&
296 "Expect a power-of-two bundle align size");
297 BundleAlignSize = Size;
300 /// \name Section List Access
303 iterator begin() { return Sections.begin(); }
304 const_iterator begin() const { return Sections.begin(); }
306 iterator end() { return Sections.end(); }
307 const_iterator end() const { return Sections.end(); }
309 size_t size() const { return Sections.size(); }
312 /// \name Symbol List Access
314 symbol_iterator symbol_begin() { return Symbols.begin(); }
315 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
317 symbol_iterator symbol_end() { return Symbols.end(); }
318 const_symbol_iterator symbol_end() const { return Symbols.end(); }
320 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
321 const_symbol_range symbols() const {
322 return make_range(symbol_begin(), symbol_end());
325 size_t symbol_size() const { return Symbols.size(); }
328 /// \name Indirect Symbol List Access
331 // FIXME: This is a total hack, this should not be here. Once things are
332 // factored so that the streamer has direct access to the .o writer, it can
334 std::vector<IndirectSymbolData> &getIndirectSymbols() {
335 return IndirectSymbols;
338 indirect_symbol_iterator indirect_symbol_begin() {
339 return IndirectSymbols.begin();
341 const_indirect_symbol_iterator indirect_symbol_begin() const {
342 return IndirectSymbols.begin();
345 indirect_symbol_iterator indirect_symbol_end() {
346 return IndirectSymbols.end();
348 const_indirect_symbol_iterator indirect_symbol_end() const {
349 return IndirectSymbols.end();
352 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
355 /// \name Linker Option List Access
358 std::vector<std::vector<std::string>> &getLinkerOptions() {
359 return LinkerOptions;
363 /// \name Data Region List Access
366 // FIXME: This is a total hack, this should not be here. Once things are
367 // factored so that the streamer has direct access to the .o writer, it can
369 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
371 data_region_iterator data_region_begin() { return DataRegions.begin(); }
372 const_data_region_iterator data_region_begin() const {
373 return DataRegions.begin();
376 data_region_iterator data_region_end() { return DataRegions.end(); }
377 const_data_region_iterator data_region_end() const {
378 return DataRegions.end();
381 size_t data_region_size() const { return DataRegions.size(); }
384 /// \name Data Region List Access
387 // FIXME: This is a total hack, this should not be here. Once things are
388 // factored so that the streamer has direct access to the .o writer, it can
390 MCLOHContainer &getLOHContainer() { return LOHContainer; }
391 const MCLOHContainer &getLOHContainer() const {
392 return const_cast<MCAssembler *>(this)->getLOHContainer();
395 /// \name Backend Data Access
398 bool registerSection(MCSection &Section);
400 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
402 ArrayRef<std::string> getFileNames() { return FileNames; }
404 void addFileName(StringRef FileName) {
405 if (!is_contained(FileNames, FileName))
406 FileNames.push_back(FileName);
409 /// \brief Write the necessary bundle padding to the given object writer.
410 /// Expects a fragment \p F containing instructions and its size \p FSize.
411 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
412 MCObjectWriter *OW) const;
419 /// \brief Compute the amount of padding required before the fragment \p F to
420 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
421 /// its section and \p FSize is the fragment's size.
422 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
423 uint64_t FOffset, uint64_t FSize);
425 } // end namespace llvm
427 #endif // LLVM_MC_MCASSEMBLER_H