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/SmallPtrSet.h" 14 #include "llvm/ADT/ilist.h" 15 #include "llvm/ADT/ilist_node.h" 16 #include "llvm/ADT/iterator.h" 17 #include "llvm/MC/MCDirectives.h" 18 #include "llvm/MC/MCDwarf.h" 19 #include "llvm/MC/MCFixup.h" 20 #include "llvm/MC/MCFragment.h" 21 #include "llvm/MC/MCInst.h" 22 #include "llvm/MC/MCLinkerOptimizationHint.h" 23 #include "llvm/MC/MCSubtargetInfo.h" 24 #include "llvm/MC/MCSymbol.h" 25 26 namespace llvm { 27 class raw_ostream; 28 class MCAsmLayout; 29 class MCAssembler; 30 class MCContext; 31 class MCCodeEmitter; 32 class MCExpr; 33 class MCFragment; 34 class MCObjectWriter; 35 class MCSection; 36 class MCSubtargetInfo; 37 class MCValue; 38 class MCAsmBackend; 39 40 // FIXME: This really doesn't belong here. See comments below. 41 struct IndirectSymbolData { 42 MCSymbol *Symbol; 43 MCSection *Section; 44 }; 45 46 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk 47 // to one another. 48 struct DataRegionData { 49 // This enum should be kept in sync w/ the mach-o definition in 50 // llvm/Object/MachOFormat.h. 51 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind; 52 MCSymbol *Start; 53 MCSymbol *End; 54 }; 55 56 class MCAssembler { 57 friend class MCAsmLayout; 58 mutable unsigned KsError; 59 60 public: setError(unsigned E)61 void setError(unsigned E) const { KsError = E; } getError()62 unsigned getError() const { return KsError; } 63 typedef std::vector<MCSection *> SectionListType; 64 typedef std::vector<const MCSymbol *> SymbolDataListType; 65 66 typedef pointee_iterator<SectionListType::const_iterator> const_iterator; 67 typedef pointee_iterator<SectionListType::iterator> iterator; 68 69 typedef pointee_iterator<SymbolDataListType::const_iterator> 70 const_symbol_iterator; 71 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator; 72 73 typedef iterator_range<symbol_iterator> symbol_range; 74 typedef iterator_range<const_symbol_iterator> const_symbol_range; 75 76 typedef std::vector<IndirectSymbolData>::const_iterator 77 const_indirect_symbol_iterator; 78 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator; 79 80 typedef std::vector<DataRegionData>::const_iterator 81 const_data_region_iterator; 82 typedef std::vector<DataRegionData>::iterator data_region_iterator; 83 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 typedef struct { 88 MCVersionMinType Kind; 89 unsigned Major; 90 unsigned Minor; 91 unsigned Update; 92 } VersionMinInfoType; 93 94 private: 95 MCAssembler(const MCAssembler &) = delete; 96 void operator=(const MCAssembler &) = delete; 97 98 MCContext &Context; 99 100 MCAsmBackend &Backend; 101 102 MCCodeEmitter &Emitter; 103 104 MCObjectWriter &Writer; 105 106 SectionListType Sections; 107 108 SymbolDataListType Symbols; 109 110 std::vector<IndirectSymbolData> IndirectSymbols; 111 112 std::vector<DataRegionData> DataRegions; 113 114 /// The list of linker options to propagate into the object file. 115 std::vector<std::vector<std::string>> LinkerOptions; 116 117 /// List of declared file names 118 std::vector<std::string> FileNames; 119 120 MCDwarfLineTableParams LTParams; 121 122 /// The set of function symbols for which a .thumb_func directive has 123 /// been seen. 124 // 125 // FIXME: We really would like this in target specific code rather than 126 // here. Maybe when the relocation stuff moves to target specific, 127 // this can go with it? The streamer would need some target specific 128 // refactoring too. 129 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs; 130 131 /// \brief The bundle alignment size currently set in the assembler. 132 /// 133 /// By default it's 0, which means bundling is disabled. 134 unsigned BundleAlignSize; 135 136 unsigned RelaxAll : 1; 137 unsigned SubsectionsViaSymbols : 1; 138 unsigned IncrementalLinkerCompatible : 1; 139 140 /// ELF specific e_header flags 141 // It would be good if there were an MCELFAssembler class to hold this. 142 // ELF header flags are used both by the integrated and standalone assemblers. 143 // Access to the flags is necessary in cases where assembler directives affect 144 // which flags to be set. 145 unsigned ELFHeaderEFlags; 146 147 /// Used to communicate Linker Optimization Hint information between 148 /// the Streamer and the .o writer 149 MCLOHContainer LOHContainer; 150 151 VersionMinInfoType VersionMinInfo; 152 153 private: 154 /// Evaluate a fixup to a relocatable expression and the value which should be 155 /// placed into the fixup. 156 /// 157 /// \param Layout The layout to use for evaluation. 158 /// \param Fixup The fixup to evaluate. 159 /// \param DF The fragment the fixup is inside. 160 /// \param Target [out] On return, the relocatable expression the fixup 161 /// evaluates to. 162 /// \param Value [out] On return, the value of the fixup as currently laid 163 /// out. 164 /// \return Whether the fixup value was fully resolved. This is true if the 165 /// \p Value result is fixed, otherwise the value may change due to 166 /// relocation. 167 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup, 168 const MCFragment *DF, MCValue &Target, 169 uint64_t &Value, unsigned int &KsError) const; 170 171 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed 172 /// (increased in size, in order to hold its value correctly). 173 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF, 174 const MCAsmLayout &Layout, unsigned &KsError) const; 175 176 /// Check whether the given fragment needs relaxation. 177 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF, 178 const MCAsmLayout &Layout, unsigned &KsError) const; 179 180 /// \brief Perform one layout iteration and return true if any offsets 181 /// were adjusted. 182 bool layoutOnce(MCAsmLayout &Layout); 183 184 /// \brief Perform one layout iteration of the given section and return true 185 /// if any offsets were adjusted. 186 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec); 187 188 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF); 189 190 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF); 191 192 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF); 193 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout, 194 MCDwarfCallFrameFragment &DF); 195 196 /// finishLayout - Finalize a layout, including fragment lowering. 197 void finishLayout(MCAsmLayout &Layout); 198 199 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout, 200 MCFragment &F, const MCFixup &Fixup, unsigned int &KsError); 201 202 public: 203 /// Compute the effective fragment size assuming it is laid out at the given 204 /// \p SectionAddress and \p FragmentOffset. 205 uint64_t computeFragmentSize(const MCAsmLayout &Layout, 206 const MCFragment &F, bool &valid) const; 207 208 /// Find the symbol which defines the atom containing the given symbol, or 209 /// null if there is no such symbol. 210 const MCSymbol *getAtom(const MCSymbol &S) const; 211 212 /// Check whether a particular symbol is visible to the linker and is required 213 /// in the symbol table, or whether it can be discarded by the assembler. This 214 /// also effects whether the assembler treats the label as potentially 215 /// defining a separate atom. 216 bool isSymbolLinkerVisible(const MCSymbol &SD) const; 217 218 /// Emit the section contents using the given object writer. 219 void writeSectionData(const MCSection *Section, 220 const MCAsmLayout &Layout) const; 221 222 /// Check whether a given symbol has been flagged with .thumb_func. 223 bool isThumbFunc(const MCSymbol *Func) const; 224 225 /// Flag a function symbol as the target of a .thumb_func directive. setIsThumbFunc(const MCSymbol * Func)226 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); } 227 228 /// ELF e_header flags getELFHeaderEFlags()229 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; } setELFHeaderEFlags(unsigned Flags)230 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; } 231 232 /// MachO deployment target version information. getVersionMinInfo()233 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; } setVersionMinInfo(MCVersionMinType Kind,unsigned Major,unsigned Minor,unsigned Update)234 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor, 235 unsigned Update) { 236 VersionMinInfo.Kind = Kind; 237 VersionMinInfo.Major = Major; 238 VersionMinInfo.Minor = Minor; 239 VersionMinInfo.Update = Update; 240 } 241 242 public: 243 /// Construct a new assembler instance. 244 // 245 // FIXME: How are we going to parameterize this? Two obvious options are stay 246 // concrete and require clients to pass in a target like object. The other 247 // option is to make this abstract, and have targets provide concrete 248 // implementations as we do with AsmParser. 249 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, 250 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_); 251 ~MCAssembler(); 252 253 /// Reuse an assembler instance 254 /// 255 void reset(); 256 getContext()257 MCContext &getContext() const { return Context; } 258 getBackend()259 MCAsmBackend &getBackend() const { return Backend; } 260 getEmitter()261 MCCodeEmitter &getEmitter() const { return Emitter; } 262 getWriter()263 MCObjectWriter &getWriter() const { return Writer; } 264 getDWARFLinetableParams()265 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; } setDWARFLinetableParams(MCDwarfLineTableParams P)266 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; } 267 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. 271 void Finish(unsigned int &KsError); 272 273 // Layout all section and prepare them for emission. 274 void layout(MCAsmLayout &Layout, unsigned int &KsError); 275 276 // FIXME: This does not belong here. getSubsectionsViaSymbols()277 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; } setSubsectionsViaSymbols(bool Value)278 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; } 279 isIncrementalLinkerCompatible()280 bool isIncrementalLinkerCompatible() const { 281 return IncrementalLinkerCompatible; 282 } setIncrementalLinkerCompatible(bool Value)283 void setIncrementalLinkerCompatible(bool Value) { 284 IncrementalLinkerCompatible = Value; 285 } 286 getRelaxAll()287 bool getRelaxAll() const { return RelaxAll; } setRelaxAll(bool Value)288 void setRelaxAll(bool Value) { RelaxAll = Value; } 289 isBundlingEnabled()290 bool isBundlingEnabled() const { return BundleAlignSize != 0; } 291 getBundleAlignSize()292 unsigned getBundleAlignSize() const { return BundleAlignSize; } 293 setBundleAlignSize(unsigned Size)294 void setBundleAlignSize(unsigned Size) { 295 assert((Size == 0 || !(Size & (Size - 1))) && 296 "Expect a power-of-two bundle align size"); 297 BundleAlignSize = Size; 298 } 299 300 /// \name Section List Access 301 /// @{ 302 begin()303 iterator begin() { return Sections.begin(); } begin()304 const_iterator begin() const { return Sections.begin(); } 305 end()306 iterator end() { return Sections.end(); } end()307 const_iterator end() const { return Sections.end(); } 308 size()309 size_t size() const { return Sections.size(); } 310 311 /// @} 312 /// \name Symbol List Access 313 /// @{ symbol_begin()314 symbol_iterator symbol_begin() { return Symbols.begin(); } symbol_begin()315 const_symbol_iterator symbol_begin() const { return Symbols.begin(); } 316 symbol_end()317 symbol_iterator symbol_end() { return Symbols.end(); } symbol_end()318 const_symbol_iterator symbol_end() const { return Symbols.end(); } 319 symbols()320 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); } symbols()321 const_symbol_range symbols() const { 322 return make_range(symbol_begin(), symbol_end()); 323 } 324 symbol_size()325 size_t symbol_size() const { return Symbols.size(); } 326 327 /// @} 328 /// \name Indirect Symbol List Access 329 /// @{ 330 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 333 // disappear. getIndirectSymbols()334 std::vector<IndirectSymbolData> &getIndirectSymbols() { 335 return IndirectSymbols; 336 } 337 indirect_symbol_begin()338 indirect_symbol_iterator indirect_symbol_begin() { 339 return IndirectSymbols.begin(); 340 } indirect_symbol_begin()341 const_indirect_symbol_iterator indirect_symbol_begin() const { 342 return IndirectSymbols.begin(); 343 } 344 indirect_symbol_end()345 indirect_symbol_iterator indirect_symbol_end() { 346 return IndirectSymbols.end(); 347 } indirect_symbol_end()348 const_indirect_symbol_iterator indirect_symbol_end() const { 349 return IndirectSymbols.end(); 350 } 351 indirect_symbol_size()352 size_t indirect_symbol_size() const { return IndirectSymbols.size(); } 353 354 /// @} 355 /// \name Linker Option List Access 356 /// @{ 357 getLinkerOptions()358 std::vector<std::vector<std::string>> &getLinkerOptions() { 359 return LinkerOptions; 360 } 361 362 /// @} 363 /// \name Data Region List Access 364 /// @{ 365 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 368 // disappear. getDataRegions()369 std::vector<DataRegionData> &getDataRegions() { return DataRegions; } 370 data_region_begin()371 data_region_iterator data_region_begin() { return DataRegions.begin(); } data_region_begin()372 const_data_region_iterator data_region_begin() const { 373 return DataRegions.begin(); 374 } 375 data_region_end()376 data_region_iterator data_region_end() { return DataRegions.end(); } data_region_end()377 const_data_region_iterator data_region_end() const { 378 return DataRegions.end(); 379 } 380 data_region_size()381 size_t data_region_size() const { return DataRegions.size(); } 382 383 /// @} 384 /// \name Data Region List Access 385 /// @{ 386 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 389 // disappear. getLOHContainer()390 MCLOHContainer &getLOHContainer() { return LOHContainer; } getLOHContainer()391 const MCLOHContainer &getLOHContainer() const { 392 return const_cast<MCAssembler *>(this)->getLOHContainer(); 393 } 394 /// @} 395 /// \name Backend Data Access 396 /// @{ 397 398 bool registerSection(MCSection &Section); 399 400 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr); 401 getFileNames()402 ArrayRef<std::string> getFileNames() { return FileNames; } 403 addFileName(StringRef FileName)404 void addFileName(StringRef FileName) { 405 if (std::find(FileNames.begin(), FileNames.end(), FileName) == 406 FileNames.end()) 407 FileNames.push_back(FileName); 408 } 409 410 /// \brief Write the necessary bundle padding to the given object writer. 411 /// Expects a fragment \p F containing instructions and its size \p FSize. 412 void writeFragmentPadding(const MCFragment &F, uint64_t FSize, 413 MCObjectWriter *OW) const; 414 415 /// @} 416 417 void dump(); 418 }; 419 420 /// \brief Compute the amount of padding required before the fragment \p F to 421 /// obey bundling restrictions, where \p FOffset is the fragment's offset in 422 /// its section and \p FSize is the fragment's size. 423 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F, 424 uint64_t FOffset, uint64_t FSize); 425 426 } // end namespace llvm 427 428 #endif 429