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/BinaryFormat/MachO.h"
20 #include "llvm/MC/MCDirectives.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCFixup.h"
23 #include "llvm/MC/MCFragment.h"
24 #include "llvm/MC/MCLinkerOptimizationHint.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/VersionTuple.h"
45 // FIXME: This really doesn't belong here. See comments below.
46 struct IndirectSymbolData {
51 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
53 struct DataRegionData {
54 // This enum should be kept in sync w/ the mach-o definition in
55 // llvm/Object/MachOFormat.h.
56 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
62 friend class MCAsmLayout;
65 using SectionListType = std::vector<MCSection *>;
66 using SymbolDataListType = std::vector<const MCSymbol *>;
68 using const_iterator = pointee_iterator<SectionListType::const_iterator>;
69 using iterator = pointee_iterator<SectionListType::iterator>;
71 using const_symbol_iterator =
72 pointee_iterator<SymbolDataListType::const_iterator>;
73 using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
75 using symbol_range = iterator_range<symbol_iterator>;
76 using const_symbol_range = iterator_range<const_symbol_iterator>;
78 using const_indirect_symbol_iterator =
79 std::vector<IndirectSymbolData>::const_iterator;
80 using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
82 using const_data_region_iterator =
83 std::vector<DataRegionData>::const_iterator;
84 using data_region_iterator = std::vector<DataRegionData>::iterator;
86 /// MachO specific deployment target version info.
87 // A Major version of 0 indicates that no version information was supplied
88 // and so the corresponding load command should not be emitted.
89 using VersionInfoType = struct {
90 bool EmitBuildVersion;
92 MCVersionMinType Type; ///< Used when EmitBuildVersion==false.
93 MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true.
98 /// An optional version of the SDK that was used to build the source.
99 VersionTuple SDKVersion;
105 std::unique_ptr<MCAsmBackend> Backend;
107 std::unique_ptr<MCCodeEmitter> Emitter;
109 std::unique_ptr<MCObjectWriter> Writer;
111 SectionListType Sections;
113 SymbolDataListType Symbols;
115 std::vector<IndirectSymbolData> IndirectSymbols;
117 std::vector<DataRegionData> DataRegions;
119 /// The list of linker options to propagate into the object file.
120 std::vector<std::vector<std::string>> LinkerOptions;
122 /// List of declared file names
123 std::vector<std::string> FileNames;
125 MCDwarfLineTableParams LTParams;
127 /// The set of function symbols for which a .thumb_func directive has
130 // FIXME: We really would like this in target specific code rather than
131 // here. Maybe when the relocation stuff moves to target specific,
132 // this can go with it? The streamer would need some target specific
134 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
136 /// The bundle alignment size currently set in the assembler.
138 /// By default it's 0, which means bundling is disabled.
139 unsigned BundleAlignSize;
142 bool SubsectionsViaSymbols : 1;
143 bool IncrementalLinkerCompatible : 1;
145 /// ELF specific e_header flags
146 // It would be good if there were an MCELFAssembler class to hold this.
147 // ELF header flags are used both by the integrated and standalone assemblers.
148 // Access to the flags is necessary in cases where assembler directives affect
149 // which flags to be set.
150 unsigned ELFHeaderEFlags;
152 /// Used to communicate Linker Optimization Hint information between
153 /// the Streamer and the .o writer
154 MCLOHContainer LOHContainer;
156 VersionInfoType VersionInfo;
158 /// Evaluate a fixup to a relocatable expression and the value which should be
159 /// placed into the fixup.
161 /// \param Layout The layout to use for evaluation.
162 /// \param Fixup The fixup to evaluate.
163 /// \param DF The fragment the fixup is inside.
164 /// \param Target [out] On return, the relocatable expression the fixup
166 /// \param Value [out] On return, the value of the fixup as currently laid
168 /// \param WasForced [out] On return, the value in the fixup is set to the
169 /// correct value if WasForced is true, even if evaluateFixup returns false.
170 /// \return Whether the fixup value was fully resolved. This is true if the
171 /// \p Value result is fixed, otherwise the value may change due to
173 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
174 const MCFragment *DF, MCValue &Target,
175 uint64_t &Value, bool &WasForced) const;
177 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
178 /// (increased in size, in order to hold its value correctly).
179 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
180 const MCAsmLayout &Layout) const;
182 /// Check whether the given fragment needs relaxation.
183 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
184 const MCAsmLayout &Layout) const;
186 /// Perform one layout iteration and return true if any offsets
188 bool layoutOnce(MCAsmLayout &Layout);
190 /// Perform one layout iteration of the given section and return true
191 /// if any offsets were adjusted.
192 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
194 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
196 bool relaxPaddingFragment(MCAsmLayout &Layout, MCPaddingFragment &PF);
198 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
200 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
201 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
202 MCDwarfCallFrameFragment &DF);
203 bool relaxCVInlineLineTable(MCAsmLayout &Layout,
204 MCCVInlineLineTableFragment &DF);
205 bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
207 /// finishLayout - Finalize a layout, including fragment lowering.
208 void finishLayout(MCAsmLayout &Layout);
210 std::tuple<MCValue, uint64_t, bool>
211 handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
214 std::vector<std::pair<StringRef, const MCSymbol *>> Symvers;
216 /// Construct a new assembler instance.
218 // FIXME: How are we going to parameterize this? Two obvious options are stay
219 // concrete and require clients to pass in a target like object. The other
220 // option is to make this abstract, and have targets provide concrete
221 // implementations as we do with AsmParser.
222 MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
223 std::unique_ptr<MCCodeEmitter> Emitter,
224 std::unique_ptr<MCObjectWriter> Writer);
225 MCAssembler(const MCAssembler &) = delete;
226 MCAssembler &operator=(const MCAssembler &) = delete;
229 /// Compute the effective fragment size assuming it is laid out at the given
230 /// \p SectionAddress and \p FragmentOffset.
231 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
232 const MCFragment &F) const;
234 /// Find the symbol which defines the atom containing the given symbol, or
235 /// null if there is no such symbol.
236 const MCSymbol *getAtom(const MCSymbol &S) const;
238 /// Check whether a particular symbol is visible to the linker and is required
239 /// in the symbol table, or whether it can be discarded by the assembler. This
240 /// also effects whether the assembler treats the label as potentially
241 /// defining a separate atom.
242 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
244 /// Emit the section contents to \p OS.
245 void writeSectionData(raw_ostream &OS, const MCSection *Section,
246 const MCAsmLayout &Layout) const;
248 /// Check whether a given symbol has been flagged with .thumb_func.
249 bool isThumbFunc(const MCSymbol *Func) const;
251 /// Flag a function symbol as the target of a .thumb_func directive.
252 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
254 /// ELF e_header flags
255 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
256 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
258 /// MachO deployment target version information.
259 const VersionInfoType &getVersionInfo() const { return VersionInfo; }
260 void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
262 VersionTuple SDKVersion = VersionTuple()) {
263 VersionInfo.EmitBuildVersion = false;
264 VersionInfo.TypeOrPlatform.Type = Type;
265 VersionInfo.Major = Major;
266 VersionInfo.Minor = Minor;
267 VersionInfo.Update = Update;
268 VersionInfo.SDKVersion = SDKVersion;
270 void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
271 unsigned Minor, unsigned Update,
272 VersionTuple SDKVersion = VersionTuple()) {
273 VersionInfo.EmitBuildVersion = true;
274 VersionInfo.TypeOrPlatform.Platform = Platform;
275 VersionInfo.Major = Major;
276 VersionInfo.Minor = Minor;
277 VersionInfo.Update = Update;
278 VersionInfo.SDKVersion = SDKVersion;
281 /// Reuse an assembler instance
285 MCContext &getContext() const { return Context; }
287 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
289 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
291 MCObjectWriter *getWriterPtr() const { return Writer.get(); }
293 MCAsmBackend &getBackend() const { return *Backend; }
295 MCCodeEmitter &getEmitter() const { return *Emitter; }
297 MCObjectWriter &getWriter() const { return *Writer; }
299 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
300 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
302 /// Finish - Do final processing and write the object to the output stream.
303 /// \p Writer is used for custom object writer (as the MCJIT does),
304 /// if not specified it is automatically created from backend.
307 // Layout all section and prepare them for emission.
308 void layout(MCAsmLayout &Layout);
310 // FIXME: This does not belong here.
311 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
312 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
314 bool isIncrementalLinkerCompatible() const {
315 return IncrementalLinkerCompatible;
317 void setIncrementalLinkerCompatible(bool Value) {
318 IncrementalLinkerCompatible = Value;
321 bool getRelaxAll() const { return RelaxAll; }
322 void setRelaxAll(bool Value) { RelaxAll = Value; }
324 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
326 unsigned getBundleAlignSize() const { return BundleAlignSize; }
328 void setBundleAlignSize(unsigned Size) {
329 assert((Size == 0 || !(Size & (Size - 1))) &&
330 "Expect a power-of-two bundle align size");
331 BundleAlignSize = Size;
334 /// \name Section List Access
337 iterator begin() { return Sections.begin(); }
338 const_iterator begin() const { return Sections.begin(); }
340 iterator end() { return Sections.end(); }
341 const_iterator end() const { return Sections.end(); }
343 size_t size() const { return Sections.size(); }
346 /// \name Symbol List Access
348 symbol_iterator symbol_begin() { return Symbols.begin(); }
349 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
351 symbol_iterator symbol_end() { return Symbols.end(); }
352 const_symbol_iterator symbol_end() const { return Symbols.end(); }
354 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
355 const_symbol_range symbols() const {
356 return make_range(symbol_begin(), symbol_end());
359 size_t symbol_size() const { return Symbols.size(); }
362 /// \name Indirect Symbol List Access
365 // FIXME: This is a total hack, this should not be here. Once things are
366 // factored so that the streamer has direct access to the .o writer, it can
368 std::vector<IndirectSymbolData> &getIndirectSymbols() {
369 return IndirectSymbols;
372 indirect_symbol_iterator indirect_symbol_begin() {
373 return IndirectSymbols.begin();
375 const_indirect_symbol_iterator indirect_symbol_begin() const {
376 return IndirectSymbols.begin();
379 indirect_symbol_iterator indirect_symbol_end() {
380 return IndirectSymbols.end();
382 const_indirect_symbol_iterator indirect_symbol_end() const {
383 return IndirectSymbols.end();
386 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
389 /// \name Linker Option List Access
392 std::vector<std::vector<std::string>> &getLinkerOptions() {
393 return LinkerOptions;
397 /// \name Data Region List Access
400 // FIXME: This is a total hack, this should not be here. Once things are
401 // factored so that the streamer has direct access to the .o writer, it can
403 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
405 data_region_iterator data_region_begin() { return DataRegions.begin(); }
406 const_data_region_iterator data_region_begin() const {
407 return DataRegions.begin();
410 data_region_iterator data_region_end() { return DataRegions.end(); }
411 const_data_region_iterator data_region_end() const {
412 return DataRegions.end();
415 size_t data_region_size() const { return DataRegions.size(); }
418 /// \name Data Region List Access
421 // FIXME: This is a total hack, this should not be here. Once things are
422 // factored so that the streamer has direct access to the .o writer, it can
424 MCLOHContainer &getLOHContainer() { return LOHContainer; }
425 const MCLOHContainer &getLOHContainer() const {
426 return const_cast<MCAssembler *>(this)->getLOHContainer();
429 struct CGProfileEntry {
430 const MCSymbolRefExpr *From;
431 const MCSymbolRefExpr *To;
434 std::vector<CGProfileEntry> CGProfile;
436 /// \name Backend Data Access
439 bool registerSection(MCSection &Section);
441 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
443 ArrayRef<std::string> getFileNames() { return FileNames; }
445 void addFileName(StringRef FileName) {
446 if (!is_contained(FileNames, FileName))
447 FileNames.push_back(FileName);
450 /// Write the necessary bundle padding to \p OS.
451 /// Expects a fragment \p F containing instructions and its size \p FSize.
452 void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,
453 uint64_t FSize) const;
460 /// Compute the amount of padding required before the fragment \p F to
461 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
462 /// its section and \p FSize is the fragment's size.
463 uint64_t computeBundlePadding(const MCAssembler &Assembler,
464 const MCEncodedFragment *F, uint64_t FOffset,
467 } // end namespace llvm
469 #endif // LLVM_MC_MCASSEMBLER_H