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" 19c2bed429SRui Ueyama #include "llvm/Support/Debug.h" 20c2bed429SRui Ueyama #include "llvm/Support/raw_ostream.h" 21981af002SWill Dietz #include <cctype> 22*9d2c15efSNico Rieck #include <limits> 238e90adafSMichael J. Spencer 248e90adafSMichael J. Spencer using namespace llvm; 258e90adafSMichael J. Spencer using namespace object; 268e90adafSMichael J. Spencer 278e90adafSMichael J. Spencer using support::ulittle8_t; 288e90adafSMichael J. Spencer using support::ulittle16_t; 298e90adafSMichael J. Spencer using support::ulittle32_t; 308e90adafSMichael J. Spencer using support::little16_t; 318e90adafSMichael J. Spencer 321d6167fdSMichael J. Spencer // Returns false if size is greater than the buffer size. And sets ec. 33686738e2SRui Ueyama static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) { 348ff24d25SRui Ueyama if (M->getBufferSize() < Size) { 358ff24d25SRui Ueyama EC = object_error::unexpected_eof; 361d6167fdSMichael J. Spencer return false; 371d6167fdSMichael J. Spencer } 381d6167fdSMichael J. Spencer return true; 398e90adafSMichael J. Spencer } 408e90adafSMichael J. Spencer 41ed64342bSRui Ueyama // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. 42ed64342bSRui Ueyama // Returns unexpected_eof if error. 43ed64342bSRui Ueyama template<typename T> 44686738e2SRui Ueyama static error_code getObject(const T *&Obj, const MemoryBuffer *M, 45686738e2SRui Ueyama const uint8_t *Ptr, const size_t Size = sizeof(T)) { 46ed64342bSRui Ueyama uintptr_t Addr = uintptr_t(Ptr); 47ed64342bSRui Ueyama if (Addr + Size < Addr || 48ed64342bSRui Ueyama Addr + Size < Size || 49ed64342bSRui Ueyama Addr + Size > uintptr_t(M->getBufferEnd())) { 50ed64342bSRui Ueyama return object_error::unexpected_eof; 511d6167fdSMichael J. Spencer } 52ed64342bSRui Ueyama Obj = reinterpret_cast<const T *>(Addr); 53ed64342bSRui Ueyama return object_error::success; 541d6167fdSMichael J. Spencer } 551d6167fdSMichael J. Spencer 56*9d2c15efSNico Rieck // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without 57*9d2c15efSNico Rieck // prefixed slashes. 58*9d2c15efSNico Rieck static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) { 59*9d2c15efSNico Rieck assert(Str.size() <= 6 && "String too long, possible overflow."); 60*9d2c15efSNico Rieck if (Str.size() > 6) 61*9d2c15efSNico Rieck return true; 62*9d2c15efSNico Rieck 63*9d2c15efSNico Rieck uint64_t Value = 0; 64*9d2c15efSNico Rieck while (!Str.empty()) { 65*9d2c15efSNico Rieck unsigned CharVal; 66*9d2c15efSNico Rieck if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25 67*9d2c15efSNico Rieck CharVal = Str[0] - 'A'; 68*9d2c15efSNico Rieck else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51 69*9d2c15efSNico Rieck CharVal = Str[0] - 'a' + 26; 70*9d2c15efSNico Rieck else if (Str[0] >= '0' && Str[0] <= '9') // 52..61 71*9d2c15efSNico Rieck CharVal = Str[0] - '0' + 52; 72*9d2c15efSNico Rieck else if (Str[0] == '+') // 62 73*9d2c15efSNico Rieck CharVal = Str[0] - '+' + 62; 74*9d2c15efSNico Rieck else if (Str[0] == '/') // 63 75*9d2c15efSNico Rieck CharVal = Str[0] - '/' + 63; 76*9d2c15efSNico Rieck else 77*9d2c15efSNico Rieck return true; 78*9d2c15efSNico Rieck 79*9d2c15efSNico Rieck Value = (Value * 64) + CharVal; 80*9d2c15efSNico Rieck Str = Str.substr(1); 81*9d2c15efSNico Rieck } 82*9d2c15efSNico Rieck 83*9d2c15efSNico Rieck if (Value > std::numeric_limits<uint32_t>::max()) 84*9d2c15efSNico Rieck return true; 85*9d2c15efSNico Rieck 86*9d2c15efSNico Rieck Result = static_cast<uint32_t>(Value); 87*9d2c15efSNico Rieck return false; 88*9d2c15efSNico Rieck } 89*9d2c15efSNico Rieck 908ff24d25SRui Ueyama const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const { 918ff24d25SRui Ueyama const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p); 921d6167fdSMichael J. Spencer 931d6167fdSMichael J. Spencer # ifndef NDEBUG 941d6167fdSMichael J. Spencer // Verify that the symbol points to a valid entry in the symbol table. 958ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); 968ff24d25SRui Ueyama if (Offset < COFFHeader->PointerToSymbolTable 978ff24d25SRui Ueyama || Offset >= COFFHeader->PointerToSymbolTable 9882ebd8e3SRui Ueyama + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 991d6167fdSMichael J. Spencer report_fatal_error("Symbol was outside of symbol table."); 1001d6167fdSMichael J. Spencer 1018ff24d25SRui Ueyama assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 1021d6167fdSMichael J. Spencer == 0 && "Symbol did not point to the beginning of a symbol"); 1031d6167fdSMichael J. Spencer # endif 1041d6167fdSMichael J. Spencer 1058ff24d25SRui Ueyama return Addr; 1061d6167fdSMichael J. Spencer } 1071d6167fdSMichael J. Spencer 1088ff24d25SRui Ueyama const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { 1098ff24d25SRui Ueyama const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); 1101d6167fdSMichael J. Spencer 1111d6167fdSMichael J. Spencer # ifndef NDEBUG 1121d6167fdSMichael J. Spencer // Verify that the section points to a valid entry in the section table. 1138ff24d25SRui Ueyama if (Addr < SectionTable 1148ff24d25SRui Ueyama || Addr >= (SectionTable + COFFHeader->NumberOfSections)) 1151d6167fdSMichael J. Spencer report_fatal_error("Section was outside of section table."); 1161d6167fdSMichael J. Spencer 1178ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); 1188ff24d25SRui Ueyama assert(Offset % sizeof(coff_section) == 0 && 1191d6167fdSMichael J. Spencer "Section did not point to the beginning of a section"); 1201d6167fdSMichael J. Spencer # endif 1211d6167fdSMichael J. Spencer 1228ff24d25SRui Ueyama return Addr; 1231d6167fdSMichael J. Spencer } 1241d6167fdSMichael J. Spencer 1255e812afaSRafael Espindola void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { 1268ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1278ff24d25SRui Ueyama Symb += 1 + Symb->NumberOfAuxSymbols; 1288ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Symb); 1291d6167fdSMichael J. Spencer } 1301d6167fdSMichael J. Spencer 1318ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(DataRefImpl Ref, 1321d6167fdSMichael J. Spencer StringRef &Result) const { 1338ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1348ff24d25SRui Ueyama return getSymbolName(Symb, Result); 1358e90adafSMichael J. Spencer } 1368e90adafSMichael J. Spencer 1378ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref, 1381d6167fdSMichael J. Spencer uint64_t &Result) const { 1398ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1405ebaed24SMichael J. Spencer const coff_section *Section = NULL; 1418ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 1428ff24d25SRui Ueyama return EC; 143e62ab11fSRafael Espindola 1448ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 1451d6167fdSMichael J. Spencer Result = UnknownAddressOrSize; 1461d6167fdSMichael J. Spencer else if (Section) 1478ff24d25SRui Ueyama Result = Section->PointerToRawData + Symb->Value; 1481d6167fdSMichael J. Spencer else 1498ff24d25SRui Ueyama Result = Symb->Value; 1501d6167fdSMichael J. Spencer return object_error::success; 1518e90adafSMichael J. Spencer } 1528e90adafSMichael J. Spencer 1538ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref, 15475d1cf33SBenjamin Kramer uint64_t &Result) const { 1558ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 15675d1cf33SBenjamin Kramer const coff_section *Section = NULL; 1578ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 1588ff24d25SRui Ueyama return EC; 159e62ab11fSRafael Espindola 1608ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 16175d1cf33SBenjamin Kramer Result = UnknownAddressOrSize; 16275d1cf33SBenjamin Kramer else if (Section) 1638ff24d25SRui Ueyama Result = Section->VirtualAddress + Symb->Value; 16475d1cf33SBenjamin Kramer else 1658ff24d25SRui Ueyama Result = Symb->Value; 16675d1cf33SBenjamin Kramer return object_error::success; 16775d1cf33SBenjamin Kramer } 16875d1cf33SBenjamin Kramer 1698ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolType(DataRefImpl Ref, 170d3946676SMichael J. Spencer SymbolRef::Type &Result) const { 1718ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 17275d1cf33SBenjamin Kramer Result = SymbolRef::ST_Other; 1738ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && 1748ff24d25SRui Ueyama Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { 1757e4b976cSDavid Meyer Result = SymbolRef::ST_Unknown; 1765efa665fSRui Ueyama } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) { 17775d1cf33SBenjamin Kramer Result = SymbolRef::ST_Function; 17875d1cf33SBenjamin Kramer } else { 17906adfac8SRafael Espindola uint32_t Characteristics = 0; 1808ff24d25SRui Ueyama if (Symb->SectionNumber > 0) { 18106adfac8SRafael Espindola const coff_section *Section = NULL; 1828ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 1838ff24d25SRui Ueyama return EC; 18406adfac8SRafael Espindola Characteristics = Section->Characteristics; 18575d1cf33SBenjamin Kramer } 18606adfac8SRafael Espindola if (Characteristics & COFF::IMAGE_SCN_MEM_READ && 18706adfac8SRafael Espindola ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. 18806adfac8SRafael Espindola Result = SymbolRef::ST_Data; 18975d1cf33SBenjamin Kramer } 19075d1cf33SBenjamin Kramer return object_error::success; 19175d1cf33SBenjamin Kramer } 19275d1cf33SBenjamin Kramer 19320122a43SRafael Espindola uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { 1948ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 19520122a43SRafael Espindola uint32_t Result = SymbolRef::SF_None; 19675d1cf33SBenjamin Kramer 197975e115eSRafael Espindola // TODO: Correctly set SF_FormatSpecific, SF_Common 1987e4b976cSDavid Meyer 19922fe9c1eSRafael Espindola if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { 20022fe9c1eSRafael Espindola if (Symb->Value == 0) 2017e4b976cSDavid Meyer Result |= SymbolRef::SF_Undefined; 20222fe9c1eSRafael Espindola else 20322fe9c1eSRafael Espindola Result |= SymbolRef::SF_Common; 20422fe9c1eSRafael Espindola } 20522fe9c1eSRafael Espindola 2061df4b84dSDavid Meyer 2071df4b84dSDavid Meyer // TODO: This are certainly too restrictive. 2088ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) 2091df4b84dSDavid Meyer Result |= SymbolRef::SF_Global; 2101df4b84dSDavid Meyer 2118ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) 2121df4b84dSDavid Meyer Result |= SymbolRef::SF_Weak; 2131df4b84dSDavid Meyer 2148ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE) 2151df4b84dSDavid Meyer Result |= SymbolRef::SF_Absolute; 2161df4b84dSDavid Meyer 21720122a43SRafael Espindola return Result; 21801759754SMichael J. Spencer } 21901759754SMichael J. Spencer 2208ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref, 2211d6167fdSMichael J. Spencer uint64_t &Result) const { 2228e90adafSMichael J. Spencer // FIXME: Return the correct size. This requires looking at all the symbols 2238e90adafSMichael J. Spencer // in the same section as this symbol, and looking for either the next 2248e90adafSMichael J. Spencer // symbol, or the end of the section. 2258ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 2265ebaed24SMichael J. Spencer const coff_section *Section = NULL; 2278ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 2288ff24d25SRui Ueyama return EC; 229e62ab11fSRafael Espindola 2308ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 2311d6167fdSMichael J. Spencer Result = UnknownAddressOrSize; 2321d6167fdSMichael J. Spencer else if (Section) 2338ff24d25SRui Ueyama Result = Section->SizeOfRawData - Symb->Value; 2341d6167fdSMichael J. Spencer else 2351d6167fdSMichael J. Spencer Result = 0; 2361d6167fdSMichael J. Spencer return object_error::success; 2378e90adafSMichael J. Spencer } 2388e90adafSMichael J. Spencer 2398ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref, 24032173153SMichael J. Spencer section_iterator &Result) const { 2418ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 2428ff24d25SRui Ueyama if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED) 243b5155a57SRafael Espindola Result = section_end(); 24432173153SMichael J. Spencer else { 2458ff24d25SRui Ueyama const coff_section *Sec = 0; 2468ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC; 2478ff24d25SRui Ueyama DataRefImpl Ref; 2488ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Sec); 2498ff24d25SRui Ueyama Result = section_iterator(SectionRef(Ref, this)); 25032173153SMichael J. Spencer } 25132173153SMichael J. Spencer return object_error::success; 25232173153SMichael J. Spencer } 25332173153SMichael J. Spencer 2548ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref, 2554f223bf7STim Northover uint64_t &Val) const { 2564f223bf7STim Northover report_fatal_error("getSymbolValue unimplemented in COFFObjectFile"); 2574f223bf7STim Northover } 2584f223bf7STim Northover 2595e812afaSRafael Espindola void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { 2608ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2618ff24d25SRui Ueyama Sec += 1; 2628ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Sec); 2638e90adafSMichael J. Spencer } 2648e90adafSMichael J. Spencer 2658ff24d25SRui Ueyama error_code COFFObjectFile::getSectionName(DataRefImpl Ref, 2661d6167fdSMichael J. Spencer StringRef &Result) const { 2678ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2688ff24d25SRui Ueyama return getSectionName(Sec, Result); 2698e90adafSMichael J. Spencer } 2708e90adafSMichael J. Spencer 2718ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, 2721d6167fdSMichael J. Spencer uint64_t &Result) const { 2738ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2748ff24d25SRui Ueyama Result = Sec->VirtualAddress; 2751d6167fdSMichael J. Spencer return object_error::success; 2768e90adafSMichael J. Spencer } 2778e90adafSMichael J. Spencer 2788ff24d25SRui Ueyama error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, 2791d6167fdSMichael J. Spencer uint64_t &Result) const { 2808ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2818ff24d25SRui Ueyama Result = Sec->SizeOfRawData; 2821d6167fdSMichael J. Spencer return object_error::success; 2838e90adafSMichael J. Spencer } 2848e90adafSMichael J. Spencer 2858ff24d25SRui Ueyama error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, 2861d6167fdSMichael J. Spencer StringRef &Result) const { 2878ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2889da9e693SMichael J. Spencer ArrayRef<uint8_t> Res; 2898ff24d25SRui Ueyama error_code EC = getSectionContents(Sec, Res); 2909da9e693SMichael J. Spencer Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); 2919da9e693SMichael J. Spencer return EC; 2928e90adafSMichael J. Spencer } 2938e90adafSMichael J. Spencer 2948ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, 2957989460aSMichael J. Spencer uint64_t &Res) const { 2968ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2978ff24d25SRui Ueyama if (!Sec) 2987989460aSMichael J. Spencer return object_error::parse_failed; 2998ff24d25SRui Ueyama Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); 3007989460aSMichael J. Spencer return object_error::success; 3017989460aSMichael J. Spencer } 3027989460aSMichael J. Spencer 3038ff24d25SRui Ueyama error_code COFFObjectFile::isSectionText(DataRefImpl Ref, 3041d6167fdSMichael J. Spencer bool &Result) const { 3058ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3068ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 3071d6167fdSMichael J. Spencer return object_error::success; 3088e90adafSMichael J. Spencer } 3098e90adafSMichael J. Spencer 3108ff24d25SRui Ueyama error_code COFFObjectFile::isSectionData(DataRefImpl Ref, 311800619f2SMichael J. Spencer bool &Result) const { 3128ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3138ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 314800619f2SMichael J. Spencer return object_error::success; 315800619f2SMichael J. Spencer } 316800619f2SMichael J. Spencer 3178ff24d25SRui Ueyama error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, 318800619f2SMichael J. Spencer bool &Result) const { 3198ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3208ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 321800619f2SMichael J. Spencer return object_error::success; 322800619f2SMichael J. Spencer } 323800619f2SMichael J. Spencer 3248ff24d25SRui Ueyama error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref, 3252138ef6dSPreston Gurd bool &Result) const { 3262138ef6dSPreston Gurd // FIXME: Unimplemented 3272138ef6dSPreston Gurd Result = true; 3282138ef6dSPreston Gurd return object_error::success; 3292138ef6dSPreston Gurd } 3302138ef6dSPreston Gurd 3318ff24d25SRui Ueyama error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, 3322138ef6dSPreston Gurd bool &Result) const { 3338ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3348ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 3352138ef6dSPreston Gurd return object_error::success; 3362138ef6dSPreston Gurd } 3372138ef6dSPreston Gurd 3388ff24d25SRui Ueyama error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, 3392138ef6dSPreston Gurd bool &Result) const { 340b96a320aSAndrew Kaylor // FIXME: Unimplemented. 3412138ef6dSPreston Gurd Result = false; 3422138ef6dSPreston Gurd return object_error::success; 3432138ef6dSPreston Gurd } 3442138ef6dSPreston Gurd 3458ff24d25SRui Ueyama error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, 3463f31fa05SAndrew Kaylor bool &Result) const { 3473f31fa05SAndrew Kaylor // FIXME: Unimplemented. 3483f31fa05SAndrew Kaylor Result = false; 3493f31fa05SAndrew Kaylor return object_error::success; 3503f31fa05SAndrew Kaylor } 3513f31fa05SAndrew Kaylor 3528ff24d25SRui Ueyama error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, 3538ff24d25SRui Ueyama DataRefImpl SymbRef, 354f6f3e81cSBenjamin Kramer bool &Result) const { 3558ff24d25SRui Ueyama const coff_section *Sec = toSec(SecRef); 3568ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(SymbRef); 3578ff24d25SRui Ueyama const coff_section *SymbSec = 0; 3588ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC; 3598ff24d25SRui Ueyama if (SymbSec == Sec) 3609a28851eSMichael J. Spencer Result = true; 3619a28851eSMichael J. Spencer else 362f6f3e81cSBenjamin Kramer Result = false; 363f6f3e81cSBenjamin Kramer return object_error::success; 364f6f3e81cSBenjamin Kramer } 365f6f3e81cSBenjamin Kramer 3668ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 3678ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3688ff24d25SRui Ueyama DataRefImpl Ret; 3698ff24d25SRui Ueyama if (Sec->NumberOfRelocations == 0) 3708ff24d25SRui Ueyama Ret.p = 0; 371e5fd0047SMichael J. Spencer else 3728ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations); 373e5fd0047SMichael J. Spencer 3748ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 375e5fd0047SMichael J. Spencer } 376e5fd0047SMichael J. Spencer 3778ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 3788ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3798ff24d25SRui Ueyama DataRefImpl Ret; 3808ff24d25SRui Ueyama if (Sec->NumberOfRelocations == 0) 3818ff24d25SRui Ueyama Ret.p = 0; 382e5fd0047SMichael J. Spencer else 3838ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>( 384e5fd0047SMichael J. Spencer reinterpret_cast<const coff_relocation*>( 3858ff24d25SRui Ueyama base() + Sec->PointerToRelocations) 3868ff24d25SRui Ueyama + Sec->NumberOfRelocations); 387e5fd0047SMichael J. Spencer 3888ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 389e5fd0047SMichael J. Spencer } 390e5fd0047SMichael J. Spencer 391c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 392c2bed429SRui Ueyama error_code COFFObjectFile::initSymbolTablePtr() { 3938ff24d25SRui Ueyama if (error_code EC = getObject( 394c2bed429SRui Ueyama SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable, 395c2bed429SRui Ueyama COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 3968ff24d25SRui Ueyama return EC; 397c2bed429SRui Ueyama 398c2bed429SRui Ueyama // Find string table. The first four byte of the string table contains the 399c2bed429SRui Ueyama // total size of the string table, including the size field itself. If the 400c2bed429SRui Ueyama // string table is empty, the value of the first four byte would be 4. 401c2bed429SRui Ueyama const uint8_t *StringTableAddr = 402c2bed429SRui Ueyama base() + COFFHeader->PointerToSymbolTable + 403c2bed429SRui Ueyama COFFHeader->NumberOfSymbols * sizeof(coff_symbol); 404c2bed429SRui Ueyama const ulittle32_t *StringTableSizePtr; 4058ff24d25SRui Ueyama if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 4068ff24d25SRui Ueyama return EC; 407c2bed429SRui Ueyama StringTableSize = *StringTableSizePtr; 4088ff24d25SRui Ueyama if (error_code EC = 409c2bed429SRui Ueyama getObject(StringTable, Data, StringTableAddr, StringTableSize)) 4108ff24d25SRui Ueyama return EC; 411c2bed429SRui Ueyama 412c2bed429SRui Ueyama // Check that the string table is null terminated if has any in it. 413c2bed429SRui Ueyama if (StringTableSize < 4 || 414c2bed429SRui Ueyama (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) 415c2bed429SRui Ueyama return object_error::parse_failed; 416c2bed429SRui Ueyama return object_error::success; 417c2bed429SRui Ueyama } 418c2bed429SRui Ueyama 419215a586cSRui Ueyama // Returns the file offset for the given VA. 420b7a40081SRui Ueyama error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { 421b6eb264aSRui Ueyama uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase 422b6eb264aSRui Ueyama : (uint64_t)PE32PlusHeader->ImageBase; 423b7a40081SRui Ueyama uint64_t Rva = Addr - ImageBase; 424b7a40081SRui Ueyama assert(Rva <= UINT32_MAX); 425b7a40081SRui Ueyama return getRvaPtr((uint32_t)Rva, Res); 426215a586cSRui Ueyama } 427215a586cSRui Ueyama 428c2bed429SRui Ueyama // Returns the file offset for the given RVA. 429215a586cSRui Ueyama error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { 430b5155a57SRafael Espindola for (section_iterator I = section_begin(), E = section_end(); I != E; 4315e812afaSRafael Espindola ++I) { 4328ff24d25SRui Ueyama const coff_section *Section = getCOFFSection(I); 433c2bed429SRui Ueyama uint32_t SectionStart = Section->VirtualAddress; 434c2bed429SRui Ueyama uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 435215a586cSRui Ueyama if (SectionStart <= Addr && Addr < SectionEnd) { 436215a586cSRui Ueyama uint32_t Offset = Addr - SectionStart; 437c2bed429SRui Ueyama Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 438c2bed429SRui Ueyama return object_error::success; 439c2bed429SRui Ueyama } 440c2bed429SRui Ueyama } 441c2bed429SRui Ueyama return object_error::parse_failed; 442c2bed429SRui Ueyama } 443c2bed429SRui Ueyama 444c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 445c2bed429SRui Ueyama // table entry. 446c2bed429SRui Ueyama error_code COFFObjectFile:: 447c2bed429SRui Ueyama getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const { 448c2bed429SRui Ueyama uintptr_t IntPtr = 0; 4498ff24d25SRui Ueyama if (error_code EC = getRvaPtr(Rva, IntPtr)) 4508ff24d25SRui Ueyama return EC; 451c2bed429SRui Ueyama const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 452c2bed429SRui Ueyama Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 453c2bed429SRui Ueyama Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 454c2bed429SRui Ueyama return object_error::success; 455c2bed429SRui Ueyama } 456c2bed429SRui Ueyama 457c2bed429SRui Ueyama // Find the import table. 458c2bed429SRui Ueyama error_code COFFObjectFile::initImportTablePtr() { 459c2bed429SRui Ueyama // First, we get the RVA of the import table. If the file lacks a pointer to 460c2bed429SRui Ueyama // the import table, do nothing. 461c2bed429SRui Ueyama const data_directory *DataEntry; 462c2bed429SRui Ueyama if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 463c2bed429SRui Ueyama return object_error::success; 464c2bed429SRui Ueyama 465c2bed429SRui Ueyama // Do nothing if the pointer to import table is NULL. 466c2bed429SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 467c2bed429SRui Ueyama return object_error::success; 468c2bed429SRui Ueyama 469c2bed429SRui Ueyama uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 470c2bed429SRui Ueyama NumberOfImportDirectory = DataEntry->Size / 471c2bed429SRui Ueyama sizeof(import_directory_table_entry); 472c2bed429SRui Ueyama 473c2bed429SRui Ueyama // Find the section that contains the RVA. This is needed because the RVA is 474c2bed429SRui Ueyama // the import table's memory address which is different from its file offset. 475c2bed429SRui Ueyama uintptr_t IntPtr = 0; 4768ff24d25SRui Ueyama if (error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 4778ff24d25SRui Ueyama return EC; 478c2bed429SRui Ueyama ImportDirectory = reinterpret_cast< 479c2bed429SRui Ueyama const import_directory_table_entry *>(IntPtr); 480ad882ba8SRui Ueyama return object_error::success; 481ad882ba8SRui Ueyama } 482c2bed429SRui Ueyama 483ad882ba8SRui Ueyama // Find the export table. 484ad882ba8SRui Ueyama error_code COFFObjectFile::initExportTablePtr() { 485ad882ba8SRui Ueyama // First, we get the RVA of the export table. If the file lacks a pointer to 486ad882ba8SRui Ueyama // the export table, do nothing. 487ad882ba8SRui Ueyama const data_directory *DataEntry; 488ad882ba8SRui Ueyama if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 489ad882ba8SRui Ueyama return object_error::success; 490ad882ba8SRui Ueyama 491ad882ba8SRui Ueyama // Do nothing if the pointer to export table is NULL. 492ad882ba8SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 493ad882ba8SRui Ueyama return object_error::success; 494ad882ba8SRui Ueyama 495ad882ba8SRui Ueyama uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 496ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 497ad882ba8SRui Ueyama if (error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 498ad882ba8SRui Ueyama return EC; 49924fc2d64SRui Ueyama ExportDirectory = 50024fc2d64SRui Ueyama reinterpret_cast<const export_directory_table_entry *>(IntPtr); 501ad882ba8SRui Ueyama return object_error::success; 502c2bed429SRui Ueyama } 503c2bed429SRui Ueyama 504afcc3df7SRafael Espindola COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC, 505afcc3df7SRafael Espindola bool BufferOwned) 506afcc3df7SRafael Espindola : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0), 50710ed9ddcSRui Ueyama PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0), 50810ed9ddcSRui Ueyama SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0), 509afcc3df7SRafael Espindola NumberOfImportDirectory(0), ExportDirectory(0) { 5101d6167fdSMichael J. Spencer // Check that we at least have enough room for a header. 5118ff24d25SRui Ueyama if (!checkSize(Data, EC, sizeof(coff_file_header))) return; 512ee066fc4SEric Christopher 51382ebd8e3SRui Ueyama // The current location in the file where we are looking at. 51482ebd8e3SRui Ueyama uint64_t CurPtr = 0; 51582ebd8e3SRui Ueyama 51682ebd8e3SRui Ueyama // PE header is optional and is present only in executables. If it exists, 51782ebd8e3SRui Ueyama // it is placed right after COFF header. 5188ff24d25SRui Ueyama bool HasPEHeader = false; 519ee066fc4SEric Christopher 5201d6167fdSMichael J. Spencer // Check if this is a PE/COFF file. 521ec29b121SMichael J. Spencer if (base()[0] == 0x4d && base()[1] == 0x5a) { 522ee066fc4SEric Christopher // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 523ee066fc4SEric Christopher // PE signature to find 'normal' COFF header. 5248ff24d25SRui Ueyama if (!checkSize(Data, EC, 0x3c + 8)) return; 52582ebd8e3SRui Ueyama CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); 52682ebd8e3SRui Ueyama // Check the PE magic bytes. ("PE\0\0") 52782ebd8e3SRui Ueyama if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) { 5288ff24d25SRui Ueyama EC = object_error::parse_failed; 5291d6167fdSMichael J. Spencer return; 5301d6167fdSMichael J. Spencer } 53182ebd8e3SRui Ueyama CurPtr += 4; // Skip the PE magic bytes. 5328ff24d25SRui Ueyama HasPEHeader = true; 533ee066fc4SEric Christopher } 534ee066fc4SEric Christopher 5358ff24d25SRui Ueyama if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 5361d6167fdSMichael J. Spencer return; 53782ebd8e3SRui Ueyama CurPtr += sizeof(coff_file_header); 53882ebd8e3SRui Ueyama 5398ff24d25SRui Ueyama if (HasPEHeader) { 54010ed9ddcSRui Ueyama const pe32_header *Header; 54110ed9ddcSRui Ueyama if ((EC = getObject(Header, Data, base() + CurPtr))) 54282ebd8e3SRui Ueyama return; 54310ed9ddcSRui Ueyama 54410ed9ddcSRui Ueyama const uint8_t *DataDirAddr; 54510ed9ddcSRui Ueyama uint64_t DataDirSize; 54610ed9ddcSRui Ueyama if (Header->Magic == 0x10b) { 54710ed9ddcSRui Ueyama PE32Header = Header; 54810ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32_header); 54910ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 55010ed9ddcSRui Ueyama } else if (Header->Magic == 0x20b) { 55110ed9ddcSRui Ueyama PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); 55210ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); 55310ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; 55410ed9ddcSRui Ueyama } else { 55510ed9ddcSRui Ueyama // It's neither PE32 nor PE32+. 55610ed9ddcSRui Ueyama EC = object_error::parse_failed; 557ed64342bSRui Ueyama return; 558ed64342bSRui Ueyama } 55910ed9ddcSRui Ueyama if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) 56010ed9ddcSRui Ueyama return; 56182ebd8e3SRui Ueyama CurPtr += COFFHeader->SizeOfOptionalHeader; 56282ebd8e3SRui Ueyama } 5631d6167fdSMichael J. Spencer 564692410efSRafael Espindola if (COFFHeader->isImportLibrary()) 565692410efSRafael Espindola return; 566692410efSRafael Espindola 5678ff24d25SRui Ueyama if ((EC = getObject(SectionTable, Data, base() + CurPtr, 568ed64342bSRui Ueyama COFFHeader->NumberOfSections * sizeof(coff_section)))) 5691d6167fdSMichael J. Spencer return; 5701d6167fdSMichael J. Spencer 571c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 572c2bed429SRui Ueyama if (COFFHeader->PointerToSymbolTable != 0) 5738ff24d25SRui Ueyama if ((EC = initSymbolTablePtr())) 5741d6167fdSMichael J. Spencer return; 5758e90adafSMichael J. Spencer 576c2bed429SRui Ueyama // Initialize the pointer to the beginning of the import table. 5778ff24d25SRui Ueyama if ((EC = initImportTablePtr())) 578ed64342bSRui Ueyama return; 5791d6167fdSMichael J. Spencer 580ad882ba8SRui Ueyama // Initialize the pointer to the export table. 5818ff24d25SRui Ueyama if ((EC = initExportTablePtr())) 582ad882ba8SRui Ueyama return; 583ad882ba8SRui Ueyama 5848ff24d25SRui Ueyama EC = object_error::success; 5858e90adafSMichael J. Spencer } 5868e90adafSMichael J. Spencer 587f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const { 5888ff24d25SRui Ueyama DataRefImpl Ret; 5898ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SymbolTable); 590f12b8282SRafael Espindola return basic_symbol_iterator(SymbolRef(Ret, this)); 5918e90adafSMichael J. Spencer } 5928e90adafSMichael J. Spencer 593f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_end_impl() const { 5948e90adafSMichael J. Spencer // The symbol table ends where the string table begins. 5958ff24d25SRui Ueyama DataRefImpl Ret; 5968ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(StringTable); 597f12b8282SRafael Espindola return basic_symbol_iterator(SymbolRef(Ret, this)); 5988e90adafSMichael J. Spencer } 5998e90adafSMichael J. Spencer 600b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_begin() const { 6012fc34c5fSDavid Meyer // TODO: implement 6022fc34c5fSDavid Meyer report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 6032fc34c5fSDavid Meyer } 6042fc34c5fSDavid Meyer 605b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_end() const { 6062fc34c5fSDavid Meyer // TODO: implement 6072fc34c5fSDavid Meyer report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 6082fc34c5fSDavid Meyer } 6092fc34c5fSDavid Meyer 610c429b80dSDavid Meyer StringRef COFFObjectFile::getLoadName() const { 611c429b80dSDavid Meyer // COFF does not have this field. 612c429b80dSDavid Meyer return ""; 613c429b80dSDavid Meyer } 614c429b80dSDavid Meyer 615bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_begin() const { 616a045b73aSRui Ueyama return import_directory_iterator( 617a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, 0, this)); 618c2bed429SRui Ueyama } 619c2bed429SRui Ueyama 620bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_end() const { 621a045b73aSRui Ueyama return import_directory_iterator( 622a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); 623c2bed429SRui Ueyama } 624c429b80dSDavid Meyer 625ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_begin() const { 626ad882ba8SRui Ueyama return export_directory_iterator( 627ad882ba8SRui Ueyama ExportDirectoryEntryRef(ExportDirectory, 0, this)); 628ad882ba8SRui Ueyama } 629ad882ba8SRui Ueyama 630ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_end() const { 631ad882ba8SRui Ueyama if (ExportDirectory == 0) 632ad882ba8SRui Ueyama return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this)); 6338ff24d25SRui Ueyama ExportDirectoryEntryRef Ref(ExportDirectory, 634ad882ba8SRui Ueyama ExportDirectory->AddressTableEntries, this); 6358ff24d25SRui Ueyama return export_directory_iterator(Ref); 636ad882ba8SRui Ueyama } 637ad882ba8SRui Ueyama 638b5155a57SRafael Espindola section_iterator COFFObjectFile::section_begin() const { 6398ff24d25SRui Ueyama DataRefImpl Ret; 6408ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 6418ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 6428e90adafSMichael J. Spencer } 6438e90adafSMichael J. Spencer 644b5155a57SRafael Espindola section_iterator COFFObjectFile::section_end() const { 6458ff24d25SRui Ueyama DataRefImpl Ret; 6468ff24d25SRui Ueyama int NumSections = COFFHeader->isImportLibrary() 64715ba1e20SRui Ueyama ? 0 : COFFHeader->NumberOfSections; 6488ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 6498ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 6508e90adafSMichael J. Spencer } 6518e90adafSMichael J. Spencer 6528e90adafSMichael J. Spencer uint8_t COFFObjectFile::getBytesInAddress() const { 6530324b672SMichael J. Spencer return getArch() == Triple::x86_64 ? 8 : 4; 6548e90adafSMichael J. Spencer } 6558e90adafSMichael J. Spencer 6568e90adafSMichael J. Spencer StringRef COFFObjectFile::getFileFormatName() const { 65782ebd8e3SRui Ueyama switch(COFFHeader->Machine) { 6588e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 6598e90adafSMichael J. Spencer return "COFF-i386"; 6608e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 6618e90adafSMichael J. Spencer return "COFF-x86-64"; 6628e90adafSMichael J. Spencer default: 6638e90adafSMichael J. Spencer return "COFF-<unknown arch>"; 6648e90adafSMichael J. Spencer } 6658e90adafSMichael J. Spencer } 6668e90adafSMichael J. Spencer 6678e90adafSMichael J. Spencer unsigned COFFObjectFile::getArch() const { 66882ebd8e3SRui Ueyama switch(COFFHeader->Machine) { 6698e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 6708e90adafSMichael J. Spencer return Triple::x86; 6718e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 6728e90adafSMichael J. Spencer return Triple::x86_64; 6738e90adafSMichael J. Spencer default: 6748e90adafSMichael J. Spencer return Triple::UnknownArch; 6758e90adafSMichael J. Spencer } 6768e90adafSMichael J. Spencer } 6778e90adafSMichael J. Spencer 67882ebd8e3SRui Ueyama // This method is kept here because lld uses this. As soon as we make 67982ebd8e3SRui Ueyama // lld to use getCOFFHeader, this method will be removed. 68089a7a5eaSMichael J. Spencer error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const { 68182ebd8e3SRui Ueyama return getCOFFHeader(Res); 68282ebd8e3SRui Ueyama } 68382ebd8e3SRui Ueyama 68482ebd8e3SRui Ueyama error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const { 68582ebd8e3SRui Ueyama Res = COFFHeader; 68682ebd8e3SRui Ueyama return object_error::success; 68782ebd8e3SRui Ueyama } 68882ebd8e3SRui Ueyama 68982ebd8e3SRui Ueyama error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 69082ebd8e3SRui Ueyama Res = PE32Header; 69189a7a5eaSMichael J. Spencer return object_error::success; 69289a7a5eaSMichael J. Spencer } 69389a7a5eaSMichael J. Spencer 69410ed9ddcSRui Ueyama error_code 69510ed9ddcSRui Ueyama COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { 69610ed9ddcSRui Ueyama Res = PE32PlusHeader; 69710ed9ddcSRui Ueyama return object_error::success; 69810ed9ddcSRui Ueyama } 69910ed9ddcSRui Ueyama 7008ff24d25SRui Ueyama error_code COFFObjectFile::getDataDirectory(uint32_t Index, 701ed64342bSRui Ueyama const data_directory *&Res) const { 702ed64342bSRui Ueyama // Error if if there's no data directory or the index is out of range. 70310ed9ddcSRui Ueyama if (!DataDirectory) 70410ed9ddcSRui Ueyama return object_error::parse_failed; 70510ed9ddcSRui Ueyama assert(PE32Header || PE32PlusHeader); 70610ed9ddcSRui Ueyama uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize 70710ed9ddcSRui Ueyama : PE32PlusHeader->NumberOfRvaAndSize; 70810ed9ddcSRui Ueyama if (Index > NumEnt) 709ed64342bSRui Ueyama return object_error::parse_failed; 7108ff24d25SRui Ueyama Res = &DataDirectory[Index]; 711ed64342bSRui Ueyama return object_error::success; 712ed64342bSRui Ueyama } 713ed64342bSRui Ueyama 7148ff24d25SRui Ueyama error_code COFFObjectFile::getSection(int32_t Index, 7151d6167fdSMichael J. Spencer const coff_section *&Result) const { 7161d6167fdSMichael J. Spencer // Check for special index values. 7178ff24d25SRui Ueyama if (Index == COFF::IMAGE_SYM_UNDEFINED || 7188ff24d25SRui Ueyama Index == COFF::IMAGE_SYM_ABSOLUTE || 7198ff24d25SRui Ueyama Index == COFF::IMAGE_SYM_DEBUG) 7201d6167fdSMichael J. Spencer Result = NULL; 7218ff24d25SRui Ueyama else if (Index > 0 && Index <= COFFHeader->NumberOfSections) 7221d6167fdSMichael J. Spencer // We already verified the section table data, so no need to check again. 7238ff24d25SRui Ueyama Result = SectionTable + (Index - 1); 7241d6167fdSMichael J. Spencer else 7251d6167fdSMichael J. Spencer return object_error::parse_failed; 7261d6167fdSMichael J. Spencer return object_error::success; 7278e90adafSMichael J. Spencer } 7288e90adafSMichael J. Spencer 7298ff24d25SRui Ueyama error_code COFFObjectFile::getString(uint32_t Offset, 7301d6167fdSMichael J. Spencer StringRef &Result) const { 7311d6167fdSMichael J. Spencer if (StringTableSize <= 4) 7321d6167fdSMichael J. Spencer // Tried to get a string from an empty string table. 7331d6167fdSMichael J. Spencer return object_error::parse_failed; 7348ff24d25SRui Ueyama if (Offset >= StringTableSize) 7351d6167fdSMichael J. Spencer return object_error::unexpected_eof; 7368ff24d25SRui Ueyama Result = StringRef(StringTable + Offset); 7371d6167fdSMichael J. Spencer return object_error::success; 7388e90adafSMichael J. Spencer } 739022ecdf2SBenjamin Kramer 7408ff24d25SRui Ueyama error_code COFFObjectFile::getSymbol(uint32_t Index, 741e5fd0047SMichael J. Spencer const coff_symbol *&Result) const { 7428ff24d25SRui Ueyama if (Index < COFFHeader->NumberOfSymbols) 7438ff24d25SRui Ueyama Result = SymbolTable + Index; 744e5fd0047SMichael J. Spencer else 745e5fd0047SMichael J. Spencer return object_error::parse_failed; 746e5fd0047SMichael J. Spencer return object_error::success; 747e5fd0047SMichael J. Spencer } 748e5fd0047SMichael J. Spencer 7498ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol, 75089a7a5eaSMichael J. Spencer StringRef &Res) const { 75189a7a5eaSMichael J. Spencer // Check for string table entry. First 4 bytes are 0. 7528ff24d25SRui Ueyama if (Symbol->Name.Offset.Zeroes == 0) { 7538ff24d25SRui Ueyama uint32_t Offset = Symbol->Name.Offset.Offset; 7548ff24d25SRui Ueyama if (error_code EC = getString(Offset, Res)) 7558ff24d25SRui Ueyama return EC; 75689a7a5eaSMichael J. Spencer return object_error::success; 75789a7a5eaSMichael J. Spencer } 75889a7a5eaSMichael J. Spencer 7598ff24d25SRui Ueyama if (Symbol->Name.ShortName[7] == 0) 76089a7a5eaSMichael J. Spencer // Null terminated, let ::strlen figure out the length. 7618ff24d25SRui Ueyama Res = StringRef(Symbol->Name.ShortName); 76289a7a5eaSMichael J. Spencer else 76389a7a5eaSMichael J. Spencer // Not null terminated, use all 8 bytes. 7648ff24d25SRui Ueyama Res = StringRef(Symbol->Name.ShortName, 8); 76589a7a5eaSMichael J. Spencer return object_error::success; 76689a7a5eaSMichael J. Spencer } 76789a7a5eaSMichael J. Spencer 76871757ef3SMarshall Clow ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData( 7698ff24d25SRui Ueyama const coff_symbol *Symbol) const { 7708ff24d25SRui Ueyama const uint8_t *Aux = NULL; 77171757ef3SMarshall Clow 7728ff24d25SRui Ueyama if (Symbol->NumberOfAuxSymbols > 0) { 77371757ef3SMarshall Clow // AUX data comes immediately after the symbol in COFF 7748ff24d25SRui Ueyama Aux = reinterpret_cast<const uint8_t *>(Symbol + 1); 77571757ef3SMarshall Clow # ifndef NDEBUG 7768ff24d25SRui Ueyama // Verify that the Aux symbol points to a valid entry in the symbol table. 7778ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 7788ff24d25SRui Ueyama if (Offset < COFFHeader->PointerToSymbolTable 7798ff24d25SRui Ueyama || Offset >= COFFHeader->PointerToSymbolTable 78082ebd8e3SRui Ueyama + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 78171757ef3SMarshall Clow report_fatal_error("Aux Symbol data was outside of symbol table."); 78271757ef3SMarshall Clow 7838ff24d25SRui Ueyama assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 78471757ef3SMarshall Clow == 0 && "Aux Symbol data did not point to the beginning of a symbol"); 78571757ef3SMarshall Clow # endif 786bfb85e67SMarshall Clow } 78724fc2d64SRui Ueyama return ArrayRef<uint8_t>(Aux, 78824fc2d64SRui Ueyama Symbol->NumberOfAuxSymbols * sizeof(coff_symbol)); 78971757ef3SMarshall Clow } 79071757ef3SMarshall Clow 79153c2d547SMichael J. Spencer error_code COFFObjectFile::getSectionName(const coff_section *Sec, 79253c2d547SMichael J. Spencer StringRef &Res) const { 79353c2d547SMichael J. Spencer StringRef Name; 79453c2d547SMichael J. Spencer if (Sec->Name[7] == 0) 79553c2d547SMichael J. Spencer // Null terminated, let ::strlen figure out the length. 79653c2d547SMichael J. Spencer Name = Sec->Name; 79753c2d547SMichael J. Spencer else 79853c2d547SMichael J. Spencer // Not null terminated, use all 8 bytes. 79953c2d547SMichael J. Spencer Name = StringRef(Sec->Name, 8); 80053c2d547SMichael J. Spencer 80153c2d547SMichael J. Spencer // Check for string table entry. First byte is '/'. 80253c2d547SMichael J. Spencer if (Name[0] == '/') { 80353c2d547SMichael J. Spencer uint32_t Offset; 804*9d2c15efSNico Rieck if (Name[1] == '/') { 805*9d2c15efSNico Rieck if (decodeBase64StringEntry(Name.substr(2), Offset)) 806*9d2c15efSNico Rieck return object_error::parse_failed; 807*9d2c15efSNico Rieck } else { 80853c2d547SMichael J. Spencer if (Name.substr(1).getAsInteger(10, Offset)) 80953c2d547SMichael J. Spencer return object_error::parse_failed; 810*9d2c15efSNico Rieck } 8118ff24d25SRui Ueyama if (error_code EC = getString(Offset, Name)) 8128ff24d25SRui Ueyama return EC; 81353c2d547SMichael J. Spencer } 81453c2d547SMichael J. Spencer 81553c2d547SMichael J. Spencer Res = Name; 81653c2d547SMichael J. Spencer return object_error::success; 81753c2d547SMichael J. Spencer } 81853c2d547SMichael J. Spencer 8199da9e693SMichael J. Spencer error_code COFFObjectFile::getSectionContents(const coff_section *Sec, 8209da9e693SMichael J. Spencer ArrayRef<uint8_t> &Res) const { 8219da9e693SMichael J. Spencer // The only thing that we need to verify is that the contents is contained 8229da9e693SMichael J. Spencer // within the file bounds. We don't need to make sure it doesn't cover other 8239da9e693SMichael J. Spencer // data, as there's nothing that says that is not allowed. 8249da9e693SMichael J. Spencer uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 8259da9e693SMichael J. Spencer uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; 8269da9e693SMichael J. Spencer if (ConEnd > uintptr_t(Data->getBufferEnd())) 8279da9e693SMichael J. Spencer return object_error::parse_failed; 8289da9e693SMichael J. Spencer Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart), 8299da9e693SMichael J. Spencer Sec->SizeOfRawData); 8309da9e693SMichael J. Spencer return object_error::success; 8319da9e693SMichael J. Spencer } 8329da9e693SMichael J. Spencer 833022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 834e5fd0047SMichael J. Spencer return reinterpret_cast<const coff_relocation*>(Rel.p); 835022ecdf2SBenjamin Kramer } 8368ff24d25SRui Ueyama 8375e812afaSRafael Espindola void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 838e5fd0047SMichael J. Spencer Rel.p = reinterpret_cast<uintptr_t>( 839e5fd0047SMichael J. Spencer reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 840022ecdf2SBenjamin Kramer } 8418ff24d25SRui Ueyama 842022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, 843022ecdf2SBenjamin Kramer uint64_t &Res) const { 8441e483879SRafael Espindola report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); 845022ecdf2SBenjamin Kramer } 8468ff24d25SRui Ueyama 847cbe72fc9SDanil Malyshev error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, 848cbe72fc9SDanil Malyshev uint64_t &Res) const { 849cbe72fc9SDanil Malyshev Res = toRel(Rel)->VirtualAddress; 850cbe72fc9SDanil Malyshev return object_error::success; 851cbe72fc9SDanil Malyshev } 8528ff24d25SRui Ueyama 853806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 854022ecdf2SBenjamin Kramer const coff_relocation* R = toRel(Rel); 8558ff24d25SRui Ueyama DataRefImpl Ref; 8568ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); 8578ff24d25SRui Ueyama return symbol_iterator(SymbolRef(Ref, this)); 858022ecdf2SBenjamin Kramer } 8598ff24d25SRui Ueyama 860022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, 8617be76590SOwen Anderson uint64_t &Res) const { 862022ecdf2SBenjamin Kramer const coff_relocation* R = toRel(Rel); 863022ecdf2SBenjamin Kramer Res = R->Type; 864022ecdf2SBenjamin Kramer return object_error::success; 865022ecdf2SBenjamin Kramer } 866e5fd0047SMichael J. Spencer 86771757ef3SMarshall Clow const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const { 86871757ef3SMarshall Clow return toSec(It->getRawDataRefImpl()); 86971757ef3SMarshall Clow } 87071757ef3SMarshall Clow 87171757ef3SMarshall Clow const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const { 87271757ef3SMarshall Clow return toSymb(It->getRawDataRefImpl()); 87371757ef3SMarshall Clow } 87471757ef3SMarshall Clow 875f12b8282SRafael Espindola const coff_relocation * 876f12b8282SRafael Espindola COFFObjectFile::getCOFFRelocation(relocation_iterator &It) const { 877d3e2a76cSMarshall Clow return toRel(It->getRawDataRefImpl()); 878d3e2a76cSMarshall Clow } 879d3e2a76cSMarshall Clow 880e5fd0047SMichael J. Spencer #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \ 8818ff24d25SRui Ueyama case COFF::enum: Res = #enum; break; 882e5fd0047SMichael J. Spencer 883e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, 884e5fd0047SMichael J. Spencer SmallVectorImpl<char> &Result) const { 8858ff24d25SRui Ueyama const coff_relocation *Reloc = toRel(Rel); 8868ff24d25SRui Ueyama StringRef Res; 88782ebd8e3SRui Ueyama switch (COFFHeader->Machine) { 888e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 8898ff24d25SRui Ueyama switch (Reloc->Type) { 890e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 891e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 892e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 893e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 894e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 895e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 896e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 897e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 898e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 899e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 900e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 901e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 902e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 903e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 904e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 905e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 906e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 907e5fd0047SMichael J. Spencer default: 9088ff24d25SRui Ueyama Res = "Unknown"; 909e5fd0047SMichael J. Spencer } 910e5fd0047SMichael J. Spencer break; 911e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 9128ff24d25SRui Ueyama switch (Reloc->Type) { 913e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 914e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 915e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 916e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 917e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 918e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 919e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 920e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 921e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 922e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 923e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 924e5fd0047SMichael J. Spencer default: 9258ff24d25SRui Ueyama Res = "Unknown"; 926e5fd0047SMichael J. Spencer } 927e5fd0047SMichael J. Spencer break; 928e5fd0047SMichael J. Spencer default: 9298ff24d25SRui Ueyama Res = "Unknown"; 930e5fd0047SMichael J. Spencer } 9318ff24d25SRui Ueyama Result.append(Res.begin(), Res.end()); 932e5fd0047SMichael J. Spencer return object_error::success; 933e5fd0047SMichael J. Spencer } 934e5fd0047SMichael J. Spencer 935e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 936e5fd0047SMichael J. Spencer 937e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel, 938e5fd0047SMichael J. Spencer SmallVectorImpl<char> &Result) const { 9398ff24d25SRui Ueyama const coff_relocation *Reloc = toRel(Rel); 9408ff24d25SRui Ueyama const coff_symbol *Symb = 0; 9418ff24d25SRui Ueyama if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC; 9428ff24d25SRui Ueyama DataRefImpl Sym; 9438ff24d25SRui Ueyama Sym.p = reinterpret_cast<uintptr_t>(Symb); 9448ff24d25SRui Ueyama StringRef SymName; 9458ff24d25SRui Ueyama if (error_code EC = getSymbolName(Sym, SymName)) return EC; 9468ff24d25SRui Ueyama Result.append(SymName.begin(), SymName.end()); 947e5fd0047SMichael J. Spencer return object_error::success; 948022ecdf2SBenjamin Kramer } 9498e90adafSMichael J. Spencer 9502fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData, 9512fc34c5fSDavid Meyer LibraryRef &Result) const { 9522fc34c5fSDavid Meyer report_fatal_error("getLibraryNext not implemented in COFFObjectFile"); 9532fc34c5fSDavid Meyer } 9542fc34c5fSDavid Meyer 9552fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData, 9562fc34c5fSDavid Meyer StringRef &Result) const { 9572fc34c5fSDavid Meyer report_fatal_error("getLibraryPath not implemented in COFFObjectFile"); 9582fc34c5fSDavid Meyer } 9592fc34c5fSDavid Meyer 960c2bed429SRui Ueyama bool ImportDirectoryEntryRef:: 961c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const { 962a045b73aSRui Ueyama return ImportTable == Other.ImportTable && Index == Other.Index; 963c2bed429SRui Ueyama } 964c2bed429SRui Ueyama 9655e812afaSRafael Espindola void ImportDirectoryEntryRef::moveNext() { 9665e812afaSRafael Espindola ++Index; 967c2bed429SRui Ueyama } 968c2bed429SRui Ueyama 969c2bed429SRui Ueyama error_code ImportDirectoryEntryRef:: 970c2bed429SRui Ueyama getImportTableEntry(const import_directory_table_entry *&Result) const { 971a045b73aSRui Ueyama Result = ImportTable; 972c2bed429SRui Ueyama return object_error::success; 973c2bed429SRui Ueyama } 974c2bed429SRui Ueyama 975c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 976c2bed429SRui Ueyama uintptr_t IntPtr = 0; 977a045b73aSRui Ueyama if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr)) 978a045b73aSRui Ueyama return EC; 979a045b73aSRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 980c2bed429SRui Ueyama return object_error::success; 981c2bed429SRui Ueyama } 982c2bed429SRui Ueyama 983c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getImportLookupEntry( 984c2bed429SRui Ueyama const import_lookup_table_entry32 *&Result) const { 985c2bed429SRui Ueyama uintptr_t IntPtr = 0; 986a045b73aSRui Ueyama if (error_code EC = 987a045b73aSRui Ueyama OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr)) 988a045b73aSRui Ueyama return EC; 989c2bed429SRui Ueyama Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); 990c2bed429SRui Ueyama return object_error::success; 991c2bed429SRui Ueyama } 992c2bed429SRui Ueyama 993ad882ba8SRui Ueyama bool ExportDirectoryEntryRef:: 994ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const { 995ad882ba8SRui Ueyama return ExportTable == Other.ExportTable && Index == Other.Index; 996ad882ba8SRui Ueyama } 997ad882ba8SRui Ueyama 9985e812afaSRafael Espindola void ExportDirectoryEntryRef::moveNext() { 9995e812afaSRafael Espindola ++Index; 1000ad882ba8SRui Ueyama } 1001ad882ba8SRui Ueyama 1002da49d0d4SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 1003da49d0d4SRui Ueyama // by ordinal, the empty string is set as a result. 1004da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { 1005da49d0d4SRui Ueyama uintptr_t IntPtr = 0; 1006da49d0d4SRui Ueyama if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) 1007da49d0d4SRui Ueyama return EC; 1008da49d0d4SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1009da49d0d4SRui Ueyama return object_error::success; 1010da49d0d4SRui Ueyama } 1011da49d0d4SRui Ueyama 1012e5df6095SRui Ueyama // Returns the starting ordinal number. 1013e5df6095SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { 1014e5df6095SRui Ueyama Result = ExportTable->OrdinalBase; 1015e5df6095SRui Ueyama return object_error::success; 1016e5df6095SRui Ueyama } 1017e5df6095SRui Ueyama 1018ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol. 1019ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 1020ad882ba8SRui Ueyama Result = ExportTable->OrdinalBase + Index; 1021ad882ba8SRui Ueyama return object_error::success; 1022ad882ba8SRui Ueyama } 1023ad882ba8SRui Ueyama 1024ad882ba8SRui Ueyama // Returns the address of the current export symbol. 1025ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 1026ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 1027ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr( 1028ad882ba8SRui Ueyama ExportTable->ExportAddressTableRVA, IntPtr)) 1029ad882ba8SRui Ueyama return EC; 103024fc2d64SRui Ueyama const export_address_table_entry *entry = 103124fc2d64SRui Ueyama reinterpret_cast<const export_address_table_entry *>(IntPtr); 1032ad882ba8SRui Ueyama Result = entry[Index].ExportRVA; 1033ad882ba8SRui Ueyama return object_error::success; 1034ad882ba8SRui Ueyama } 1035ad882ba8SRui Ueyama 1036ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 1037ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result. 1038da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { 1039ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 1040ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr( 1041ad882ba8SRui Ueyama ExportTable->OrdinalTableRVA, IntPtr)) 1042ad882ba8SRui Ueyama return EC; 1043ad882ba8SRui Ueyama const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 1044ad882ba8SRui Ueyama 1045ad882ba8SRui Ueyama uint32_t NumEntries = ExportTable->NumberOfNamePointers; 1046ad882ba8SRui Ueyama int Offset = 0; 1047ad882ba8SRui Ueyama for (const ulittle16_t *I = Start, *E = Start + NumEntries; 1048ad882ba8SRui Ueyama I < E; ++I, ++Offset) { 1049ad882ba8SRui Ueyama if (*I != Index) 1050ad882ba8SRui Ueyama continue; 1051ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr( 1052ad882ba8SRui Ueyama ExportTable->NamePointerRVA, IntPtr)) 1053ad882ba8SRui Ueyama return EC; 1054ad882ba8SRui Ueyama const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 1055ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 1056ad882ba8SRui Ueyama return EC; 1057ad882ba8SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1058ad882ba8SRui Ueyama return object_error::success; 1059ad882ba8SRui Ueyama } 1060ad882ba8SRui Ueyama Result = ""; 1061ad882ba8SRui Ueyama return object_error::success; 1062ad882ba8SRui Ueyama } 1063ad882ba8SRui Ueyama 1064afcc3df7SRafael Espindola ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object, 1065afcc3df7SRafael Espindola bool BufferOwned) { 10668ff24d25SRui Ueyama error_code EC; 1067afcc3df7SRafael Espindola OwningPtr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC, BufferOwned)); 1068692410efSRafael Espindola if (EC) 1069692410efSRafael Espindola return EC; 1070692410efSRafael Espindola return Ret.take(); 1071686738e2SRui Ueyama } 1072