1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_MC_MCASSEMBLER_H 11 #define LLVM_MC_MCASSEMBLER_H 12 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" 27 #include <cassert> 28 #include <cstddef> 29 #include <cstdint> 30 #include <string> 31 #include <utility> 32 #include <vector> 33 34 namespace llvm { 35 36 class MCAsmBackend; 37 class MCAsmLayout; 38 class MCContext; 39 class MCCodeEmitter; 40 class MCFragment; 41 class MCObjectWriter; 42 class MCSection; 43 class MCValue; 44 45 // FIXME: This really doesn't belong here. See comments below. 46 struct IndirectSymbolData { 47 MCSymbol *Symbol; 48 MCSection *Section; 49 }; 50 51 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk 52 // to one another. 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; 57 MCSymbol *Start; 58 MCSymbol *End; 59 }; 60 61 class MCAssembler { 62 friend class MCAsmLayout; 63 64 public: 65 using SectionListType = std::vector<MCSection *>; 66 using SymbolDataListType = std::vector<const MCSymbol *>; 67 68 using const_iterator = pointee_iterator<SectionListType::const_iterator>; 69 using iterator = pointee_iterator<SectionListType::iterator>; 70 71 using const_symbol_iterator = 72 pointee_iterator<SymbolDataListType::const_iterator>; 73 using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>; 74 75 using symbol_range = iterator_range<symbol_iterator>; 76 using const_symbol_range = iterator_range<const_symbol_iterator>; 77 78 using const_indirect_symbol_iterator = 79 std::vector<IndirectSymbolData>::const_iterator; 80 using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator; 81 82 using const_data_region_iterator = 83 std::vector<DataRegionData>::const_iterator; 84 using data_region_iterator = std::vector<DataRegionData>::iterator; 85 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; 91 union { 92 MCVersionMinType Type; ///< Used when EmitBuildVersion==false. 93 MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true. 94 } TypeOrPlatform; 95 unsigned Major; 96 unsigned Minor; 97 unsigned Update; 98 /// An optional version of the SDK that was used to build the source. 99 VersionTuple SDKVersion; 100 }; 101 102 private: 103 MCContext &Context; 104 105 std::unique_ptr<MCAsmBackend> Backend; 106 107 std::unique_ptr<MCCodeEmitter> Emitter; 108 109 std::unique_ptr<MCObjectWriter> Writer; 110 111 SectionListType Sections; 112 113 SymbolDataListType Symbols; 114 115 std::vector<IndirectSymbolData> IndirectSymbols; 116 117 std::vector<DataRegionData> DataRegions; 118 119 /// The list of linker options to propagate into the object file. 120 std::vector<std::vector<std::string>> LinkerOptions; 121 122 /// List of declared file names 123 std::vector<std::string> FileNames; 124 125 MCDwarfLineTableParams LTParams; 126 127 /// The set of function symbols for which a .thumb_func directive has 128 /// been seen. 129 // 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 133 // refactoring too. 134 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs; 135 136 /// The bundle alignment size currently set in the assembler. 137 /// 138 /// By default it's 0, which means bundling is disabled. 139 unsigned BundleAlignSize; 140 141 bool RelaxAll : 1; 142 bool SubsectionsViaSymbols : 1; 143 bool IncrementalLinkerCompatible : 1; 144 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; 151 152 /// Used to communicate Linker Optimization Hint information between 153 /// the Streamer and the .o writer 154 MCLOHContainer LOHContainer; 155 156 VersionInfoType VersionInfo; 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 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF); 195 196 bool relaxPaddingFragment(MCAsmLayout &Layout, MCPaddingFragment &PF); 197 198 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF); 199 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 207 /// finishLayout - Finalize a layout, including fragment lowering. 208 void finishLayout(MCAsmLayout &Layout); 209 210 std::tuple<MCValue, uint64_t, bool> 211 handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup); 212 213 public: 214 std::vector<std::pair<StringRef, const MCSymbol *>> Symvers; 215 216 /// Construct a new assembler instance. 217 // 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; 227 ~MCAssembler(); 228 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; 233 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; 237 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; 243 244 /// Emit the section contents to \p OS. 245 void writeSectionData(raw_ostream &OS, const MCSection *Section, 246 const MCAsmLayout &Layout) const; 247 248 /// Check whether a given symbol has been flagged with .thumb_func. 249 bool isThumbFunc(const MCSymbol *Func) const; 250 251 /// Flag a function symbol as the target of a .thumb_func directive. setIsThumbFunc(const MCSymbol * Func)252 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); } 253 254 /// ELF e_header flags getELFHeaderEFlags()255 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; } setELFHeaderEFlags(unsigned Flags)256 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; } 257 258 /// MachO deployment target version information. getVersionInfo()259 const VersionInfoType &getVersionInfo() const { return VersionInfo; } 260 void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor, 261 unsigned Update, 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; 269 } 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; 279 } 280 281 /// Reuse an assembler instance 282 /// 283 void reset(); 284 getContext()285 MCContext &getContext() const { return Context; } 286 getBackendPtr()287 MCAsmBackend *getBackendPtr() const { return Backend.get(); } 288 getEmitterPtr()289 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); } 290 getWriterPtr()291 MCObjectWriter *getWriterPtr() const { return Writer.get(); } 292 getBackend()293 MCAsmBackend &getBackend() const { return *Backend; } 294 getEmitter()295 MCCodeEmitter &getEmitter() const { return *Emitter; } 296 getWriter()297 MCObjectWriter &getWriter() const { return *Writer; } 298 getDWARFLinetableParams()299 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; } setDWARFLinetableParams(MCDwarfLineTableParams P)300 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; } 301 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. 305 void Finish(); 306 307 // Layout all section and prepare them for emission. 308 void layout(MCAsmLayout &Layout); 309 310 // FIXME: This does not belong here. getSubsectionsViaSymbols()311 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; } setSubsectionsViaSymbols(bool Value)312 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; } 313 isIncrementalLinkerCompatible()314 bool isIncrementalLinkerCompatible() const { 315 return IncrementalLinkerCompatible; 316 } setIncrementalLinkerCompatible(bool Value)317 void setIncrementalLinkerCompatible(bool Value) { 318 IncrementalLinkerCompatible = Value; 319 } 320 getRelaxAll()321 bool getRelaxAll() const { return RelaxAll; } setRelaxAll(bool Value)322 void setRelaxAll(bool Value) { RelaxAll = Value; } 323 isBundlingEnabled()324 bool isBundlingEnabled() const { return BundleAlignSize != 0; } 325 getBundleAlignSize()326 unsigned getBundleAlignSize() const { return BundleAlignSize; } 327 setBundleAlignSize(unsigned Size)328 void setBundleAlignSize(unsigned Size) { 329 assert((Size == 0 || !(Size & (Size - 1))) && 330 "Expect a power-of-two bundle align size"); 331 BundleAlignSize = Size; 332 } 333 334 /// \name Section List Access 335 /// @{ 336 begin()337 iterator begin() { return Sections.begin(); } begin()338 const_iterator begin() const { return Sections.begin(); } 339 end()340 iterator end() { return Sections.end(); } end()341 const_iterator end() const { return Sections.end(); } 342 size()343 size_t size() const { return Sections.size(); } 344 345 /// @} 346 /// \name Symbol List Access 347 /// @{ symbol_begin()348 symbol_iterator symbol_begin() { return Symbols.begin(); } symbol_begin()349 const_symbol_iterator symbol_begin() const { return Symbols.begin(); } 350 symbol_end()351 symbol_iterator symbol_end() { return Symbols.end(); } symbol_end()352 const_symbol_iterator symbol_end() const { return Symbols.end(); } 353 symbols()354 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); } symbols()355 const_symbol_range symbols() const { 356 return make_range(symbol_begin(), symbol_end()); 357 } 358 symbol_size()359 size_t symbol_size() const { return Symbols.size(); } 360 361 /// @} 362 /// \name Indirect Symbol List Access 363 /// @{ 364 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 367 // disappear. getIndirectSymbols()368 std::vector<IndirectSymbolData> &getIndirectSymbols() { 369 return IndirectSymbols; 370 } 371 indirect_symbol_begin()372 indirect_symbol_iterator indirect_symbol_begin() { 373 return IndirectSymbols.begin(); 374 } indirect_symbol_begin()375 const_indirect_symbol_iterator indirect_symbol_begin() const { 376 return IndirectSymbols.begin(); 377 } 378 indirect_symbol_end()379 indirect_symbol_iterator indirect_symbol_end() { 380 return IndirectSymbols.end(); 381 } indirect_symbol_end()382 const_indirect_symbol_iterator indirect_symbol_end() const { 383 return IndirectSymbols.end(); 384 } 385 indirect_symbol_size()386 size_t indirect_symbol_size() const { return IndirectSymbols.size(); } 387 388 /// @} 389 /// \name Linker Option List Access 390 /// @{ 391 getLinkerOptions()392 std::vector<std::vector<std::string>> &getLinkerOptions() { 393 return LinkerOptions; 394 } 395 396 /// @} 397 /// \name Data Region List Access 398 /// @{ 399 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 402 // disappear. getDataRegions()403 std::vector<DataRegionData> &getDataRegions() { return DataRegions; } 404 data_region_begin()405 data_region_iterator data_region_begin() { return DataRegions.begin(); } data_region_begin()406 const_data_region_iterator data_region_begin() const { 407 return DataRegions.begin(); 408 } 409 data_region_end()410 data_region_iterator data_region_end() { return DataRegions.end(); } data_region_end()411 const_data_region_iterator data_region_end() const { 412 return DataRegions.end(); 413 } 414 data_region_size()415 size_t data_region_size() const { return DataRegions.size(); } 416 417 /// @} 418 /// \name Data Region List Access 419 /// @{ 420 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 423 // disappear. getLOHContainer()424 MCLOHContainer &getLOHContainer() { return LOHContainer; } getLOHContainer()425 const MCLOHContainer &getLOHContainer() const { 426 return const_cast<MCAssembler *>(this)->getLOHContainer(); 427 } 428 429 struct CGProfileEntry { 430 const MCSymbolRefExpr *From; 431 const MCSymbolRefExpr *To; 432 uint64_t Count; 433 }; 434 std::vector<CGProfileEntry> CGProfile; 435 /// @} 436 /// \name Backend Data Access 437 /// @{ 438 439 bool registerSection(MCSection &Section); 440 441 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr); 442 getFileNames()443 ArrayRef<std::string> getFileNames() { return FileNames; } 444 addFileName(StringRef FileName)445 void addFileName(StringRef FileName) { 446 if (!is_contained(FileNames, FileName)) 447 FileNames.push_back(FileName); 448 } 449 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; 454 455 /// @} 456 457 void dump() const; 458 }; 459 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, 465 uint64_t FSize); 466 467 } // end namespace llvm 468 469 #endif // LLVM_MC_MCASSEMBLER_H 470