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/SmallString.h" 16 #include "llvm/ADT/StringSwitch.h" 17 #include "llvm/ADT/Triple.h" 18 19 using namespace llvm; 20 using namespace object; 21 22 namespace { 23 using support::ulittle8_t; 24 using support::ulittle16_t; 25 using support::ulittle32_t; 26 using support::little16_t; 27 } 28 29 namespace { 30 // Returns false if size is greater than the buffer size. And sets ec. 31 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) { 32 if (m->getBufferSize() < size) { 33 ec = object_error::unexpected_eof; 34 return false; 35 } 36 return true; 37 } 38 39 // Returns false if any bytes in [addr, addr + size) fall outsize of m. 40 bool checkAddr(const MemoryBuffer *m, 41 error_code &ec, 42 uintptr_t addr, 43 uint64_t size) { 44 if (addr + size < addr || 45 addr + size < size || 46 addr + size > uintptr_t(m->getBufferEnd())) { 47 ec = object_error::unexpected_eof; 48 return false; 49 } 50 return true; 51 } 52 } 53 54 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const { 55 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p); 56 57 # ifndef NDEBUG 58 // Verify that the symbol points to a valid entry in the symbol table. 59 uintptr_t offset = uintptr_t(addr) - uintptr_t(base()); 60 if (offset < Header->PointerToSymbolTable 61 || offset >= Header->PointerToSymbolTable 62 + (Header->NumberOfSymbols * sizeof(coff_symbol))) 63 report_fatal_error("Symbol was outside of symbol table."); 64 65 assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol) 66 == 0 && "Symbol did not point to the beginning of a symbol"); 67 # endif 68 69 return addr; 70 } 71 72 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const { 73 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p); 74 75 # ifndef NDEBUG 76 // Verify that the section points to a valid entry in the section table. 77 if (addr < SectionTable 78 || addr >= (SectionTable + Header->NumberOfSections)) 79 report_fatal_error("Section was outside of section table."); 80 81 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable); 82 assert(offset % sizeof(coff_section) == 0 && 83 "Section did not point to the beginning of a section"); 84 # endif 85 86 return addr; 87 } 88 89 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb, 90 SymbolRef &Result) const { 91 const coff_symbol *symb = toSymb(Symb); 92 symb += 1 + symb->NumberOfAuxSymbols; 93 Symb.p = reinterpret_cast<uintptr_t>(symb); 94 Result = SymbolRef(Symb, this); 95 return object_error::success; 96 } 97 98 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb, 99 StringRef &Result) const { 100 const coff_symbol *symb = toSymb(Symb); 101 return getSymbolName(symb, Result); 102 } 103 104 error_code COFFObjectFile::getSymbolOffset(DataRefImpl Symb, 105 uint64_t &Result) const { 106 const coff_symbol *symb = toSymb(Symb); 107 const coff_section *Section = NULL; 108 if (error_code ec = getSection(symb->SectionNumber, Section)) 109 return ec; 110 char Type; 111 if (error_code ec = getSymbolNMTypeChar(Symb, Type)) 112 return ec; 113 if (Type == 'U' || Type == 'w') 114 Result = UnknownAddressOrSize; 115 else if (Section) 116 Result = Section->VirtualAddress + symb->Value; 117 else 118 Result = symb->Value; 119 return object_error::success; 120 } 121 122 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb, 123 uint64_t &Result) const { 124 const coff_symbol *symb = toSymb(Symb); 125 const coff_section *Section = NULL; 126 if (error_code ec = getSection(symb->SectionNumber, Section)) 127 return ec; 128 char Type; 129 if (error_code ec = getSymbolNMTypeChar(Symb, Type)) 130 return ec; 131 if (Type == 'U' || Type == 'w') 132 Result = UnknownAddressOrSize; 133 else if (Section) 134 Result = reinterpret_cast<uintptr_t>(base() + 135 Section->PointerToRawData + 136 symb->Value); 137 else 138 Result = reinterpret_cast<uintptr_t>(base() + symb->Value); 139 return object_error::success; 140 } 141 142 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb, 143 SymbolRef::Type &Result) const { 144 const coff_symbol *symb = toSymb(Symb); 145 Result = SymbolRef::ST_Other; 146 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && 147 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { 148 Result = SymbolRef::ST_External; 149 } else { 150 if (symb->Type.ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION) { 151 Result = SymbolRef::ST_Function; 152 } else { 153 char Type; 154 if (error_code ec = getSymbolNMTypeChar(Symb, Type)) 155 return ec; 156 if (Type == 'r' || Type == 'R') { 157 Result = SymbolRef::ST_Data; 158 } 159 } 160 } 161 return object_error::success; 162 } 163 164 error_code COFFObjectFile::isSymbolGlobal(DataRefImpl Symb, 165 bool &Result) const { 166 const coff_symbol *symb = toSymb(Symb); 167 Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL); 168 return object_error::success; 169 } 170 171 error_code COFFObjectFile::isSymbolWeak(DataRefImpl Symb, 172 bool &Result) const { 173 const coff_symbol *symb = toSymb(Symb); 174 Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL); 175 return object_error::success; 176 } 177 178 error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb, 179 uint64_t &Result) const { 180 // FIXME: Return the correct size. This requires looking at all the symbols 181 // in the same section as this symbol, and looking for either the next 182 // symbol, or the end of the section. 183 const coff_symbol *symb = toSymb(Symb); 184 const coff_section *Section = NULL; 185 if (error_code ec = getSection(symb->SectionNumber, Section)) 186 return ec; 187 char Type; 188 if (error_code ec = getSymbolNMTypeChar(Symb, Type)) 189 return ec; 190 if (Type == 'U' || Type == 'w') 191 Result = UnknownAddressOrSize; 192 else if (Section) 193 Result = Section->SizeOfRawData - symb->Value; 194 else 195 Result = 0; 196 return object_error::success; 197 } 198 199 error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb, 200 char &Result) const { 201 const coff_symbol *symb = toSymb(Symb); 202 StringRef name; 203 if (error_code ec = getSymbolName(Symb, name)) 204 return ec; 205 char ret = StringSwitch<char>(name) 206 .StartsWith(".debug", 'N') 207 .StartsWith(".sxdata", 'N') 208 .Default('?'); 209 210 if (ret != '?') { 211 Result = ret; 212 return object_error::success; 213 } 214 215 uint32_t Characteristics = 0; 216 if (symb->SectionNumber > 0) { 217 const coff_section *Section = NULL; 218 if (error_code ec = getSection(symb->SectionNumber, Section)) 219 return ec; 220 Characteristics = Section->Characteristics; 221 } 222 223 switch (symb->SectionNumber) { 224 case COFF::IMAGE_SYM_UNDEFINED: 225 // Check storage classes. 226 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) { 227 Result = 'w'; 228 return object_error::success; // Don't do ::toupper. 229 } else 230 ret = 'u'; 231 break; 232 case COFF::IMAGE_SYM_ABSOLUTE: 233 ret = 'a'; 234 break; 235 case COFF::IMAGE_SYM_DEBUG: 236 ret = 'n'; 237 break; 238 default: 239 // Check section type. 240 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE) 241 ret = 't'; 242 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ 243 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. 244 ret = 'r'; 245 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) 246 ret = 'd'; 247 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) 248 ret = 'b'; 249 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO) 250 ret = 'i'; 251 252 // Check for section symbol. 253 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC 254 && symb->Value == 0) 255 ret = 's'; 256 } 257 258 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) 259 ret = ::toupper(ret); 260 261 Result = ret; 262 return object_error::success; 263 } 264 265 error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb, 266 bool &Result) const { 267 Result = false; 268 return object_error::success; 269 } 270 271 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec, 272 SectionRef &Result) const { 273 const coff_section *sec = toSec(Sec); 274 sec += 1; 275 Sec.p = reinterpret_cast<uintptr_t>(sec); 276 Result = SectionRef(Sec, this); 277 return object_error::success; 278 } 279 280 error_code COFFObjectFile::getSectionName(DataRefImpl Sec, 281 StringRef &Result) const { 282 const coff_section *sec = toSec(Sec); 283 StringRef name; 284 if (sec->Name[7] == 0) 285 // Null terminated, let ::strlen figure out the length. 286 name = sec->Name; 287 else 288 // Not null terminated, use all 8 bytes. 289 name = StringRef(sec->Name, 8); 290 291 // Check for string table entry. First byte is '/'. 292 if (name[0] == '/') { 293 uint32_t Offset; 294 name.substr(1).getAsInteger(10, Offset); 295 if (error_code ec = getString(Offset, name)) 296 return ec; 297 } 298 299 Result = name; 300 return object_error::success; 301 } 302 303 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec, 304 uint64_t &Result) const { 305 const coff_section *sec = toSec(Sec); 306 Result = sec->VirtualAddress; 307 return object_error::success; 308 } 309 310 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec, 311 uint64_t &Result) const { 312 const coff_section *sec = toSec(Sec); 313 Result = sec->SizeOfRawData; 314 return object_error::success; 315 } 316 317 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec, 318 StringRef &Result) const { 319 const coff_section *sec = toSec(Sec); 320 // The only thing that we need to verify is that the contents is contained 321 // within the file bounds. We don't need to make sure it doesn't cover other 322 // data, as there's nothing that says that is not allowed. 323 uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData; 324 uintptr_t con_end = con_start + sec->SizeOfRawData; 325 if (con_end >= uintptr_t(Data->getBufferEnd())) 326 return object_error::parse_failed; 327 Result = StringRef(reinterpret_cast<const char*>(con_start), 328 sec->SizeOfRawData); 329 return object_error::success; 330 } 331 332 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec, 333 uint64_t &Res) const { 334 const coff_section *sec = toSec(Sec); 335 if (!sec) 336 return object_error::parse_failed; 337 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1); 338 return object_error::success; 339 } 340 341 error_code COFFObjectFile::isSectionText(DataRefImpl Sec, 342 bool &Result) const { 343 const coff_section *sec = toSec(Sec); 344 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 345 return object_error::success; 346 } 347 348 error_code COFFObjectFile::isSectionData(DataRefImpl Sec, 349 bool &Result) const { 350 const coff_section *sec = toSec(Sec); 351 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 352 return object_error::success; 353 } 354 355 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec, 356 bool &Result) const { 357 const coff_section *sec = toSec(Sec); 358 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 359 return object_error::success; 360 } 361 362 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec, 363 DataRefImpl Symb, 364 bool &Result) const { 365 const coff_section *sec = toSec(Sec); 366 const coff_symbol *symb = toSymb(Symb); 367 const coff_section *symb_sec; 368 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec; 369 if (symb_sec == sec) 370 Result = true; 371 else 372 Result = false; 373 return object_error::success; 374 } 375 376 relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const { 377 const coff_section *sec = toSec(Sec); 378 DataRefImpl ret; 379 std::memset(&ret, 0, sizeof(ret)); 380 if (sec->NumberOfRelocations == 0) 381 ret.p = 0; 382 else 383 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations); 384 385 return relocation_iterator(RelocationRef(ret, this)); 386 } 387 388 relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const { 389 const coff_section *sec = toSec(Sec); 390 DataRefImpl ret; 391 std::memset(&ret, 0, sizeof(ret)); 392 if (sec->NumberOfRelocations == 0) 393 ret.p = 0; 394 else 395 ret.p = reinterpret_cast<uintptr_t>( 396 reinterpret_cast<const coff_relocation*>( 397 base() + sec->PointerToRelocations) 398 + sec->NumberOfRelocations); 399 400 return relocation_iterator(RelocationRef(ret, this)); 401 } 402 403 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec) 404 : ObjectFile(Binary::isCOFF, Object, ec) { 405 // Check that we at least have enough room for a header. 406 if (!checkSize(Data, ec, sizeof(coff_file_header))) return; 407 408 // The actual starting location of the COFF header in the file. This can be 409 // non-zero in PE/COFF files. 410 uint64_t HeaderStart = 0; 411 412 // Check if this is a PE/COFF file. 413 if (base()[0] == 0x4d && base()[1] == 0x5a) { 414 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 415 // PE signature to find 'normal' COFF header. 416 if (!checkSize(Data, ec, 0x3c + 8)) return; 417 HeaderStart += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c); 418 // Check the PE header. ("PE\0\0") 419 if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) { 420 ec = object_error::parse_failed; 421 return; 422 } 423 HeaderStart += 4; // Skip the PE Header. 424 } 425 426 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart); 427 if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header))) 428 return; 429 430 SectionTable = 431 reinterpret_cast<const coff_section *>( base() 432 + HeaderStart 433 + sizeof(coff_file_header) 434 + Header->SizeOfOptionalHeader); 435 if (!checkAddr(Data, ec, uintptr_t(SectionTable), 436 Header->NumberOfSections * sizeof(coff_section))) 437 return; 438 439 SymbolTable = 440 reinterpret_cast<const coff_symbol *>(base() 441 + Header->PointerToSymbolTable); 442 if (!checkAddr(Data, ec, uintptr_t(SymbolTable), 443 Header->NumberOfSymbols * sizeof(coff_symbol))) 444 return; 445 446 // Find string table. 447 StringTable = reinterpret_cast<const char *>(base()) 448 + Header->PointerToSymbolTable 449 + Header->NumberOfSymbols * sizeof(coff_symbol); 450 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t))) 451 return; 452 453 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable); 454 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize)) 455 return; 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 ec = object_error::parse_failed; 460 return; 461 } 462 463 ec = object_error::success; 464 } 465 466 symbol_iterator COFFObjectFile::begin_symbols() const { 467 DataRefImpl ret; 468 std::memset(&ret, 0, sizeof(DataRefImpl)); 469 ret.p = reinterpret_cast<intptr_t>(SymbolTable); 470 return symbol_iterator(SymbolRef(ret, this)); 471 } 472 473 symbol_iterator COFFObjectFile::end_symbols() const { 474 // The symbol table ends where the string table begins. 475 DataRefImpl ret; 476 std::memset(&ret, 0, sizeof(DataRefImpl)); 477 ret.p = reinterpret_cast<intptr_t>(StringTable); 478 return symbol_iterator(SymbolRef(ret, this)); 479 } 480 481 section_iterator COFFObjectFile::begin_sections() const { 482 DataRefImpl ret; 483 std::memset(&ret, 0, sizeof(DataRefImpl)); 484 ret.p = reinterpret_cast<intptr_t>(SectionTable); 485 return section_iterator(SectionRef(ret, this)); 486 } 487 488 section_iterator COFFObjectFile::end_sections() const { 489 DataRefImpl ret; 490 std::memset(&ret, 0, sizeof(DataRefImpl)); 491 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections); 492 return section_iterator(SectionRef(ret, this)); 493 } 494 495 uint8_t COFFObjectFile::getBytesInAddress() const { 496 return getArch() == Triple::x86_64 ? 8 : 4; 497 } 498 499 StringRef COFFObjectFile::getFileFormatName() const { 500 switch(Header->Machine) { 501 case COFF::IMAGE_FILE_MACHINE_I386: 502 return "COFF-i386"; 503 case COFF::IMAGE_FILE_MACHINE_AMD64: 504 return "COFF-x86-64"; 505 default: 506 return "COFF-<unknown arch>"; 507 } 508 } 509 510 unsigned COFFObjectFile::getArch() const { 511 switch(Header->Machine) { 512 case COFF::IMAGE_FILE_MACHINE_I386: 513 return Triple::x86; 514 case COFF::IMAGE_FILE_MACHINE_AMD64: 515 return Triple::x86_64; 516 default: 517 return Triple::UnknownArch; 518 } 519 } 520 521 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const { 522 Res = Header; 523 return object_error::success; 524 } 525 526 error_code COFFObjectFile::getSection(int32_t index, 527 const coff_section *&Result) const { 528 // Check for special index values. 529 if (index == COFF::IMAGE_SYM_UNDEFINED || 530 index == COFF::IMAGE_SYM_ABSOLUTE || 531 index == COFF::IMAGE_SYM_DEBUG) 532 Result = NULL; 533 else if (index > 0 && index <= Header->NumberOfSections) 534 // We already verified the section table data, so no need to check again. 535 Result = SectionTable + (index - 1); 536 else 537 return object_error::parse_failed; 538 return object_error::success; 539 } 540 541 error_code COFFObjectFile::getString(uint32_t offset, 542 StringRef &Result) const { 543 if (StringTableSize <= 4) 544 // Tried to get a string from an empty string table. 545 return object_error::parse_failed; 546 if (offset >= StringTableSize) 547 return object_error::unexpected_eof; 548 Result = StringRef(StringTable + offset); 549 return object_error::success; 550 } 551 552 error_code COFFObjectFile::getSymbol(uint32_t index, 553 const coff_symbol *&Result) const { 554 if (index >= 0 && index < Header->NumberOfSymbols) 555 Result = SymbolTable + index; 556 else 557 return object_error::parse_failed; 558 return object_error::success; 559 } 560 561 error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol, 562 StringRef &Res) const { 563 // Check for string table entry. First 4 bytes are 0. 564 if (symbol->Name.Offset.Zeroes == 0) { 565 uint32_t Offset = symbol->Name.Offset.Offset; 566 if (error_code ec = getString(Offset, Res)) 567 return ec; 568 return object_error::success; 569 } 570 571 if (symbol->Name.ShortName[7] == 0) 572 // Null terminated, let ::strlen figure out the length. 573 Res = StringRef(symbol->Name.ShortName); 574 else 575 // Not null terminated, use all 8 bytes. 576 Res = StringRef(symbol->Name.ShortName, 8); 577 return object_error::success; 578 } 579 580 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 581 return reinterpret_cast<const coff_relocation*>(Rel.p); 582 } 583 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel, 584 RelocationRef &Res) const { 585 Rel.p = reinterpret_cast<uintptr_t>( 586 reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 587 Res = RelocationRef(Rel, this); 588 return object_error::success; 589 } 590 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, 591 uint64_t &Res) const { 592 Res = toRel(Rel)->VirtualAddress; 593 return object_error::success; 594 } 595 error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel, 596 SymbolRef &Res) const { 597 const coff_relocation* R = toRel(Rel); 598 DataRefImpl Symb; 599 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); 600 Res = SymbolRef(Symb, this); 601 return object_error::success; 602 } 603 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, 604 uint32_t &Res) const { 605 const coff_relocation* R = toRel(Rel); 606 Res = R->Type; 607 return object_error::success; 608 } 609 610 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \ 611 case COFF::enum: res = #enum; break; 612 613 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, 614 SmallVectorImpl<char> &Result) const { 615 const coff_relocation *reloc = toRel(Rel); 616 StringRef res; 617 switch (Header->Machine) { 618 case COFF::IMAGE_FILE_MACHINE_AMD64: 619 switch (reloc->Type) { 620 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 621 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 622 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 623 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 624 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 625 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 626 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 627 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 628 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 629 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 630 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 631 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 632 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 633 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 634 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 635 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 636 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 637 default: 638 res = "Unknown"; 639 } 640 break; 641 case COFF::IMAGE_FILE_MACHINE_I386: 642 switch (reloc->Type) { 643 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 644 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 645 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 646 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 647 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 648 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 649 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 650 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 651 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 652 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 653 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 654 default: 655 res = "Unknown"; 656 } 657 break; 658 default: 659 res = "Unknown"; 660 } 661 Result.append(res.begin(), res.end()); 662 return object_error::success; 663 } 664 665 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 666 667 error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, 668 int64_t &Res) const { 669 Res = 0; 670 return object_error::success; 671 } 672 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel, 673 SmallVectorImpl<char> &Result) const { 674 const coff_relocation *reloc = toRel(Rel); 675 const coff_symbol *symb = 0; 676 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec; 677 DataRefImpl sym; 678 ::memset(&sym, 0, sizeof(sym)); 679 sym.p = reinterpret_cast<uintptr_t>(symb); 680 StringRef symname; 681 if (error_code ec = getSymbolName(sym, symname)) return ec; 682 Result.append(symname.begin(), symname.end()); 683 return object_error::success; 684 } 685 686 namespace llvm { 687 688 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) { 689 error_code ec; 690 return new COFFObjectFile(Object, ec); 691 } 692 693 } // end namespace llvm 694