1 //===- COFFObjectFile.cpp - COFF object file implementation -----*- 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 the COFFObjectFile class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Object/COFF.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/StringSwitch.h" 18 #include "llvm/ADT/Triple.h" 19 #include "llvm/Support/COFF.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include <cctype> 23 #include <limits> 24 25 using namespace llvm; 26 using namespace object; 27 28 using support::ulittle16_t; 29 using support::ulittle32_t; 30 using support::ulittle64_t; 31 using support::little16_t; 32 33 // Returns false if size is greater than the buffer size. And sets ec. 34 static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) { 35 if (M.getBufferSize() < Size) { 36 EC = object_error::unexpected_eof; 37 return false; 38 } 39 return true; 40 } 41 42 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. 43 // Returns unexpected_eof if error. 44 template <typename T> 45 static std::error_code getObject(const T *&Obj, MemoryBufferRef M, 46 const uint8_t *Ptr, 47 const size_t Size = sizeof(T)) { 48 uintptr_t Addr = uintptr_t(Ptr); 49 if (Addr + Size < Addr || Addr + Size < Size || 50 Addr + Size > uintptr_t(M.getBufferEnd())) { 51 return object_error::unexpected_eof; 52 } 53 Obj = reinterpret_cast<const T *>(Addr); 54 return object_error::success; 55 } 56 57 // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without 58 // prefixed slashes. 59 static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) { 60 assert(Str.size() <= 6 && "String too long, possible overflow."); 61 if (Str.size() > 6) 62 return true; 63 64 uint64_t Value = 0; 65 while (!Str.empty()) { 66 unsigned CharVal; 67 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25 68 CharVal = Str[0] - 'A'; 69 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51 70 CharVal = Str[0] - 'a' + 26; 71 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61 72 CharVal = Str[0] - '0' + 52; 73 else if (Str[0] == '+') // 62 74 CharVal = 62; 75 else if (Str[0] == '/') // 63 76 CharVal = 63; 77 else 78 return true; 79 80 Value = (Value * 64) + CharVal; 81 Str = Str.substr(1); 82 } 83 84 if (Value > std::numeric_limits<uint32_t>::max()) 85 return true; 86 87 Result = static_cast<uint32_t>(Value); 88 return false; 89 } 90 91 template <typename coff_symbol_type> 92 const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const { 93 const coff_symbol_type *Addr = 94 reinterpret_cast<const coff_symbol_type *>(Ref.p); 95 96 #ifndef NDEBUG 97 // Verify that the symbol points to a valid entry in the symbol table. 98 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); 99 if (Offset < getPointerToSymbolTable() || 100 Offset >= getPointerToSymbolTable() + 101 (getNumberOfSymbols() * sizeof(coff_symbol_type))) 102 report_fatal_error("Symbol was outside of symbol table."); 103 104 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 && 105 "Symbol did not point to the beginning of a symbol"); 106 #endif 107 108 return Addr; 109 } 110 111 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { 112 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); 113 114 # ifndef NDEBUG 115 // Verify that the section points to a valid entry in the section table. 116 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections())) 117 report_fatal_error("Section was outside of section table."); 118 119 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); 120 assert(Offset % sizeof(coff_section) == 0 && 121 "Section did not point to the beginning of a section"); 122 # endif 123 124 return Addr; 125 } 126 127 void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { 128 if (SymbolTable16) { 129 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref); 130 Symb += 1 + Symb->NumberOfAuxSymbols; 131 Ref.p = reinterpret_cast<uintptr_t>(Symb); 132 } else if (SymbolTable32) { 133 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref); 134 Symb += 1 + Symb->NumberOfAuxSymbols; 135 Ref.p = reinterpret_cast<uintptr_t>(Symb); 136 } else { 137 llvm_unreachable("no symbol table pointer!"); 138 } 139 } 140 141 std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref, 142 StringRef &Result) const { 143 COFFSymbolRef Symb = getCOFFSymbol(Ref); 144 return getSymbolName(Symb, Result); 145 } 146 147 std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref, 148 uint64_t &Result) const { 149 COFFSymbolRef Symb = getCOFFSymbol(Ref); 150 const coff_section *Section = nullptr; 151 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section)) 152 return EC; 153 154 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) 155 Result = UnknownAddressOrSize; 156 else if (Section) 157 Result = Section->VirtualAddress + Symb.getValue(); 158 else 159 Result = Symb.getValue(); 160 return object_error::success; 161 } 162 163 std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref, 164 SymbolRef::Type &Result) const { 165 COFFSymbolRef Symb = getCOFFSymbol(Ref); 166 Result = SymbolRef::ST_Other; 167 168 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL && 169 Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) { 170 Result = SymbolRef::ST_Unknown; 171 } else if (Symb.isFunctionDefinition()) { 172 Result = SymbolRef::ST_Function; 173 } else { 174 uint32_t Characteristics = 0; 175 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) { 176 const coff_section *Section = nullptr; 177 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section)) 178 return EC; 179 Characteristics = Section->Characteristics; 180 } 181 if (Characteristics & COFF::IMAGE_SCN_MEM_READ && 182 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. 183 Result = SymbolRef::ST_Data; 184 } 185 return object_error::success; 186 } 187 188 uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { 189 COFFSymbolRef Symb = getCOFFSymbol(Ref); 190 uint32_t Result = SymbolRef::SF_None; 191 192 // TODO: Correctly set SF_FormatSpecific, SF_Common 193 194 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) { 195 if (Symb.getValue() == 0) 196 Result |= SymbolRef::SF_Undefined; 197 else 198 Result |= SymbolRef::SF_Common; 199 } 200 201 202 // TODO: This are certainly too restrictive. 203 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL) 204 Result |= SymbolRef::SF_Global; 205 206 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) 207 Result |= SymbolRef::SF_Weak; 208 209 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE) 210 Result |= SymbolRef::SF_Absolute; 211 212 return Result; 213 } 214 215 std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref, 216 uint64_t &Result) const { 217 // FIXME: Return the correct size. This requires looking at all the symbols 218 // in the same section as this symbol, and looking for either the next 219 // symbol, or the end of the section. 220 COFFSymbolRef Symb = getCOFFSymbol(Ref); 221 const coff_section *Section = nullptr; 222 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section)) 223 return EC; 224 225 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) 226 Result = UnknownAddressOrSize; 227 else if (Section) 228 Result = Section->SizeOfRawData - Symb.getValue(); 229 else 230 Result = 0; 231 return object_error::success; 232 } 233 234 std::error_code 235 COFFObjectFile::getSymbolSection(DataRefImpl Ref, 236 section_iterator &Result) const { 237 COFFSymbolRef Symb = getCOFFSymbol(Ref); 238 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) { 239 Result = section_end(); 240 } else { 241 const coff_section *Sec = nullptr; 242 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec)) 243 return EC; 244 DataRefImpl Ref; 245 Ref.p = reinterpret_cast<uintptr_t>(Sec); 246 Result = section_iterator(SectionRef(Ref, this)); 247 } 248 return object_error::success; 249 } 250 251 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { 252 const coff_section *Sec = toSec(Ref); 253 Sec += 1; 254 Ref.p = reinterpret_cast<uintptr_t>(Sec); 255 } 256 257 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref, 258 StringRef &Result) const { 259 const coff_section *Sec = toSec(Ref); 260 return getSectionName(Sec, Result); 261 } 262 263 std::error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, 264 uint64_t &Result) const { 265 const coff_section *Sec = toSec(Ref); 266 Result = Sec->VirtualAddress; 267 return object_error::success; 268 } 269 270 std::error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, 271 uint64_t &Result) const { 272 const coff_section *Sec = toSec(Ref); 273 Result = Sec->SizeOfRawData; 274 return object_error::success; 275 } 276 277 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, 278 StringRef &Result) const { 279 const coff_section *Sec = toSec(Ref); 280 ArrayRef<uint8_t> Res; 281 std::error_code EC = getSectionContents(Sec, Res); 282 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); 283 return EC; 284 } 285 286 std::error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, 287 uint64_t &Res) const { 288 const coff_section *Sec = toSec(Ref); 289 if (!Sec) 290 return object_error::parse_failed; 291 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); 292 return object_error::success; 293 } 294 295 std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref, 296 bool &Result) const { 297 const coff_section *Sec = toSec(Ref); 298 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 299 return object_error::success; 300 } 301 302 std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref, 303 bool &Result) const { 304 const coff_section *Sec = toSec(Ref); 305 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 306 return object_error::success; 307 } 308 309 std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, 310 bool &Result) const { 311 const coff_section *Sec = toSec(Ref); 312 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 313 return object_error::success; 314 } 315 316 std::error_code 317 COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref, 318 bool &Result) const { 319 // FIXME: Unimplemented 320 Result = true; 321 return object_error::success; 322 } 323 324 std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, 325 bool &Result) const { 326 const coff_section *Sec = toSec(Ref); 327 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 328 return object_error::success; 329 } 330 331 std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, 332 bool &Result) const { 333 // FIXME: Unimplemented. 334 Result = false; 335 return object_error::success; 336 } 337 338 std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, 339 bool &Result) const { 340 // FIXME: Unimplemented. 341 Result = false; 342 return object_error::success; 343 } 344 345 std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, 346 DataRefImpl SymbRef, 347 bool &Result) const { 348 const coff_section *Sec = toSec(SecRef); 349 COFFSymbolRef Symb = getCOFFSymbol(SymbRef); 350 int32_t SecNumber = (Sec - SectionTable) + 1; 351 Result = SecNumber == Symb.getSectionNumber(); 352 return object_error::success; 353 } 354 355 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 356 const coff_section *Sec = toSec(Ref); 357 DataRefImpl Ret; 358 if (Sec->NumberOfRelocations == 0) { 359 Ret.p = 0; 360 } else { 361 auto begin = reinterpret_cast<const coff_relocation*>( 362 base() + Sec->PointerToRelocations); 363 if (Sec->hasExtendedRelocations()) { 364 // Skip the first relocation entry repurposed to store the number of 365 // relocations. 366 begin++; 367 } 368 Ret.p = reinterpret_cast<uintptr_t>(begin); 369 } 370 return relocation_iterator(RelocationRef(Ret, this)); 371 } 372 373 static uint32_t getNumberOfRelocations(const coff_section *Sec, 374 const uint8_t *base) { 375 // The field for the number of relocations in COFF section table is only 376 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to 377 // NumberOfRelocations field, and the actual relocation count is stored in the 378 // VirtualAddress field in the first relocation entry. 379 if (Sec->hasExtendedRelocations()) { 380 auto *FirstReloc = reinterpret_cast<const coff_relocation*>( 381 base + Sec->PointerToRelocations); 382 return FirstReloc->VirtualAddress; 383 } 384 return Sec->NumberOfRelocations; 385 } 386 387 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 388 const coff_section *Sec = toSec(Ref); 389 DataRefImpl Ret; 390 if (Sec->NumberOfRelocations == 0) { 391 Ret.p = 0; 392 } else { 393 auto begin = reinterpret_cast<const coff_relocation*>( 394 base() + Sec->PointerToRelocations); 395 uint32_t NumReloc = getNumberOfRelocations(Sec, base()); 396 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc); 397 } 398 return relocation_iterator(RelocationRef(Ret, this)); 399 } 400 401 // Initialize the pointer to the symbol table. 402 std::error_code COFFObjectFile::initSymbolTablePtr() { 403 if (COFFHeader) 404 if (std::error_code EC = 405 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(), 406 getNumberOfSymbols() * getSymbolTableEntrySize())) 407 return EC; 408 409 if (COFFBigObjHeader) 410 if (std::error_code EC = 411 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(), 412 getNumberOfSymbols() * getSymbolTableEntrySize())) 413 return EC; 414 415 // Find string table. The first four byte of the string table contains the 416 // total size of the string table, including the size field itself. If the 417 // string table is empty, the value of the first four byte would be 4. 418 const uint8_t *StringTableAddr = 419 base() + getPointerToSymbolTable() + 420 getNumberOfSymbols() * getSymbolTableEntrySize(); 421 const ulittle32_t *StringTableSizePtr; 422 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 423 return EC; 424 StringTableSize = *StringTableSizePtr; 425 if (std::error_code EC = 426 getObject(StringTable, Data, StringTableAddr, StringTableSize)) 427 return EC; 428 429 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some 430 // tools like cvtres write a size of 0 for an empty table instead of 4. 431 if (StringTableSize < 4) 432 StringTableSize = 4; 433 434 // Check that the string table is null terminated if has any in it. 435 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) 436 return object_error::parse_failed; 437 return object_error::success; 438 } 439 440 // Returns the file offset for the given VA. 441 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { 442 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase 443 : (uint64_t)PE32PlusHeader->ImageBase; 444 uint64_t Rva = Addr - ImageBase; 445 assert(Rva <= UINT32_MAX); 446 return getRvaPtr((uint32_t)Rva, Res); 447 } 448 449 // Returns the file offset for the given RVA. 450 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { 451 for (const SectionRef &S : sections()) { 452 const coff_section *Section = getCOFFSection(S); 453 uint32_t SectionStart = Section->VirtualAddress; 454 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 455 if (SectionStart <= Addr && Addr < SectionEnd) { 456 uint32_t Offset = Addr - SectionStart; 457 Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 458 return object_error::success; 459 } 460 } 461 return object_error::parse_failed; 462 } 463 464 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 465 // table entry. 466 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint, 467 StringRef &Name) const { 468 uintptr_t IntPtr = 0; 469 if (std::error_code EC = getRvaPtr(Rva, IntPtr)) 470 return EC; 471 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 472 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 473 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 474 return object_error::success; 475 } 476 477 // Find the import table. 478 std::error_code COFFObjectFile::initImportTablePtr() { 479 // First, we get the RVA of the import table. If the file lacks a pointer to 480 // the import table, do nothing. 481 const data_directory *DataEntry; 482 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 483 return object_error::success; 484 485 // Do nothing if the pointer to import table is NULL. 486 if (DataEntry->RelativeVirtualAddress == 0) 487 return object_error::success; 488 489 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 490 // -1 because the last entry is the null entry. 491 NumberOfImportDirectory = DataEntry->Size / 492 sizeof(import_directory_table_entry) - 1; 493 494 // Find the section that contains the RVA. This is needed because the RVA is 495 // the import table's memory address which is different from its file offset. 496 uintptr_t IntPtr = 0; 497 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 498 return EC; 499 ImportDirectory = reinterpret_cast< 500 const import_directory_table_entry *>(IntPtr); 501 return object_error::success; 502 } 503 504 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory. 505 std::error_code COFFObjectFile::initDelayImportTablePtr() { 506 const data_directory *DataEntry; 507 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry)) 508 return object_error::success; 509 if (DataEntry->RelativeVirtualAddress == 0) 510 return object_error::success; 511 512 uint32_t RVA = DataEntry->RelativeVirtualAddress; 513 NumberOfDelayImportDirectory = DataEntry->Size / 514 sizeof(delay_import_directory_table_entry) - 1; 515 516 uintptr_t IntPtr = 0; 517 if (std::error_code EC = getRvaPtr(RVA, IntPtr)) 518 return EC; 519 DelayImportDirectory = reinterpret_cast< 520 const delay_import_directory_table_entry *>(IntPtr); 521 return object_error::success; 522 } 523 524 // Find the export table. 525 std::error_code COFFObjectFile::initExportTablePtr() { 526 // First, we get the RVA of the export table. If the file lacks a pointer to 527 // the export table, do nothing. 528 const data_directory *DataEntry; 529 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 530 return object_error::success; 531 532 // Do nothing if the pointer to export table is NULL. 533 if (DataEntry->RelativeVirtualAddress == 0) 534 return object_error::success; 535 536 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 537 uintptr_t IntPtr = 0; 538 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 539 return EC; 540 ExportDirectory = 541 reinterpret_cast<const export_directory_table_entry *>(IntPtr); 542 return object_error::success; 543 } 544 545 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC) 546 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr), 547 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr), 548 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr), 549 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0), 550 ImportDirectory(nullptr), NumberOfImportDirectory(0), 551 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0), 552 ExportDirectory(nullptr) { 553 // Check that we at least have enough room for a header. 554 if (!checkSize(Data, EC, sizeof(coff_file_header))) 555 return; 556 557 // The current location in the file where we are looking at. 558 uint64_t CurPtr = 0; 559 560 // PE header is optional and is present only in executables. If it exists, 561 // it is placed right after COFF header. 562 bool HasPEHeader = false; 563 564 // Check if this is a PE/COFF file. 565 if (base()[0] == 0x4d && base()[1] == 0x5a) { 566 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 567 // PE signature to find 'normal' COFF header. 568 if (!checkSize(Data, EC, 0x3c + 8)) 569 return; 570 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); 571 // Check the PE magic bytes. ("PE\0\0") 572 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 573 0) { 574 EC = object_error::parse_failed; 575 return; 576 } 577 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes. 578 HasPEHeader = true; 579 } 580 581 if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 582 return; 583 584 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF 585 // import libraries share a common prefix but bigobj is more restrictive. 586 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN && 587 COFFHeader->NumberOfSections == uint16_t(0xffff) && 588 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) { 589 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr))) 590 return; 591 592 // Verify that we are dealing with bigobj. 593 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion && 594 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic, 595 sizeof(COFF::BigObjMagic)) == 0) { 596 COFFHeader = nullptr; 597 CurPtr += sizeof(coff_bigobj_file_header); 598 } else { 599 // It's not a bigobj. 600 COFFBigObjHeader = nullptr; 601 } 602 } 603 if (COFFHeader) { 604 // The prior checkSize call may have failed. This isn't a hard error 605 // because we were just trying to sniff out bigobj. 606 EC = object_error::success; 607 CurPtr += sizeof(coff_file_header); 608 609 if (COFFHeader->isImportLibrary()) 610 return; 611 } 612 613 if (HasPEHeader) { 614 const pe32_header *Header; 615 if ((EC = getObject(Header, Data, base() + CurPtr))) 616 return; 617 618 const uint8_t *DataDirAddr; 619 uint64_t DataDirSize; 620 if (Header->Magic == 0x10b) { 621 PE32Header = Header; 622 DataDirAddr = base() + CurPtr + sizeof(pe32_header); 623 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 624 } else if (Header->Magic == 0x20b) { 625 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); 626 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); 627 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; 628 } else { 629 // It's neither PE32 nor PE32+. 630 EC = object_error::parse_failed; 631 return; 632 } 633 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) 634 return; 635 CurPtr += COFFHeader->SizeOfOptionalHeader; 636 } 637 638 if ((EC = getObject(SectionTable, Data, base() + CurPtr, 639 getNumberOfSections() * sizeof(coff_section)))) 640 return; 641 642 // Initialize the pointer to the symbol table. 643 if (getPointerToSymbolTable() != 0) 644 if ((EC = initSymbolTablePtr())) 645 return; 646 647 // Initialize the pointer to the beginning of the import table. 648 if ((EC = initImportTablePtr())) 649 return; 650 if ((EC = initDelayImportTablePtr())) 651 return; 652 653 // Initialize the pointer to the export table. 654 if ((EC = initExportTablePtr())) 655 return; 656 657 EC = object_error::success; 658 } 659 660 basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const { 661 DataRefImpl Ret; 662 Ret.p = getSymbolTable(); 663 return basic_symbol_iterator(SymbolRef(Ret, this)); 664 } 665 666 basic_symbol_iterator COFFObjectFile::symbol_end_impl() const { 667 // The symbol table ends where the string table begins. 668 DataRefImpl Ret; 669 Ret.p = reinterpret_cast<uintptr_t>(StringTable); 670 return basic_symbol_iterator(SymbolRef(Ret, this)); 671 } 672 673 import_directory_iterator COFFObjectFile::import_directory_begin() const { 674 return import_directory_iterator( 675 ImportDirectoryEntryRef(ImportDirectory, 0, this)); 676 } 677 678 import_directory_iterator COFFObjectFile::import_directory_end() const { 679 return import_directory_iterator( 680 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); 681 } 682 683 delay_import_directory_iterator 684 COFFObjectFile::delay_import_directory_begin() const { 685 return delay_import_directory_iterator( 686 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this)); 687 } 688 689 delay_import_directory_iterator 690 COFFObjectFile::delay_import_directory_end() const { 691 return delay_import_directory_iterator( 692 DelayImportDirectoryEntryRef( 693 DelayImportDirectory, NumberOfDelayImportDirectory, this)); 694 } 695 696 export_directory_iterator COFFObjectFile::export_directory_begin() const { 697 return export_directory_iterator( 698 ExportDirectoryEntryRef(ExportDirectory, 0, this)); 699 } 700 701 export_directory_iterator COFFObjectFile::export_directory_end() const { 702 if (!ExportDirectory) 703 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this)); 704 ExportDirectoryEntryRef Ref(ExportDirectory, 705 ExportDirectory->AddressTableEntries, this); 706 return export_directory_iterator(Ref); 707 } 708 709 section_iterator COFFObjectFile::section_begin() const { 710 DataRefImpl Ret; 711 Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 712 return section_iterator(SectionRef(Ret, this)); 713 } 714 715 section_iterator COFFObjectFile::section_end() const { 716 DataRefImpl Ret; 717 int NumSections = 718 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections(); 719 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 720 return section_iterator(SectionRef(Ret, this)); 721 } 722 723 uint8_t COFFObjectFile::getBytesInAddress() const { 724 return getArch() == Triple::x86_64 ? 8 : 4; 725 } 726 727 StringRef COFFObjectFile::getFileFormatName() const { 728 switch(getMachine()) { 729 case COFF::IMAGE_FILE_MACHINE_I386: 730 return "COFF-i386"; 731 case COFF::IMAGE_FILE_MACHINE_AMD64: 732 return "COFF-x86-64"; 733 case COFF::IMAGE_FILE_MACHINE_ARMNT: 734 return "COFF-ARM"; 735 default: 736 return "COFF-<unknown arch>"; 737 } 738 } 739 740 unsigned COFFObjectFile::getArch() const { 741 switch (getMachine()) { 742 case COFF::IMAGE_FILE_MACHINE_I386: 743 return Triple::x86; 744 case COFF::IMAGE_FILE_MACHINE_AMD64: 745 return Triple::x86_64; 746 case COFF::IMAGE_FILE_MACHINE_ARMNT: 747 return Triple::thumb; 748 default: 749 return Triple::UnknownArch; 750 } 751 } 752 753 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 754 Res = PE32Header; 755 return object_error::success; 756 } 757 758 std::error_code 759 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { 760 Res = PE32PlusHeader; 761 return object_error::success; 762 } 763 764 std::error_code 765 COFFObjectFile::getDataDirectory(uint32_t Index, 766 const data_directory *&Res) const { 767 // Error if if there's no data directory or the index is out of range. 768 if (!DataDirectory) 769 return object_error::parse_failed; 770 assert(PE32Header || PE32PlusHeader); 771 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize 772 : PE32PlusHeader->NumberOfRvaAndSize; 773 if (Index > NumEnt) 774 return object_error::parse_failed; 775 Res = &DataDirectory[Index]; 776 return object_error::success; 777 } 778 779 std::error_code COFFObjectFile::getSection(int32_t Index, 780 const coff_section *&Result) const { 781 // Check for special index values. 782 if (COFF::isReservedSectionNumber(Index)) 783 Result = nullptr; 784 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections()) 785 // We already verified the section table data, so no need to check again. 786 Result = SectionTable + (Index - 1); 787 else 788 return object_error::parse_failed; 789 return object_error::success; 790 } 791 792 std::error_code COFFObjectFile::getString(uint32_t Offset, 793 StringRef &Result) const { 794 if (StringTableSize <= 4) 795 // Tried to get a string from an empty string table. 796 return object_error::parse_failed; 797 if (Offset >= StringTableSize) 798 return object_error::unexpected_eof; 799 Result = StringRef(StringTable + Offset); 800 return object_error::success; 801 } 802 803 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol, 804 StringRef &Res) const { 805 // Check for string table entry. First 4 bytes are 0. 806 if (Symbol.getStringTableOffset().Zeroes == 0) { 807 uint32_t Offset = Symbol.getStringTableOffset().Offset; 808 if (std::error_code EC = getString(Offset, Res)) 809 return EC; 810 return object_error::success; 811 } 812 813 if (Symbol.getShortName()[COFF::NameSize - 1] == 0) 814 // Null terminated, let ::strlen figure out the length. 815 Res = StringRef(Symbol.getShortName()); 816 else 817 // Not null terminated, use all 8 bytes. 818 Res = StringRef(Symbol.getShortName(), COFF::NameSize); 819 return object_error::success; 820 } 821 822 ArrayRef<uint8_t> 823 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const { 824 const uint8_t *Aux = nullptr; 825 826 size_t SymbolSize = getSymbolTableEntrySize(); 827 if (Symbol.getNumberOfAuxSymbols() > 0) { 828 // AUX data comes immediately after the symbol in COFF 829 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize; 830 # ifndef NDEBUG 831 // Verify that the Aux symbol points to a valid entry in the symbol table. 832 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 833 if (Offset < getPointerToSymbolTable() || 834 Offset >= 835 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize)) 836 report_fatal_error("Aux Symbol data was outside of symbol table."); 837 838 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && 839 "Aux Symbol data did not point to the beginning of a symbol"); 840 # endif 841 } 842 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize); 843 } 844 845 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec, 846 StringRef &Res) const { 847 StringRef Name; 848 if (Sec->Name[COFF::NameSize - 1] == 0) 849 // Null terminated, let ::strlen figure out the length. 850 Name = Sec->Name; 851 else 852 // Not null terminated, use all 8 bytes. 853 Name = StringRef(Sec->Name, COFF::NameSize); 854 855 // Check for string table entry. First byte is '/'. 856 if (Name[0] == '/') { 857 uint32_t Offset; 858 if (Name[1] == '/') { 859 if (decodeBase64StringEntry(Name.substr(2), Offset)) 860 return object_error::parse_failed; 861 } else { 862 if (Name.substr(1).getAsInteger(10, Offset)) 863 return object_error::parse_failed; 864 } 865 if (std::error_code EC = getString(Offset, Name)) 866 return EC; 867 } 868 869 Res = Name; 870 return object_error::success; 871 } 872 873 std::error_code 874 COFFObjectFile::getSectionContents(const coff_section *Sec, 875 ArrayRef<uint8_t> &Res) const { 876 // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't 877 // do anything interesting for them. 878 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 && 879 "BSS sections don't have contents!"); 880 // The only thing that we need to verify is that the contents is contained 881 // within the file bounds. We don't need to make sure it doesn't cover other 882 // data, as there's nothing that says that is not allowed. 883 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 884 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; 885 if (ConEnd > uintptr_t(Data.getBufferEnd())) 886 return object_error::parse_failed; 887 Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart), 888 Sec->SizeOfRawData); 889 return object_error::success; 890 } 891 892 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 893 return reinterpret_cast<const coff_relocation*>(Rel.p); 894 } 895 896 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 897 Rel.p = reinterpret_cast<uintptr_t>( 898 reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 899 } 900 901 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, 902 uint64_t &Res) const { 903 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); 904 } 905 906 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, 907 uint64_t &Res) const { 908 Res = toRel(Rel)->VirtualAddress; 909 return object_error::success; 910 } 911 912 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 913 const coff_relocation *R = toRel(Rel); 914 DataRefImpl Ref; 915 if (SymbolTable16) 916 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex); 917 else if (SymbolTable32) 918 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex); 919 else 920 llvm_unreachable("no symbol table pointer!"); 921 return symbol_iterator(SymbolRef(Ref, this)); 922 } 923 924 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, 925 uint64_t &Res) const { 926 const coff_relocation* R = toRel(Rel); 927 Res = R->Type; 928 return object_error::success; 929 } 930 931 const coff_section * 932 COFFObjectFile::getCOFFSection(const SectionRef &Section) const { 933 return toSec(Section.getRawDataRefImpl()); 934 } 935 936 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const { 937 if (SymbolTable16) 938 return toSymb<coff_symbol16>(Ref); 939 if (SymbolTable32) 940 return toSymb<coff_symbol32>(Ref); 941 llvm_unreachable("no symbol table pointer!"); 942 } 943 944 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { 945 return getCOFFSymbol(Symbol.getRawDataRefImpl()); 946 } 947 948 const coff_relocation * 949 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { 950 return toRel(Reloc.getRawDataRefImpl()); 951 } 952 953 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ 954 case COFF::reloc_type: \ 955 Res = #reloc_type; \ 956 break; 957 958 std::error_code 959 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, 960 SmallVectorImpl<char> &Result) const { 961 const coff_relocation *Reloc = toRel(Rel); 962 StringRef Res; 963 switch (getMachine()) { 964 case COFF::IMAGE_FILE_MACHINE_AMD64: 965 switch (Reloc->Type) { 966 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 967 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 968 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 969 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 970 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 971 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 972 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 973 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 974 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 975 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 976 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 977 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 978 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 979 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 980 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 983 default: 984 Res = "Unknown"; 985 } 986 break; 987 case COFF::IMAGE_FILE_MACHINE_ARMNT: 988 switch (Reloc->Type) { 989 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE); 990 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32); 991 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB); 992 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24); 993 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11); 994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN); 995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24); 996 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11); 997 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION); 998 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL); 999 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A); 1000 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T); 1001 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T); 1002 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T); 1003 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T); 1004 default: 1005 Res = "Unknown"; 1006 } 1007 break; 1008 case COFF::IMAGE_FILE_MACHINE_I386: 1009 switch (Reloc->Type) { 1010 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 1011 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 1012 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 1013 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 1014 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 1015 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 1016 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 1017 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 1018 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 1019 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 1020 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 1021 default: 1022 Res = "Unknown"; 1023 } 1024 break; 1025 default: 1026 Res = "Unknown"; 1027 } 1028 Result.append(Res.begin(), Res.end()); 1029 return object_error::success; 1030 } 1031 1032 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 1033 1034 std::error_code 1035 COFFObjectFile::getRelocationValueString(DataRefImpl Rel, 1036 SmallVectorImpl<char> &Result) const { 1037 const coff_relocation *Reloc = toRel(Rel); 1038 DataRefImpl Sym; 1039 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex); 1040 if (std::error_code EC = Symb.getError()) 1041 return EC; 1042 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr()); 1043 StringRef SymName; 1044 if (std::error_code EC = getSymbolName(Sym, SymName)) 1045 return EC; 1046 Result.append(SymName.begin(), SymName.end()); 1047 return object_error::success; 1048 } 1049 1050 bool COFFObjectFile::isRelocatableObject() const { 1051 return !DataDirectory; 1052 } 1053 1054 bool ImportDirectoryEntryRef:: 1055 operator==(const ImportDirectoryEntryRef &Other) const { 1056 return ImportTable == Other.ImportTable && Index == Other.Index; 1057 } 1058 1059 void ImportDirectoryEntryRef::moveNext() { 1060 ++Index; 1061 } 1062 1063 std::error_code ImportDirectoryEntryRef::getImportTableEntry( 1064 const import_directory_table_entry *&Result) const { 1065 Result = ImportTable + Index; 1066 return object_error::success; 1067 } 1068 1069 static imported_symbol_iterator 1070 makeImportedSymbolIterator(const COFFObjectFile *Object, 1071 uintptr_t Ptr, int Index) { 1072 if (Object->getBytesInAddress() == 4) { 1073 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr); 1074 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1075 } 1076 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr); 1077 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1078 } 1079 1080 static imported_symbol_iterator 1081 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) { 1082 uintptr_t IntPtr = 0; 1083 Object->getRvaPtr(RVA, IntPtr); 1084 return makeImportedSymbolIterator(Object, IntPtr, 0); 1085 } 1086 1087 static imported_symbol_iterator 1088 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) { 1089 uintptr_t IntPtr = 0; 1090 Object->getRvaPtr(RVA, IntPtr); 1091 // Forward the pointer to the last entry which is null. 1092 int Index = 0; 1093 if (Object->getBytesInAddress() == 4) { 1094 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr); 1095 while (*Entry++) 1096 ++Index; 1097 } else { 1098 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr); 1099 while (*Entry++) 1100 ++Index; 1101 } 1102 return makeImportedSymbolIterator(Object, IntPtr, Index); 1103 } 1104 1105 imported_symbol_iterator 1106 ImportDirectoryEntryRef::imported_symbol_begin() const { 1107 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA, 1108 OwningObject); 1109 } 1110 1111 imported_symbol_iterator 1112 ImportDirectoryEntryRef::imported_symbol_end() const { 1113 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA, 1114 OwningObject); 1115 } 1116 1117 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 1118 uintptr_t IntPtr = 0; 1119 if (std::error_code EC = 1120 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr)) 1121 return EC; 1122 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1123 return object_error::success; 1124 } 1125 1126 std::error_code 1127 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const { 1128 Result = ImportTable[Index].ImportLookupTableRVA; 1129 return object_error::success; 1130 } 1131 1132 std::error_code 1133 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const { 1134 Result = ImportTable[Index].ImportAddressTableRVA; 1135 return object_error::success; 1136 } 1137 1138 std::error_code ImportDirectoryEntryRef::getImportLookupEntry( 1139 const import_lookup_table_entry32 *&Result) const { 1140 uintptr_t IntPtr = 0; 1141 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA; 1142 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1143 return EC; 1144 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); 1145 return object_error::success; 1146 } 1147 1148 bool DelayImportDirectoryEntryRef:: 1149 operator==(const DelayImportDirectoryEntryRef &Other) const { 1150 return Table == Other.Table && Index == Other.Index; 1151 } 1152 1153 void DelayImportDirectoryEntryRef::moveNext() { 1154 ++Index; 1155 } 1156 1157 imported_symbol_iterator 1158 DelayImportDirectoryEntryRef::imported_symbol_begin() const { 1159 return importedSymbolBegin(Table[Index].DelayImportNameTable, 1160 OwningObject); 1161 } 1162 1163 imported_symbol_iterator 1164 DelayImportDirectoryEntryRef::imported_symbol_end() const { 1165 return importedSymbolEnd(Table[Index].DelayImportNameTable, 1166 OwningObject); 1167 } 1168 1169 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const { 1170 uintptr_t IntPtr = 0; 1171 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr)) 1172 return EC; 1173 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1174 return object_error::success; 1175 } 1176 1177 std::error_code DelayImportDirectoryEntryRef:: 1178 getDelayImportTable(const delay_import_directory_table_entry *&Result) const { 1179 Result = Table; 1180 return object_error::success; 1181 } 1182 1183 bool ExportDirectoryEntryRef:: 1184 operator==(const ExportDirectoryEntryRef &Other) const { 1185 return ExportTable == Other.ExportTable && Index == Other.Index; 1186 } 1187 1188 void ExportDirectoryEntryRef::moveNext() { 1189 ++Index; 1190 } 1191 1192 // Returns the name of the current export symbol. If the symbol is exported only 1193 // by ordinal, the empty string is set as a result. 1194 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { 1195 uintptr_t IntPtr = 0; 1196 if (std::error_code EC = 1197 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) 1198 return EC; 1199 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1200 return object_error::success; 1201 } 1202 1203 // Returns the starting ordinal number. 1204 std::error_code 1205 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { 1206 Result = ExportTable->OrdinalBase; 1207 return object_error::success; 1208 } 1209 1210 // Returns the export ordinal of the current export symbol. 1211 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 1212 Result = ExportTable->OrdinalBase + Index; 1213 return object_error::success; 1214 } 1215 1216 // Returns the address of the current export symbol. 1217 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 1218 uintptr_t IntPtr = 0; 1219 if (std::error_code EC = 1220 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) 1221 return EC; 1222 const export_address_table_entry *entry = 1223 reinterpret_cast<const export_address_table_entry *>(IntPtr); 1224 Result = entry[Index].ExportRVA; 1225 return object_error::success; 1226 } 1227 1228 // Returns the name of the current export symbol. If the symbol is exported only 1229 // by ordinal, the empty string is set as a result. 1230 std::error_code 1231 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { 1232 uintptr_t IntPtr = 0; 1233 if (std::error_code EC = 1234 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) 1235 return EC; 1236 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 1237 1238 uint32_t NumEntries = ExportTable->NumberOfNamePointers; 1239 int Offset = 0; 1240 for (const ulittle16_t *I = Start, *E = Start + NumEntries; 1241 I < E; ++I, ++Offset) { 1242 if (*I != Index) 1243 continue; 1244 if (std::error_code EC = 1245 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) 1246 return EC; 1247 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 1248 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 1249 return EC; 1250 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1251 return object_error::success; 1252 } 1253 Result = ""; 1254 return object_error::success; 1255 } 1256 1257 bool ImportedSymbolRef:: 1258 operator==(const ImportedSymbolRef &Other) const { 1259 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64 1260 && Index == Other.Index; 1261 } 1262 1263 void ImportedSymbolRef::moveNext() { 1264 ++Index; 1265 } 1266 1267 std::error_code 1268 ImportedSymbolRef::getSymbolName(StringRef &Result) const { 1269 uint32_t RVA; 1270 if (Entry32) { 1271 // If a symbol is imported only by ordinal, it has no name. 1272 if (Entry32[Index].isOrdinal()) 1273 return object_error::success; 1274 RVA = Entry32[Index].getHintNameRVA(); 1275 } else { 1276 if (Entry64[Index].isOrdinal()) 1277 return object_error::success; 1278 RVA = Entry64[Index].getHintNameRVA(); 1279 } 1280 uintptr_t IntPtr = 0; 1281 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1282 return EC; 1283 // +2 because the first two bytes is hint. 1284 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2)); 1285 return object_error::success; 1286 } 1287 1288 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const { 1289 uint32_t RVA; 1290 if (Entry32) { 1291 if (Entry32[Index].isOrdinal()) { 1292 Result = Entry32[Index].getOrdinal(); 1293 return object_error::success; 1294 } 1295 RVA = Entry32[Index].getHintNameRVA(); 1296 } else { 1297 if (Entry64[Index].isOrdinal()) { 1298 Result = Entry64[Index].getOrdinal(); 1299 return object_error::success; 1300 } 1301 RVA = Entry64[Index].getHintNameRVA(); 1302 } 1303 uintptr_t IntPtr = 0; 1304 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1305 return EC; 1306 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr); 1307 return object_error::success; 1308 } 1309 1310 ErrorOr<std::unique_ptr<COFFObjectFile>> 1311 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) { 1312 std::error_code EC; 1313 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC)); 1314 if (EC) 1315 return EC; 1316 return std::move(Ret); 1317 } 1318