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