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 const coff_section *SymbSec = nullptr; 351 if (std::error_code EC = getSection(Symb.getSectionNumber(), SymbSec)) 352 return EC; 353 if (SymbSec == Sec) 354 Result = true; 355 else 356 Result = false; 357 return object_error::success; 358 } 359 360 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 361 const coff_section *Sec = toSec(Ref); 362 DataRefImpl Ret; 363 if (Sec->NumberOfRelocations == 0) { 364 Ret.p = 0; 365 } else { 366 auto begin = reinterpret_cast<const coff_relocation*>( 367 base() + Sec->PointerToRelocations); 368 if (Sec->hasExtendedRelocations()) { 369 // Skip the first relocation entry repurposed to store the number of 370 // relocations. 371 begin++; 372 } 373 Ret.p = reinterpret_cast<uintptr_t>(begin); 374 } 375 return relocation_iterator(RelocationRef(Ret, this)); 376 } 377 378 static uint32_t getNumberOfRelocations(const coff_section *Sec, 379 const uint8_t *base) { 380 // The field for the number of relocations in COFF section table is only 381 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to 382 // NumberOfRelocations field, and the actual relocation count is stored in the 383 // VirtualAddress field in the first relocation entry. 384 if (Sec->hasExtendedRelocations()) { 385 auto *FirstReloc = reinterpret_cast<const coff_relocation*>( 386 base + Sec->PointerToRelocations); 387 return FirstReloc->VirtualAddress; 388 } 389 return Sec->NumberOfRelocations; 390 } 391 392 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 393 const coff_section *Sec = toSec(Ref); 394 DataRefImpl Ret; 395 if (Sec->NumberOfRelocations == 0) { 396 Ret.p = 0; 397 } else { 398 auto begin = reinterpret_cast<const coff_relocation*>( 399 base() + Sec->PointerToRelocations); 400 uint32_t NumReloc = getNumberOfRelocations(Sec, base()); 401 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc); 402 } 403 return relocation_iterator(RelocationRef(Ret, this)); 404 } 405 406 // Initialize the pointer to the symbol table. 407 std::error_code COFFObjectFile::initSymbolTablePtr() { 408 if (COFFHeader) 409 if (std::error_code EC = 410 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(), 411 getNumberOfSymbols() * getSymbolTableEntrySize())) 412 return EC; 413 414 if (COFFBigObjHeader) 415 if (std::error_code EC = 416 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(), 417 getNumberOfSymbols() * getSymbolTableEntrySize())) 418 return EC; 419 420 // Find string table. The first four byte of the string table contains the 421 // total size of the string table, including the size field itself. If the 422 // string table is empty, the value of the first four byte would be 4. 423 const uint8_t *StringTableAddr = 424 base() + getPointerToSymbolTable() + 425 getNumberOfSymbols() * getSymbolTableEntrySize(); 426 const ulittle32_t *StringTableSizePtr; 427 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 428 return EC; 429 StringTableSize = *StringTableSizePtr; 430 if (std::error_code EC = 431 getObject(StringTable, Data, StringTableAddr, StringTableSize)) 432 return EC; 433 434 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some 435 // tools like cvtres write a size of 0 for an empty table instead of 4. 436 if (StringTableSize < 4) 437 StringTableSize = 4; 438 439 // Check that the string table is null terminated if has any in it. 440 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) 441 return object_error::parse_failed; 442 return object_error::success; 443 } 444 445 // Returns the file offset for the given VA. 446 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { 447 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase 448 : (uint64_t)PE32PlusHeader->ImageBase; 449 uint64_t Rva = Addr - ImageBase; 450 assert(Rva <= UINT32_MAX); 451 return getRvaPtr((uint32_t)Rva, Res); 452 } 453 454 // Returns the file offset for the given RVA. 455 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { 456 for (const SectionRef &S : sections()) { 457 const coff_section *Section = getCOFFSection(S); 458 uint32_t SectionStart = Section->VirtualAddress; 459 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 460 if (SectionStart <= Addr && Addr < SectionEnd) { 461 uint32_t Offset = Addr - SectionStart; 462 Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 463 return object_error::success; 464 } 465 } 466 return object_error::parse_failed; 467 } 468 469 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 470 // table entry. 471 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint, 472 StringRef &Name) const { 473 uintptr_t IntPtr = 0; 474 if (std::error_code EC = getRvaPtr(Rva, IntPtr)) 475 return EC; 476 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 477 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 478 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 479 return object_error::success; 480 } 481 482 // Find the import table. 483 std::error_code COFFObjectFile::initImportTablePtr() { 484 // First, we get the RVA of the import table. If the file lacks a pointer to 485 // the import table, do nothing. 486 const data_directory *DataEntry; 487 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 488 return object_error::success; 489 490 // Do nothing if the pointer to import table is NULL. 491 if (DataEntry->RelativeVirtualAddress == 0) 492 return object_error::success; 493 494 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 495 // -1 because the last entry is the null entry. 496 NumberOfImportDirectory = DataEntry->Size / 497 sizeof(import_directory_table_entry) - 1; 498 499 // Find the section that contains the RVA. This is needed because the RVA is 500 // the import table's memory address which is different from its file offset. 501 uintptr_t IntPtr = 0; 502 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 503 return EC; 504 ImportDirectory = reinterpret_cast< 505 const import_directory_table_entry *>(IntPtr); 506 return object_error::success; 507 } 508 509 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory. 510 std::error_code COFFObjectFile::initDelayImportTablePtr() { 511 const data_directory *DataEntry; 512 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry)) 513 return object_error::success; 514 if (DataEntry->RelativeVirtualAddress == 0) 515 return object_error::success; 516 517 uint32_t RVA = DataEntry->RelativeVirtualAddress; 518 NumberOfDelayImportDirectory = DataEntry->Size / 519 sizeof(delay_import_directory_table_entry) - 1; 520 521 uintptr_t IntPtr = 0; 522 if (std::error_code EC = getRvaPtr(RVA, IntPtr)) 523 return EC; 524 DelayImportDirectory = reinterpret_cast< 525 const delay_import_directory_table_entry *>(IntPtr); 526 return object_error::success; 527 } 528 529 // Find the export table. 530 std::error_code COFFObjectFile::initExportTablePtr() { 531 // First, we get the RVA of the export table. If the file lacks a pointer to 532 // the export table, do nothing. 533 const data_directory *DataEntry; 534 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 535 return object_error::success; 536 537 // Do nothing if the pointer to export table is NULL. 538 if (DataEntry->RelativeVirtualAddress == 0) 539 return object_error::success; 540 541 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 542 uintptr_t IntPtr = 0; 543 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 544 return EC; 545 ExportDirectory = 546 reinterpret_cast<const export_directory_table_entry *>(IntPtr); 547 return object_error::success; 548 } 549 550 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC) 551 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr), 552 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr), 553 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr), 554 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0), 555 ImportDirectory(nullptr), NumberOfImportDirectory(0), 556 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0), 557 ExportDirectory(nullptr) { 558 // Check that we at least have enough room for a header. 559 if (!checkSize(Data, EC, sizeof(coff_file_header))) 560 return; 561 562 // The current location in the file where we are looking at. 563 uint64_t CurPtr = 0; 564 565 // PE header is optional and is present only in executables. If it exists, 566 // it is placed right after COFF header. 567 bool HasPEHeader = false; 568 569 // Check if this is a PE/COFF file. 570 if (base()[0] == 0x4d && base()[1] == 0x5a) { 571 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 572 // PE signature to find 'normal' COFF header. 573 if (!checkSize(Data, EC, 0x3c + 8)) 574 return; 575 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); 576 // Check the PE magic bytes. ("PE\0\0") 577 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 578 0) { 579 EC = object_error::parse_failed; 580 return; 581 } 582 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes. 583 HasPEHeader = true; 584 } 585 586 if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 587 return; 588 589 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF 590 // import libraries share a common prefix but bigobj is more restrictive. 591 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN && 592 COFFHeader->NumberOfSections == uint16_t(0xffff) && 593 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) { 594 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr))) 595 return; 596 597 // Verify that we are dealing with bigobj. 598 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion && 599 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic, 600 sizeof(COFF::BigObjMagic)) == 0) { 601 COFFHeader = nullptr; 602 CurPtr += sizeof(coff_bigobj_file_header); 603 } else { 604 // It's not a bigobj. 605 COFFBigObjHeader = nullptr; 606 } 607 } 608 if (COFFHeader) { 609 // The prior checkSize call may have failed. This isn't a hard error 610 // because we were just trying to sniff out bigobj. 611 EC = object_error::success; 612 CurPtr += sizeof(coff_file_header); 613 614 if (COFFHeader->isImportLibrary()) 615 return; 616 } 617 618 if (HasPEHeader) { 619 const pe32_header *Header; 620 if ((EC = getObject(Header, Data, base() + CurPtr))) 621 return; 622 623 const uint8_t *DataDirAddr; 624 uint64_t DataDirSize; 625 if (Header->Magic == 0x10b) { 626 PE32Header = Header; 627 DataDirAddr = base() + CurPtr + sizeof(pe32_header); 628 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 629 } else if (Header->Magic == 0x20b) { 630 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); 631 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); 632 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; 633 } else { 634 // It's neither PE32 nor PE32+. 635 EC = object_error::parse_failed; 636 return; 637 } 638 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) 639 return; 640 CurPtr += COFFHeader->SizeOfOptionalHeader; 641 } 642 643 if ((EC = getObject(SectionTable, Data, base() + CurPtr, 644 getNumberOfSections() * sizeof(coff_section)))) 645 return; 646 647 // Initialize the pointer to the symbol table. 648 if (getPointerToSymbolTable() != 0) 649 if ((EC = initSymbolTablePtr())) 650 return; 651 652 // Initialize the pointer to the beginning of the import table. 653 if ((EC = initImportTablePtr())) 654 return; 655 if ((EC = initDelayImportTablePtr())) 656 return; 657 658 // Initialize the pointer to the export table. 659 if ((EC = initExportTablePtr())) 660 return; 661 662 EC = object_error::success; 663 } 664 665 basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const { 666 DataRefImpl Ret; 667 Ret.p = getSymbolTable(); 668 return basic_symbol_iterator(SymbolRef(Ret, this)); 669 } 670 671 basic_symbol_iterator COFFObjectFile::symbol_end_impl() const { 672 // The symbol table ends where the string table begins. 673 DataRefImpl Ret; 674 Ret.p = reinterpret_cast<uintptr_t>(StringTable); 675 return basic_symbol_iterator(SymbolRef(Ret, this)); 676 } 677 678 import_directory_iterator COFFObjectFile::import_directory_begin() const { 679 return import_directory_iterator( 680 ImportDirectoryEntryRef(ImportDirectory, 0, this)); 681 } 682 683 import_directory_iterator COFFObjectFile::import_directory_end() const { 684 return import_directory_iterator( 685 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); 686 } 687 688 delay_import_directory_iterator 689 COFFObjectFile::delay_import_directory_begin() const { 690 return delay_import_directory_iterator( 691 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this)); 692 } 693 694 delay_import_directory_iterator 695 COFFObjectFile::delay_import_directory_end() const { 696 return delay_import_directory_iterator( 697 DelayImportDirectoryEntryRef( 698 DelayImportDirectory, NumberOfDelayImportDirectory, this)); 699 } 700 701 export_directory_iterator COFFObjectFile::export_directory_begin() const { 702 return export_directory_iterator( 703 ExportDirectoryEntryRef(ExportDirectory, 0, this)); 704 } 705 706 export_directory_iterator COFFObjectFile::export_directory_end() const { 707 if (!ExportDirectory) 708 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this)); 709 ExportDirectoryEntryRef Ref(ExportDirectory, 710 ExportDirectory->AddressTableEntries, this); 711 return export_directory_iterator(Ref); 712 } 713 714 section_iterator COFFObjectFile::section_begin() const { 715 DataRefImpl Ret; 716 Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 717 return section_iterator(SectionRef(Ret, this)); 718 } 719 720 section_iterator COFFObjectFile::section_end() const { 721 DataRefImpl Ret; 722 int NumSections = 723 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections(); 724 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 725 return section_iterator(SectionRef(Ret, this)); 726 } 727 728 uint8_t COFFObjectFile::getBytesInAddress() const { 729 return getArch() == Triple::x86_64 ? 8 : 4; 730 } 731 732 StringRef COFFObjectFile::getFileFormatName() const { 733 switch(getMachine()) { 734 case COFF::IMAGE_FILE_MACHINE_I386: 735 return "COFF-i386"; 736 case COFF::IMAGE_FILE_MACHINE_AMD64: 737 return "COFF-x86-64"; 738 case COFF::IMAGE_FILE_MACHINE_ARMNT: 739 return "COFF-ARM"; 740 default: 741 return "COFF-<unknown arch>"; 742 } 743 } 744 745 unsigned COFFObjectFile::getArch() const { 746 switch (getMachine()) { 747 case COFF::IMAGE_FILE_MACHINE_I386: 748 return Triple::x86; 749 case COFF::IMAGE_FILE_MACHINE_AMD64: 750 return Triple::x86_64; 751 case COFF::IMAGE_FILE_MACHINE_ARMNT: 752 return Triple::thumb; 753 default: 754 return Triple::UnknownArch; 755 } 756 } 757 758 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 759 Res = PE32Header; 760 return object_error::success; 761 } 762 763 std::error_code 764 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { 765 Res = PE32PlusHeader; 766 return object_error::success; 767 } 768 769 std::error_code 770 COFFObjectFile::getDataDirectory(uint32_t Index, 771 const data_directory *&Res) const { 772 // Error if if there's no data directory or the index is out of range. 773 if (!DataDirectory) 774 return object_error::parse_failed; 775 assert(PE32Header || PE32PlusHeader); 776 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize 777 : PE32PlusHeader->NumberOfRvaAndSize; 778 if (Index > NumEnt) 779 return object_error::parse_failed; 780 Res = &DataDirectory[Index]; 781 return object_error::success; 782 } 783 784 std::error_code COFFObjectFile::getSection(int32_t Index, 785 const coff_section *&Result) const { 786 // Check for special index values. 787 if (COFF::isReservedSectionNumber(Index)) 788 Result = nullptr; 789 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections()) 790 // We already verified the section table data, so no need to check again. 791 Result = SectionTable + (Index - 1); 792 else 793 return object_error::parse_failed; 794 return object_error::success; 795 } 796 797 std::error_code COFFObjectFile::getString(uint32_t Offset, 798 StringRef &Result) const { 799 if (StringTableSize <= 4) 800 // Tried to get a string from an empty string table. 801 return object_error::parse_failed; 802 if (Offset >= StringTableSize) 803 return object_error::unexpected_eof; 804 Result = StringRef(StringTable + Offset); 805 return object_error::success; 806 } 807 808 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol, 809 StringRef &Res) const { 810 // Check for string table entry. First 4 bytes are 0. 811 if (Symbol.getStringTableOffset().Zeroes == 0) { 812 uint32_t Offset = Symbol.getStringTableOffset().Offset; 813 if (std::error_code EC = getString(Offset, Res)) 814 return EC; 815 return object_error::success; 816 } 817 818 if (Symbol.getShortName()[COFF::NameSize - 1] == 0) 819 // Null terminated, let ::strlen figure out the length. 820 Res = StringRef(Symbol.getShortName()); 821 else 822 // Not null terminated, use all 8 bytes. 823 Res = StringRef(Symbol.getShortName(), COFF::NameSize); 824 return object_error::success; 825 } 826 827 ArrayRef<uint8_t> 828 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const { 829 const uint8_t *Aux = nullptr; 830 831 size_t SymbolSize = getSymbolTableEntrySize(); 832 if (Symbol.getNumberOfAuxSymbols() > 0) { 833 // AUX data comes immediately after the symbol in COFF 834 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize; 835 # ifndef NDEBUG 836 // Verify that the Aux symbol points to a valid entry in the symbol table. 837 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 838 if (Offset < getPointerToSymbolTable() || 839 Offset >= 840 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize)) 841 report_fatal_error("Aux Symbol data was outside of symbol table."); 842 843 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && 844 "Aux Symbol data did not point to the beginning of a symbol"); 845 # endif 846 } 847 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize); 848 } 849 850 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec, 851 StringRef &Res) const { 852 StringRef Name; 853 if (Sec->Name[COFF::NameSize - 1] == 0) 854 // Null terminated, let ::strlen figure out the length. 855 Name = Sec->Name; 856 else 857 // Not null terminated, use all 8 bytes. 858 Name = StringRef(Sec->Name, COFF::NameSize); 859 860 // Check for string table entry. First byte is '/'. 861 if (Name[0] == '/') { 862 uint32_t Offset; 863 if (Name[1] == '/') { 864 if (decodeBase64StringEntry(Name.substr(2), Offset)) 865 return object_error::parse_failed; 866 } else { 867 if (Name.substr(1).getAsInteger(10, Offset)) 868 return object_error::parse_failed; 869 } 870 if (std::error_code EC = getString(Offset, Name)) 871 return EC; 872 } 873 874 Res = Name; 875 return object_error::success; 876 } 877 878 std::error_code 879 COFFObjectFile::getSectionContents(const coff_section *Sec, 880 ArrayRef<uint8_t> &Res) const { 881 // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't 882 // do anything interesting for them. 883 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 && 884 "BSS sections don't have contents!"); 885 // The only thing that we need to verify is that the contents is contained 886 // within the file bounds. We don't need to make sure it doesn't cover other 887 // data, as there's nothing that says that is not allowed. 888 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 889 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; 890 if (ConEnd > uintptr_t(Data.getBufferEnd())) 891 return object_error::parse_failed; 892 Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart), 893 Sec->SizeOfRawData); 894 return object_error::success; 895 } 896 897 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 898 return reinterpret_cast<const coff_relocation*>(Rel.p); 899 } 900 901 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 902 Rel.p = reinterpret_cast<uintptr_t>( 903 reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 904 } 905 906 std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, 907 uint64_t &Res) const { 908 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); 909 } 910 911 std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, 912 uint64_t &Res) const { 913 Res = toRel(Rel)->VirtualAddress; 914 return object_error::success; 915 } 916 917 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 918 const coff_relocation *R = toRel(Rel); 919 DataRefImpl Ref; 920 if (SymbolTable16) 921 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex); 922 else if (SymbolTable32) 923 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex); 924 else 925 llvm_unreachable("no symbol table pointer!"); 926 return symbol_iterator(SymbolRef(Ref, this)); 927 } 928 929 std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, 930 uint64_t &Res) const { 931 const coff_relocation* R = toRel(Rel); 932 Res = R->Type; 933 return object_error::success; 934 } 935 936 const coff_section * 937 COFFObjectFile::getCOFFSection(const SectionRef &Section) const { 938 return toSec(Section.getRawDataRefImpl()); 939 } 940 941 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const { 942 if (SymbolTable16) 943 return toSymb<coff_symbol16>(Ref); 944 if (SymbolTable32) 945 return toSymb<coff_symbol32>(Ref); 946 llvm_unreachable("no symbol table pointer!"); 947 } 948 949 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { 950 return getCOFFSymbol(Symbol.getRawDataRefImpl()); 951 } 952 953 const coff_relocation * 954 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { 955 return toRel(Reloc.getRawDataRefImpl()); 956 } 957 958 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ 959 case COFF::reloc_type: \ 960 Res = #reloc_type; \ 961 break; 962 963 std::error_code 964 COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, 965 SmallVectorImpl<char> &Result) const { 966 const coff_relocation *Reloc = toRel(Rel); 967 StringRef Res; 968 switch (getMachine()) { 969 case COFF::IMAGE_FILE_MACHINE_AMD64: 970 switch (Reloc->Type) { 971 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 972 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 973 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 974 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 975 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 976 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 977 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 978 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 979 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 980 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 983 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 984 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 985 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 986 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 988 default: 989 Res = "Unknown"; 990 } 991 break; 992 case COFF::IMAGE_FILE_MACHINE_ARMNT: 993 switch (Reloc->Type) { 994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE); 995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32); 996 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB); 997 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24); 998 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11); 999 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN); 1000 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24); 1001 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11); 1002 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION); 1003 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL); 1004 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A); 1005 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T); 1006 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T); 1007 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T); 1008 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T); 1009 default: 1010 Res = "Unknown"; 1011 } 1012 break; 1013 case COFF::IMAGE_FILE_MACHINE_I386: 1014 switch (Reloc->Type) { 1015 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 1016 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 1017 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 1018 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 1019 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 1020 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 1021 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 1022 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 1023 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 1024 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 1025 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 1026 default: 1027 Res = "Unknown"; 1028 } 1029 break; 1030 default: 1031 Res = "Unknown"; 1032 } 1033 Result.append(Res.begin(), Res.end()); 1034 return object_error::success; 1035 } 1036 1037 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 1038 1039 std::error_code 1040 COFFObjectFile::getRelocationValueString(DataRefImpl Rel, 1041 SmallVectorImpl<char> &Result) const { 1042 const coff_relocation *Reloc = toRel(Rel); 1043 DataRefImpl Sym; 1044 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex); 1045 if (std::error_code EC = Symb.getError()) 1046 return EC; 1047 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr()); 1048 StringRef SymName; 1049 if (std::error_code EC = getSymbolName(Sym, SymName)) 1050 return EC; 1051 Result.append(SymName.begin(), SymName.end()); 1052 return object_error::success; 1053 } 1054 1055 bool COFFObjectFile::isRelocatableObject() const { 1056 return !DataDirectory; 1057 } 1058 1059 bool ImportDirectoryEntryRef:: 1060 operator==(const ImportDirectoryEntryRef &Other) const { 1061 return ImportTable == Other.ImportTable && Index == Other.Index; 1062 } 1063 1064 void ImportDirectoryEntryRef::moveNext() { 1065 ++Index; 1066 } 1067 1068 std::error_code ImportDirectoryEntryRef::getImportTableEntry( 1069 const import_directory_table_entry *&Result) const { 1070 Result = ImportTable + Index; 1071 return object_error::success; 1072 } 1073 1074 static imported_symbol_iterator 1075 makeImportedSymbolIterator(const COFFObjectFile *Object, 1076 uintptr_t Ptr, int Index) { 1077 if (Object->getBytesInAddress() == 4) { 1078 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr); 1079 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1080 } 1081 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr); 1082 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1083 } 1084 1085 static imported_symbol_iterator 1086 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) { 1087 uintptr_t IntPtr = 0; 1088 Object->getRvaPtr(RVA, IntPtr); 1089 return makeImportedSymbolIterator(Object, IntPtr, 0); 1090 } 1091 1092 static imported_symbol_iterator 1093 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) { 1094 uintptr_t IntPtr = 0; 1095 Object->getRvaPtr(RVA, IntPtr); 1096 // Forward the pointer to the last entry which is null. 1097 int Index = 0; 1098 if (Object->getBytesInAddress() == 4) { 1099 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr); 1100 while (*Entry++) 1101 ++Index; 1102 } else { 1103 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr); 1104 while (*Entry++) 1105 ++Index; 1106 } 1107 return makeImportedSymbolIterator(Object, IntPtr, Index); 1108 } 1109 1110 imported_symbol_iterator 1111 ImportDirectoryEntryRef::imported_symbol_begin() const { 1112 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA, 1113 OwningObject); 1114 } 1115 1116 imported_symbol_iterator 1117 ImportDirectoryEntryRef::imported_symbol_end() const { 1118 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA, 1119 OwningObject); 1120 } 1121 1122 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 1123 uintptr_t IntPtr = 0; 1124 if (std::error_code EC = 1125 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr)) 1126 return EC; 1127 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1128 return object_error::success; 1129 } 1130 1131 std::error_code 1132 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const { 1133 Result = ImportTable[Index].ImportLookupTableRVA; 1134 return object_error::success; 1135 } 1136 1137 std::error_code 1138 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const { 1139 Result = ImportTable[Index].ImportAddressTableRVA; 1140 return object_error::success; 1141 } 1142 1143 std::error_code ImportDirectoryEntryRef::getImportLookupEntry( 1144 const import_lookup_table_entry32 *&Result) const { 1145 uintptr_t IntPtr = 0; 1146 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA; 1147 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1148 return EC; 1149 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); 1150 return object_error::success; 1151 } 1152 1153 bool DelayImportDirectoryEntryRef:: 1154 operator==(const DelayImportDirectoryEntryRef &Other) const { 1155 return Table == Other.Table && Index == Other.Index; 1156 } 1157 1158 void DelayImportDirectoryEntryRef::moveNext() { 1159 ++Index; 1160 } 1161 1162 imported_symbol_iterator 1163 DelayImportDirectoryEntryRef::imported_symbol_begin() const { 1164 return importedSymbolBegin(Table[Index].DelayImportNameTable, 1165 OwningObject); 1166 } 1167 1168 imported_symbol_iterator 1169 DelayImportDirectoryEntryRef::imported_symbol_end() const { 1170 return importedSymbolEnd(Table[Index].DelayImportNameTable, 1171 OwningObject); 1172 } 1173 1174 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const { 1175 uintptr_t IntPtr = 0; 1176 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr)) 1177 return EC; 1178 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1179 return object_error::success; 1180 } 1181 1182 std::error_code DelayImportDirectoryEntryRef:: 1183 getDelayImportTable(const delay_import_directory_table_entry *&Result) const { 1184 Result = Table; 1185 return object_error::success; 1186 } 1187 1188 bool ExportDirectoryEntryRef:: 1189 operator==(const ExportDirectoryEntryRef &Other) const { 1190 return ExportTable == Other.ExportTable && Index == Other.Index; 1191 } 1192 1193 void ExportDirectoryEntryRef::moveNext() { 1194 ++Index; 1195 } 1196 1197 // Returns the name of the current export symbol. If the symbol is exported only 1198 // by ordinal, the empty string is set as a result. 1199 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { 1200 uintptr_t IntPtr = 0; 1201 if (std::error_code EC = 1202 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) 1203 return EC; 1204 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1205 return object_error::success; 1206 } 1207 1208 // Returns the starting ordinal number. 1209 std::error_code 1210 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { 1211 Result = ExportTable->OrdinalBase; 1212 return object_error::success; 1213 } 1214 1215 // Returns the export ordinal of the current export symbol. 1216 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 1217 Result = ExportTable->OrdinalBase + Index; 1218 return object_error::success; 1219 } 1220 1221 // Returns the address of the current export symbol. 1222 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 1223 uintptr_t IntPtr = 0; 1224 if (std::error_code EC = 1225 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) 1226 return EC; 1227 const export_address_table_entry *entry = 1228 reinterpret_cast<const export_address_table_entry *>(IntPtr); 1229 Result = entry[Index].ExportRVA; 1230 return object_error::success; 1231 } 1232 1233 // Returns the name of the current export symbol. If the symbol is exported only 1234 // by ordinal, the empty string is set as a result. 1235 std::error_code 1236 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { 1237 uintptr_t IntPtr = 0; 1238 if (std::error_code EC = 1239 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) 1240 return EC; 1241 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 1242 1243 uint32_t NumEntries = ExportTable->NumberOfNamePointers; 1244 int Offset = 0; 1245 for (const ulittle16_t *I = Start, *E = Start + NumEntries; 1246 I < E; ++I, ++Offset) { 1247 if (*I != Index) 1248 continue; 1249 if (std::error_code EC = 1250 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) 1251 return EC; 1252 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 1253 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 1254 return EC; 1255 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1256 return object_error::success; 1257 } 1258 Result = ""; 1259 return object_error::success; 1260 } 1261 1262 bool ImportedSymbolRef:: 1263 operator==(const ImportedSymbolRef &Other) const { 1264 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64 1265 && Index == Other.Index; 1266 } 1267 1268 void ImportedSymbolRef::moveNext() { 1269 ++Index; 1270 } 1271 1272 std::error_code 1273 ImportedSymbolRef::getSymbolName(StringRef &Result) const { 1274 uint32_t RVA; 1275 if (Entry32) { 1276 // If a symbol is imported only by ordinal, it has no name. 1277 if (Entry32[Index].isOrdinal()) 1278 return object_error::success; 1279 RVA = Entry32[Index].getHintNameRVA(); 1280 } else { 1281 if (Entry64[Index].isOrdinal()) 1282 return object_error::success; 1283 RVA = Entry64[Index].getHintNameRVA(); 1284 } 1285 uintptr_t IntPtr = 0; 1286 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1287 return EC; 1288 // +2 because the first two bytes is hint. 1289 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2)); 1290 return object_error::success; 1291 } 1292 1293 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const { 1294 uint32_t RVA; 1295 if (Entry32) { 1296 if (Entry32[Index].isOrdinal()) { 1297 Result = Entry32[Index].getOrdinal(); 1298 return object_error::success; 1299 } 1300 RVA = Entry32[Index].getHintNameRVA(); 1301 } else { 1302 if (Entry64[Index].isOrdinal()) { 1303 Result = Entry64[Index].getOrdinal(); 1304 return object_error::success; 1305 } 1306 RVA = Entry64[Index].getHintNameRVA(); 1307 } 1308 uintptr_t IntPtr = 0; 1309 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1310 return EC; 1311 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr); 1312 return object_error::success; 1313 } 1314 1315 ErrorOr<std::unique_ptr<COFFObjectFile>> 1316 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) { 1317 std::error_code EC; 1318 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC)); 1319 if (EC) 1320 return EC; 1321 return std::move(Ret); 1322 } 1323