xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/MC/MCAssembler.h (revision c9ccf3a32da427475985b85d7df023ccfb138c27)
1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_MC_MCASSEMBLER_H
10 #define LLVM_MC_MCASSEMBLER_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/iterator.h"
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/BinaryFormat/MachO.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"
25 #include "llvm/Support/VersionTuple.h"
26 #include <cassert>
27 #include <cstddef>
28 #include <cstdint>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace llvm {
34 
35 class MCAsmBackend;
36 class MCAsmLayout;
37 class MCContext;
38 class MCCodeEmitter;
39 class MCFragment;
40 class MCObjectWriter;
41 class MCSection;
42 class MCValue;
43 
44 // FIXME: This really doesn't belong here. See comments below.
45 struct IndirectSymbolData {
46   MCSymbol *Symbol;
47   MCSection *Section;
48 };
49 
50 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
51 // to one another.
52 struct DataRegionData {
53   // This enum should be kept in sync w/ the mach-o definition in
54   // llvm/Object/MachOFormat.h.
55   enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
56   MCSymbol *Start;
57   MCSymbol *End;
58 };
59 
60 class MCAssembler {
61   friend class MCAsmLayout;
62 
63 public:
64   using SectionListType = std::vector<MCSection *>;
65   using SymbolDataListType = std::vector<const MCSymbol *>;
66 
67   using const_iterator = pointee_iterator<SectionListType::const_iterator>;
68   using iterator = pointee_iterator<SectionListType::iterator>;
69 
70   using const_symbol_iterator =
71       pointee_iterator<SymbolDataListType::const_iterator>;
72   using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
73 
74   using symbol_range = iterator_range<symbol_iterator>;
75   using const_symbol_range = iterator_range<const_symbol_iterator>;
76 
77   using const_indirect_symbol_iterator =
78       std::vector<IndirectSymbolData>::const_iterator;
79   using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
80 
81   using const_data_region_iterator =
82       std::vector<DataRegionData>::const_iterator;
83   using data_region_iterator = std::vector<DataRegionData>::iterator;
84 
85   /// MachO specific deployment target version info.
86   // A Major version of 0 indicates that no version information was supplied
87   // and so the corresponding load command should not be emitted.
88   using VersionInfoType = struct {
89     bool EmitBuildVersion;
90     union {
91       MCVersionMinType Type;          ///< Used when EmitBuildVersion==false.
92       MachO::PlatformType Platform;   ///< Used when EmitBuildVersion==true.
93     } TypeOrPlatform;
94     unsigned Major;
95     unsigned Minor;
96     unsigned Update;
97     /// An optional version of the SDK that was used to build the source.
98     VersionTuple SDKVersion;
99   };
100 
101 private:
102   MCContext &Context;
103 
104   std::unique_ptr<MCAsmBackend> Backend;
105 
106   std::unique_ptr<MCCodeEmitter> Emitter;
107 
108   std::unique_ptr<MCObjectWriter> Writer;
109 
110   SectionListType Sections;
111 
112   SymbolDataListType Symbols;
113 
114   std::vector<IndirectSymbolData> IndirectSymbols;
115 
116   std::vector<DataRegionData> DataRegions;
117 
118   /// The list of linker options to propagate into the object file.
119   std::vector<std::vector<std::string>> LinkerOptions;
120 
121   /// List of declared file names
122   std::vector<std::pair<std::string, size_t>> FileNames;
123 
124   MCDwarfLineTableParams LTParams;
125 
126   /// The set of function symbols for which a .thumb_func directive has
127   /// been seen.
128   //
129   // FIXME: We really would like this in target specific code rather than
130   // here. Maybe when the relocation stuff moves to target specific,
131   // this can go with it? The streamer would need some target specific
132   // refactoring too.
133   mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
134 
135   /// The bundle alignment size currently set in the assembler.
136   ///
137   /// By default it's 0, which means bundling is disabled.
138   unsigned BundleAlignSize;
139 
140   bool RelaxAll : 1;
141   bool SubsectionsViaSymbols : 1;
142   bool IncrementalLinkerCompatible : 1;
143 
144   /// ELF specific e_header flags
145   // It would be good if there were an MCELFAssembler class to hold this.
146   // ELF header flags are used both by the integrated and standalone assemblers.
147   // Access to the flags is necessary in cases where assembler directives affect
148   // which flags to be set.
149   unsigned ELFHeaderEFlags;
150 
151   /// Used to communicate Linker Optimization Hint information between
152   /// the Streamer and the .o writer
153   MCLOHContainer LOHContainer;
154 
155   VersionInfoType VersionInfo;
156   VersionInfoType DarwinTargetVariantVersionInfo;
157 
158   /// Evaluate a fixup to a relocatable expression and the value which should be
159   /// placed into the fixup.
160   ///
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
165   /// evaluates to.
166   /// \param Value [out] On return, the value of the fixup as currently laid
167   /// out.
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
172   /// relocation.
173   bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
174                      const MCFragment *DF, MCValue &Target,
175                      uint64_t &Value, bool &WasForced) const;
176 
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;
181 
182   /// Check whether the given fragment needs relaxation.
183   bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
184                                const MCAsmLayout &Layout) const;
185 
186   /// Perform one layout iteration and return true if any offsets
187   /// were adjusted.
188   bool layoutOnce(MCAsmLayout &Layout);
189 
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);
193 
194   /// Perform relaxation on a single fragment - returns true if the fragment
195   /// changes as a result of relaxation.
196   bool relaxFragment(MCAsmLayout &Layout, MCFragment &F);
197   bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
198   bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
199   bool relaxBoundaryAlign(MCAsmLayout &Layout, MCBoundaryAlignFragment &BF);
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);
206   bool relaxPseudoProbeAddr(MCAsmLayout &Layout, MCPseudoProbeAddrFragment &DF);
207 
208   /// finishLayout - Finalize a layout, including fragment lowering.
209   void finishLayout(MCAsmLayout &Layout);
210 
211   std::tuple<MCValue, uint64_t, bool>
212   handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
213 
214 public:
215   struct Symver {
216     SMLoc Loc;
217     const MCSymbol *Sym;
218     StringRef Name;
219     // True if .symver *, *@@@* or .symver *, *, remove.
220     bool KeepOriginalSym;
221   };
222   std::vector<Symver> Symvers;
223 
224   /// Construct a new assembler instance.
225   //
226   // FIXME: How are we going to parameterize this? Two obvious options are stay
227   // concrete and require clients to pass in a target like object. The other
228   // option is to make this abstract, and have targets provide concrete
229   // implementations as we do with AsmParser.
230   MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
231               std::unique_ptr<MCCodeEmitter> Emitter,
232               std::unique_ptr<MCObjectWriter> Writer);
233   MCAssembler(const MCAssembler &) = delete;
234   MCAssembler &operator=(const MCAssembler &) = delete;
235   ~MCAssembler();
236 
237   /// Compute the effective fragment size assuming it is laid out at the given
238   /// \p SectionAddress and \p FragmentOffset.
239   uint64_t computeFragmentSize(const MCAsmLayout &Layout,
240                                const MCFragment &F) const;
241 
242   /// Find the symbol which defines the atom containing the given symbol, or
243   /// null if there is no such symbol.
244   const MCSymbol *getAtom(const MCSymbol &S) const;
245 
246   /// Check whether a particular symbol is visible to the linker and is required
247   /// in the symbol table, or whether it can be discarded by the assembler. This
248   /// also effects whether the assembler treats the label as potentially
249   /// defining a separate atom.
250   bool isSymbolLinkerVisible(const MCSymbol &SD) const;
251 
252   /// Emit the section contents to \p OS.
253   void writeSectionData(raw_ostream &OS, const MCSection *Section,
254                         const MCAsmLayout &Layout) const;
255 
256   /// Check whether a given symbol has been flagged with .thumb_func.
257   bool isThumbFunc(const MCSymbol *Func) const;
258 
259   /// Flag a function symbol as the target of a .thumb_func directive.
260   void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
261 
262   /// ELF e_header flags
263   unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
264   void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
265 
266   /// MachO deployment target version information.
267   const VersionInfoType &getVersionInfo() const { return VersionInfo; }
268   void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
269                      unsigned Update,
270                      VersionTuple SDKVersion = VersionTuple()) {
271     VersionInfo.EmitBuildVersion = false;
272     VersionInfo.TypeOrPlatform.Type = Type;
273     VersionInfo.Major = Major;
274     VersionInfo.Minor = Minor;
275     VersionInfo.Update = Update;
276     VersionInfo.SDKVersion = SDKVersion;
277   }
278   void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
279                        unsigned Minor, unsigned Update,
280                        VersionTuple SDKVersion = VersionTuple()) {
281     VersionInfo.EmitBuildVersion = true;
282     VersionInfo.TypeOrPlatform.Platform = Platform;
283     VersionInfo.Major = Major;
284     VersionInfo.Minor = Minor;
285     VersionInfo.Update = Update;
286     VersionInfo.SDKVersion = SDKVersion;
287   }
288 
289   const VersionInfoType &getDarwinTargetVariantVersionInfo() const {
290     return DarwinTargetVariantVersionInfo;
291   }
292   void setDarwinTargetVariantBuildVersion(MachO::PlatformType Platform,
293                                           unsigned Major, unsigned Minor,
294                                           unsigned Update,
295                                           VersionTuple SDKVersion) {
296     DarwinTargetVariantVersionInfo.EmitBuildVersion = true;
297     DarwinTargetVariantVersionInfo.TypeOrPlatform.Platform = Platform;
298     DarwinTargetVariantVersionInfo.Major = Major;
299     DarwinTargetVariantVersionInfo.Minor = Minor;
300     DarwinTargetVariantVersionInfo.Update = Update;
301     DarwinTargetVariantVersionInfo.SDKVersion = SDKVersion;
302   }
303 
304   /// Reuse an assembler instance
305   ///
306   void reset();
307 
308   MCContext &getContext() const { return Context; }
309 
310   MCAsmBackend *getBackendPtr() const { return Backend.get(); }
311 
312   MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
313 
314   MCObjectWriter *getWriterPtr() const { return Writer.get(); }
315 
316   MCAsmBackend &getBackend() const { return *Backend; }
317 
318   MCCodeEmitter &getEmitter() const { return *Emitter; }
319 
320   MCObjectWriter &getWriter() const { return *Writer; }
321 
322   MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
323   void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
324 
325   /// Finish - Do final processing and write the object to the output stream.
326   /// \p Writer is used for custom object writer (as the MCJIT does),
327   /// if not specified it is automatically created from backend.
328   void Finish();
329 
330   // Layout all section and prepare them for emission.
331   void layout(MCAsmLayout &Layout);
332 
333   // FIXME: This does not belong here.
334   bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
335   void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
336 
337   bool isIncrementalLinkerCompatible() const {
338     return IncrementalLinkerCompatible;
339   }
340   void setIncrementalLinkerCompatible(bool Value) {
341     IncrementalLinkerCompatible = Value;
342   }
343 
344   bool getRelaxAll() const { return RelaxAll; }
345   void setRelaxAll(bool Value) { RelaxAll = Value; }
346 
347   bool isBundlingEnabled() const { return BundleAlignSize != 0; }
348 
349   unsigned getBundleAlignSize() const { return BundleAlignSize; }
350 
351   void setBundleAlignSize(unsigned Size) {
352     assert((Size == 0 || !(Size & (Size - 1))) &&
353            "Expect a power-of-two bundle align size");
354     BundleAlignSize = Size;
355   }
356 
357   /// \name Section List Access
358   /// @{
359 
360   iterator begin() { return Sections.begin(); }
361   const_iterator begin() const { return Sections.begin(); }
362 
363   iterator end() { return Sections.end(); }
364   const_iterator end() const { return Sections.end(); }
365 
366   size_t size() const { return Sections.size(); }
367 
368   /// @}
369   /// \name Symbol List Access
370   /// @{
371   symbol_iterator symbol_begin() { return Symbols.begin(); }
372   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
373 
374   symbol_iterator symbol_end() { return Symbols.end(); }
375   const_symbol_iterator symbol_end() const { return Symbols.end(); }
376 
377   symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
378   const_symbol_range symbols() const {
379     return make_range(symbol_begin(), symbol_end());
380   }
381 
382   size_t symbol_size() const { return Symbols.size(); }
383 
384   /// @}
385   /// \name Indirect Symbol List Access
386   /// @{
387 
388   // FIXME: This is a total hack, this should not be here. Once things are
389   // factored so that the streamer has direct access to the .o writer, it can
390   // disappear.
391   std::vector<IndirectSymbolData> &getIndirectSymbols() {
392     return IndirectSymbols;
393   }
394 
395   indirect_symbol_iterator indirect_symbol_begin() {
396     return IndirectSymbols.begin();
397   }
398   const_indirect_symbol_iterator indirect_symbol_begin() const {
399     return IndirectSymbols.begin();
400   }
401 
402   indirect_symbol_iterator indirect_symbol_end() {
403     return IndirectSymbols.end();
404   }
405   const_indirect_symbol_iterator indirect_symbol_end() const {
406     return IndirectSymbols.end();
407   }
408 
409   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
410 
411   /// @}
412   /// \name Linker Option List Access
413   /// @{
414 
415   std::vector<std::vector<std::string>> &getLinkerOptions() {
416     return LinkerOptions;
417   }
418 
419   /// @}
420   /// \name Data Region List Access
421   /// @{
422 
423   // FIXME: This is a total hack, this should not be here. Once things are
424   // factored so that the streamer has direct access to the .o writer, it can
425   // disappear.
426   std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
427 
428   data_region_iterator data_region_begin() { return DataRegions.begin(); }
429   const_data_region_iterator data_region_begin() const {
430     return DataRegions.begin();
431   }
432 
433   data_region_iterator data_region_end() { return DataRegions.end(); }
434   const_data_region_iterator data_region_end() const {
435     return DataRegions.end();
436   }
437 
438   size_t data_region_size() const { return DataRegions.size(); }
439 
440   /// @}
441   /// \name Data Region List Access
442   /// @{
443 
444   // FIXME: This is a total hack, this should not be here. Once things are
445   // factored so that the streamer has direct access to the .o writer, it can
446   // disappear.
447   MCLOHContainer &getLOHContainer() { return LOHContainer; }
448   const MCLOHContainer &getLOHContainer() const {
449     return const_cast<MCAssembler *>(this)->getLOHContainer();
450   }
451 
452   struct CGProfileEntry {
453     const MCSymbolRefExpr *From;
454     const MCSymbolRefExpr *To;
455     uint64_t Count;
456   };
457   std::vector<CGProfileEntry> CGProfile;
458   /// @}
459   /// \name Backend Data Access
460   /// @{
461 
462   bool registerSection(MCSection &Section);
463 
464   void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
465 
466   MutableArrayRef<std::pair<std::string, size_t>> getFileNames() {
467     return FileNames;
468   }
469 
470   void addFileName(StringRef FileName) {
471     FileNames.emplace_back(std::string(FileName), Symbols.size());
472   }
473 
474   /// Write the necessary bundle padding to \p OS.
475   /// Expects a fragment \p F containing instructions and its size \p FSize.
476   void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,
477                             uint64_t FSize) const;
478 
479   /// @}
480 
481   void dump() const;
482 };
483 
484 /// Compute the amount of padding required before the fragment \p F to
485 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
486 /// its section and \p FSize is the fragment's size.
487 uint64_t computeBundlePadding(const MCAssembler &Assembler,
488                               const MCEncodedFragment *F, uint64_t FOffset,
489                               uint64_t FSize);
490 
491 } // end namespace llvm
492 
493 #endif // LLVM_MC_MCASSEMBLER_H
494