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 
908ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolNext(DataRefImpl Ref,
911d6167fdSMichael J. Spencer                                          SymbolRef &Result) const {
928ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
938ff24d25SRui Ueyama   Symb += 1 + Symb->NumberOfAuxSymbols;
948ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Symb);
958ff24d25SRui Ueyama   Result = SymbolRef(Ref, this);
961d6167fdSMichael J. Spencer   return object_error::success;
971d6167fdSMichael J. Spencer }
981d6167fdSMichael J. Spencer 
998ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
1001d6167fdSMichael J. Spencer                                          StringRef &Result) const {
1018ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1028ff24d25SRui Ueyama   return getSymbolName(Symb, Result);
1038e90adafSMichael J. Spencer }
1048e90adafSMichael J. Spencer 
1058ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
1061d6167fdSMichael J. Spencer                                             uint64_t &Result) const {
1078ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1085ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
1098ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1108ff24d25SRui Ueyama     return EC;
111e62ab11fSRafael Espindola 
1128ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1131d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
1141d6167fdSMichael J. Spencer   else if (Section)
1158ff24d25SRui Ueyama     Result = Section->PointerToRawData + Symb->Value;
1161d6167fdSMichael J. Spencer   else
1178ff24d25SRui Ueyama     Result = Symb->Value;
1181d6167fdSMichael J. Spencer   return object_error::success;
1198e90adafSMichael J. Spencer }
1208e90adafSMichael J. Spencer 
1218ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
12275d1cf33SBenjamin Kramer                                             uint64_t &Result) const {
1238ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
12475d1cf33SBenjamin Kramer   const coff_section *Section = NULL;
1258ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1268ff24d25SRui Ueyama     return EC;
127e62ab11fSRafael Espindola 
1288ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
12975d1cf33SBenjamin Kramer     Result = UnknownAddressOrSize;
13075d1cf33SBenjamin Kramer   else if (Section)
1318ff24d25SRui Ueyama     Result = Section->VirtualAddress + Symb->Value;
13275d1cf33SBenjamin Kramer   else
1338ff24d25SRui Ueyama     Result = Symb->Value;
13475d1cf33SBenjamin Kramer   return object_error::success;
13575d1cf33SBenjamin Kramer }
13675d1cf33SBenjamin Kramer 
1378ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
138d3946676SMichael J. Spencer                                          SymbolRef::Type &Result) const {
1398ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
14075d1cf33SBenjamin Kramer   Result = SymbolRef::ST_Other;
1418ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
1428ff24d25SRui Ueyama       Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
1437e4b976cSDavid Meyer     Result = SymbolRef::ST_Unknown;
1445efa665fSRui Ueyama   } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
14575d1cf33SBenjamin Kramer     Result = SymbolRef::ST_Function;
14675d1cf33SBenjamin Kramer   } else {
14706adfac8SRafael Espindola     uint32_t Characteristics = 0;
1488ff24d25SRui Ueyama     if (Symb->SectionNumber > 0) {
14906adfac8SRafael Espindola       const coff_section *Section = NULL;
1508ff24d25SRui Ueyama       if (error_code EC = getSection(Symb->SectionNumber, Section))
1518ff24d25SRui Ueyama         return EC;
15206adfac8SRafael Espindola       Characteristics = Section->Characteristics;
15375d1cf33SBenjamin Kramer     }
15406adfac8SRafael Espindola     if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
15506adfac8SRafael Espindola         ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
15606adfac8SRafael Espindola       Result = SymbolRef::ST_Data;
15775d1cf33SBenjamin Kramer   }
15875d1cf33SBenjamin Kramer   return object_error::success;
15975d1cf33SBenjamin Kramer }
16075d1cf33SBenjamin Kramer 
1618ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFlags(DataRefImpl Ref,
1621df4b84dSDavid Meyer                                           uint32_t &Result) const {
1638ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1641df4b84dSDavid Meyer   Result = SymbolRef::SF_None;
16575d1cf33SBenjamin Kramer 
1667e4b976cSDavid Meyer   // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
1677e4b976cSDavid Meyer 
1688ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
1698ff24d25SRui Ueyama       Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1707e4b976cSDavid Meyer     Result |= SymbolRef::SF_Undefined;
1711df4b84dSDavid Meyer 
1721df4b84dSDavid Meyer   // TODO: This are certainly too restrictive.
1738ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
1741df4b84dSDavid Meyer     Result |= SymbolRef::SF_Global;
1751df4b84dSDavid Meyer 
1768ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
1771df4b84dSDavid Meyer     Result |= SymbolRef::SF_Weak;
1781df4b84dSDavid Meyer 
1798ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
1801df4b84dSDavid Meyer     Result |= SymbolRef::SF_Absolute;
1811df4b84dSDavid Meyer 
18201759754SMichael J. Spencer   return object_error::success;
18301759754SMichael J. Spencer }
18401759754SMichael J. Spencer 
1858ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
1861d6167fdSMichael J. Spencer                                          uint64_t &Result) const {
1878e90adafSMichael J. Spencer   // FIXME: Return the correct size. This requires looking at all the symbols
1888e90adafSMichael J. Spencer   //        in the same section as this symbol, and looking for either the next
1898e90adafSMichael J. Spencer   //        symbol, or the end of the section.
1908ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1915ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
1928ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1938ff24d25SRui Ueyama     return EC;
194e62ab11fSRafael Espindola 
1958ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1961d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
1971d6167fdSMichael J. Spencer   else if (Section)
1988ff24d25SRui Ueyama     Result = Section->SizeOfRawData - Symb->Value;
1991d6167fdSMichael J. Spencer   else
2001d6167fdSMichael J. Spencer     Result = 0;
2011d6167fdSMichael J. Spencer   return object_error::success;
2028e90adafSMichael J. Spencer }
2038e90adafSMichael J. Spencer 
2048ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
20532173153SMichael J. Spencer                                             section_iterator &Result) const {
2068ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
2078ff24d25SRui Ueyama   if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
20832173153SMichael J. Spencer     Result = end_sections();
20932173153SMichael J. Spencer   else {
2108ff24d25SRui Ueyama     const coff_section *Sec = 0;
2118ff24d25SRui Ueyama     if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
2128ff24d25SRui Ueyama     DataRefImpl Ref;
2138ff24d25SRui Ueyama     Ref.p = reinterpret_cast<uintptr_t>(Sec);
2148ff24d25SRui Ueyama     Result = section_iterator(SectionRef(Ref, this));
21532173153SMichael J. Spencer   }
21632173153SMichael J. Spencer   return object_error::success;
21732173153SMichael J. Spencer }
21832173153SMichael J. Spencer 
2198ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
2204f223bf7STim Northover                                           uint64_t &Val) const {
2214f223bf7STim Northover   report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
2224f223bf7STim Northover }
2234f223bf7STim Northover 
2248ff24d25SRui Ueyama error_code COFFObjectFile::getSectionNext(DataRefImpl Ref,
2251d6167fdSMichael J. Spencer                                           SectionRef &Result) const {
2268ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2278ff24d25SRui Ueyama   Sec += 1;
2288ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Sec);
2298ff24d25SRui Ueyama   Result = SectionRef(Ref, this);
2301d6167fdSMichael J. Spencer   return object_error::success;
2318e90adafSMichael J. Spencer }
2328e90adafSMichael J. Spencer 
2338ff24d25SRui Ueyama error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
2341d6167fdSMichael J. Spencer                                           StringRef &Result) const {
2358ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2368ff24d25SRui Ueyama   return getSectionName(Sec, Result);
2378e90adafSMichael J. Spencer }
2388e90adafSMichael J. Spencer 
2398ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
2401d6167fdSMichael J. Spencer                                              uint64_t &Result) const {
2418ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2428ff24d25SRui Ueyama   Result = Sec->VirtualAddress;
2431d6167fdSMichael J. Spencer   return object_error::success;
2448e90adafSMichael J. Spencer }
2458e90adafSMichael J. Spencer 
2468ff24d25SRui Ueyama error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
2471d6167fdSMichael J. Spencer                                           uint64_t &Result) const {
2488ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2498ff24d25SRui Ueyama   Result = Sec->SizeOfRawData;
2501d6167fdSMichael J. Spencer   return object_error::success;
2518e90adafSMichael J. Spencer }
2528e90adafSMichael J. Spencer 
2538ff24d25SRui Ueyama error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
2541d6167fdSMichael J. Spencer                                               StringRef &Result) const {
2558ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2569da9e693SMichael J. Spencer   ArrayRef<uint8_t> Res;
2578ff24d25SRui Ueyama   error_code EC = getSectionContents(Sec, Res);
2589da9e693SMichael J. Spencer   Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
2599da9e693SMichael J. Spencer   return EC;
2608e90adafSMichael J. Spencer }
2618e90adafSMichael J. Spencer 
2628ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
2637989460aSMichael J. Spencer                                                uint64_t &Res) const {
2648ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2658ff24d25SRui Ueyama   if (!Sec)
2667989460aSMichael J. Spencer     return object_error::parse_failed;
2678ff24d25SRui Ueyama   Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
2687989460aSMichael J. Spencer   return object_error::success;
2697989460aSMichael J. Spencer }
2707989460aSMichael J. Spencer 
2718ff24d25SRui Ueyama error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
2721d6167fdSMichael J. Spencer                                          bool &Result) const {
2738ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2748ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
2751d6167fdSMichael J. Spencer   return object_error::success;
2768e90adafSMichael J. Spencer }
2778e90adafSMichael J. Spencer 
2788ff24d25SRui Ueyama error_code COFFObjectFile::isSectionData(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_INITIALIZED_DATA;
282800619f2SMichael J. Spencer   return object_error::success;
283800619f2SMichael J. Spencer }
284800619f2SMichael J. Spencer 
2858ff24d25SRui Ueyama error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
286800619f2SMichael J. Spencer                                         bool &Result) const {
2878ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2888ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
289800619f2SMichael J. Spencer   return object_error::success;
290800619f2SMichael J. Spencer }
291800619f2SMichael J. Spencer 
2928ff24d25SRui Ueyama error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
2932138ef6dSPreston Gurd                                                          bool &Result) const {
2942138ef6dSPreston Gurd   // FIXME: Unimplemented
2952138ef6dSPreston Gurd   Result = true;
2962138ef6dSPreston Gurd   return object_error::success;
2972138ef6dSPreston Gurd }
2982138ef6dSPreston Gurd 
2998ff24d25SRui Ueyama error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
3002138ef6dSPreston Gurd                                            bool &Result) const {
3018ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3028ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
3032138ef6dSPreston Gurd   return object_error::success;
3042138ef6dSPreston Gurd }
3052138ef6dSPreston Gurd 
3068ff24d25SRui Ueyama error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
3072138ef6dSPreston Gurd                                              bool &Result) const {
308b96a320aSAndrew Kaylor   // FIXME: Unimplemented.
3092138ef6dSPreston Gurd   Result = false;
3102138ef6dSPreston Gurd   return object_error::success;
3112138ef6dSPreston Gurd }
3122138ef6dSPreston Gurd 
3138ff24d25SRui Ueyama error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
3143f31fa05SAndrew Kaylor                                                 bool &Result) const {
3153f31fa05SAndrew Kaylor   // FIXME: Unimplemented.
3163f31fa05SAndrew Kaylor   Result = false;
3173f31fa05SAndrew Kaylor   return object_error::success;
3183f31fa05SAndrew Kaylor }
3193f31fa05SAndrew Kaylor 
3208ff24d25SRui Ueyama error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
3218ff24d25SRui Ueyama                                                  DataRefImpl SymbRef,
322f6f3e81cSBenjamin Kramer                                                  bool &Result) const {
3238ff24d25SRui Ueyama   const coff_section *Sec = toSec(SecRef);
3248ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(SymbRef);
3258ff24d25SRui Ueyama   const coff_section *SymbSec = 0;
3268ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
3278ff24d25SRui Ueyama   if (SymbSec == Sec)
3289a28851eSMichael J. Spencer     Result = true;
3299a28851eSMichael J. Spencer   else
330f6f3e81cSBenjamin Kramer     Result = false;
331f6f3e81cSBenjamin Kramer   return object_error::success;
332f6f3e81cSBenjamin Kramer }
333f6f3e81cSBenjamin Kramer 
3348ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
3358ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3368ff24d25SRui Ueyama   DataRefImpl Ret;
3378ff24d25SRui Ueyama   if (Sec->NumberOfRelocations == 0)
3388ff24d25SRui Ueyama     Ret.p = 0;
339e5fd0047SMichael J. Spencer   else
3408ff24d25SRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations);
341e5fd0047SMichael J. Spencer 
3428ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
343e5fd0047SMichael J. Spencer }
344e5fd0047SMichael J. Spencer 
3458ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
3468ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3478ff24d25SRui Ueyama   DataRefImpl Ret;
3488ff24d25SRui Ueyama   if (Sec->NumberOfRelocations == 0)
3498ff24d25SRui Ueyama     Ret.p = 0;
350e5fd0047SMichael J. Spencer   else
3518ff24d25SRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(
352e5fd0047SMichael J. Spencer               reinterpret_cast<const coff_relocation*>(
3538ff24d25SRui Ueyama                 base() + Sec->PointerToRelocations)
3548ff24d25SRui Ueyama               + Sec->NumberOfRelocations);
355e5fd0047SMichael J. Spencer 
3568ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
357e5fd0047SMichael J. Spencer }
358e5fd0047SMichael J. Spencer 
359c2bed429SRui Ueyama // Initialize the pointer to the symbol table.
360c2bed429SRui Ueyama error_code COFFObjectFile::initSymbolTablePtr() {
3618ff24d25SRui Ueyama   if (error_code EC = getObject(
362c2bed429SRui Ueyama           SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
363c2bed429SRui Ueyama           COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
3648ff24d25SRui Ueyama     return EC;
365c2bed429SRui Ueyama 
366c2bed429SRui Ueyama   // Find string table. The first four byte of the string table contains the
367c2bed429SRui Ueyama   // total size of the string table, including the size field itself. If the
368c2bed429SRui Ueyama   // string table is empty, the value of the first four byte would be 4.
369c2bed429SRui Ueyama   const uint8_t *StringTableAddr =
370c2bed429SRui Ueyama       base() + COFFHeader->PointerToSymbolTable +
371c2bed429SRui Ueyama       COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
372c2bed429SRui Ueyama   const ulittle32_t *StringTableSizePtr;
3738ff24d25SRui Ueyama   if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
3748ff24d25SRui Ueyama     return EC;
375c2bed429SRui Ueyama   StringTableSize = *StringTableSizePtr;
3768ff24d25SRui Ueyama   if (error_code EC =
377c2bed429SRui Ueyama       getObject(StringTable, Data, StringTableAddr, StringTableSize))
3788ff24d25SRui Ueyama     return EC;
379c2bed429SRui Ueyama 
380c2bed429SRui Ueyama   // Check that the string table is null terminated if has any in it.
381c2bed429SRui Ueyama   if (StringTableSize < 4 ||
382c2bed429SRui Ueyama       (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
383c2bed429SRui Ueyama     return  object_error::parse_failed;
384c2bed429SRui Ueyama   return object_error::success;
385c2bed429SRui Ueyama }
386c2bed429SRui Ueyama 
387c2bed429SRui Ueyama // Returns the file offset for the given RVA.
388c2bed429SRui Ueyama error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
3898ff24d25SRui Ueyama   error_code EC;
3908ff24d25SRui Ueyama   for (section_iterator I = begin_sections(), E = end_sections(); I != E;
3918ff24d25SRui Ueyama        I.increment(EC)) {
3928ff24d25SRui Ueyama     if (EC)
3938ff24d25SRui Ueyama       return EC;
3948ff24d25SRui Ueyama     const coff_section *Section = getCOFFSection(I);
395c2bed429SRui Ueyama     uint32_t SectionStart = Section->VirtualAddress;
396c2bed429SRui Ueyama     uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
397c2bed429SRui Ueyama     if (SectionStart <= Rva && Rva < SectionEnd) {
398c2bed429SRui Ueyama       uint32_t Offset = Rva - SectionStart;
399c2bed429SRui Ueyama       Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
400c2bed429SRui Ueyama       return object_error::success;
401c2bed429SRui Ueyama     }
402c2bed429SRui Ueyama   }
403c2bed429SRui Ueyama   return object_error::parse_failed;
404c2bed429SRui Ueyama }
405c2bed429SRui Ueyama 
406c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
407c2bed429SRui Ueyama // table entry.
408c2bed429SRui Ueyama error_code COFFObjectFile::
409c2bed429SRui Ueyama getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
410c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
4118ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(Rva, IntPtr))
4128ff24d25SRui Ueyama     return EC;
413c2bed429SRui Ueyama   const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
414c2bed429SRui Ueyama   Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
415c2bed429SRui Ueyama   Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
416c2bed429SRui Ueyama   return object_error::success;
417c2bed429SRui Ueyama }
418c2bed429SRui Ueyama 
419c2bed429SRui Ueyama // Find the import table.
420c2bed429SRui Ueyama error_code COFFObjectFile::initImportTablePtr() {
421c2bed429SRui Ueyama   // First, we get the RVA of the import table. If the file lacks a pointer to
422c2bed429SRui Ueyama   // the import table, do nothing.
423c2bed429SRui Ueyama   const data_directory *DataEntry;
424c2bed429SRui Ueyama   if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
425c2bed429SRui Ueyama     return object_error::success;
426c2bed429SRui Ueyama 
427c2bed429SRui Ueyama   // Do nothing if the pointer to import table is NULL.
428c2bed429SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
429c2bed429SRui Ueyama     return object_error::success;
430c2bed429SRui Ueyama 
431c2bed429SRui Ueyama   uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
432c2bed429SRui Ueyama   NumberOfImportDirectory = DataEntry->Size /
433c2bed429SRui Ueyama       sizeof(import_directory_table_entry);
434c2bed429SRui Ueyama 
435c2bed429SRui Ueyama   // Find the section that contains the RVA. This is needed because the RVA is
436c2bed429SRui Ueyama   // the import table's memory address which is different from its file offset.
437c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
4388ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
4398ff24d25SRui Ueyama     return EC;
440c2bed429SRui Ueyama   ImportDirectory = reinterpret_cast<
441c2bed429SRui Ueyama       const import_directory_table_entry *>(IntPtr);
442ad882ba8SRui Ueyama   return object_error::success;
443ad882ba8SRui Ueyama }
444c2bed429SRui Ueyama 
445ad882ba8SRui Ueyama // Find the export table.
446ad882ba8SRui Ueyama error_code COFFObjectFile::initExportTablePtr() {
447ad882ba8SRui Ueyama   // First, we get the RVA of the export table. If the file lacks a pointer to
448ad882ba8SRui Ueyama   // the export table, do nothing.
449ad882ba8SRui Ueyama   const data_directory *DataEntry;
450ad882ba8SRui Ueyama   if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
451ad882ba8SRui Ueyama     return object_error::success;
452ad882ba8SRui Ueyama 
453ad882ba8SRui Ueyama   // Do nothing if the pointer to export table is NULL.
454ad882ba8SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
455ad882ba8SRui Ueyama     return object_error::success;
456ad882ba8SRui Ueyama 
457ad882ba8SRui Ueyama   uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
458ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
459ad882ba8SRui Ueyama   if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
460ad882ba8SRui Ueyama     return EC;
46124fc2d64SRui Ueyama   ExportDirectory =
46224fc2d64SRui Ueyama       reinterpret_cast<const export_directory_table_entry *>(IntPtr);
463ad882ba8SRui Ueyama   return object_error::success;
464c2bed429SRui Ueyama }
465c2bed429SRui Ueyama 
466afcc3df7SRafael Espindola COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
467afcc3df7SRafael Espindola                                bool BufferOwned)
468afcc3df7SRafael Espindola     : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0),
469*10ed9ddcSRui Ueyama       PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0),
470*10ed9ddcSRui Ueyama       SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0),
471afcc3df7SRafael Espindola       NumberOfImportDirectory(0), ExportDirectory(0) {
4721d6167fdSMichael J. Spencer   // Check that we at least have enough room for a header.
4738ff24d25SRui Ueyama   if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
474ee066fc4SEric Christopher 
47582ebd8e3SRui Ueyama   // The current location in the file where we are looking at.
47682ebd8e3SRui Ueyama   uint64_t CurPtr = 0;
47782ebd8e3SRui Ueyama 
47882ebd8e3SRui Ueyama   // PE header is optional and is present only in executables. If it exists,
47982ebd8e3SRui Ueyama   // it is placed right after COFF header.
4808ff24d25SRui Ueyama   bool HasPEHeader = false;
481ee066fc4SEric Christopher 
4821d6167fdSMichael J. Spencer   // Check if this is a PE/COFF file.
483ec29b121SMichael J. Spencer   if (base()[0] == 0x4d && base()[1] == 0x5a) {
484ee066fc4SEric Christopher     // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
485ee066fc4SEric Christopher     // PE signature to find 'normal' COFF header.
4868ff24d25SRui Ueyama     if (!checkSize(Data, EC, 0x3c + 8)) return;
48782ebd8e3SRui Ueyama     CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
48882ebd8e3SRui Ueyama     // Check the PE magic bytes. ("PE\0\0")
48982ebd8e3SRui Ueyama     if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
4908ff24d25SRui Ueyama       EC = object_error::parse_failed;
4911d6167fdSMichael J. Spencer       return;
4921d6167fdSMichael J. Spencer     }
49382ebd8e3SRui Ueyama     CurPtr += 4; // Skip the PE magic bytes.
4948ff24d25SRui Ueyama     HasPEHeader = true;
495ee066fc4SEric Christopher   }
496ee066fc4SEric Christopher 
4978ff24d25SRui Ueyama   if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
4981d6167fdSMichael J. Spencer     return;
49982ebd8e3SRui Ueyama   CurPtr += sizeof(coff_file_header);
50082ebd8e3SRui Ueyama 
5018ff24d25SRui Ueyama   if (HasPEHeader) {
502*10ed9ddcSRui Ueyama     const pe32_header *Header;
503*10ed9ddcSRui Ueyama     if ((EC = getObject(Header, Data, base() + CurPtr)))
50482ebd8e3SRui Ueyama       return;
505*10ed9ddcSRui Ueyama 
506*10ed9ddcSRui Ueyama     const uint8_t *DataDirAddr;
507*10ed9ddcSRui Ueyama     uint64_t DataDirSize;
508*10ed9ddcSRui Ueyama     if (Header->Magic == 0x10b) {
509*10ed9ddcSRui Ueyama       PE32Header = Header;
510*10ed9ddcSRui Ueyama       DataDirAddr = base() + CurPtr + sizeof(pe32_header);
511*10ed9ddcSRui Ueyama       DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
512*10ed9ddcSRui Ueyama     } else if (Header->Magic == 0x20b) {
513*10ed9ddcSRui Ueyama       PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
514*10ed9ddcSRui Ueyama       DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
515*10ed9ddcSRui Ueyama       DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
516*10ed9ddcSRui Ueyama     } else {
517*10ed9ddcSRui Ueyama       // It's neither PE32 nor PE32+.
518*10ed9ddcSRui Ueyama       EC = object_error::parse_failed;
519ed64342bSRui Ueyama       return;
520ed64342bSRui Ueyama     }
521*10ed9ddcSRui Ueyama     if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
522*10ed9ddcSRui Ueyama       return;
52382ebd8e3SRui Ueyama     CurPtr += COFFHeader->SizeOfOptionalHeader;
52482ebd8e3SRui Ueyama   }
5251d6167fdSMichael J. Spencer 
526692410efSRafael Espindola   if (COFFHeader->isImportLibrary())
527692410efSRafael Espindola     return;
528692410efSRafael Espindola 
5298ff24d25SRui Ueyama   if ((EC = getObject(SectionTable, Data, base() + CurPtr,
530ed64342bSRui Ueyama                       COFFHeader->NumberOfSections * sizeof(coff_section))))
5311d6167fdSMichael J. Spencer     return;
5321d6167fdSMichael J. Spencer 
533c2bed429SRui Ueyama   // Initialize the pointer to the symbol table.
534c2bed429SRui Ueyama   if (COFFHeader->PointerToSymbolTable != 0)
5358ff24d25SRui Ueyama     if ((EC = initSymbolTablePtr()))
5361d6167fdSMichael J. Spencer       return;
5378e90adafSMichael J. Spencer 
538c2bed429SRui Ueyama   // Initialize the pointer to the beginning of the import table.
5398ff24d25SRui Ueyama   if ((EC = initImportTablePtr()))
540ed64342bSRui Ueyama     return;
5411d6167fdSMichael J. Spencer 
542ad882ba8SRui Ueyama   // Initialize the pointer to the export table.
5438ff24d25SRui Ueyama   if ((EC = initExportTablePtr()))
544ad882ba8SRui Ueyama     return;
545ad882ba8SRui Ueyama 
5468ff24d25SRui Ueyama   EC = object_error::success;
5478e90adafSMichael J. Spencer }
5488e90adafSMichael J. Spencer 
549e5fd0047SMichael J. Spencer symbol_iterator COFFObjectFile::begin_symbols() const {
5508ff24d25SRui Ueyama   DataRefImpl Ret;
5518ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
5528ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ret, this));
5538e90adafSMichael J. Spencer }
5548e90adafSMichael J. Spencer 
555e5fd0047SMichael J. Spencer symbol_iterator COFFObjectFile::end_symbols() const {
5568e90adafSMichael J. Spencer   // The symbol table ends where the string table begins.
5578ff24d25SRui Ueyama   DataRefImpl Ret;
5588ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(StringTable);
5598ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ret, this));
5608e90adafSMichael J. Spencer }
5618e90adafSMichael J. Spencer 
5628c4729fdSMichael J. Spencer symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
5638c4729fdSMichael J. Spencer   // TODO: implement
5648c4729fdSMichael J. Spencer   report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
5658c4729fdSMichael J. Spencer }
5668c4729fdSMichael J. Spencer 
5678c4729fdSMichael J. Spencer symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
5688c4729fdSMichael J. Spencer   // TODO: implement
5698c4729fdSMichael J. Spencer   report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
5708c4729fdSMichael J. Spencer }
5718c4729fdSMichael J. Spencer 
5722fc34c5fSDavid Meyer library_iterator COFFObjectFile::begin_libraries_needed() const {
5732fc34c5fSDavid Meyer   // TODO: implement
5742fc34c5fSDavid Meyer   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
5752fc34c5fSDavid Meyer }
5762fc34c5fSDavid Meyer 
5772fc34c5fSDavid Meyer library_iterator COFFObjectFile::end_libraries_needed() const {
5782fc34c5fSDavid Meyer   // TODO: implement
5792fc34c5fSDavid Meyer   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
5802fc34c5fSDavid Meyer }
5812fc34c5fSDavid Meyer 
582c429b80dSDavid Meyer StringRef COFFObjectFile::getLoadName() const {
583c429b80dSDavid Meyer   // COFF does not have this field.
584c429b80dSDavid Meyer   return "";
585c429b80dSDavid Meyer }
586c429b80dSDavid Meyer 
587bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_begin() const {
588a045b73aSRui Ueyama   return import_directory_iterator(
589a045b73aSRui Ueyama       ImportDirectoryEntryRef(ImportDirectory, 0, this));
590c2bed429SRui Ueyama }
591c2bed429SRui Ueyama 
592bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_end() const {
593a045b73aSRui Ueyama   return import_directory_iterator(
594a045b73aSRui Ueyama       ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
595c2bed429SRui Ueyama }
596c429b80dSDavid Meyer 
597ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_begin() const {
598ad882ba8SRui Ueyama   return export_directory_iterator(
599ad882ba8SRui Ueyama       ExportDirectoryEntryRef(ExportDirectory, 0, this));
600ad882ba8SRui Ueyama }
601ad882ba8SRui Ueyama 
602ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_end() const {
603ad882ba8SRui Ueyama   if (ExportDirectory == 0)
604ad882ba8SRui Ueyama     return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
6058ff24d25SRui Ueyama   ExportDirectoryEntryRef Ref(ExportDirectory,
606ad882ba8SRui Ueyama                               ExportDirectory->AddressTableEntries, this);
6078ff24d25SRui Ueyama   return export_directory_iterator(Ref);
608ad882ba8SRui Ueyama }
609ad882ba8SRui Ueyama 
610e5fd0047SMichael J. Spencer section_iterator COFFObjectFile::begin_sections() const {
6118ff24d25SRui Ueyama   DataRefImpl Ret;
6128ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
6138ff24d25SRui Ueyama   return section_iterator(SectionRef(Ret, this));
6148e90adafSMichael J. Spencer }
6158e90adafSMichael J. Spencer 
616e5fd0047SMichael J. Spencer section_iterator COFFObjectFile::end_sections() const {
6178ff24d25SRui Ueyama   DataRefImpl Ret;
6188ff24d25SRui Ueyama   int NumSections = COFFHeader->isImportLibrary()
61915ba1e20SRui Ueyama       ? 0 : COFFHeader->NumberOfSections;
6208ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
6218ff24d25SRui Ueyama   return section_iterator(SectionRef(Ret, this));
6228e90adafSMichael J. Spencer }
6238e90adafSMichael J. Spencer 
6248e90adafSMichael J. Spencer uint8_t COFFObjectFile::getBytesInAddress() const {
6250324b672SMichael J. Spencer   return getArch() == Triple::x86_64 ? 8 : 4;
6268e90adafSMichael J. Spencer }
6278e90adafSMichael J. Spencer 
6288e90adafSMichael J. Spencer StringRef COFFObjectFile::getFileFormatName() const {
62982ebd8e3SRui Ueyama   switch(COFFHeader->Machine) {
6308e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
6318e90adafSMichael J. Spencer     return "COFF-i386";
6328e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
6338e90adafSMichael J. Spencer     return "COFF-x86-64";
6348e90adafSMichael J. Spencer   default:
6358e90adafSMichael J. Spencer     return "COFF-<unknown arch>";
6368e90adafSMichael J. Spencer   }
6378e90adafSMichael J. Spencer }
6388e90adafSMichael J. Spencer 
6398e90adafSMichael J. Spencer unsigned COFFObjectFile::getArch() const {
64082ebd8e3SRui Ueyama   switch(COFFHeader->Machine) {
6418e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
6428e90adafSMichael J. Spencer     return Triple::x86;
6438e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
6448e90adafSMichael J. Spencer     return Triple::x86_64;
6458e90adafSMichael J. Spencer   default:
6468e90adafSMichael J. Spencer     return Triple::UnknownArch;
6478e90adafSMichael J. Spencer   }
6488e90adafSMichael J. Spencer }
6498e90adafSMichael J. Spencer 
65082ebd8e3SRui Ueyama // This method is kept here because lld uses this. As soon as we make
65182ebd8e3SRui Ueyama // lld to use getCOFFHeader, this method will be removed.
65289a7a5eaSMichael J. Spencer error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
65382ebd8e3SRui Ueyama   return getCOFFHeader(Res);
65482ebd8e3SRui Ueyama }
65582ebd8e3SRui Ueyama 
65682ebd8e3SRui Ueyama error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
65782ebd8e3SRui Ueyama   Res = COFFHeader;
65882ebd8e3SRui Ueyama   return object_error::success;
65982ebd8e3SRui Ueyama }
66082ebd8e3SRui Ueyama 
66182ebd8e3SRui Ueyama error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
66282ebd8e3SRui Ueyama   Res = PE32Header;
66389a7a5eaSMichael J. Spencer   return object_error::success;
66489a7a5eaSMichael J. Spencer }
66589a7a5eaSMichael J. Spencer 
666*10ed9ddcSRui Ueyama error_code
667*10ed9ddcSRui Ueyama COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
668*10ed9ddcSRui Ueyama   Res = PE32PlusHeader;
669*10ed9ddcSRui Ueyama   return object_error::success;
670*10ed9ddcSRui Ueyama }
671*10ed9ddcSRui Ueyama 
6728ff24d25SRui Ueyama error_code COFFObjectFile::getDataDirectory(uint32_t Index,
673ed64342bSRui Ueyama                                             const data_directory *&Res) const {
674ed64342bSRui Ueyama   // Error if if there's no data directory or the index is out of range.
675*10ed9ddcSRui Ueyama   if (!DataDirectory)
676*10ed9ddcSRui Ueyama     return object_error::parse_failed;
677*10ed9ddcSRui Ueyama   assert(PE32Header || PE32PlusHeader);
678*10ed9ddcSRui Ueyama   uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
679*10ed9ddcSRui Ueyama                                : PE32PlusHeader->NumberOfRvaAndSize;
680*10ed9ddcSRui Ueyama   if (Index > NumEnt)
681ed64342bSRui Ueyama     return object_error::parse_failed;
6828ff24d25SRui Ueyama   Res = &DataDirectory[Index];
683ed64342bSRui Ueyama   return object_error::success;
684ed64342bSRui Ueyama }
685ed64342bSRui Ueyama 
6868ff24d25SRui Ueyama error_code COFFObjectFile::getSection(int32_t Index,
6871d6167fdSMichael J. Spencer                                       const coff_section *&Result) const {
6881d6167fdSMichael J. Spencer   // Check for special index values.
6898ff24d25SRui Ueyama   if (Index == COFF::IMAGE_SYM_UNDEFINED ||
6908ff24d25SRui Ueyama       Index == COFF::IMAGE_SYM_ABSOLUTE ||
6918ff24d25SRui Ueyama       Index == COFF::IMAGE_SYM_DEBUG)
6921d6167fdSMichael J. Spencer     Result = NULL;
6938ff24d25SRui Ueyama   else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
6941d6167fdSMichael J. Spencer     // We already verified the section table data, so no need to check again.
6958ff24d25SRui Ueyama     Result = SectionTable + (Index - 1);
6961d6167fdSMichael J. Spencer   else
6971d6167fdSMichael J. Spencer     return object_error::parse_failed;
6981d6167fdSMichael J. Spencer   return object_error::success;
6998e90adafSMichael J. Spencer }
7008e90adafSMichael J. Spencer 
7018ff24d25SRui Ueyama error_code COFFObjectFile::getString(uint32_t Offset,
7021d6167fdSMichael J. Spencer                                      StringRef &Result) const {
7031d6167fdSMichael J. Spencer   if (StringTableSize <= 4)
7041d6167fdSMichael J. Spencer     // Tried to get a string from an empty string table.
7051d6167fdSMichael J. Spencer     return object_error::parse_failed;
7068ff24d25SRui Ueyama   if (Offset >= StringTableSize)
7071d6167fdSMichael J. Spencer     return object_error::unexpected_eof;
7088ff24d25SRui Ueyama   Result = StringRef(StringTable + Offset);
7091d6167fdSMichael J. Spencer   return object_error::success;
7108e90adafSMichael J. Spencer }
711022ecdf2SBenjamin Kramer 
7128ff24d25SRui Ueyama error_code COFFObjectFile::getSymbol(uint32_t Index,
713e5fd0047SMichael J. Spencer                                      const coff_symbol *&Result) const {
7148ff24d25SRui Ueyama   if (Index < COFFHeader->NumberOfSymbols)
7158ff24d25SRui Ueyama     Result = SymbolTable + Index;
716e5fd0047SMichael J. Spencer   else
717e5fd0047SMichael J. Spencer     return object_error::parse_failed;
718e5fd0047SMichael J. Spencer   return object_error::success;
719e5fd0047SMichael J. Spencer }
720e5fd0047SMichael J. Spencer 
7218ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
72289a7a5eaSMichael J. Spencer                                          StringRef &Res) const {
72389a7a5eaSMichael J. Spencer   // Check for string table entry. First 4 bytes are 0.
7248ff24d25SRui Ueyama   if (Symbol->Name.Offset.Zeroes == 0) {
7258ff24d25SRui Ueyama     uint32_t Offset = Symbol->Name.Offset.Offset;
7268ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Res))
7278ff24d25SRui Ueyama       return EC;
72889a7a5eaSMichael J. Spencer     return object_error::success;
72989a7a5eaSMichael J. Spencer   }
73089a7a5eaSMichael J. Spencer 
7318ff24d25SRui Ueyama   if (Symbol->Name.ShortName[7] == 0)
73289a7a5eaSMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
7338ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName);
73489a7a5eaSMichael J. Spencer   else
73589a7a5eaSMichael J. Spencer     // Not null terminated, use all 8 bytes.
7368ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName, 8);
73789a7a5eaSMichael J. Spencer   return object_error::success;
73889a7a5eaSMichael J. Spencer }
73989a7a5eaSMichael J. Spencer 
74071757ef3SMarshall Clow ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
7418ff24d25SRui Ueyama                                   const coff_symbol *Symbol) const {
7428ff24d25SRui Ueyama   const uint8_t *Aux = NULL;
74371757ef3SMarshall Clow 
7448ff24d25SRui Ueyama   if (Symbol->NumberOfAuxSymbols > 0) {
74571757ef3SMarshall Clow   // AUX data comes immediately after the symbol in COFF
7468ff24d25SRui Ueyama     Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
74771757ef3SMarshall Clow # ifndef NDEBUG
7488ff24d25SRui Ueyama     // Verify that the Aux symbol points to a valid entry in the symbol table.
7498ff24d25SRui Ueyama     uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
7508ff24d25SRui Ueyama     if (Offset < COFFHeader->PointerToSymbolTable
7518ff24d25SRui Ueyama         || Offset >= COFFHeader->PointerToSymbolTable
75282ebd8e3SRui Ueyama            + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
75371757ef3SMarshall Clow       report_fatal_error("Aux Symbol data was outside of symbol table.");
75471757ef3SMarshall Clow 
7558ff24d25SRui Ueyama     assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
75671757ef3SMarshall Clow          == 0 && "Aux Symbol data did not point to the beginning of a symbol");
75771757ef3SMarshall Clow # endif
758bfb85e67SMarshall Clow   }
75924fc2d64SRui Ueyama   return ArrayRef<uint8_t>(Aux,
76024fc2d64SRui Ueyama                            Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
76171757ef3SMarshall Clow }
76271757ef3SMarshall Clow 
76353c2d547SMichael J. Spencer error_code COFFObjectFile::getSectionName(const coff_section *Sec,
76453c2d547SMichael J. Spencer                                           StringRef &Res) const {
76553c2d547SMichael J. Spencer   StringRef Name;
76653c2d547SMichael J. Spencer   if (Sec->Name[7] == 0)
76753c2d547SMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
76853c2d547SMichael J. Spencer     Name = Sec->Name;
76953c2d547SMichael J. Spencer   else
77053c2d547SMichael J. Spencer     // Not null terminated, use all 8 bytes.
77153c2d547SMichael J. Spencer     Name = StringRef(Sec->Name, 8);
77253c2d547SMichael J. Spencer 
77353c2d547SMichael J. Spencer   // Check for string table entry. First byte is '/'.
77453c2d547SMichael J. Spencer   if (Name[0] == '/') {
77553c2d547SMichael J. Spencer     uint32_t Offset;
77653c2d547SMichael J. Spencer     if (Name.substr(1).getAsInteger(10, Offset))
77753c2d547SMichael J. Spencer       return object_error::parse_failed;
7788ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Name))
7798ff24d25SRui Ueyama       return EC;
78053c2d547SMichael J. Spencer   }
78153c2d547SMichael J. Spencer 
78253c2d547SMichael J. Spencer   Res = Name;
78353c2d547SMichael J. Spencer   return object_error::success;
78453c2d547SMichael J. Spencer }
78553c2d547SMichael J. Spencer 
7869da9e693SMichael J. Spencer error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
7879da9e693SMichael J. Spencer                                               ArrayRef<uint8_t> &Res) const {
7889da9e693SMichael J. Spencer   // The only thing that we need to verify is that the contents is contained
7899da9e693SMichael J. Spencer   // within the file bounds. We don't need to make sure it doesn't cover other
7909da9e693SMichael J. Spencer   // data, as there's nothing that says that is not allowed.
7919da9e693SMichael J. Spencer   uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
7929da9e693SMichael J. Spencer   uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
7939da9e693SMichael J. Spencer   if (ConEnd > uintptr_t(Data->getBufferEnd()))
7949da9e693SMichael J. Spencer     return object_error::parse_failed;
7959da9e693SMichael J. Spencer   Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
7969da9e693SMichael J. Spencer                           Sec->SizeOfRawData);
7979da9e693SMichael J. Spencer   return object_error::success;
7989da9e693SMichael J. Spencer }
7999da9e693SMichael J. Spencer 
800022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
801e5fd0047SMichael J. Spencer   return reinterpret_cast<const coff_relocation*>(Rel.p);
802022ecdf2SBenjamin Kramer }
8038ff24d25SRui Ueyama 
804022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
805022ecdf2SBenjamin Kramer                                              RelocationRef &Res) const {
806e5fd0047SMichael J. Spencer   Rel.p = reinterpret_cast<uintptr_t>(
807e5fd0047SMichael J. Spencer             reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
808022ecdf2SBenjamin Kramer   Res = RelocationRef(Rel, this);
809022ecdf2SBenjamin Kramer   return object_error::success;
810022ecdf2SBenjamin Kramer }
8118ff24d25SRui Ueyama 
812022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
813022ecdf2SBenjamin Kramer                                                 uint64_t &Res) const {
8141e483879SRafael Espindola   report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
815022ecdf2SBenjamin Kramer }
8168ff24d25SRui Ueyama 
817cbe72fc9SDanil Malyshev error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
818cbe72fc9SDanil Malyshev                                                uint64_t &Res) const {
819cbe72fc9SDanil Malyshev   Res = toRel(Rel)->VirtualAddress;
820cbe72fc9SDanil Malyshev   return object_error::success;
821cbe72fc9SDanil Malyshev }
8228ff24d25SRui Ueyama 
823806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
824022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
8258ff24d25SRui Ueyama   DataRefImpl Ref;
8268ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
8278ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ref, this));
828022ecdf2SBenjamin Kramer }
8298ff24d25SRui Ueyama 
830022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
8317be76590SOwen Anderson                                              uint64_t &Res) const {
832022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
833022ecdf2SBenjamin Kramer   Res = R->Type;
834022ecdf2SBenjamin Kramer   return object_error::success;
835022ecdf2SBenjamin Kramer }
836e5fd0047SMichael J. Spencer 
83771757ef3SMarshall Clow const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
83871757ef3SMarshall Clow   return toSec(It->getRawDataRefImpl());
83971757ef3SMarshall Clow }
84071757ef3SMarshall Clow 
84171757ef3SMarshall Clow const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
84271757ef3SMarshall Clow   return toSymb(It->getRawDataRefImpl());
84371757ef3SMarshall Clow }
84471757ef3SMarshall Clow 
845d3e2a76cSMarshall Clow const coff_relocation *COFFObjectFile::getCOFFRelocation(
846d3e2a76cSMarshall Clow                                              relocation_iterator &It) const {
847d3e2a76cSMarshall Clow   return toRel(It->getRawDataRefImpl());
848d3e2a76cSMarshall Clow }
849d3e2a76cSMarshall Clow 
850e5fd0047SMichael J. Spencer #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
8518ff24d25SRui Ueyama   case COFF::enum: Res = #enum; break;
852e5fd0047SMichael J. Spencer 
853e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
854e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
8558ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
8568ff24d25SRui Ueyama   StringRef Res;
85782ebd8e3SRui Ueyama   switch (COFFHeader->Machine) {
858e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
8598ff24d25SRui Ueyama     switch (Reloc->Type) {
860e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
861e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
862e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
863e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
864e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
865e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
866e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
867e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
868e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
869e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
870e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
871e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
872e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
873e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
874e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
875e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
876e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
877e5fd0047SMichael J. Spencer     default:
8788ff24d25SRui Ueyama       Res = "Unknown";
879e5fd0047SMichael J. Spencer     }
880e5fd0047SMichael J. Spencer     break;
881e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
8828ff24d25SRui Ueyama     switch (Reloc->Type) {
883e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
884e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
885e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
886e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
887e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
888e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
889e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
890e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
891e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
892e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
893e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
894e5fd0047SMichael J. Spencer     default:
8958ff24d25SRui Ueyama       Res = "Unknown";
896e5fd0047SMichael J. Spencer     }
897e5fd0047SMichael J. Spencer     break;
898e5fd0047SMichael J. Spencer   default:
8998ff24d25SRui Ueyama     Res = "Unknown";
900e5fd0047SMichael J. Spencer   }
9018ff24d25SRui Ueyama   Result.append(Res.begin(), Res.end());
902e5fd0047SMichael J. Spencer   return object_error::success;
903e5fd0047SMichael J. Spencer }
904e5fd0047SMichael J. Spencer 
905e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
906e5fd0047SMichael J. Spencer 
907e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
908e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
9098ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
9108ff24d25SRui Ueyama   const coff_symbol *Symb = 0;
9118ff24d25SRui Ueyama   if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
9128ff24d25SRui Ueyama   DataRefImpl Sym;
9138ff24d25SRui Ueyama   Sym.p = reinterpret_cast<uintptr_t>(Symb);
9148ff24d25SRui Ueyama   StringRef SymName;
9158ff24d25SRui Ueyama   if (error_code EC = getSymbolName(Sym, SymName)) return EC;
9168ff24d25SRui Ueyama   Result.append(SymName.begin(), SymName.end());
917e5fd0047SMichael J. Spencer   return object_error::success;
918022ecdf2SBenjamin Kramer }
9198e90adafSMichael J. Spencer 
9202fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
9212fc34c5fSDavid Meyer                                           LibraryRef &Result) const {
9222fc34c5fSDavid Meyer   report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
9232fc34c5fSDavid Meyer }
9242fc34c5fSDavid Meyer 
9252fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
9262fc34c5fSDavid Meyer                                           StringRef &Result) const {
9272fc34c5fSDavid Meyer   report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
9282fc34c5fSDavid Meyer }
9292fc34c5fSDavid Meyer 
930c2bed429SRui Ueyama bool ImportDirectoryEntryRef::
931c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const {
932a045b73aSRui Ueyama   return ImportTable == Other.ImportTable && Index == Other.Index;
933c2bed429SRui Ueyama }
934c2bed429SRui Ueyama 
935c2bed429SRui Ueyama error_code
936c2bed429SRui Ueyama ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
937a045b73aSRui Ueyama   Result = ImportDirectoryEntryRef(ImportTable, Index + 1, OwningObject);
938c2bed429SRui Ueyama   return object_error::success;
939c2bed429SRui Ueyama }
940c2bed429SRui Ueyama 
941c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::
942c2bed429SRui Ueyama getImportTableEntry(const import_directory_table_entry *&Result) const {
943a045b73aSRui Ueyama   Result = ImportTable;
944c2bed429SRui Ueyama   return object_error::success;
945c2bed429SRui Ueyama }
946c2bed429SRui Ueyama 
947c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
948c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
949a045b73aSRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
950a045b73aSRui Ueyama     return EC;
951a045b73aSRui Ueyama   Result = StringRef(reinterpret_cast<const char *>(IntPtr));
952c2bed429SRui Ueyama   return object_error::success;
953c2bed429SRui Ueyama }
954c2bed429SRui Ueyama 
955c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getImportLookupEntry(
956c2bed429SRui Ueyama     const import_lookup_table_entry32 *&Result) const {
957c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
958a045b73aSRui Ueyama   if (error_code EC =
959a045b73aSRui Ueyama           OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
960a045b73aSRui Ueyama     return EC;
961c2bed429SRui Ueyama   Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
962c2bed429SRui Ueyama   return object_error::success;
963c2bed429SRui Ueyama }
964c2bed429SRui Ueyama 
965ad882ba8SRui Ueyama bool ExportDirectoryEntryRef::
966ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const {
967ad882ba8SRui Ueyama   return ExportTable == Other.ExportTable && Index == Other.Index;
968ad882ba8SRui Ueyama }
969ad882ba8SRui Ueyama 
970ad882ba8SRui Ueyama error_code
971ad882ba8SRui Ueyama ExportDirectoryEntryRef::getNext(ExportDirectoryEntryRef &Result) const {
972ad882ba8SRui Ueyama   Result = ExportDirectoryEntryRef(ExportTable, Index + 1, OwningObject);
973ad882ba8SRui Ueyama   return object_error::success;
974ad882ba8SRui Ueyama }
975ad882ba8SRui Ueyama 
976da49d0d4SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only
977da49d0d4SRui Ueyama // by ordinal, the empty string is set as a result.
978da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
979da49d0d4SRui Ueyama   uintptr_t IntPtr = 0;
980da49d0d4SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
981da49d0d4SRui Ueyama     return EC;
982da49d0d4SRui Ueyama   Result = StringRef(reinterpret_cast<const char *>(IntPtr));
983da49d0d4SRui Ueyama   return object_error::success;
984da49d0d4SRui Ueyama }
985da49d0d4SRui Ueyama 
986e5df6095SRui Ueyama // Returns the starting ordinal number.
987e5df6095SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
988e5df6095SRui Ueyama   Result = ExportTable->OrdinalBase;
989e5df6095SRui Ueyama   return object_error::success;
990e5df6095SRui Ueyama }
991e5df6095SRui Ueyama 
992ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol.
993ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
994ad882ba8SRui Ueyama   Result = ExportTable->OrdinalBase + Index;
995ad882ba8SRui Ueyama   return object_error::success;
996ad882ba8SRui Ueyama }
997ad882ba8SRui Ueyama 
998ad882ba8SRui Ueyama // Returns the address of the current export symbol.
999ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1000ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
1001ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
1002ad882ba8SRui Ueyama           ExportTable->ExportAddressTableRVA, IntPtr))
1003ad882ba8SRui Ueyama     return EC;
100424fc2d64SRui Ueyama   const export_address_table_entry *entry =
100524fc2d64SRui Ueyama       reinterpret_cast<const export_address_table_entry *>(IntPtr);
1006ad882ba8SRui Ueyama   Result = entry[Index].ExportRVA;
1007ad882ba8SRui Ueyama   return object_error::success;
1008ad882ba8SRui Ueyama }
1009ad882ba8SRui Ueyama 
1010ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only
1011ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result.
1012da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1013ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
1014ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
1015ad882ba8SRui Ueyama           ExportTable->OrdinalTableRVA, IntPtr))
1016ad882ba8SRui Ueyama     return EC;
1017ad882ba8SRui Ueyama   const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1018ad882ba8SRui Ueyama 
1019ad882ba8SRui Ueyama   uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1020ad882ba8SRui Ueyama   int Offset = 0;
1021ad882ba8SRui Ueyama   for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1022ad882ba8SRui Ueyama        I < E; ++I, ++Offset) {
1023ad882ba8SRui Ueyama     if (*I != Index)
1024ad882ba8SRui Ueyama       continue;
1025ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(
1026ad882ba8SRui Ueyama             ExportTable->NamePointerRVA, IntPtr))
1027ad882ba8SRui Ueyama       return EC;
1028ad882ba8SRui Ueyama     const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1029ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1030ad882ba8SRui Ueyama       return EC;
1031ad882ba8SRui Ueyama     Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1032ad882ba8SRui Ueyama     return object_error::success;
1033ad882ba8SRui Ueyama   }
1034ad882ba8SRui Ueyama   Result = "";
1035ad882ba8SRui Ueyama   return object_error::success;
1036ad882ba8SRui Ueyama }
1037ad882ba8SRui Ueyama 
1038afcc3df7SRafael Espindola ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
1039afcc3df7SRafael Espindola                                                        bool BufferOwned) {
10408ff24d25SRui Ueyama   error_code EC;
1041afcc3df7SRafael Espindola   OwningPtr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC, BufferOwned));
1042692410efSRafael Espindola   if (EC)
1043692410efSRafael Espindola     return EC;
1044692410efSRafael Espindola   return Ret.take();
1045686738e2SRui Ueyama }
1046