18e90adafSMichael J. Spencer //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===// 28e90adafSMichael J. Spencer // 38e90adafSMichael J. Spencer // The LLVM Compiler Infrastructure 48e90adafSMichael J. Spencer // 58e90adafSMichael J. Spencer // This file is distributed under the University of Illinois Open Source 68e90adafSMichael J. Spencer // License. See LICENSE.TXT for details. 78e90adafSMichael J. Spencer // 88e90adafSMichael J. Spencer //===----------------------------------------------------------------------===// 98e90adafSMichael J. Spencer // 108e90adafSMichael J. Spencer // This file declares the COFFObjectFile class. 118e90adafSMichael J. Spencer // 128e90adafSMichael J. Spencer //===----------------------------------------------------------------------===// 138e90adafSMichael J. Spencer 14ec29b121SMichael J. Spencer #include "llvm/Object/COFF.h" 159da9e693SMichael J. Spencer #include "llvm/ADT/ArrayRef.h" 168e90adafSMichael J. Spencer #include "llvm/ADT/StringSwitch.h" 178e90adafSMichael J. Spencer #include "llvm/ADT/Triple.h" 186a75acb1SRui Ueyama #include "llvm/ADT/iterator_range.h" 19f078eff3SRui Ueyama #include "llvm/Support/COFF.h" 20c2bed429SRui Ueyama #include "llvm/Support/Debug.h" 21c2bed429SRui Ueyama #include "llvm/Support/raw_ostream.h" 22981af002SWill Dietz #include <cctype> 239d2c15efSNico Rieck #include <limits> 248e90adafSMichael J. Spencer 258e90adafSMichael J. Spencer using namespace llvm; 268e90adafSMichael J. Spencer using namespace object; 278e90adafSMichael J. Spencer 288e90adafSMichael J. Spencer using support::ulittle16_t; 298e90adafSMichael J. Spencer using support::ulittle32_t; 30861021f9SRui Ueyama using support::ulittle64_t; 318e90adafSMichael J. Spencer using support::little16_t; 328e90adafSMichael J. Spencer 331d6167fdSMichael J. Spencer // Returns false if size is greater than the buffer size. And sets ec. 3448af1c2aSRafael Espindola static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) { 35c3f9b5a5SRafael Espindola if (M.getBufferSize() < Size) { 368ff24d25SRui Ueyama EC = object_error::unexpected_eof; 371d6167fdSMichael J. Spencer return false; 381d6167fdSMichael J. Spencer } 391d6167fdSMichael J. Spencer return true; 408e90adafSMichael J. Spencer } 418e90adafSMichael J. Spencer 42e830c60dSDavid Majnemer static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, 4394751be7SDavid Majnemer const uint64_t Size) { 44e830c60dSDavid Majnemer if (Addr + Size < Addr || Addr + Size < Size || 45e830c60dSDavid Majnemer Addr + Size > uintptr_t(M.getBufferEnd()) || 46e830c60dSDavid Majnemer Addr < uintptr_t(M.getBufferStart())) { 47e830c60dSDavid Majnemer return object_error::unexpected_eof; 48e830c60dSDavid Majnemer } 497d099195SRui Ueyama return std::error_code(); 50e830c60dSDavid Majnemer } 51e830c60dSDavid Majnemer 52ed64342bSRui Ueyama // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. 53ed64342bSRui Ueyama // Returns unexpected_eof if error. 54ed64342bSRui Ueyama template <typename T> 5548af1c2aSRafael Espindola static std::error_code getObject(const T *&Obj, MemoryBufferRef M, 5658323a97SDavid Majnemer const void *Ptr, 57236b0ca7SDavid Majnemer const uint64_t Size = sizeof(T)) { 58ed64342bSRui Ueyama uintptr_t Addr = uintptr_t(Ptr); 59e830c60dSDavid Majnemer if (std::error_code EC = checkOffset(M, Addr, Size)) 60e830c60dSDavid Majnemer return EC; 61ed64342bSRui Ueyama Obj = reinterpret_cast<const T *>(Addr); 627d099195SRui Ueyama return std::error_code(); 631d6167fdSMichael J. Spencer } 641d6167fdSMichael J. Spencer 659d2c15efSNico Rieck // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without 669d2c15efSNico Rieck // prefixed slashes. 679d2c15efSNico Rieck static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) { 689d2c15efSNico Rieck assert(Str.size() <= 6 && "String too long, possible overflow."); 699d2c15efSNico Rieck if (Str.size() > 6) 709d2c15efSNico Rieck return true; 719d2c15efSNico Rieck 729d2c15efSNico Rieck uint64_t Value = 0; 739d2c15efSNico Rieck while (!Str.empty()) { 749d2c15efSNico Rieck unsigned CharVal; 759d2c15efSNico Rieck if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25 769d2c15efSNico Rieck CharVal = Str[0] - 'A'; 779d2c15efSNico Rieck else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51 789d2c15efSNico Rieck CharVal = Str[0] - 'a' + 26; 799d2c15efSNico Rieck else if (Str[0] >= '0' && Str[0] <= '9') // 52..61 809d2c15efSNico Rieck CharVal = Str[0] - '0' + 52; 819d2c15efSNico Rieck else if (Str[0] == '+') // 62 825500b07cSRui Ueyama CharVal = 62; 839d2c15efSNico Rieck else if (Str[0] == '/') // 63 845500b07cSRui Ueyama CharVal = 63; 859d2c15efSNico Rieck else 869d2c15efSNico Rieck return true; 879d2c15efSNico Rieck 889d2c15efSNico Rieck Value = (Value * 64) + CharVal; 899d2c15efSNico Rieck Str = Str.substr(1); 909d2c15efSNico Rieck } 919d2c15efSNico Rieck 929d2c15efSNico Rieck if (Value > std::numeric_limits<uint32_t>::max()) 939d2c15efSNico Rieck return true; 949d2c15efSNico Rieck 959d2c15efSNico Rieck Result = static_cast<uint32_t>(Value); 969d2c15efSNico Rieck return false; 979d2c15efSNico Rieck } 989d2c15efSNico Rieck 9944f51e51SDavid Majnemer template <typename coff_symbol_type> 10044f51e51SDavid Majnemer const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const { 10144f51e51SDavid Majnemer const coff_symbol_type *Addr = 10244f51e51SDavid Majnemer reinterpret_cast<const coff_symbol_type *>(Ref.p); 1031d6167fdSMichael J. Spencer 104236b0ca7SDavid Majnemer assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr))); 1051d6167fdSMichael J. Spencer #ifndef NDEBUG 1061d6167fdSMichael J. Spencer // Verify that the symbol points to a valid entry in the symbol table. 1078ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); 1081d6167fdSMichael J. Spencer 10944f51e51SDavid Majnemer assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 && 11044f51e51SDavid Majnemer "Symbol did not point to the beginning of a symbol"); 1111d6167fdSMichael J. Spencer #endif 1121d6167fdSMichael J. Spencer 1138ff24d25SRui Ueyama return Addr; 1141d6167fdSMichael J. Spencer } 1151d6167fdSMichael J. Spencer 1168ff24d25SRui Ueyama const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { 1178ff24d25SRui Ueyama const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); 1181d6167fdSMichael J. Spencer 1191d6167fdSMichael J. Spencer # ifndef NDEBUG 1201d6167fdSMichael J. Spencer // Verify that the section points to a valid entry in the section table. 12144f51e51SDavid Majnemer if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections())) 1221d6167fdSMichael J. Spencer report_fatal_error("Section was outside of section table."); 1231d6167fdSMichael J. Spencer 1248ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); 1258ff24d25SRui Ueyama assert(Offset % sizeof(coff_section) == 0 && 1261d6167fdSMichael J. Spencer "Section did not point to the beginning of a section"); 1271d6167fdSMichael J. Spencer # endif 1281d6167fdSMichael J. Spencer 1298ff24d25SRui Ueyama return Addr; 1301d6167fdSMichael J. Spencer } 1311d6167fdSMichael J. Spencer 1325e812afaSRafael Espindola void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { 133236b0ca7SDavid Majnemer auto End = reinterpret_cast<uintptr_t>(StringTable); 13444f51e51SDavid Majnemer if (SymbolTable16) { 13544f51e51SDavid Majnemer const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref); 1368ff24d25SRui Ueyama Symb += 1 + Symb->NumberOfAuxSymbols; 137236b0ca7SDavid Majnemer Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End); 13844f51e51SDavid Majnemer } else if (SymbolTable32) { 13944f51e51SDavid Majnemer const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref); 14044f51e51SDavid Majnemer Symb += 1 + Symb->NumberOfAuxSymbols; 141236b0ca7SDavid Majnemer Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End); 14244f51e51SDavid Majnemer } else { 14344f51e51SDavid Majnemer llvm_unreachable("no symbol table pointer!"); 14444f51e51SDavid Majnemer } 1451d6167fdSMichael J. Spencer } 1461d6167fdSMichael J. Spencer 14781e8b7d9SKevin Enderby Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const { 14844f51e51SDavid Majnemer COFFSymbolRef Symb = getCOFFSymbol(Ref); 1495d0c2ffaSRafael Espindola StringRef Result; 1505d0c2ffaSRafael Espindola std::error_code EC = getSymbolName(Symb, Result); 1515d0c2ffaSRafael Espindola if (EC) 15281e8b7d9SKevin Enderby return errorCodeToError(EC); 1535d0c2ffaSRafael Espindola return Result; 1548e90adafSMichael J. Spencer } 1558e90adafSMichael J. Spencer 156be8b0ea8SRafael Espindola uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const { 157be8b0ea8SRafael Espindola return getCOFFSymbol(Ref).getValue(); 158991af666SRafael Espindola } 159991af666SRafael Espindola 160ed067c45SRafael Espindola ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const { 161ed067c45SRafael Espindola uint64_t Result = getSymbolValue(Ref); 16244f51e51SDavid Majnemer COFFSymbolRef Symb = getCOFFSymbol(Ref); 163c7d7c6fbSDavid Majnemer int32_t SectionNumber = Symb.getSectionNumber(); 164991af666SRafael Espindola 165991af666SRafael Espindola if (Symb.isAnyUndefined() || Symb.isCommon() || 166991af666SRafael Espindola COFF::isReservedSectionNumber(SectionNumber)) 167ed067c45SRafael Espindola return Result; 16854c9f3daSRafael Espindola 1692617dcceSCraig Topper const coff_section *Section = nullptr; 170c7d7c6fbSDavid Majnemer if (std::error_code EC = getSection(SectionNumber, Section)) 1718ff24d25SRui Ueyama return EC; 172991af666SRafael Espindola Result += Section->VirtualAddress; 17347ea9eceSReid Kleckner 17447ea9eceSReid Kleckner // The section VirtualAddress does not include ImageBase, and we want to 17547ea9eceSReid Kleckner // return virtual addresses. 17621427adaSReid Kleckner Result += getImageBase(); 17747ea9eceSReid Kleckner 178ed067c45SRafael Espindola return Result; 179c7d7c6fbSDavid Majnemer } 180c7d7c6fbSDavid Majnemer 1817bd8d994SKevin Enderby Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const { 18244f51e51SDavid Majnemer COFFSymbolRef Symb = getCOFFSymbol(Ref); 183c7d7c6fbSDavid Majnemer int32_t SectionNumber = Symb.getSectionNumber(); 18444f51e51SDavid Majnemer 185e834f420SPeter Collingbourne if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) 186e834f420SPeter Collingbourne return SymbolRef::ST_Function; 1872fa80cc5SRafael Espindola if (Symb.isAnyUndefined()) 1882fa80cc5SRafael Espindola return SymbolRef::ST_Unknown; 1892fa80cc5SRafael Espindola if (Symb.isCommon()) 1902fa80cc5SRafael Espindola return SymbolRef::ST_Data; 1912fa80cc5SRafael Espindola if (Symb.isFileRecord()) 1922fa80cc5SRafael Espindola return SymbolRef::ST_File; 1932fa80cc5SRafael Espindola 1941a666e0fSDavid Majnemer // TODO: perhaps we need a new symbol type ST_Section. 1952fa80cc5SRafael Espindola if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition()) 1962fa80cc5SRafael Espindola return SymbolRef::ST_Debug; 1972fa80cc5SRafael Espindola 1982fa80cc5SRafael Espindola if (!COFF::isReservedSectionNumber(SectionNumber)) 1992fa80cc5SRafael Espindola return SymbolRef::ST_Data; 2002fa80cc5SRafael Espindola 2012fa80cc5SRafael Espindola return SymbolRef::ST_Other; 20275d1cf33SBenjamin Kramer } 20375d1cf33SBenjamin Kramer 20420122a43SRafael Espindola uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { 20544f51e51SDavid Majnemer COFFSymbolRef Symb = getCOFFSymbol(Ref); 20620122a43SRafael Espindola uint32_t Result = SymbolRef::SF_None; 20775d1cf33SBenjamin Kramer 208c7d7c6fbSDavid Majnemer if (Symb.isExternal() || Symb.isWeakExternal()) 2099dc0eb42SLang Hames Result |= SymbolRef::SF_Global; 2101df4b84dSDavid Meyer 211c7d7c6fbSDavid Majnemer if (Symb.isWeakExternal()) 2121df4b84dSDavid Meyer Result |= SymbolRef::SF_Weak; 2131df4b84dSDavid Meyer 21444f51e51SDavid Majnemer if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE) 2151df4b84dSDavid Meyer Result |= SymbolRef::SF_Absolute; 2161df4b84dSDavid Meyer 217c7d7c6fbSDavid Majnemer if (Symb.isFileRecord()) 218c7d7c6fbSDavid Majnemer Result |= SymbolRef::SF_FormatSpecific; 219c7d7c6fbSDavid Majnemer 220c7d7c6fbSDavid Majnemer if (Symb.isSectionDefinition()) 221c7d7c6fbSDavid Majnemer Result |= SymbolRef::SF_FormatSpecific; 222c7d7c6fbSDavid Majnemer 223c7d7c6fbSDavid Majnemer if (Symb.isCommon()) 224c7d7c6fbSDavid Majnemer Result |= SymbolRef::SF_Common; 225c7d7c6fbSDavid Majnemer 226c7d7c6fbSDavid Majnemer if (Symb.isAnyUndefined()) 227c7d7c6fbSDavid Majnemer Result |= SymbolRef::SF_Undefined; 228c7d7c6fbSDavid Majnemer 22920122a43SRafael Espindola return Result; 23001759754SMichael J. Spencer } 23101759754SMichael J. Spencer 232d7a32ea4SRafael Espindola uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const { 233c7d7c6fbSDavid Majnemer COFFSymbolRef Symb = getCOFFSymbol(Ref); 2345eb02e45SRafael Espindola return Symb.getValue(); 2358e90adafSMichael J. Spencer } 2368e90adafSMichael J. Spencer 2377bd8d994SKevin Enderby Expected<section_iterator> 2388bab889bSRafael Espindola COFFObjectFile::getSymbolSection(DataRefImpl Ref) const { 23944f51e51SDavid Majnemer COFFSymbolRef Symb = getCOFFSymbol(Ref); 2408bab889bSRafael Espindola if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) 2418bab889bSRafael Espindola return section_end(); 2422617dcceSCraig Topper const coff_section *Sec = nullptr; 24344f51e51SDavid Majnemer if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec)) 2447bd8d994SKevin Enderby return errorCodeToError(EC); 2458bab889bSRafael Espindola DataRefImpl Ret; 2468bab889bSRafael Espindola Ret.p = reinterpret_cast<uintptr_t>(Sec); 2478bab889bSRafael Espindola return section_iterator(SectionRef(Ret, this)); 24832173153SMichael J. Spencer } 24932173153SMichael J. Spencer 2506bf32210SRafael Espindola unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const { 2516bf32210SRafael Espindola COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl()); 2526bf32210SRafael Espindola return Symb.getSectionNumber(); 2536bf32210SRafael Espindola } 2546bf32210SRafael Espindola 2555e812afaSRafael Espindola void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { 2568ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2578ff24d25SRui Ueyama Sec += 1; 2588ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Sec); 2598e90adafSMichael J. Spencer } 2608e90adafSMichael J. Spencer 261db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref, 2621d6167fdSMichael J. Spencer StringRef &Result) const { 2638ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2648ff24d25SRui Ueyama return getSectionName(Sec, Result); 2658e90adafSMichael J. Spencer } 2668e90adafSMichael J. Spencer 26780291274SRafael Espindola uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const { 2688ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2697c6a071bSDavid Majnemer uint64_t Result = Sec->VirtualAddress; 2707c6a071bSDavid Majnemer 2717c6a071bSDavid Majnemer // The section VirtualAddress does not include ImageBase, and we want to 2727c6a071bSDavid Majnemer // return virtual addresses. 27321427adaSReid Kleckner Result += getImageBase(); 2747c6a071bSDavid Majnemer return Result; 2758e90adafSMichael J. Spencer } 2768e90adafSMichael J. Spencer 27780291274SRafael Espindola uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const { 278a9ee5c06SDavid Majnemer return getSectionSize(toSec(Ref)); 2798e90adafSMichael J. Spencer } 2808e90adafSMichael J. Spencer 281db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, 2821d6167fdSMichael J. Spencer StringRef &Result) const { 2838ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2849da9e693SMichael J. Spencer ArrayRef<uint8_t> Res; 285db4ed0bdSRafael Espindola std::error_code EC = getSectionContents(Sec, Res); 2869da9e693SMichael J. Spencer Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); 2879da9e693SMichael J. Spencer return EC; 2888e90adafSMichael J. Spencer } 2898e90adafSMichael J. Spencer 29080291274SRafael Espindola uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const { 2918ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 292511391feSDavid Majnemer return Sec->getAlignment(); 2937989460aSMichael J. Spencer } 2947989460aSMichael J. Spencer 295*401e4e57SGeorge Rimar bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const { 296*401e4e57SGeorge Rimar return false; 297*401e4e57SGeorge Rimar } 298*401e4e57SGeorge Rimar 29980291274SRafael Espindola bool COFFObjectFile::isSectionText(DataRefImpl Ref) const { 3008ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 30180291274SRafael Espindola return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 3028e90adafSMichael J. Spencer } 3038e90adafSMichael J. Spencer 30480291274SRafael Espindola bool COFFObjectFile::isSectionData(DataRefImpl Ref) const { 3058ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 30680291274SRafael Espindola return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 307800619f2SMichael J. Spencer } 308800619f2SMichael J. Spencer 30980291274SRafael Espindola bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const { 3108ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3111a666e0fSDavid Majnemer const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | 3121a666e0fSDavid Majnemer COFF::IMAGE_SCN_MEM_READ | 3131a666e0fSDavid Majnemer COFF::IMAGE_SCN_MEM_WRITE; 3141a666e0fSDavid Majnemer return (Sec->Characteristics & BssFlags) == BssFlags; 315800619f2SMichael J. Spencer } 316800619f2SMichael J. Spencer 3176bf32210SRafael Espindola unsigned COFFObjectFile::getSectionID(SectionRef Sec) const { 3186bf32210SRafael Espindola uintptr_t Offset = 3196bf32210SRafael Espindola uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable); 3206bf32210SRafael Espindola assert((Offset % sizeof(coff_section)) == 0); 3216bf32210SRafael Espindola return (Offset / sizeof(coff_section)) + 1; 3226bf32210SRafael Espindola } 3236bf32210SRafael Espindola 32480291274SRafael Espindola bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const { 3258ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3261a666e0fSDavid Majnemer // In COFF, a virtual section won't have any in-file 3271a666e0fSDavid Majnemer // content, so the file pointer to the content will be zero. 3281a666e0fSDavid Majnemer return Sec->PointerToRawData == 0; 3292138ef6dSPreston Gurd } 3302138ef6dSPreston Gurd 331e830c60dSDavid Majnemer static uint32_t getNumberOfRelocations(const coff_section *Sec, 332e830c60dSDavid Majnemer MemoryBufferRef M, const uint8_t *base) { 333e830c60dSDavid Majnemer // The field for the number of relocations in COFF section table is only 334e830c60dSDavid Majnemer // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to 335e830c60dSDavid Majnemer // NumberOfRelocations field, and the actual relocation count is stored in the 336e830c60dSDavid Majnemer // VirtualAddress field in the first relocation entry. 337e830c60dSDavid Majnemer if (Sec->hasExtendedRelocations()) { 338e830c60dSDavid Majnemer const coff_relocation *FirstReloc; 339e830c60dSDavid Majnemer if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>( 340e830c60dSDavid Majnemer base + Sec->PointerToRelocations))) 341e830c60dSDavid Majnemer return 0; 34298fe58a3SRui Ueyama // -1 to exclude this first relocation entry. 34398fe58a3SRui Ueyama return FirstReloc->VirtualAddress - 1; 344e830c60dSDavid Majnemer } 345e830c60dSDavid Majnemer return Sec->NumberOfRelocations; 346e830c60dSDavid Majnemer } 347e830c60dSDavid Majnemer 34894751be7SDavid Majnemer static const coff_relocation * 34994751be7SDavid Majnemer getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) { 35094751be7SDavid Majnemer uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base); 35194751be7SDavid Majnemer if (!NumRelocs) 35294751be7SDavid Majnemer return nullptr; 353827c8a2bSRui Ueyama auto begin = reinterpret_cast<const coff_relocation *>( 35494751be7SDavid Majnemer Base + Sec->PointerToRelocations); 355827c8a2bSRui Ueyama if (Sec->hasExtendedRelocations()) { 356827c8a2bSRui Ueyama // Skip the first relocation entry repurposed to store the number of 357827c8a2bSRui Ueyama // relocations. 358827c8a2bSRui Ueyama begin++; 359827c8a2bSRui Ueyama } 36094751be7SDavid Majnemer if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs)) 36194751be7SDavid Majnemer return nullptr; 36294751be7SDavid Majnemer return begin; 363827c8a2bSRui Ueyama } 36494751be7SDavid Majnemer 36594751be7SDavid Majnemer relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 36694751be7SDavid Majnemer const coff_section *Sec = toSec(Ref); 36794751be7SDavid Majnemer const coff_relocation *begin = getFirstReloc(Sec, Data, base()); 36876d650e8SRafael Espindola if (begin && Sec->VirtualAddress != 0) 36976d650e8SRafael Espindola report_fatal_error("Sections with relocations should have an address of 0"); 37094751be7SDavid Majnemer DataRefImpl Ret; 37194751be7SDavid Majnemer Ret.p = reinterpret_cast<uintptr_t>(begin); 3728ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 373e5fd0047SMichael J. Spencer } 374e5fd0047SMichael J. Spencer 3758ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 3768ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 37794751be7SDavid Majnemer const coff_relocation *I = getFirstReloc(Sec, Data, base()); 37894751be7SDavid Majnemer if (I) 37994751be7SDavid Majnemer I += getNumberOfRelocations(Sec, Data, base()); 3808ff24d25SRui Ueyama DataRefImpl Ret; 38194751be7SDavid Majnemer Ret.p = reinterpret_cast<uintptr_t>(I); 3828ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 383e5fd0047SMichael J. Spencer } 384e5fd0047SMichael J. Spencer 385c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 386db4ed0bdSRafael Espindola std::error_code COFFObjectFile::initSymbolTablePtr() { 38744f51e51SDavid Majnemer if (COFFHeader) 388236b0ca7SDavid Majnemer if (std::error_code EC = getObject( 389236b0ca7SDavid Majnemer SymbolTable16, Data, base() + getPointerToSymbolTable(), 390236b0ca7SDavid Majnemer (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize())) 39144f51e51SDavid Majnemer return EC; 39244f51e51SDavid Majnemer 39344f51e51SDavid Majnemer if (COFFBigObjHeader) 394236b0ca7SDavid Majnemer if (std::error_code EC = getObject( 395236b0ca7SDavid Majnemer SymbolTable32, Data, base() + getPointerToSymbolTable(), 396236b0ca7SDavid Majnemer (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize())) 3978ff24d25SRui Ueyama return EC; 398c2bed429SRui Ueyama 399c2bed429SRui Ueyama // Find string table. The first four byte of the string table contains the 400c2bed429SRui Ueyama // total size of the string table, including the size field itself. If the 401c2bed429SRui Ueyama // string table is empty, the value of the first four byte would be 4. 402f69b0585SDavid Majnemer uint32_t StringTableOffset = getPointerToSymbolTable() + 40344f51e51SDavid Majnemer getNumberOfSymbols() * getSymbolTableEntrySize(); 404f69b0585SDavid Majnemer const uint8_t *StringTableAddr = base() + StringTableOffset; 405c2bed429SRui Ueyama const ulittle32_t *StringTableSizePtr; 40648af1c2aSRafael Espindola if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 4078ff24d25SRui Ueyama return EC; 408c2bed429SRui Ueyama StringTableSize = *StringTableSizePtr; 409db4ed0bdSRafael Espindola if (std::error_code EC = 41048af1c2aSRafael Espindola getObject(StringTable, Data, StringTableAddr, StringTableSize)) 4118ff24d25SRui Ueyama return EC; 412c2bed429SRui Ueyama 413773a5795SNico Rieck // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some 414773a5795SNico Rieck // tools like cvtres write a size of 0 for an empty table instead of 4. 415773a5795SNico Rieck if (StringTableSize < 4) 416773a5795SNico Rieck StringTableSize = 4; 417773a5795SNico Rieck 418c2bed429SRui Ueyama // Check that the string table is null terminated if has any in it. 419773a5795SNico Rieck if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) 420c2bed429SRui Ueyama return object_error::parse_failed; 4217d099195SRui Ueyama return std::error_code(); 422c2bed429SRui Ueyama } 423c2bed429SRui Ueyama 42421427adaSReid Kleckner uint64_t COFFObjectFile::getImageBase() const { 425e94fef7bSReid Kleckner if (PE32Header) 42621427adaSReid Kleckner return PE32Header->ImageBase; 427e94fef7bSReid Kleckner else if (PE32PlusHeader) 42821427adaSReid Kleckner return PE32PlusHeader->ImageBase; 42921427adaSReid Kleckner // This actually comes up in practice. 43021427adaSReid Kleckner return 0; 431e94fef7bSReid Kleckner } 432e94fef7bSReid Kleckner 433215a586cSRui Ueyama // Returns the file offset for the given VA. 434db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { 43521427adaSReid Kleckner uint64_t ImageBase = getImageBase(); 436b7a40081SRui Ueyama uint64_t Rva = Addr - ImageBase; 437b7a40081SRui Ueyama assert(Rva <= UINT32_MAX); 438b7a40081SRui Ueyama return getRvaPtr((uint32_t)Rva, Res); 439215a586cSRui Ueyama } 440215a586cSRui Ueyama 441c2bed429SRui Ueyama // Returns the file offset for the given RVA. 442db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { 44327dc8394SAlexey Samsonov for (const SectionRef &S : sections()) { 44427dc8394SAlexey Samsonov const coff_section *Section = getCOFFSection(S); 445c2bed429SRui Ueyama uint32_t SectionStart = Section->VirtualAddress; 446c2bed429SRui Ueyama uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 447215a586cSRui Ueyama if (SectionStart <= Addr && Addr < SectionEnd) { 448215a586cSRui Ueyama uint32_t Offset = Addr - SectionStart; 449c2bed429SRui Ueyama Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 4507d099195SRui Ueyama return std::error_code(); 451c2bed429SRui Ueyama } 452c2bed429SRui Ueyama } 453c2bed429SRui Ueyama return object_error::parse_failed; 454c2bed429SRui Ueyama } 455c2bed429SRui Ueyama 456c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 457c2bed429SRui Ueyama // table entry. 458db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint, 459db4ed0bdSRafael Espindola StringRef &Name) const { 460c2bed429SRui Ueyama uintptr_t IntPtr = 0; 461db4ed0bdSRafael Espindola if (std::error_code EC = getRvaPtr(Rva, IntPtr)) 4628ff24d25SRui Ueyama return EC; 463c2bed429SRui Ueyama const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 464c2bed429SRui Ueyama Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 465c2bed429SRui Ueyama Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 4667d099195SRui Ueyama return std::error_code(); 467c2bed429SRui Ueyama } 468c2bed429SRui Ueyama 469c2bed429SRui Ueyama // Find the import table. 470db4ed0bdSRafael Espindola std::error_code COFFObjectFile::initImportTablePtr() { 471c2bed429SRui Ueyama // First, we get the RVA of the import table. If the file lacks a pointer to 472c2bed429SRui Ueyama // the import table, do nothing. 473c2bed429SRui Ueyama const data_directory *DataEntry; 474c2bed429SRui Ueyama if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 4757d099195SRui Ueyama return std::error_code(); 476c2bed429SRui Ueyama 477c2bed429SRui Ueyama // Do nothing if the pointer to import table is NULL. 478c2bed429SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 4797d099195SRui Ueyama return std::error_code(); 480c2bed429SRui Ueyama 481c2bed429SRui Ueyama uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 4821e152d5eSRui Ueyama // -1 because the last entry is the null entry. 483c2bed429SRui Ueyama NumberOfImportDirectory = DataEntry->Size / 4841e152d5eSRui Ueyama sizeof(import_directory_table_entry) - 1; 485c2bed429SRui Ueyama 486c2bed429SRui Ueyama // Find the section that contains the RVA. This is needed because the RVA is 487c2bed429SRui Ueyama // the import table's memory address which is different from its file offset. 488c2bed429SRui Ueyama uintptr_t IntPtr = 0; 489db4ed0bdSRafael Espindola if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 4908ff24d25SRui Ueyama return EC; 491c2bed429SRui Ueyama ImportDirectory = reinterpret_cast< 492c2bed429SRui Ueyama const import_directory_table_entry *>(IntPtr); 4937d099195SRui Ueyama return std::error_code(); 494ad882ba8SRui Ueyama } 495c2bed429SRui Ueyama 49615d99359SRui Ueyama // Initializes DelayImportDirectory and NumberOfDelayImportDirectory. 49715d99359SRui Ueyama std::error_code COFFObjectFile::initDelayImportTablePtr() { 49815d99359SRui Ueyama const data_directory *DataEntry; 49915d99359SRui Ueyama if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry)) 5007d099195SRui Ueyama return std::error_code(); 50115d99359SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 5027d099195SRui Ueyama return std::error_code(); 50315d99359SRui Ueyama 50415d99359SRui Ueyama uint32_t RVA = DataEntry->RelativeVirtualAddress; 50515d99359SRui Ueyama NumberOfDelayImportDirectory = DataEntry->Size / 50615d99359SRui Ueyama sizeof(delay_import_directory_table_entry) - 1; 50715d99359SRui Ueyama 50815d99359SRui Ueyama uintptr_t IntPtr = 0; 50915d99359SRui Ueyama if (std::error_code EC = getRvaPtr(RVA, IntPtr)) 51015d99359SRui Ueyama return EC; 51115d99359SRui Ueyama DelayImportDirectory = reinterpret_cast< 51215d99359SRui Ueyama const delay_import_directory_table_entry *>(IntPtr); 5137d099195SRui Ueyama return std::error_code(); 51415d99359SRui Ueyama } 51515d99359SRui Ueyama 516ad882ba8SRui Ueyama // Find the export table. 517db4ed0bdSRafael Espindola std::error_code COFFObjectFile::initExportTablePtr() { 518ad882ba8SRui Ueyama // First, we get the RVA of the export table. If the file lacks a pointer to 519ad882ba8SRui Ueyama // the export table, do nothing. 520ad882ba8SRui Ueyama const data_directory *DataEntry; 521ad882ba8SRui Ueyama if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 5227d099195SRui Ueyama return std::error_code(); 523ad882ba8SRui Ueyama 524ad882ba8SRui Ueyama // Do nothing if the pointer to export table is NULL. 525ad882ba8SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 5267d099195SRui Ueyama return std::error_code(); 527ad882ba8SRui Ueyama 528ad882ba8SRui Ueyama uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 529ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 530db4ed0bdSRafael Espindola if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 531ad882ba8SRui Ueyama return EC; 53224fc2d64SRui Ueyama ExportDirectory = 53324fc2d64SRui Ueyama reinterpret_cast<const export_directory_table_entry *>(IntPtr); 5347d099195SRui Ueyama return std::error_code(); 535c2bed429SRui Ueyama } 536c2bed429SRui Ueyama 53774e85130SRui Ueyama std::error_code COFFObjectFile::initBaseRelocPtr() { 53874e85130SRui Ueyama const data_directory *DataEntry; 53974e85130SRui Ueyama if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry)) 5407d099195SRui Ueyama return std::error_code(); 54174e85130SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 5427d099195SRui Ueyama return std::error_code(); 54374e85130SRui Ueyama 54474e85130SRui Ueyama uintptr_t IntPtr = 0; 54574e85130SRui Ueyama if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr)) 54674e85130SRui Ueyama return EC; 54774e85130SRui Ueyama BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>( 54874e85130SRui Ueyama IntPtr); 54974e85130SRui Ueyama BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>( 55074e85130SRui Ueyama IntPtr + DataEntry->Size); 5517d099195SRui Ueyama return std::error_code(); 55274e85130SRui Ueyama } 55374e85130SRui Ueyama 55448af1c2aSRafael Espindola COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC) 55548af1c2aSRafael Espindola : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr), 55644f51e51SDavid Majnemer COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr), 55744f51e51SDavid Majnemer DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr), 55844f51e51SDavid Majnemer SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0), 55944f51e51SDavid Majnemer ImportDirectory(nullptr), NumberOfImportDirectory(0), 56015d99359SRui Ueyama DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0), 56174e85130SRui Ueyama ExportDirectory(nullptr), BaseRelocHeader(nullptr), 56274e85130SRui Ueyama BaseRelocEnd(nullptr) { 5631d6167fdSMichael J. Spencer // Check that we at least have enough room for a header. 56448af1c2aSRafael Espindola if (!checkSize(Data, EC, sizeof(coff_file_header))) 565c3f9b5a5SRafael Espindola return; 566ee066fc4SEric Christopher 56782ebd8e3SRui Ueyama // The current location in the file where we are looking at. 56882ebd8e3SRui Ueyama uint64_t CurPtr = 0; 56982ebd8e3SRui Ueyama 57082ebd8e3SRui Ueyama // PE header is optional and is present only in executables. If it exists, 57182ebd8e3SRui Ueyama // it is placed right after COFF header. 5728ff24d25SRui Ueyama bool HasPEHeader = false; 573ee066fc4SEric Christopher 5741d6167fdSMichael J. Spencer // Check if this is a PE/COFF file. 57550267222SDavid Majnemer if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) { 576ee066fc4SEric Christopher // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 577ee066fc4SEric Christopher // PE signature to find 'normal' COFF header. 57850267222SDavid Majnemer const auto *DH = reinterpret_cast<const dos_header *>(base()); 57950267222SDavid Majnemer if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') { 58050267222SDavid Majnemer CurPtr = DH->AddressOfNewExeHeader; 58182ebd8e3SRui Ueyama // Check the PE magic bytes. ("PE\0\0") 58250267222SDavid Majnemer if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) { 5838ff24d25SRui Ueyama EC = object_error::parse_failed; 5841d6167fdSMichael J. Spencer return; 5851d6167fdSMichael J. Spencer } 58644f51e51SDavid Majnemer CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes. 5878ff24d25SRui Ueyama HasPEHeader = true; 588ee066fc4SEric Christopher } 58950267222SDavid Majnemer } 590ee066fc4SEric Christopher 59148af1c2aSRafael Espindola if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 5921d6167fdSMichael J. Spencer return; 59344f51e51SDavid Majnemer 59444f51e51SDavid Majnemer // It might be a bigobj file, let's check. Note that COFF bigobj and COFF 59544f51e51SDavid Majnemer // import libraries share a common prefix but bigobj is more restrictive. 59644f51e51SDavid Majnemer if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN && 59744f51e51SDavid Majnemer COFFHeader->NumberOfSections == uint16_t(0xffff) && 59844f51e51SDavid Majnemer checkSize(Data, EC, sizeof(coff_bigobj_file_header))) { 59944f51e51SDavid Majnemer if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr))) 60044f51e51SDavid Majnemer return; 60144f51e51SDavid Majnemer 60244f51e51SDavid Majnemer // Verify that we are dealing with bigobj. 60344f51e51SDavid Majnemer if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion && 60444f51e51SDavid Majnemer std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic, 60544f51e51SDavid Majnemer sizeof(COFF::BigObjMagic)) == 0) { 60644f51e51SDavid Majnemer COFFHeader = nullptr; 60744f51e51SDavid Majnemer CurPtr += sizeof(coff_bigobj_file_header); 60844f51e51SDavid Majnemer } else { 60944f51e51SDavid Majnemer // It's not a bigobj. 61044f51e51SDavid Majnemer COFFBigObjHeader = nullptr; 61144f51e51SDavid Majnemer } 61244f51e51SDavid Majnemer } 61344f51e51SDavid Majnemer if (COFFHeader) { 61444f51e51SDavid Majnemer // The prior checkSize call may have failed. This isn't a hard error 61544f51e51SDavid Majnemer // because we were just trying to sniff out bigobj. 6167d099195SRui Ueyama EC = std::error_code(); 61782ebd8e3SRui Ueyama CurPtr += sizeof(coff_file_header); 61882ebd8e3SRui Ueyama 61944f51e51SDavid Majnemer if (COFFHeader->isImportLibrary()) 62044f51e51SDavid Majnemer return; 62144f51e51SDavid Majnemer } 62244f51e51SDavid Majnemer 6238ff24d25SRui Ueyama if (HasPEHeader) { 62410ed9ddcSRui Ueyama const pe32_header *Header; 62548af1c2aSRafael Espindola if ((EC = getObject(Header, Data, base() + CurPtr))) 62682ebd8e3SRui Ueyama return; 62710ed9ddcSRui Ueyama 62810ed9ddcSRui Ueyama const uint8_t *DataDirAddr; 62910ed9ddcSRui Ueyama uint64_t DataDirSize; 63050267222SDavid Majnemer if (Header->Magic == COFF::PE32Header::PE32) { 63110ed9ddcSRui Ueyama PE32Header = Header; 63210ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32_header); 63310ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 63450267222SDavid Majnemer } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) { 63510ed9ddcSRui Ueyama PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); 63610ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); 63710ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; 63810ed9ddcSRui Ueyama } else { 63910ed9ddcSRui Ueyama // It's neither PE32 nor PE32+. 64010ed9ddcSRui Ueyama EC = object_error::parse_failed; 641ed64342bSRui Ueyama return; 642ed64342bSRui Ueyama } 64348af1c2aSRafael Espindola if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) 64410ed9ddcSRui Ueyama return; 64582ebd8e3SRui Ueyama CurPtr += COFFHeader->SizeOfOptionalHeader; 64682ebd8e3SRui Ueyama } 6471d6167fdSMichael J. Spencer 64848af1c2aSRafael Espindola if ((EC = getObject(SectionTable, Data, base() + CurPtr, 649236b0ca7SDavid Majnemer (uint64_t)getNumberOfSections() * sizeof(coff_section)))) 6501d6167fdSMichael J. Spencer return; 6511d6167fdSMichael J. Spencer 652c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 653236b0ca7SDavid Majnemer if (getPointerToSymbolTable() != 0) { 6548ff24d25SRui Ueyama if ((EC = initSymbolTablePtr())) 6551d6167fdSMichael J. Spencer return; 656236b0ca7SDavid Majnemer } else { 657236b0ca7SDavid Majnemer // We had better not have any symbols if we don't have a symbol table. 658236b0ca7SDavid Majnemer if (getNumberOfSymbols() != 0) { 659236b0ca7SDavid Majnemer EC = object_error::parse_failed; 660236b0ca7SDavid Majnemer return; 661236b0ca7SDavid Majnemer } 662236b0ca7SDavid Majnemer } 6638e90adafSMichael J. Spencer 664c2bed429SRui Ueyama // Initialize the pointer to the beginning of the import table. 6658ff24d25SRui Ueyama if ((EC = initImportTablePtr())) 666ed64342bSRui Ueyama return; 66715d99359SRui Ueyama if ((EC = initDelayImportTablePtr())) 66815d99359SRui Ueyama return; 6691d6167fdSMichael J. Spencer 670ad882ba8SRui Ueyama // Initialize the pointer to the export table. 6718ff24d25SRui Ueyama if ((EC = initExportTablePtr())) 672ad882ba8SRui Ueyama return; 673ad882ba8SRui Ueyama 67474e85130SRui Ueyama // Initialize the pointer to the base relocation table. 67574e85130SRui Ueyama if ((EC = initBaseRelocPtr())) 67674e85130SRui Ueyama return; 67774e85130SRui Ueyama 6787d099195SRui Ueyama EC = std::error_code(); 6798e90adafSMichael J. Spencer } 6808e90adafSMichael J. Spencer 681f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const { 6828ff24d25SRui Ueyama DataRefImpl Ret; 68344f51e51SDavid Majnemer Ret.p = getSymbolTable(); 684f12b8282SRafael Espindola return basic_symbol_iterator(SymbolRef(Ret, this)); 6858e90adafSMichael J. Spencer } 6868e90adafSMichael J. Spencer 687f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_end_impl() const { 6888e90adafSMichael J. Spencer // The symbol table ends where the string table begins. 6898ff24d25SRui Ueyama DataRefImpl Ret; 6908ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(StringTable); 691f12b8282SRafael Espindola return basic_symbol_iterator(SymbolRef(Ret, this)); 6928e90adafSMichael J. Spencer } 6938e90adafSMichael J. Spencer 694bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_begin() const { 695a045b73aSRui Ueyama return import_directory_iterator( 696a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, 0, this)); 697c2bed429SRui Ueyama } 698c2bed429SRui Ueyama 699bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_end() const { 700a045b73aSRui Ueyama return import_directory_iterator( 701a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); 702c2bed429SRui Ueyama } 703c429b80dSDavid Meyer 70415d99359SRui Ueyama delay_import_directory_iterator 70515d99359SRui Ueyama COFFObjectFile::delay_import_directory_begin() const { 70615d99359SRui Ueyama return delay_import_directory_iterator( 70715d99359SRui Ueyama DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this)); 70815d99359SRui Ueyama } 70915d99359SRui Ueyama 71015d99359SRui Ueyama delay_import_directory_iterator 71115d99359SRui Ueyama COFFObjectFile::delay_import_directory_end() const { 71215d99359SRui Ueyama return delay_import_directory_iterator( 71315d99359SRui Ueyama DelayImportDirectoryEntryRef( 71415d99359SRui Ueyama DelayImportDirectory, NumberOfDelayImportDirectory, this)); 71515d99359SRui Ueyama } 71615d99359SRui Ueyama 717ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_begin() const { 718ad882ba8SRui Ueyama return export_directory_iterator( 719ad882ba8SRui Ueyama ExportDirectoryEntryRef(ExportDirectory, 0, this)); 720ad882ba8SRui Ueyama } 721ad882ba8SRui Ueyama 722ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_end() const { 7232617dcceSCraig Topper if (!ExportDirectory) 7242617dcceSCraig Topper return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this)); 7258ff24d25SRui Ueyama ExportDirectoryEntryRef Ref(ExportDirectory, 726ad882ba8SRui Ueyama ExportDirectory->AddressTableEntries, this); 7278ff24d25SRui Ueyama return export_directory_iterator(Ref); 728ad882ba8SRui Ueyama } 729ad882ba8SRui Ueyama 730b5155a57SRafael Espindola section_iterator COFFObjectFile::section_begin() const { 7318ff24d25SRui Ueyama DataRefImpl Ret; 7328ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 7338ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 7348e90adafSMichael J. Spencer } 7358e90adafSMichael J. Spencer 736b5155a57SRafael Espindola section_iterator COFFObjectFile::section_end() const { 7378ff24d25SRui Ueyama DataRefImpl Ret; 73844f51e51SDavid Majnemer int NumSections = 73944f51e51SDavid Majnemer COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections(); 7408ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 7418ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 7428e90adafSMichael J. Spencer } 7438e90adafSMichael J. Spencer 74474e85130SRui Ueyama base_reloc_iterator COFFObjectFile::base_reloc_begin() const { 74574e85130SRui Ueyama return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this)); 74674e85130SRui Ueyama } 74774e85130SRui Ueyama 74874e85130SRui Ueyama base_reloc_iterator COFFObjectFile::base_reloc_end() const { 74974e85130SRui Ueyama return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this)); 75074e85130SRui Ueyama } 75174e85130SRui Ueyama 7528e90adafSMichael J. Spencer uint8_t COFFObjectFile::getBytesInAddress() const { 7530324b672SMichael J. Spencer return getArch() == Triple::x86_64 ? 8 : 4; 7548e90adafSMichael J. Spencer } 7558e90adafSMichael J. Spencer 7568e90adafSMichael J. Spencer StringRef COFFObjectFile::getFileFormatName() const { 75744f51e51SDavid Majnemer switch(getMachine()) { 7588e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 7598e90adafSMichael J. Spencer return "COFF-i386"; 7608e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 7618e90adafSMichael J. Spencer return "COFF-x86-64"; 7629b7c0af2SSaleem Abdulrasool case COFF::IMAGE_FILE_MACHINE_ARMNT: 7639b7c0af2SSaleem Abdulrasool return "COFF-ARM"; 7641eff5c9cSMartell Malone case COFF::IMAGE_FILE_MACHINE_ARM64: 7651eff5c9cSMartell Malone return "COFF-ARM64"; 7668e90adafSMichael J. Spencer default: 7678e90adafSMichael J. Spencer return "COFF-<unknown arch>"; 7688e90adafSMichael J. Spencer } 7698e90adafSMichael J. Spencer } 7708e90adafSMichael J. Spencer 7718e90adafSMichael J. Spencer unsigned COFFObjectFile::getArch() const { 77244f51e51SDavid Majnemer switch (getMachine()) { 7738e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 7748e90adafSMichael J. Spencer return Triple::x86; 7758e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 7768e90adafSMichael J. Spencer return Triple::x86_64; 7779b7c0af2SSaleem Abdulrasool case COFF::IMAGE_FILE_MACHINE_ARMNT: 7789b7c0af2SSaleem Abdulrasool return Triple::thumb; 7791eff5c9cSMartell Malone case COFF::IMAGE_FILE_MACHINE_ARM64: 7801eff5c9cSMartell Malone return Triple::aarch64; 7818e90adafSMichael J. Spencer default: 7828e90adafSMichael J. Spencer return Triple::UnknownArch; 7838e90adafSMichael J. Spencer } 7848e90adafSMichael J. Spencer } 7858e90adafSMichael J. Spencer 786979fb40bSRui Ueyama iterator_range<import_directory_iterator> 787979fb40bSRui Ueyama COFFObjectFile::import_directories() const { 788979fb40bSRui Ueyama return make_range(import_directory_begin(), import_directory_end()); 789979fb40bSRui Ueyama } 790979fb40bSRui Ueyama 791979fb40bSRui Ueyama iterator_range<delay_import_directory_iterator> 792979fb40bSRui Ueyama COFFObjectFile::delay_import_directories() const { 793979fb40bSRui Ueyama return make_range(delay_import_directory_begin(), 794979fb40bSRui Ueyama delay_import_directory_end()); 795979fb40bSRui Ueyama } 796979fb40bSRui Ueyama 797979fb40bSRui Ueyama iterator_range<export_directory_iterator> 798979fb40bSRui Ueyama COFFObjectFile::export_directories() const { 799979fb40bSRui Ueyama return make_range(export_directory_begin(), export_directory_end()); 800979fb40bSRui Ueyama } 801979fb40bSRui Ueyama 80274e85130SRui Ueyama iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const { 80374e85130SRui Ueyama return make_range(base_reloc_begin(), base_reloc_end()); 80474e85130SRui Ueyama } 80574e85130SRui Ueyama 806db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 80782ebd8e3SRui Ueyama Res = PE32Header; 8087d099195SRui Ueyama return std::error_code(); 80989a7a5eaSMichael J. Spencer } 81089a7a5eaSMichael J. Spencer 811db4ed0bdSRafael Espindola std::error_code 81210ed9ddcSRui Ueyama COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { 81310ed9ddcSRui Ueyama Res = PE32PlusHeader; 8147d099195SRui Ueyama return std::error_code(); 81510ed9ddcSRui Ueyama } 81610ed9ddcSRui Ueyama 817db4ed0bdSRafael Espindola std::error_code 818db4ed0bdSRafael Espindola COFFObjectFile::getDataDirectory(uint32_t Index, 819ed64342bSRui Ueyama const data_directory *&Res) const { 820ed64342bSRui Ueyama // Error if if there's no data directory or the index is out of range. 821f69b0585SDavid Majnemer if (!DataDirectory) { 822f69b0585SDavid Majnemer Res = nullptr; 82310ed9ddcSRui Ueyama return object_error::parse_failed; 824f69b0585SDavid Majnemer } 82510ed9ddcSRui Ueyama assert(PE32Header || PE32PlusHeader); 82610ed9ddcSRui Ueyama uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize 82710ed9ddcSRui Ueyama : PE32PlusHeader->NumberOfRvaAndSize; 828f69b0585SDavid Majnemer if (Index >= NumEnt) { 829f69b0585SDavid Majnemer Res = nullptr; 830ed64342bSRui Ueyama return object_error::parse_failed; 831f69b0585SDavid Majnemer } 8328ff24d25SRui Ueyama Res = &DataDirectory[Index]; 8337d099195SRui Ueyama return std::error_code(); 834ed64342bSRui Ueyama } 835ed64342bSRui Ueyama 836db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSection(int32_t Index, 8371d6167fdSMichael J. Spencer const coff_section *&Result) const { 8382617dcceSCraig Topper Result = nullptr; 839236b0ca7SDavid Majnemer if (COFF::isReservedSectionNumber(Index)) 8407d099195SRui Ueyama return std::error_code(); 841236b0ca7SDavid Majnemer if (static_cast<uint32_t>(Index) <= getNumberOfSections()) { 8421d6167fdSMichael J. Spencer // We already verified the section table data, so no need to check again. 8438ff24d25SRui Ueyama Result = SectionTable + (Index - 1); 8447d099195SRui Ueyama return std::error_code(); 8458e90adafSMichael J. Spencer } 846236b0ca7SDavid Majnemer return object_error::parse_failed; 847236b0ca7SDavid Majnemer } 8488e90adafSMichael J. Spencer 849db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getString(uint32_t Offset, 8501d6167fdSMichael J. Spencer StringRef &Result) const { 8511d6167fdSMichael J. Spencer if (StringTableSize <= 4) 8521d6167fdSMichael J. Spencer // Tried to get a string from an empty string table. 8531d6167fdSMichael J. Spencer return object_error::parse_failed; 8548ff24d25SRui Ueyama if (Offset >= StringTableSize) 8551d6167fdSMichael J. Spencer return object_error::unexpected_eof; 8568ff24d25SRui Ueyama Result = StringRef(StringTable + Offset); 8577d099195SRui Ueyama return std::error_code(); 8588e90adafSMichael J. Spencer } 859022ecdf2SBenjamin Kramer 86044f51e51SDavid Majnemer std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol, 86189a7a5eaSMichael J. Spencer StringRef &Res) const { 862e40d30f3SRui Ueyama return getSymbolName(Symbol.getGeneric(), Res); 863e40d30f3SRui Ueyama } 864e40d30f3SRui Ueyama 865e40d30f3SRui Ueyama std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol, 866e40d30f3SRui Ueyama StringRef &Res) const { 86789a7a5eaSMichael J. Spencer // Check for string table entry. First 4 bytes are 0. 868e40d30f3SRui Ueyama if (Symbol->Name.Offset.Zeroes == 0) { 869e40d30f3SRui Ueyama if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res)) 8708ff24d25SRui Ueyama return EC; 8717d099195SRui Ueyama return std::error_code(); 87289a7a5eaSMichael J. Spencer } 87389a7a5eaSMichael J. Spencer 874e40d30f3SRui Ueyama if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0) 87589a7a5eaSMichael J. Spencer // Null terminated, let ::strlen figure out the length. 876e40d30f3SRui Ueyama Res = StringRef(Symbol->Name.ShortName); 87789a7a5eaSMichael J. Spencer else 87889a7a5eaSMichael J. Spencer // Not null terminated, use all 8 bytes. 879e40d30f3SRui Ueyama Res = StringRef(Symbol->Name.ShortName, COFF::NameSize); 8807d099195SRui Ueyama return std::error_code(); 88189a7a5eaSMichael J. Spencer } 88289a7a5eaSMichael J. Spencer 88344f51e51SDavid Majnemer ArrayRef<uint8_t> 88444f51e51SDavid Majnemer COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const { 8852617dcceSCraig Topper const uint8_t *Aux = nullptr; 88671757ef3SMarshall Clow 88744f51e51SDavid Majnemer size_t SymbolSize = getSymbolTableEntrySize(); 88844f51e51SDavid Majnemer if (Symbol.getNumberOfAuxSymbols() > 0) { 88971757ef3SMarshall Clow // AUX data comes immediately after the symbol in COFF 89044f51e51SDavid Majnemer Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize; 89171757ef3SMarshall Clow # ifndef NDEBUG 8928ff24d25SRui Ueyama // Verify that the Aux symbol points to a valid entry in the symbol table. 8938ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 89444f51e51SDavid Majnemer if (Offset < getPointerToSymbolTable() || 89544f51e51SDavid Majnemer Offset >= 89644f51e51SDavid Majnemer getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize)) 89771757ef3SMarshall Clow report_fatal_error("Aux Symbol data was outside of symbol table."); 89871757ef3SMarshall Clow 89944f51e51SDavid Majnemer assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && 90044f51e51SDavid Majnemer "Aux Symbol data did not point to the beginning of a symbol"); 90171757ef3SMarshall Clow # endif 902bfb85e67SMarshall Clow } 90344f51e51SDavid Majnemer return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize); 90471757ef3SMarshall Clow } 90571757ef3SMarshall Clow 906db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionName(const coff_section *Sec, 90753c2d547SMichael J. Spencer StringRef &Res) const { 90853c2d547SMichael J. Spencer StringRef Name; 90944f51e51SDavid Majnemer if (Sec->Name[COFF::NameSize - 1] == 0) 91053c2d547SMichael J. Spencer // Null terminated, let ::strlen figure out the length. 91153c2d547SMichael J. Spencer Name = Sec->Name; 91253c2d547SMichael J. Spencer else 91353c2d547SMichael J. Spencer // Not null terminated, use all 8 bytes. 91444f51e51SDavid Majnemer Name = StringRef(Sec->Name, COFF::NameSize); 91553c2d547SMichael J. Spencer 91653c2d547SMichael J. Spencer // Check for string table entry. First byte is '/'. 9172314b3deSDavid Majnemer if (Name.startswith("/")) { 91853c2d547SMichael J. Spencer uint32_t Offset; 9192314b3deSDavid Majnemer if (Name.startswith("//")) { 9209d2c15efSNico Rieck if (decodeBase64StringEntry(Name.substr(2), Offset)) 9219d2c15efSNico Rieck return object_error::parse_failed; 9229d2c15efSNico Rieck } else { 92353c2d547SMichael J. Spencer if (Name.substr(1).getAsInteger(10, Offset)) 92453c2d547SMichael J. Spencer return object_error::parse_failed; 9259d2c15efSNico Rieck } 926db4ed0bdSRafael Espindola if (std::error_code EC = getString(Offset, Name)) 9278ff24d25SRui Ueyama return EC; 92853c2d547SMichael J. Spencer } 92953c2d547SMichael J. Spencer 93053c2d547SMichael J. Spencer Res = Name; 9317d099195SRui Ueyama return std::error_code(); 93253c2d547SMichael J. Spencer } 93353c2d547SMichael J. Spencer 934a9ee5c06SDavid Majnemer uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const { 935a9ee5c06SDavid Majnemer // SizeOfRawData and VirtualSize change what they represent depending on 936a9ee5c06SDavid Majnemer // whether or not we have an executable image. 937a9ee5c06SDavid Majnemer // 938a9ee5c06SDavid Majnemer // For object files, SizeOfRawData contains the size of section's data; 939d5297ee7SRui Ueyama // VirtualSize should be zero but isn't due to buggy COFF writers. 940a9ee5c06SDavid Majnemer // 941a9ee5c06SDavid Majnemer // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the 942a9ee5c06SDavid Majnemer // actual section size is in VirtualSize. It is possible for VirtualSize to 943a9ee5c06SDavid Majnemer // be greater than SizeOfRawData; the contents past that point should be 944a9ee5c06SDavid Majnemer // considered to be zero. 945d5297ee7SRui Ueyama if (getDOSHeader()) 946d5297ee7SRui Ueyama return std::min(Sec->VirtualSize, Sec->SizeOfRawData); 947d5297ee7SRui Ueyama return Sec->SizeOfRawData; 948a9ee5c06SDavid Majnemer } 949a9ee5c06SDavid Majnemer 950db4ed0bdSRafael Espindola std::error_code 951db4ed0bdSRafael Espindola COFFObjectFile::getSectionContents(const coff_section *Sec, 9529da9e693SMichael J. Spencer ArrayRef<uint8_t> &Res) const { 953dd9cff2eSDavid Majnemer // PointerToRawData and SizeOfRawData won't make sense for BSS sections, 954dd9cff2eSDavid Majnemer // don't do anything interesting for them. 955dac39857SDavid Majnemer assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 && 956dac39857SDavid Majnemer "BSS sections don't have contents!"); 9579da9e693SMichael J. Spencer // The only thing that we need to verify is that the contents is contained 9589da9e693SMichael J. Spencer // within the file bounds. We don't need to make sure it doesn't cover other 9599da9e693SMichael J. Spencer // data, as there's nothing that says that is not allowed. 9609da9e693SMichael J. Spencer uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 961a9ee5c06SDavid Majnemer uint32_t SectionSize = getSectionSize(Sec); 962e830c60dSDavid Majnemer if (checkOffset(Data, ConStart, SectionSize)) 9639da9e693SMichael J. Spencer return object_error::parse_failed; 964a9ee5c06SDavid Majnemer Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize); 9657d099195SRui Ueyama return std::error_code(); 9669da9e693SMichael J. Spencer } 9679da9e693SMichael J. Spencer 968022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 969e5fd0047SMichael J. Spencer return reinterpret_cast<const coff_relocation*>(Rel.p); 970022ecdf2SBenjamin Kramer } 9718ff24d25SRui Ueyama 9725e812afaSRafael Espindola void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 973e5fd0047SMichael J. Spencer Rel.p = reinterpret_cast<uintptr_t>( 974e5fd0047SMichael J. Spencer reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 975022ecdf2SBenjamin Kramer } 9768ff24d25SRui Ueyama 97796d071cdSRafael Espindola uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const { 97858323a97SDavid Majnemer const coff_relocation *R = toRel(Rel); 97996d071cdSRafael Espindola return R->VirtualAddress; 980cbe72fc9SDanil Malyshev } 9818ff24d25SRui Ueyama 982806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 983022ecdf2SBenjamin Kramer const coff_relocation *R = toRel(Rel); 9848ff24d25SRui Ueyama DataRefImpl Ref; 985236b0ca7SDavid Majnemer if (R->SymbolTableIndex >= getNumberOfSymbols()) 986236b0ca7SDavid Majnemer return symbol_end(); 98744f51e51SDavid Majnemer if (SymbolTable16) 98844f51e51SDavid Majnemer Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex); 98944f51e51SDavid Majnemer else if (SymbolTable32) 99044f51e51SDavid Majnemer Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex); 99144f51e51SDavid Majnemer else 992c7353b58SDavid Majnemer llvm_unreachable("no symbol table pointer!"); 9938ff24d25SRui Ueyama return symbol_iterator(SymbolRef(Ref, this)); 994022ecdf2SBenjamin Kramer } 9958ff24d25SRui Ueyama 99699c041b7SRafael Espindola uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const { 997022ecdf2SBenjamin Kramer const coff_relocation* R = toRel(Rel); 99899c041b7SRafael Espindola return R->Type; 999022ecdf2SBenjamin Kramer } 1000e5fd0047SMichael J. Spencer 100127dc8394SAlexey Samsonov const coff_section * 100227dc8394SAlexey Samsonov COFFObjectFile::getCOFFSection(const SectionRef &Section) const { 100327dc8394SAlexey Samsonov return toSec(Section.getRawDataRefImpl()); 100471757ef3SMarshall Clow } 100571757ef3SMarshall Clow 100644f51e51SDavid Majnemer COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const { 100744f51e51SDavid Majnemer if (SymbolTable16) 100844f51e51SDavid Majnemer return toSymb<coff_symbol16>(Ref); 100944f51e51SDavid Majnemer if (SymbolTable32) 101044f51e51SDavid Majnemer return toSymb<coff_symbol32>(Ref); 101144f51e51SDavid Majnemer llvm_unreachable("no symbol table pointer!"); 101244f51e51SDavid Majnemer } 101344f51e51SDavid Majnemer 101444f51e51SDavid Majnemer COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { 101544f51e51SDavid Majnemer return getCOFFSymbol(Symbol.getRawDataRefImpl()); 101671757ef3SMarshall Clow } 101771757ef3SMarshall Clow 1018f12b8282SRafael Espindola const coff_relocation * 101927dc8394SAlexey Samsonov COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { 102027dc8394SAlexey Samsonov return toRel(Reloc.getRawDataRefImpl()); 1021d3e2a76cSMarshall Clow } 1022d3e2a76cSMarshall Clow 10236a75acb1SRui Ueyama iterator_range<const coff_relocation *> 10246a75acb1SRui Ueyama COFFObjectFile::getRelocations(const coff_section *Sec) const { 10256a75acb1SRui Ueyama const coff_relocation *I = getFirstReloc(Sec, Data, base()); 10266a75acb1SRui Ueyama const coff_relocation *E = I; 10276a75acb1SRui Ueyama if (I) 10286a75acb1SRui Ueyama E += getNumberOfRelocations(Sec, Data, base()); 10296a75acb1SRui Ueyama return make_range(I, E); 10306a75acb1SRui Ueyama } 10316a75acb1SRui Ueyama 103227dc8394SAlexey Samsonov #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ 103327dc8394SAlexey Samsonov case COFF::reloc_type: \ 103427dc8394SAlexey Samsonov Res = #reloc_type; \ 103527dc8394SAlexey Samsonov break; 1036e5fd0047SMichael J. Spencer 103741bb4325SRafael Espindola void COFFObjectFile::getRelocationTypeName( 103841bb4325SRafael Espindola DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 10398ff24d25SRui Ueyama const coff_relocation *Reloc = toRel(Rel); 10408ff24d25SRui Ueyama StringRef Res; 104144f51e51SDavid Majnemer switch (getMachine()) { 1042e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 10438ff24d25SRui Ueyama switch (Reloc->Type) { 1044e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 1045e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 1046e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 1047e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 1048e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 1049e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 1050e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 1051e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 1052e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 1053e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 1054e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 1055e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 1056e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 1057e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 1058e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 1059e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 1060e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 1061e5fd0047SMichael J. Spencer default: 10628ff24d25SRui Ueyama Res = "Unknown"; 1063e5fd0047SMichael J. Spencer } 1064e5fd0047SMichael J. Spencer break; 10655c503bf4SSaleem Abdulrasool case COFF::IMAGE_FILE_MACHINE_ARMNT: 10665c503bf4SSaleem Abdulrasool switch (Reloc->Type) { 10675c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE); 10685c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32); 10695c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB); 10705c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24); 10715c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11); 10725c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN); 10735c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24); 10745c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11); 10755c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION); 10765c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL); 10775c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A); 10785c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T); 10795c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T); 10805c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T); 10815c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T); 10825c503bf4SSaleem Abdulrasool default: 10835c503bf4SSaleem Abdulrasool Res = "Unknown"; 10845c503bf4SSaleem Abdulrasool } 10855c503bf4SSaleem Abdulrasool break; 1086e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 10878ff24d25SRui Ueyama switch (Reloc->Type) { 1088e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 1089e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 1090e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 1091e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 1092e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 1093e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 1094e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 1095e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 1096e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 1097e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 1098e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 1099e5fd0047SMichael J. Spencer default: 11008ff24d25SRui Ueyama Res = "Unknown"; 1101e5fd0047SMichael J. Spencer } 1102e5fd0047SMichael J. Spencer break; 1103e5fd0047SMichael J. Spencer default: 11048ff24d25SRui Ueyama Res = "Unknown"; 1105e5fd0047SMichael J. Spencer } 11068ff24d25SRui Ueyama Result.append(Res.begin(), Res.end()); 1107e5fd0047SMichael J. Spencer } 1108e5fd0047SMichael J. Spencer 1109e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 1110e5fd0047SMichael J. Spencer 1111c66d761bSRafael Espindola bool COFFObjectFile::isRelocatableObject() const { 1112c66d761bSRafael Espindola return !DataDirectory; 1113c66d761bSRafael Espindola } 1114c66d761bSRafael Espindola 1115c2bed429SRui Ueyama bool ImportDirectoryEntryRef:: 1116c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const { 1117a045b73aSRui Ueyama return ImportTable == Other.ImportTable && Index == Other.Index; 1118c2bed429SRui Ueyama } 1119c2bed429SRui Ueyama 11205e812afaSRafael Espindola void ImportDirectoryEntryRef::moveNext() { 11215e812afaSRafael Espindola ++Index; 1122c2bed429SRui Ueyama } 1123c2bed429SRui Ueyama 1124db4ed0bdSRafael Espindola std::error_code ImportDirectoryEntryRef::getImportTableEntry( 1125db4ed0bdSRafael Espindola const import_directory_table_entry *&Result) const { 11261e152d5eSRui Ueyama Result = ImportTable + Index; 11277d099195SRui Ueyama return std::error_code(); 1128c2bed429SRui Ueyama } 1129c2bed429SRui Ueyama 1130861021f9SRui Ueyama static imported_symbol_iterator 113115d99359SRui Ueyama makeImportedSymbolIterator(const COFFObjectFile *Object, 1132861021f9SRui Ueyama uintptr_t Ptr, int Index) { 113315d99359SRui Ueyama if (Object->getBytesInAddress() == 4) { 1134861021f9SRui Ueyama auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr); 113515d99359SRui Ueyama return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1136861021f9SRui Ueyama } 1137861021f9SRui Ueyama auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr); 113815d99359SRui Ueyama return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object)); 1139861021f9SRui Ueyama } 1140861021f9SRui Ueyama 114115d99359SRui Ueyama static imported_symbol_iterator 114215d99359SRui Ueyama importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) { 1143861021f9SRui Ueyama uintptr_t IntPtr = 0; 114415d99359SRui Ueyama Object->getRvaPtr(RVA, IntPtr); 114515d99359SRui Ueyama return makeImportedSymbolIterator(Object, IntPtr, 0); 1146861021f9SRui Ueyama } 1147861021f9SRui Ueyama 114815d99359SRui Ueyama static imported_symbol_iterator 114915d99359SRui Ueyama importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) { 1150861021f9SRui Ueyama uintptr_t IntPtr = 0; 115115d99359SRui Ueyama Object->getRvaPtr(RVA, IntPtr); 1152861021f9SRui Ueyama // Forward the pointer to the last entry which is null. 1153861021f9SRui Ueyama int Index = 0; 115415d99359SRui Ueyama if (Object->getBytesInAddress() == 4) { 1155861021f9SRui Ueyama auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr); 1156861021f9SRui Ueyama while (*Entry++) 1157861021f9SRui Ueyama ++Index; 1158861021f9SRui Ueyama } else { 1159861021f9SRui Ueyama auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr); 1160861021f9SRui Ueyama while (*Entry++) 1161861021f9SRui Ueyama ++Index; 1162861021f9SRui Ueyama } 116315d99359SRui Ueyama return makeImportedSymbolIterator(Object, IntPtr, Index); 116415d99359SRui Ueyama } 116515d99359SRui Ueyama 116615d99359SRui Ueyama imported_symbol_iterator 116715d99359SRui Ueyama ImportDirectoryEntryRef::imported_symbol_begin() const { 116815d99359SRui Ueyama return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA, 116915d99359SRui Ueyama OwningObject); 117015d99359SRui Ueyama } 117115d99359SRui Ueyama 117215d99359SRui Ueyama imported_symbol_iterator 117315d99359SRui Ueyama ImportDirectoryEntryRef::imported_symbol_end() const { 117415d99359SRui Ueyama return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA, 117515d99359SRui Ueyama OwningObject); 1176861021f9SRui Ueyama } 1177861021f9SRui Ueyama 1178979fb40bSRui Ueyama iterator_range<imported_symbol_iterator> 1179979fb40bSRui Ueyama ImportDirectoryEntryRef::imported_symbols() const { 1180979fb40bSRui Ueyama return make_range(imported_symbol_begin(), imported_symbol_end()); 1181979fb40bSRui Ueyama } 1182979fb40bSRui Ueyama 1183db4ed0bdSRafael Espindola std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 1184c2bed429SRui Ueyama uintptr_t IntPtr = 0; 1185db4ed0bdSRafael Espindola if (std::error_code EC = 11861e152d5eSRui Ueyama OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr)) 1187a045b73aSRui Ueyama return EC; 1188a045b73aSRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 11897d099195SRui Ueyama return std::error_code(); 1190c2bed429SRui Ueyama } 1191c2bed429SRui Ueyama 11921e152d5eSRui Ueyama std::error_code 11931e152d5eSRui Ueyama ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const { 11941e152d5eSRui Ueyama Result = ImportTable[Index].ImportLookupTableRVA; 11957d099195SRui Ueyama return std::error_code(); 11961e152d5eSRui Ueyama } 11971e152d5eSRui Ueyama 11981e152d5eSRui Ueyama std::error_code 11991e152d5eSRui Ueyama ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const { 12001e152d5eSRui Ueyama Result = ImportTable[Index].ImportAddressTableRVA; 12017d099195SRui Ueyama return std::error_code(); 12021e152d5eSRui Ueyama } 12031e152d5eSRui Ueyama 1204db4ed0bdSRafael Espindola std::error_code ImportDirectoryEntryRef::getImportLookupEntry( 1205c2bed429SRui Ueyama const import_lookup_table_entry32 *&Result) const { 1206c2bed429SRui Ueyama uintptr_t IntPtr = 0; 12071e152d5eSRui Ueyama uint32_t RVA = ImportTable[Index].ImportLookupTableRVA; 12081e152d5eSRui Ueyama if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1209a045b73aSRui Ueyama return EC; 1210c2bed429SRui Ueyama Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); 12117d099195SRui Ueyama return std::error_code(); 1212c2bed429SRui Ueyama } 1213c2bed429SRui Ueyama 121415d99359SRui Ueyama bool DelayImportDirectoryEntryRef:: 121515d99359SRui Ueyama operator==(const DelayImportDirectoryEntryRef &Other) const { 121615d99359SRui Ueyama return Table == Other.Table && Index == Other.Index; 121715d99359SRui Ueyama } 121815d99359SRui Ueyama 121915d99359SRui Ueyama void DelayImportDirectoryEntryRef::moveNext() { 122015d99359SRui Ueyama ++Index; 122115d99359SRui Ueyama } 122215d99359SRui Ueyama 122315d99359SRui Ueyama imported_symbol_iterator 122415d99359SRui Ueyama DelayImportDirectoryEntryRef::imported_symbol_begin() const { 122515d99359SRui Ueyama return importedSymbolBegin(Table[Index].DelayImportNameTable, 122615d99359SRui Ueyama OwningObject); 122715d99359SRui Ueyama } 122815d99359SRui Ueyama 122915d99359SRui Ueyama imported_symbol_iterator 123015d99359SRui Ueyama DelayImportDirectoryEntryRef::imported_symbol_end() const { 123115d99359SRui Ueyama return importedSymbolEnd(Table[Index].DelayImportNameTable, 123215d99359SRui Ueyama OwningObject); 123315d99359SRui Ueyama } 123415d99359SRui Ueyama 1235979fb40bSRui Ueyama iterator_range<imported_symbol_iterator> 1236979fb40bSRui Ueyama DelayImportDirectoryEntryRef::imported_symbols() const { 1237979fb40bSRui Ueyama return make_range(imported_symbol_begin(), imported_symbol_end()); 1238979fb40bSRui Ueyama } 1239979fb40bSRui Ueyama 124015d99359SRui Ueyama std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const { 124115d99359SRui Ueyama uintptr_t IntPtr = 0; 124215d99359SRui Ueyama if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr)) 124315d99359SRui Ueyama return EC; 124415d99359SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 12457d099195SRui Ueyama return std::error_code(); 124615d99359SRui Ueyama } 124715d99359SRui Ueyama 12481af08658SRui Ueyama std::error_code DelayImportDirectoryEntryRef:: 12491af08658SRui Ueyama getDelayImportTable(const delay_import_directory_table_entry *&Result) const { 12501af08658SRui Ueyama Result = Table; 12517d099195SRui Ueyama return std::error_code(); 12521af08658SRui Ueyama } 12531af08658SRui Ueyama 1254ffa4cebeSRui Ueyama std::error_code DelayImportDirectoryEntryRef:: 1255ffa4cebeSRui Ueyama getImportAddress(int AddrIndex, uint64_t &Result) const { 1256ffa4cebeSRui Ueyama uint32_t RVA = Table[Index].DelayImportAddressTable + 1257ffa4cebeSRui Ueyama AddrIndex * (OwningObject->is64() ? 8 : 4); 1258ffa4cebeSRui Ueyama uintptr_t IntPtr = 0; 1259ffa4cebeSRui Ueyama if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1260ffa4cebeSRui Ueyama return EC; 1261ffa4cebeSRui Ueyama if (OwningObject->is64()) 12625dcf11d1SRui Ueyama Result = *reinterpret_cast<const ulittle64_t *>(IntPtr); 1263ffa4cebeSRui Ueyama else 12645dcf11d1SRui Ueyama Result = *reinterpret_cast<const ulittle32_t *>(IntPtr); 12657d099195SRui Ueyama return std::error_code(); 1266ffa4cebeSRui Ueyama } 1267ffa4cebeSRui Ueyama 1268ad882ba8SRui Ueyama bool ExportDirectoryEntryRef:: 1269ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const { 1270ad882ba8SRui Ueyama return ExportTable == Other.ExportTable && Index == Other.Index; 1271ad882ba8SRui Ueyama } 1272ad882ba8SRui Ueyama 12735e812afaSRafael Espindola void ExportDirectoryEntryRef::moveNext() { 12745e812afaSRafael Espindola ++Index; 1275ad882ba8SRui Ueyama } 1276ad882ba8SRui Ueyama 1277da49d0d4SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 1278da49d0d4SRui Ueyama // by ordinal, the empty string is set as a result. 1279db4ed0bdSRafael Espindola std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { 1280da49d0d4SRui Ueyama uintptr_t IntPtr = 0; 1281db4ed0bdSRafael Espindola if (std::error_code EC = 1282db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) 1283da49d0d4SRui Ueyama return EC; 1284da49d0d4SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 12857d099195SRui Ueyama return std::error_code(); 1286da49d0d4SRui Ueyama } 1287da49d0d4SRui Ueyama 1288e5df6095SRui Ueyama // Returns the starting ordinal number. 1289db4ed0bdSRafael Espindola std::error_code 1290db4ed0bdSRafael Espindola ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { 1291e5df6095SRui Ueyama Result = ExportTable->OrdinalBase; 12927d099195SRui Ueyama return std::error_code(); 1293e5df6095SRui Ueyama } 1294e5df6095SRui Ueyama 1295ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol. 1296db4ed0bdSRafael Espindola std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 1297ad882ba8SRui Ueyama Result = ExportTable->OrdinalBase + Index; 12987d099195SRui Ueyama return std::error_code(); 1299ad882ba8SRui Ueyama } 1300ad882ba8SRui Ueyama 1301ad882ba8SRui Ueyama // Returns the address of the current export symbol. 1302db4ed0bdSRafael Espindola std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 1303ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 1304db4ed0bdSRafael Espindola if (std::error_code EC = 1305db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) 1306ad882ba8SRui Ueyama return EC; 130724fc2d64SRui Ueyama const export_address_table_entry *entry = 130824fc2d64SRui Ueyama reinterpret_cast<const export_address_table_entry *>(IntPtr); 1309ad882ba8SRui Ueyama Result = entry[Index].ExportRVA; 13107d099195SRui Ueyama return std::error_code(); 1311ad882ba8SRui Ueyama } 1312ad882ba8SRui Ueyama 1313ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 1314ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result. 1315db4ed0bdSRafael Espindola std::error_code 1316db4ed0bdSRafael Espindola ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { 1317ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 1318db4ed0bdSRafael Espindola if (std::error_code EC = 1319db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) 1320ad882ba8SRui Ueyama return EC; 1321ad882ba8SRui Ueyama const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 1322ad882ba8SRui Ueyama 1323ad882ba8SRui Ueyama uint32_t NumEntries = ExportTable->NumberOfNamePointers; 1324ad882ba8SRui Ueyama int Offset = 0; 1325ad882ba8SRui Ueyama for (const ulittle16_t *I = Start, *E = Start + NumEntries; 1326ad882ba8SRui Ueyama I < E; ++I, ++Offset) { 1327ad882ba8SRui Ueyama if (*I != Index) 1328ad882ba8SRui Ueyama continue; 1329db4ed0bdSRafael Espindola if (std::error_code EC = 1330db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) 1331ad882ba8SRui Ueyama return EC; 1332ad882ba8SRui Ueyama const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 1333db4ed0bdSRafael Espindola if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 1334ad882ba8SRui Ueyama return EC; 1335ad882ba8SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 13367d099195SRui Ueyama return std::error_code(); 1337ad882ba8SRui Ueyama } 1338ad882ba8SRui Ueyama Result = ""; 13397d099195SRui Ueyama return std::error_code(); 1340ad882ba8SRui Ueyama } 1341ad882ba8SRui Ueyama 13426161b38dSRui Ueyama std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const { 13436161b38dSRui Ueyama const data_directory *DataEntry; 13446161b38dSRui Ueyama if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 13456161b38dSRui Ueyama return EC; 13466161b38dSRui Ueyama uint32_t RVA; 13476161b38dSRui Ueyama if (auto EC = getExportRVA(RVA)) 13486161b38dSRui Ueyama return EC; 13496161b38dSRui Ueyama uint32_t Begin = DataEntry->RelativeVirtualAddress; 13506161b38dSRui Ueyama uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size; 13516161b38dSRui Ueyama Result = (Begin <= RVA && RVA < End); 13526161b38dSRui Ueyama return std::error_code(); 13536161b38dSRui Ueyama } 13546161b38dSRui Ueyama 13556161b38dSRui Ueyama std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const { 13566161b38dSRui Ueyama uint32_t RVA; 13576161b38dSRui Ueyama if (auto EC = getExportRVA(RVA)) 13586161b38dSRui Ueyama return EC; 13596161b38dSRui Ueyama uintptr_t IntPtr = 0; 13606161b38dSRui Ueyama if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr)) 13616161b38dSRui Ueyama return EC; 13626161b38dSRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 13636161b38dSRui Ueyama return std::error_code(); 13646161b38dSRui Ueyama } 13656161b38dSRui Ueyama 1366861021f9SRui Ueyama bool ImportedSymbolRef:: 1367861021f9SRui Ueyama operator==(const ImportedSymbolRef &Other) const { 1368861021f9SRui Ueyama return Entry32 == Other.Entry32 && Entry64 == Other.Entry64 1369861021f9SRui Ueyama && Index == Other.Index; 1370861021f9SRui Ueyama } 1371861021f9SRui Ueyama 1372861021f9SRui Ueyama void ImportedSymbolRef::moveNext() { 1373861021f9SRui Ueyama ++Index; 1374861021f9SRui Ueyama } 1375861021f9SRui Ueyama 1376861021f9SRui Ueyama std::error_code 1377861021f9SRui Ueyama ImportedSymbolRef::getSymbolName(StringRef &Result) const { 1378861021f9SRui Ueyama uint32_t RVA; 1379861021f9SRui Ueyama if (Entry32) { 1380861021f9SRui Ueyama // If a symbol is imported only by ordinal, it has no name. 1381861021f9SRui Ueyama if (Entry32[Index].isOrdinal()) 13827d099195SRui Ueyama return std::error_code(); 1383861021f9SRui Ueyama RVA = Entry32[Index].getHintNameRVA(); 1384861021f9SRui Ueyama } else { 1385861021f9SRui Ueyama if (Entry64[Index].isOrdinal()) 13867d099195SRui Ueyama return std::error_code(); 1387861021f9SRui Ueyama RVA = Entry64[Index].getHintNameRVA(); 1388861021f9SRui Ueyama } 1389861021f9SRui Ueyama uintptr_t IntPtr = 0; 1390861021f9SRui Ueyama if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1391861021f9SRui Ueyama return EC; 1392861021f9SRui Ueyama // +2 because the first two bytes is hint. 1393861021f9SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2)); 13947d099195SRui Ueyama return std::error_code(); 1395861021f9SRui Ueyama } 1396861021f9SRui Ueyama 1397861021f9SRui Ueyama std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const { 1398861021f9SRui Ueyama uint32_t RVA; 1399861021f9SRui Ueyama if (Entry32) { 1400861021f9SRui Ueyama if (Entry32[Index].isOrdinal()) { 1401861021f9SRui Ueyama Result = Entry32[Index].getOrdinal(); 14027d099195SRui Ueyama return std::error_code(); 1403861021f9SRui Ueyama } 1404861021f9SRui Ueyama RVA = Entry32[Index].getHintNameRVA(); 1405861021f9SRui Ueyama } else { 1406861021f9SRui Ueyama if (Entry64[Index].isOrdinal()) { 1407861021f9SRui Ueyama Result = Entry64[Index].getOrdinal(); 14087d099195SRui Ueyama return std::error_code(); 1409861021f9SRui Ueyama } 1410861021f9SRui Ueyama RVA = Entry64[Index].getHintNameRVA(); 1411861021f9SRui Ueyama } 1412861021f9SRui Ueyama uintptr_t IntPtr = 0; 1413861021f9SRui Ueyama if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) 1414861021f9SRui Ueyama return EC; 1415861021f9SRui Ueyama Result = *reinterpret_cast<const ulittle16_t *>(IntPtr); 14167d099195SRui Ueyama return std::error_code(); 1417861021f9SRui Ueyama } 1418861021f9SRui Ueyama 1419437b0d58SRafael Espindola ErrorOr<std::unique_ptr<COFFObjectFile>> 142048af1c2aSRafael Espindola ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) { 1421db4ed0bdSRafael Espindola std::error_code EC; 142248af1c2aSRafael Espindola std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC)); 1423692410efSRafael Espindola if (EC) 1424692410efSRafael Espindola return EC; 1425437b0d58SRafael Espindola return std::move(Ret); 1426686738e2SRui Ueyama } 142774e85130SRui Ueyama 142874e85130SRui Ueyama bool BaseRelocRef::operator==(const BaseRelocRef &Other) const { 142974e85130SRui Ueyama return Header == Other.Header && Index == Other.Index; 143074e85130SRui Ueyama } 143174e85130SRui Ueyama 143274e85130SRui Ueyama void BaseRelocRef::moveNext() { 143374e85130SRui Ueyama // Header->BlockSize is the size of the current block, including the 143474e85130SRui Ueyama // size of the header itself. 143574e85130SRui Ueyama uint32_t Size = sizeof(*Header) + 1436970dda29SRui Ueyama sizeof(coff_base_reloc_block_entry) * (Index + 1); 143774e85130SRui Ueyama if (Size == Header->BlockSize) { 143874e85130SRui Ueyama // .reloc contains a list of base relocation blocks. Each block 143974e85130SRui Ueyama // consists of the header followed by entries. The header contains 144074e85130SRui Ueyama // how many entories will follow. When we reach the end of the 144174e85130SRui Ueyama // current block, proceed to the next block. 144274e85130SRui Ueyama Header = reinterpret_cast<const coff_base_reloc_block_header *>( 144374e85130SRui Ueyama reinterpret_cast<const uint8_t *>(Header) + Size); 144474e85130SRui Ueyama Index = 0; 144574e85130SRui Ueyama } else { 144674e85130SRui Ueyama ++Index; 144774e85130SRui Ueyama } 144874e85130SRui Ueyama } 144974e85130SRui Ueyama 145074e85130SRui Ueyama std::error_code BaseRelocRef::getType(uint8_t &Type) const { 145174e85130SRui Ueyama auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1); 145274e85130SRui Ueyama Type = Entry[Index].getType(); 14537d099195SRui Ueyama return std::error_code(); 145474e85130SRui Ueyama } 145574e85130SRui Ueyama 145674e85130SRui Ueyama std::error_code BaseRelocRef::getRVA(uint32_t &Result) const { 145774e85130SRui Ueyama auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1); 145874e85130SRui Ueyama Result = Header->PageRVA + Entry[Index].getOffset(); 14597d099195SRui Ueyama return std::error_code(); 146074e85130SRui Ueyama } 1461