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 namespace {
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 }
328e90adafSMichael J. Spencer 
331d6167fdSMichael J. Spencer namespace {
341d6167fdSMichael J. Spencer // Returns false if size is greater than the buffer size. And sets ec.
35*8ff24d25SRui Ueyama bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
36*8ff24d25SRui Ueyama   if (M->getBufferSize() < Size) {
37*8ff24d25SRui Ueyama     EC = object_error::unexpected_eof;
381d6167fdSMichael J. Spencer     return false;
391d6167fdSMichael J. Spencer   }
401d6167fdSMichael J. Spencer   return true;
418e90adafSMichael J. Spencer }
428e90adafSMichael J. Spencer 
43ed64342bSRui Ueyama // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
44ed64342bSRui Ueyama // Returns unexpected_eof if error.
45ed64342bSRui Ueyama template<typename T>
46ed64342bSRui Ueyama error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
47ed64342bSRui Ueyama                      const size_t Size = sizeof(T)) {
48ed64342bSRui Ueyama   uintptr_t Addr = uintptr_t(Ptr);
49ed64342bSRui Ueyama   if (Addr + Size < Addr ||
50ed64342bSRui Ueyama       Addr + Size < Size ||
51ed64342bSRui Ueyama       Addr + Size > uintptr_t(M->getBufferEnd())) {
52ed64342bSRui Ueyama     return object_error::unexpected_eof;
531d6167fdSMichael J. Spencer   }
54ed64342bSRui Ueyama   Obj = reinterpret_cast<const T *>(Addr);
55ed64342bSRui Ueyama   return object_error::success;
561d6167fdSMichael J. Spencer }
571d6167fdSMichael J. Spencer }
581d6167fdSMichael J. Spencer 
59*8ff24d25SRui Ueyama const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
60*8ff24d25SRui Ueyama   const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
611d6167fdSMichael J. Spencer 
621d6167fdSMichael J. Spencer # ifndef NDEBUG
631d6167fdSMichael J. Spencer   // Verify that the symbol points to a valid entry in the symbol table.
64*8ff24d25SRui Ueyama   uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
65*8ff24d25SRui Ueyama   if (Offset < COFFHeader->PointerToSymbolTable
66*8ff24d25SRui Ueyama       || Offset >= COFFHeader->PointerToSymbolTable
6782ebd8e3SRui Ueyama          + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
681d6167fdSMichael J. Spencer     report_fatal_error("Symbol was outside of symbol table.");
691d6167fdSMichael J. Spencer 
70*8ff24d25SRui Ueyama   assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
711d6167fdSMichael J. Spencer          == 0 && "Symbol did not point to the beginning of a symbol");
721d6167fdSMichael J. Spencer # endif
731d6167fdSMichael J. Spencer 
74*8ff24d25SRui Ueyama   return Addr;
751d6167fdSMichael J. Spencer }
761d6167fdSMichael J. Spencer 
77*8ff24d25SRui Ueyama const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
78*8ff24d25SRui Ueyama   const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
791d6167fdSMichael J. Spencer 
801d6167fdSMichael J. Spencer # ifndef NDEBUG
811d6167fdSMichael J. Spencer   // Verify that the section points to a valid entry in the section table.
82*8ff24d25SRui Ueyama   if (Addr < SectionTable
83*8ff24d25SRui Ueyama       || Addr >= (SectionTable + COFFHeader->NumberOfSections))
841d6167fdSMichael J. Spencer     report_fatal_error("Section was outside of section table.");
851d6167fdSMichael J. Spencer 
86*8ff24d25SRui Ueyama   uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
87*8ff24d25SRui Ueyama   assert(Offset % sizeof(coff_section) == 0 &&
881d6167fdSMichael J. Spencer          "Section did not point to the beginning of a section");
891d6167fdSMichael J. Spencer # endif
901d6167fdSMichael J. Spencer 
91*8ff24d25SRui Ueyama   return Addr;
921d6167fdSMichael J. Spencer }
931d6167fdSMichael J. Spencer 
94*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolNext(DataRefImpl Ref,
951d6167fdSMichael J. Spencer                                          SymbolRef &Result) const {
96*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
97*8ff24d25SRui Ueyama   Symb += 1 + Symb->NumberOfAuxSymbols;
98*8ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Symb);
99*8ff24d25SRui Ueyama   Result = SymbolRef(Ref, this);
1001d6167fdSMichael J. Spencer   return object_error::success;
1011d6167fdSMichael J. Spencer }
1021d6167fdSMichael J. Spencer 
103*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
1041d6167fdSMichael J. Spencer                                          StringRef &Result) const {
105*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
106*8ff24d25SRui Ueyama   return getSymbolName(Symb, Result);
1078e90adafSMichael J. Spencer }
1088e90adafSMichael J. Spencer 
109*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
1101d6167fdSMichael J. Spencer                                             uint64_t &Result) const {
111*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1125ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
113*8ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
114*8ff24d25SRui Ueyama     return EC;
115e62ab11fSRafael Espindola 
116*8ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1171d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
1181d6167fdSMichael J. Spencer   else if (Section)
119*8ff24d25SRui Ueyama     Result = Section->PointerToRawData + Symb->Value;
1201d6167fdSMichael J. Spencer   else
121*8ff24d25SRui Ueyama     Result = Symb->Value;
1221d6167fdSMichael J. Spencer   return object_error::success;
1238e90adafSMichael J. Spencer }
1248e90adafSMichael J. Spencer 
125*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
12675d1cf33SBenjamin Kramer                                             uint64_t &Result) const {
127*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
12875d1cf33SBenjamin Kramer   const coff_section *Section = NULL;
129*8ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
130*8ff24d25SRui Ueyama     return EC;
131e62ab11fSRafael Espindola 
132*8ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
13375d1cf33SBenjamin Kramer     Result = UnknownAddressOrSize;
13475d1cf33SBenjamin Kramer   else if (Section)
135*8ff24d25SRui Ueyama     Result = Section->VirtualAddress + Symb->Value;
13675d1cf33SBenjamin Kramer   else
137*8ff24d25SRui Ueyama     Result = Symb->Value;
13875d1cf33SBenjamin Kramer   return object_error::success;
13975d1cf33SBenjamin Kramer }
14075d1cf33SBenjamin Kramer 
141*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
142d3946676SMichael J. Spencer                                          SymbolRef::Type &Result) const {
143*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
14475d1cf33SBenjamin Kramer   Result = SymbolRef::ST_Other;
145*8ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
146*8ff24d25SRui Ueyama       Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
1477e4b976cSDavid Meyer     Result = SymbolRef::ST_Unknown;
14875d1cf33SBenjamin Kramer   } else {
149*8ff24d25SRui Ueyama     if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
15075d1cf33SBenjamin Kramer       Result = SymbolRef::ST_Function;
15175d1cf33SBenjamin Kramer     } else {
15206adfac8SRafael Espindola       uint32_t Characteristics = 0;
153*8ff24d25SRui Ueyama       if (Symb->SectionNumber > 0) {
15406adfac8SRafael Espindola         const coff_section *Section = NULL;
155*8ff24d25SRui Ueyama         if (error_code EC = getSection(Symb->SectionNumber, Section))
156*8ff24d25SRui Ueyama           return EC;
15706adfac8SRafael Espindola         Characteristics = Section->Characteristics;
15875d1cf33SBenjamin Kramer       }
15906adfac8SRafael Espindola       if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
16006adfac8SRafael Espindola           ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
16106adfac8SRafael Espindola         Result = SymbolRef::ST_Data;
16275d1cf33SBenjamin Kramer     }
16375d1cf33SBenjamin Kramer   }
16475d1cf33SBenjamin Kramer   return object_error::success;
16575d1cf33SBenjamin Kramer }
16675d1cf33SBenjamin Kramer 
167*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFlags(DataRefImpl Ref,
1681df4b84dSDavid Meyer                                           uint32_t &Result) const {
169*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1701df4b84dSDavid Meyer   Result = SymbolRef::SF_None;
17175d1cf33SBenjamin Kramer 
1727e4b976cSDavid Meyer   // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
1737e4b976cSDavid Meyer 
174*8ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
175*8ff24d25SRui Ueyama       Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1767e4b976cSDavid Meyer     Result |= SymbolRef::SF_Undefined;
1771df4b84dSDavid Meyer 
1781df4b84dSDavid Meyer   // TODO: This are certainly too restrictive.
179*8ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
1801df4b84dSDavid Meyer     Result |= SymbolRef::SF_Global;
1811df4b84dSDavid Meyer 
182*8ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
1831df4b84dSDavid Meyer     Result |= SymbolRef::SF_Weak;
1841df4b84dSDavid Meyer 
185*8ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
1861df4b84dSDavid Meyer     Result |= SymbolRef::SF_Absolute;
1871df4b84dSDavid Meyer 
18801759754SMichael J. Spencer   return object_error::success;
18901759754SMichael J. Spencer }
19001759754SMichael J. Spencer 
191*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
1921d6167fdSMichael J. Spencer                                          uint64_t &Result) const {
1938e90adafSMichael J. Spencer   // FIXME: Return the correct size. This requires looking at all the symbols
1948e90adafSMichael J. Spencer   //        in the same section as this symbol, and looking for either the next
1958e90adafSMichael J. Spencer   //        symbol, or the end of the section.
196*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1975ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
198*8ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
199*8ff24d25SRui Ueyama     return EC;
200e62ab11fSRafael Espindola 
201*8ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
2021d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
2031d6167fdSMichael J. Spencer   else if (Section)
204*8ff24d25SRui Ueyama     Result = Section->SizeOfRawData - Symb->Value;
2051d6167fdSMichael J. Spencer   else
2061d6167fdSMichael J. Spencer     Result = 0;
2071d6167fdSMichael J. Spencer   return object_error::success;
2088e90adafSMichael J. Spencer }
2098e90adafSMichael J. Spencer 
210*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
21132173153SMichael J. Spencer                                             section_iterator &Result) const {
212*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
213*8ff24d25SRui Ueyama   if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
21432173153SMichael J. Spencer     Result = end_sections();
21532173153SMichael J. Spencer   else {
216*8ff24d25SRui Ueyama     const coff_section *Sec = 0;
217*8ff24d25SRui Ueyama     if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
218*8ff24d25SRui Ueyama     DataRefImpl Ref;
219*8ff24d25SRui Ueyama     Ref.p = reinterpret_cast<uintptr_t>(Sec);
220*8ff24d25SRui Ueyama     Result = section_iterator(SectionRef(Ref, this));
22132173153SMichael J. Spencer   }
22232173153SMichael J. Spencer   return object_error::success;
22332173153SMichael J. Spencer }
22432173153SMichael J. Spencer 
225*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
2264f223bf7STim Northover                                           uint64_t &Val) const {
2274f223bf7STim Northover   report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
2284f223bf7STim Northover }
2294f223bf7STim Northover 
230*8ff24d25SRui Ueyama error_code COFFObjectFile::getSectionNext(DataRefImpl Ref,
2311d6167fdSMichael J. Spencer                                           SectionRef &Result) const {
232*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
233*8ff24d25SRui Ueyama   Sec += 1;
234*8ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Sec);
235*8ff24d25SRui Ueyama   Result = SectionRef(Ref, this);
2361d6167fdSMichael J. Spencer   return object_error::success;
2378e90adafSMichael J. Spencer }
2388e90adafSMichael J. Spencer 
239*8ff24d25SRui Ueyama error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
2401d6167fdSMichael J. Spencer                                           StringRef &Result) const {
241*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
242*8ff24d25SRui Ueyama   return getSectionName(Sec, Result);
2438e90adafSMichael J. Spencer }
2448e90adafSMichael J. Spencer 
245*8ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
2461d6167fdSMichael J. Spencer                                              uint64_t &Result) const {
247*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
248*8ff24d25SRui Ueyama   Result = Sec->VirtualAddress;
2491d6167fdSMichael J. Spencer   return object_error::success;
2508e90adafSMichael J. Spencer }
2518e90adafSMichael J. Spencer 
252*8ff24d25SRui Ueyama error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
2531d6167fdSMichael J. Spencer                                           uint64_t &Result) const {
254*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
255*8ff24d25SRui Ueyama   Result = Sec->SizeOfRawData;
2561d6167fdSMichael J. Spencer   return object_error::success;
2578e90adafSMichael J. Spencer }
2588e90adafSMichael J. Spencer 
259*8ff24d25SRui Ueyama error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
2601d6167fdSMichael J. Spencer                                               StringRef &Result) const {
261*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2629da9e693SMichael J. Spencer   ArrayRef<uint8_t> Res;
263*8ff24d25SRui Ueyama   error_code EC = getSectionContents(Sec, Res);
2649da9e693SMichael J. Spencer   Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
2659da9e693SMichael J. Spencer   return EC;
2668e90adafSMichael J. Spencer }
2678e90adafSMichael J. Spencer 
268*8ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
2697989460aSMichael J. Spencer                                                uint64_t &Res) const {
270*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
271*8ff24d25SRui Ueyama   if (!Sec)
2727989460aSMichael J. Spencer     return object_error::parse_failed;
273*8ff24d25SRui Ueyama   Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
2747989460aSMichael J. Spencer   return object_error::success;
2757989460aSMichael J. Spencer }
2767989460aSMichael J. Spencer 
277*8ff24d25SRui Ueyama error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
2781d6167fdSMichael J. Spencer                                          bool &Result) const {
279*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
280*8ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
2811d6167fdSMichael J. Spencer   return object_error::success;
2828e90adafSMichael J. Spencer }
2838e90adafSMichael J. Spencer 
284*8ff24d25SRui Ueyama error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
285800619f2SMichael J. Spencer                                          bool &Result) const {
286*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
287*8ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
288800619f2SMichael J. Spencer   return object_error::success;
289800619f2SMichael J. Spencer }
290800619f2SMichael J. Spencer 
291*8ff24d25SRui Ueyama error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
292800619f2SMichael J. Spencer                                         bool &Result) const {
293*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
294*8ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
295800619f2SMichael J. Spencer   return object_error::success;
296800619f2SMichael J. Spencer }
297800619f2SMichael J. Spencer 
298*8ff24d25SRui Ueyama error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
2992138ef6dSPreston Gurd                                                          bool &Result) const {
3002138ef6dSPreston Gurd   // FIXME: Unimplemented
3012138ef6dSPreston Gurd   Result = true;
3022138ef6dSPreston Gurd   return object_error::success;
3032138ef6dSPreston Gurd }
3042138ef6dSPreston Gurd 
305*8ff24d25SRui Ueyama error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
3062138ef6dSPreston Gurd                                            bool &Result) const {
307*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
308*8ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
3092138ef6dSPreston Gurd   return object_error::success;
3102138ef6dSPreston Gurd }
3112138ef6dSPreston Gurd 
312*8ff24d25SRui Ueyama error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
3132138ef6dSPreston Gurd                                              bool &Result) const {
314b96a320aSAndrew Kaylor   // FIXME: Unimplemented.
3152138ef6dSPreston Gurd   Result = false;
3162138ef6dSPreston Gurd   return object_error::success;
3172138ef6dSPreston Gurd }
3182138ef6dSPreston Gurd 
319*8ff24d25SRui Ueyama error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
3203f31fa05SAndrew Kaylor                                                 bool &Result) const {
3213f31fa05SAndrew Kaylor   // FIXME: Unimplemented.
3223f31fa05SAndrew Kaylor   Result = false;
3233f31fa05SAndrew Kaylor   return object_error::success;
3243f31fa05SAndrew Kaylor }
3253f31fa05SAndrew Kaylor 
326*8ff24d25SRui Ueyama error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
327*8ff24d25SRui Ueyama                                                  DataRefImpl SymbRef,
328f6f3e81cSBenjamin Kramer                                                  bool &Result) const {
329*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(SecRef);
330*8ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(SymbRef);
331*8ff24d25SRui Ueyama   const coff_section *SymbSec = 0;
332*8ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
333*8ff24d25SRui Ueyama   if (SymbSec == Sec)
3349a28851eSMichael J. Spencer     Result = true;
3359a28851eSMichael J. Spencer   else
336f6f3e81cSBenjamin Kramer     Result = false;
337f6f3e81cSBenjamin Kramer   return object_error::success;
338f6f3e81cSBenjamin Kramer }
339f6f3e81cSBenjamin Kramer 
340*8ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
341*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
342*8ff24d25SRui Ueyama   DataRefImpl Ret;
343*8ff24d25SRui Ueyama   if (Sec->NumberOfRelocations == 0)
344*8ff24d25SRui Ueyama     Ret.p = 0;
345e5fd0047SMichael J. Spencer   else
346*8ff24d25SRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations);
347e5fd0047SMichael J. Spencer 
348*8ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
349e5fd0047SMichael J. Spencer }
350e5fd0047SMichael J. Spencer 
351*8ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
352*8ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
353*8ff24d25SRui Ueyama   DataRefImpl Ret;
354*8ff24d25SRui Ueyama   if (Sec->NumberOfRelocations == 0)
355*8ff24d25SRui Ueyama     Ret.p = 0;
356e5fd0047SMichael J. Spencer   else
357*8ff24d25SRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(
358e5fd0047SMichael J. Spencer               reinterpret_cast<const coff_relocation*>(
359*8ff24d25SRui Ueyama                 base() + Sec->PointerToRelocations)
360*8ff24d25SRui Ueyama               + Sec->NumberOfRelocations);
361e5fd0047SMichael J. Spencer 
362*8ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
363e5fd0047SMichael J. Spencer }
364e5fd0047SMichael J. Spencer 
365c2bed429SRui Ueyama // Initialize the pointer to the symbol table.
366c2bed429SRui Ueyama error_code COFFObjectFile::initSymbolTablePtr() {
367*8ff24d25SRui Ueyama   if (error_code EC = getObject(
368c2bed429SRui Ueyama           SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
369c2bed429SRui Ueyama           COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
370*8ff24d25SRui Ueyama     return EC;
371c2bed429SRui Ueyama 
372c2bed429SRui Ueyama   // Find string table. The first four byte of the string table contains the
373c2bed429SRui Ueyama   // total size of the string table, including the size field itself. If the
374c2bed429SRui Ueyama   // string table is empty, the value of the first four byte would be 4.
375c2bed429SRui Ueyama   const uint8_t *StringTableAddr =
376c2bed429SRui Ueyama       base() + COFFHeader->PointerToSymbolTable +
377c2bed429SRui Ueyama       COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
378c2bed429SRui Ueyama   const ulittle32_t *StringTableSizePtr;
379*8ff24d25SRui Ueyama   if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
380*8ff24d25SRui Ueyama     return EC;
381c2bed429SRui Ueyama   StringTableSize = *StringTableSizePtr;
382*8ff24d25SRui Ueyama   if (error_code EC =
383c2bed429SRui Ueyama       getObject(StringTable, Data, StringTableAddr, StringTableSize))
384*8ff24d25SRui Ueyama     return EC;
385c2bed429SRui Ueyama 
386c2bed429SRui Ueyama   // Check that the string table is null terminated if has any in it.
387c2bed429SRui Ueyama   if (StringTableSize < 4 ||
388c2bed429SRui Ueyama       (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
389c2bed429SRui Ueyama     return  object_error::parse_failed;
390c2bed429SRui Ueyama   return object_error::success;
391c2bed429SRui Ueyama }
392c2bed429SRui Ueyama 
393c2bed429SRui Ueyama // Returns the file offset for the given RVA.
394c2bed429SRui Ueyama error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
395*8ff24d25SRui Ueyama   error_code EC;
396*8ff24d25SRui Ueyama   for (section_iterator I = begin_sections(), E = end_sections(); I != E;
397*8ff24d25SRui Ueyama        I.increment(EC)) {
398*8ff24d25SRui Ueyama     if (EC)
399*8ff24d25SRui Ueyama       return EC;
400*8ff24d25SRui Ueyama     const coff_section *Section = getCOFFSection(I);
401c2bed429SRui Ueyama     uint32_t SectionStart = Section->VirtualAddress;
402c2bed429SRui Ueyama     uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
403c2bed429SRui Ueyama     if (SectionStart <= Rva && Rva < SectionEnd) {
404c2bed429SRui Ueyama       uint32_t Offset = Rva - SectionStart;
405c2bed429SRui Ueyama       Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
406c2bed429SRui Ueyama       return object_error::success;
407c2bed429SRui Ueyama     }
408c2bed429SRui Ueyama   }
409c2bed429SRui Ueyama   return object_error::parse_failed;
410c2bed429SRui Ueyama }
411c2bed429SRui Ueyama 
412c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
413c2bed429SRui Ueyama // table entry.
414c2bed429SRui Ueyama error_code COFFObjectFile::
415c2bed429SRui Ueyama getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
416c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
417*8ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(Rva, IntPtr))
418*8ff24d25SRui Ueyama     return EC;
419c2bed429SRui Ueyama   const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
420c2bed429SRui Ueyama   Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
421c2bed429SRui Ueyama   Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
422c2bed429SRui Ueyama   return object_error::success;
423c2bed429SRui Ueyama }
424c2bed429SRui Ueyama 
425c2bed429SRui Ueyama // Find the import table.
426c2bed429SRui Ueyama error_code COFFObjectFile::initImportTablePtr() {
427c2bed429SRui Ueyama   // First, we get the RVA of the import table. If the file lacks a pointer to
428c2bed429SRui Ueyama   // the import table, do nothing.
429c2bed429SRui Ueyama   const data_directory *DataEntry;
430c2bed429SRui Ueyama   if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
431c2bed429SRui Ueyama     return object_error::success;
432c2bed429SRui Ueyama 
433c2bed429SRui Ueyama   // Do nothing if the pointer to import table is NULL.
434c2bed429SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
435c2bed429SRui Ueyama     return object_error::success;
436c2bed429SRui Ueyama 
437c2bed429SRui Ueyama   uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
438c2bed429SRui Ueyama   NumberOfImportDirectory = DataEntry->Size /
439c2bed429SRui Ueyama       sizeof(import_directory_table_entry);
440c2bed429SRui Ueyama 
441c2bed429SRui Ueyama   // Find the section that contains the RVA. This is needed because the RVA is
442c2bed429SRui Ueyama   // the import table's memory address which is different from its file offset.
443c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
444*8ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
445*8ff24d25SRui Ueyama     return EC;
446c2bed429SRui Ueyama   ImportDirectory = reinterpret_cast<
447c2bed429SRui Ueyama       const import_directory_table_entry *>(IntPtr);
448ad882ba8SRui Ueyama   return object_error::success;
449ad882ba8SRui Ueyama }
450c2bed429SRui Ueyama 
451ad882ba8SRui Ueyama // Find the export table.
452ad882ba8SRui Ueyama error_code COFFObjectFile::initExportTablePtr() {
453ad882ba8SRui Ueyama   // First, we get the RVA of the export table. If the file lacks a pointer to
454ad882ba8SRui Ueyama   // the export table, do nothing.
455ad882ba8SRui Ueyama   const data_directory *DataEntry;
456ad882ba8SRui Ueyama   if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
457ad882ba8SRui Ueyama     return object_error::success;
458ad882ba8SRui Ueyama 
459ad882ba8SRui Ueyama   // Do nothing if the pointer to export table is NULL.
460ad882ba8SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
461ad882ba8SRui Ueyama     return object_error::success;
462ad882ba8SRui Ueyama 
463ad882ba8SRui Ueyama   uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
464ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
465ad882ba8SRui Ueyama   if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
466ad882ba8SRui Ueyama     return EC;
467ad882ba8SRui Ueyama   ExportDirectory = reinterpret_cast<const export_directory_table_entry *>(IntPtr);
468ad882ba8SRui Ueyama   return object_error::success;
469c2bed429SRui Ueyama }
470c2bed429SRui Ueyama 
471*8ff24d25SRui Ueyama COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC)
472*8ff24d25SRui Ueyama   : ObjectFile(Binary::ID_COFF, Object), COFFHeader(0), PE32Header(0),
473*8ff24d25SRui Ueyama     DataDirectory(0), SectionTable(0), SymbolTable(0), StringTable(0),
474*8ff24d25SRui Ueyama     StringTableSize(0), ImportDirectory(0), NumberOfImportDirectory(0),
475*8ff24d25SRui Ueyama     ExportDirectory(0) {
4761d6167fdSMichael J. Spencer   // Check that we at least have enough room for a header.
477*8ff24d25SRui 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.
484*8ff24d25SRui 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.
490*8ff24d25SRui 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) {
494*8ff24d25SRui Ueyama       EC = object_error::parse_failed;
4951d6167fdSMichael J. Spencer       return;
4961d6167fdSMichael J. Spencer     }
49782ebd8e3SRui Ueyama     CurPtr += 4; // Skip the PE magic bytes.
498*8ff24d25SRui Ueyama     HasPEHeader = true;
499ee066fc4SEric Christopher   }
500ee066fc4SEric Christopher 
501*8ff24d25SRui Ueyama   if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
5021d6167fdSMichael J. Spencer     return;
50382ebd8e3SRui Ueyama   CurPtr += sizeof(coff_file_header);
50482ebd8e3SRui Ueyama 
505*8ff24d25SRui Ueyama   if (HasPEHeader) {
506*8ff24d25SRui Ueyama     if ((EC = getObject(PE32Header, Data, base() + CurPtr)))
50782ebd8e3SRui Ueyama       return;
508ed64342bSRui Ueyama     if (PE32Header->Magic != 0x10b) {
50982ebd8e3SRui Ueyama       // We only support PE32. If this is PE32 (not PE32+), the magic byte
51082ebd8e3SRui Ueyama       // should be 0x10b. If this is not PE32, continue as if there's no PE
51182ebd8e3SRui Ueyama       // header in this file.
51282ebd8e3SRui Ueyama       PE32Header = 0;
513ed64342bSRui Ueyama     } else if (PE32Header->NumberOfRvaAndSize > 0) {
514*8ff24d25SRui Ueyama       const uint8_t *Addr = base() + CurPtr + sizeof(pe32_header);
515ed64342bSRui Ueyama       uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
516*8ff24d25SRui Ueyama       if ((EC = getObject(DataDirectory, Data, Addr, size)))
517ed64342bSRui Ueyama         return;
518ed64342bSRui Ueyama     }
51982ebd8e3SRui Ueyama     CurPtr += COFFHeader->SizeOfOptionalHeader;
52082ebd8e3SRui Ueyama   }
5211d6167fdSMichael J. Spencer 
52215ba1e20SRui Ueyama   if (!COFFHeader->isImportLibrary())
523*8ff24d25SRui Ueyama     if ((EC = getObject(SectionTable, Data, base() + CurPtr,
524ed64342bSRui Ueyama                         COFFHeader->NumberOfSections * sizeof(coff_section))))
5251d6167fdSMichael J. Spencer       return;
5261d6167fdSMichael J. Spencer 
527c2bed429SRui Ueyama   // Initialize the pointer to the symbol table.
528c2bed429SRui Ueyama   if (COFFHeader->PointerToSymbolTable != 0)
529*8ff24d25SRui Ueyama     if ((EC = initSymbolTablePtr()))
5301d6167fdSMichael J. Spencer       return;
5318e90adafSMichael J. Spencer 
532c2bed429SRui Ueyama   // Initialize the pointer to the beginning of the import table.
533*8ff24d25SRui Ueyama   if ((EC = initImportTablePtr()))
534ed64342bSRui Ueyama     return;
5351d6167fdSMichael J. Spencer 
536ad882ba8SRui Ueyama   // Initialize the pointer to the export table.
537*8ff24d25SRui Ueyama   if ((EC = initExportTablePtr()))
538ad882ba8SRui Ueyama     return;
539ad882ba8SRui Ueyama 
540*8ff24d25SRui Ueyama   EC = object_error::success;
5418e90adafSMichael J. Spencer }
5428e90adafSMichael J. Spencer 
543e5fd0047SMichael J. Spencer symbol_iterator COFFObjectFile::begin_symbols() const {
544*8ff24d25SRui Ueyama   DataRefImpl Ret;
545*8ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
546*8ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ret, this));
5478e90adafSMichael J. Spencer }
5488e90adafSMichael J. Spencer 
549e5fd0047SMichael J. Spencer symbol_iterator COFFObjectFile::end_symbols() const {
5508e90adafSMichael J. Spencer   // The symbol table ends where the string table begins.
551*8ff24d25SRui Ueyama   DataRefImpl Ret;
552*8ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(StringTable);
553*8ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ret, this));
5548e90adafSMichael J. Spencer }
5558e90adafSMichael J. Spencer 
5568c4729fdSMichael J. Spencer symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
5578c4729fdSMichael J. Spencer   // TODO: implement
5588c4729fdSMichael J. Spencer   report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
5598c4729fdSMichael J. Spencer }
5608c4729fdSMichael J. Spencer 
5618c4729fdSMichael J. Spencer symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
5628c4729fdSMichael J. Spencer   // TODO: implement
5638c4729fdSMichael J. Spencer   report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
5648c4729fdSMichael J. Spencer }
5658c4729fdSMichael J. Spencer 
5662fc34c5fSDavid Meyer library_iterator COFFObjectFile::begin_libraries_needed() const {
5672fc34c5fSDavid Meyer   // TODO: implement
5682fc34c5fSDavid Meyer   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
5692fc34c5fSDavid Meyer }
5702fc34c5fSDavid Meyer 
5712fc34c5fSDavid Meyer library_iterator COFFObjectFile::end_libraries_needed() 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));
599*8ff24d25SRui Ueyama   ExportDirectoryEntryRef Ref(ExportDirectory,
600ad882ba8SRui Ueyama                               ExportDirectory->AddressTableEntries, this);
601*8ff24d25SRui Ueyama   return export_directory_iterator(Ref);
602ad882ba8SRui Ueyama }
603ad882ba8SRui Ueyama 
604e5fd0047SMichael J. Spencer section_iterator COFFObjectFile::begin_sections() const {
605*8ff24d25SRui Ueyama   DataRefImpl Ret;
606*8ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
607*8ff24d25SRui Ueyama   return section_iterator(SectionRef(Ret, this));
6088e90adafSMichael J. Spencer }
6098e90adafSMichael J. Spencer 
610e5fd0047SMichael J. Spencer section_iterator COFFObjectFile::end_sections() const {
611*8ff24d25SRui Ueyama   DataRefImpl Ret;
612*8ff24d25SRui Ueyama   int NumSections = COFFHeader->isImportLibrary()
61315ba1e20SRui Ueyama       ? 0 : COFFHeader->NumberOfSections;
614*8ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
615*8ff24d25SRui 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 
660*8ff24d25SRui Ueyama error_code COFFObjectFile::getDataDirectory(uint32_t Index,
661ed64342bSRui Ueyama                                             const data_directory *&Res) const {
662ed64342bSRui Ueyama   // Error if if there's no data directory or the index is out of range.
663*8ff24d25SRui Ueyama   if (!DataDirectory || Index > PE32Header->NumberOfRvaAndSize)
664ed64342bSRui Ueyama     return object_error::parse_failed;
665*8ff24d25SRui Ueyama   Res = &DataDirectory[Index];
666ed64342bSRui Ueyama   return object_error::success;
667ed64342bSRui Ueyama }
668ed64342bSRui Ueyama 
669*8ff24d25SRui Ueyama error_code COFFObjectFile::getSection(int32_t Index,
6701d6167fdSMichael J. Spencer                                       const coff_section *&Result) const {
6711d6167fdSMichael J. Spencer   // Check for special index values.
672*8ff24d25SRui Ueyama   if (Index == COFF::IMAGE_SYM_UNDEFINED ||
673*8ff24d25SRui Ueyama       Index == COFF::IMAGE_SYM_ABSOLUTE ||
674*8ff24d25SRui Ueyama       Index == COFF::IMAGE_SYM_DEBUG)
6751d6167fdSMichael J. Spencer     Result = NULL;
676*8ff24d25SRui Ueyama   else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
6771d6167fdSMichael J. Spencer     // We already verified the section table data, so no need to check again.
678*8ff24d25SRui Ueyama     Result = SectionTable + (Index - 1);
6791d6167fdSMichael J. Spencer   else
6801d6167fdSMichael J. Spencer     return object_error::parse_failed;
6811d6167fdSMichael J. Spencer   return object_error::success;
6828e90adafSMichael J. Spencer }
6838e90adafSMichael J. Spencer 
684*8ff24d25SRui Ueyama error_code COFFObjectFile::getString(uint32_t Offset,
6851d6167fdSMichael J. Spencer                                      StringRef &Result) const {
6861d6167fdSMichael J. Spencer   if (StringTableSize <= 4)
6871d6167fdSMichael J. Spencer     // Tried to get a string from an empty string table.
6881d6167fdSMichael J. Spencer     return object_error::parse_failed;
689*8ff24d25SRui Ueyama   if (Offset >= StringTableSize)
6901d6167fdSMichael J. Spencer     return object_error::unexpected_eof;
691*8ff24d25SRui Ueyama   Result = StringRef(StringTable + Offset);
6921d6167fdSMichael J. Spencer   return object_error::success;
6938e90adafSMichael J. Spencer }
694022ecdf2SBenjamin Kramer 
695*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbol(uint32_t Index,
696e5fd0047SMichael J. Spencer                                      const coff_symbol *&Result) const {
697*8ff24d25SRui Ueyama   if (Index < COFFHeader->NumberOfSymbols)
698*8ff24d25SRui Ueyama     Result = SymbolTable + Index;
699e5fd0047SMichael J. Spencer   else
700e5fd0047SMichael J. Spencer     return object_error::parse_failed;
701e5fd0047SMichael J. Spencer   return object_error::success;
702e5fd0047SMichael J. Spencer }
703e5fd0047SMichael J. Spencer 
704*8ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
70589a7a5eaSMichael J. Spencer                                          StringRef &Res) const {
70689a7a5eaSMichael J. Spencer   // Check for string table entry. First 4 bytes are 0.
707*8ff24d25SRui Ueyama   if (Symbol->Name.Offset.Zeroes == 0) {
708*8ff24d25SRui Ueyama     uint32_t Offset = Symbol->Name.Offset.Offset;
709*8ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Res))
710*8ff24d25SRui Ueyama       return EC;
71189a7a5eaSMichael J. Spencer     return object_error::success;
71289a7a5eaSMichael J. Spencer   }
71389a7a5eaSMichael J. Spencer 
714*8ff24d25SRui Ueyama   if (Symbol->Name.ShortName[7] == 0)
71589a7a5eaSMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
716*8ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName);
71789a7a5eaSMichael J. Spencer   else
71889a7a5eaSMichael J. Spencer     // Not null terminated, use all 8 bytes.
719*8ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName, 8);
72089a7a5eaSMichael J. Spencer   return object_error::success;
72189a7a5eaSMichael J. Spencer }
72289a7a5eaSMichael J. Spencer 
72371757ef3SMarshall Clow ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
724*8ff24d25SRui Ueyama                                   const coff_symbol *Symbol) const {
725*8ff24d25SRui Ueyama   const uint8_t *Aux = NULL;
72671757ef3SMarshall Clow 
727*8ff24d25SRui Ueyama   if (Symbol->NumberOfAuxSymbols > 0) {
72871757ef3SMarshall Clow   // AUX data comes immediately after the symbol in COFF
729*8ff24d25SRui Ueyama     Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
73071757ef3SMarshall Clow # ifndef NDEBUG
731*8ff24d25SRui Ueyama     // Verify that the Aux symbol points to a valid entry in the symbol table.
732*8ff24d25SRui Ueyama     uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
733*8ff24d25SRui Ueyama     if (Offset < COFFHeader->PointerToSymbolTable
734*8ff24d25SRui Ueyama         || Offset >= COFFHeader->PointerToSymbolTable
73582ebd8e3SRui Ueyama            + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
73671757ef3SMarshall Clow       report_fatal_error("Aux Symbol data was outside of symbol table.");
73771757ef3SMarshall Clow 
738*8ff24d25SRui Ueyama     assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
73971757ef3SMarshall Clow          == 0 && "Aux Symbol data did not point to the beginning of a symbol");
74071757ef3SMarshall Clow # endif
741bfb85e67SMarshall Clow   }
742*8ff24d25SRui Ueyama   return ArrayRef<uint8_t>(Aux, Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
74371757ef3SMarshall Clow }
74471757ef3SMarshall Clow 
74553c2d547SMichael J. Spencer error_code COFFObjectFile::getSectionName(const coff_section *Sec,
74653c2d547SMichael J. Spencer                                           StringRef &Res) const {
74753c2d547SMichael J. Spencer   StringRef Name;
74853c2d547SMichael J. Spencer   if (Sec->Name[7] == 0)
74953c2d547SMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
75053c2d547SMichael J. Spencer     Name = Sec->Name;
75153c2d547SMichael J. Spencer   else
75253c2d547SMichael J. Spencer     // Not null terminated, use all 8 bytes.
75353c2d547SMichael J. Spencer     Name = StringRef(Sec->Name, 8);
75453c2d547SMichael J. Spencer 
75553c2d547SMichael J. Spencer   // Check for string table entry. First byte is '/'.
75653c2d547SMichael J. Spencer   if (Name[0] == '/') {
75753c2d547SMichael J. Spencer     uint32_t Offset;
75853c2d547SMichael J. Spencer     if (Name.substr(1).getAsInteger(10, Offset))
75953c2d547SMichael J. Spencer       return object_error::parse_failed;
760*8ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Name))
761*8ff24d25SRui Ueyama       return EC;
76253c2d547SMichael J. Spencer   }
76353c2d547SMichael J. Spencer 
76453c2d547SMichael J. Spencer   Res = Name;
76553c2d547SMichael J. Spencer   return object_error::success;
76653c2d547SMichael J. Spencer }
76753c2d547SMichael J. Spencer 
7689da9e693SMichael J. Spencer error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
7699da9e693SMichael J. Spencer                                               ArrayRef<uint8_t> &Res) const {
7709da9e693SMichael J. Spencer   // The only thing that we need to verify is that the contents is contained
7719da9e693SMichael J. Spencer   // within the file bounds. We don't need to make sure it doesn't cover other
7729da9e693SMichael J. Spencer   // data, as there's nothing that says that is not allowed.
7739da9e693SMichael J. Spencer   uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
7749da9e693SMichael J. Spencer   uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
7759da9e693SMichael J. Spencer   if (ConEnd > uintptr_t(Data->getBufferEnd()))
7769da9e693SMichael J. Spencer     return object_error::parse_failed;
7779da9e693SMichael J. Spencer   Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
7789da9e693SMichael J. Spencer                           Sec->SizeOfRawData);
7799da9e693SMichael J. Spencer   return object_error::success;
7809da9e693SMichael J. Spencer }
7819da9e693SMichael J. Spencer 
782022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
783e5fd0047SMichael J. Spencer   return reinterpret_cast<const coff_relocation*>(Rel.p);
784022ecdf2SBenjamin Kramer }
785*8ff24d25SRui Ueyama 
786022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
787022ecdf2SBenjamin Kramer                                              RelocationRef &Res) const {
788e5fd0047SMichael J. Spencer   Rel.p = reinterpret_cast<uintptr_t>(
789e5fd0047SMichael J. Spencer             reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
790022ecdf2SBenjamin Kramer   Res = RelocationRef(Rel, this);
791022ecdf2SBenjamin Kramer   return object_error::success;
792022ecdf2SBenjamin Kramer }
793*8ff24d25SRui Ueyama 
794022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
795022ecdf2SBenjamin Kramer                                                 uint64_t &Res) const {
7961e483879SRafael Espindola   report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
797022ecdf2SBenjamin Kramer }
798*8ff24d25SRui Ueyama 
799cbe72fc9SDanil Malyshev error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
800cbe72fc9SDanil Malyshev                                                uint64_t &Res) const {
801cbe72fc9SDanil Malyshev   Res = toRel(Rel)->VirtualAddress;
802cbe72fc9SDanil Malyshev   return object_error::success;
803cbe72fc9SDanil Malyshev }
804*8ff24d25SRui Ueyama 
805806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
806022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
807*8ff24d25SRui Ueyama   DataRefImpl Ref;
808*8ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
809*8ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ref, this));
810022ecdf2SBenjamin Kramer }
811*8ff24d25SRui Ueyama 
812022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
8137be76590SOwen Anderson                                              uint64_t &Res) const {
814022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
815022ecdf2SBenjamin Kramer   Res = R->Type;
816022ecdf2SBenjamin Kramer   return object_error::success;
817022ecdf2SBenjamin Kramer }
818e5fd0047SMichael J. Spencer 
81971757ef3SMarshall Clow const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
82071757ef3SMarshall Clow   return toSec(It->getRawDataRefImpl());
82171757ef3SMarshall Clow }
82271757ef3SMarshall Clow 
82371757ef3SMarshall Clow const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
82471757ef3SMarshall Clow   return toSymb(It->getRawDataRefImpl());
82571757ef3SMarshall Clow }
82671757ef3SMarshall Clow 
827d3e2a76cSMarshall Clow const coff_relocation *COFFObjectFile::getCOFFRelocation(
828d3e2a76cSMarshall Clow                                              relocation_iterator &It) const {
829d3e2a76cSMarshall Clow   return toRel(It->getRawDataRefImpl());
830d3e2a76cSMarshall Clow }
831d3e2a76cSMarshall Clow 
832e5fd0047SMichael J. Spencer #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
833*8ff24d25SRui Ueyama   case COFF::enum: Res = #enum; break;
834e5fd0047SMichael J. Spencer 
835e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
836e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
837*8ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
838*8ff24d25SRui Ueyama   StringRef Res;
83982ebd8e3SRui Ueyama   switch (COFFHeader->Machine) {
840e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
841*8ff24d25SRui Ueyama     switch (Reloc->Type) {
842e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
843e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
844e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
845e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
846e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
847e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
848e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
849e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
850e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
851e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
852e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
853e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
854e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
855e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
856e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
857e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
858e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
859e5fd0047SMichael J. Spencer     default:
860*8ff24d25SRui Ueyama       Res = "Unknown";
861e5fd0047SMichael J. Spencer     }
862e5fd0047SMichael J. Spencer     break;
863e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
864*8ff24d25SRui Ueyama     switch (Reloc->Type) {
865e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
866e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
867e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
868e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
869e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
870e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
871e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
872e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
873e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
874e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
875e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
876e5fd0047SMichael J. Spencer     default:
877*8ff24d25SRui Ueyama       Res = "Unknown";
878e5fd0047SMichael J. Spencer     }
879e5fd0047SMichael J. Spencer     break;
880e5fd0047SMichael J. Spencer   default:
881*8ff24d25SRui Ueyama     Res = "Unknown";
882e5fd0047SMichael J. Spencer   }
883*8ff24d25SRui Ueyama   Result.append(Res.begin(), Res.end());
884e5fd0047SMichael J. Spencer   return object_error::success;
885e5fd0047SMichael J. Spencer }
886e5fd0047SMichael J. Spencer 
887e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
888e5fd0047SMichael J. Spencer 
889e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
890e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
891*8ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
892*8ff24d25SRui Ueyama   const coff_symbol *Symb = 0;
893*8ff24d25SRui Ueyama   if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
894*8ff24d25SRui Ueyama   DataRefImpl Sym;
895*8ff24d25SRui Ueyama   Sym.p = reinterpret_cast<uintptr_t>(Symb);
896*8ff24d25SRui Ueyama   StringRef SymName;
897*8ff24d25SRui Ueyama   if (error_code EC = getSymbolName(Sym, SymName)) return EC;
898*8ff24d25SRui Ueyama   Result.append(SymName.begin(), SymName.end());
899e5fd0047SMichael J. Spencer   return object_error::success;
900022ecdf2SBenjamin Kramer }
9018e90adafSMichael J. Spencer 
9022fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
9032fc34c5fSDavid Meyer                                           LibraryRef &Result) const {
9042fc34c5fSDavid Meyer   report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
9052fc34c5fSDavid Meyer }
9062fc34c5fSDavid Meyer 
9072fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
9082fc34c5fSDavid Meyer                                           StringRef &Result) const {
9092fc34c5fSDavid Meyer   report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
9102fc34c5fSDavid Meyer }
9112fc34c5fSDavid Meyer 
912c2bed429SRui Ueyama bool ImportDirectoryEntryRef::
913c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const {
914a045b73aSRui Ueyama   return ImportTable == Other.ImportTable && Index == Other.Index;
915c2bed429SRui Ueyama }
916c2bed429SRui Ueyama 
917c2bed429SRui Ueyama error_code
918c2bed429SRui Ueyama ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
919a045b73aSRui Ueyama   Result = ImportDirectoryEntryRef(ImportTable, Index + 1, OwningObject);
920c2bed429SRui Ueyama   return object_error::success;
921c2bed429SRui Ueyama }
922c2bed429SRui Ueyama 
923c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::
924c2bed429SRui Ueyama getImportTableEntry(const import_directory_table_entry *&Result) const {
925a045b73aSRui Ueyama   Result = ImportTable;
926c2bed429SRui Ueyama   return object_error::success;
927c2bed429SRui Ueyama }
928c2bed429SRui Ueyama 
929c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
930c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
931a045b73aSRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
932a045b73aSRui Ueyama     return EC;
933a045b73aSRui Ueyama   Result = StringRef(reinterpret_cast<const char *>(IntPtr));
934c2bed429SRui Ueyama   return object_error::success;
935c2bed429SRui Ueyama }
936c2bed429SRui Ueyama 
937c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getImportLookupEntry(
938c2bed429SRui Ueyama     const import_lookup_table_entry32 *&Result) const {
939c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
940a045b73aSRui Ueyama   if (error_code EC =
941a045b73aSRui Ueyama           OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
942a045b73aSRui Ueyama     return EC;
943c2bed429SRui Ueyama   Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
944c2bed429SRui Ueyama   return object_error::success;
945c2bed429SRui Ueyama }
946c2bed429SRui Ueyama 
947ad882ba8SRui Ueyama bool ExportDirectoryEntryRef::
948ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const {
949ad882ba8SRui Ueyama   return ExportTable == Other.ExportTable && Index == Other.Index;
950ad882ba8SRui Ueyama }
951ad882ba8SRui Ueyama 
952ad882ba8SRui Ueyama error_code
953ad882ba8SRui Ueyama ExportDirectoryEntryRef::getNext(ExportDirectoryEntryRef &Result) const {
954ad882ba8SRui Ueyama   Result = ExportDirectoryEntryRef(ExportTable, Index + 1, OwningObject);
955ad882ba8SRui Ueyama   return object_error::success;
956ad882ba8SRui Ueyama }
957ad882ba8SRui Ueyama 
958ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol.
959ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
960ad882ba8SRui Ueyama   Result = ExportTable->OrdinalBase + Index;
961ad882ba8SRui Ueyama   return object_error::success;
962ad882ba8SRui Ueyama }
963ad882ba8SRui Ueyama 
964ad882ba8SRui Ueyama // Returns the address of the current export symbol.
965ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
966ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
967ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
968ad882ba8SRui Ueyama           ExportTable->ExportAddressTableRVA, IntPtr))
969ad882ba8SRui Ueyama     return EC;
970ad882ba8SRui Ueyama   const export_address_table_entry *entry = reinterpret_cast<const export_address_table_entry *>(IntPtr);
971ad882ba8SRui Ueyama   Result = entry[Index].ExportRVA;
972ad882ba8SRui Ueyama   return object_error::success;
973ad882ba8SRui Ueyama }
974ad882ba8SRui Ueyama 
975ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only
976ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result.
977ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getName(StringRef &Result) const {
978ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
979ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
980ad882ba8SRui Ueyama           ExportTable->OrdinalTableRVA, IntPtr))
981ad882ba8SRui Ueyama     return EC;
982ad882ba8SRui Ueyama   const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
983ad882ba8SRui Ueyama 
984ad882ba8SRui Ueyama   uint32_t NumEntries = ExportTable->NumberOfNamePointers;
985ad882ba8SRui Ueyama   int Offset = 0;
986ad882ba8SRui Ueyama   for (const ulittle16_t *I = Start, *E = Start + NumEntries;
987ad882ba8SRui Ueyama        I < E; ++I, ++Offset) {
988ad882ba8SRui Ueyama     if (*I != Index)
989ad882ba8SRui Ueyama       continue;
990ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(
991ad882ba8SRui Ueyama             ExportTable->NamePointerRVA, IntPtr))
992ad882ba8SRui Ueyama       return EC;
993ad882ba8SRui Ueyama     const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
994ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
995ad882ba8SRui Ueyama       return EC;
996ad882ba8SRui Ueyama     Result = StringRef(reinterpret_cast<const char *>(IntPtr));
997ad882ba8SRui Ueyama     return object_error::success;
998ad882ba8SRui Ueyama   }
999ad882ba8SRui Ueyama   Result = "";
1000ad882ba8SRui Ueyama   return object_error::success;
1001ad882ba8SRui Ueyama }
1002ad882ba8SRui Ueyama 
10038e90adafSMichael J. Spencer namespace llvm {
10048e90adafSMichael J. Spencer ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
1005*8ff24d25SRui Ueyama   error_code EC;
1006*8ff24d25SRui Ueyama   return new COFFObjectFile(Object, EC);
10078e90adafSMichael J. Spencer }
10088e90adafSMichael J. Spencer } // end namespace llvm
1009