1//===- ObjectFile.h - File format independent object file -------*- 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// This file declares a file format independent ObjectFile class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_OBJECT_OBJECTFILE_H 15#define LLVM_OBJECT_OBJECTFILE_H 16 17#include "llvm/ADT/StringRef.h" 18#include "llvm/ADT/iterator_range.h" 19#include "llvm/BinaryFormat/Magic.h" 20#include "llvm/MC/SubtargetFeature.h" 21#include "llvm/Object/Binary.h" 22#include "llvm/Object/Error.h" 23#include "llvm/Object/SymbolicFile.h" 24#include "llvm/Support/Casting.h" 25#include "llvm/Support/Error.h" 26#include "llvm/Support/ErrorOr.h" 27#include "llvm/Support/FileSystem.h" 28#include "llvm/Support/MemoryBuffer.h" 29#include <cassert> 30#include <cstdint> 31#include <memory> 32#include <system_error> 33 34namespace llvm { 35 36class ARMAttributeParser; 37 38namespace object { 39 40class COFFObjectFile; 41class MachOObjectFile; 42class ObjectFile; 43class SectionRef; 44class SymbolRef; 45class symbol_iterator; 46class WasmObjectFile; 47 48using section_iterator = content_iterator<SectionRef>; 49 50/// This is a value type class that represents a single relocation in the list 51/// of relocations in the object file. 52class RelocationRef { 53 DataRefImpl RelocationPimpl; 54 const ObjectFile *OwningObject = nullptr; 55 56public: 57 RelocationRef() = default; 58 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner); 59 60 bool operator==(const RelocationRef &Other) const; 61 62 void moveNext(); 63 64 uint64_t getOffset() const; 65 symbol_iterator getSymbol() const; 66 uint64_t getType() const; 67 68 /// @brief Get a string that represents the type of this relocation. 69 /// 70 /// This is for display purposes only. 71 void getTypeName(SmallVectorImpl<char> &Result) const; 72 73 DataRefImpl getRawDataRefImpl() const; 74 const ObjectFile *getObject() const; 75}; 76 77using relocation_iterator = content_iterator<RelocationRef>; 78 79/// This is a value type class that represents a single section in the list of 80/// sections in the object file. 81class SectionRef { 82 friend class SymbolRef; 83 84 DataRefImpl SectionPimpl; 85 const ObjectFile *OwningObject = nullptr; 86 87public: 88 SectionRef() = default; 89 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner); 90 91 bool operator==(const SectionRef &Other) const; 92 bool operator!=(const SectionRef &Other) const; 93 bool operator<(const SectionRef &Other) const; 94 95 void moveNext(); 96 97 std::error_code getName(StringRef &Result) const; 98 uint64_t getAddress() const; 99 uint64_t getIndex() const; 100 uint64_t getSize() const; 101 std::error_code getContents(StringRef &Result) const; 102 103 /// @brief Get the alignment of this section as the actual value (not log 2). 104 uint64_t getAlignment() const; 105 106 bool isCompressed() const; 107 bool isText() const; 108 bool isData() const; 109 bool isBSS() const; 110 bool isVirtual() const; 111 bool isBitcode() const; 112 113 bool containsSymbol(SymbolRef S) const; 114 115 relocation_iterator relocation_begin() const; 116 relocation_iterator relocation_end() const; 117 iterator_range<relocation_iterator> relocations() const { 118 return make_range(relocation_begin(), relocation_end()); 119 } 120 section_iterator getRelocatedSection() const; 121 122 DataRefImpl getRawDataRefImpl() const; 123 const ObjectFile *getObject() const; 124}; 125 126/// This is a value type class that represents a single symbol in the list of 127/// symbols in the object file. 128class SymbolRef : public BasicSymbolRef { 129 friend class SectionRef; 130 131public: 132 enum Type { 133 ST_Unknown, // Type not specified 134 ST_Data, 135 ST_Debug, 136 ST_File, 137 ST_Function, 138 ST_Other 139 }; 140 141 SymbolRef() = default; 142 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); 143 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) { 144 assert(isa<ObjectFile>(BasicSymbolRef::getObject())); 145 } 146 147 Expected<StringRef> getName() const; 148 /// Returns the symbol virtual address (i.e. address at which it will be 149 /// mapped). 150 Expected<uint64_t> getAddress() const; 151 152 /// Return the value of the symbol depending on the object this can be an 153 /// offset or a virtual address. 154 uint64_t getValue() const; 155 156 /// @brief Get the alignment of this symbol as the actual value (not log 2). 157 uint32_t getAlignment() const; 158 uint64_t getCommonSize() const; 159 Expected<SymbolRef::Type> getType() const; 160 161 /// @brief Get section this symbol is defined in reference to. Result is 162 /// end_sections() if it is undefined or is an absolute symbol. 163 Expected<section_iterator> getSection() const; 164 165 const ObjectFile *getObject() const; 166}; 167 168class symbol_iterator : public basic_symbol_iterator { 169public: 170 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {} 171 symbol_iterator(const basic_symbol_iterator &B) 172 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(), 173 cast<ObjectFile>(B->getObject()))) {} 174 175 const SymbolRef *operator->() const { 176 const BasicSymbolRef &P = basic_symbol_iterator::operator *(); 177 return static_cast<const SymbolRef*>(&P); 178 } 179 180 const SymbolRef &operator*() const { 181 const BasicSymbolRef &P = basic_symbol_iterator::operator *(); 182 return static_cast<const SymbolRef&>(P); 183 } 184}; 185 186/// This class is the base class for all object file types. Concrete instances 187/// of this object are created by createObjectFile, which figures out which type 188/// to create. 189class ObjectFile : public SymbolicFile { 190 virtual void anchor(); 191 192protected: 193 ObjectFile(unsigned int Type, MemoryBufferRef Source); 194 195 const uint8_t *base() const { 196 return reinterpret_cast<const uint8_t *>(Data.getBufferStart()); 197 } 198 199 // These functions are for SymbolRef to call internally. The main goal of 200 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol 201 // entry in the memory mapped object file. SymbolPimpl cannot contain any 202 // virtual functions because then it could not point into the memory mapped 203 // file. 204 // 205 // Implementations assume that the DataRefImpl is valid and has not been 206 // modified externally. It's UB otherwise. 207 friend class SymbolRef; 208 209 virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0; 210 std::error_code printSymbolName(raw_ostream &OS, 211 DataRefImpl Symb) const override; 212 virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0; 213 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0; 214 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const; 215 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0; 216 virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0; 217 virtual Expected<section_iterator> 218 getSymbolSection(DataRefImpl Symb) const = 0; 219 220 // Same as above for SectionRef. 221 friend class SectionRef; 222 223 virtual void moveSectionNext(DataRefImpl &Sec) const = 0; 224 virtual std::error_code getSectionName(DataRefImpl Sec, 225 StringRef &Res) const = 0; 226 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0; 227 virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0; 228 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0; 229 virtual std::error_code getSectionContents(DataRefImpl Sec, 230 StringRef &Res) const = 0; 231 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0; 232 virtual bool isSectionCompressed(DataRefImpl Sec) const = 0; 233 virtual bool isSectionText(DataRefImpl Sec) const = 0; 234 virtual bool isSectionData(DataRefImpl Sec) const = 0; 235 virtual bool isSectionBSS(DataRefImpl Sec) const = 0; 236 // A section is 'virtual' if its contents aren't present in the object image. 237 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0; 238 virtual bool isSectionBitcode(DataRefImpl Sec) const; 239 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0; 240 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0; 241 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const; 242 243 // Same as above for RelocationRef. 244 friend class RelocationRef; 245 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0; 246 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0; 247 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0; 248 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0; 249 virtual void getRelocationTypeName(DataRefImpl Rel, 250 SmallVectorImpl<char> &Result) const = 0; 251 252 uint64_t getSymbolValue(DataRefImpl Symb) const; 253 254public: 255 ObjectFile() = delete; 256 ObjectFile(const ObjectFile &other) = delete; 257 258 uint64_t getCommonSymbolSize(DataRefImpl Symb) const { 259 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common); 260 return getCommonSymbolSizeImpl(Symb); 261 } 262 263 using symbol_iterator_range = iterator_range<symbol_iterator>; 264 symbol_iterator_range symbols() const { 265 return symbol_iterator_range(symbol_begin(), symbol_end()); 266 } 267 268 virtual section_iterator section_begin() const = 0; 269 virtual section_iterator section_end() const = 0; 270 271 using section_iterator_range = iterator_range<section_iterator>; 272 section_iterator_range sections() const { 273 return section_iterator_range(section_begin(), section_end()); 274 } 275 276 /// @brief The number of bytes used to represent an address in this object 277 /// file format. 278 virtual uint8_t getBytesInAddress() const = 0; 279 280 virtual StringRef getFileFormatName() const = 0; 281 virtual /* Triple::ArchType */ unsigned getArch() const = 0; 282 virtual SubtargetFeatures getFeatures() const = 0; 283 virtual void setARMSubArch(Triple &TheTriple) const { } 284 285 /// Returns platform-specific object flags, if any. 286 virtual std::error_code getPlatformFlags(unsigned &Result) const { 287 Result = 0; 288 return object_error::invalid_file_type; 289 } 290 291 virtual std::error_code 292 getBuildAttributes(ARMAttributeParser &Attributes) const { 293 return std::error_code(); 294 } 295 296 /// Maps a debug section name to a standard DWARF section name. 297 virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; } 298 299 /// True if this is a relocatable object (.o/.obj). 300 virtual bool isRelocatableObject() const = 0; 301 302 /// @returns Pointer to ObjectFile subclass to handle this type of object. 303 /// @param ObjectPath The path to the object file. ObjectPath.isObject must 304 /// return true. 305 /// @brief Create ObjectFile from path. 306 static Expected<OwningBinary<ObjectFile>> 307 createObjectFile(StringRef ObjectPath); 308 309 static Expected<std::unique_ptr<ObjectFile>> 310 createObjectFile(MemoryBufferRef Object, llvm::file_magic Type); 311 static Expected<std::unique_ptr<ObjectFile>> 312 createObjectFile(MemoryBufferRef Object) { 313 return createObjectFile(Object, llvm::file_magic::unknown); 314 } 315 316 static inline bool classof(const Binary *v) { 317 return v->isObject(); 318 } 319 320 static ErrorOr<std::unique_ptr<COFFObjectFile>> 321 createCOFFObjectFile(MemoryBufferRef Object); 322 323 static ErrorOr<std::unique_ptr<ObjectFile>> 324 createELFObjectFile(MemoryBufferRef Object); 325 326 static Expected<std::unique_ptr<MachOObjectFile>> 327 createMachOObjectFile(MemoryBufferRef Object, 328 uint32_t UniversalCputype = 0, 329 uint32_t UniversalIndex = 0); 330 331 static Expected<std::unique_ptr<WasmObjectFile>> 332 createWasmObjectFile(MemoryBufferRef Object); 333}; 334 335// Inline function definitions. 336inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner) 337 : BasicSymbolRef(SymbolP, Owner) {} 338 339inline Expected<StringRef> SymbolRef::getName() const { 340 return getObject()->getSymbolName(getRawDataRefImpl()); 341} 342 343inline Expected<uint64_t> SymbolRef::getAddress() const { 344 return getObject()->getSymbolAddress(getRawDataRefImpl()); 345} 346 347inline uint64_t SymbolRef::getValue() const { 348 return getObject()->getSymbolValue(getRawDataRefImpl()); 349} 350 351inline uint32_t SymbolRef::getAlignment() const { 352 return getObject()->getSymbolAlignment(getRawDataRefImpl()); 353} 354 355inline uint64_t SymbolRef::getCommonSize() const { 356 return getObject()->getCommonSymbolSize(getRawDataRefImpl()); 357} 358 359inline Expected<section_iterator> SymbolRef::getSection() const { 360 return getObject()->getSymbolSection(getRawDataRefImpl()); 361} 362 363inline Expected<SymbolRef::Type> SymbolRef::getType() const { 364 return getObject()->getSymbolType(getRawDataRefImpl()); 365} 366 367inline const ObjectFile *SymbolRef::getObject() const { 368 const SymbolicFile *O = BasicSymbolRef::getObject(); 369 return cast<ObjectFile>(O); 370} 371 372/// SectionRef 373inline SectionRef::SectionRef(DataRefImpl SectionP, 374 const ObjectFile *Owner) 375 : SectionPimpl(SectionP) 376 , OwningObject(Owner) {} 377 378inline bool SectionRef::operator==(const SectionRef &Other) const { 379 return SectionPimpl == Other.SectionPimpl; 380} 381 382inline bool SectionRef::operator!=(const SectionRef &Other) const { 383 return SectionPimpl != Other.SectionPimpl; 384} 385 386inline bool SectionRef::operator<(const SectionRef &Other) const { 387 return SectionPimpl < Other.SectionPimpl; 388} 389 390inline void SectionRef::moveNext() { 391 return OwningObject->moveSectionNext(SectionPimpl); 392} 393 394inline std::error_code SectionRef::getName(StringRef &Result) const { 395 return OwningObject->getSectionName(SectionPimpl, Result); 396} 397 398inline uint64_t SectionRef::getAddress() const { 399 return OwningObject->getSectionAddress(SectionPimpl); 400} 401 402inline uint64_t SectionRef::getIndex() const { 403 return OwningObject->getSectionIndex(SectionPimpl); 404} 405 406inline uint64_t SectionRef::getSize() const { 407 return OwningObject->getSectionSize(SectionPimpl); 408} 409 410inline std::error_code SectionRef::getContents(StringRef &Result) const { 411 return OwningObject->getSectionContents(SectionPimpl, Result); 412} 413 414inline uint64_t SectionRef::getAlignment() const { 415 return OwningObject->getSectionAlignment(SectionPimpl); 416} 417 418inline bool SectionRef::isCompressed() const { 419 return OwningObject->isSectionCompressed(SectionPimpl); 420} 421 422inline bool SectionRef::isText() const { 423 return OwningObject->isSectionText(SectionPimpl); 424} 425 426inline bool SectionRef::isData() const { 427 return OwningObject->isSectionData(SectionPimpl); 428} 429 430inline bool SectionRef::isBSS() const { 431 return OwningObject->isSectionBSS(SectionPimpl); 432} 433 434inline bool SectionRef::isVirtual() const { 435 return OwningObject->isSectionVirtual(SectionPimpl); 436} 437 438inline bool SectionRef::isBitcode() const { 439 return OwningObject->isSectionBitcode(SectionPimpl); 440} 441 442inline relocation_iterator SectionRef::relocation_begin() const { 443 return OwningObject->section_rel_begin(SectionPimpl); 444} 445 446inline relocation_iterator SectionRef::relocation_end() const { 447 return OwningObject->section_rel_end(SectionPimpl); 448} 449 450inline section_iterator SectionRef::getRelocatedSection() const { 451 return OwningObject->getRelocatedSection(SectionPimpl); 452} 453 454inline DataRefImpl SectionRef::getRawDataRefImpl() const { 455 return SectionPimpl; 456} 457 458inline const ObjectFile *SectionRef::getObject() const { 459 return OwningObject; 460} 461 462/// RelocationRef 463inline RelocationRef::RelocationRef(DataRefImpl RelocationP, 464 const ObjectFile *Owner) 465 : RelocationPimpl(RelocationP) 466 , OwningObject(Owner) {} 467 468inline bool RelocationRef::operator==(const RelocationRef &Other) const { 469 return RelocationPimpl == Other.RelocationPimpl; 470} 471 472inline void RelocationRef::moveNext() { 473 return OwningObject->moveRelocationNext(RelocationPimpl); 474} 475 476inline uint64_t RelocationRef::getOffset() const { 477 return OwningObject->getRelocationOffset(RelocationPimpl); 478} 479 480inline symbol_iterator RelocationRef::getSymbol() const { 481 return OwningObject->getRelocationSymbol(RelocationPimpl); 482} 483 484inline uint64_t RelocationRef::getType() const { 485 return OwningObject->getRelocationType(RelocationPimpl); 486} 487 488inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const { 489 return OwningObject->getRelocationTypeName(RelocationPimpl, Result); 490} 491 492inline DataRefImpl RelocationRef::getRawDataRefImpl() const { 493 return RelocationPimpl; 494} 495 496inline const ObjectFile *RelocationRef::getObject() const { 497 return OwningObject; 498} 499 500} // end namespace object 501 502} // end namespace llvm 503 504#endif // LLVM_OBJECT_OBJECTFILE_H 505