1 //===- COFFObjectFile.cpp - COFF object file implementation ---------------===// 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/ADT/ArrayRef.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/ADT/Triple.h" 17 #include "llvm/ADT/iterator_range.h" 18 #include "llvm/BinaryFormat/COFF.h" 19 #include "llvm/Object/Binary.h" 20 #include "llvm/Object/COFF.h" 21 #include "llvm/Object/Error.h" 22 #include "llvm/Object/ObjectFile.h" 23 #include "llvm/Support/BinaryStreamReader.h" 24 #include "llvm/Support/Endian.h" 25 #include "llvm/Support/Error.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/MathExtras.h" 28 #include "llvm/Support/MemoryBuffer.h" 29 #include <algorithm> 30 #include <cassert> 31 #include <cstddef> 32 #include <cstdint> 33 #include <cstring> 34 #include <limits> 35 #include <memory> 36 #include <system_error> 37 38 using namespace llvm; 39 using namespace object; 40 41 using support::ulittle16_t; 42 using support::ulittle32_t; 43 using support::ulittle64_t; 44 using support::little16_t; 45 46 // Returns false if size is greater than the buffer size. And sets ec. 47 static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) { 48 if (M.getBufferSize() < Size) { 49 EC = object_error::unexpected_eof; 50 return false; 51 } 52 return true; 53 } 54 55 static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, 56 const uint64_t Size) { 57 if (Addr + Size < Addr || Addr + Size < Size || 58 Addr + Size > uintptr_t(M.getBufferEnd()) || 59 Addr < uintptr_t(M.getBufferStart())) { 60 return object_error::unexpected_eof; 61 } 62 return std::error_code(); 63 } 64 65 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. 66 // Returns unexpected_eof if error. 67 template <typename T> 68 static std::error_code getObject(const T *&Obj, MemoryBufferRef M, 69 const void *Ptr, 70 const uint64_t Size = sizeof(T)) { 71 uintptr_t Addr = uintptr_t(Ptr); 72 if (std::error_code EC = checkOffset(M, Addr, Size)) 73 return EC; 74 Obj = reinterpret_cast<const T *>(Addr); 75 return std::error_code(); 76 } 77 78 // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without 79 // prefixed slashes. 80 static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) { 81 assert(Str.size() <= 6 && "String too long, possible overflow."); 82 if (Str.size() > 6) 83 return true; 84 85 uint64_t Value = 0; 86 while (!Str.empty()) { 87 unsigned CharVal; 88 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25 89 CharVal = Str[0] - 'A'; 90 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51 91 CharVal = Str[0] - 'a' + 26; 92 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61 93 CharVal = Str[0] - '0' + 52; 94 else if (Str[0] == '+') // 62 95 CharVal = 62; 96 else if (Str[0] == '/') // 63 97 CharVal = 63; 98 else 99 return true; 100 101 Value = (Value * 64) + CharVal; 102 Str = Str.substr(1); 103 } 104 105 if (Value > std::numeric_limits<uint32_t>::max()) 106 return true; 107 108 Result = static_cast<uint32_t>(Value); 109 return false; 110 } 111 112 template <typename coff_symbol_type> 113 const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const { 114 const coff_symbol_type *Addr = 115 reinterpret_cast<const coff_symbol_type *>(Ref.p); 116 117 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr))); 118 #ifndef NDEBUG 119 // Verify that the symbol points to a valid entry in the symbol table. 120 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); 121 122 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 && 123 "Symbol did not point to the beginning of a symbol"); 124 #endif 125 126 return Addr; 127 } 128 129 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { 130 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); 131 132 #ifndef NDEBUG 133 // Verify that the section points to a valid entry in the section table. 134 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections())) 135 report_fatal_error("Section was outside of section table."); 136 137 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); 138 assert(Offset % sizeof(coff_section) == 0 && 139 "Section did not point to the beginning of a section"); 140 #endif 141 142 return Addr; 143 } 144 145 void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { 146 auto End = reinterpret_cast<uintptr_t>(StringTable); 147 if (SymbolTable16) { 148 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref); 149 Symb += 1 + Symb->NumberOfAuxSymbols; 150 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End); 151 } else if (SymbolTable32) { 152 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref); 153 Symb += 1 + Symb->NumberOfAuxSymbols; 154 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End); 155 } else { 156 llvm_unreachable("no symbol table pointer!"); 157 } 158 } 159 160 Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const { 161 COFFSymbolRef Symb = getCOFFSymbol(Ref); 162 StringRef Result; 163 if (std::error_code EC = getSymbolName(Symb, Result)) 164 return errorCodeToError(EC); 165 return Result; 166 } 167 168 uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const { 169 return getCOFFSymbol(Ref).getValue(); 170 } 171 172 uint32_t COFFObjectFile::getSymbolAlignment(DataRefImpl Ref) const { 173 // MSVC/link.exe seems to align symbols to the next-power-of-2 174 // up to 32 bytes. 175 COFFSymbolRef Symb = getCOFFSymbol(Ref); 176 return std::min(uint64_t(32), PowerOf2Ceil(Symb.getValue())); 177 } 178 179 Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const { 180 uint64_t Result = getSymbolValue(Ref); 181 COFFSymbolRef Symb = getCOFFSymbol(Ref); 182 int32_t SectionNumber = Symb.getSectionNumber(); 183 184 if (Symb.isAnyUndefined() || Symb.isCommon() || 185 COFF::isReservedSectionNumber(SectionNumber)) 186 return Result; 187 188 const coff_section *Section = nullptr; 189 if (std::error_code EC = getSection(SectionNumber, Section)) 190 return errorCodeToError(EC); 191 Result += Section->VirtualAddress; 192 193 // The section VirtualAddress does not include ImageBase, and we want to 194 // return virtual addresses. 195 Result += getImageBase(); 196 197 return Result; 198 } 199 200 Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const { 201 COFFSymbolRef Symb = getCOFFSymbol(Ref); 202 int32_t SectionNumber = Symb.getSectionNumber(); 203 204 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) 205 return SymbolRef::ST_Function; 206 if (Symb.isAnyUndefined()) 207 return SymbolRef::ST_Unknown; 208 if (Symb.isCommon()) 209 return SymbolRef::ST_Data; 210 if (Symb.isFileRecord()) 211 return SymbolRef::ST_File; 212 213 // TODO: perhaps we need a new symbol type ST_Section. 214 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition()) 215 return SymbolRef::ST_Debug; 216 217 if (!COFF::isReservedSectionNumber(SectionNumber)) 218 return SymbolRef::ST_Data; 219 220 return SymbolRef::ST_Other; 221 } 222 223 uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { 224 COFFSymbolRef Symb = getCOFFSymbol(Ref); 225 uint32_t Result = SymbolRef::SF_None; 226 227 if (Symb.isExternal() || Symb.isWeakExternal()) 228 Result |= SymbolRef::SF_Global; 229 230 if (Symb.isWeakExternal()) 231 Result |= SymbolRef::SF_Weak; 232 233 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE) 234 Result |= SymbolRef::SF_Absolute; 235 236 if (Symb.isFileRecord()) 237 Result |= SymbolRef::SF_FormatSpecific; 238 239 if (Symb.isSectionDefinition()) 240 Result |= SymbolRef::SF_FormatSpecific; 241 242 if (Symb.isCommon()) 243 Result |= SymbolRef::SF_Common; 244 245 if (Symb.isAnyUndefined()) 246 Result |= SymbolRef::SF_Undefined; 247 248 return Result; 249 } 250 251 uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const { 252 COFFSymbolRef Symb = getCOFFSymbol(Ref); 253 return Symb.getValue(); 254 } 255 256 Expected<section_iterator> 257 COFFObjectFile::getSymbolSection(DataRefImpl Ref) const { 258 COFFSymbolRef Symb = getCOFFSymbol(Ref); 259 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) 260 return section_end(); 261 const coff_section *Sec = nullptr; 262 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec)) 263 return errorCodeToError(EC); 264 DataRefImpl Ret; 265 Ret.p = reinterpret_cast<uintptr_t>(Sec); 266 return section_iterator(SectionRef(Ret, this)); 267 } 268 269 unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const { 270 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl()); 271 return Symb.getSectionNumber(); 272 } 273 274 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { 275 const coff_section *Sec = toSec(Ref); 276 Sec += 1; 277 Ref.p = reinterpret_cast<uintptr_t>(Sec); 278 } 279 280 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref, 281 StringRef &Result) const { 282 const coff_section *Sec = toSec(Ref); 283 return getSectionName(Sec, Result); 284 } 285 286 uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const { 287 const coff_section *Sec = toSec(Ref); 288 uint64_t Result = Sec->VirtualAddress; 289 290 // The section VirtualAddress does not include ImageBase, and we want to 291 // return virtual addresses. 292 Result += getImageBase(); 293 return Result; 294 } 295 296 uint64_t COFFObjectFile::getSectionIndex(DataRefImpl Sec) const { 297 return toSec(Sec) - SectionTable; 298 } 299 300 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const { 301 return getSectionSize(toSec(Ref)); 302 } 303 304 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, 305 StringRef &Result) const { 306 const coff_section *Sec = toSec(Ref); 307 ArrayRef<uint8_t> Res; 308 std::error_code EC = getSectionContents(Sec, Res); 309 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); 310 return EC; 311 } 312 313 uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const { 314 const coff_section *Sec = toSec(Ref); 315 return Sec->getAlignment(); 316 } 317 318 bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { 319 return false; 320 } 321 322 bool COFFObjectFile::isSectionText(DataRefImpl Ref) const { 323 const coff_section *Sec = toSec(Ref); 324 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 325 } 326 327 bool COFFObjectFile::isSectionData(DataRefImpl Ref) const { 328 const coff_section *Sec = toSec(Ref); 329 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 330 } 331 332 bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const { 333 const coff_section *Sec = toSec(Ref); 334 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 335 COFF::IMAGE_SCN_MEM_READ | 336 COFF::IMAGE_SCN_MEM_WRITE; 337 return (Sec->Characteristics & BssFlags) == BssFlags; 338 } 339 340 unsigned COFFObjectFile::getSectionID(SectionRef Sec) const { 341 uintptr_t Offset = 342 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable); 343 assert((Offset % sizeof(coff_section)) == 0); 344 return (Offset / sizeof(coff_section)) + 1; 345 } 346 347 bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const { 348 const coff_section *Sec = toSec(Ref); 349 // In COFF, a virtual section won't have any in-file 350 // content, so the file pointer to the content will be zero. 351 return Sec->PointerToRawData == 0; 352 } 353 354 static uint32_t getNumberOfRelocations(const coff_section *Sec, 355 MemoryBufferRef M, const uint8_t *base) { 356 // The field for the number of relocations in COFF section table is only 357 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to 358 // NumberOfRelocations field, and the actual relocation count is stored in the 359 // VirtualAddress field in the first relocation entry. 360 if (Sec->hasExtendedRelocations()) { 361 const coff_relocation *FirstReloc; 362 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>( 363 base + Sec->PointerToRelocations))) 364 return 0; 365 // -1 to exclude this first relocation entry. 366 return FirstReloc->VirtualAddress - 1; 367 } 368 return Sec->NumberOfRelocations; 369 } 370 371 static const coff_relocation * 372 getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) { 373 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base); 374 if (!NumRelocs) 375 return nullptr; 376 auto begin = reinterpret_cast<const coff_relocation *>( 377 Base + Sec->PointerToRelocations); 378 if (Sec->hasExtendedRelocations()) { 379 // Skip the first relocation entry repurposed to store the number of 380 // relocations. 381 begin++; 382 } 383 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs)) 384 return nullptr; 385 return begin; 386 } 387 388 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 389 const coff_section *Sec = toSec(Ref); 390 const coff_relocation *begin = getFirstReloc(Sec, Data, base()); 391 if (begin && Sec->VirtualAddress != 0) 392 report_fatal_error("Sections with relocations should have an address of 0"); 393 DataRefImpl Ret; 394 Ret.p = reinterpret_cast<uintptr_t>(begin); 395 return relocation_iterator(RelocationRef(Ret, this)); 396 } 397 398 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 399 const coff_section *Sec = toSec(Ref); 400 const coff_relocation *I = getFirstReloc(Sec, Data, base()); 401 if (I) 402 I += getNumberOfRelocations(Sec, Data, base()); 403 DataRefImpl Ret; 404 Ret.p = reinterpret_cast<uintptr_t>(I); 405 return relocation_iterator(RelocationRef(Ret, this)); 406 } 407 408 // Initialize the pointer to the symbol table. 409 std::error_code COFFObjectFile::initSymbolTablePtr() { 410 if (COFFHeader) 411 if (std::error_code EC = getObject( 412 SymbolTable16, Data, base() + getPointerToSymbolTable(), 413 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize())) 414 return EC; 415 416 if (COFFBigObjHeader) 417 if (std::error_code EC = getObject( 418 SymbolTable32, Data, base() + getPointerToSymbolTable(), 419 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize())) 420 return EC; 421 422 // Find string table. The first four byte of the string table contains the 423 // total size of the string table, including the size field itself. If the 424 // string table is empty, the value of the first four byte would be 4. 425 uint32_t StringTableOffset = getPointerToSymbolTable() + 426 getNumberOfSymbols() * getSymbolTableEntrySize(); 427 const uint8_t *StringTableAddr = base() + StringTableOffset; 428 const ulittle32_t *StringTableSizePtr; 429 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 430 return EC; 431 StringTableSize = *StringTableSizePtr; 432 if (std::error_code EC = 433 getObject(StringTable, Data, StringTableAddr, StringTableSize)) 434 return EC; 435 436 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some 437 // tools like cvtres write a size of 0 for an empty table instead of 4. 438 if (StringTableSize < 4) 439 StringTableSize = 4; 440 441 // Check that the string table is null terminated if has any in it. 442 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) 443 return object_error::parse_failed; 444 return std::error_code(); 445 } 446 447 uint64_t COFFObjectFile::getImageBase() const { 448 if (PE32Header) 449 return PE32Header->ImageBase; 450 else if (PE32PlusHeader) 451 return PE32PlusHeader->ImageBase; 452 // This actually comes up in practice. 453 return 0; 454 } 455 456 // Returns the file offset for the given VA. 457 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { 458 uint64_t ImageBase = getImageBase(); 459 uint64_t Rva = Addr - ImageBase; 460 assert(Rva <= UINT32_MAX); 461 return getRvaPtr((uint32_t)Rva, Res); 462 } 463 464 // Returns the file offset for the given RVA. 465 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { 466 for (const SectionRef &S : sections()) { 467 const coff_section *Section = getCOFFSection(S); 468 uint32_t SectionStart = Section->VirtualAddress; 469 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 470 if (SectionStart <= Addr && Addr < SectionEnd) { 471 uint32_t Offset = Addr - SectionStart; 472 Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 473 return std::error_code(); 474 } 475 } 476 return object_error::parse_failed; 477 } 478 479 std::error_code 480 COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size, 481 ArrayRef<uint8_t> &Contents) const { 482 for (const SectionRef &S : sections()) { 483 const coff_section *Section = getCOFFSection(S); 484 uint32_t SectionStart = Section->VirtualAddress; 485 // Check if this RVA is within the section bounds. Be careful about integer 486 // overflow. 487 uint32_t OffsetIntoSection = RVA - SectionStart; 488 if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize && 489 Size <= Section->VirtualSize - OffsetIntoSection) { 490 uintptr_t Begin = 491 uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection; 492 Contents = 493 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size); 494 return std::error_code(); 495 } 496 } 497 return object_error::parse_failed; 498 } 499 500 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 501 // table entry. 502 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint, 503 StringRef &Name) const { 504 uintptr_t IntPtr = 0; 505 if (std::error_code EC = getRvaPtr(Rva, IntPtr)) 506 return EC; 507 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 508 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 509 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 510 return std::error_code(); 511 } 512 513 std::error_code 514 COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir, 515 const codeview::DebugInfo *&PDBInfo, 516 StringRef &PDBFileName) const { 517 ArrayRef<uint8_t> InfoBytes; 518 if (std::error_code EC = getRvaAndSizeAsBytes( 519 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes)) 520 return EC; 521 if (InfoBytes.size() < sizeof(*PDBInfo) + 1) 522 return object_error::parse_failed; 523 PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data()); 524 InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo)); 525 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()), 526 InfoBytes.size()); 527 // Truncate the name at the first null byte. Ignore any padding. 528 PDBFileName = PDBFileName.split('\0').first; 529 return std::error_code(); 530 } 531 532 std::error_code 533 COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo, 534 StringRef &PDBFileName) const { 535 for (const debug_directory &D : debug_directories()) 536 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW) 537 return getDebugPDBInfo(&D, PDBInfo, PDBFileName); 538 // If we get here, there is no PDB info to return. 539 PDBInfo = nullptr; 540 PDBFileName = StringRef(); 541 return std::error_code(); 542 } 543 544 // Find the import table. 545 std::error_code COFFObjectFile::initImportTablePtr() { 546 // First, we get the RVA of the import table. If the file lacks a pointer to 547 // the import table, do nothing. 548 const data_directory *DataEntry; 549 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 550 return std::error_code(); 551 552 // Do nothing if the pointer to import table is NULL. 553 if (DataEntry->RelativeVirtualAddress == 0) 554 return std::error_code(); 555 556 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 557 558 // Find the section that contains the RVA. This is needed because the RVA is 559 // the import table's memory address which is different from its file offset. 560 uintptr_t IntPtr = 0; 561 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 562 return EC; 563 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size)) 564 return EC; 565 ImportDirectory = reinterpret_cast< 566 const coff_import_directory_table_entry *>(IntPtr); 567 return std::error_code(); 568 } 569 570 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory. 571 std::error_code COFFObjectFile::initDelayImportTablePtr() { 572 const data_directory *DataEntry; 573 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry)) 574 return std::error_code(); 575 if (DataEntry->RelativeVirtualAddress == 0) 576 return std::error_code(); 577 578 uint32_t RVA = DataEntry->RelativeVirtualAddress; 579 NumberOfDelayImportDirectory = DataEntry->Size / 580 sizeof(delay_import_directory_table_entry) - 1; 581 582 uintptr_t IntPtr = 0; 583 if (std::error_code EC = getRvaPtr(RVA, IntPtr)) 584 return EC; 585 DelayImportDirectory = reinterpret_cast< 586 const delay_import_directory_table_entry *>(IntPtr); 587 return std::error_code(); 588 } 589 590 // Find the export table. 591 std::error_code COFFObjectFile::initExportTablePtr() { 592 // First, we get the RVA of the export table. If the file lacks a pointer to 593 // the export table, do nothing. 594 const data_directory *DataEntry; 595 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 596 return std::error_code(); 597 598 // Do nothing if the pointer to export table is NULL. 599 if (DataEntry->RelativeVirtualAddress == 0) 600 return std::error_code(); 601 602 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 603 uintptr_t IntPtr = 0; 604 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 605 return EC; 606 ExportDirectory = 607 reinterpret_cast<const export_directory_table_entry *>(IntPtr); 608 return std::error_code(); 609 } 610 611 std::error_code COFFObjectFile::initBaseRelocPtr() { 612 const data_directory *DataEntry; 613 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry)) 614 return std::error_code(); 615 if (DataEntry->RelativeVirtualAddress == 0) 616 return std::error_code(); 617 618 uintptr_t IntPtr = 0; 619 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) 620 return EC; 621 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>( 622 IntPtr); 623 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>( 624 IntPtr + DataEntry->Size); 625 return std::error_code(); 626 } 627 628 std::error_code COFFObjectFile::initDebugDirectoryPtr() { 629 // Get the RVA of the debug directory. Do nothing if it does not exist. 630 const data_directory *DataEntry; 631 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry)) 632 return std::error_code(); 633 634 // Do nothing if the RVA is NULL. 635 if (DataEntry->RelativeVirtualAddress == 0) 636 return std::error_code(); 637 638 // Check that the size is a multiple of the entry size. 639 if (DataEntry->Size % sizeof(debug_directory) != 0) 640 return object_error::parse_failed; 641 642 uintptr_t IntPtr = 0; 643 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) 644 return EC; 645 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr); 646 if (std::error_code EC = getRvaPtr( 647 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr)) 648 return EC; 649 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr); 650 return std::error_code(); 651 } 652 653 std::error_code COFFObjectFile::initLoadConfigPtr() { 654 // Get the RVA of the debug directory. Do nothing if it does not exist. 655 const data_directory *DataEntry; 656 if (getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataEntry)) 657 return std::error_code(); 658 659 // Do nothing if the RVA is NULL. 660 if (DataEntry->RelativeVirtualAddress == 0) 661 return std::error_code(); 662 uintptr_t IntPtr = 0; 663 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) 664 return EC; 665 666 LoadConfig = (const void *)IntPtr; 667 return std::error_code(); 668 } 669 670 COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC) 671 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr), 672 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr), 673 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr), 674 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0), 675 ImportDirectory(nullptr), 676 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0), 677 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr), 678 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) { 679 // Check that we at least have enough room for a header. 680 if (!checkSize(Data, EC, sizeof(coff_file_header))) 681 return; 682 683 // The current location in the file where we are looking at. 684 uint64_t CurPtr = 0; 685 686 // PE header is optional and is present only in executables. If it exists, 687 // it is placed right after COFF header. 688 bool HasPEHeader = false; 689 690 // Check if this is a PE/COFF file. 691 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) { 692 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 693 // PE signature to find 'normal' COFF header. 694 const auto *DH = reinterpret_cast<const dos_header *>(base()); 695 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') { 696 CurPtr = DH->AddressOfNewExeHeader; 697 // Check the PE magic bytes. ("PE\0\0") 698 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) { 699 EC = object_error::parse_failed; 700 return; 701 } 702 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes. 703 HasPEHeader = true; 704 } 705 } 706 707 if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 708 return; 709 710 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF 711 // import libraries share a common prefix but bigobj is more restrictive. 712 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN && 713 COFFHeader->NumberOfSections == uint16_t(0xffff) && 714 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) { 715 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr))) 716 return; 717 718 // Verify that we are dealing with bigobj. 719 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion && 720 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic, 721 sizeof(COFF::BigObjMagic)) == 0) { 722 COFFHeader = nullptr; 723 CurPtr += sizeof(coff_bigobj_file_header); 724 } else { 725 // It's not a bigobj. 726 COFFBigObjHeader = nullptr; 727 } 728 } 729 if (COFFHeader) { 730 // The prior checkSize call may have failed. This isn't a hard error 731 // because we were just trying to sniff out bigobj. 732 EC = std::error_code(); 733 CurPtr += sizeof(coff_file_header); 734 735 if (COFFHeader->isImportLibrary()) 736 return; 737 } 738 739 if (HasPEHeader) { 740 const pe32_header *Header; 741 if ((EC = getObject(Header, Data, base() + CurPtr))) 742 return; 743 744 const uint8_t *DataDirAddr; 745 uint64_t DataDirSize; 746 if (Header->Magic == COFF::PE32Header::PE32) { 747 PE32Header = Header; 748 DataDirAddr = base() + CurPtr + sizeof(pe32_header); 749 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 750 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) { 751 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); 752 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); 753 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; 754 } else { 755 // It's neither PE32 nor PE32+. 756 EC = object_error::parse_failed; 757 return; 758 } 759 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) 760 return; 761 } 762 763 if (COFFHeader) 764 CurPtr += COFFHeader->SizeOfOptionalHeader; 765 766 if ((EC = getObject(SectionTable, Data, base() + CurPtr, 767 (uint64_t)getNumberOfSections() * sizeof(coff_section)))) 768 return; 769 770 // Initialize the pointer to the symbol table. 771 if (getPointerToSymbolTable() != 0) { 772 if ((EC = initSymbolTablePtr())) { 773 SymbolTable16 = nullptr; 774 SymbolTable32 = nullptr; 775 StringTable = nullptr; 776 StringTableSize = 0; 777 } 778 } else { 779 // We had better not have any symbols if we don't have a symbol table. 780 if (getNumberOfSymbols() != 0) { 781 EC = object_error::parse_failed; 782 return; 783 } 784 } 785 786 // Initialize the pointer to the beginning of the import table. 787 if ((EC = initImportTablePtr())) 788 return; 789 if ((EC = initDelayImportTablePtr())) 790 return; 791 792 // Initialize the pointer to the export table. 793 if ((EC = initExportTablePtr())) 794 return; 795 796 // Initialize the pointer to the base relocation table. 797 if ((EC = initBaseRelocPtr())) 798 return; 799 800 // Initialize the pointer to the export table. 801 if ((EC = initDebugDirectoryPtr())) 802 return; 803 804 if ((EC = initLoadConfigPtr())) 805 return; 806 807 EC = std::error_code(); 808 } 809 810 basic_symbol_iterator COFFObjectFile::symbol_begin() const { 811 DataRefImpl Ret; 812 Ret.p = getSymbolTable(); 813 return basic_symbol_iterator(SymbolRef(Ret, this)); 814 } 815 816 basic_symbol_iterator COFFObjectFile::symbol_end() const { 817 // The symbol table ends where the string table begins. 818 DataRefImpl Ret; 819 Ret.p = reinterpret_cast<uintptr_t>(StringTable); 820 return basic_symbol_iterator(SymbolRef(Ret, this)); 821 } 822 823 import_directory_iterator COFFObjectFile::import_directory_begin() const { 824 if (!ImportDirectory) 825 return import_directory_end(); 826 if (ImportDirectory->isNull()) 827 return import_directory_end(); 828 return import_directory_iterator( 829 ImportDirectoryEntryRef(ImportDirectory, 0, this)); 830 } 831 832 import_directory_iterator COFFObjectFile::import_directory_end() const { 833 return import_directory_iterator( 834 ImportDirectoryEntryRef(nullptr, -1, this)); 835 } 836 837 delay_import_directory_iterator 838 COFFObjectFile::delay_import_directory_begin() const { 839 return delay_import_directory_iterator( 840 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this)); 841 } 842 843 delay_import_directory_iterator 844 COFFObjectFile::delay_import_directory_end() const { 845 return delay_import_directory_iterator( 846 DelayImportDirectoryEntryRef( 847 DelayImportDirectory, NumberOfDelayImportDirectory, this)); 848 } 849 850 export_directory_iterator COFFObjectFile::export_directory_begin() const { 851 return export_directory_iterator( 852 ExportDirectoryEntryRef(ExportDirectory, 0, this)); 853 } 854 855 export_directory_iterator COFFObjectFile::export_directory_end() const { 856 if (!ExportDirectory) 857 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this)); 858 ExportDirectoryEntryRef Ref(ExportDirectory, 859 ExportDirectory->AddressTableEntries, this); 860 return export_directory_iterator(Ref); 861 } 862 863 section_iterator COFFObjectFile::section_begin() const { 864 DataRefImpl Ret; 865 Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 866 return section_iterator(SectionRef(Ret, this)); 867 } 868 869 section_iterator COFFObjectFile::section_end() const { 870 DataRefImpl Ret; 871 int NumSections = 872 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections(); 873 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 874 return section_iterator(SectionRef(Ret, this)); 875 } 876 877 base_reloc_iterator COFFObjectFile::base_reloc_begin() const { 878 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this)); 879 } 880 881 base_reloc_iterator COFFObjectFile::base_reloc_end() const { 882 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this)); 883 } 884 885 uint8_t COFFObjectFile::getBytesInAddress() const { 886 return getArch() == Triple::x86_64 ? 8 : 4; 887 } 888 889 StringRef COFFObjectFile::getFileFormatName() const { 890 switch(getMachine()) { 891 case COFF::IMAGE_FILE_MACHINE_I386: 892 return "COFF-i386"; 893 case COFF::IMAGE_FILE_MACHINE_AMD64: 894 return "COFF-x86-64"; 895 case COFF::IMAGE_FILE_MACHINE_ARMNT: 896 return "COFF-ARM"; 897 case COFF::IMAGE_FILE_MACHINE_ARM64: 898 return "COFF-ARM64"; 899 default: 900 return "COFF-<unknown arch>"; 901 } 902 } 903 904 unsigned COFFObjectFile::getArch() const { 905 switch (getMachine()) { 906 case COFF::IMAGE_FILE_MACHINE_I386: 907 return Triple::x86; 908 case COFF::IMAGE_FILE_MACHINE_AMD64: 909 return Triple::x86_64; 910 case COFF::IMAGE_FILE_MACHINE_ARMNT: 911 return Triple::thumb; 912 case COFF::IMAGE_FILE_MACHINE_ARM64: 913 return Triple::aarch64; 914 default: 915 return Triple::UnknownArch; 916 } 917 } 918 919 iterator_range<import_directory_iterator> 920 COFFObjectFile::import_directories() const { 921 return make_range(import_directory_begin(), import_directory_end()); 922 } 923 924 iterator_range<delay_import_directory_iterator> 925 COFFObjectFile::delay_import_directories() const { 926 return make_range(delay_import_directory_begin(), 927 delay_import_directory_end()); 928 } 929 930 iterator_range<export_directory_iterator> 931 COFFObjectFile::export_directories() const { 932 return make_range(export_directory_begin(), export_directory_end()); 933 } 934 935 iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const { 936 return make_range(base_reloc_begin(), base_reloc_end()); 937 } 938 939 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 940 Res = PE32Header; 941 return std::error_code(); 942 } 943 944 std::error_code 945 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { 946 Res = PE32PlusHeader; 947 return std::error_code(); 948 } 949 950 std::error_code 951 COFFObjectFile::getDataDirectory(uint32_t Index, 952 const data_directory *&Res) const { 953 // Error if if there's no data directory or the index is out of range. 954 if (!DataDirectory) { 955 Res = nullptr; 956 return object_error::parse_failed; 957 } 958 assert(PE32Header || PE32PlusHeader); 959 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize 960 : PE32PlusHeader->NumberOfRvaAndSize; 961 if (Index >= NumEnt) { 962 Res = nullptr; 963 return object_error::parse_failed; 964 } 965 Res = &DataDirectory[Index]; 966 return std::error_code(); 967 } 968 969 std::error_code COFFObjectFile::getSection(int32_t Index, 970 const coff_section *&Result) const { 971 Result = nullptr; 972 if (COFF::isReservedSectionNumber(Index)) 973 return std::error_code(); 974 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) { 975 // We already verified the section table data, so no need to check again. 976 Result = SectionTable + (Index - 1); 977 return std::error_code(); 978 } 979 return object_error::parse_failed; 980 } 981 982 std::error_code COFFObjectFile::getString(uint32_t Offset, 983 StringRef &Result) const { 984 if (StringTableSize <= 4) 985 // Tried to get a string from an empty string table. 986 return object_error::parse_failed; 987 if (Offset >= StringTableSize) 988 return object_error::unexpected_eof; 989 Result = StringRef(StringTable + Offset); 990 return std::error_code(); 991 } 992 993 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol, 994 StringRef &Res) const { 995 return getSymbolName(Symbol.getGeneric(), Res); 996 } 997 998 std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol, 999 StringRef &Res) const { 1000 // Check for string table entry. First 4 bytes are 0. 1001 if (Symbol->Name.Offset.Zeroes == 0) { 1002 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res)) 1003 return EC; 1004 return std::error_code(); 1005 } 1006 1007 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0) 1008 // Null terminated, let ::strlen figure out the length. 1009 Res = StringRef(Symbol->Name.ShortName); 1010 else 1011 // Not null terminated, use all 8 bytes. 1012 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize); 1013 return std::error_code(); 1014 } 1015 1016 ArrayRef<uint8_t> 1017 COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const { 1018 const uint8_t *Aux = nullptr; 1019 1020 size_t SymbolSize = getSymbolTableEntrySize(); 1021 if (Symbol.getNumberOfAuxSymbols() > 0) { 1022 // AUX data comes immediately after the symbol in COFF 1023 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize; 1024 #ifndef NDEBUG 1025 // Verify that the Aux symbol points to a valid entry in the symbol table. 1026 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 1027 if (Offset < getPointerToSymbolTable() || 1028 Offset >= 1029 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize)) 1030 report_fatal_error("Aux Symbol data was outside of symbol table."); 1031 1032 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && 1033 "Aux Symbol data did not point to the beginning of a symbol"); 1034 #endif 1035 } 1036 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize); 1037 } 1038 1039 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec, 1040 StringRef &Res) const { 1041 StringRef Name; 1042 if (Sec->Name[COFF::NameSize - 1] == 0) 1043 // Null terminated, let ::strlen figure out the length. 1044 Name = Sec->Name; 1045 else 1046 // Not null terminated, use all 8 bytes. 1047 Name = StringRef(Sec->Name, COFF::NameSize); 1048 1049 // Check for string table entry. First byte is '/'. 1050 if (Name.startswith("/")) { 1051 uint32_t Offset; 1052 if (Name.startswith("//")) { 1053 if (decodeBase64StringEntry(Name.substr(2), Offset)) 1054 return object_error::parse_failed; 1055 } else { 1056 if (Name.substr(1).getAsInteger(10, Offset)) 1057 return object_error::parse_failed; 1058 } 1059 if (std::error_code EC = getString(Offset, Name)) 1060 return EC; 1061 } 1062 1063 Res = Name; 1064 return std::error_code(); 1065 } 1066 1067 uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const { 1068 // SizeOfRawData and VirtualSize change what they represent depending on 1069 // whether or not we have an executable image. 1070 // 1071 // For object files, SizeOfRawData contains the size of section's data; 1072 // VirtualSize should be zero but isn't due to buggy COFF writers. 1073 // 1074 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the 1075 // actual section size is in VirtualSize. It is possible for VirtualSize to 1076 // be greater than SizeOfRawData; the contents past that point should be 1077 // considered to be zero. 1078 if (getDOSHeader()) 1079 return std::min(Sec->VirtualSize, Sec->SizeOfRawData); 1080 return Sec->SizeOfRawData; 1081 } 1082 1083 std::error_code 1084 COFFObjectFile::getSectionContents(const coff_section *Sec, 1085 ArrayRef<uint8_t> &Res) const { 1086 // In COFF, a virtual section won't have any in-file 1087 // content, so the file pointer to the content will be zero. 1088 if (Sec->PointerToRawData == 0) 1089 return std::error_code(); 1090 // The only thing that we need to verify is that the contents is contained 1091 // within the file bounds. We don't need to make sure it doesn't cover other 1092 // data, as there's nothing that says that is not allowed. 1093 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 1094 uint32_t SectionSize = getSectionSize(Sec); 1095 if (checkOffset(Data, ConStart, SectionSize)) 1096 return object_error::parse_failed; 1097 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize); 1098 return std::error_code(); 1099 } 1100 1101 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 1102 return reinterpret_cast<const coff_relocation*>(Rel.p); 1103 } 1104 1105 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 1106 Rel.p = reinterpret_cast<uintptr_t>( 1107 reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 1108 } 1109 1110 uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const { 1111 const coff_relocation *R = toRel(Rel); 1112 return R->VirtualAddress; 1113 } 1114 1115 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 1116 const coff_relocation *R = toRel(Rel); 1117 DataRefImpl Ref; 1118 if (R->SymbolTableIndex >= getNumberOfSymbols()) 1119 return symbol_end(); 1120 if (SymbolTable16) 1121 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex); 1122 else if (SymbolTable32) 1123 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex); 1124 else 1125 llvm_unreachable("no symbol table pointer!"); 1126 return symbol_iterator(SymbolRef(Ref, this)); 1127 } 1128 1129 uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const { 1130 const coff_relocation* R = toRel(Rel); 1131 return R->Type; 1132 } 1133 1134 const coff_section * 1135 COFFObjectFile::getCOFFSection(const SectionRef &Section) const { 1136 return toSec(Section.getRawDataRefImpl()); 1137 } 1138 1139 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const { 1140 if (SymbolTable16) 1141 return toSymb<coff_symbol16>(Ref); 1142 if (SymbolTable32) 1143 return toSymb<coff_symbol32>(Ref); 1144 llvm_unreachable("no symbol table pointer!"); 1145 } 1146 1147 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { 1148 return getCOFFSymbol(Symbol.getRawDataRefImpl()); 1149 } 1150 1151 const coff_relocation * 1152 COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { 1153 return toRel(Reloc.getRawDataRefImpl()); 1154 } 1155 1156 iterator_range<const coff_relocation *> 1157 COFFObjectFile::getRelocations(const coff_section *Sec) const { 1158 const coff_relocation *I = getFirstReloc(Sec, Data, base()); 1159 const coff_relocation *E = I; 1160 if (I) 1161 E += getNumberOfRelocations(Sec, Data, base()); 1162 return make_range(I, E); 1163 } 1164 1165 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ 1166 case COFF::reloc_type: \ 1167 Res = #reloc_type; \ 1168 break; 1169 1170 void COFFObjectFile::getRelocationTypeName( 1171 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 1172 const coff_relocation *Reloc = toRel(Rel); 1173 StringRef Res; 1174 switch (getMachine()) { 1175 case COFF::IMAGE_FILE_MACHINE_AMD64: 1176 switch (Reloc->Type) { 1177 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 1178 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 1179 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 1180 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 1181 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 1182 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 1183 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 1184 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 1185 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 1186 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 1187 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 1188 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 1189 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 1190 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 1191 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 1192 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 1193 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 1194 default: 1195 Res = "Unknown"; 1196 } 1197 break; 1198 case COFF::IMAGE_FILE_MACHINE_ARMNT: 1199 switch (Reloc->Type) { 1200 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE); 1201 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32); 1202 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB); 1203 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24); 1204 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11); 1205 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN); 1206 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24); 1207 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11); 1208 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION); 1209 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL); 1210 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A); 1211 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T); 1212 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T); 1213 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T); 1214 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T); 1215 default: 1216 Res = "Unknown"; 1217 } 1218 break; 1219 case COFF::IMAGE_FILE_MACHINE_ARM64: 1220 switch (Reloc->Type) { 1221 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ABSOLUTE); 1222 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32); 1223 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32NB); 1224 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH26); 1225 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEBASE_REL21); 1226 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_REL21); 1227 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12A); 1228 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12L); 1229 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL); 1230 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12A); 1231 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_HIGH12A); 1232 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12L); 1233 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_TOKEN); 1234 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECTION); 1235 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR64); 1236 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH19); 1237 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH14); 1238 default: 1239 Res = "Unknown"; 1240 } 1241 break; 1242 case COFF::IMAGE_FILE_MACHINE_I386: 1243 switch (Reloc->Type) { 1244 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 1245 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 1246 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 1247 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 1248 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 1249 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 1250 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 1251 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 1252 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 1253 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 1254 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 1255 default: 1256 Res = "Unknown"; 1257 } 1258 break; 1259 default: 1260 Res = "Unknown"; 1261 } 1262 Result.append(Res.begin(), Res.end()); 1263 } 1264 1265 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 1266 1267 bool COFFObjectFile::isRelocatableObject() const { 1268 return !DataDirectory; 1269 } 1270 1271 bool ImportDirectoryEntryRef:: 1272 operator==(const ImportDirectoryEntryRef &Other) const { 1273 return ImportTable == Other.ImportTable && Index == Other.Index; 1274 } 1275 1276 void ImportDirectoryEntryRef::moveNext() { 1277 ++Index; 1278 if (ImportTable[Index].isNull()) { 1279 Index = -1; 1280 ImportTable = nullptr; 1281 } 1282 } 1283 1284 std::error_code ImportDirectoryEntryRef::getImportTableEntry( 1285 const coff_import_directory_table_entry *&Result) const { 1286 return getObject(Result, OwningObject->Data, ImportTable + Index); 1287 } 1288 1289 static imported_symbol_iterator 1290 makeImportedSymbolIterator(const COFFObjectFile *Object, 1291 uintptr_t Ptr, int Index) { 1292 if (Object->getBytesInAddress() == 4) { 1293 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr); 1294 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1295 } 1296 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr); 1297 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1298 } 1299 1300 static imported_symbol_iterator 1301 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) { 1302 uintptr_t IntPtr = 0; 1303 Object->getRvaPtr(RVA, IntPtr); 1304 return makeImportedSymbolIterator(Object, IntPtr, 0); 1305 } 1306 1307 static imported_symbol_iterator 1308 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) { 1309 uintptr_t IntPtr = 0; 1310 Object->getRvaPtr(RVA, IntPtr); 1311 // Forward the pointer to the last entry which is null. 1312 int Index = 0; 1313 if (Object->getBytesInAddress() == 4) { 1314 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr); 1315 while (*Entry++) 1316 ++Index; 1317 } else { 1318 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr); 1319 while (*Entry++) 1320 ++Index; 1321 } 1322 return makeImportedSymbolIterator(Object, IntPtr, Index); 1323 } 1324 1325 imported_symbol_iterator 1326 ImportDirectoryEntryRef::imported_symbol_begin() const { 1327 return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA, 1328 OwningObject); 1329 } 1330 1331 imported_symbol_iterator 1332 ImportDirectoryEntryRef::imported_symbol_end() const { 1333 return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA, 1334 OwningObject); 1335 } 1336 1337 iterator_range<imported_symbol_iterator> 1338 ImportDirectoryEntryRef::imported_symbols() const { 1339 return make_range(imported_symbol_begin(), imported_symbol_end()); 1340 } 1341 1342 imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const { 1343 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA, 1344 OwningObject); 1345 } 1346 1347 imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const { 1348 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA, 1349 OwningObject); 1350 } 1351 1352 iterator_range<imported_symbol_iterator> 1353 ImportDirectoryEntryRef::lookup_table_symbols() const { 1354 return make_range(lookup_table_begin(), lookup_table_end()); 1355 } 1356 1357 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 1358 uintptr_t IntPtr = 0; 1359 if (std::error_code EC = 1360 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr)) 1361 return EC; 1362 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1363 return std::error_code(); 1364 } 1365 1366 std::error_code 1367 ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const { 1368 Result = ImportTable[Index].ImportLookupTableRVA; 1369 return std::error_code(); 1370 } 1371 1372 std::error_code 1373 ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const { 1374 Result = ImportTable[Index].ImportAddressTableRVA; 1375 return std::error_code(); 1376 } 1377 1378 bool DelayImportDirectoryEntryRef:: 1379 operator==(const DelayImportDirectoryEntryRef &Other) const { 1380 return Table == Other.Table && Index == Other.Index; 1381 } 1382 1383 void DelayImportDirectoryEntryRef::moveNext() { 1384 ++Index; 1385 } 1386 1387 imported_symbol_iterator 1388 DelayImportDirectoryEntryRef::imported_symbol_begin() const { 1389 return importedSymbolBegin(Table[Index].DelayImportNameTable, 1390 OwningObject); 1391 } 1392 1393 imported_symbol_iterator 1394 DelayImportDirectoryEntryRef::imported_symbol_end() const { 1395 return importedSymbolEnd(Table[Index].DelayImportNameTable, 1396 OwningObject); 1397 } 1398 1399 iterator_range<imported_symbol_iterator> 1400 DelayImportDirectoryEntryRef::imported_symbols() const { 1401 return make_range(imported_symbol_begin(), imported_symbol_end()); 1402 } 1403 1404 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const { 1405 uintptr_t IntPtr = 0; 1406 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr)) 1407 return EC; 1408 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1409 return std::error_code(); 1410 } 1411 1412 std::error_code DelayImportDirectoryEntryRef:: 1413 getDelayImportTable(const delay_import_directory_table_entry *&Result) const { 1414 Result = Table; 1415 return std::error_code(); 1416 } 1417 1418 std::error_code DelayImportDirectoryEntryRef:: 1419 getImportAddress(int AddrIndex, uint64_t &Result) const { 1420 uint32_t RVA = Table[Index].DelayImportAddressTable + 1421 AddrIndex * (OwningObject->is64() ? 8 : 4); 1422 uintptr_t IntPtr = 0; 1423 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1424 return EC; 1425 if (OwningObject->is64()) 1426 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr); 1427 else 1428 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr); 1429 return std::error_code(); 1430 } 1431 1432 bool ExportDirectoryEntryRef:: 1433 operator==(const ExportDirectoryEntryRef &Other) const { 1434 return ExportTable == Other.ExportTable && Index == Other.Index; 1435 } 1436 1437 void ExportDirectoryEntryRef::moveNext() { 1438 ++Index; 1439 } 1440 1441 // Returns the name of the current export symbol. If the symbol is exported only 1442 // by ordinal, the empty string is set as a result. 1443 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { 1444 uintptr_t IntPtr = 0; 1445 if (std::error_code EC = 1446 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) 1447 return EC; 1448 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1449 return std::error_code(); 1450 } 1451 1452 // Returns the starting ordinal number. 1453 std::error_code 1454 ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { 1455 Result = ExportTable->OrdinalBase; 1456 return std::error_code(); 1457 } 1458 1459 // Returns the export ordinal of the current export symbol. 1460 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 1461 Result = ExportTable->OrdinalBase + Index; 1462 return std::error_code(); 1463 } 1464 1465 // Returns the address of the current export symbol. 1466 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 1467 uintptr_t IntPtr = 0; 1468 if (std::error_code EC = 1469 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) 1470 return EC; 1471 const export_address_table_entry *entry = 1472 reinterpret_cast<const export_address_table_entry *>(IntPtr); 1473 Result = entry[Index].ExportRVA; 1474 return std::error_code(); 1475 } 1476 1477 // Returns the name of the current export symbol. If the symbol is exported only 1478 // by ordinal, the empty string is set as a result. 1479 std::error_code 1480 ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { 1481 uintptr_t IntPtr = 0; 1482 if (std::error_code EC = 1483 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) 1484 return EC; 1485 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 1486 1487 uint32_t NumEntries = ExportTable->NumberOfNamePointers; 1488 int Offset = 0; 1489 for (const ulittle16_t *I = Start, *E = Start + NumEntries; 1490 I < E; ++I, ++Offset) { 1491 if (*I != Index) 1492 continue; 1493 if (std::error_code EC = 1494 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) 1495 return EC; 1496 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 1497 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 1498 return EC; 1499 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1500 return std::error_code(); 1501 } 1502 Result = ""; 1503 return std::error_code(); 1504 } 1505 1506 std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const { 1507 const data_directory *DataEntry; 1508 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 1509 return EC; 1510 uint32_t RVA; 1511 if (auto EC = getExportRVA(RVA)) 1512 return EC; 1513 uint32_t Begin = DataEntry->RelativeVirtualAddress; 1514 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size; 1515 Result = (Begin <= RVA && RVA < End); 1516 return std::error_code(); 1517 } 1518 1519 std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const { 1520 uint32_t RVA; 1521 if (auto EC = getExportRVA(RVA)) 1522 return EC; 1523 uintptr_t IntPtr = 0; 1524 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1525 return EC; 1526 Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1527 return std::error_code(); 1528 } 1529 1530 bool ImportedSymbolRef:: 1531 operator==(const ImportedSymbolRef &Other) const { 1532 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64 1533 && Index == Other.Index; 1534 } 1535 1536 void ImportedSymbolRef::moveNext() { 1537 ++Index; 1538 } 1539 1540 std::error_code 1541 ImportedSymbolRef::getSymbolName(StringRef &Result) const { 1542 uint32_t RVA; 1543 if (Entry32) { 1544 // If a symbol is imported only by ordinal, it has no name. 1545 if (Entry32[Index].isOrdinal()) 1546 return std::error_code(); 1547 RVA = Entry32[Index].getHintNameRVA(); 1548 } else { 1549 if (Entry64[Index].isOrdinal()) 1550 return std::error_code(); 1551 RVA = Entry64[Index].getHintNameRVA(); 1552 } 1553 uintptr_t IntPtr = 0; 1554 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1555 return EC; 1556 // +2 because the first two bytes is hint. 1557 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2)); 1558 return std::error_code(); 1559 } 1560 1561 std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const { 1562 if (Entry32) 1563 Result = Entry32[Index].isOrdinal(); 1564 else 1565 Result = Entry64[Index].isOrdinal(); 1566 return std::error_code(); 1567 } 1568 1569 std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const { 1570 if (Entry32) 1571 Result = Entry32[Index].getHintNameRVA(); 1572 else 1573 Result = Entry64[Index].getHintNameRVA(); 1574 return std::error_code(); 1575 } 1576 1577 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const { 1578 uint32_t RVA; 1579 if (Entry32) { 1580 if (Entry32[Index].isOrdinal()) { 1581 Result = Entry32[Index].getOrdinal(); 1582 return std::error_code(); 1583 } 1584 RVA = Entry32[Index].getHintNameRVA(); 1585 } else { 1586 if (Entry64[Index].isOrdinal()) { 1587 Result = Entry64[Index].getOrdinal(); 1588 return std::error_code(); 1589 } 1590 RVA = Entry64[Index].getHintNameRVA(); 1591 } 1592 uintptr_t IntPtr = 0; 1593 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1594 return EC; 1595 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr); 1596 return std::error_code(); 1597 } 1598 1599 ErrorOr<std::unique_ptr<COFFObjectFile>> 1600 ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) { 1601 std::error_code EC; 1602 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC)); 1603 if (EC) 1604 return EC; 1605 return std::move(Ret); 1606 } 1607 1608 bool BaseRelocRef::operator==(const BaseRelocRef &Other) const { 1609 return Header == Other.Header && Index == Other.Index; 1610 } 1611 1612 void BaseRelocRef::moveNext() { 1613 // Header->BlockSize is the size of the current block, including the 1614 // size of the header itself. 1615 uint32_t Size = sizeof(*Header) + 1616 sizeof(coff_base_reloc_block_entry) * (Index + 1); 1617 if (Size == Header->BlockSize) { 1618 // .reloc contains a list of base relocation blocks. Each block 1619 // consists of the header followed by entries. The header contains 1620 // how many entories will follow. When we reach the end of the 1621 // current block, proceed to the next block. 1622 Header = reinterpret_cast<const coff_base_reloc_block_header *>( 1623 reinterpret_cast<const uint8_t *>(Header) + Size); 1624 Index = 0; 1625 } else { 1626 ++Index; 1627 } 1628 } 1629 1630 std::error_code BaseRelocRef::getType(uint8_t &Type) const { 1631 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1); 1632 Type = Entry[Index].getType(); 1633 return std::error_code(); 1634 } 1635 1636 std::error_code BaseRelocRef::getRVA(uint32_t &Result) const { 1637 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1); 1638 Result = Header->PageRVA + Entry[Index].getOffset(); 1639 return std::error_code(); 1640 } 1641 1642 #define RETURN_IF_ERROR(X) \ 1643 if (auto EC = errorToErrorCode(X)) \ 1644 return EC; 1645 1646 ErrorOr<ArrayRef<UTF16>> ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) { 1647 BinaryStreamReader Reader = BinaryStreamReader(BBS); 1648 Reader.setOffset(Offset); 1649 uint16_t Length; 1650 RETURN_IF_ERROR(Reader.readInteger(Length)); 1651 ArrayRef<UTF16> RawDirString; 1652 RETURN_IF_ERROR(Reader.readArray(RawDirString, Length)); 1653 return RawDirString; 1654 } 1655 1656 ErrorOr<ArrayRef<UTF16>> 1657 ResourceSectionRef::getEntryNameString(const coff_resource_dir_entry &Entry) { 1658 return getDirStringAtOffset(Entry.Identifier.getNameOffset()); 1659 } 1660 1661 ErrorOr<const coff_resource_dir_table &> 1662 ResourceSectionRef::getTableAtOffset(uint32_t Offset) { 1663 const coff_resource_dir_table *Table = nullptr; 1664 1665 BinaryStreamReader Reader(BBS); 1666 Reader.setOffset(Offset); 1667 RETURN_IF_ERROR(Reader.readObject(Table)); 1668 assert(Table != nullptr); 1669 return *Table; 1670 } 1671 1672 ErrorOr<const coff_resource_dir_table &> 1673 ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) { 1674 return getTableAtOffset(Entry.Offset.value()); 1675 } 1676 1677 ErrorOr<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() { 1678 return getTableAtOffset(0); 1679 } 1680