ObjectFile.h revision 9b24738bd1dc791eb2c21b5562fcfe4b7b99fe2a
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_OBJECT_FILE_H 15#define LLVM_OBJECT_OBJECT_FILE_H 16 17#include "llvm/Object/Binary.h" 18#include "llvm/ADT/StringRef.h" 19#include "llvm/Support/DataTypes.h" 20#include "llvm/Support/ErrorHandling.h" 21#include "llvm/Support/MemoryBuffer.h" 22#include <cstring> 23 24namespace llvm { 25namespace object { 26 27class ObjectFile; 28 29union DataRefImpl { 30 struct { 31 // ELF needs this for relocations. This entire union should probably be a 32 // char[max(8, sizeof(uintptr_t))] and require the impl to cast. 33 uint16_t a, b; 34 uint32_t c; 35 } w; 36 struct { 37 uint32_t a, b; 38 } d; 39 uintptr_t p; 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 75static 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 81static 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 std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl)); 98 } 99 100 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner); 101 102 bool operator==(const RelocationRef &Other) const; 103 104 error_code getNext(RelocationRef &Result) const; 105 106 error_code getAddress(uint64_t &Result) const; 107 error_code getOffset(uint64_t &Result) const; 108 error_code getSymbol(SymbolRef &Result) const; 109 error_code getType(uint64_t &Result) const; 110 111 /// @brief Indicates whether this relocation should hidden when listing 112 /// relocations, usually because it is the trailing part of a multipart 113 /// relocation that will be printed as part of the leading relocation. 114 error_code getHidden(bool &Result) const; 115 116 /// @brief Get a string that represents the type of this relocation. 117 /// 118 /// This is for display purposes only. 119 error_code getTypeName(SmallVectorImpl<char> &Result) const; 120 error_code getAdditionalInfo(int64_t &Result) const; 121 122 /// @brief Get a string that represents the calculation of the value of this 123 /// relocation. 124 /// 125 /// This is for display purposes only. 126 error_code getValueString(SmallVectorImpl<char> &Result) 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 { 133 friend class SymbolRef; 134 DataRefImpl SectionPimpl; 135 const ObjectFile *OwningObject; 136 137public: 138 SectionRef() : OwningObject(NULL) { 139 std::memset(&SectionPimpl, 0, sizeof(SectionPimpl)); 140 } 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 162 error_code containsSymbol(SymbolRef S, bool &Result) const; 163 164 relocation_iterator begin_relocations() const; 165 relocation_iterator end_relocations() const; 166}; 167typedef content_iterator<SectionRef> section_iterator; 168 169/// SymbolRef - This is a value type class that represents a single symbol in 170/// the list of symbols in the object file. 171class SymbolRef { 172 friend class SectionRef; 173 DataRefImpl SymbolPimpl; 174 const ObjectFile *OwningObject; 175 176public: 177 SymbolRef() : OwningObject(NULL) { 178 std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl)); 179 } 180 181 enum Type { 182 ST_Data, 183 ST_Debug, 184 ST_External, // Defined in another object file 185 ST_File, 186 ST_Function, 187 ST_Other 188 }; 189 190 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner); 191 192 bool operator==(const SymbolRef &Other) const; 193 bool operator <(const SymbolRef &Other) const; 194 195 error_code getNext(SymbolRef &Result) const; 196 197 error_code getName(StringRef &Result) const; 198 error_code getAddress(uint64_t &Result) const; 199 error_code getFileOffset(uint64_t &Result) const; 200 error_code getSize(uint64_t &Result) const; 201 error_code getType(SymbolRef::Type &Result) const; 202 203 /// Returns the ascii char that should be displayed in a symbol table dump via 204 /// nm for this symbol. 205 error_code getNMTypeChar(char &Result) const; 206 207 /// Returns true for symbols that are internal to the object file format such 208 /// as section symbols. 209 error_code isInternal(bool &Result) const; 210 211 /// Returns true for symbols that can be used in another objects, 212 /// such as library functions 213 error_code isGlobal(bool &Result) const; 214 215 /// Returns true for weak symbols. 216 error_code isWeak(bool &Result) const; 217 218 /// @brief Return true for absolute symbols. 219 error_code isAbsolute(bool &Result) const; 220 221 /// @brief Get section this symbol is defined in reference to. Result is 222 /// end_sections() if it is undefined or is an absolute symbol. 223 error_code getSection(section_iterator &Result) const; 224 225 DataRefImpl getRawDataRefImpl() const; 226}; 227typedef content_iterator<SymbolRef> symbol_iterator; 228 229const uint64_t UnknownAddressOrSize = ~0ULL; 230 231/// ObjectFile - This class is the base class for all object file types. 232/// Concrete instances of this object are created by createObjectFile, which 233/// figure out which type to create. 234class ObjectFile : public Binary { 235private: 236 ObjectFile(); // = delete 237 ObjectFile(const ObjectFile &other); // = delete 238 239protected: 240 ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec); 241 242 const uint8_t *base() const { 243 return reinterpret_cast<const uint8_t *>(Data->getBufferStart()); 244 } 245 246 // These functions are for SymbolRef to call internally. The main goal of 247 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol 248 // entry in the memory mapped object file. SymbolPimpl cannot contain any 249 // virtual functions because then it could not point into the memory mapped 250 // file. 251 // 252 // Implementations assume that the DataRefImpl is valid and has not been 253 // modified externally. It's UB otherwise. 254 friend class SymbolRef; 255 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0; 256 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0; 257 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0; 258 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const =0; 259 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0; 260 virtual error_code getSymbolType(DataRefImpl Symb, 261 SymbolRef::Type &Res) const = 0; 262 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0; 263 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0; 264 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const = 0; 265 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const = 0; 266 virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const = 0; 267 virtual error_code getSymbolSection(DataRefImpl Symb, 268 section_iterator &Res) const = 0; 269 270 // Same as above for SectionRef. 271 friend class SectionRef; 272 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0; 273 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0; 274 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0; 275 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0; 276 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0; 277 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0; 278 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0; 279 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0; 280 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0; 281 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 282 bool &Result) const = 0; 283 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0; 284 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0; 285 286 287 // Same as above for RelocationRef. 288 friend class RelocationRef; 289 virtual error_code getRelocationNext(DataRefImpl Rel, 290 RelocationRef &Res) const = 0; 291 virtual error_code getRelocationAddress(DataRefImpl Rel, 292 uint64_t &Res) const =0; 293 virtual error_code getRelocationOffset(DataRefImpl Rel, 294 uint64_t &Res) const =0; 295 virtual error_code getRelocationSymbol(DataRefImpl Rel, 296 SymbolRef &Res) const = 0; 297 virtual error_code getRelocationType(DataRefImpl Rel, 298 uint64_t &Res) const = 0; 299 virtual error_code getRelocationTypeName(DataRefImpl Rel, 300 SmallVectorImpl<char> &Result) const = 0; 301 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, 302 int64_t &Res) const = 0; 303 virtual error_code getRelocationValueString(DataRefImpl Rel, 304 SmallVectorImpl<char> &Result) const = 0; 305 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const { 306 Result = false; 307 return object_error::success; 308 } 309 310public: 311 312 virtual symbol_iterator begin_symbols() const = 0; 313 virtual symbol_iterator end_symbols() const = 0; 314 315 virtual section_iterator begin_sections() const = 0; 316 virtual section_iterator end_sections() const = 0; 317 318 /// @brief The number of bytes used to represent an address in this object 319 /// file format. 320 virtual uint8_t getBytesInAddress() const = 0; 321 322 virtual StringRef getFileFormatName() const = 0; 323 virtual /* Triple::ArchType */ unsigned getArch() const = 0; 324 325 /// @returns Pointer to ObjectFile subclass to handle this type of object. 326 /// @param ObjectPath The path to the object file. ObjectPath.isObject must 327 /// return true. 328 /// @brief Create ObjectFile from path. 329 static ObjectFile *createObjectFile(StringRef ObjectPath); 330 static ObjectFile *createObjectFile(MemoryBuffer *Object); 331 332 static inline bool classof(const Binary *v) { 333 return v->getType() >= isObject && 334 v->getType() < lastObject; 335 } 336 static inline bool classof(const ObjectFile *v) { return true; } 337 338public: 339 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object); 340 static ObjectFile *createELFObjectFile(MemoryBuffer *Object); 341 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object); 342}; 343 344// Inline function definitions. 345inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner) 346 : SymbolPimpl(SymbolP) 347 , OwningObject(Owner) {} 348 349inline bool SymbolRef::operator==(const SymbolRef &Other) const { 350 return SymbolPimpl == Other.SymbolPimpl; 351} 352 353inline bool SymbolRef::operator <(const SymbolRef &Other) const { 354 return SymbolPimpl < Other.SymbolPimpl; 355} 356 357inline error_code SymbolRef::getNext(SymbolRef &Result) const { 358 return OwningObject->getSymbolNext(SymbolPimpl, Result); 359} 360 361inline error_code SymbolRef::getName(StringRef &Result) const { 362 return OwningObject->getSymbolName(SymbolPimpl, Result); 363} 364 365inline error_code SymbolRef::getAddress(uint64_t &Result) const { 366 return OwningObject->getSymbolAddress(SymbolPimpl, Result); 367} 368 369inline error_code SymbolRef::getFileOffset(uint64_t &Result) const { 370 return OwningObject->getSymbolFileOffset(SymbolPimpl, Result); 371} 372 373inline error_code SymbolRef::getSize(uint64_t &Result) const { 374 return OwningObject->getSymbolSize(SymbolPimpl, Result); 375} 376 377inline error_code SymbolRef::getNMTypeChar(char &Result) const { 378 return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result); 379} 380 381inline error_code SymbolRef::isInternal(bool &Result) const { 382 return OwningObject->isSymbolInternal(SymbolPimpl, Result); 383} 384 385inline error_code SymbolRef::isGlobal(bool &Result) const { 386 return OwningObject->isSymbolGlobal(SymbolPimpl, Result); 387} 388 389inline error_code SymbolRef::isWeak(bool &Result) const { 390 return OwningObject->isSymbolWeak(SymbolPimpl, Result); 391} 392 393inline error_code SymbolRef::isAbsolute(bool &Result) const { 394 return OwningObject->isSymbolAbsolute(SymbolPimpl, Result); 395} 396 397inline error_code SymbolRef::getSection(section_iterator &Result) const { 398 return OwningObject->getSymbolSection(SymbolPimpl, Result); 399} 400 401inline error_code SymbolRef::getType(SymbolRef::Type &Result) const { 402 return OwningObject->getSymbolType(SymbolPimpl, Result); 403} 404 405inline DataRefImpl SymbolRef::getRawDataRefImpl() const { 406 return SymbolPimpl; 407} 408 409 410/// SectionRef 411inline SectionRef::SectionRef(DataRefImpl SectionP, 412 const ObjectFile *Owner) 413 : SectionPimpl(SectionP) 414 , OwningObject(Owner) {} 415 416inline bool SectionRef::operator==(const SectionRef &Other) const { 417 return SectionPimpl == Other.SectionPimpl; 418} 419 420inline bool SectionRef::operator <(const SectionRef &Other) const { 421 return SectionPimpl < Other.SectionPimpl; 422} 423 424inline error_code SectionRef::getNext(SectionRef &Result) const { 425 return OwningObject->getSectionNext(SectionPimpl, Result); 426} 427 428inline error_code SectionRef::getName(StringRef &Result) const { 429 return OwningObject->getSectionName(SectionPimpl, Result); 430} 431 432inline error_code SectionRef::getAddress(uint64_t &Result) const { 433 return OwningObject->getSectionAddress(SectionPimpl, Result); 434} 435 436inline error_code SectionRef::getSize(uint64_t &Result) const { 437 return OwningObject->getSectionSize(SectionPimpl, Result); 438} 439 440inline error_code SectionRef::getContents(StringRef &Result) const { 441 return OwningObject->getSectionContents(SectionPimpl, Result); 442} 443 444inline error_code SectionRef::getAlignment(uint64_t &Result) const { 445 return OwningObject->getSectionAlignment(SectionPimpl, Result); 446} 447 448inline error_code SectionRef::isText(bool &Result) const { 449 return OwningObject->isSectionText(SectionPimpl, Result); 450} 451 452inline error_code SectionRef::isData(bool &Result) const { 453 return OwningObject->isSectionData(SectionPimpl, Result); 454} 455 456inline error_code SectionRef::isBSS(bool &Result) const { 457 return OwningObject->isSectionBSS(SectionPimpl, Result); 458} 459 460inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const { 461 return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl, 462 Result); 463} 464 465inline relocation_iterator SectionRef::begin_relocations() const { 466 return OwningObject->getSectionRelBegin(SectionPimpl); 467} 468 469inline relocation_iterator SectionRef::end_relocations() const { 470 return OwningObject->getSectionRelEnd(SectionPimpl); 471} 472 473 474/// RelocationRef 475inline RelocationRef::RelocationRef(DataRefImpl RelocationP, 476 const ObjectFile *Owner) 477 : RelocationPimpl(RelocationP) 478 , OwningObject(Owner) {} 479 480inline bool RelocationRef::operator==(const RelocationRef &Other) const { 481 return RelocationPimpl == Other.RelocationPimpl; 482} 483 484inline error_code RelocationRef::getNext(RelocationRef &Result) const { 485 return OwningObject->getRelocationNext(RelocationPimpl, Result); 486} 487 488inline error_code RelocationRef::getAddress(uint64_t &Result) const { 489 return OwningObject->getRelocationAddress(RelocationPimpl, Result); 490} 491 492inline error_code RelocationRef::getOffset(uint64_t &Result) const { 493 return OwningObject->getRelocationOffset(RelocationPimpl, Result); 494} 495 496inline error_code RelocationRef::getSymbol(SymbolRef &Result) const { 497 return OwningObject->getRelocationSymbol(RelocationPimpl, Result); 498} 499 500inline error_code RelocationRef::getType(uint64_t &Result) const { 501 return OwningObject->getRelocationType(RelocationPimpl, Result); 502} 503 504inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result) 505 const { 506 return OwningObject->getRelocationTypeName(RelocationPimpl, Result); 507} 508 509inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const { 510 return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result); 511} 512 513inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result) 514 const { 515 return OwningObject->getRelocationValueString(RelocationPimpl, Result); 516} 517 518inline error_code RelocationRef::getHidden(bool &Result) const { 519 return OwningObject->getRelocationHidden(RelocationPimpl, Result); 520} 521 522} // end namespace object 523} // end namespace llvm 524 525#endif 526