ObjectFile.h revision 7486d92a6c949a193bb75c0ffa0170eeb2fabb80
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/Object/Binary.h" 19#include "llvm/Support/DataTypes.h" 20#include "llvm/Support/ErrorHandling.h" 21#include "llvm/Support/MemoryBuffer.h" 22#include <cstring> 23#include <vector> 24 25namespace llvm { 26namespace object { 27 28class ObjectFile; 29 30union DataRefImpl { 31 // This entire union should probably be a 32 // char[max(8, sizeof(uintptr_t))] and require the impl to cast. 33 struct { 34 uint32_t a, b; 35 } d; 36 uintptr_t p; 37 DataRefImpl() { 38 std::memset(this, 0, sizeof(DataRefImpl)); 39 } 40}; 41 42template<class content_type> 43class content_iterator { 44 content_type Current; 45public: 46 content_iterator(content_type symb) 47 : Current(symb) {} 48 49 const content_type* operator->() const { 50 return &Current; 51 } 52 53 const content_type &operator*() const { 54 return Current; 55 } 56 57 bool operator==(const content_iterator &other) const { 58 return Current == other.Current; 59 } 60 61 bool operator!=(const content_iterator &other) const { 62 return !(*this == other); 63 } 64 65 content_iterator& increment(error_code &err) { 66 content_type next; 67 if (error_code ec = Current.getNext(next)) 68 err = ec; 69 else 70 Current = next; 71 return *this; 72 } 73}; 74 75inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) { 76 // Check bitwise identical. This is the only legal way to compare a union w/o 77 // knowing which member is in use. 78 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0; 79} 80 81inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) { 82 // Check bitwise identical. This is the only legal way to compare a union w/o 83 // knowing which member is in use. 84 return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0; 85} 86 87class SymbolRef; 88 89/// RelocationRef - This is a value type class that represents a single 90/// relocation in the list of relocations in the object file. 91class RelocationRef { 92 DataRefImpl RelocationPimpl; 93 const ObjectFile *OwningObject; 94 95public: 96 RelocationRef() : OwningObject(NULL) { } 97 98 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner); 99 100 bool operator==(const RelocationRef &Other) const; 101 102 error_code getNext(RelocationRef &Result) const; 103 104 error_code getAddress(uint64_t &Result) const; 105 error_code getOffset(uint64_t &Result) const; 106 error_code getSymbol(SymbolRef &Result) const; 107 error_code getType(uint64_t &Result) const; 108 109 /// @brief Indicates whether this relocation should hidden when listing 110 /// relocations, usually because it is the trailing part of a multipart 111 /// relocation that will be printed as part of the leading relocation. 112 error_code getHidden(bool &Result) const; 113 114 /// @brief Get a string that represents the type of this relocation. 115 /// 116 /// This is for display purposes only. 117 error_code getTypeName(SmallVectorImpl<char> &Result) const; 118 119 /// @brief Get a string that represents the calculation of the value of this 120 /// relocation. 121 /// 122 /// This is for display purposes only. 123 error_code getValueString(SmallVectorImpl<char> &Result) const; 124 125 DataRefImpl getRawDataRefImpl() const; 126 const ObjectFile *getObjectFile() const; 127}; 128typedef content_iterator<RelocationRef> relocation_iterator; 129 130/// SectionRef - This is a value type class that represents a single section in 131/// the list of sections in the object file. 132class SectionRef; 133typedef content_iterator<SectionRef> section_iterator; 134class SectionRef { 135 friend class SymbolRef; 136 DataRefImpl SectionPimpl; 137 const ObjectFile *OwningObject; 138 139public: 140 SectionRef() : OwningObject(NULL) { } 141 142 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner); 143 144 bool operator==(const SectionRef &Other) const; 145 bool operator<(const SectionRef &Other) const; 146 147 error_code getNext(SectionRef &Result) const; 148 149 error_code getName(StringRef &Result) const; 150 error_code getAddress(uint64_t &Result) const; 151 error_code getSize(uint64_t &Result) const; 152 error_code getContents(StringRef &Result) const; 153 154 /// @brief Get the alignment of this section as the actual value (not log 2). 155 error_code getAlignment(uint64_t &Result) const; 156 157 // FIXME: Move to the normalization layer when it's created. 158 error_code isText(bool &Result) const; 159 error_code isData(bool &Result) const; 160 error_code isBSS(bool &Result) const; 161 error_code isRequiredForExecution(bool &Result) const; 162 error_code isVirtual(bool &Result) const; 163 error_code isZeroInit(bool &Result) const; 164 error_code isReadOnlyData(bool &Result) const; 165 166 error_code containsSymbol(SymbolRef S, bool &Result) const; 167 168 relocation_iterator begin_relocations() const; 169 relocation_iterator end_relocations() const; 170 section_iterator getRelocatedSection() const; 171 172 DataRefImpl getRawDataRefImpl() const; 173}; 174 175/// SymbolRef - This is a value type class that represents a single symbol in 176/// the list of symbols in the object file. 177class SymbolRef { 178 friend class SectionRef; 179 DataRefImpl SymbolPimpl; 180 const ObjectFile *OwningObject; 181 182public: 183 SymbolRef() : OwningObject(NULL) { } 184 185 enum Type { 186 ST_Unknown, // Type not specified 187 ST_Data, 188 ST_Debug, 189 ST_File, 190 ST_Function, 191 ST_Other 192 }; 193 194 enum Flags { 195 SF_None = 0, 196 SF_Undefined = 1U << 0, // Symbol is defined in another object file 197 SF_Global = 1U << 1, // Global symbol 198 SF_Weak = 1U << 2, // Weak symbol 199 SF_Absolute = 1U << 3, // Absolute symbol 200 SF_ThreadLocal = 1U << 4, // Thread local symbol 201 SF_Common = 1U << 5, // Symbol has common linkage 202 SF_FormatSpecific = 1U << 31 // Specific to the object file format 203 // (e.g. section symbols) 204 }; 205 206 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); 207 208 bool operator==(const SymbolRef &Other) const; 209 bool operator<(const SymbolRef &Other) const; 210 211 error_code getNext(SymbolRef &Result) const; 212 213 error_code getName(StringRef &Result) const; 214 /// Returns the symbol virtual address (i.e. address at which it will be 215 /// mapped). 216 error_code getAddress(uint64_t &Result) const; 217 error_code getFileOffset(uint64_t &Result) const; 218 /// @brief Get the alignment of this symbol as the actual value (not log 2). 219 error_code getAlignment(uint32_t &Result) const; 220 error_code getSize(uint64_t &Result) const; 221 error_code getType(SymbolRef::Type &Result) const; 222 223 /// Returns the ascii char that should be displayed in a symbol table dump via 224 /// nm for this symbol. 225 error_code getNMTypeChar(char &Result) const; 226 227 /// Get symbol flags (bitwise OR of SymbolRef::Flags) 228 error_code getFlags(uint32_t &Result) const; 229 230 /// @brief Get section this symbol is defined in reference to. Result is 231 /// end_sections() if it is undefined or is an absolute symbol. 232 error_code getSection(section_iterator &Result) const; 233 234 /// @brief Get value of the symbol in the symbol table. 235 error_code getValue(uint64_t &Val) const; 236 237 DataRefImpl getRawDataRefImpl() const; 238}; 239typedef content_iterator<SymbolRef> symbol_iterator; 240 241/// LibraryRef - This is a value type class that represents a single library in 242/// the list of libraries needed by a shared or dynamic object. 243class LibraryRef { 244 friend class SectionRef; 245 DataRefImpl LibraryPimpl; 246 const ObjectFile *OwningObject; 247 248public: 249 LibraryRef() : OwningObject(NULL) { } 250 251 LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner); 252 253 bool operator==(const LibraryRef &Other) const; 254 bool operator<(const LibraryRef &Other) const; 255 256 error_code getNext(LibraryRef &Result) const; 257 258 // Get the path to this library, as stored in the object file. 259 error_code getPath(StringRef &Result) const; 260 261 DataRefImpl getRawDataRefImpl() const; 262}; 263typedef content_iterator<LibraryRef> library_iterator; 264 265const uint64_t UnknownAddressOrSize = ~0ULL; 266 267/// ObjectFile - This class is the base class for all object file types. 268/// Concrete instances of this object are created by createObjectFile, which 269/// figures out which type to create. 270class ObjectFile : public Binary { 271 virtual void anchor(); 272 ObjectFile() LLVM_DELETED_FUNCTION; 273 ObjectFile(const ObjectFile &other) LLVM_DELETED_FUNCTION; 274 275protected: 276 ObjectFile(unsigned int Type, MemoryBuffer *source); 277 278 const uint8_t *base() const { 279 return reinterpret_cast<const uint8_t *>(Data->getBufferStart()); 280 } 281 282 // These functions are for SymbolRef to call internally. The main goal of 283 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol 284 // entry in the memory mapped object file. SymbolPimpl cannot contain any 285 // virtual functions because then it could not point into the memory mapped 286 // file. 287 // 288 // Implementations assume that the DataRefImpl is valid and has not been 289 // modified externally. It's UB otherwise. 290 friend class SymbolRef; 291 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0; 292 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0; 293 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const = 0; 294 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res)const=0; 295 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const; 296 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0; 297 virtual error_code getSymbolType(DataRefImpl Symb, 298 SymbolRef::Type &Res) const = 0; 299 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0; 300 virtual error_code getSymbolFlags(DataRefImpl Symb, 301 uint32_t &Res) const = 0; 302 virtual error_code getSymbolSection(DataRefImpl Symb, 303 section_iterator &Res) const = 0; 304 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const = 0; 305 306 // Same as above for SectionRef. 307 friend class SectionRef; 308 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0; 309 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0; 310 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0; 311 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0; 312 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0; 313 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0; 314 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0; 315 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0; 316 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0; 317 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, 318 bool &Res) const = 0; 319 // A section is 'virtual' if its contents aren't present in the object image. 320 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const = 0; 321 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const = 0; 322 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const =0; 323 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 324 bool &Result) const = 0; 325 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0; 326 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0; 327 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const; 328 329 // Same as above for RelocationRef. 330 friend class RelocationRef; 331 virtual error_code getRelocationNext(DataRefImpl Rel, 332 RelocationRef &Res) const = 0; 333 virtual error_code getRelocationAddress(DataRefImpl Rel, 334 uint64_t &Res) const =0; 335 virtual error_code getRelocationOffset(DataRefImpl Rel, 336 uint64_t &Res) const =0; 337 virtual error_code getRelocationSymbol(DataRefImpl Rel, 338 SymbolRef &Res) const = 0; 339 virtual error_code getRelocationType(DataRefImpl Rel, 340 uint64_t &Res) const = 0; 341 virtual error_code getRelocationTypeName(DataRefImpl Rel, 342 SmallVectorImpl<char> &Result) const = 0; 343 virtual error_code getRelocationValueString(DataRefImpl Rel, 344 SmallVectorImpl<char> &Result) const = 0; 345 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const { 346 Result = false; 347 return object_error::success; 348 } 349 350 // Same for LibraryRef 351 friend class LibraryRef; 352 virtual error_code getLibraryNext(DataRefImpl Lib, LibraryRef &Res) const = 0; 353 virtual error_code getLibraryPath(DataRefImpl Lib, StringRef &Res) const = 0; 354 355public: 356 357 virtual symbol_iterator begin_symbols() const = 0; 358 virtual symbol_iterator end_symbols() const = 0; 359 360 virtual symbol_iterator begin_dynamic_symbols() const = 0; 361 virtual symbol_iterator end_dynamic_symbols() const = 0; 362 363 virtual section_iterator begin_sections() const = 0; 364 virtual section_iterator end_sections() const = 0; 365 366 virtual library_iterator begin_libraries_needed() const = 0; 367 virtual library_iterator end_libraries_needed() const = 0; 368 369 /// @brief The number of bytes used to represent an address in this object 370 /// file format. 371 virtual uint8_t getBytesInAddress() const = 0; 372 373 virtual StringRef getFileFormatName() const = 0; 374 virtual /* Triple::ArchType */ unsigned getArch() const = 0; 375 376 /// For shared objects, returns the name which this object should be 377 /// loaded from at runtime. This corresponds to DT_SONAME on ELF and 378 /// LC_ID_DYLIB (install name) on MachO. 379 virtual StringRef getLoadName() const = 0; 380 381 /// @returns Pointer to ObjectFile subclass to handle this type of object. 382 /// @param ObjectPath The path to the object file. ObjectPath.isObject must 383 /// return true. 384 /// @brief Create ObjectFile from path. 385 static ObjectFile *createObjectFile(StringRef ObjectPath); 386 static ObjectFile *createObjectFile(MemoryBuffer *Object); 387 388 static inline bool classof(const Binary *v) { 389 return v->isObject(); 390 } 391 392public: 393 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object); 394 static ObjectFile *createELFObjectFile(MemoryBuffer *Object); 395 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object); 396}; 397 398// Inline function definitions. 399inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner) 400 : SymbolPimpl(SymbolP) 401 , OwningObject(Owner) {} 402 403inline bool SymbolRef::operator==(const SymbolRef &Other) const { 404 return SymbolPimpl == Other.SymbolPimpl; 405} 406 407inline bool SymbolRef::operator<(const SymbolRef &Other) const { 408 return SymbolPimpl < Other.SymbolPimpl; 409} 410 411inline error_code SymbolRef::getNext(SymbolRef &Result) const { 412 return OwningObject->getSymbolNext(SymbolPimpl, Result); 413} 414 415inline error_code SymbolRef::getName(StringRef &Result) const { 416 return OwningObject->getSymbolName(SymbolPimpl, Result); 417} 418 419inline error_code SymbolRef::getAddress(uint64_t &Result) const { 420 return OwningObject->getSymbolAddress(SymbolPimpl, Result); 421} 422 423inline error_code SymbolRef::getFileOffset(uint64_t &Result) const { 424 return OwningObject->getSymbolFileOffset(SymbolPimpl, Result); 425} 426 427inline error_code SymbolRef::getAlignment(uint32_t &Result) const { 428 return OwningObject->getSymbolAlignment(SymbolPimpl, Result); 429} 430 431inline error_code SymbolRef::getSize(uint64_t &Result) const { 432 return OwningObject->getSymbolSize(SymbolPimpl, Result); 433} 434 435inline error_code SymbolRef::getNMTypeChar(char &Result) const { 436 return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result); 437} 438 439inline error_code SymbolRef::getFlags(uint32_t &Result) const { 440 return OwningObject->getSymbolFlags(SymbolPimpl, Result); 441} 442 443inline error_code SymbolRef::getSection(section_iterator &Result) const { 444 return OwningObject->getSymbolSection(SymbolPimpl, Result); 445} 446 447inline error_code SymbolRef::getType(SymbolRef::Type &Result) const { 448 return OwningObject->getSymbolType(SymbolPimpl, Result); 449} 450 451inline error_code SymbolRef::getValue(uint64_t &Val) const { 452 return OwningObject->getSymbolValue(SymbolPimpl, Val); 453} 454 455inline DataRefImpl SymbolRef::getRawDataRefImpl() const { 456 return SymbolPimpl; 457} 458 459 460/// SectionRef 461inline SectionRef::SectionRef(DataRefImpl SectionP, 462 const ObjectFile *Owner) 463 : SectionPimpl(SectionP) 464 , OwningObject(Owner) {} 465 466inline bool SectionRef::operator==(const SectionRef &Other) const { 467 return SectionPimpl == Other.SectionPimpl; 468} 469 470inline bool SectionRef::operator<(const SectionRef &Other) const { 471 return SectionPimpl < Other.SectionPimpl; 472} 473 474inline error_code SectionRef::getNext(SectionRef &Result) const { 475 return OwningObject->getSectionNext(SectionPimpl, Result); 476} 477 478inline error_code SectionRef::getName(StringRef &Result) const { 479 return OwningObject->getSectionName(SectionPimpl, Result); 480} 481 482inline error_code SectionRef::getAddress(uint64_t &Result) const { 483 return OwningObject->getSectionAddress(SectionPimpl, Result); 484} 485 486inline error_code SectionRef::getSize(uint64_t &Result) const { 487 return OwningObject->getSectionSize(SectionPimpl, Result); 488} 489 490inline error_code SectionRef::getContents(StringRef &Result) const { 491 return OwningObject->getSectionContents(SectionPimpl, Result); 492} 493 494inline error_code SectionRef::getAlignment(uint64_t &Result) const { 495 return OwningObject->getSectionAlignment(SectionPimpl, Result); 496} 497 498inline error_code SectionRef::isText(bool &Result) const { 499 return OwningObject->isSectionText(SectionPimpl, Result); 500} 501 502inline error_code SectionRef::isData(bool &Result) const { 503 return OwningObject->isSectionData(SectionPimpl, Result); 504} 505 506inline error_code SectionRef::isBSS(bool &Result) const { 507 return OwningObject->isSectionBSS(SectionPimpl, Result); 508} 509 510inline error_code SectionRef::isRequiredForExecution(bool &Result) const { 511 return OwningObject->isSectionRequiredForExecution(SectionPimpl, Result); 512} 513 514inline error_code SectionRef::isVirtual(bool &Result) const { 515 return OwningObject->isSectionVirtual(SectionPimpl, Result); 516} 517 518inline error_code SectionRef::isZeroInit(bool &Result) const { 519 return OwningObject->isSectionZeroInit(SectionPimpl, Result); 520} 521 522inline error_code SectionRef::isReadOnlyData(bool &Result) const { 523 return OwningObject->isSectionReadOnlyData(SectionPimpl, Result); 524} 525 526inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const { 527 return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl, 528 Result); 529} 530 531inline relocation_iterator SectionRef::begin_relocations() const { 532 return OwningObject->getSectionRelBegin(SectionPimpl); 533} 534 535inline relocation_iterator SectionRef::end_relocations() const { 536 return OwningObject->getSectionRelEnd(SectionPimpl); 537} 538 539inline section_iterator SectionRef::getRelocatedSection() const { 540 return OwningObject->getRelocatedSection(SectionPimpl); 541} 542 543inline DataRefImpl SectionRef::getRawDataRefImpl() const { 544 return SectionPimpl; 545} 546 547/// RelocationRef 548inline RelocationRef::RelocationRef(DataRefImpl RelocationP, 549 const ObjectFile *Owner) 550 : RelocationPimpl(RelocationP) 551 , OwningObject(Owner) {} 552 553inline bool RelocationRef::operator==(const RelocationRef &Other) const { 554 return RelocationPimpl == Other.RelocationPimpl; 555} 556 557inline error_code RelocationRef::getNext(RelocationRef &Result) const { 558 return OwningObject->getRelocationNext(RelocationPimpl, Result); 559} 560 561inline error_code RelocationRef::getAddress(uint64_t &Result) const { 562 return OwningObject->getRelocationAddress(RelocationPimpl, Result); 563} 564 565inline error_code RelocationRef::getOffset(uint64_t &Result) const { 566 return OwningObject->getRelocationOffset(RelocationPimpl, Result); 567} 568 569inline error_code RelocationRef::getSymbol(SymbolRef &Result) const { 570 return OwningObject->getRelocationSymbol(RelocationPimpl, Result); 571} 572 573inline error_code RelocationRef::getType(uint64_t &Result) const { 574 return OwningObject->getRelocationType(RelocationPimpl, Result); 575} 576 577inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result) 578 const { 579 return OwningObject->getRelocationTypeName(RelocationPimpl, Result); 580} 581 582inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result) 583 const { 584 return OwningObject->getRelocationValueString(RelocationPimpl, Result); 585} 586 587inline error_code RelocationRef::getHidden(bool &Result) const { 588 return OwningObject->getRelocationHidden(RelocationPimpl, Result); 589} 590 591inline DataRefImpl RelocationRef::getRawDataRefImpl() const { 592 return RelocationPimpl; 593} 594 595inline const ObjectFile *RelocationRef::getObjectFile() const { 596 return OwningObject; 597} 598 599// Inline function definitions. 600inline LibraryRef::LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner) 601 : LibraryPimpl(LibraryP) 602 , OwningObject(Owner) {} 603 604inline bool LibraryRef::operator==(const LibraryRef &Other) const { 605 return LibraryPimpl == Other.LibraryPimpl; 606} 607 608inline bool LibraryRef::operator<(const LibraryRef &Other) const { 609 return LibraryPimpl < Other.LibraryPimpl; 610} 611 612inline error_code LibraryRef::getNext(LibraryRef &Result) const { 613 return OwningObject->getLibraryNext(LibraryPimpl, Result); 614} 615 616inline error_code LibraryRef::getPath(StringRef &Result) const { 617 return OwningObject->getLibraryPath(LibraryPimpl, Result); 618} 619 620} // end namespace object 621} // end namespace llvm 622 623#endif 624