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