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>
22b7a40081SRui Ueyama #include <cstdint>
238e90adafSMichael J. Spencer 
248e90adafSMichael J. Spencer using namespace llvm;
258e90adafSMichael J. Spencer using namespace object;
268e90adafSMichael J. Spencer 
278e90adafSMichael J. Spencer using support::ulittle8_t;
288e90adafSMichael J. Spencer using support::ulittle16_t;
298e90adafSMichael J. Spencer using support::ulittle32_t;
308e90adafSMichael J. Spencer using support::little16_t;
318e90adafSMichael J. Spencer 
321d6167fdSMichael J. Spencer // Returns false if size is greater than the buffer size. And sets ec.
33686738e2SRui Ueyama static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
348ff24d25SRui Ueyama   if (M->getBufferSize() < Size) {
358ff24d25SRui Ueyama     EC = object_error::unexpected_eof;
361d6167fdSMichael J. Spencer     return false;
371d6167fdSMichael J. Spencer   }
381d6167fdSMichael J. Spencer   return true;
398e90adafSMichael J. Spencer }
408e90adafSMichael J. Spencer 
41ed64342bSRui Ueyama // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
42ed64342bSRui Ueyama // Returns unexpected_eof if error.
43ed64342bSRui Ueyama template<typename T>
44686738e2SRui Ueyama static error_code getObject(const T *&Obj, const MemoryBuffer *M,
45686738e2SRui Ueyama                             const uint8_t *Ptr, const size_t Size = sizeof(T)) {
46ed64342bSRui Ueyama   uintptr_t Addr = uintptr_t(Ptr);
47ed64342bSRui Ueyama   if (Addr + Size < Addr ||
48ed64342bSRui Ueyama       Addr + Size < Size ||
49ed64342bSRui Ueyama       Addr + Size > uintptr_t(M->getBufferEnd())) {
50ed64342bSRui Ueyama     return object_error::unexpected_eof;
511d6167fdSMichael J. Spencer   }
52ed64342bSRui Ueyama   Obj = reinterpret_cast<const T *>(Addr);
53ed64342bSRui Ueyama   return object_error::success;
541d6167fdSMichael J. Spencer }
551d6167fdSMichael J. Spencer 
568ff24d25SRui Ueyama const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
578ff24d25SRui Ueyama   const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
581d6167fdSMichael J. Spencer 
591d6167fdSMichael J. Spencer # ifndef NDEBUG
601d6167fdSMichael J. Spencer   // Verify that the symbol points to a valid entry in the symbol table.
618ff24d25SRui Ueyama   uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
628ff24d25SRui Ueyama   if (Offset < COFFHeader->PointerToSymbolTable
638ff24d25SRui Ueyama       || Offset >= COFFHeader->PointerToSymbolTable
6482ebd8e3SRui Ueyama          + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
651d6167fdSMichael J. Spencer     report_fatal_error("Symbol was outside of symbol table.");
661d6167fdSMichael J. Spencer 
678ff24d25SRui Ueyama   assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
681d6167fdSMichael J. Spencer          == 0 && "Symbol did not point to the beginning of a symbol");
691d6167fdSMichael J. Spencer # endif
701d6167fdSMichael J. Spencer 
718ff24d25SRui Ueyama   return Addr;
721d6167fdSMichael J. Spencer }
731d6167fdSMichael J. Spencer 
748ff24d25SRui Ueyama const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
758ff24d25SRui Ueyama   const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
761d6167fdSMichael J. Spencer 
771d6167fdSMichael J. Spencer # ifndef NDEBUG
781d6167fdSMichael J. Spencer   // Verify that the section points to a valid entry in the section table.
798ff24d25SRui Ueyama   if (Addr < SectionTable
808ff24d25SRui Ueyama       || Addr >= (SectionTable + COFFHeader->NumberOfSections))
811d6167fdSMichael J. Spencer     report_fatal_error("Section was outside of section table.");
821d6167fdSMichael J. Spencer 
838ff24d25SRui Ueyama   uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
848ff24d25SRui Ueyama   assert(Offset % sizeof(coff_section) == 0 &&
851d6167fdSMichael J. Spencer          "Section did not point to the beginning of a section");
861d6167fdSMichael J. Spencer # endif
871d6167fdSMichael J. Spencer 
888ff24d25SRui Ueyama   return Addr;
891d6167fdSMichael J. Spencer }
901d6167fdSMichael J. Spencer 
915e812afaSRafael Espindola void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
928ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
938ff24d25SRui Ueyama   Symb += 1 + Symb->NumberOfAuxSymbols;
948ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Symb);
951d6167fdSMichael J. Spencer }
961d6167fdSMichael J. Spencer 
978ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
981d6167fdSMichael J. Spencer                                          StringRef &Result) const {
998ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1008ff24d25SRui Ueyama   return getSymbolName(Symb, Result);
1018e90adafSMichael J. Spencer }
1028e90adafSMichael J. Spencer 
1038ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
1041d6167fdSMichael J. Spencer                                             uint64_t &Result) const {
1058ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1065ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
1078ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1088ff24d25SRui Ueyama     return EC;
109e62ab11fSRafael Espindola 
1108ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1111d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
1121d6167fdSMichael J. Spencer   else if (Section)
1138ff24d25SRui Ueyama     Result = Section->PointerToRawData + Symb->Value;
1141d6167fdSMichael J. Spencer   else
1158ff24d25SRui Ueyama     Result = Symb->Value;
1161d6167fdSMichael J. Spencer   return object_error::success;
1178e90adafSMichael J. Spencer }
1188e90adafSMichael J. Spencer 
1198ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
12075d1cf33SBenjamin Kramer                                             uint64_t &Result) const {
1218ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
12275d1cf33SBenjamin Kramer   const coff_section *Section = NULL;
1238ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1248ff24d25SRui Ueyama     return EC;
125e62ab11fSRafael Espindola 
1268ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
12775d1cf33SBenjamin Kramer     Result = UnknownAddressOrSize;
12875d1cf33SBenjamin Kramer   else if (Section)
1298ff24d25SRui Ueyama     Result = Section->VirtualAddress + Symb->Value;
13075d1cf33SBenjamin Kramer   else
1318ff24d25SRui Ueyama     Result = Symb->Value;
13275d1cf33SBenjamin Kramer   return object_error::success;
13375d1cf33SBenjamin Kramer }
13475d1cf33SBenjamin Kramer 
1358ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
136d3946676SMichael J. Spencer                                          SymbolRef::Type &Result) const {
1378ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
13875d1cf33SBenjamin Kramer   Result = SymbolRef::ST_Other;
1398ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
1408ff24d25SRui Ueyama       Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
1417e4b976cSDavid Meyer     Result = SymbolRef::ST_Unknown;
1425efa665fSRui Ueyama   } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
14375d1cf33SBenjamin Kramer     Result = SymbolRef::ST_Function;
14475d1cf33SBenjamin Kramer   } else {
14506adfac8SRafael Espindola     uint32_t Characteristics = 0;
1468ff24d25SRui Ueyama     if (Symb->SectionNumber > 0) {
14706adfac8SRafael Espindola       const coff_section *Section = NULL;
1488ff24d25SRui Ueyama       if (error_code EC = getSection(Symb->SectionNumber, Section))
1498ff24d25SRui Ueyama         return EC;
15006adfac8SRafael Espindola       Characteristics = Section->Characteristics;
15175d1cf33SBenjamin Kramer     }
15206adfac8SRafael Espindola     if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
15306adfac8SRafael Espindola         ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
15406adfac8SRafael Espindola       Result = SymbolRef::ST_Data;
15575d1cf33SBenjamin Kramer   }
15675d1cf33SBenjamin Kramer   return object_error::success;
15775d1cf33SBenjamin Kramer }
15875d1cf33SBenjamin Kramer 
15920122a43SRafael Espindola uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
1608ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
16120122a43SRafael Espindola   uint32_t Result = SymbolRef::SF_None;
16275d1cf33SBenjamin Kramer 
163975e115eSRafael Espindola   // TODO: Correctly set SF_FormatSpecific, SF_Common
1647e4b976cSDavid Meyer 
16522fe9c1eSRafael Espindola   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
16622fe9c1eSRafael Espindola     if (Symb->Value == 0)
1677e4b976cSDavid Meyer       Result |= SymbolRef::SF_Undefined;
16822fe9c1eSRafael Espindola     else
16922fe9c1eSRafael Espindola       Result |= SymbolRef::SF_Common;
17022fe9c1eSRafael Espindola   }
17122fe9c1eSRafael Espindola 
1721df4b84dSDavid Meyer 
1731df4b84dSDavid Meyer   // TODO: This are certainly too restrictive.
1748ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
1751df4b84dSDavid Meyer     Result |= SymbolRef::SF_Global;
1761df4b84dSDavid Meyer 
1778ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
1781df4b84dSDavid Meyer     Result |= SymbolRef::SF_Weak;
1791df4b84dSDavid Meyer 
1808ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
1811df4b84dSDavid Meyer     Result |= SymbolRef::SF_Absolute;
1821df4b84dSDavid Meyer 
18320122a43SRafael Espindola   return Result;
18401759754SMichael J. Spencer }
18501759754SMichael J. Spencer 
1868ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
1871d6167fdSMichael J. Spencer                                          uint64_t &Result) const {
1888e90adafSMichael J. Spencer   // FIXME: Return the correct size. This requires looking at all the symbols
1898e90adafSMichael J. Spencer   //        in the same section as this symbol, and looking for either the next
1908e90adafSMichael J. Spencer   //        symbol, or the end of the section.
1918ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1925ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
1938ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1948ff24d25SRui Ueyama     return EC;
195e62ab11fSRafael Espindola 
1968ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1971d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
1981d6167fdSMichael J. Spencer   else if (Section)
1998ff24d25SRui Ueyama     Result = Section->SizeOfRawData - Symb->Value;
2001d6167fdSMichael J. Spencer   else
2011d6167fdSMichael J. Spencer     Result = 0;
2021d6167fdSMichael J. Spencer   return object_error::success;
2038e90adafSMichael J. Spencer }
2048e90adafSMichael J. Spencer 
2058ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
20632173153SMichael J. Spencer                                             section_iterator &Result) const {
2078ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
2088ff24d25SRui Ueyama   if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
209b5155a57SRafael Espindola     Result = section_end();
21032173153SMichael J. Spencer   else {
2118ff24d25SRui Ueyama     const coff_section *Sec = 0;
2128ff24d25SRui Ueyama     if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
2138ff24d25SRui Ueyama     DataRefImpl Ref;
2148ff24d25SRui Ueyama     Ref.p = reinterpret_cast<uintptr_t>(Sec);
2158ff24d25SRui Ueyama     Result = section_iterator(SectionRef(Ref, this));
21632173153SMichael J. Spencer   }
21732173153SMichael J. Spencer   return object_error::success;
21832173153SMichael J. Spencer }
21932173153SMichael J. Spencer 
2208ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
2214f223bf7STim Northover                                           uint64_t &Val) const {
2224f223bf7STim Northover   report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
2234f223bf7STim Northover }
2244f223bf7STim Northover 
2255e812afaSRafael Espindola void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
2268ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2278ff24d25SRui Ueyama   Sec += 1;
2288ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Sec);
2298e90adafSMichael J. Spencer }
2308e90adafSMichael J. Spencer 
2318ff24d25SRui Ueyama error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
2321d6167fdSMichael J. Spencer                                           StringRef &Result) const {
2338ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2348ff24d25SRui Ueyama   return getSectionName(Sec, Result);
2358e90adafSMichael J. Spencer }
2368e90adafSMichael J. Spencer 
2378ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
2381d6167fdSMichael J. Spencer                                              uint64_t &Result) const {
2398ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2408ff24d25SRui Ueyama   Result = Sec->VirtualAddress;
2411d6167fdSMichael J. Spencer   return object_error::success;
2428e90adafSMichael J. Spencer }
2438e90adafSMichael J. Spencer 
2448ff24d25SRui Ueyama error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
2451d6167fdSMichael J. Spencer                                           uint64_t &Result) const {
2468ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2478ff24d25SRui Ueyama   Result = Sec->SizeOfRawData;
2481d6167fdSMichael J. Spencer   return object_error::success;
2498e90adafSMichael J. Spencer }
2508e90adafSMichael J. Spencer 
2518ff24d25SRui Ueyama error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
2521d6167fdSMichael J. Spencer                                               StringRef &Result) const {
2538ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2549da9e693SMichael J. Spencer   ArrayRef<uint8_t> Res;
2558ff24d25SRui Ueyama   error_code EC = getSectionContents(Sec, Res);
2569da9e693SMichael J. Spencer   Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
2579da9e693SMichael J. Spencer   return EC;
2588e90adafSMichael J. Spencer }
2598e90adafSMichael J. Spencer 
2608ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
2617989460aSMichael J. Spencer                                                uint64_t &Res) const {
2628ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2638ff24d25SRui Ueyama   if (!Sec)
2647989460aSMichael J. Spencer     return object_error::parse_failed;
2658ff24d25SRui Ueyama   Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
2667989460aSMichael J. Spencer   return object_error::success;
2677989460aSMichael J. Spencer }
2687989460aSMichael J. Spencer 
2698ff24d25SRui Ueyama error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
2701d6167fdSMichael J. Spencer                                          bool &Result) const {
2718ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2728ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
2731d6167fdSMichael J. Spencer   return object_error::success;
2748e90adafSMichael J. Spencer }
2758e90adafSMichael J. Spencer 
2768ff24d25SRui Ueyama error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
277800619f2SMichael J. Spencer                                          bool &Result) const {
2788ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2798ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
280800619f2SMichael J. Spencer   return object_error::success;
281800619f2SMichael J. Spencer }
282800619f2SMichael J. Spencer 
2838ff24d25SRui Ueyama error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
284800619f2SMichael J. Spencer                                         bool &Result) const {
2858ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2868ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
287800619f2SMichael J. Spencer   return object_error::success;
288800619f2SMichael J. Spencer }
289800619f2SMichael J. Spencer 
2908ff24d25SRui Ueyama error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
2912138ef6dSPreston Gurd                                                          bool &Result) const {
2922138ef6dSPreston Gurd   // FIXME: Unimplemented
2932138ef6dSPreston Gurd   Result = true;
2942138ef6dSPreston Gurd   return object_error::success;
2952138ef6dSPreston Gurd }
2962138ef6dSPreston Gurd 
2978ff24d25SRui Ueyama error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
2982138ef6dSPreston Gurd                                            bool &Result) const {
2998ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3008ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
3012138ef6dSPreston Gurd   return object_error::success;
3022138ef6dSPreston Gurd }
3032138ef6dSPreston Gurd 
3048ff24d25SRui Ueyama error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
3052138ef6dSPreston Gurd                                              bool &Result) const {
306b96a320aSAndrew Kaylor   // FIXME: Unimplemented.
3072138ef6dSPreston Gurd   Result = false;
3082138ef6dSPreston Gurd   return object_error::success;
3092138ef6dSPreston Gurd }
3102138ef6dSPreston Gurd 
3118ff24d25SRui Ueyama error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
3123f31fa05SAndrew Kaylor                                                 bool &Result) const {
3133f31fa05SAndrew Kaylor   // FIXME: Unimplemented.
3143f31fa05SAndrew Kaylor   Result = false;
3153f31fa05SAndrew Kaylor   return object_error::success;
3163f31fa05SAndrew Kaylor }
3173f31fa05SAndrew Kaylor 
3188ff24d25SRui Ueyama error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
3198ff24d25SRui Ueyama                                                  DataRefImpl SymbRef,
320f6f3e81cSBenjamin Kramer                                                  bool &Result) const {
3218ff24d25SRui Ueyama   const coff_section *Sec = toSec(SecRef);
3228ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(SymbRef);
3238ff24d25SRui Ueyama   const coff_section *SymbSec = 0;
3248ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
3258ff24d25SRui Ueyama   if (SymbSec == Sec)
3269a28851eSMichael J. Spencer     Result = true;
3279a28851eSMichael J. Spencer   else
328f6f3e81cSBenjamin Kramer     Result = false;
329f6f3e81cSBenjamin Kramer   return object_error::success;
330f6f3e81cSBenjamin Kramer }
331f6f3e81cSBenjamin Kramer 
3328ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
3338ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3348ff24d25SRui Ueyama   DataRefImpl Ret;
3358ff24d25SRui Ueyama   if (Sec->NumberOfRelocations == 0)
3368ff24d25SRui Ueyama     Ret.p = 0;
337e5fd0047SMichael J. Spencer   else
3388ff24d25SRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations);
339e5fd0047SMichael J. Spencer 
3408ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
341e5fd0047SMichael J. Spencer }
342e5fd0047SMichael J. Spencer 
3438ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
3448ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3458ff24d25SRui Ueyama   DataRefImpl Ret;
3468ff24d25SRui Ueyama   if (Sec->NumberOfRelocations == 0)
3478ff24d25SRui Ueyama     Ret.p = 0;
348e5fd0047SMichael J. Spencer   else
3498ff24d25SRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(
350e5fd0047SMichael J. Spencer               reinterpret_cast<const coff_relocation*>(
3518ff24d25SRui Ueyama                 base() + Sec->PointerToRelocations)
3528ff24d25SRui Ueyama               + Sec->NumberOfRelocations);
353e5fd0047SMichael J. Spencer 
3548ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
355e5fd0047SMichael J. Spencer }
356e5fd0047SMichael J. Spencer 
357c2bed429SRui Ueyama // Initialize the pointer to the symbol table.
358c2bed429SRui Ueyama error_code COFFObjectFile::initSymbolTablePtr() {
3598ff24d25SRui Ueyama   if (error_code EC = getObject(
360c2bed429SRui Ueyama           SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
361c2bed429SRui Ueyama           COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
3628ff24d25SRui Ueyama     return EC;
363c2bed429SRui Ueyama 
364c2bed429SRui Ueyama   // Find string table. The first four byte of the string table contains the
365c2bed429SRui Ueyama   // total size of the string table, including the size field itself. If the
366c2bed429SRui Ueyama   // string table is empty, the value of the first four byte would be 4.
367c2bed429SRui Ueyama   const uint8_t *StringTableAddr =
368c2bed429SRui Ueyama       base() + COFFHeader->PointerToSymbolTable +
369c2bed429SRui Ueyama       COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
370c2bed429SRui Ueyama   const ulittle32_t *StringTableSizePtr;
3718ff24d25SRui Ueyama   if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
3728ff24d25SRui Ueyama     return EC;
373c2bed429SRui Ueyama   StringTableSize = *StringTableSizePtr;
3748ff24d25SRui Ueyama   if (error_code EC =
375c2bed429SRui Ueyama       getObject(StringTable, Data, StringTableAddr, StringTableSize))
3768ff24d25SRui Ueyama     return EC;
377c2bed429SRui Ueyama 
378c2bed429SRui Ueyama   // Check that the string table is null terminated if has any in it.
379c2bed429SRui Ueyama   if (StringTableSize < 4 ||
380c2bed429SRui Ueyama       (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
381c2bed429SRui Ueyama     return  object_error::parse_failed;
382c2bed429SRui Ueyama   return object_error::success;
383c2bed429SRui Ueyama }
384c2bed429SRui Ueyama 
385215a586cSRui Ueyama // Returns the file offset for the given VA.
386b7a40081SRui Ueyama error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
387*b6eb264aSRui Ueyama   uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
388*b6eb264aSRui Ueyama                                   : (uint64_t)PE32PlusHeader->ImageBase;
389b7a40081SRui Ueyama   uint64_t Rva = Addr - ImageBase;
390b7a40081SRui Ueyama   assert(Rva <= UINT32_MAX);
391b7a40081SRui Ueyama   return getRvaPtr((uint32_t)Rva, Res);
392215a586cSRui Ueyama }
393215a586cSRui Ueyama 
394c2bed429SRui Ueyama // Returns the file offset for the given RVA.
395215a586cSRui Ueyama error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
396b5155a57SRafael Espindola   for (section_iterator I = section_begin(), E = section_end(); I != E;
3975e812afaSRafael Espindola        ++I) {
3988ff24d25SRui Ueyama     const coff_section *Section = getCOFFSection(I);
399c2bed429SRui Ueyama     uint32_t SectionStart = Section->VirtualAddress;
400c2bed429SRui Ueyama     uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
401215a586cSRui Ueyama     if (SectionStart <= Addr && Addr < SectionEnd) {
402215a586cSRui Ueyama       uint32_t Offset = Addr - SectionStart;
403c2bed429SRui Ueyama       Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
404c2bed429SRui Ueyama       return object_error::success;
405c2bed429SRui Ueyama     }
406c2bed429SRui Ueyama   }
407c2bed429SRui Ueyama   return object_error::parse_failed;
408c2bed429SRui Ueyama }
409c2bed429SRui Ueyama 
410c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
411c2bed429SRui Ueyama // table entry.
412c2bed429SRui Ueyama error_code COFFObjectFile::
413c2bed429SRui Ueyama getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
414c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
4158ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(Rva, IntPtr))
4168ff24d25SRui Ueyama     return EC;
417c2bed429SRui Ueyama   const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
418c2bed429SRui Ueyama   Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
419c2bed429SRui Ueyama   Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
420c2bed429SRui Ueyama   return object_error::success;
421c2bed429SRui Ueyama }
422c2bed429SRui Ueyama 
423c2bed429SRui Ueyama // Find the import table.
424c2bed429SRui Ueyama error_code COFFObjectFile::initImportTablePtr() {
425c2bed429SRui Ueyama   // First, we get the RVA of the import table. If the file lacks a pointer to
426c2bed429SRui Ueyama   // the import table, do nothing.
427c2bed429SRui Ueyama   const data_directory *DataEntry;
428c2bed429SRui Ueyama   if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
429c2bed429SRui Ueyama     return object_error::success;
430c2bed429SRui Ueyama 
431c2bed429SRui Ueyama   // Do nothing if the pointer to import table is NULL.
432c2bed429SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
433c2bed429SRui Ueyama     return object_error::success;
434c2bed429SRui Ueyama 
435c2bed429SRui Ueyama   uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
436c2bed429SRui Ueyama   NumberOfImportDirectory = DataEntry->Size /
437c2bed429SRui Ueyama       sizeof(import_directory_table_entry);
438c2bed429SRui Ueyama 
439c2bed429SRui Ueyama   // Find the section that contains the RVA. This is needed because the RVA is
440c2bed429SRui Ueyama   // the import table's memory address which is different from its file offset.
441c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
4428ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
4438ff24d25SRui Ueyama     return EC;
444c2bed429SRui Ueyama   ImportDirectory = reinterpret_cast<
445c2bed429SRui Ueyama       const import_directory_table_entry *>(IntPtr);
446ad882ba8SRui Ueyama   return object_error::success;
447ad882ba8SRui Ueyama }
448c2bed429SRui Ueyama 
449ad882ba8SRui Ueyama // Find the export table.
450ad882ba8SRui Ueyama error_code COFFObjectFile::initExportTablePtr() {
451ad882ba8SRui Ueyama   // First, we get the RVA of the export table. If the file lacks a pointer to
452ad882ba8SRui Ueyama   // the export table, do nothing.
453ad882ba8SRui Ueyama   const data_directory *DataEntry;
454ad882ba8SRui Ueyama   if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
455ad882ba8SRui Ueyama     return object_error::success;
456ad882ba8SRui Ueyama 
457ad882ba8SRui Ueyama   // Do nothing if the pointer to export table is NULL.
458ad882ba8SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
459ad882ba8SRui Ueyama     return object_error::success;
460ad882ba8SRui Ueyama 
461ad882ba8SRui Ueyama   uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
462ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
463ad882ba8SRui Ueyama   if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
464ad882ba8SRui Ueyama     return EC;
46524fc2d64SRui Ueyama   ExportDirectory =
46624fc2d64SRui Ueyama       reinterpret_cast<const export_directory_table_entry *>(IntPtr);
467ad882ba8SRui Ueyama   return object_error::success;
468c2bed429SRui Ueyama }
469c2bed429SRui Ueyama 
470afcc3df7SRafael Espindola COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
471afcc3df7SRafael Espindola                                bool BufferOwned)
472afcc3df7SRafael Espindola     : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0),
47310ed9ddcSRui Ueyama       PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0),
47410ed9ddcSRui Ueyama       SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0),
475afcc3df7SRafael Espindola       NumberOfImportDirectory(0), ExportDirectory(0) {
4761d6167fdSMichael J. Spencer   // Check that we at least have enough room for a header.
4778ff24d25SRui Ueyama   if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
478ee066fc4SEric Christopher 
47982ebd8e3SRui Ueyama   // The current location in the file where we are looking at.
48082ebd8e3SRui Ueyama   uint64_t CurPtr = 0;
48182ebd8e3SRui Ueyama 
48282ebd8e3SRui Ueyama   // PE header is optional and is present only in executables. If it exists,
48382ebd8e3SRui Ueyama   // it is placed right after COFF header.
4848ff24d25SRui Ueyama   bool HasPEHeader = false;
485ee066fc4SEric Christopher 
4861d6167fdSMichael J. Spencer   // Check if this is a PE/COFF file.
487ec29b121SMichael J. Spencer   if (base()[0] == 0x4d && base()[1] == 0x5a) {
488ee066fc4SEric Christopher     // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
489ee066fc4SEric Christopher     // PE signature to find 'normal' COFF header.
4908ff24d25SRui Ueyama     if (!checkSize(Data, EC, 0x3c + 8)) return;
49182ebd8e3SRui Ueyama     CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
49282ebd8e3SRui Ueyama     // Check the PE magic bytes. ("PE\0\0")
49382ebd8e3SRui Ueyama     if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
4948ff24d25SRui Ueyama       EC = object_error::parse_failed;
4951d6167fdSMichael J. Spencer       return;
4961d6167fdSMichael J. Spencer     }
49782ebd8e3SRui Ueyama     CurPtr += 4; // Skip the PE magic bytes.
4988ff24d25SRui Ueyama     HasPEHeader = true;
499ee066fc4SEric Christopher   }
500ee066fc4SEric Christopher 
5018ff24d25SRui Ueyama   if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
5021d6167fdSMichael J. Spencer     return;
50382ebd8e3SRui Ueyama   CurPtr += sizeof(coff_file_header);
50482ebd8e3SRui Ueyama 
5058ff24d25SRui Ueyama   if (HasPEHeader) {
50610ed9ddcSRui Ueyama     const pe32_header *Header;
50710ed9ddcSRui Ueyama     if ((EC = getObject(Header, Data, base() + CurPtr)))
50882ebd8e3SRui Ueyama       return;
50910ed9ddcSRui Ueyama 
51010ed9ddcSRui Ueyama     const uint8_t *DataDirAddr;
51110ed9ddcSRui Ueyama     uint64_t DataDirSize;
51210ed9ddcSRui Ueyama     if (Header->Magic == 0x10b) {
51310ed9ddcSRui Ueyama       PE32Header = Header;
51410ed9ddcSRui Ueyama       DataDirAddr = base() + CurPtr + sizeof(pe32_header);
51510ed9ddcSRui Ueyama       DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
51610ed9ddcSRui Ueyama     } else if (Header->Magic == 0x20b) {
51710ed9ddcSRui Ueyama       PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
51810ed9ddcSRui Ueyama       DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
51910ed9ddcSRui Ueyama       DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
52010ed9ddcSRui Ueyama     } else {
52110ed9ddcSRui Ueyama       // It's neither PE32 nor PE32+.
52210ed9ddcSRui Ueyama       EC = object_error::parse_failed;
523ed64342bSRui Ueyama       return;
524ed64342bSRui Ueyama     }
52510ed9ddcSRui Ueyama     if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
52610ed9ddcSRui Ueyama       return;
52782ebd8e3SRui Ueyama     CurPtr += COFFHeader->SizeOfOptionalHeader;
52882ebd8e3SRui Ueyama   }
5291d6167fdSMichael J. Spencer 
530692410efSRafael Espindola   if (COFFHeader->isImportLibrary())
531692410efSRafael Espindola     return;
532692410efSRafael Espindola 
5338ff24d25SRui Ueyama   if ((EC = getObject(SectionTable, Data, base() + CurPtr,
534ed64342bSRui Ueyama                       COFFHeader->NumberOfSections * sizeof(coff_section))))
5351d6167fdSMichael J. Spencer     return;
5361d6167fdSMichael J. Spencer 
537c2bed429SRui Ueyama   // Initialize the pointer to the symbol table.
538c2bed429SRui Ueyama   if (COFFHeader->PointerToSymbolTable != 0)
5398ff24d25SRui Ueyama     if ((EC = initSymbolTablePtr()))
5401d6167fdSMichael J. Spencer       return;
5418e90adafSMichael J. Spencer 
542c2bed429SRui Ueyama   // Initialize the pointer to the beginning of the import table.
5438ff24d25SRui Ueyama   if ((EC = initImportTablePtr()))
544ed64342bSRui Ueyama     return;
5451d6167fdSMichael J. Spencer 
546ad882ba8SRui Ueyama   // Initialize the pointer to the export table.
5478ff24d25SRui Ueyama   if ((EC = initExportTablePtr()))
548ad882ba8SRui Ueyama     return;
549ad882ba8SRui Ueyama 
5508ff24d25SRui Ueyama   EC = object_error::success;
5518e90adafSMichael J. Spencer }
5528e90adafSMichael J. Spencer 
553b5155a57SRafael Espindola symbol_iterator COFFObjectFile::symbol_begin() const {
5548ff24d25SRui Ueyama   DataRefImpl Ret;
5558ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
5568ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ret, this));
5578e90adafSMichael J. Spencer }
5588e90adafSMichael J. Spencer 
559b5155a57SRafael Espindola symbol_iterator COFFObjectFile::symbol_end() const {
5608e90adafSMichael J. Spencer   // The symbol table ends where the string table begins.
5618ff24d25SRui Ueyama   DataRefImpl Ret;
5628ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(StringTable);
5638ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ret, this));
5648e90adafSMichael J. Spencer }
5658e90adafSMichael J. Spencer 
566b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_begin() const {
5672fc34c5fSDavid Meyer   // TODO: implement
5682fc34c5fSDavid Meyer   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
5692fc34c5fSDavid Meyer }
5702fc34c5fSDavid Meyer 
571b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_end() const {
5722fc34c5fSDavid Meyer   // TODO: implement
5732fc34c5fSDavid Meyer   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
5742fc34c5fSDavid Meyer }
5752fc34c5fSDavid Meyer 
576c429b80dSDavid Meyer StringRef COFFObjectFile::getLoadName() const {
577c429b80dSDavid Meyer   // COFF does not have this field.
578c429b80dSDavid Meyer   return "";
579c429b80dSDavid Meyer }
580c429b80dSDavid Meyer 
581bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_begin() const {
582a045b73aSRui Ueyama   return import_directory_iterator(
583a045b73aSRui Ueyama       ImportDirectoryEntryRef(ImportDirectory, 0, this));
584c2bed429SRui Ueyama }
585c2bed429SRui Ueyama 
586bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_end() const {
587a045b73aSRui Ueyama   return import_directory_iterator(
588a045b73aSRui Ueyama       ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
589c2bed429SRui Ueyama }
590c429b80dSDavid Meyer 
591ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_begin() const {
592ad882ba8SRui Ueyama   return export_directory_iterator(
593ad882ba8SRui Ueyama       ExportDirectoryEntryRef(ExportDirectory, 0, this));
594ad882ba8SRui Ueyama }
595ad882ba8SRui Ueyama 
596ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_end() const {
597ad882ba8SRui Ueyama   if (ExportDirectory == 0)
598ad882ba8SRui Ueyama     return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
5998ff24d25SRui Ueyama   ExportDirectoryEntryRef Ref(ExportDirectory,
600ad882ba8SRui Ueyama                               ExportDirectory->AddressTableEntries, this);
6018ff24d25SRui Ueyama   return export_directory_iterator(Ref);
602ad882ba8SRui Ueyama }
603ad882ba8SRui Ueyama 
604b5155a57SRafael Espindola section_iterator COFFObjectFile::section_begin() const {
6058ff24d25SRui Ueyama   DataRefImpl Ret;
6068ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
6078ff24d25SRui Ueyama   return section_iterator(SectionRef(Ret, this));
6088e90adafSMichael J. Spencer }
6098e90adafSMichael J. Spencer 
610b5155a57SRafael Espindola section_iterator COFFObjectFile::section_end() const {
6118ff24d25SRui Ueyama   DataRefImpl Ret;
6128ff24d25SRui Ueyama   int NumSections = COFFHeader->isImportLibrary()
61315ba1e20SRui Ueyama       ? 0 : COFFHeader->NumberOfSections;
6148ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
6158ff24d25SRui Ueyama   return section_iterator(SectionRef(Ret, this));
6168e90adafSMichael J. Spencer }
6178e90adafSMichael J. Spencer 
6188e90adafSMichael J. Spencer uint8_t COFFObjectFile::getBytesInAddress() const {
6190324b672SMichael J. Spencer   return getArch() == Triple::x86_64 ? 8 : 4;
6208e90adafSMichael J. Spencer }
6218e90adafSMichael J. Spencer 
6228e90adafSMichael J. Spencer StringRef COFFObjectFile::getFileFormatName() const {
62382ebd8e3SRui Ueyama   switch(COFFHeader->Machine) {
6248e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
6258e90adafSMichael J. Spencer     return "COFF-i386";
6268e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
6278e90adafSMichael J. Spencer     return "COFF-x86-64";
6288e90adafSMichael J. Spencer   default:
6298e90adafSMichael J. Spencer     return "COFF-<unknown arch>";
6308e90adafSMichael J. Spencer   }
6318e90adafSMichael J. Spencer }
6328e90adafSMichael J. Spencer 
6338e90adafSMichael J. Spencer unsigned COFFObjectFile::getArch() const {
63482ebd8e3SRui Ueyama   switch(COFFHeader->Machine) {
6358e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
6368e90adafSMichael J. Spencer     return Triple::x86;
6378e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
6388e90adafSMichael J. Spencer     return Triple::x86_64;
6398e90adafSMichael J. Spencer   default:
6408e90adafSMichael J. Spencer     return Triple::UnknownArch;
6418e90adafSMichael J. Spencer   }
6428e90adafSMichael J. Spencer }
6438e90adafSMichael J. Spencer 
64482ebd8e3SRui Ueyama // This method is kept here because lld uses this. As soon as we make
64582ebd8e3SRui Ueyama // lld to use getCOFFHeader, this method will be removed.
64689a7a5eaSMichael J. Spencer error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
64782ebd8e3SRui Ueyama   return getCOFFHeader(Res);
64882ebd8e3SRui Ueyama }
64982ebd8e3SRui Ueyama 
65082ebd8e3SRui Ueyama error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
65182ebd8e3SRui Ueyama   Res = COFFHeader;
65282ebd8e3SRui Ueyama   return object_error::success;
65382ebd8e3SRui Ueyama }
65482ebd8e3SRui Ueyama 
65582ebd8e3SRui Ueyama error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
65682ebd8e3SRui Ueyama   Res = PE32Header;
65789a7a5eaSMichael J. Spencer   return object_error::success;
65889a7a5eaSMichael J. Spencer }
65989a7a5eaSMichael J. Spencer 
66010ed9ddcSRui Ueyama error_code
66110ed9ddcSRui Ueyama COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
66210ed9ddcSRui Ueyama   Res = PE32PlusHeader;
66310ed9ddcSRui Ueyama   return object_error::success;
66410ed9ddcSRui Ueyama }
66510ed9ddcSRui Ueyama 
6668ff24d25SRui Ueyama error_code COFFObjectFile::getDataDirectory(uint32_t Index,
667ed64342bSRui Ueyama                                             const data_directory *&Res) const {
668ed64342bSRui Ueyama   // Error if if there's no data directory or the index is out of range.
66910ed9ddcSRui Ueyama   if (!DataDirectory)
67010ed9ddcSRui Ueyama     return object_error::parse_failed;
67110ed9ddcSRui Ueyama   assert(PE32Header || PE32PlusHeader);
67210ed9ddcSRui Ueyama   uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
67310ed9ddcSRui Ueyama                                : PE32PlusHeader->NumberOfRvaAndSize;
67410ed9ddcSRui Ueyama   if (Index > NumEnt)
675ed64342bSRui Ueyama     return object_error::parse_failed;
6768ff24d25SRui Ueyama   Res = &DataDirectory[Index];
677ed64342bSRui Ueyama   return object_error::success;
678ed64342bSRui Ueyama }
679ed64342bSRui Ueyama 
6808ff24d25SRui Ueyama error_code COFFObjectFile::getSection(int32_t Index,
6811d6167fdSMichael J. Spencer                                       const coff_section *&Result) const {
6821d6167fdSMichael J. Spencer   // Check for special index values.
6838ff24d25SRui Ueyama   if (Index == COFF::IMAGE_SYM_UNDEFINED ||
6848ff24d25SRui Ueyama       Index == COFF::IMAGE_SYM_ABSOLUTE ||
6858ff24d25SRui Ueyama       Index == COFF::IMAGE_SYM_DEBUG)
6861d6167fdSMichael J. Spencer     Result = NULL;
6878ff24d25SRui Ueyama   else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
6881d6167fdSMichael J. Spencer     // We already verified the section table data, so no need to check again.
6898ff24d25SRui Ueyama     Result = SectionTable + (Index - 1);
6901d6167fdSMichael J. Spencer   else
6911d6167fdSMichael J. Spencer     return object_error::parse_failed;
6921d6167fdSMichael J. Spencer   return object_error::success;
6938e90adafSMichael J. Spencer }
6948e90adafSMichael J. Spencer 
6958ff24d25SRui Ueyama error_code COFFObjectFile::getString(uint32_t Offset,
6961d6167fdSMichael J. Spencer                                      StringRef &Result) const {
6971d6167fdSMichael J. Spencer   if (StringTableSize <= 4)
6981d6167fdSMichael J. Spencer     // Tried to get a string from an empty string table.
6991d6167fdSMichael J. Spencer     return object_error::parse_failed;
7008ff24d25SRui Ueyama   if (Offset >= StringTableSize)
7011d6167fdSMichael J. Spencer     return object_error::unexpected_eof;
7028ff24d25SRui Ueyama   Result = StringRef(StringTable + Offset);
7031d6167fdSMichael J. Spencer   return object_error::success;
7048e90adafSMichael J. Spencer }
705022ecdf2SBenjamin Kramer 
7068ff24d25SRui Ueyama error_code COFFObjectFile::getSymbol(uint32_t Index,
707e5fd0047SMichael J. Spencer                                      const coff_symbol *&Result) const {
7088ff24d25SRui Ueyama   if (Index < COFFHeader->NumberOfSymbols)
7098ff24d25SRui Ueyama     Result = SymbolTable + Index;
710e5fd0047SMichael J. Spencer   else
711e5fd0047SMichael J. Spencer     return object_error::parse_failed;
712e5fd0047SMichael J. Spencer   return object_error::success;
713e5fd0047SMichael J. Spencer }
714e5fd0047SMichael J. Spencer 
7158ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
71689a7a5eaSMichael J. Spencer                                          StringRef &Res) const {
71789a7a5eaSMichael J. Spencer   // Check for string table entry. First 4 bytes are 0.
7188ff24d25SRui Ueyama   if (Symbol->Name.Offset.Zeroes == 0) {
7198ff24d25SRui Ueyama     uint32_t Offset = Symbol->Name.Offset.Offset;
7208ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Res))
7218ff24d25SRui Ueyama       return EC;
72289a7a5eaSMichael J. Spencer     return object_error::success;
72389a7a5eaSMichael J. Spencer   }
72489a7a5eaSMichael J. Spencer 
7258ff24d25SRui Ueyama   if (Symbol->Name.ShortName[7] == 0)
72689a7a5eaSMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
7278ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName);
72889a7a5eaSMichael J. Spencer   else
72989a7a5eaSMichael J. Spencer     // Not null terminated, use all 8 bytes.
7308ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName, 8);
73189a7a5eaSMichael J. Spencer   return object_error::success;
73289a7a5eaSMichael J. Spencer }
73389a7a5eaSMichael J. Spencer 
73471757ef3SMarshall Clow ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
7358ff24d25SRui Ueyama                                   const coff_symbol *Symbol) const {
7368ff24d25SRui Ueyama   const uint8_t *Aux = NULL;
73771757ef3SMarshall Clow 
7388ff24d25SRui Ueyama   if (Symbol->NumberOfAuxSymbols > 0) {
73971757ef3SMarshall Clow   // AUX data comes immediately after the symbol in COFF
7408ff24d25SRui Ueyama     Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
74171757ef3SMarshall Clow # ifndef NDEBUG
7428ff24d25SRui Ueyama     // Verify that the Aux symbol points to a valid entry in the symbol table.
7438ff24d25SRui Ueyama     uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
7448ff24d25SRui Ueyama     if (Offset < COFFHeader->PointerToSymbolTable
7458ff24d25SRui Ueyama         || Offset >= COFFHeader->PointerToSymbolTable
74682ebd8e3SRui Ueyama            + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
74771757ef3SMarshall Clow       report_fatal_error("Aux Symbol data was outside of symbol table.");
74871757ef3SMarshall Clow 
7498ff24d25SRui Ueyama     assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
75071757ef3SMarshall Clow          == 0 && "Aux Symbol data did not point to the beginning of a symbol");
75171757ef3SMarshall Clow # endif
752bfb85e67SMarshall Clow   }
75324fc2d64SRui Ueyama   return ArrayRef<uint8_t>(Aux,
75424fc2d64SRui Ueyama                            Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
75571757ef3SMarshall Clow }
75671757ef3SMarshall Clow 
75753c2d547SMichael J. Spencer error_code COFFObjectFile::getSectionName(const coff_section *Sec,
75853c2d547SMichael J. Spencer                                           StringRef &Res) const {
75953c2d547SMichael J. Spencer   StringRef Name;
76053c2d547SMichael J. Spencer   if (Sec->Name[7] == 0)
76153c2d547SMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
76253c2d547SMichael J. Spencer     Name = Sec->Name;
76353c2d547SMichael J. Spencer   else
76453c2d547SMichael J. Spencer     // Not null terminated, use all 8 bytes.
76553c2d547SMichael J. Spencer     Name = StringRef(Sec->Name, 8);
76653c2d547SMichael J. Spencer 
76753c2d547SMichael J. Spencer   // Check for string table entry. First byte is '/'.
76853c2d547SMichael J. Spencer   if (Name[0] == '/') {
76953c2d547SMichael J. Spencer     uint32_t Offset;
77053c2d547SMichael J. Spencer     if (Name.substr(1).getAsInteger(10, Offset))
77153c2d547SMichael J. Spencer       return object_error::parse_failed;
7728ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Name))
7738ff24d25SRui Ueyama       return EC;
77453c2d547SMichael J. Spencer   }
77553c2d547SMichael J. Spencer 
77653c2d547SMichael J. Spencer   Res = Name;
77753c2d547SMichael J. Spencer   return object_error::success;
77853c2d547SMichael J. Spencer }
77953c2d547SMichael J. Spencer 
7809da9e693SMichael J. Spencer error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
7819da9e693SMichael J. Spencer                                               ArrayRef<uint8_t> &Res) const {
7829da9e693SMichael J. Spencer   // The only thing that we need to verify is that the contents is contained
7839da9e693SMichael J. Spencer   // within the file bounds. We don't need to make sure it doesn't cover other
7849da9e693SMichael J. Spencer   // data, as there's nothing that says that is not allowed.
7859da9e693SMichael J. Spencer   uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
7869da9e693SMichael J. Spencer   uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
7879da9e693SMichael J. Spencer   if (ConEnd > uintptr_t(Data->getBufferEnd()))
7889da9e693SMichael J. Spencer     return object_error::parse_failed;
7899da9e693SMichael J. Spencer   Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
7909da9e693SMichael J. Spencer                           Sec->SizeOfRawData);
7919da9e693SMichael J. Spencer   return object_error::success;
7929da9e693SMichael J. Spencer }
7939da9e693SMichael J. Spencer 
794022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
795e5fd0047SMichael J. Spencer   return reinterpret_cast<const coff_relocation*>(Rel.p);
796022ecdf2SBenjamin Kramer }
7978ff24d25SRui Ueyama 
7985e812afaSRafael Espindola void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
799e5fd0047SMichael J. Spencer   Rel.p = reinterpret_cast<uintptr_t>(
800e5fd0047SMichael J. Spencer             reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
801022ecdf2SBenjamin Kramer }
8028ff24d25SRui Ueyama 
803022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
804022ecdf2SBenjamin Kramer                                                 uint64_t &Res) const {
8051e483879SRafael Espindola   report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
806022ecdf2SBenjamin Kramer }
8078ff24d25SRui Ueyama 
808cbe72fc9SDanil Malyshev error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
809cbe72fc9SDanil Malyshev                                                uint64_t &Res) const {
810cbe72fc9SDanil Malyshev   Res = toRel(Rel)->VirtualAddress;
811cbe72fc9SDanil Malyshev   return object_error::success;
812cbe72fc9SDanil Malyshev }
8138ff24d25SRui Ueyama 
814806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
815022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
8168ff24d25SRui Ueyama   DataRefImpl Ref;
8178ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
8188ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ref, this));
819022ecdf2SBenjamin Kramer }
8208ff24d25SRui Ueyama 
821022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
8227be76590SOwen Anderson                                              uint64_t &Res) const {
823022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
824022ecdf2SBenjamin Kramer   Res = R->Type;
825022ecdf2SBenjamin Kramer   return object_error::success;
826022ecdf2SBenjamin Kramer }
827e5fd0047SMichael J. Spencer 
82871757ef3SMarshall Clow const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
82971757ef3SMarshall Clow   return toSec(It->getRawDataRefImpl());
83071757ef3SMarshall Clow }
83171757ef3SMarshall Clow 
83271757ef3SMarshall Clow const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
83371757ef3SMarshall Clow   return toSymb(It->getRawDataRefImpl());
83471757ef3SMarshall Clow }
83571757ef3SMarshall Clow 
836d3e2a76cSMarshall Clow const coff_relocation *COFFObjectFile::getCOFFRelocation(
837d3e2a76cSMarshall Clow                                              relocation_iterator &It) const {
838d3e2a76cSMarshall Clow   return toRel(It->getRawDataRefImpl());
839d3e2a76cSMarshall Clow }
840d3e2a76cSMarshall Clow 
841e5fd0047SMichael J. Spencer #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
8428ff24d25SRui Ueyama   case COFF::enum: Res = #enum; break;
843e5fd0047SMichael J. Spencer 
844e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
845e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
8468ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
8478ff24d25SRui Ueyama   StringRef Res;
84882ebd8e3SRui Ueyama   switch (COFFHeader->Machine) {
849e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
8508ff24d25SRui Ueyama     switch (Reloc->Type) {
851e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
852e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
853e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
854e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
855e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
856e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
857e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
858e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
859e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
860e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
861e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
862e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
863e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
864e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
865e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
866e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
867e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
868e5fd0047SMichael J. Spencer     default:
8698ff24d25SRui Ueyama       Res = "Unknown";
870e5fd0047SMichael J. Spencer     }
871e5fd0047SMichael J. Spencer     break;
872e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
8738ff24d25SRui Ueyama     switch (Reloc->Type) {
874e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
875e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
876e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
877e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
878e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
879e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
880e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
881e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
882e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
883e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
884e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
885e5fd0047SMichael J. Spencer     default:
8868ff24d25SRui Ueyama       Res = "Unknown";
887e5fd0047SMichael J. Spencer     }
888e5fd0047SMichael J. Spencer     break;
889e5fd0047SMichael J. Spencer   default:
8908ff24d25SRui Ueyama     Res = "Unknown";
891e5fd0047SMichael J. Spencer   }
8928ff24d25SRui Ueyama   Result.append(Res.begin(), Res.end());
893e5fd0047SMichael J. Spencer   return object_error::success;
894e5fd0047SMichael J. Spencer }
895e5fd0047SMichael J. Spencer 
896e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
897e5fd0047SMichael J. Spencer 
898e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
899e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
9008ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
9018ff24d25SRui Ueyama   const coff_symbol *Symb = 0;
9028ff24d25SRui Ueyama   if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
9038ff24d25SRui Ueyama   DataRefImpl Sym;
9048ff24d25SRui Ueyama   Sym.p = reinterpret_cast<uintptr_t>(Symb);
9058ff24d25SRui Ueyama   StringRef SymName;
9068ff24d25SRui Ueyama   if (error_code EC = getSymbolName(Sym, SymName)) return EC;
9078ff24d25SRui Ueyama   Result.append(SymName.begin(), SymName.end());
908e5fd0047SMichael J. Spencer   return object_error::success;
909022ecdf2SBenjamin Kramer }
9108e90adafSMichael J. Spencer 
9112fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
9122fc34c5fSDavid Meyer                                           LibraryRef &Result) const {
9132fc34c5fSDavid Meyer   report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
9142fc34c5fSDavid Meyer }
9152fc34c5fSDavid Meyer 
9162fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
9172fc34c5fSDavid Meyer                                           StringRef &Result) const {
9182fc34c5fSDavid Meyer   report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
9192fc34c5fSDavid Meyer }
9202fc34c5fSDavid Meyer 
921c2bed429SRui Ueyama bool ImportDirectoryEntryRef::
922c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const {
923a045b73aSRui Ueyama   return ImportTable == Other.ImportTable && Index == Other.Index;
924c2bed429SRui Ueyama }
925c2bed429SRui Ueyama 
9265e812afaSRafael Espindola void ImportDirectoryEntryRef::moveNext() {
9275e812afaSRafael Espindola   ++Index;
928c2bed429SRui Ueyama }
929c2bed429SRui Ueyama 
930c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::
931c2bed429SRui Ueyama getImportTableEntry(const import_directory_table_entry *&Result) const {
932a045b73aSRui Ueyama   Result = ImportTable;
933c2bed429SRui Ueyama   return object_error::success;
934c2bed429SRui Ueyama }
935c2bed429SRui Ueyama 
936c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
937c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
938a045b73aSRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
939a045b73aSRui Ueyama     return EC;
940a045b73aSRui Ueyama   Result = StringRef(reinterpret_cast<const char *>(IntPtr));
941c2bed429SRui Ueyama   return object_error::success;
942c2bed429SRui Ueyama }
943c2bed429SRui Ueyama 
944c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getImportLookupEntry(
945c2bed429SRui Ueyama     const import_lookup_table_entry32 *&Result) const {
946c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
947a045b73aSRui Ueyama   if (error_code EC =
948a045b73aSRui Ueyama           OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
949a045b73aSRui Ueyama     return EC;
950c2bed429SRui Ueyama   Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
951c2bed429SRui Ueyama   return object_error::success;
952c2bed429SRui Ueyama }
953c2bed429SRui Ueyama 
954ad882ba8SRui Ueyama bool ExportDirectoryEntryRef::
955ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const {
956ad882ba8SRui Ueyama   return ExportTable == Other.ExportTable && Index == Other.Index;
957ad882ba8SRui Ueyama }
958ad882ba8SRui Ueyama 
9595e812afaSRafael Espindola void ExportDirectoryEntryRef::moveNext() {
9605e812afaSRafael Espindola   ++Index;
961ad882ba8SRui Ueyama }
962ad882ba8SRui Ueyama 
963da49d0d4SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only
964da49d0d4SRui Ueyama // by ordinal, the empty string is set as a result.
965da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
966da49d0d4SRui Ueyama   uintptr_t IntPtr = 0;
967da49d0d4SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
968da49d0d4SRui Ueyama     return EC;
969da49d0d4SRui Ueyama   Result = StringRef(reinterpret_cast<const char *>(IntPtr));
970da49d0d4SRui Ueyama   return object_error::success;
971da49d0d4SRui Ueyama }
972da49d0d4SRui Ueyama 
973e5df6095SRui Ueyama // Returns the starting ordinal number.
974e5df6095SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
975e5df6095SRui Ueyama   Result = ExportTable->OrdinalBase;
976e5df6095SRui Ueyama   return object_error::success;
977e5df6095SRui Ueyama }
978e5df6095SRui Ueyama 
979ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol.
980ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
981ad882ba8SRui Ueyama   Result = ExportTable->OrdinalBase + Index;
982ad882ba8SRui Ueyama   return object_error::success;
983ad882ba8SRui Ueyama }
984ad882ba8SRui Ueyama 
985ad882ba8SRui Ueyama // Returns the address of the current export symbol.
986ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
987ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
988ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
989ad882ba8SRui Ueyama           ExportTable->ExportAddressTableRVA, IntPtr))
990ad882ba8SRui Ueyama     return EC;
99124fc2d64SRui Ueyama   const export_address_table_entry *entry =
99224fc2d64SRui Ueyama       reinterpret_cast<const export_address_table_entry *>(IntPtr);
993ad882ba8SRui Ueyama   Result = entry[Index].ExportRVA;
994ad882ba8SRui Ueyama   return object_error::success;
995ad882ba8SRui Ueyama }
996ad882ba8SRui Ueyama 
997ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only
998ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result.
999da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1000ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
1001ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
1002ad882ba8SRui Ueyama           ExportTable->OrdinalTableRVA, IntPtr))
1003ad882ba8SRui Ueyama     return EC;
1004ad882ba8SRui Ueyama   const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1005ad882ba8SRui Ueyama 
1006ad882ba8SRui Ueyama   uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1007ad882ba8SRui Ueyama   int Offset = 0;
1008ad882ba8SRui Ueyama   for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1009ad882ba8SRui Ueyama        I < E; ++I, ++Offset) {
1010ad882ba8SRui Ueyama     if (*I != Index)
1011ad882ba8SRui Ueyama       continue;
1012ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(
1013ad882ba8SRui Ueyama             ExportTable->NamePointerRVA, IntPtr))
1014ad882ba8SRui Ueyama       return EC;
1015ad882ba8SRui Ueyama     const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1016ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1017ad882ba8SRui Ueyama       return EC;
1018ad882ba8SRui Ueyama     Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1019ad882ba8SRui Ueyama     return object_error::success;
1020ad882ba8SRui Ueyama   }
1021ad882ba8SRui Ueyama   Result = "";
1022ad882ba8SRui Ueyama   return object_error::success;
1023ad882ba8SRui Ueyama }
1024ad882ba8SRui Ueyama 
1025afcc3df7SRafael Espindola ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
1026afcc3df7SRafael Espindola                                                        bool BufferOwned) {
10278ff24d25SRui Ueyama   error_code EC;
1028afcc3df7SRafael Espindola   OwningPtr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC, BufferOwned));
1029692410efSRafael Espindola   if (EC)
1030692410efSRafael Espindola     return EC;
1031692410efSRafael Espindola   return Ret.take();
1032686738e2SRui Ueyama }
1033