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" 16e5fd0047SMichael J. Spencer #include "llvm/ADT/SmallString.h" 178e90adafSMichael J. Spencer #include "llvm/ADT/StringSwitch.h" 188e90adafSMichael J. Spencer #include "llvm/ADT/Triple.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::ulittle8_t; 298e90adafSMichael J. Spencer using support::ulittle16_t; 308e90adafSMichael J. Spencer using support::ulittle32_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. 34*db4ed0bdSRafael Espindola static bool checkSize(const MemoryBuffer *M, std::error_code &EC, 35*db4ed0bdSRafael Espindola uint64_t Size) { 368ff24d25SRui Ueyama if (M->getBufferSize() < Size) { 378ff24d25SRui Ueyama EC = object_error::unexpected_eof; 381d6167fdSMichael J. Spencer return false; 391d6167fdSMichael J. Spencer } 401d6167fdSMichael J. Spencer return true; 418e90adafSMichael J. Spencer } 428e90adafSMichael J. Spencer 43ed64342bSRui Ueyama // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. 44ed64342bSRui Ueyama // Returns unexpected_eof if error. 45ed64342bSRui Ueyama template <typename T> 46*db4ed0bdSRafael Espindola static std::error_code getObject(const T *&Obj, const MemoryBuffer *M, 47*db4ed0bdSRafael Espindola const uint8_t *Ptr, 48*db4ed0bdSRafael Espindola const size_t Size = sizeof(T)) { 49ed64342bSRui Ueyama uintptr_t Addr = uintptr_t(Ptr); 50ed64342bSRui Ueyama if (Addr + Size < Addr || 51ed64342bSRui Ueyama Addr + Size < Size || 52ed64342bSRui Ueyama Addr + Size > uintptr_t(M->getBufferEnd())) { 53ed64342bSRui Ueyama return object_error::unexpected_eof; 541d6167fdSMichael J. Spencer } 55ed64342bSRui Ueyama Obj = reinterpret_cast<const T *>(Addr); 56ed64342bSRui Ueyama return object_error::success; 571d6167fdSMichael J. Spencer } 581d6167fdSMichael J. Spencer 599d2c15efSNico Rieck // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without 609d2c15efSNico Rieck // prefixed slashes. 619d2c15efSNico Rieck static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) { 629d2c15efSNico Rieck assert(Str.size() <= 6 && "String too long, possible overflow."); 639d2c15efSNico Rieck if (Str.size() > 6) 649d2c15efSNico Rieck return true; 659d2c15efSNico Rieck 669d2c15efSNico Rieck uint64_t Value = 0; 679d2c15efSNico Rieck while (!Str.empty()) { 689d2c15efSNico Rieck unsigned CharVal; 699d2c15efSNico Rieck if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25 709d2c15efSNico Rieck CharVal = Str[0] - 'A'; 719d2c15efSNico Rieck else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51 729d2c15efSNico Rieck CharVal = Str[0] - 'a' + 26; 739d2c15efSNico Rieck else if (Str[0] >= '0' && Str[0] <= '9') // 52..61 749d2c15efSNico Rieck CharVal = Str[0] - '0' + 52; 759d2c15efSNico Rieck else if (Str[0] == '+') // 62 765500b07cSRui Ueyama CharVal = 62; 779d2c15efSNico Rieck else if (Str[0] == '/') // 63 785500b07cSRui Ueyama CharVal = 63; 799d2c15efSNico Rieck else 809d2c15efSNico Rieck return true; 819d2c15efSNico Rieck 829d2c15efSNico Rieck Value = (Value * 64) + CharVal; 839d2c15efSNico Rieck Str = Str.substr(1); 849d2c15efSNico Rieck } 859d2c15efSNico Rieck 869d2c15efSNico Rieck if (Value > std::numeric_limits<uint32_t>::max()) 879d2c15efSNico Rieck return true; 889d2c15efSNico Rieck 899d2c15efSNico Rieck Result = static_cast<uint32_t>(Value); 909d2c15efSNico Rieck return false; 919d2c15efSNico Rieck } 929d2c15efSNico Rieck 938ff24d25SRui Ueyama const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const { 948ff24d25SRui Ueyama const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p); 951d6167fdSMichael J. Spencer 961d6167fdSMichael J. Spencer # ifndef NDEBUG 971d6167fdSMichael J. Spencer // Verify that the symbol points to a valid entry in the symbol table. 988ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); 998ff24d25SRui Ueyama if (Offset < COFFHeader->PointerToSymbolTable 1008ff24d25SRui Ueyama || Offset >= COFFHeader->PointerToSymbolTable 10182ebd8e3SRui Ueyama + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 1021d6167fdSMichael J. Spencer report_fatal_error("Symbol was outside of symbol table."); 1031d6167fdSMichael J. Spencer 1048ff24d25SRui Ueyama assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 1051d6167fdSMichael J. Spencer == 0 && "Symbol did not point to the beginning of a symbol"); 1061d6167fdSMichael J. Spencer # endif 1071d6167fdSMichael J. Spencer 1088ff24d25SRui Ueyama return Addr; 1091d6167fdSMichael J. Spencer } 1101d6167fdSMichael J. Spencer 1118ff24d25SRui Ueyama const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { 1128ff24d25SRui Ueyama const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); 1131d6167fdSMichael J. Spencer 1141d6167fdSMichael J. Spencer # ifndef NDEBUG 1151d6167fdSMichael J. Spencer // Verify that the section points to a valid entry in the section table. 1168ff24d25SRui Ueyama if (Addr < SectionTable 1178ff24d25SRui Ueyama || Addr >= (SectionTable + COFFHeader->NumberOfSections)) 1181d6167fdSMichael J. Spencer report_fatal_error("Section was outside of section table."); 1191d6167fdSMichael J. Spencer 1208ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); 1218ff24d25SRui Ueyama assert(Offset % sizeof(coff_section) == 0 && 1221d6167fdSMichael J. Spencer "Section did not point to the beginning of a section"); 1231d6167fdSMichael J. Spencer # endif 1241d6167fdSMichael J. Spencer 1258ff24d25SRui Ueyama return Addr; 1261d6167fdSMichael J. Spencer } 1271d6167fdSMichael J. Spencer 1285e812afaSRafael Espindola void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { 1298ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1308ff24d25SRui Ueyama Symb += 1 + Symb->NumberOfAuxSymbols; 1318ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Symb); 1321d6167fdSMichael J. Spencer } 1331d6167fdSMichael J. Spencer 134*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref, 1351d6167fdSMichael J. Spencer StringRef &Result) const { 1368ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1378ff24d25SRui Ueyama return getSymbolName(Symb, Result); 1388e90adafSMichael J. Spencer } 1398e90adafSMichael J. Spencer 140*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref, 14175d1cf33SBenjamin Kramer uint64_t &Result) const { 1428ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1432617dcceSCraig Topper const coff_section *Section = nullptr; 144*db4ed0bdSRafael Espindola if (std::error_code EC = getSection(Symb->SectionNumber, Section)) 1458ff24d25SRui Ueyama return EC; 146e62ab11fSRafael Espindola 1478ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 14875d1cf33SBenjamin Kramer Result = UnknownAddressOrSize; 14975d1cf33SBenjamin Kramer else if (Section) 1508ff24d25SRui Ueyama Result = Section->VirtualAddress + Symb->Value; 15175d1cf33SBenjamin Kramer else 1528ff24d25SRui Ueyama Result = Symb->Value; 15375d1cf33SBenjamin Kramer return object_error::success; 15475d1cf33SBenjamin Kramer } 15575d1cf33SBenjamin Kramer 156*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref, 157d3946676SMichael J. Spencer SymbolRef::Type &Result) const { 1588ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 15975d1cf33SBenjamin Kramer Result = SymbolRef::ST_Other; 1608ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && 1618ff24d25SRui Ueyama Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { 1627e4b976cSDavid Meyer Result = SymbolRef::ST_Unknown; 163ddf28f2bSDavid Majnemer } else if (Symb->isFunctionDefinition()) { 16475d1cf33SBenjamin Kramer Result = SymbolRef::ST_Function; 16575d1cf33SBenjamin Kramer } else { 16606adfac8SRafael Espindola uint32_t Characteristics = 0; 167f078eff3SRui Ueyama if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) { 1682617dcceSCraig Topper const coff_section *Section = nullptr; 169*db4ed0bdSRafael Espindola if (std::error_code EC = getSection(Symb->SectionNumber, Section)) 1708ff24d25SRui Ueyama return EC; 17106adfac8SRafael Espindola Characteristics = Section->Characteristics; 17275d1cf33SBenjamin Kramer } 17306adfac8SRafael Espindola if (Characteristics & COFF::IMAGE_SCN_MEM_READ && 17406adfac8SRafael Espindola ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. 17506adfac8SRafael Espindola Result = SymbolRef::ST_Data; 17675d1cf33SBenjamin Kramer } 17775d1cf33SBenjamin Kramer return object_error::success; 17875d1cf33SBenjamin Kramer } 17975d1cf33SBenjamin Kramer 18020122a43SRafael Espindola uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { 1818ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 18220122a43SRafael Espindola uint32_t Result = SymbolRef::SF_None; 18375d1cf33SBenjamin Kramer 184975e115eSRafael Espindola // TODO: Correctly set SF_FormatSpecific, SF_Common 1857e4b976cSDavid Meyer 18622fe9c1eSRafael Espindola if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { 18722fe9c1eSRafael Espindola if (Symb->Value == 0) 1887e4b976cSDavid Meyer Result |= SymbolRef::SF_Undefined; 18922fe9c1eSRafael Espindola else 19022fe9c1eSRafael Espindola Result |= SymbolRef::SF_Common; 19122fe9c1eSRafael Espindola } 19222fe9c1eSRafael Espindola 1931df4b84dSDavid Meyer 1941df4b84dSDavid Meyer // TODO: This are certainly too restrictive. 1958ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) 1961df4b84dSDavid Meyer Result |= SymbolRef::SF_Global; 1971df4b84dSDavid Meyer 1988ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) 1991df4b84dSDavid Meyer Result |= SymbolRef::SF_Weak; 2001df4b84dSDavid Meyer 2018ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE) 2021df4b84dSDavid Meyer Result |= SymbolRef::SF_Absolute; 2031df4b84dSDavid Meyer 20420122a43SRafael Espindola return Result; 20501759754SMichael J. Spencer } 20601759754SMichael J. Spencer 207*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref, 2081d6167fdSMichael J. Spencer uint64_t &Result) const { 2098e90adafSMichael J. Spencer // FIXME: Return the correct size. This requires looking at all the symbols 2108e90adafSMichael J. Spencer // in the same section as this symbol, and looking for either the next 2118e90adafSMichael J. Spencer // symbol, or the end of the section. 2128ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 2132617dcceSCraig Topper const coff_section *Section = nullptr; 214*db4ed0bdSRafael Espindola if (std::error_code EC = getSection(Symb->SectionNumber, Section)) 2158ff24d25SRui Ueyama return EC; 216e62ab11fSRafael Espindola 2178ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 2181d6167fdSMichael J. Spencer Result = UnknownAddressOrSize; 2191d6167fdSMichael J. Spencer else if (Section) 2208ff24d25SRui Ueyama Result = Section->SizeOfRawData - Symb->Value; 2211d6167fdSMichael J. Spencer else 2221d6167fdSMichael J. Spencer Result = 0; 2231d6167fdSMichael J. Spencer return object_error::success; 2248e90adafSMichael J. Spencer } 2258e90adafSMichael J. Spencer 226*db4ed0bdSRafael Espindola std::error_code 227*db4ed0bdSRafael Espindola COFFObjectFile::getSymbolSection(DataRefImpl Ref, 22832173153SMichael J. Spencer section_iterator &Result) const { 2298ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 230f078eff3SRui Ueyama if (COFF::isReservedSectionNumber(Symb->SectionNumber)) { 231b5155a57SRafael Espindola Result = section_end(); 232f078eff3SRui Ueyama } else { 2332617dcceSCraig Topper const coff_section *Sec = nullptr; 234*db4ed0bdSRafael Espindola if (std::error_code EC = getSection(Symb->SectionNumber, Sec)) 235*db4ed0bdSRafael Espindola return EC; 2368ff24d25SRui Ueyama DataRefImpl Ref; 2378ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Sec); 2388ff24d25SRui Ueyama Result = section_iterator(SectionRef(Ref, this)); 23932173153SMichael J. Spencer } 24032173153SMichael J. Spencer return object_error::success; 24132173153SMichael J. Spencer } 24232173153SMichael J. Spencer 2435e812afaSRafael Espindola void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { 2448ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2458ff24d25SRui Ueyama Sec += 1; 2468ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Sec); 2478e90adafSMichael J. Spencer } 2488e90adafSMichael J. Spencer 249*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref, 2501d6167fdSMichael J. Spencer StringRef &Result) const { 2518ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2528ff24d25SRui Ueyama return getSectionName(Sec, Result); 2538e90adafSMichael J. Spencer } 2548e90adafSMichael J. Spencer 255*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, 2561d6167fdSMichael J. Spencer uint64_t &Result) const { 2578ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2588ff24d25SRui Ueyama Result = Sec->VirtualAddress; 2591d6167fdSMichael J. Spencer return object_error::success; 2608e90adafSMichael J. Spencer } 2618e90adafSMichael J. Spencer 262*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, 2631d6167fdSMichael J. Spencer uint64_t &Result) const { 2648ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2658ff24d25SRui Ueyama Result = Sec->SizeOfRawData; 2661d6167fdSMichael J. Spencer return object_error::success; 2678e90adafSMichael J. Spencer } 2688e90adafSMichael J. Spencer 269*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, 2701d6167fdSMichael J. Spencer StringRef &Result) const { 2718ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2729da9e693SMichael J. Spencer ArrayRef<uint8_t> Res; 273*db4ed0bdSRafael Espindola std::error_code EC = getSectionContents(Sec, Res); 2749da9e693SMichael J. Spencer Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); 2759da9e693SMichael J. Spencer return EC; 2768e90adafSMichael J. Spencer } 2778e90adafSMichael J. Spencer 278*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, 2797989460aSMichael J. Spencer uint64_t &Res) const { 2808ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2818ff24d25SRui Ueyama if (!Sec) 2827989460aSMichael J. Spencer return object_error::parse_failed; 2838ff24d25SRui Ueyama Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); 2847989460aSMichael J. Spencer return object_error::success; 2857989460aSMichael J. Spencer } 2867989460aSMichael J. Spencer 287*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref, 2881d6167fdSMichael J. Spencer bool &Result) const { 2898ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2908ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 2911d6167fdSMichael J. Spencer return object_error::success; 2928e90adafSMichael J. Spencer } 2938e90adafSMichael J. Spencer 294*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref, 295800619f2SMichael J. Spencer bool &Result) const { 2968ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2978ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 298800619f2SMichael J. Spencer return object_error::success; 299800619f2SMichael J. Spencer } 300800619f2SMichael J. Spencer 301*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, 302800619f2SMichael J. Spencer bool &Result) const { 3038ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3048ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 305800619f2SMichael J. Spencer return object_error::success; 306800619f2SMichael J. Spencer } 307800619f2SMichael J. Spencer 308*db4ed0bdSRafael Espindola std::error_code 309*db4ed0bdSRafael Espindola COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref, 3102138ef6dSPreston Gurd bool &Result) const { 3112138ef6dSPreston Gurd // FIXME: Unimplemented 3122138ef6dSPreston Gurd Result = true; 3132138ef6dSPreston Gurd return object_error::success; 3142138ef6dSPreston Gurd } 3152138ef6dSPreston Gurd 316*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, 3172138ef6dSPreston Gurd bool &Result) const { 3188ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3198ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 3202138ef6dSPreston Gurd return object_error::success; 3212138ef6dSPreston Gurd } 3222138ef6dSPreston Gurd 323*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, 3242138ef6dSPreston Gurd bool &Result) const { 325b96a320aSAndrew Kaylor // FIXME: Unimplemented. 3262138ef6dSPreston Gurd Result = false; 3272138ef6dSPreston Gurd return object_error::success; 3282138ef6dSPreston Gurd } 3292138ef6dSPreston Gurd 330*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, 3313f31fa05SAndrew Kaylor bool &Result) const { 3323f31fa05SAndrew Kaylor // FIXME: Unimplemented. 3333f31fa05SAndrew Kaylor Result = false; 3343f31fa05SAndrew Kaylor return object_error::success; 3353f31fa05SAndrew Kaylor } 3363f31fa05SAndrew Kaylor 337*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, 3388ff24d25SRui Ueyama DataRefImpl SymbRef, 339f6f3e81cSBenjamin Kramer bool &Result) const { 3408ff24d25SRui Ueyama const coff_section *Sec = toSec(SecRef); 3418ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(SymbRef); 3422617dcceSCraig Topper const coff_section *SymbSec = nullptr; 343*db4ed0bdSRafael Espindola if (std::error_code EC = getSection(Symb->SectionNumber, SymbSec)) 344*db4ed0bdSRafael Espindola return EC; 3458ff24d25SRui Ueyama if (SymbSec == Sec) 3469a28851eSMichael J. Spencer Result = true; 3479a28851eSMichael J. Spencer else 348f6f3e81cSBenjamin Kramer Result = false; 349f6f3e81cSBenjamin Kramer return object_error::success; 350f6f3e81cSBenjamin Kramer } 351f6f3e81cSBenjamin Kramer 3528ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 3538ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3548ff24d25SRui Ueyama DataRefImpl Ret; 355827c8a2bSRui Ueyama if (Sec->NumberOfRelocations == 0) { 3568ff24d25SRui Ueyama Ret.p = 0; 357827c8a2bSRui Ueyama } else { 358827c8a2bSRui Ueyama auto begin = reinterpret_cast<const coff_relocation*>( 359827c8a2bSRui Ueyama base() + Sec->PointerToRelocations); 360827c8a2bSRui Ueyama if (Sec->hasExtendedRelocations()) { 361827c8a2bSRui Ueyama // Skip the first relocation entry repurposed to store the number of 362827c8a2bSRui Ueyama // relocations. 363827c8a2bSRui Ueyama begin++; 364827c8a2bSRui Ueyama } 365827c8a2bSRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(begin); 366827c8a2bSRui Ueyama } 3678ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 368e5fd0047SMichael J. Spencer } 369e5fd0047SMichael J. Spencer 370827c8a2bSRui Ueyama static uint32_t getNumberOfRelocations(const coff_section *Sec, 371827c8a2bSRui Ueyama const uint8_t *base) { 372827c8a2bSRui Ueyama // The field for the number of relocations in COFF section table is only 373827c8a2bSRui Ueyama // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to 374827c8a2bSRui Ueyama // NumberOfRelocations field, and the actual relocation count is stored in the 375827c8a2bSRui Ueyama // VirtualAddress field in the first relocation entry. 376827c8a2bSRui Ueyama if (Sec->hasExtendedRelocations()) { 377827c8a2bSRui Ueyama auto *FirstReloc = reinterpret_cast<const coff_relocation*>( 378827c8a2bSRui Ueyama base + Sec->PointerToRelocations); 379827c8a2bSRui Ueyama return FirstReloc->VirtualAddress; 380827c8a2bSRui Ueyama } 381827c8a2bSRui Ueyama return Sec->NumberOfRelocations; 382827c8a2bSRui Ueyama } 383827c8a2bSRui Ueyama 3848ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 3858ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3868ff24d25SRui Ueyama DataRefImpl Ret; 387827c8a2bSRui Ueyama if (Sec->NumberOfRelocations == 0) { 3888ff24d25SRui Ueyama Ret.p = 0; 389827c8a2bSRui Ueyama } else { 390827c8a2bSRui Ueyama auto begin = reinterpret_cast<const coff_relocation*>( 391827c8a2bSRui Ueyama base() + Sec->PointerToRelocations); 392827c8a2bSRui Ueyama uint32_t NumReloc = getNumberOfRelocations(Sec, base()); 393827c8a2bSRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc); 394827c8a2bSRui Ueyama } 3958ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 396e5fd0047SMichael J. Spencer } 397e5fd0047SMichael J. Spencer 398c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 399*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::initSymbolTablePtr() { 400*db4ed0bdSRafael Espindola if (std::error_code EC = getObject( 401c2bed429SRui Ueyama SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable, 402c2bed429SRui Ueyama COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 4038ff24d25SRui Ueyama return EC; 404c2bed429SRui Ueyama 405c2bed429SRui Ueyama // Find string table. The first four byte of the string table contains the 406c2bed429SRui Ueyama // total size of the string table, including the size field itself. If the 407c2bed429SRui Ueyama // string table is empty, the value of the first four byte would be 4. 408c2bed429SRui Ueyama const uint8_t *StringTableAddr = 409c2bed429SRui Ueyama base() + COFFHeader->PointerToSymbolTable + 410c2bed429SRui Ueyama COFFHeader->NumberOfSymbols * sizeof(coff_symbol); 411c2bed429SRui Ueyama const ulittle32_t *StringTableSizePtr; 412*db4ed0bdSRafael Espindola if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 4138ff24d25SRui Ueyama return EC; 414c2bed429SRui Ueyama StringTableSize = *StringTableSizePtr; 415*db4ed0bdSRafael Espindola if (std::error_code EC = 416c2bed429SRui Ueyama getObject(StringTable, Data, StringTableAddr, StringTableSize)) 4178ff24d25SRui Ueyama return EC; 418c2bed429SRui Ueyama 419773a5795SNico Rieck // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some 420773a5795SNico Rieck // tools like cvtres write a size of 0 for an empty table instead of 4. 421773a5795SNico Rieck if (StringTableSize < 4) 422773a5795SNico Rieck StringTableSize = 4; 423773a5795SNico Rieck 424c2bed429SRui Ueyama // Check that the string table is null terminated if has any in it. 425773a5795SNico Rieck if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) 426c2bed429SRui Ueyama return object_error::parse_failed; 427c2bed429SRui Ueyama return object_error::success; 428c2bed429SRui Ueyama } 429c2bed429SRui Ueyama 430215a586cSRui Ueyama // Returns the file offset for the given VA. 431*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { 432b6eb264aSRui Ueyama uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase 433b6eb264aSRui Ueyama : (uint64_t)PE32PlusHeader->ImageBase; 434b7a40081SRui Ueyama uint64_t Rva = Addr - ImageBase; 435b7a40081SRui Ueyama assert(Rva <= UINT32_MAX); 436b7a40081SRui Ueyama return getRvaPtr((uint32_t)Rva, Res); 437215a586cSRui Ueyama } 438215a586cSRui Ueyama 439c2bed429SRui Ueyama // Returns the file offset for the given RVA. 440*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { 44127dc8394SAlexey Samsonov for (const SectionRef &S : sections()) { 44227dc8394SAlexey Samsonov const coff_section *Section = getCOFFSection(S); 443c2bed429SRui Ueyama uint32_t SectionStart = Section->VirtualAddress; 444c2bed429SRui Ueyama uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 445215a586cSRui Ueyama if (SectionStart <= Addr && Addr < SectionEnd) { 446215a586cSRui Ueyama uint32_t Offset = Addr - SectionStart; 447c2bed429SRui Ueyama Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 448c2bed429SRui Ueyama return object_error::success; 449c2bed429SRui Ueyama } 450c2bed429SRui Ueyama } 451c2bed429SRui Ueyama return object_error::parse_failed; 452c2bed429SRui Ueyama } 453c2bed429SRui Ueyama 454c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 455c2bed429SRui Ueyama // table entry. 456*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint, 457*db4ed0bdSRafael Espindola StringRef &Name) const { 458c2bed429SRui Ueyama uintptr_t IntPtr = 0; 459*db4ed0bdSRafael Espindola if (std::error_code EC = getRvaPtr(Rva, IntPtr)) 4608ff24d25SRui Ueyama return EC; 461c2bed429SRui Ueyama const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 462c2bed429SRui Ueyama Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 463c2bed429SRui Ueyama Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 464c2bed429SRui Ueyama return object_error::success; 465c2bed429SRui Ueyama } 466c2bed429SRui Ueyama 467c2bed429SRui Ueyama // Find the import table. 468*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::initImportTablePtr() { 469c2bed429SRui Ueyama // First, we get the RVA of the import table. If the file lacks a pointer to 470c2bed429SRui Ueyama // the import table, do nothing. 471c2bed429SRui Ueyama const data_directory *DataEntry; 472c2bed429SRui Ueyama if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 473c2bed429SRui Ueyama return object_error::success; 474c2bed429SRui Ueyama 475c2bed429SRui Ueyama // Do nothing if the pointer to import table is NULL. 476c2bed429SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 477c2bed429SRui Ueyama return object_error::success; 478c2bed429SRui Ueyama 479c2bed429SRui Ueyama uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 480c2bed429SRui Ueyama NumberOfImportDirectory = DataEntry->Size / 481c2bed429SRui Ueyama sizeof(import_directory_table_entry); 482c2bed429SRui Ueyama 483c2bed429SRui Ueyama // Find the section that contains the RVA. This is needed because the RVA is 484c2bed429SRui Ueyama // the import table's memory address which is different from its file offset. 485c2bed429SRui Ueyama uintptr_t IntPtr = 0; 486*db4ed0bdSRafael Espindola if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 4878ff24d25SRui Ueyama return EC; 488c2bed429SRui Ueyama ImportDirectory = reinterpret_cast< 489c2bed429SRui Ueyama const import_directory_table_entry *>(IntPtr); 490ad882ba8SRui Ueyama return object_error::success; 491ad882ba8SRui Ueyama } 492c2bed429SRui Ueyama 493ad882ba8SRui Ueyama // Find the export table. 494*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::initExportTablePtr() { 495ad882ba8SRui Ueyama // First, we get the RVA of the export table. If the file lacks a pointer to 496ad882ba8SRui Ueyama // the export table, do nothing. 497ad882ba8SRui Ueyama const data_directory *DataEntry; 498ad882ba8SRui Ueyama if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 499ad882ba8SRui Ueyama return object_error::success; 500ad882ba8SRui Ueyama 501ad882ba8SRui Ueyama // Do nothing if the pointer to export table is NULL. 502ad882ba8SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 503ad882ba8SRui Ueyama return object_error::success; 504ad882ba8SRui Ueyama 505ad882ba8SRui Ueyama uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 506ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 507*db4ed0bdSRafael Espindola if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 508ad882ba8SRui Ueyama return EC; 50924fc2d64SRui Ueyama ExportDirectory = 51024fc2d64SRui Ueyama reinterpret_cast<const export_directory_table_entry *>(IntPtr); 511ad882ba8SRui Ueyama return object_error::success; 512c2bed429SRui Ueyama } 513c2bed429SRui Ueyama 514*db4ed0bdSRafael Espindola COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, std::error_code &EC, 515afcc3df7SRafael Espindola bool BufferOwned) 5162617dcceSCraig Topper : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(nullptr), 5172617dcceSCraig Topper PE32Header(nullptr), PE32PlusHeader(nullptr), DataDirectory(nullptr), 5182617dcceSCraig Topper SectionTable(nullptr), SymbolTable(nullptr), StringTable(nullptr), 5192617dcceSCraig Topper StringTableSize(0), ImportDirectory(nullptr), NumberOfImportDirectory(0), 5202617dcceSCraig Topper ExportDirectory(nullptr) { 5211d6167fdSMichael J. Spencer // Check that we at least have enough room for a header. 5228ff24d25SRui Ueyama if (!checkSize(Data, EC, sizeof(coff_file_header))) return; 523ee066fc4SEric Christopher 52482ebd8e3SRui Ueyama // The current location in the file where we are looking at. 52582ebd8e3SRui Ueyama uint64_t CurPtr = 0; 52682ebd8e3SRui Ueyama 52782ebd8e3SRui Ueyama // PE header is optional and is present only in executables. If it exists, 52882ebd8e3SRui Ueyama // it is placed right after COFF header. 5298ff24d25SRui Ueyama bool HasPEHeader = false; 530ee066fc4SEric Christopher 5311d6167fdSMichael J. Spencer // Check if this is a PE/COFF file. 532ec29b121SMichael J. Spencer if (base()[0] == 0x4d && base()[1] == 0x5a) { 533ee066fc4SEric Christopher // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 534ee066fc4SEric Christopher // PE signature to find 'normal' COFF header. 5358ff24d25SRui Ueyama if (!checkSize(Data, EC, 0x3c + 8)) return; 53682ebd8e3SRui Ueyama CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); 53782ebd8e3SRui Ueyama // Check the PE magic bytes. ("PE\0\0") 53882ebd8e3SRui Ueyama if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) { 5398ff24d25SRui Ueyama EC = object_error::parse_failed; 5401d6167fdSMichael J. Spencer return; 5411d6167fdSMichael J. Spencer } 54282ebd8e3SRui Ueyama CurPtr += 4; // Skip the PE magic bytes. 5438ff24d25SRui Ueyama HasPEHeader = true; 544ee066fc4SEric Christopher } 545ee066fc4SEric Christopher 5468ff24d25SRui Ueyama if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 5471d6167fdSMichael J. Spencer return; 54882ebd8e3SRui Ueyama CurPtr += sizeof(coff_file_header); 54982ebd8e3SRui Ueyama 5508ff24d25SRui Ueyama if (HasPEHeader) { 55110ed9ddcSRui Ueyama const pe32_header *Header; 55210ed9ddcSRui Ueyama if ((EC = getObject(Header, Data, base() + CurPtr))) 55382ebd8e3SRui Ueyama return; 55410ed9ddcSRui Ueyama 55510ed9ddcSRui Ueyama const uint8_t *DataDirAddr; 55610ed9ddcSRui Ueyama uint64_t DataDirSize; 55710ed9ddcSRui Ueyama if (Header->Magic == 0x10b) { 55810ed9ddcSRui Ueyama PE32Header = Header; 55910ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32_header); 56010ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 56110ed9ddcSRui Ueyama } else if (Header->Magic == 0x20b) { 56210ed9ddcSRui Ueyama PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); 56310ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); 56410ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; 56510ed9ddcSRui Ueyama } else { 56610ed9ddcSRui Ueyama // It's neither PE32 nor PE32+. 56710ed9ddcSRui Ueyama EC = object_error::parse_failed; 568ed64342bSRui Ueyama return; 569ed64342bSRui Ueyama } 57010ed9ddcSRui Ueyama if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) 57110ed9ddcSRui Ueyama return; 57282ebd8e3SRui Ueyama CurPtr += COFFHeader->SizeOfOptionalHeader; 57382ebd8e3SRui Ueyama } 5741d6167fdSMichael J. Spencer 575692410efSRafael Espindola if (COFFHeader->isImportLibrary()) 576692410efSRafael Espindola return; 577692410efSRafael Espindola 5788ff24d25SRui Ueyama if ((EC = getObject(SectionTable, Data, base() + CurPtr, 579ed64342bSRui Ueyama COFFHeader->NumberOfSections * sizeof(coff_section)))) 5801d6167fdSMichael J. Spencer return; 5811d6167fdSMichael J. Spencer 582c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 583c2bed429SRui Ueyama if (COFFHeader->PointerToSymbolTable != 0) 5848ff24d25SRui Ueyama if ((EC = initSymbolTablePtr())) 5851d6167fdSMichael J. Spencer return; 5868e90adafSMichael J. Spencer 587c2bed429SRui Ueyama // Initialize the pointer to the beginning of the import table. 5888ff24d25SRui Ueyama if ((EC = initImportTablePtr())) 589ed64342bSRui Ueyama return; 5901d6167fdSMichael J. Spencer 591ad882ba8SRui Ueyama // Initialize the pointer to the export table. 5928ff24d25SRui Ueyama if ((EC = initExportTablePtr())) 593ad882ba8SRui Ueyama return; 594ad882ba8SRui Ueyama 5958ff24d25SRui Ueyama EC = object_error::success; 5968e90adafSMichael J. Spencer } 5978e90adafSMichael J. Spencer 598f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const { 5998ff24d25SRui Ueyama DataRefImpl Ret; 6008ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SymbolTable); 601f12b8282SRafael Espindola return basic_symbol_iterator(SymbolRef(Ret, this)); 6028e90adafSMichael J. Spencer } 6038e90adafSMichael J. Spencer 604f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_end_impl() const { 6058e90adafSMichael J. Spencer // The symbol table ends where the string table begins. 6068ff24d25SRui Ueyama DataRefImpl Ret; 6078ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(StringTable); 608f12b8282SRafael Espindola return basic_symbol_iterator(SymbolRef(Ret, this)); 6098e90adafSMichael J. Spencer } 6108e90adafSMichael J. Spencer 611b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_begin() const { 6122fc34c5fSDavid Meyer // TODO: implement 6132fc34c5fSDavid Meyer report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 6142fc34c5fSDavid Meyer } 6152fc34c5fSDavid Meyer 616b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_end() const { 6172fc34c5fSDavid Meyer // TODO: implement 6182fc34c5fSDavid Meyer report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 6192fc34c5fSDavid Meyer } 6202fc34c5fSDavid Meyer 621c429b80dSDavid Meyer StringRef COFFObjectFile::getLoadName() const { 622c429b80dSDavid Meyer // COFF does not have this field. 623c429b80dSDavid Meyer return ""; 624c429b80dSDavid Meyer } 625c429b80dSDavid Meyer 626bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_begin() const { 627a045b73aSRui Ueyama return import_directory_iterator( 628a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, 0, this)); 629c2bed429SRui Ueyama } 630c2bed429SRui Ueyama 631bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_end() const { 632a045b73aSRui Ueyama return import_directory_iterator( 633a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); 634c2bed429SRui Ueyama } 635c429b80dSDavid Meyer 636ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_begin() const { 637ad882ba8SRui Ueyama return export_directory_iterator( 638ad882ba8SRui Ueyama ExportDirectoryEntryRef(ExportDirectory, 0, this)); 639ad882ba8SRui Ueyama } 640ad882ba8SRui Ueyama 641ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_end() const { 6422617dcceSCraig Topper if (!ExportDirectory) 6432617dcceSCraig Topper return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this)); 6448ff24d25SRui Ueyama ExportDirectoryEntryRef Ref(ExportDirectory, 645ad882ba8SRui Ueyama ExportDirectory->AddressTableEntries, this); 6468ff24d25SRui Ueyama return export_directory_iterator(Ref); 647ad882ba8SRui Ueyama } 648ad882ba8SRui Ueyama 649b5155a57SRafael Espindola section_iterator COFFObjectFile::section_begin() const { 6508ff24d25SRui Ueyama DataRefImpl Ret; 6518ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 6528ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 6538e90adafSMichael J. Spencer } 6548e90adafSMichael J. Spencer 655b5155a57SRafael Espindola section_iterator COFFObjectFile::section_end() const { 6568ff24d25SRui Ueyama DataRefImpl Ret; 6578ff24d25SRui Ueyama int NumSections = COFFHeader->isImportLibrary() 65815ba1e20SRui Ueyama ? 0 : COFFHeader->NumberOfSections; 6598ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 6608ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 6618e90adafSMichael J. Spencer } 6628e90adafSMichael J. Spencer 6638e90adafSMichael J. Spencer uint8_t COFFObjectFile::getBytesInAddress() const { 6640324b672SMichael J. Spencer return getArch() == Triple::x86_64 ? 8 : 4; 6658e90adafSMichael J. Spencer } 6668e90adafSMichael J. Spencer 6678e90adafSMichael J. Spencer StringRef COFFObjectFile::getFileFormatName() const { 66882ebd8e3SRui Ueyama switch(COFFHeader->Machine) { 6698e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 6708e90adafSMichael J. Spencer return "COFF-i386"; 6718e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 6728e90adafSMichael J. Spencer return "COFF-x86-64"; 6739b7c0af2SSaleem Abdulrasool case COFF::IMAGE_FILE_MACHINE_ARMNT: 6749b7c0af2SSaleem Abdulrasool return "COFF-ARM"; 6758e90adafSMichael J. Spencer default: 6768e90adafSMichael J. Spencer return "COFF-<unknown arch>"; 6778e90adafSMichael J. Spencer } 6788e90adafSMichael J. Spencer } 6798e90adafSMichael J. Spencer 6808e90adafSMichael J. Spencer unsigned COFFObjectFile::getArch() const { 68182ebd8e3SRui Ueyama switch(COFFHeader->Machine) { 6828e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 6838e90adafSMichael J. Spencer return Triple::x86; 6848e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 6858e90adafSMichael J. Spencer return Triple::x86_64; 6869b7c0af2SSaleem Abdulrasool case COFF::IMAGE_FILE_MACHINE_ARMNT: 6879b7c0af2SSaleem Abdulrasool return Triple::thumb; 6888e90adafSMichael J. Spencer default: 6898e90adafSMichael J. Spencer return Triple::UnknownArch; 6908e90adafSMichael J. Spencer } 6918e90adafSMichael J. Spencer } 6928e90adafSMichael J. Spencer 69382ebd8e3SRui Ueyama // This method is kept here because lld uses this. As soon as we make 69482ebd8e3SRui Ueyama // lld to use getCOFFHeader, this method will be removed. 695*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const { 69682ebd8e3SRui Ueyama return getCOFFHeader(Res); 69782ebd8e3SRui Ueyama } 69882ebd8e3SRui Ueyama 699*db4ed0bdSRafael Espindola std::error_code 700*db4ed0bdSRafael Espindola COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const { 70182ebd8e3SRui Ueyama Res = COFFHeader; 70282ebd8e3SRui Ueyama return object_error::success; 70382ebd8e3SRui Ueyama } 70482ebd8e3SRui Ueyama 705*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 70682ebd8e3SRui Ueyama Res = PE32Header; 70789a7a5eaSMichael J. Spencer return object_error::success; 70889a7a5eaSMichael J. Spencer } 70989a7a5eaSMichael J. Spencer 710*db4ed0bdSRafael Espindola std::error_code 71110ed9ddcSRui Ueyama COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { 71210ed9ddcSRui Ueyama Res = PE32PlusHeader; 71310ed9ddcSRui Ueyama return object_error::success; 71410ed9ddcSRui Ueyama } 71510ed9ddcSRui Ueyama 716*db4ed0bdSRafael Espindola std::error_code 717*db4ed0bdSRafael Espindola COFFObjectFile::getDataDirectory(uint32_t Index, 718ed64342bSRui Ueyama const data_directory *&Res) const { 719ed64342bSRui Ueyama // Error if if there's no data directory or the index is out of range. 72010ed9ddcSRui Ueyama if (!DataDirectory) 72110ed9ddcSRui Ueyama return object_error::parse_failed; 72210ed9ddcSRui Ueyama assert(PE32Header || PE32PlusHeader); 72310ed9ddcSRui Ueyama uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize 72410ed9ddcSRui Ueyama : PE32PlusHeader->NumberOfRvaAndSize; 72510ed9ddcSRui Ueyama if (Index > NumEnt) 726ed64342bSRui Ueyama return object_error::parse_failed; 7278ff24d25SRui Ueyama Res = &DataDirectory[Index]; 728ed64342bSRui Ueyama return object_error::success; 729ed64342bSRui Ueyama } 730ed64342bSRui Ueyama 731*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSection(int32_t Index, 7321d6167fdSMichael J. Spencer const coff_section *&Result) const { 7331d6167fdSMichael J. Spencer // Check for special index values. 734f078eff3SRui Ueyama if (COFF::isReservedSectionNumber(Index)) 7352617dcceSCraig Topper Result = nullptr; 7368ff24d25SRui Ueyama else if (Index > 0 && Index <= COFFHeader->NumberOfSections) 7371d6167fdSMichael J. Spencer // We already verified the section table data, so no need to check again. 7388ff24d25SRui Ueyama Result = SectionTable + (Index - 1); 7391d6167fdSMichael J. Spencer else 7401d6167fdSMichael J. Spencer return object_error::parse_failed; 7411d6167fdSMichael J. Spencer return object_error::success; 7428e90adafSMichael J. Spencer } 7438e90adafSMichael J. Spencer 744*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getString(uint32_t Offset, 7451d6167fdSMichael J. Spencer StringRef &Result) const { 7461d6167fdSMichael J. Spencer if (StringTableSize <= 4) 7471d6167fdSMichael J. Spencer // Tried to get a string from an empty string table. 7481d6167fdSMichael J. Spencer return object_error::parse_failed; 7498ff24d25SRui Ueyama if (Offset >= StringTableSize) 7501d6167fdSMichael J. Spencer return object_error::unexpected_eof; 7518ff24d25SRui Ueyama Result = StringRef(StringTable + Offset); 7521d6167fdSMichael J. Spencer return object_error::success; 7538e90adafSMichael J. Spencer } 754022ecdf2SBenjamin Kramer 755*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSymbol(uint32_t Index, 756e5fd0047SMichael J. Spencer const coff_symbol *&Result) const { 7578ff24d25SRui Ueyama if (Index < COFFHeader->NumberOfSymbols) 7588ff24d25SRui Ueyama Result = SymbolTable + Index; 759e5fd0047SMichael J. Spencer else 760e5fd0047SMichael J. Spencer return object_error::parse_failed; 761e5fd0047SMichael J. Spencer return object_error::success; 762e5fd0047SMichael J. Spencer } 763e5fd0047SMichael J. Spencer 764*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol, 76589a7a5eaSMichael J. Spencer StringRef &Res) const { 76689a7a5eaSMichael J. Spencer // Check for string table entry. First 4 bytes are 0. 7678ff24d25SRui Ueyama if (Symbol->Name.Offset.Zeroes == 0) { 7688ff24d25SRui Ueyama uint32_t Offset = Symbol->Name.Offset.Offset; 769*db4ed0bdSRafael Espindola if (std::error_code EC = getString(Offset, Res)) 7708ff24d25SRui Ueyama return EC; 77189a7a5eaSMichael J. Spencer return object_error::success; 77289a7a5eaSMichael J. Spencer } 77389a7a5eaSMichael J. Spencer 7748ff24d25SRui Ueyama if (Symbol->Name.ShortName[7] == 0) 77589a7a5eaSMichael J. Spencer // Null terminated, let ::strlen figure out the length. 7768ff24d25SRui Ueyama Res = StringRef(Symbol->Name.ShortName); 77789a7a5eaSMichael J. Spencer else 77889a7a5eaSMichael J. Spencer // Not null terminated, use all 8 bytes. 7798ff24d25SRui Ueyama Res = StringRef(Symbol->Name.ShortName, 8); 78089a7a5eaSMichael J. Spencer return object_error::success; 78189a7a5eaSMichael J. Spencer } 78289a7a5eaSMichael J. Spencer 78371757ef3SMarshall Clow ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData( 7848ff24d25SRui Ueyama const coff_symbol *Symbol) const { 7852617dcceSCraig Topper const uint8_t *Aux = nullptr; 78671757ef3SMarshall Clow 7878ff24d25SRui Ueyama if (Symbol->NumberOfAuxSymbols > 0) { 78871757ef3SMarshall Clow // AUX data comes immediately after the symbol in COFF 7898ff24d25SRui Ueyama Aux = reinterpret_cast<const uint8_t *>(Symbol + 1); 79071757ef3SMarshall Clow # ifndef NDEBUG 7918ff24d25SRui Ueyama // Verify that the Aux symbol points to a valid entry in the symbol table. 7928ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 7938ff24d25SRui Ueyama if (Offset < COFFHeader->PointerToSymbolTable 7948ff24d25SRui Ueyama || Offset >= COFFHeader->PointerToSymbolTable 79582ebd8e3SRui Ueyama + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 79671757ef3SMarshall Clow report_fatal_error("Aux Symbol data was outside of symbol table."); 79771757ef3SMarshall Clow 7988ff24d25SRui Ueyama assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 79971757ef3SMarshall Clow == 0 && "Aux Symbol data did not point to the beginning of a symbol"); 80071757ef3SMarshall Clow # endif 801bfb85e67SMarshall Clow } 80224fc2d64SRui Ueyama return ArrayRef<uint8_t>(Aux, 80324fc2d64SRui Ueyama Symbol->NumberOfAuxSymbols * sizeof(coff_symbol)); 80471757ef3SMarshall Clow } 80571757ef3SMarshall Clow 806*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getSectionName(const coff_section *Sec, 80753c2d547SMichael J. Spencer StringRef &Res) const { 80853c2d547SMichael J. Spencer StringRef Name; 80953c2d547SMichael J. Spencer if (Sec->Name[7] == 0) 81053c2d547SMichael J. Spencer // Null terminated, let ::strlen figure out the length. 81153c2d547SMichael J. Spencer Name = Sec->Name; 81253c2d547SMichael J. Spencer else 81353c2d547SMichael J. Spencer // Not null terminated, use all 8 bytes. 81453c2d547SMichael J. Spencer Name = StringRef(Sec->Name, 8); 81553c2d547SMichael J. Spencer 81653c2d547SMichael J. Spencer // Check for string table entry. First byte is '/'. 81753c2d547SMichael J. Spencer if (Name[0] == '/') { 81853c2d547SMichael J. Spencer uint32_t Offset; 8199d2c15efSNico Rieck if (Name[1] == '/') { 8209d2c15efSNico Rieck if (decodeBase64StringEntry(Name.substr(2), Offset)) 8219d2c15efSNico Rieck return object_error::parse_failed; 8229d2c15efSNico Rieck } else { 82353c2d547SMichael J. Spencer if (Name.substr(1).getAsInteger(10, Offset)) 82453c2d547SMichael J. Spencer return object_error::parse_failed; 8259d2c15efSNico Rieck } 826*db4ed0bdSRafael Espindola if (std::error_code EC = getString(Offset, Name)) 8278ff24d25SRui Ueyama return EC; 82853c2d547SMichael J. Spencer } 82953c2d547SMichael J. Spencer 83053c2d547SMichael J. Spencer Res = Name; 83153c2d547SMichael J. Spencer return object_error::success; 83253c2d547SMichael J. Spencer } 83353c2d547SMichael J. Spencer 834*db4ed0bdSRafael Espindola std::error_code 835*db4ed0bdSRafael Espindola COFFObjectFile::getSectionContents(const coff_section *Sec, 8369da9e693SMichael J. Spencer ArrayRef<uint8_t> &Res) const { 8379da9e693SMichael J. Spencer // The only thing that we need to verify is that the contents is contained 8389da9e693SMichael J. Spencer // within the file bounds. We don't need to make sure it doesn't cover other 8399da9e693SMichael J. Spencer // data, as there's nothing that says that is not allowed. 8409da9e693SMichael J. Spencer uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 8419da9e693SMichael J. Spencer uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; 8429da9e693SMichael J. Spencer if (ConEnd > uintptr_t(Data->getBufferEnd())) 8439da9e693SMichael J. Spencer return object_error::parse_failed; 8449da9e693SMichael J. Spencer Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart), 8459da9e693SMichael J. Spencer Sec->SizeOfRawData); 8469da9e693SMichael J. Spencer return object_error::success; 8479da9e693SMichael J. Spencer } 8489da9e693SMichael J. Spencer 849022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 850e5fd0047SMichael J. Spencer return reinterpret_cast<const coff_relocation*>(Rel.p); 851022ecdf2SBenjamin Kramer } 8528ff24d25SRui Ueyama 8535e812afaSRafael Espindola void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 854e5fd0047SMichael J. Spencer Rel.p = reinterpret_cast<uintptr_t>( 855e5fd0047SMichael J. Spencer reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 856022ecdf2SBenjamin Kramer } 8578ff24d25SRui Ueyama 858*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, 859022ecdf2SBenjamin Kramer uint64_t &Res) const { 8601e483879SRafael Espindola report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); 861022ecdf2SBenjamin Kramer } 8628ff24d25SRui Ueyama 863*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, 864cbe72fc9SDanil Malyshev uint64_t &Res) const { 865cbe72fc9SDanil Malyshev Res = toRel(Rel)->VirtualAddress; 866cbe72fc9SDanil Malyshev return object_error::success; 867cbe72fc9SDanil Malyshev } 8688ff24d25SRui Ueyama 869806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 870022ecdf2SBenjamin Kramer const coff_relocation* R = toRel(Rel); 8718ff24d25SRui Ueyama DataRefImpl Ref; 8728ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); 8738ff24d25SRui Ueyama return symbol_iterator(SymbolRef(Ref, this)); 874022ecdf2SBenjamin Kramer } 8758ff24d25SRui Ueyama 876*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, 8777be76590SOwen Anderson uint64_t &Res) const { 878022ecdf2SBenjamin Kramer const coff_relocation* R = toRel(Rel); 879022ecdf2SBenjamin Kramer Res = R->Type; 880022ecdf2SBenjamin Kramer return object_error::success; 881022ecdf2SBenjamin Kramer } 882e5fd0047SMichael J. Spencer 88327dc8394SAlexey Samsonov const coff_section * 88427dc8394SAlexey Samsonov COFFObjectFile::getCOFFSection(const SectionRef &Section) const { 88527dc8394SAlexey Samsonov return toSec(Section.getRawDataRefImpl()); 88671757ef3SMarshall Clow } 88771757ef3SMarshall Clow 88827dc8394SAlexey Samsonov const coff_symbol * 88927dc8394SAlexey Samsonov COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { 89027dc8394SAlexey Samsonov return toSymb(Symbol.getRawDataRefImpl()); 89171757ef3SMarshall Clow } 89271757ef3SMarshall Clow 893f12b8282SRafael Espindola const coff_relocation * 89427dc8394SAlexey Samsonov COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { 89527dc8394SAlexey Samsonov return toRel(Reloc.getRawDataRefImpl()); 896d3e2a76cSMarshall Clow } 897d3e2a76cSMarshall Clow 89827dc8394SAlexey Samsonov #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ 89927dc8394SAlexey Samsonov case COFF::reloc_type: \ 90027dc8394SAlexey Samsonov Res = #reloc_type; \ 90127dc8394SAlexey Samsonov break; 902e5fd0047SMichael J. Spencer 903*db4ed0bdSRafael Espindola std::error_code 904*db4ed0bdSRafael Espindola COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, 905e5fd0047SMichael J. Spencer SmallVectorImpl<char> &Result) const { 9068ff24d25SRui Ueyama const coff_relocation *Reloc = toRel(Rel); 9078ff24d25SRui Ueyama StringRef Res; 90882ebd8e3SRui Ueyama switch (COFFHeader->Machine) { 909e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 9108ff24d25SRui Ueyama switch (Reloc->Type) { 911e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 912e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 913e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 914e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 915e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 916e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 917e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 918e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 919e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 920e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 921e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 922e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 923e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 924e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 925e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 926e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 927e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 928e5fd0047SMichael J. Spencer default: 9298ff24d25SRui Ueyama Res = "Unknown"; 930e5fd0047SMichael J. Spencer } 931e5fd0047SMichael J. Spencer break; 9325c503bf4SSaleem Abdulrasool case COFF::IMAGE_FILE_MACHINE_ARMNT: 9335c503bf4SSaleem Abdulrasool switch (Reloc->Type) { 9345c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE); 9355c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32); 9365c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB); 9375c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24); 9385c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11); 9395c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN); 9405c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24); 9415c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11); 9425c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION); 9435c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL); 9445c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A); 9455c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T); 9465c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T); 9475c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T); 9485c503bf4SSaleem Abdulrasool LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T); 9495c503bf4SSaleem Abdulrasool default: 9505c503bf4SSaleem Abdulrasool Res = "Unknown"; 9515c503bf4SSaleem Abdulrasool } 9525c503bf4SSaleem Abdulrasool break; 953e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 9548ff24d25SRui Ueyama switch (Reloc->Type) { 955e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 956e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 957e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 958e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 959e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 960e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 961e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 962e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 963e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 964e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 965e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 966e5fd0047SMichael J. Spencer default: 9678ff24d25SRui Ueyama Res = "Unknown"; 968e5fd0047SMichael J. Spencer } 969e5fd0047SMichael J. Spencer break; 970e5fd0047SMichael J. Spencer default: 9718ff24d25SRui Ueyama Res = "Unknown"; 972e5fd0047SMichael J. Spencer } 9738ff24d25SRui Ueyama Result.append(Res.begin(), Res.end()); 974e5fd0047SMichael J. Spencer return object_error::success; 975e5fd0047SMichael J. Spencer } 976e5fd0047SMichael J. Spencer 977e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 978e5fd0047SMichael J. Spencer 979*db4ed0bdSRafael Espindola std::error_code 980*db4ed0bdSRafael Espindola COFFObjectFile::getRelocationValueString(DataRefImpl Rel, 981e5fd0047SMichael J. Spencer SmallVectorImpl<char> &Result) const { 9828ff24d25SRui Ueyama const coff_relocation *Reloc = toRel(Rel); 9832617dcceSCraig Topper const coff_symbol *Symb = nullptr; 984*db4ed0bdSRafael Espindola if (std::error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) 985*db4ed0bdSRafael Espindola return EC; 9868ff24d25SRui Ueyama DataRefImpl Sym; 9878ff24d25SRui Ueyama Sym.p = reinterpret_cast<uintptr_t>(Symb); 9888ff24d25SRui Ueyama StringRef SymName; 989*db4ed0bdSRafael Espindola if (std::error_code EC = getSymbolName(Sym, SymName)) 990*db4ed0bdSRafael Espindola return EC; 9918ff24d25SRui Ueyama Result.append(SymName.begin(), SymName.end()); 992e5fd0047SMichael J. Spencer return object_error::success; 993022ecdf2SBenjamin Kramer } 9948e90adafSMichael J. Spencer 995*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData, 9962fc34c5fSDavid Meyer LibraryRef &Result) const { 9972fc34c5fSDavid Meyer report_fatal_error("getLibraryNext not implemented in COFFObjectFile"); 9982fc34c5fSDavid Meyer } 9992fc34c5fSDavid Meyer 1000*db4ed0bdSRafael Espindola std::error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData, 10012fc34c5fSDavid Meyer StringRef &Result) const { 10022fc34c5fSDavid Meyer report_fatal_error("getLibraryPath not implemented in COFFObjectFile"); 10032fc34c5fSDavid Meyer } 10042fc34c5fSDavid Meyer 1005c2bed429SRui Ueyama bool ImportDirectoryEntryRef:: 1006c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const { 1007a045b73aSRui Ueyama return ImportTable == Other.ImportTable && Index == Other.Index; 1008c2bed429SRui Ueyama } 1009c2bed429SRui Ueyama 10105e812afaSRafael Espindola void ImportDirectoryEntryRef::moveNext() { 10115e812afaSRafael Espindola ++Index; 1012c2bed429SRui Ueyama } 1013c2bed429SRui Ueyama 1014*db4ed0bdSRafael Espindola std::error_code ImportDirectoryEntryRef::getImportTableEntry( 1015*db4ed0bdSRafael Espindola const import_directory_table_entry *&Result) const { 1016a045b73aSRui Ueyama Result = ImportTable; 1017c2bed429SRui Ueyama return object_error::success; 1018c2bed429SRui Ueyama } 1019c2bed429SRui Ueyama 1020*db4ed0bdSRafael Espindola std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 1021c2bed429SRui Ueyama uintptr_t IntPtr = 0; 1022*db4ed0bdSRafael Espindola if (std::error_code EC = 1023*db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr)) 1024a045b73aSRui Ueyama return EC; 1025a045b73aSRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1026c2bed429SRui Ueyama return object_error::success; 1027c2bed429SRui Ueyama } 1028c2bed429SRui Ueyama 1029*db4ed0bdSRafael Espindola std::error_code ImportDirectoryEntryRef::getImportLookupEntry( 1030c2bed429SRui Ueyama const import_lookup_table_entry32 *&Result) const { 1031c2bed429SRui Ueyama uintptr_t IntPtr = 0; 1032*db4ed0bdSRafael Espindola if (std::error_code EC = 1033a045b73aSRui Ueyama OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr)) 1034a045b73aSRui Ueyama return EC; 1035c2bed429SRui Ueyama Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); 1036c2bed429SRui Ueyama return object_error::success; 1037c2bed429SRui Ueyama } 1038c2bed429SRui Ueyama 1039ad882ba8SRui Ueyama bool ExportDirectoryEntryRef:: 1040ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const { 1041ad882ba8SRui Ueyama return ExportTable == Other.ExportTable && Index == Other.Index; 1042ad882ba8SRui Ueyama } 1043ad882ba8SRui Ueyama 10445e812afaSRafael Espindola void ExportDirectoryEntryRef::moveNext() { 10455e812afaSRafael Espindola ++Index; 1046ad882ba8SRui Ueyama } 1047ad882ba8SRui Ueyama 1048da49d0d4SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 1049da49d0d4SRui Ueyama // by ordinal, the empty string is set as a result. 1050*db4ed0bdSRafael Espindola std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { 1051da49d0d4SRui Ueyama uintptr_t IntPtr = 0; 1052*db4ed0bdSRafael Espindola if (std::error_code EC = 1053*db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) 1054da49d0d4SRui Ueyama return EC; 1055da49d0d4SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1056da49d0d4SRui Ueyama return object_error::success; 1057da49d0d4SRui Ueyama } 1058da49d0d4SRui Ueyama 1059e5df6095SRui Ueyama // Returns the starting ordinal number. 1060*db4ed0bdSRafael Espindola std::error_code 1061*db4ed0bdSRafael Espindola ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { 1062e5df6095SRui Ueyama Result = ExportTable->OrdinalBase; 1063e5df6095SRui Ueyama return object_error::success; 1064e5df6095SRui Ueyama } 1065e5df6095SRui Ueyama 1066ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol. 1067*db4ed0bdSRafael Espindola std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 1068ad882ba8SRui Ueyama Result = ExportTable->OrdinalBase + Index; 1069ad882ba8SRui Ueyama return object_error::success; 1070ad882ba8SRui Ueyama } 1071ad882ba8SRui Ueyama 1072ad882ba8SRui Ueyama // Returns the address of the current export symbol. 1073*db4ed0bdSRafael Espindola std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 1074ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 1075*db4ed0bdSRafael Espindola if (std::error_code EC = 1076*db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) 1077ad882ba8SRui Ueyama return EC; 107824fc2d64SRui Ueyama const export_address_table_entry *entry = 107924fc2d64SRui Ueyama reinterpret_cast<const export_address_table_entry *>(IntPtr); 1080ad882ba8SRui Ueyama Result = entry[Index].ExportRVA; 1081ad882ba8SRui Ueyama return object_error::success; 1082ad882ba8SRui Ueyama } 1083ad882ba8SRui Ueyama 1084ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 1085ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result. 1086*db4ed0bdSRafael Espindola std::error_code 1087*db4ed0bdSRafael Espindola ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { 1088ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 1089*db4ed0bdSRafael Espindola if (std::error_code EC = 1090*db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) 1091ad882ba8SRui Ueyama return EC; 1092ad882ba8SRui Ueyama const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 1093ad882ba8SRui Ueyama 1094ad882ba8SRui Ueyama uint32_t NumEntries = ExportTable->NumberOfNamePointers; 1095ad882ba8SRui Ueyama int Offset = 0; 1096ad882ba8SRui Ueyama for (const ulittle16_t *I = Start, *E = Start + NumEntries; 1097ad882ba8SRui Ueyama I < E; ++I, ++Offset) { 1098ad882ba8SRui Ueyama if (*I != Index) 1099ad882ba8SRui Ueyama continue; 1100*db4ed0bdSRafael Espindola if (std::error_code EC = 1101*db4ed0bdSRafael Espindola OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) 1102ad882ba8SRui Ueyama return EC; 1103ad882ba8SRui Ueyama const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 1104*db4ed0bdSRafael Espindola if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 1105ad882ba8SRui Ueyama return EC; 1106ad882ba8SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1107ad882ba8SRui Ueyama return object_error::success; 1108ad882ba8SRui Ueyama } 1109ad882ba8SRui Ueyama Result = ""; 1110ad882ba8SRui Ueyama return object_error::success; 1111ad882ba8SRui Ueyama } 1112ad882ba8SRui Ueyama 1113afcc3df7SRafael Espindola ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object, 1114afcc3df7SRafael Espindola bool BufferOwned) { 1115*db4ed0bdSRafael Espindola std::error_code EC; 111656440fd8SAhmed Charles std::unique_ptr<COFFObjectFile> Ret( 111756440fd8SAhmed Charles new COFFObjectFile(Object, EC, BufferOwned)); 1118692410efSRafael Espindola if (EC) 1119692410efSRafael Espindola return EC; 112096c9d95fSAhmed Charles return Ret.release(); 1121686738e2SRui Ueyama } 1122