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