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/Debug.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include <cctype> 22 23 using namespace llvm; 24 using namespace object; 25 26 namespace { 27 using support::ulittle8_t; 28 using support::ulittle16_t; 29 using support::ulittle32_t; 30 using support::little16_t; 31 } 32 33 namespace { 34 // Returns false if size is greater than the buffer size. And sets ec. 35 bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) { 36 if (M->getBufferSize() < Size) { 37 EC = object_error::unexpected_eof; 38 return false; 39 } 40 return true; 41 } 42 43 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. 44 // Returns unexpected_eof if error. 45 template<typename T> 46 error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr, 47 const size_t Size = sizeof(T)) { 48 uintptr_t Addr = uintptr_t(Ptr); 49 if (Addr + Size < Addr || 50 Addr + Size < Size || 51 Addr + Size > uintptr_t(M->getBufferEnd())) { 52 return object_error::unexpected_eof; 53 } 54 Obj = reinterpret_cast<const T *>(Addr); 55 return object_error::success; 56 } 57 } 58 59 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const { 60 const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p); 61 62 # ifndef NDEBUG 63 // Verify that the symbol points to a valid entry in the symbol table. 64 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); 65 if (Offset < COFFHeader->PointerToSymbolTable 66 || Offset >= COFFHeader->PointerToSymbolTable 67 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 68 report_fatal_error("Symbol was outside of symbol table."); 69 70 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 71 == 0 && "Symbol did not point to the beginning of a symbol"); 72 # endif 73 74 return Addr; 75 } 76 77 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { 78 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); 79 80 # ifndef NDEBUG 81 // Verify that the section points to a valid entry in the section table. 82 if (Addr < SectionTable 83 || Addr >= (SectionTable + COFFHeader->NumberOfSections)) 84 report_fatal_error("Section was outside of section table."); 85 86 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); 87 assert(Offset % sizeof(coff_section) == 0 && 88 "Section did not point to the beginning of a section"); 89 # endif 90 91 return Addr; 92 } 93 94 error_code COFFObjectFile::getSymbolNext(DataRefImpl Ref, 95 SymbolRef &Result) const { 96 const coff_symbol *Symb = toSymb(Ref); 97 Symb += 1 + Symb->NumberOfAuxSymbols; 98 Ref.p = reinterpret_cast<uintptr_t>(Symb); 99 Result = SymbolRef(Ref, this); 100 return object_error::success; 101 } 102 103 error_code COFFObjectFile::getSymbolName(DataRefImpl Ref, 104 StringRef &Result) const { 105 const coff_symbol *Symb = toSymb(Ref); 106 return getSymbolName(Symb, Result); 107 } 108 109 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref, 110 uint64_t &Result) const { 111 const coff_symbol *Symb = toSymb(Ref); 112 const coff_section *Section = NULL; 113 if (error_code EC = getSection(Symb->SectionNumber, Section)) 114 return EC; 115 116 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 117 Result = UnknownAddressOrSize; 118 else if (Section) 119 Result = Section->PointerToRawData + Symb->Value; 120 else 121 Result = Symb->Value; 122 return object_error::success; 123 } 124 125 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref, 126 uint64_t &Result) const { 127 const coff_symbol *Symb = toSymb(Ref); 128 const coff_section *Section = NULL; 129 if (error_code EC = getSection(Symb->SectionNumber, Section)) 130 return EC; 131 132 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 133 Result = UnknownAddressOrSize; 134 else if (Section) 135 Result = Section->VirtualAddress + Symb->Value; 136 else 137 Result = Symb->Value; 138 return object_error::success; 139 } 140 141 error_code COFFObjectFile::getSymbolType(DataRefImpl Ref, 142 SymbolRef::Type &Result) const { 143 const coff_symbol *Symb = toSymb(Ref); 144 Result = SymbolRef::ST_Other; 145 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && 146 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { 147 Result = SymbolRef::ST_Unknown; 148 } else { 149 if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) { 150 Result = SymbolRef::ST_Function; 151 } else { 152 uint32_t Characteristics = 0; 153 if (Symb->SectionNumber > 0) { 154 const coff_section *Section = NULL; 155 if (error_code EC = getSection(Symb->SectionNumber, Section)) 156 return EC; 157 Characteristics = Section->Characteristics; 158 } 159 if (Characteristics & COFF::IMAGE_SCN_MEM_READ && 160 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. 161 Result = SymbolRef::ST_Data; 162 } 163 } 164 return object_error::success; 165 } 166 167 error_code COFFObjectFile::getSymbolFlags(DataRefImpl Ref, 168 uint32_t &Result) const { 169 const coff_symbol *Symb = toSymb(Ref); 170 Result = SymbolRef::SF_None; 171 172 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common 173 174 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && 175 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 176 Result |= SymbolRef::SF_Undefined; 177 178 // TODO: This are certainly too restrictive. 179 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) 180 Result |= SymbolRef::SF_Global; 181 182 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) 183 Result |= SymbolRef::SF_Weak; 184 185 if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE) 186 Result |= SymbolRef::SF_Absolute; 187 188 return object_error::success; 189 } 190 191 error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref, 192 uint64_t &Result) const { 193 // FIXME: Return the correct size. This requires looking at all the symbols 194 // in the same section as this symbol, and looking for either the next 195 // symbol, or the end of the section. 196 const coff_symbol *Symb = toSymb(Ref); 197 const coff_section *Section = NULL; 198 if (error_code EC = getSection(Symb->SectionNumber, Section)) 199 return EC; 200 201 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 202 Result = UnknownAddressOrSize; 203 else if (Section) 204 Result = Section->SizeOfRawData - Symb->Value; 205 else 206 Result = 0; 207 return object_error::success; 208 } 209 210 error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref, 211 section_iterator &Result) const { 212 const coff_symbol *Symb = toSymb(Ref); 213 if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED) 214 Result = end_sections(); 215 else { 216 const coff_section *Sec = 0; 217 if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC; 218 DataRefImpl Ref; 219 Ref.p = reinterpret_cast<uintptr_t>(Sec); 220 Result = section_iterator(SectionRef(Ref, this)); 221 } 222 return object_error::success; 223 } 224 225 error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref, 226 uint64_t &Val) const { 227 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile"); 228 } 229 230 error_code COFFObjectFile::getSectionNext(DataRefImpl Ref, 231 SectionRef &Result) const { 232 const coff_section *Sec = toSec(Ref); 233 Sec += 1; 234 Ref.p = reinterpret_cast<uintptr_t>(Sec); 235 Result = SectionRef(Ref, this); 236 return object_error::success; 237 } 238 239 error_code COFFObjectFile::getSectionName(DataRefImpl Ref, 240 StringRef &Result) const { 241 const coff_section *Sec = toSec(Ref); 242 return getSectionName(Sec, Result); 243 } 244 245 error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, 246 uint64_t &Result) const { 247 const coff_section *Sec = toSec(Ref); 248 Result = Sec->VirtualAddress; 249 return object_error::success; 250 } 251 252 error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, 253 uint64_t &Result) const { 254 const coff_section *Sec = toSec(Ref); 255 Result = Sec->SizeOfRawData; 256 return object_error::success; 257 } 258 259 error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, 260 StringRef &Result) const { 261 const coff_section *Sec = toSec(Ref); 262 ArrayRef<uint8_t> Res; 263 error_code EC = getSectionContents(Sec, Res); 264 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); 265 return EC; 266 } 267 268 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, 269 uint64_t &Res) const { 270 const coff_section *Sec = toSec(Ref); 271 if (!Sec) 272 return object_error::parse_failed; 273 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); 274 return object_error::success; 275 } 276 277 error_code COFFObjectFile::isSectionText(DataRefImpl Ref, 278 bool &Result) const { 279 const coff_section *Sec = toSec(Ref); 280 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 281 return object_error::success; 282 } 283 284 error_code COFFObjectFile::isSectionData(DataRefImpl Ref, 285 bool &Result) const { 286 const coff_section *Sec = toSec(Ref); 287 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 288 return object_error::success; 289 } 290 291 error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, 292 bool &Result) const { 293 const coff_section *Sec = toSec(Ref); 294 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 295 return object_error::success; 296 } 297 298 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref, 299 bool &Result) const { 300 // FIXME: Unimplemented 301 Result = true; 302 return object_error::success; 303 } 304 305 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, 306 bool &Result) const { 307 const coff_section *Sec = toSec(Ref); 308 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 309 return object_error::success; 310 } 311 312 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, 313 bool &Result) const { 314 // FIXME: Unimplemented. 315 Result = false; 316 return object_error::success; 317 } 318 319 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, 320 bool &Result) const { 321 // FIXME: Unimplemented. 322 Result = false; 323 return object_error::success; 324 } 325 326 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, 327 DataRefImpl SymbRef, 328 bool &Result) const { 329 const coff_section *Sec = toSec(SecRef); 330 const coff_symbol *Symb = toSymb(SymbRef); 331 const coff_section *SymbSec = 0; 332 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC; 333 if (SymbSec == Sec) 334 Result = true; 335 else 336 Result = false; 337 return object_error::success; 338 } 339 340 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 341 const coff_section *Sec = toSec(Ref); 342 DataRefImpl Ret; 343 if (Sec->NumberOfRelocations == 0) 344 Ret.p = 0; 345 else 346 Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations); 347 348 return relocation_iterator(RelocationRef(Ret, this)); 349 } 350 351 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 352 const coff_section *Sec = toSec(Ref); 353 DataRefImpl Ret; 354 if (Sec->NumberOfRelocations == 0) 355 Ret.p = 0; 356 else 357 Ret.p = reinterpret_cast<uintptr_t>( 358 reinterpret_cast<const coff_relocation*>( 359 base() + Sec->PointerToRelocations) 360 + Sec->NumberOfRelocations); 361 362 return relocation_iterator(RelocationRef(Ret, this)); 363 } 364 365 // Initialize the pointer to the symbol table. 366 error_code COFFObjectFile::initSymbolTablePtr() { 367 if (error_code EC = getObject( 368 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable, 369 COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 370 return EC; 371 372 // Find string table. The first four byte of the string table contains the 373 // total size of the string table, including the size field itself. If the 374 // string table is empty, the value of the first four byte would be 4. 375 const uint8_t *StringTableAddr = 376 base() + COFFHeader->PointerToSymbolTable + 377 COFFHeader->NumberOfSymbols * sizeof(coff_symbol); 378 const ulittle32_t *StringTableSizePtr; 379 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 380 return EC; 381 StringTableSize = *StringTableSizePtr; 382 if (error_code EC = 383 getObject(StringTable, Data, StringTableAddr, StringTableSize)) 384 return EC; 385 386 // Check that the string table is null terminated if has any in it. 387 if (StringTableSize < 4 || 388 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) 389 return object_error::parse_failed; 390 return object_error::success; 391 } 392 393 // Returns the file offset for the given RVA. 394 error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const { 395 error_code EC; 396 for (section_iterator I = begin_sections(), E = end_sections(); I != E; 397 I.increment(EC)) { 398 if (EC) 399 return EC; 400 const coff_section *Section = getCOFFSection(I); 401 uint32_t SectionStart = Section->VirtualAddress; 402 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 403 if (SectionStart <= Rva && Rva < SectionEnd) { 404 uint32_t Offset = Rva - SectionStart; 405 Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 406 return object_error::success; 407 } 408 } 409 return object_error::parse_failed; 410 } 411 412 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 413 // table entry. 414 error_code COFFObjectFile:: 415 getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const { 416 uintptr_t IntPtr = 0; 417 if (error_code EC = getRvaPtr(Rva, IntPtr)) 418 return EC; 419 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 420 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 421 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 422 return object_error::success; 423 } 424 425 // Find the import table. 426 error_code COFFObjectFile::initImportTablePtr() { 427 // First, we get the RVA of the import table. If the file lacks a pointer to 428 // the import table, do nothing. 429 const data_directory *DataEntry; 430 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 431 return object_error::success; 432 433 // Do nothing if the pointer to import table is NULL. 434 if (DataEntry->RelativeVirtualAddress == 0) 435 return object_error::success; 436 437 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 438 NumberOfImportDirectory = DataEntry->Size / 439 sizeof(import_directory_table_entry); 440 441 // Find the section that contains the RVA. This is needed because the RVA is 442 // the import table's memory address which is different from its file offset. 443 uintptr_t IntPtr = 0; 444 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 445 return EC; 446 ImportDirectory = reinterpret_cast< 447 const import_directory_table_entry *>(IntPtr); 448 return object_error::success; 449 } 450 451 // Find the export table. 452 error_code COFFObjectFile::initExportTablePtr() { 453 // First, we get the RVA of the export table. If the file lacks a pointer to 454 // the export table, do nothing. 455 const data_directory *DataEntry; 456 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 457 return object_error::success; 458 459 // Do nothing if the pointer to export table is NULL. 460 if (DataEntry->RelativeVirtualAddress == 0) 461 return object_error::success; 462 463 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 464 uintptr_t IntPtr = 0; 465 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 466 return EC; 467 ExportDirectory = reinterpret_cast<const export_directory_table_entry *>(IntPtr); 468 return object_error::success; 469 } 470 471 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC) 472 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(0), PE32Header(0), 473 DataDirectory(0), SectionTable(0), SymbolTable(0), StringTable(0), 474 StringTableSize(0), ImportDirectory(0), NumberOfImportDirectory(0), 475 ExportDirectory(0) { 476 // Check that we at least have enough room for a header. 477 if (!checkSize(Data, EC, sizeof(coff_file_header))) return; 478 479 // The current location in the file where we are looking at. 480 uint64_t CurPtr = 0; 481 482 // PE header is optional and is present only in executables. If it exists, 483 // it is placed right after COFF header. 484 bool HasPEHeader = false; 485 486 // Check if this is a PE/COFF file. 487 if (base()[0] == 0x4d && base()[1] == 0x5a) { 488 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 489 // PE signature to find 'normal' COFF header. 490 if (!checkSize(Data, EC, 0x3c + 8)) return; 491 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); 492 // Check the PE magic bytes. ("PE\0\0") 493 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) { 494 EC = object_error::parse_failed; 495 return; 496 } 497 CurPtr += 4; // Skip the PE magic bytes. 498 HasPEHeader = true; 499 } 500 501 if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 502 return; 503 CurPtr += sizeof(coff_file_header); 504 505 if (HasPEHeader) { 506 if ((EC = getObject(PE32Header, Data, base() + CurPtr))) 507 return; 508 if (PE32Header->Magic != 0x10b) { 509 // We only support PE32. If this is PE32 (not PE32+), the magic byte 510 // should be 0x10b. If this is not PE32, continue as if there's no PE 511 // header in this file. 512 PE32Header = 0; 513 } else if (PE32Header->NumberOfRvaAndSize > 0) { 514 const uint8_t *Addr = base() + CurPtr + sizeof(pe32_header); 515 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 516 if ((EC = getObject(DataDirectory, Data, Addr, size))) 517 return; 518 } 519 CurPtr += COFFHeader->SizeOfOptionalHeader; 520 } 521 522 if (!COFFHeader->isImportLibrary()) 523 if ((EC = getObject(SectionTable, Data, base() + CurPtr, 524 COFFHeader->NumberOfSections * sizeof(coff_section)))) 525 return; 526 527 // Initialize the pointer to the symbol table. 528 if (COFFHeader->PointerToSymbolTable != 0) 529 if ((EC = initSymbolTablePtr())) 530 return; 531 532 // Initialize the pointer to the beginning of the import table. 533 if ((EC = initImportTablePtr())) 534 return; 535 536 // Initialize the pointer to the export table. 537 if ((EC = initExportTablePtr())) 538 return; 539 540 EC = object_error::success; 541 } 542 543 symbol_iterator COFFObjectFile::begin_symbols() const { 544 DataRefImpl Ret; 545 Ret.p = reinterpret_cast<uintptr_t>(SymbolTable); 546 return symbol_iterator(SymbolRef(Ret, this)); 547 } 548 549 symbol_iterator COFFObjectFile::end_symbols() const { 550 // The symbol table ends where the string table begins. 551 DataRefImpl Ret; 552 Ret.p = reinterpret_cast<uintptr_t>(StringTable); 553 return symbol_iterator(SymbolRef(Ret, this)); 554 } 555 556 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const { 557 // TODO: implement 558 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); 559 } 560 561 symbol_iterator COFFObjectFile::end_dynamic_symbols() const { 562 // TODO: implement 563 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); 564 } 565 566 library_iterator COFFObjectFile::begin_libraries_needed() const { 567 // TODO: implement 568 report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 569 } 570 571 library_iterator COFFObjectFile::end_libraries_needed() const { 572 // TODO: implement 573 report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 574 } 575 576 StringRef COFFObjectFile::getLoadName() const { 577 // COFF does not have this field. 578 return ""; 579 } 580 581 import_directory_iterator COFFObjectFile::import_directory_begin() const { 582 return import_directory_iterator( 583 ImportDirectoryEntryRef(ImportDirectory, 0, this)); 584 } 585 586 import_directory_iterator COFFObjectFile::import_directory_end() const { 587 return import_directory_iterator( 588 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); 589 } 590 591 export_directory_iterator COFFObjectFile::export_directory_begin() const { 592 return export_directory_iterator( 593 ExportDirectoryEntryRef(ExportDirectory, 0, this)); 594 } 595 596 export_directory_iterator COFFObjectFile::export_directory_end() const { 597 if (ExportDirectory == 0) 598 return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this)); 599 ExportDirectoryEntryRef Ref(ExportDirectory, 600 ExportDirectory->AddressTableEntries, this); 601 return export_directory_iterator(Ref); 602 } 603 604 section_iterator COFFObjectFile::begin_sections() const { 605 DataRefImpl Ret; 606 Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 607 return section_iterator(SectionRef(Ret, this)); 608 } 609 610 section_iterator COFFObjectFile::end_sections() const { 611 DataRefImpl Ret; 612 int NumSections = COFFHeader->isImportLibrary() 613 ? 0 : COFFHeader->NumberOfSections; 614 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 615 return section_iterator(SectionRef(Ret, this)); 616 } 617 618 uint8_t COFFObjectFile::getBytesInAddress() const { 619 return getArch() == Triple::x86_64 ? 8 : 4; 620 } 621 622 StringRef COFFObjectFile::getFileFormatName() const { 623 switch(COFFHeader->Machine) { 624 case COFF::IMAGE_FILE_MACHINE_I386: 625 return "COFF-i386"; 626 case COFF::IMAGE_FILE_MACHINE_AMD64: 627 return "COFF-x86-64"; 628 default: 629 return "COFF-<unknown arch>"; 630 } 631 } 632 633 unsigned COFFObjectFile::getArch() const { 634 switch(COFFHeader->Machine) { 635 case COFF::IMAGE_FILE_MACHINE_I386: 636 return Triple::x86; 637 case COFF::IMAGE_FILE_MACHINE_AMD64: 638 return Triple::x86_64; 639 default: 640 return Triple::UnknownArch; 641 } 642 } 643 644 // This method is kept here because lld uses this. As soon as we make 645 // lld to use getCOFFHeader, this method will be removed. 646 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const { 647 return getCOFFHeader(Res); 648 } 649 650 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const { 651 Res = COFFHeader; 652 return object_error::success; 653 } 654 655 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 656 Res = PE32Header; 657 return object_error::success; 658 } 659 660 error_code COFFObjectFile::getDataDirectory(uint32_t Index, 661 const data_directory *&Res) const { 662 // Error if if there's no data directory or the index is out of range. 663 if (!DataDirectory || Index > PE32Header->NumberOfRvaAndSize) 664 return object_error::parse_failed; 665 Res = &DataDirectory[Index]; 666 return object_error::success; 667 } 668 669 error_code COFFObjectFile::getSection(int32_t Index, 670 const coff_section *&Result) const { 671 // Check for special index values. 672 if (Index == COFF::IMAGE_SYM_UNDEFINED || 673 Index == COFF::IMAGE_SYM_ABSOLUTE || 674 Index == COFF::IMAGE_SYM_DEBUG) 675 Result = NULL; 676 else if (Index > 0 && Index <= COFFHeader->NumberOfSections) 677 // We already verified the section table data, so no need to check again. 678 Result = SectionTable + (Index - 1); 679 else 680 return object_error::parse_failed; 681 return object_error::success; 682 } 683 684 error_code COFFObjectFile::getString(uint32_t Offset, 685 StringRef &Result) const { 686 if (StringTableSize <= 4) 687 // Tried to get a string from an empty string table. 688 return object_error::parse_failed; 689 if (Offset >= StringTableSize) 690 return object_error::unexpected_eof; 691 Result = StringRef(StringTable + Offset); 692 return object_error::success; 693 } 694 695 error_code COFFObjectFile::getSymbol(uint32_t Index, 696 const coff_symbol *&Result) const { 697 if (Index < COFFHeader->NumberOfSymbols) 698 Result = SymbolTable + Index; 699 else 700 return object_error::parse_failed; 701 return object_error::success; 702 } 703 704 error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol, 705 StringRef &Res) const { 706 // Check for string table entry. First 4 bytes are 0. 707 if (Symbol->Name.Offset.Zeroes == 0) { 708 uint32_t Offset = Symbol->Name.Offset.Offset; 709 if (error_code EC = getString(Offset, Res)) 710 return EC; 711 return object_error::success; 712 } 713 714 if (Symbol->Name.ShortName[7] == 0) 715 // Null terminated, let ::strlen figure out the length. 716 Res = StringRef(Symbol->Name.ShortName); 717 else 718 // Not null terminated, use all 8 bytes. 719 Res = StringRef(Symbol->Name.ShortName, 8); 720 return object_error::success; 721 } 722 723 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData( 724 const coff_symbol *Symbol) const { 725 const uint8_t *Aux = NULL; 726 727 if (Symbol->NumberOfAuxSymbols > 0) { 728 // AUX data comes immediately after the symbol in COFF 729 Aux = reinterpret_cast<const uint8_t *>(Symbol + 1); 730 # ifndef NDEBUG 731 // Verify that the Aux symbol points to a valid entry in the symbol table. 732 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 733 if (Offset < COFFHeader->PointerToSymbolTable 734 || Offset >= COFFHeader->PointerToSymbolTable 735 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 736 report_fatal_error("Aux Symbol data was outside of symbol table."); 737 738 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 739 == 0 && "Aux Symbol data did not point to the beginning of a symbol"); 740 # endif 741 } 742 return ArrayRef<uint8_t>(Aux, Symbol->NumberOfAuxSymbols * sizeof(coff_symbol)); 743 } 744 745 error_code COFFObjectFile::getSectionName(const coff_section *Sec, 746 StringRef &Res) const { 747 StringRef Name; 748 if (Sec->Name[7] == 0) 749 // Null terminated, let ::strlen figure out the length. 750 Name = Sec->Name; 751 else 752 // Not null terminated, use all 8 bytes. 753 Name = StringRef(Sec->Name, 8); 754 755 // Check for string table entry. First byte is '/'. 756 if (Name[0] == '/') { 757 uint32_t Offset; 758 if (Name.substr(1).getAsInteger(10, Offset)) 759 return object_error::parse_failed; 760 if (error_code EC = getString(Offset, Name)) 761 return EC; 762 } 763 764 Res = Name; 765 return object_error::success; 766 } 767 768 error_code COFFObjectFile::getSectionContents(const coff_section *Sec, 769 ArrayRef<uint8_t> &Res) const { 770 // The only thing that we need to verify is that the contents is contained 771 // within the file bounds. We don't need to make sure it doesn't cover other 772 // data, as there's nothing that says that is not allowed. 773 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 774 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; 775 if (ConEnd > uintptr_t(Data->getBufferEnd())) 776 return object_error::parse_failed; 777 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart), 778 Sec->SizeOfRawData); 779 return object_error::success; 780 } 781 782 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 783 return reinterpret_cast<const coff_relocation*>(Rel.p); 784 } 785 786 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel, 787 RelocationRef &Res) const { 788 Rel.p = reinterpret_cast<uintptr_t>( 789 reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 790 Res = RelocationRef(Rel, this); 791 return object_error::success; 792 } 793 794 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, 795 uint64_t &Res) const { 796 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); 797 } 798 799 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, 800 uint64_t &Res) const { 801 Res = toRel(Rel)->VirtualAddress; 802 return object_error::success; 803 } 804 805 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 806 const coff_relocation* R = toRel(Rel); 807 DataRefImpl Ref; 808 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); 809 return symbol_iterator(SymbolRef(Ref, this)); 810 } 811 812 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, 813 uint64_t &Res) const { 814 const coff_relocation* R = toRel(Rel); 815 Res = R->Type; 816 return object_error::success; 817 } 818 819 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const { 820 return toSec(It->getRawDataRefImpl()); 821 } 822 823 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const { 824 return toSymb(It->getRawDataRefImpl()); 825 } 826 827 const coff_relocation *COFFObjectFile::getCOFFRelocation( 828 relocation_iterator &It) const { 829 return toRel(It->getRawDataRefImpl()); 830 } 831 832 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \ 833 case COFF::enum: Res = #enum; break; 834 835 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, 836 SmallVectorImpl<char> &Result) const { 837 const coff_relocation *Reloc = toRel(Rel); 838 StringRef Res; 839 switch (COFFHeader->Machine) { 840 case COFF::IMAGE_FILE_MACHINE_AMD64: 841 switch (Reloc->Type) { 842 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 843 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 844 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 845 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 846 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 847 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 848 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 849 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 850 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 851 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 852 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 853 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 854 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 855 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 856 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 857 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 858 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 859 default: 860 Res = "Unknown"; 861 } 862 break; 863 case COFF::IMAGE_FILE_MACHINE_I386: 864 switch (Reloc->Type) { 865 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 866 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 867 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 868 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 869 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 870 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 871 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 872 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 873 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 874 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 875 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 876 default: 877 Res = "Unknown"; 878 } 879 break; 880 default: 881 Res = "Unknown"; 882 } 883 Result.append(Res.begin(), Res.end()); 884 return object_error::success; 885 } 886 887 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 888 889 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel, 890 SmallVectorImpl<char> &Result) const { 891 const coff_relocation *Reloc = toRel(Rel); 892 const coff_symbol *Symb = 0; 893 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC; 894 DataRefImpl Sym; 895 Sym.p = reinterpret_cast<uintptr_t>(Symb); 896 StringRef SymName; 897 if (error_code EC = getSymbolName(Sym, SymName)) return EC; 898 Result.append(SymName.begin(), SymName.end()); 899 return object_error::success; 900 } 901 902 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData, 903 LibraryRef &Result) const { 904 report_fatal_error("getLibraryNext not implemented in COFFObjectFile"); 905 } 906 907 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData, 908 StringRef &Result) const { 909 report_fatal_error("getLibraryPath not implemented in COFFObjectFile"); 910 } 911 912 bool ImportDirectoryEntryRef:: 913 operator==(const ImportDirectoryEntryRef &Other) const { 914 return ImportTable == Other.ImportTable && Index == Other.Index; 915 } 916 917 error_code 918 ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const { 919 Result = ImportDirectoryEntryRef(ImportTable, Index + 1, OwningObject); 920 return object_error::success; 921 } 922 923 error_code ImportDirectoryEntryRef:: 924 getImportTableEntry(const import_directory_table_entry *&Result) const { 925 Result = ImportTable; 926 return object_error::success; 927 } 928 929 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 930 uintptr_t IntPtr = 0; 931 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr)) 932 return EC; 933 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 934 return object_error::success; 935 } 936 937 error_code ImportDirectoryEntryRef::getImportLookupEntry( 938 const import_lookup_table_entry32 *&Result) const { 939 uintptr_t IntPtr = 0; 940 if (error_code EC = 941 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr)) 942 return EC; 943 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); 944 return object_error::success; 945 } 946 947 bool ExportDirectoryEntryRef:: 948 operator==(const ExportDirectoryEntryRef &Other) const { 949 return ExportTable == Other.ExportTable && Index == Other.Index; 950 } 951 952 error_code 953 ExportDirectoryEntryRef::getNext(ExportDirectoryEntryRef &Result) const { 954 Result = ExportDirectoryEntryRef(ExportTable, Index + 1, OwningObject); 955 return object_error::success; 956 } 957 958 // Returns the export ordinal of the current export symbol. 959 error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 960 Result = ExportTable->OrdinalBase + Index; 961 return object_error::success; 962 } 963 964 // Returns the address of the current export symbol. 965 error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 966 uintptr_t IntPtr = 0; 967 if (error_code EC = OwningObject->getRvaPtr( 968 ExportTable->ExportAddressTableRVA, IntPtr)) 969 return EC; 970 const export_address_table_entry *entry = reinterpret_cast<const export_address_table_entry *>(IntPtr); 971 Result = entry[Index].ExportRVA; 972 return object_error::success; 973 } 974 975 // Returns the name of the current export symbol. If the symbol is exported only 976 // by ordinal, the empty string is set as a result. 977 error_code ExportDirectoryEntryRef::getName(StringRef &Result) const { 978 uintptr_t IntPtr = 0; 979 if (error_code EC = OwningObject->getRvaPtr( 980 ExportTable->OrdinalTableRVA, IntPtr)) 981 return EC; 982 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 983 984 uint32_t NumEntries = ExportTable->NumberOfNamePointers; 985 int Offset = 0; 986 for (const ulittle16_t *I = Start, *E = Start + NumEntries; 987 I < E; ++I, ++Offset) { 988 if (*I != Index) 989 continue; 990 if (error_code EC = OwningObject->getRvaPtr( 991 ExportTable->NamePointerRVA, IntPtr)) 992 return EC; 993 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 994 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 995 return EC; 996 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 997 return object_error::success; 998 } 999 Result = ""; 1000 return object_error::success; 1001 } 1002 1003 namespace llvm { 1004 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) { 1005 error_code EC; 1006 return new COFFObjectFile(Object, EC); 1007 } 1008 } // end namespace llvm 1009