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