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> 228e90adafSMichael J. Spencer 238e90adafSMichael J. Spencer using namespace llvm; 248e90adafSMichael J. Spencer using namespace object; 258e90adafSMichael J. Spencer 268e90adafSMichael J. Spencer using support::ulittle8_t; 278e90adafSMichael J. Spencer using support::ulittle16_t; 288e90adafSMichael J. Spencer using support::ulittle32_t; 298e90adafSMichael J. Spencer using support::little16_t; 308e90adafSMichael J. Spencer 311d6167fdSMichael J. Spencer // Returns false if size is greater than the buffer size. And sets ec. 32686738e2SRui Ueyama static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) { 338ff24d25SRui Ueyama if (M->getBufferSize() < Size) { 348ff24d25SRui Ueyama EC = object_error::unexpected_eof; 351d6167fdSMichael J. Spencer return false; 361d6167fdSMichael J. Spencer } 371d6167fdSMichael J. Spencer return true; 388e90adafSMichael J. Spencer } 398e90adafSMichael J. Spencer 40ed64342bSRui Ueyama // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. 41ed64342bSRui Ueyama // Returns unexpected_eof if error. 42ed64342bSRui Ueyama template<typename T> 43686738e2SRui Ueyama static error_code getObject(const T *&Obj, const MemoryBuffer *M, 44686738e2SRui Ueyama const uint8_t *Ptr, const size_t Size = sizeof(T)) { 45ed64342bSRui Ueyama uintptr_t Addr = uintptr_t(Ptr); 46ed64342bSRui Ueyama if (Addr + Size < Addr || 47ed64342bSRui Ueyama Addr + Size < Size || 48ed64342bSRui Ueyama Addr + Size > uintptr_t(M->getBufferEnd())) { 49ed64342bSRui Ueyama return object_error::unexpected_eof; 501d6167fdSMichael J. Spencer } 51ed64342bSRui Ueyama Obj = reinterpret_cast<const T *>(Addr); 52ed64342bSRui Ueyama return object_error::success; 531d6167fdSMichael J. Spencer } 541d6167fdSMichael J. Spencer 558ff24d25SRui Ueyama const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const { 568ff24d25SRui Ueyama const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p); 571d6167fdSMichael J. Spencer 581d6167fdSMichael J. Spencer # ifndef NDEBUG 591d6167fdSMichael J. Spencer // Verify that the symbol points to a valid entry in the symbol table. 608ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); 618ff24d25SRui Ueyama if (Offset < COFFHeader->PointerToSymbolTable 628ff24d25SRui Ueyama || Offset >= COFFHeader->PointerToSymbolTable 6382ebd8e3SRui Ueyama + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 641d6167fdSMichael J. Spencer report_fatal_error("Symbol was outside of symbol table."); 651d6167fdSMichael J. Spencer 668ff24d25SRui Ueyama assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 671d6167fdSMichael J. Spencer == 0 && "Symbol did not point to the beginning of a symbol"); 681d6167fdSMichael J. Spencer # endif 691d6167fdSMichael J. Spencer 708ff24d25SRui Ueyama return Addr; 711d6167fdSMichael J. Spencer } 721d6167fdSMichael J. Spencer 738ff24d25SRui Ueyama const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { 748ff24d25SRui Ueyama const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); 751d6167fdSMichael J. Spencer 761d6167fdSMichael J. Spencer # ifndef NDEBUG 771d6167fdSMichael J. Spencer // Verify that the section points to a valid entry in the section table. 788ff24d25SRui Ueyama if (Addr < SectionTable 798ff24d25SRui Ueyama || Addr >= (SectionTable + COFFHeader->NumberOfSections)) 801d6167fdSMichael J. Spencer report_fatal_error("Section was outside of section table."); 811d6167fdSMichael J. Spencer 828ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); 838ff24d25SRui Ueyama assert(Offset % sizeof(coff_section) == 0 && 841d6167fdSMichael J. Spencer "Section did not point to the beginning of a section"); 851d6167fdSMichael J. Spencer # endif 861d6167fdSMichael J. Spencer 878ff24d25SRui Ueyama return Addr; 881d6167fdSMichael J. Spencer } 891d6167fdSMichael J. Spencer 905e812afaSRafael Espindola void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { 918ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 928ff24d25SRui Ueyama Symb += 1 + Symb->NumberOfAuxSymbols; 938ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Symb); 941d6167fdSMichael J. Spencer } 951d6167fdSMichael J. Spencer 968ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(DataRefImpl Ref, 971d6167fdSMichael J. Spencer StringRef &Result) const { 988ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 998ff24d25SRui Ueyama return getSymbolName(Symb, Result); 1008e90adafSMichael J. Spencer } 1018e90adafSMichael J. Spencer 1028ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref, 1031d6167fdSMichael J. Spencer uint64_t &Result) const { 1048ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1055ebaed24SMichael J. Spencer const coff_section *Section = NULL; 1068ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 1078ff24d25SRui Ueyama return EC; 108e62ab11fSRafael Espindola 1098ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 1101d6167fdSMichael J. Spencer Result = UnknownAddressOrSize; 1111d6167fdSMichael J. Spencer else if (Section) 1128ff24d25SRui Ueyama Result = Section->PointerToRawData + Symb->Value; 1131d6167fdSMichael J. Spencer else 1148ff24d25SRui Ueyama Result = Symb->Value; 1151d6167fdSMichael J. Spencer return object_error::success; 1168e90adafSMichael J. Spencer } 1178e90adafSMichael J. Spencer 1188ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref, 11975d1cf33SBenjamin Kramer uint64_t &Result) const { 1208ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 12175d1cf33SBenjamin Kramer const coff_section *Section = NULL; 1228ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 1238ff24d25SRui Ueyama return EC; 124e62ab11fSRafael Espindola 1258ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 12675d1cf33SBenjamin Kramer Result = UnknownAddressOrSize; 12775d1cf33SBenjamin Kramer else if (Section) 1288ff24d25SRui Ueyama Result = Section->VirtualAddress + Symb->Value; 12975d1cf33SBenjamin Kramer else 1308ff24d25SRui Ueyama Result = Symb->Value; 13175d1cf33SBenjamin Kramer return object_error::success; 13275d1cf33SBenjamin Kramer } 13375d1cf33SBenjamin Kramer 1348ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolType(DataRefImpl Ref, 135d3946676SMichael J. Spencer SymbolRef::Type &Result) const { 1368ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 13775d1cf33SBenjamin Kramer Result = SymbolRef::ST_Other; 1388ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && 1398ff24d25SRui Ueyama Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { 1407e4b976cSDavid Meyer Result = SymbolRef::ST_Unknown; 1415efa665fSRui Ueyama } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) { 14275d1cf33SBenjamin Kramer Result = SymbolRef::ST_Function; 14375d1cf33SBenjamin Kramer } else { 14406adfac8SRafael Espindola uint32_t Characteristics = 0; 1458ff24d25SRui Ueyama if (Symb->SectionNumber > 0) { 14606adfac8SRafael Espindola const coff_section *Section = NULL; 1478ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 1488ff24d25SRui Ueyama return EC; 14906adfac8SRafael Espindola Characteristics = Section->Characteristics; 15075d1cf33SBenjamin Kramer } 15106adfac8SRafael Espindola if (Characteristics & COFF::IMAGE_SCN_MEM_READ && 15206adfac8SRafael Espindola ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. 15306adfac8SRafael Espindola Result = SymbolRef::ST_Data; 15475d1cf33SBenjamin Kramer } 15575d1cf33SBenjamin Kramer return object_error::success; 15675d1cf33SBenjamin Kramer } 15775d1cf33SBenjamin Kramer 158*20122a43SRafael Espindola uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { 1598ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 160*20122a43SRafael Espindola uint32_t Result = SymbolRef::SF_None; 16175d1cf33SBenjamin Kramer 1627e4b976cSDavid Meyer // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common 1637e4b976cSDavid Meyer 1648ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && 1658ff24d25SRui Ueyama Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 1667e4b976cSDavid Meyer Result |= SymbolRef::SF_Undefined; 1671df4b84dSDavid Meyer 1681df4b84dSDavid Meyer // TODO: This are certainly too restrictive. 1698ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) 1701df4b84dSDavid Meyer Result |= SymbolRef::SF_Global; 1711df4b84dSDavid Meyer 1728ff24d25SRui Ueyama if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) 1731df4b84dSDavid Meyer Result |= SymbolRef::SF_Weak; 1741df4b84dSDavid Meyer 1758ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE) 1761df4b84dSDavid Meyer Result |= SymbolRef::SF_Absolute; 1771df4b84dSDavid Meyer 178*20122a43SRafael Espindola return Result; 17901759754SMichael J. Spencer } 18001759754SMichael J. Spencer 1818ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref, 1821d6167fdSMichael J. Spencer uint64_t &Result) const { 1838e90adafSMichael J. Spencer // FIXME: Return the correct size. This requires looking at all the symbols 1848e90adafSMichael J. Spencer // in the same section as this symbol, and looking for either the next 1858e90adafSMichael J. Spencer // symbol, or the end of the section. 1868ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 1875ebaed24SMichael J. Spencer const coff_section *Section = NULL; 1888ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Section)) 1898ff24d25SRui Ueyama return EC; 190e62ab11fSRafael Espindola 1918ff24d25SRui Ueyama if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) 1921d6167fdSMichael J. Spencer Result = UnknownAddressOrSize; 1931d6167fdSMichael J. Spencer else if (Section) 1948ff24d25SRui Ueyama Result = Section->SizeOfRawData - Symb->Value; 1951d6167fdSMichael J. Spencer else 1961d6167fdSMichael J. Spencer Result = 0; 1971d6167fdSMichael J. Spencer return object_error::success; 1988e90adafSMichael J. Spencer } 1998e90adafSMichael J. Spencer 2008ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref, 20132173153SMichael J. Spencer section_iterator &Result) const { 2028ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(Ref); 2038ff24d25SRui Ueyama if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED) 20432173153SMichael J. Spencer Result = end_sections(); 20532173153SMichael J. Spencer else { 2068ff24d25SRui Ueyama const coff_section *Sec = 0; 2078ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC; 2088ff24d25SRui Ueyama DataRefImpl Ref; 2098ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Sec); 2108ff24d25SRui Ueyama Result = section_iterator(SectionRef(Ref, this)); 21132173153SMichael J. Spencer } 21232173153SMichael J. Spencer return object_error::success; 21332173153SMichael J. Spencer } 21432173153SMichael J. Spencer 2158ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref, 2164f223bf7STim Northover uint64_t &Val) const { 2174f223bf7STim Northover report_fatal_error("getSymbolValue unimplemented in COFFObjectFile"); 2184f223bf7STim Northover } 2194f223bf7STim Northover 2205e812afaSRafael Espindola void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { 2218ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2228ff24d25SRui Ueyama Sec += 1; 2238ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(Sec); 2248e90adafSMichael J. Spencer } 2258e90adafSMichael J. Spencer 2268ff24d25SRui Ueyama error_code COFFObjectFile::getSectionName(DataRefImpl Ref, 2271d6167fdSMichael J. Spencer StringRef &Result) const { 2288ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2298ff24d25SRui Ueyama return getSectionName(Sec, Result); 2308e90adafSMichael J. Spencer } 2318e90adafSMichael J. Spencer 2328ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, 2331d6167fdSMichael J. Spencer uint64_t &Result) const { 2348ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2358ff24d25SRui Ueyama Result = Sec->VirtualAddress; 2361d6167fdSMichael J. Spencer return object_error::success; 2378e90adafSMichael J. Spencer } 2388e90adafSMichael J. Spencer 2398ff24d25SRui Ueyama error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, 2401d6167fdSMichael J. Spencer uint64_t &Result) const { 2418ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2428ff24d25SRui Ueyama Result = Sec->SizeOfRawData; 2431d6167fdSMichael J. Spencer return object_error::success; 2448e90adafSMichael J. Spencer } 2458e90adafSMichael J. Spencer 2468ff24d25SRui Ueyama error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, 2471d6167fdSMichael J. Spencer StringRef &Result) const { 2488ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2499da9e693SMichael J. Spencer ArrayRef<uint8_t> Res; 2508ff24d25SRui Ueyama error_code EC = getSectionContents(Sec, Res); 2519da9e693SMichael J. Spencer Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); 2529da9e693SMichael J. Spencer return EC; 2538e90adafSMichael J. Spencer } 2548e90adafSMichael J. Spencer 2558ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, 2567989460aSMichael J. Spencer uint64_t &Res) const { 2578ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2588ff24d25SRui Ueyama if (!Sec) 2597989460aSMichael J. Spencer return object_error::parse_failed; 2608ff24d25SRui Ueyama Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); 2617989460aSMichael J. Spencer return object_error::success; 2627989460aSMichael J. Spencer } 2637989460aSMichael J. Spencer 2648ff24d25SRui Ueyama error_code COFFObjectFile::isSectionText(DataRefImpl Ref, 2651d6167fdSMichael J. Spencer bool &Result) const { 2668ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2678ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; 2681d6167fdSMichael J. Spencer return object_error::success; 2698e90adafSMichael J. Spencer } 2708e90adafSMichael J. Spencer 2718ff24d25SRui Ueyama error_code COFFObjectFile::isSectionData(DataRefImpl Ref, 272800619f2SMichael J. Spencer bool &Result) const { 2738ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2748ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; 275800619f2SMichael J. Spencer return object_error::success; 276800619f2SMichael J. Spencer } 277800619f2SMichael J. Spencer 2788ff24d25SRui Ueyama error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, 279800619f2SMichael J. Spencer bool &Result) const { 2808ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2818ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 282800619f2SMichael J. Spencer return object_error::success; 283800619f2SMichael J. Spencer } 284800619f2SMichael J. Spencer 2858ff24d25SRui Ueyama error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref, 2862138ef6dSPreston Gurd bool &Result) const { 2872138ef6dSPreston Gurd // FIXME: Unimplemented 2882138ef6dSPreston Gurd Result = true; 2892138ef6dSPreston Gurd return object_error::success; 2902138ef6dSPreston Gurd } 2912138ef6dSPreston Gurd 2928ff24d25SRui Ueyama error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, 2932138ef6dSPreston Gurd bool &Result) const { 2948ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 2958ff24d25SRui Ueyama Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 2962138ef6dSPreston Gurd return object_error::success; 2972138ef6dSPreston Gurd } 2982138ef6dSPreston Gurd 2998ff24d25SRui Ueyama error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, 3002138ef6dSPreston Gurd bool &Result) const { 301b96a320aSAndrew Kaylor // FIXME: Unimplemented. 3022138ef6dSPreston Gurd Result = false; 3032138ef6dSPreston Gurd return object_error::success; 3042138ef6dSPreston Gurd } 3052138ef6dSPreston Gurd 3068ff24d25SRui Ueyama error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, 3073f31fa05SAndrew Kaylor bool &Result) const { 3083f31fa05SAndrew Kaylor // FIXME: Unimplemented. 3093f31fa05SAndrew Kaylor Result = false; 3103f31fa05SAndrew Kaylor return object_error::success; 3113f31fa05SAndrew Kaylor } 3123f31fa05SAndrew Kaylor 3138ff24d25SRui Ueyama error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, 3148ff24d25SRui Ueyama DataRefImpl SymbRef, 315f6f3e81cSBenjamin Kramer bool &Result) const { 3168ff24d25SRui Ueyama const coff_section *Sec = toSec(SecRef); 3178ff24d25SRui Ueyama const coff_symbol *Symb = toSymb(SymbRef); 3188ff24d25SRui Ueyama const coff_section *SymbSec = 0; 3198ff24d25SRui Ueyama if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC; 3208ff24d25SRui Ueyama if (SymbSec == Sec) 3219a28851eSMichael J. Spencer Result = true; 3229a28851eSMichael J. Spencer else 323f6f3e81cSBenjamin Kramer Result = false; 324f6f3e81cSBenjamin Kramer return object_error::success; 325f6f3e81cSBenjamin Kramer } 326f6f3e81cSBenjamin Kramer 3278ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { 3288ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3298ff24d25SRui Ueyama DataRefImpl Ret; 3308ff24d25SRui Ueyama if (Sec->NumberOfRelocations == 0) 3318ff24d25SRui Ueyama Ret.p = 0; 332e5fd0047SMichael J. Spencer else 3338ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations); 334e5fd0047SMichael J. Spencer 3358ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 336e5fd0047SMichael J. Spencer } 337e5fd0047SMichael J. Spencer 3388ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { 3398ff24d25SRui Ueyama const coff_section *Sec = toSec(Ref); 3408ff24d25SRui Ueyama DataRefImpl Ret; 3418ff24d25SRui Ueyama if (Sec->NumberOfRelocations == 0) 3428ff24d25SRui Ueyama Ret.p = 0; 343e5fd0047SMichael J. Spencer else 3448ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>( 345e5fd0047SMichael J. Spencer reinterpret_cast<const coff_relocation*>( 3468ff24d25SRui Ueyama base() + Sec->PointerToRelocations) 3478ff24d25SRui Ueyama + Sec->NumberOfRelocations); 348e5fd0047SMichael J. Spencer 3498ff24d25SRui Ueyama return relocation_iterator(RelocationRef(Ret, this)); 350e5fd0047SMichael J. Spencer } 351e5fd0047SMichael J. Spencer 352c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 353c2bed429SRui Ueyama error_code COFFObjectFile::initSymbolTablePtr() { 3548ff24d25SRui Ueyama if (error_code EC = getObject( 355c2bed429SRui Ueyama SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable, 356c2bed429SRui Ueyama COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 3578ff24d25SRui Ueyama return EC; 358c2bed429SRui Ueyama 359c2bed429SRui Ueyama // Find string table. The first four byte of the string table contains the 360c2bed429SRui Ueyama // total size of the string table, including the size field itself. If the 361c2bed429SRui Ueyama // string table is empty, the value of the first four byte would be 4. 362c2bed429SRui Ueyama const uint8_t *StringTableAddr = 363c2bed429SRui Ueyama base() + COFFHeader->PointerToSymbolTable + 364c2bed429SRui Ueyama COFFHeader->NumberOfSymbols * sizeof(coff_symbol); 365c2bed429SRui Ueyama const ulittle32_t *StringTableSizePtr; 3668ff24d25SRui Ueyama if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) 3678ff24d25SRui Ueyama return EC; 368c2bed429SRui Ueyama StringTableSize = *StringTableSizePtr; 3698ff24d25SRui Ueyama if (error_code EC = 370c2bed429SRui Ueyama getObject(StringTable, Data, StringTableAddr, StringTableSize)) 3718ff24d25SRui Ueyama return EC; 372c2bed429SRui Ueyama 373c2bed429SRui Ueyama // Check that the string table is null terminated if has any in it. 374c2bed429SRui Ueyama if (StringTableSize < 4 || 375c2bed429SRui Ueyama (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) 376c2bed429SRui Ueyama return object_error::parse_failed; 377c2bed429SRui Ueyama return object_error::success; 378c2bed429SRui Ueyama } 379c2bed429SRui Ueyama 380c2bed429SRui Ueyama // Returns the file offset for the given RVA. 381c2bed429SRui Ueyama error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const { 3828ff24d25SRui Ueyama for (section_iterator I = begin_sections(), E = end_sections(); I != E; 3835e812afaSRafael Espindola ++I) { 3848ff24d25SRui Ueyama const coff_section *Section = getCOFFSection(I); 385c2bed429SRui Ueyama uint32_t SectionStart = Section->VirtualAddress; 386c2bed429SRui Ueyama uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; 387c2bed429SRui Ueyama if (SectionStart <= Rva && Rva < SectionEnd) { 388c2bed429SRui Ueyama uint32_t Offset = Rva - SectionStart; 389c2bed429SRui Ueyama Res = uintptr_t(base()) + Section->PointerToRawData + Offset; 390c2bed429SRui Ueyama return object_error::success; 391c2bed429SRui Ueyama } 392c2bed429SRui Ueyama } 393c2bed429SRui Ueyama return object_error::parse_failed; 394c2bed429SRui Ueyama } 395c2bed429SRui Ueyama 396c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name 397c2bed429SRui Ueyama // table entry. 398c2bed429SRui Ueyama error_code COFFObjectFile:: 399c2bed429SRui Ueyama getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const { 400c2bed429SRui Ueyama uintptr_t IntPtr = 0; 4018ff24d25SRui Ueyama if (error_code EC = getRvaPtr(Rva, IntPtr)) 4028ff24d25SRui Ueyama return EC; 403c2bed429SRui Ueyama const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); 404c2bed429SRui Ueyama Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); 405c2bed429SRui Ueyama Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); 406c2bed429SRui Ueyama return object_error::success; 407c2bed429SRui Ueyama } 408c2bed429SRui Ueyama 409c2bed429SRui Ueyama // Find the import table. 410c2bed429SRui Ueyama error_code COFFObjectFile::initImportTablePtr() { 411c2bed429SRui Ueyama // First, we get the RVA of the import table. If the file lacks a pointer to 412c2bed429SRui Ueyama // the import table, do nothing. 413c2bed429SRui Ueyama const data_directory *DataEntry; 414c2bed429SRui Ueyama if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) 415c2bed429SRui Ueyama return object_error::success; 416c2bed429SRui Ueyama 417c2bed429SRui Ueyama // Do nothing if the pointer to import table is NULL. 418c2bed429SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 419c2bed429SRui Ueyama return object_error::success; 420c2bed429SRui Ueyama 421c2bed429SRui Ueyama uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; 422c2bed429SRui Ueyama NumberOfImportDirectory = DataEntry->Size / 423c2bed429SRui Ueyama sizeof(import_directory_table_entry); 424c2bed429SRui Ueyama 425c2bed429SRui Ueyama // Find the section that contains the RVA. This is needed because the RVA is 426c2bed429SRui Ueyama // the import table's memory address which is different from its file offset. 427c2bed429SRui Ueyama uintptr_t IntPtr = 0; 4288ff24d25SRui Ueyama if (error_code EC = getRvaPtr(ImportTableRva, IntPtr)) 4298ff24d25SRui Ueyama return EC; 430c2bed429SRui Ueyama ImportDirectory = reinterpret_cast< 431c2bed429SRui Ueyama const import_directory_table_entry *>(IntPtr); 432ad882ba8SRui Ueyama return object_error::success; 433ad882ba8SRui Ueyama } 434c2bed429SRui Ueyama 435ad882ba8SRui Ueyama // Find the export table. 436ad882ba8SRui Ueyama error_code COFFObjectFile::initExportTablePtr() { 437ad882ba8SRui Ueyama // First, we get the RVA of the export table. If the file lacks a pointer to 438ad882ba8SRui Ueyama // the export table, do nothing. 439ad882ba8SRui Ueyama const data_directory *DataEntry; 440ad882ba8SRui Ueyama if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) 441ad882ba8SRui Ueyama return object_error::success; 442ad882ba8SRui Ueyama 443ad882ba8SRui Ueyama // Do nothing if the pointer to export table is NULL. 444ad882ba8SRui Ueyama if (DataEntry->RelativeVirtualAddress == 0) 445ad882ba8SRui Ueyama return object_error::success; 446ad882ba8SRui Ueyama 447ad882ba8SRui Ueyama uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; 448ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 449ad882ba8SRui Ueyama if (error_code EC = getRvaPtr(ExportTableRva, IntPtr)) 450ad882ba8SRui Ueyama return EC; 45124fc2d64SRui Ueyama ExportDirectory = 45224fc2d64SRui Ueyama reinterpret_cast<const export_directory_table_entry *>(IntPtr); 453ad882ba8SRui Ueyama return object_error::success; 454c2bed429SRui Ueyama } 455c2bed429SRui Ueyama 456afcc3df7SRafael Espindola COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC, 457afcc3df7SRafael Espindola bool BufferOwned) 458afcc3df7SRafael Espindola : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0), 45910ed9ddcSRui Ueyama PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0), 46010ed9ddcSRui Ueyama SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0), 461afcc3df7SRafael Espindola NumberOfImportDirectory(0), ExportDirectory(0) { 4621d6167fdSMichael J. Spencer // Check that we at least have enough room for a header. 4638ff24d25SRui Ueyama if (!checkSize(Data, EC, sizeof(coff_file_header))) return; 464ee066fc4SEric Christopher 46582ebd8e3SRui Ueyama // The current location in the file where we are looking at. 46682ebd8e3SRui Ueyama uint64_t CurPtr = 0; 46782ebd8e3SRui Ueyama 46882ebd8e3SRui Ueyama // PE header is optional and is present only in executables. If it exists, 46982ebd8e3SRui Ueyama // it is placed right after COFF header. 4708ff24d25SRui Ueyama bool HasPEHeader = false; 471ee066fc4SEric Christopher 4721d6167fdSMichael J. Spencer // Check if this is a PE/COFF file. 473ec29b121SMichael J. Spencer if (base()[0] == 0x4d && base()[1] == 0x5a) { 474ee066fc4SEric Christopher // PE/COFF, seek through MS-DOS compatibility stub and 4-byte 475ee066fc4SEric Christopher // PE signature to find 'normal' COFF header. 4768ff24d25SRui Ueyama if (!checkSize(Data, EC, 0x3c + 8)) return; 47782ebd8e3SRui Ueyama CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); 47882ebd8e3SRui Ueyama // Check the PE magic bytes. ("PE\0\0") 47982ebd8e3SRui Ueyama if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) { 4808ff24d25SRui Ueyama EC = object_error::parse_failed; 4811d6167fdSMichael J. Spencer return; 4821d6167fdSMichael J. Spencer } 48382ebd8e3SRui Ueyama CurPtr += 4; // Skip the PE magic bytes. 4848ff24d25SRui Ueyama HasPEHeader = true; 485ee066fc4SEric Christopher } 486ee066fc4SEric Christopher 4878ff24d25SRui Ueyama if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) 4881d6167fdSMichael J. Spencer return; 48982ebd8e3SRui Ueyama CurPtr += sizeof(coff_file_header); 49082ebd8e3SRui Ueyama 4918ff24d25SRui Ueyama if (HasPEHeader) { 49210ed9ddcSRui Ueyama const pe32_header *Header; 49310ed9ddcSRui Ueyama if ((EC = getObject(Header, Data, base() + CurPtr))) 49482ebd8e3SRui Ueyama return; 49510ed9ddcSRui Ueyama 49610ed9ddcSRui Ueyama const uint8_t *DataDirAddr; 49710ed9ddcSRui Ueyama uint64_t DataDirSize; 49810ed9ddcSRui Ueyama if (Header->Magic == 0x10b) { 49910ed9ddcSRui Ueyama PE32Header = Header; 50010ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32_header); 50110ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; 50210ed9ddcSRui Ueyama } else if (Header->Magic == 0x20b) { 50310ed9ddcSRui Ueyama PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); 50410ed9ddcSRui Ueyama DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); 50510ed9ddcSRui Ueyama DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; 50610ed9ddcSRui Ueyama } else { 50710ed9ddcSRui Ueyama // It's neither PE32 nor PE32+. 50810ed9ddcSRui Ueyama EC = object_error::parse_failed; 509ed64342bSRui Ueyama return; 510ed64342bSRui Ueyama } 51110ed9ddcSRui Ueyama if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) 51210ed9ddcSRui Ueyama return; 51382ebd8e3SRui Ueyama CurPtr += COFFHeader->SizeOfOptionalHeader; 51482ebd8e3SRui Ueyama } 5151d6167fdSMichael J. Spencer 516692410efSRafael Espindola if (COFFHeader->isImportLibrary()) 517692410efSRafael Espindola return; 518692410efSRafael Espindola 5198ff24d25SRui Ueyama if ((EC = getObject(SectionTable, Data, base() + CurPtr, 520ed64342bSRui Ueyama COFFHeader->NumberOfSections * sizeof(coff_section)))) 5211d6167fdSMichael J. Spencer return; 5221d6167fdSMichael J. Spencer 523c2bed429SRui Ueyama // Initialize the pointer to the symbol table. 524c2bed429SRui Ueyama if (COFFHeader->PointerToSymbolTable != 0) 5258ff24d25SRui Ueyama if ((EC = initSymbolTablePtr())) 5261d6167fdSMichael J. Spencer return; 5278e90adafSMichael J. Spencer 528c2bed429SRui Ueyama // Initialize the pointer to the beginning of the import table. 5298ff24d25SRui Ueyama if ((EC = initImportTablePtr())) 530ed64342bSRui Ueyama return; 5311d6167fdSMichael J. Spencer 532ad882ba8SRui Ueyama // Initialize the pointer to the export table. 5338ff24d25SRui Ueyama if ((EC = initExportTablePtr())) 534ad882ba8SRui Ueyama return; 535ad882ba8SRui Ueyama 5368ff24d25SRui Ueyama EC = object_error::success; 5378e90adafSMichael J. Spencer } 5388e90adafSMichael J. Spencer 539e5fd0047SMichael J. Spencer symbol_iterator COFFObjectFile::begin_symbols() const { 5408ff24d25SRui Ueyama DataRefImpl Ret; 5418ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SymbolTable); 5428ff24d25SRui Ueyama return symbol_iterator(SymbolRef(Ret, this)); 5438e90adafSMichael J. Spencer } 5448e90adafSMichael J. Spencer 545e5fd0047SMichael J. Spencer symbol_iterator COFFObjectFile::end_symbols() const { 5468e90adafSMichael J. Spencer // The symbol table ends where the string table begins. 5478ff24d25SRui Ueyama DataRefImpl Ret; 5488ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(StringTable); 5498ff24d25SRui Ueyama return symbol_iterator(SymbolRef(Ret, this)); 5508e90adafSMichael J. Spencer } 5518e90adafSMichael J. Spencer 5522fc34c5fSDavid Meyer library_iterator COFFObjectFile::begin_libraries_needed() const { 5532fc34c5fSDavid Meyer // TODO: implement 5542fc34c5fSDavid Meyer report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 5552fc34c5fSDavid Meyer } 5562fc34c5fSDavid Meyer 5572fc34c5fSDavid Meyer library_iterator COFFObjectFile::end_libraries_needed() const { 5582fc34c5fSDavid Meyer // TODO: implement 5592fc34c5fSDavid Meyer report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); 5602fc34c5fSDavid Meyer } 5612fc34c5fSDavid Meyer 562c429b80dSDavid Meyer StringRef COFFObjectFile::getLoadName() const { 563c429b80dSDavid Meyer // COFF does not have this field. 564c429b80dSDavid Meyer return ""; 565c429b80dSDavid Meyer } 566c429b80dSDavid Meyer 567bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_begin() const { 568a045b73aSRui Ueyama return import_directory_iterator( 569a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, 0, this)); 570c2bed429SRui Ueyama } 571c2bed429SRui Ueyama 572bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_end() const { 573a045b73aSRui Ueyama return import_directory_iterator( 574a045b73aSRui Ueyama ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); 575c2bed429SRui Ueyama } 576c429b80dSDavid Meyer 577ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_begin() const { 578ad882ba8SRui Ueyama return export_directory_iterator( 579ad882ba8SRui Ueyama ExportDirectoryEntryRef(ExportDirectory, 0, this)); 580ad882ba8SRui Ueyama } 581ad882ba8SRui Ueyama 582ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_end() const { 583ad882ba8SRui Ueyama if (ExportDirectory == 0) 584ad882ba8SRui Ueyama return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this)); 5858ff24d25SRui Ueyama ExportDirectoryEntryRef Ref(ExportDirectory, 586ad882ba8SRui Ueyama ExportDirectory->AddressTableEntries, this); 5878ff24d25SRui Ueyama return export_directory_iterator(Ref); 588ad882ba8SRui Ueyama } 589ad882ba8SRui Ueyama 590e5fd0047SMichael J. Spencer section_iterator COFFObjectFile::begin_sections() const { 5918ff24d25SRui Ueyama DataRefImpl Ret; 5928ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable); 5938ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 5948e90adafSMichael J. Spencer } 5958e90adafSMichael J. Spencer 596e5fd0047SMichael J. Spencer section_iterator COFFObjectFile::end_sections() const { 5978ff24d25SRui Ueyama DataRefImpl Ret; 5988ff24d25SRui Ueyama int NumSections = COFFHeader->isImportLibrary() 59915ba1e20SRui Ueyama ? 0 : COFFHeader->NumberOfSections; 6008ff24d25SRui Ueyama Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); 6018ff24d25SRui Ueyama return section_iterator(SectionRef(Ret, this)); 6028e90adafSMichael J. Spencer } 6038e90adafSMichael J. Spencer 6048e90adafSMichael J. Spencer uint8_t COFFObjectFile::getBytesInAddress() const { 6050324b672SMichael J. Spencer return getArch() == Triple::x86_64 ? 8 : 4; 6068e90adafSMichael J. Spencer } 6078e90adafSMichael J. Spencer 6088e90adafSMichael J. Spencer StringRef COFFObjectFile::getFileFormatName() const { 60982ebd8e3SRui Ueyama switch(COFFHeader->Machine) { 6108e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 6118e90adafSMichael J. Spencer return "COFF-i386"; 6128e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 6138e90adafSMichael J. Spencer return "COFF-x86-64"; 6148e90adafSMichael J. Spencer default: 6158e90adafSMichael J. Spencer return "COFF-<unknown arch>"; 6168e90adafSMichael J. Spencer } 6178e90adafSMichael J. Spencer } 6188e90adafSMichael J. Spencer 6198e90adafSMichael J. Spencer unsigned COFFObjectFile::getArch() const { 62082ebd8e3SRui Ueyama switch(COFFHeader->Machine) { 6218e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 6228e90adafSMichael J. Spencer return Triple::x86; 6238e90adafSMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 6248e90adafSMichael J. Spencer return Triple::x86_64; 6258e90adafSMichael J. Spencer default: 6268e90adafSMichael J. Spencer return Triple::UnknownArch; 6278e90adafSMichael J. Spencer } 6288e90adafSMichael J. Spencer } 6298e90adafSMichael J. Spencer 63082ebd8e3SRui Ueyama // This method is kept here because lld uses this. As soon as we make 63182ebd8e3SRui Ueyama // lld to use getCOFFHeader, this method will be removed. 63289a7a5eaSMichael J. Spencer error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const { 63382ebd8e3SRui Ueyama return getCOFFHeader(Res); 63482ebd8e3SRui Ueyama } 63582ebd8e3SRui Ueyama 63682ebd8e3SRui Ueyama error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const { 63782ebd8e3SRui Ueyama Res = COFFHeader; 63882ebd8e3SRui Ueyama return object_error::success; 63982ebd8e3SRui Ueyama } 64082ebd8e3SRui Ueyama 64182ebd8e3SRui Ueyama error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { 64282ebd8e3SRui Ueyama Res = PE32Header; 64389a7a5eaSMichael J. Spencer return object_error::success; 64489a7a5eaSMichael J. Spencer } 64589a7a5eaSMichael J. Spencer 64610ed9ddcSRui Ueyama error_code 64710ed9ddcSRui Ueyama COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { 64810ed9ddcSRui Ueyama Res = PE32PlusHeader; 64910ed9ddcSRui Ueyama return object_error::success; 65010ed9ddcSRui Ueyama } 65110ed9ddcSRui Ueyama 6528ff24d25SRui Ueyama error_code COFFObjectFile::getDataDirectory(uint32_t Index, 653ed64342bSRui Ueyama const data_directory *&Res) const { 654ed64342bSRui Ueyama // Error if if there's no data directory or the index is out of range. 65510ed9ddcSRui Ueyama if (!DataDirectory) 65610ed9ddcSRui Ueyama return object_error::parse_failed; 65710ed9ddcSRui Ueyama assert(PE32Header || PE32PlusHeader); 65810ed9ddcSRui Ueyama uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize 65910ed9ddcSRui Ueyama : PE32PlusHeader->NumberOfRvaAndSize; 66010ed9ddcSRui Ueyama if (Index > NumEnt) 661ed64342bSRui Ueyama return object_error::parse_failed; 6628ff24d25SRui Ueyama Res = &DataDirectory[Index]; 663ed64342bSRui Ueyama return object_error::success; 664ed64342bSRui Ueyama } 665ed64342bSRui Ueyama 6668ff24d25SRui Ueyama error_code COFFObjectFile::getSection(int32_t Index, 6671d6167fdSMichael J. Spencer const coff_section *&Result) const { 6681d6167fdSMichael J. Spencer // Check for special index values. 6698ff24d25SRui Ueyama if (Index == COFF::IMAGE_SYM_UNDEFINED || 6708ff24d25SRui Ueyama Index == COFF::IMAGE_SYM_ABSOLUTE || 6718ff24d25SRui Ueyama Index == COFF::IMAGE_SYM_DEBUG) 6721d6167fdSMichael J. Spencer Result = NULL; 6738ff24d25SRui Ueyama else if (Index > 0 && Index <= COFFHeader->NumberOfSections) 6741d6167fdSMichael J. Spencer // We already verified the section table data, so no need to check again. 6758ff24d25SRui Ueyama Result = SectionTable + (Index - 1); 6761d6167fdSMichael J. Spencer else 6771d6167fdSMichael J. Spencer return object_error::parse_failed; 6781d6167fdSMichael J. Spencer return object_error::success; 6798e90adafSMichael J. Spencer } 6808e90adafSMichael J. Spencer 6818ff24d25SRui Ueyama error_code COFFObjectFile::getString(uint32_t Offset, 6821d6167fdSMichael J. Spencer StringRef &Result) const { 6831d6167fdSMichael J. Spencer if (StringTableSize <= 4) 6841d6167fdSMichael J. Spencer // Tried to get a string from an empty string table. 6851d6167fdSMichael J. Spencer return object_error::parse_failed; 6868ff24d25SRui Ueyama if (Offset >= StringTableSize) 6871d6167fdSMichael J. Spencer return object_error::unexpected_eof; 6888ff24d25SRui Ueyama Result = StringRef(StringTable + Offset); 6891d6167fdSMichael J. Spencer return object_error::success; 6908e90adafSMichael J. Spencer } 691022ecdf2SBenjamin Kramer 6928ff24d25SRui Ueyama error_code COFFObjectFile::getSymbol(uint32_t Index, 693e5fd0047SMichael J. Spencer const coff_symbol *&Result) const { 6948ff24d25SRui Ueyama if (Index < COFFHeader->NumberOfSymbols) 6958ff24d25SRui Ueyama Result = SymbolTable + Index; 696e5fd0047SMichael J. Spencer else 697e5fd0047SMichael J. Spencer return object_error::parse_failed; 698e5fd0047SMichael J. Spencer return object_error::success; 699e5fd0047SMichael J. Spencer } 700e5fd0047SMichael J. Spencer 7018ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol, 70289a7a5eaSMichael J. Spencer StringRef &Res) const { 70389a7a5eaSMichael J. Spencer // Check for string table entry. First 4 bytes are 0. 7048ff24d25SRui Ueyama if (Symbol->Name.Offset.Zeroes == 0) { 7058ff24d25SRui Ueyama uint32_t Offset = Symbol->Name.Offset.Offset; 7068ff24d25SRui Ueyama if (error_code EC = getString(Offset, Res)) 7078ff24d25SRui Ueyama return EC; 70889a7a5eaSMichael J. Spencer return object_error::success; 70989a7a5eaSMichael J. Spencer } 71089a7a5eaSMichael J. Spencer 7118ff24d25SRui Ueyama if (Symbol->Name.ShortName[7] == 0) 71289a7a5eaSMichael J. Spencer // Null terminated, let ::strlen figure out the length. 7138ff24d25SRui Ueyama Res = StringRef(Symbol->Name.ShortName); 71489a7a5eaSMichael J. Spencer else 71589a7a5eaSMichael J. Spencer // Not null terminated, use all 8 bytes. 7168ff24d25SRui Ueyama Res = StringRef(Symbol->Name.ShortName, 8); 71789a7a5eaSMichael J. Spencer return object_error::success; 71889a7a5eaSMichael J. Spencer } 71989a7a5eaSMichael J. Spencer 72071757ef3SMarshall Clow ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData( 7218ff24d25SRui Ueyama const coff_symbol *Symbol) const { 7228ff24d25SRui Ueyama const uint8_t *Aux = NULL; 72371757ef3SMarshall Clow 7248ff24d25SRui Ueyama if (Symbol->NumberOfAuxSymbols > 0) { 72571757ef3SMarshall Clow // AUX data comes immediately after the symbol in COFF 7268ff24d25SRui Ueyama Aux = reinterpret_cast<const uint8_t *>(Symbol + 1); 72771757ef3SMarshall Clow # ifndef NDEBUG 7288ff24d25SRui Ueyama // Verify that the Aux symbol points to a valid entry in the symbol table. 7298ff24d25SRui Ueyama uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); 7308ff24d25SRui Ueyama if (Offset < COFFHeader->PointerToSymbolTable 7318ff24d25SRui Ueyama || Offset >= COFFHeader->PointerToSymbolTable 73282ebd8e3SRui Ueyama + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) 73371757ef3SMarshall Clow report_fatal_error("Aux Symbol data was outside of symbol table."); 73471757ef3SMarshall Clow 7358ff24d25SRui Ueyama assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) 73671757ef3SMarshall Clow == 0 && "Aux Symbol data did not point to the beginning of a symbol"); 73771757ef3SMarshall Clow # endif 738bfb85e67SMarshall Clow } 73924fc2d64SRui Ueyama return ArrayRef<uint8_t>(Aux, 74024fc2d64SRui Ueyama Symbol->NumberOfAuxSymbols * sizeof(coff_symbol)); 74171757ef3SMarshall Clow } 74271757ef3SMarshall Clow 74353c2d547SMichael J. Spencer error_code COFFObjectFile::getSectionName(const coff_section *Sec, 74453c2d547SMichael J. Spencer StringRef &Res) const { 74553c2d547SMichael J. Spencer StringRef Name; 74653c2d547SMichael J. Spencer if (Sec->Name[7] == 0) 74753c2d547SMichael J. Spencer // Null terminated, let ::strlen figure out the length. 74853c2d547SMichael J. Spencer Name = Sec->Name; 74953c2d547SMichael J. Spencer else 75053c2d547SMichael J. Spencer // Not null terminated, use all 8 bytes. 75153c2d547SMichael J. Spencer Name = StringRef(Sec->Name, 8); 75253c2d547SMichael J. Spencer 75353c2d547SMichael J. Spencer // Check for string table entry. First byte is '/'. 75453c2d547SMichael J. Spencer if (Name[0] == '/') { 75553c2d547SMichael J. Spencer uint32_t Offset; 75653c2d547SMichael J. Spencer if (Name.substr(1).getAsInteger(10, Offset)) 75753c2d547SMichael J. Spencer return object_error::parse_failed; 7588ff24d25SRui Ueyama if (error_code EC = getString(Offset, Name)) 7598ff24d25SRui Ueyama return EC; 76053c2d547SMichael J. Spencer } 76153c2d547SMichael J. Spencer 76253c2d547SMichael J. Spencer Res = Name; 76353c2d547SMichael J. Spencer return object_error::success; 76453c2d547SMichael J. Spencer } 76553c2d547SMichael J. Spencer 7669da9e693SMichael J. Spencer error_code COFFObjectFile::getSectionContents(const coff_section *Sec, 7679da9e693SMichael J. Spencer ArrayRef<uint8_t> &Res) const { 7689da9e693SMichael J. Spencer // The only thing that we need to verify is that the contents is contained 7699da9e693SMichael J. Spencer // within the file bounds. We don't need to make sure it doesn't cover other 7709da9e693SMichael J. Spencer // data, as there's nothing that says that is not allowed. 7719da9e693SMichael J. Spencer uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; 7729da9e693SMichael J. Spencer uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; 7739da9e693SMichael J. Spencer if (ConEnd > uintptr_t(Data->getBufferEnd())) 7749da9e693SMichael J. Spencer return object_error::parse_failed; 7759da9e693SMichael J. Spencer Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart), 7769da9e693SMichael J. Spencer Sec->SizeOfRawData); 7779da9e693SMichael J. Spencer return object_error::success; 7789da9e693SMichael J. Spencer } 7799da9e693SMichael J. Spencer 780022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { 781e5fd0047SMichael J. Spencer return reinterpret_cast<const coff_relocation*>(Rel.p); 782022ecdf2SBenjamin Kramer } 7838ff24d25SRui Ueyama 7845e812afaSRafael Espindola void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { 785e5fd0047SMichael J. Spencer Rel.p = reinterpret_cast<uintptr_t>( 786e5fd0047SMichael J. Spencer reinterpret_cast<const coff_relocation*>(Rel.p) + 1); 787022ecdf2SBenjamin Kramer } 7888ff24d25SRui Ueyama 789022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, 790022ecdf2SBenjamin Kramer uint64_t &Res) const { 7911e483879SRafael Espindola report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); 792022ecdf2SBenjamin Kramer } 7938ff24d25SRui Ueyama 794cbe72fc9SDanil Malyshev error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, 795cbe72fc9SDanil Malyshev uint64_t &Res) const { 796cbe72fc9SDanil Malyshev Res = toRel(Rel)->VirtualAddress; 797cbe72fc9SDanil Malyshev return object_error::success; 798cbe72fc9SDanil Malyshev } 7998ff24d25SRui Ueyama 800806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { 801022ecdf2SBenjamin Kramer const coff_relocation* R = toRel(Rel); 8028ff24d25SRui Ueyama DataRefImpl Ref; 8038ff24d25SRui Ueyama Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); 8048ff24d25SRui Ueyama return symbol_iterator(SymbolRef(Ref, this)); 805022ecdf2SBenjamin Kramer } 8068ff24d25SRui Ueyama 807022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, 8087be76590SOwen Anderson uint64_t &Res) const { 809022ecdf2SBenjamin Kramer const coff_relocation* R = toRel(Rel); 810022ecdf2SBenjamin Kramer Res = R->Type; 811022ecdf2SBenjamin Kramer return object_error::success; 812022ecdf2SBenjamin Kramer } 813e5fd0047SMichael J. Spencer 81471757ef3SMarshall Clow const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const { 81571757ef3SMarshall Clow return toSec(It->getRawDataRefImpl()); 81671757ef3SMarshall Clow } 81771757ef3SMarshall Clow 81871757ef3SMarshall Clow const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const { 81971757ef3SMarshall Clow return toSymb(It->getRawDataRefImpl()); 82071757ef3SMarshall Clow } 82171757ef3SMarshall Clow 822d3e2a76cSMarshall Clow const coff_relocation *COFFObjectFile::getCOFFRelocation( 823d3e2a76cSMarshall Clow relocation_iterator &It) const { 824d3e2a76cSMarshall Clow return toRel(It->getRawDataRefImpl()); 825d3e2a76cSMarshall Clow } 826d3e2a76cSMarshall Clow 827e5fd0047SMichael J. Spencer #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \ 8288ff24d25SRui Ueyama case COFF::enum: Res = #enum; break; 829e5fd0047SMichael J. Spencer 830e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, 831e5fd0047SMichael J. Spencer SmallVectorImpl<char> &Result) const { 8328ff24d25SRui Ueyama const coff_relocation *Reloc = toRel(Rel); 8338ff24d25SRui Ueyama StringRef Res; 83482ebd8e3SRui Ueyama switch (COFFHeader->Machine) { 835e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_AMD64: 8368ff24d25SRui Ueyama switch (Reloc->Type) { 837e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); 838e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); 839e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); 840e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); 841e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); 842e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); 843e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); 844e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); 845e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); 846e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); 847e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); 848e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); 849e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); 850e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); 851e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); 852e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); 853e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); 854e5fd0047SMichael J. Spencer default: 8558ff24d25SRui Ueyama Res = "Unknown"; 856e5fd0047SMichael J. Spencer } 857e5fd0047SMichael J. Spencer break; 858e5fd0047SMichael J. Spencer case COFF::IMAGE_FILE_MACHINE_I386: 8598ff24d25SRui Ueyama switch (Reloc->Type) { 860e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); 861e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); 862e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); 863e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); 864e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); 865e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); 866e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); 867e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); 868e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); 869e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); 870e5fd0047SMichael J. Spencer LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); 871e5fd0047SMichael J. Spencer default: 8728ff24d25SRui Ueyama Res = "Unknown"; 873e5fd0047SMichael J. Spencer } 874e5fd0047SMichael J. Spencer break; 875e5fd0047SMichael J. Spencer default: 8768ff24d25SRui Ueyama Res = "Unknown"; 877e5fd0047SMichael J. Spencer } 8788ff24d25SRui Ueyama Result.append(Res.begin(), Res.end()); 879e5fd0047SMichael J. Spencer return object_error::success; 880e5fd0047SMichael J. Spencer } 881e5fd0047SMichael J. Spencer 882e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME 883e5fd0047SMichael J. Spencer 884e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel, 885e5fd0047SMichael J. Spencer SmallVectorImpl<char> &Result) const { 8868ff24d25SRui Ueyama const coff_relocation *Reloc = toRel(Rel); 8878ff24d25SRui Ueyama const coff_symbol *Symb = 0; 8888ff24d25SRui Ueyama if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC; 8898ff24d25SRui Ueyama DataRefImpl Sym; 8908ff24d25SRui Ueyama Sym.p = reinterpret_cast<uintptr_t>(Symb); 8918ff24d25SRui Ueyama StringRef SymName; 8928ff24d25SRui Ueyama if (error_code EC = getSymbolName(Sym, SymName)) return EC; 8938ff24d25SRui Ueyama Result.append(SymName.begin(), SymName.end()); 894e5fd0047SMichael J. Spencer return object_error::success; 895022ecdf2SBenjamin Kramer } 8968e90adafSMichael J. Spencer 8972fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData, 8982fc34c5fSDavid Meyer LibraryRef &Result) const { 8992fc34c5fSDavid Meyer report_fatal_error("getLibraryNext not implemented in COFFObjectFile"); 9002fc34c5fSDavid Meyer } 9012fc34c5fSDavid Meyer 9022fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData, 9032fc34c5fSDavid Meyer StringRef &Result) const { 9042fc34c5fSDavid Meyer report_fatal_error("getLibraryPath not implemented in COFFObjectFile"); 9052fc34c5fSDavid Meyer } 9062fc34c5fSDavid Meyer 907c2bed429SRui Ueyama bool ImportDirectoryEntryRef:: 908c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const { 909a045b73aSRui Ueyama return ImportTable == Other.ImportTable && Index == Other.Index; 910c2bed429SRui Ueyama } 911c2bed429SRui Ueyama 9125e812afaSRafael Espindola void ImportDirectoryEntryRef::moveNext() { 9135e812afaSRafael Espindola ++Index; 914c2bed429SRui Ueyama } 915c2bed429SRui Ueyama 916c2bed429SRui Ueyama error_code ImportDirectoryEntryRef:: 917c2bed429SRui Ueyama getImportTableEntry(const import_directory_table_entry *&Result) const { 918a045b73aSRui Ueyama Result = ImportTable; 919c2bed429SRui Ueyama return object_error::success; 920c2bed429SRui Ueyama } 921c2bed429SRui Ueyama 922c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { 923c2bed429SRui Ueyama uintptr_t IntPtr = 0; 924a045b73aSRui Ueyama if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr)) 925a045b73aSRui Ueyama return EC; 926a045b73aSRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 927c2bed429SRui Ueyama return object_error::success; 928c2bed429SRui Ueyama } 929c2bed429SRui Ueyama 930c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getImportLookupEntry( 931c2bed429SRui Ueyama const import_lookup_table_entry32 *&Result) const { 932c2bed429SRui Ueyama uintptr_t IntPtr = 0; 933a045b73aSRui Ueyama if (error_code EC = 934a045b73aSRui Ueyama OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr)) 935a045b73aSRui Ueyama return EC; 936c2bed429SRui Ueyama Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); 937c2bed429SRui Ueyama return object_error::success; 938c2bed429SRui Ueyama } 939c2bed429SRui Ueyama 940ad882ba8SRui Ueyama bool ExportDirectoryEntryRef:: 941ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const { 942ad882ba8SRui Ueyama return ExportTable == Other.ExportTable && Index == Other.Index; 943ad882ba8SRui Ueyama } 944ad882ba8SRui Ueyama 9455e812afaSRafael Espindola void ExportDirectoryEntryRef::moveNext() { 9465e812afaSRafael Espindola ++Index; 947ad882ba8SRui Ueyama } 948ad882ba8SRui Ueyama 949da49d0d4SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 950da49d0d4SRui Ueyama // by ordinal, the empty string is set as a result. 951da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { 952da49d0d4SRui Ueyama uintptr_t IntPtr = 0; 953da49d0d4SRui Ueyama if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) 954da49d0d4SRui Ueyama return EC; 955da49d0d4SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 956da49d0d4SRui Ueyama return object_error::success; 957da49d0d4SRui Ueyama } 958da49d0d4SRui Ueyama 959e5df6095SRui Ueyama // Returns the starting ordinal number. 960e5df6095SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { 961e5df6095SRui Ueyama Result = ExportTable->OrdinalBase; 962e5df6095SRui Ueyama return object_error::success; 963e5df6095SRui Ueyama } 964e5df6095SRui Ueyama 965ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol. 966ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { 967ad882ba8SRui Ueyama Result = ExportTable->OrdinalBase + Index; 968ad882ba8SRui Ueyama return object_error::success; 969ad882ba8SRui Ueyama } 970ad882ba8SRui Ueyama 971ad882ba8SRui Ueyama // Returns the address of the current export symbol. 972ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { 973ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 974ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr( 975ad882ba8SRui Ueyama ExportTable->ExportAddressTableRVA, IntPtr)) 976ad882ba8SRui Ueyama return EC; 97724fc2d64SRui Ueyama const export_address_table_entry *entry = 97824fc2d64SRui Ueyama reinterpret_cast<const export_address_table_entry *>(IntPtr); 979ad882ba8SRui Ueyama Result = entry[Index].ExportRVA; 980ad882ba8SRui Ueyama return object_error::success; 981ad882ba8SRui Ueyama } 982ad882ba8SRui Ueyama 983ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only 984ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result. 985da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { 986ad882ba8SRui Ueyama uintptr_t IntPtr = 0; 987ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr( 988ad882ba8SRui Ueyama ExportTable->OrdinalTableRVA, IntPtr)) 989ad882ba8SRui Ueyama return EC; 990ad882ba8SRui Ueyama const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); 991ad882ba8SRui Ueyama 992ad882ba8SRui Ueyama uint32_t NumEntries = ExportTable->NumberOfNamePointers; 993ad882ba8SRui Ueyama int Offset = 0; 994ad882ba8SRui Ueyama for (const ulittle16_t *I = Start, *E = Start + NumEntries; 995ad882ba8SRui Ueyama I < E; ++I, ++Offset) { 996ad882ba8SRui Ueyama if (*I != Index) 997ad882ba8SRui Ueyama continue; 998ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr( 999ad882ba8SRui Ueyama ExportTable->NamePointerRVA, IntPtr)) 1000ad882ba8SRui Ueyama return EC; 1001ad882ba8SRui Ueyama const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); 1002ad882ba8SRui Ueyama if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) 1003ad882ba8SRui Ueyama return EC; 1004ad882ba8SRui Ueyama Result = StringRef(reinterpret_cast<const char *>(IntPtr)); 1005ad882ba8SRui Ueyama return object_error::success; 1006ad882ba8SRui Ueyama } 1007ad882ba8SRui Ueyama Result = ""; 1008ad882ba8SRui Ueyama return object_error::success; 1009ad882ba8SRui Ueyama } 1010ad882ba8SRui Ueyama 1011afcc3df7SRafael Espindola ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object, 1012afcc3df7SRafael Espindola bool BufferOwned) { 10138ff24d25SRui Ueyama error_code EC; 1014afcc3df7SRafael Espindola OwningPtr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC, BufferOwned)); 1015692410efSRafael Espindola if (EC) 1016692410efSRafael Espindola return EC; 1017692410efSRafael Espindola return Ret.take(); 1018686738e2SRui Ueyama } 1019