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"
19f078eff3SRui Ueyama #include "llvm/Support/COFF.h"
20c2bed429SRui Ueyama #include "llvm/Support/Debug.h"
21c2bed429SRui Ueyama #include "llvm/Support/raw_ostream.h"
22981af002SWill Dietz #include <cctype>
239d2c15efSNico Rieck #include <limits>
248e90adafSMichael J. Spencer 
258e90adafSMichael J. Spencer using namespace llvm;
268e90adafSMichael J. Spencer using namespace object;
278e90adafSMichael J. Spencer 
288e90adafSMichael J. Spencer using support::ulittle8_t;
298e90adafSMichael J. Spencer using support::ulittle16_t;
308e90adafSMichael J. Spencer using support::ulittle32_t;
318e90adafSMichael J. Spencer using support::little16_t;
328e90adafSMichael J. Spencer 
331d6167fdSMichael J. Spencer // Returns false if size is greater than the buffer size. And sets ec.
34686738e2SRui Ueyama static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
358ff24d25SRui Ueyama   if (M->getBufferSize() < Size) {
368ff24d25SRui Ueyama     EC = object_error::unexpected_eof;
371d6167fdSMichael J. Spencer     return false;
381d6167fdSMichael J. Spencer   }
391d6167fdSMichael J. Spencer   return true;
408e90adafSMichael J. Spencer }
418e90adafSMichael J. Spencer 
42ed64342bSRui Ueyama // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
43ed64342bSRui Ueyama // Returns unexpected_eof if error.
44ed64342bSRui Ueyama template<typename T>
45686738e2SRui Ueyama static error_code getObject(const T *&Obj, const MemoryBuffer *M,
46686738e2SRui Ueyama                             const uint8_t *Ptr, const size_t Size = sizeof(T)) {
47ed64342bSRui Ueyama   uintptr_t Addr = uintptr_t(Ptr);
48ed64342bSRui Ueyama   if (Addr + Size < Addr ||
49ed64342bSRui Ueyama       Addr + Size < Size ||
50ed64342bSRui Ueyama       Addr + Size > uintptr_t(M->getBufferEnd())) {
51ed64342bSRui Ueyama     return object_error::unexpected_eof;
521d6167fdSMichael J. Spencer   }
53ed64342bSRui Ueyama   Obj = reinterpret_cast<const T *>(Addr);
54ed64342bSRui Ueyama   return object_error::success;
551d6167fdSMichael J. Spencer }
561d6167fdSMichael J. Spencer 
579d2c15efSNico Rieck // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
589d2c15efSNico Rieck // prefixed slashes.
599d2c15efSNico Rieck static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
609d2c15efSNico Rieck   assert(Str.size() <= 6 && "String too long, possible overflow.");
619d2c15efSNico Rieck   if (Str.size() > 6)
629d2c15efSNico Rieck     return true;
639d2c15efSNico Rieck 
649d2c15efSNico Rieck   uint64_t Value = 0;
659d2c15efSNico Rieck   while (!Str.empty()) {
669d2c15efSNico Rieck     unsigned CharVal;
679d2c15efSNico Rieck     if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
689d2c15efSNico Rieck       CharVal = Str[0] - 'A';
699d2c15efSNico Rieck     else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
709d2c15efSNico Rieck       CharVal = Str[0] - 'a' + 26;
719d2c15efSNico Rieck     else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
729d2c15efSNico Rieck       CharVal = Str[0] - '0' + 52;
739d2c15efSNico Rieck     else if (Str[0] == '+') // 62
745500b07cSRui Ueyama       CharVal = 62;
759d2c15efSNico Rieck     else if (Str[0] == '/') // 63
765500b07cSRui Ueyama       CharVal = 63;
779d2c15efSNico Rieck     else
789d2c15efSNico Rieck       return true;
799d2c15efSNico Rieck 
809d2c15efSNico Rieck     Value = (Value * 64) + CharVal;
819d2c15efSNico Rieck     Str = Str.substr(1);
829d2c15efSNico Rieck   }
839d2c15efSNico Rieck 
849d2c15efSNico Rieck   if (Value > std::numeric_limits<uint32_t>::max())
859d2c15efSNico Rieck     return true;
869d2c15efSNico Rieck 
879d2c15efSNico Rieck   Result = static_cast<uint32_t>(Value);
889d2c15efSNico Rieck   return false;
899d2c15efSNico Rieck }
909d2c15efSNico Rieck 
918ff24d25SRui Ueyama const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
928ff24d25SRui Ueyama   const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
931d6167fdSMichael J. Spencer 
941d6167fdSMichael J. Spencer # ifndef NDEBUG
951d6167fdSMichael J. Spencer   // Verify that the symbol points to a valid entry in the symbol table.
968ff24d25SRui Ueyama   uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
978ff24d25SRui Ueyama   if (Offset < COFFHeader->PointerToSymbolTable
988ff24d25SRui Ueyama       || Offset >= COFFHeader->PointerToSymbolTable
9982ebd8e3SRui Ueyama          + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
1001d6167fdSMichael J. Spencer     report_fatal_error("Symbol was outside of symbol table.");
1011d6167fdSMichael J. Spencer 
1028ff24d25SRui Ueyama   assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
1031d6167fdSMichael J. Spencer          == 0 && "Symbol did not point to the beginning of a symbol");
1041d6167fdSMichael J. Spencer # endif
1051d6167fdSMichael J. Spencer 
1068ff24d25SRui Ueyama   return Addr;
1071d6167fdSMichael J. Spencer }
1081d6167fdSMichael J. Spencer 
1098ff24d25SRui Ueyama const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
1108ff24d25SRui Ueyama   const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
1111d6167fdSMichael J. Spencer 
1121d6167fdSMichael J. Spencer # ifndef NDEBUG
1131d6167fdSMichael J. Spencer   // Verify that the section points to a valid entry in the section table.
1148ff24d25SRui Ueyama   if (Addr < SectionTable
1158ff24d25SRui Ueyama       || Addr >= (SectionTable + COFFHeader->NumberOfSections))
1161d6167fdSMichael J. Spencer     report_fatal_error("Section was outside of section table.");
1171d6167fdSMichael J. Spencer 
1188ff24d25SRui Ueyama   uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
1198ff24d25SRui Ueyama   assert(Offset % sizeof(coff_section) == 0 &&
1201d6167fdSMichael J. Spencer          "Section did not point to the beginning of a section");
1211d6167fdSMichael J. Spencer # endif
1221d6167fdSMichael J. Spencer 
1238ff24d25SRui Ueyama   return Addr;
1241d6167fdSMichael J. Spencer }
1251d6167fdSMichael J. Spencer 
1265e812afaSRafael Espindola void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
1278ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1288ff24d25SRui Ueyama   Symb += 1 + Symb->NumberOfAuxSymbols;
1298ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Symb);
1301d6167fdSMichael J. Spencer }
1311d6167fdSMichael J. Spencer 
1328ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
1331d6167fdSMichael J. Spencer                                          StringRef &Result) const {
1348ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1358ff24d25SRui Ueyama   return getSymbolName(Symb, Result);
1368e90adafSMichael J. Spencer }
1378e90adafSMichael J. Spencer 
1388ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
1391d6167fdSMichael J. Spencer                                             uint64_t &Result) const {
1408ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
1415ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
1428ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1438ff24d25SRui Ueyama     return EC;
144e62ab11fSRafael Espindola 
1458ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
1461d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
1471d6167fdSMichael J. Spencer   else if (Section)
1488ff24d25SRui Ueyama     Result = Section->PointerToRawData + Symb->Value;
1491d6167fdSMichael J. Spencer   else
1508ff24d25SRui Ueyama     Result = Symb->Value;
1511d6167fdSMichael J. Spencer   return object_error::success;
1528e90adafSMichael J. Spencer }
1538e90adafSMichael J. Spencer 
1548ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
15575d1cf33SBenjamin Kramer                                             uint64_t &Result) const {
1568ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
15775d1cf33SBenjamin Kramer   const coff_section *Section = NULL;
1588ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
1598ff24d25SRui Ueyama     return EC;
160e62ab11fSRafael Espindola 
1618ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
16275d1cf33SBenjamin Kramer     Result = UnknownAddressOrSize;
16375d1cf33SBenjamin Kramer   else if (Section)
1648ff24d25SRui Ueyama     Result = Section->VirtualAddress + Symb->Value;
16575d1cf33SBenjamin Kramer   else
1668ff24d25SRui Ueyama     Result = Symb->Value;
16775d1cf33SBenjamin Kramer   return object_error::success;
16875d1cf33SBenjamin Kramer }
16975d1cf33SBenjamin Kramer 
1708ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
171d3946676SMichael J. Spencer                                          SymbolRef::Type &Result) const {
1728ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
17375d1cf33SBenjamin Kramer   Result = SymbolRef::ST_Other;
1748ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
1758ff24d25SRui Ueyama       Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
1767e4b976cSDavid Meyer     Result = SymbolRef::ST_Unknown;
177ddf28f2bSDavid Majnemer   } else if (Symb->isFunctionDefinition()) {
17875d1cf33SBenjamin Kramer     Result = SymbolRef::ST_Function;
17975d1cf33SBenjamin Kramer   } else {
18006adfac8SRafael Espindola     uint32_t Characteristics = 0;
181f078eff3SRui Ueyama     if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
18206adfac8SRafael Espindola       const coff_section *Section = NULL;
1838ff24d25SRui Ueyama       if (error_code EC = getSection(Symb->SectionNumber, Section))
1848ff24d25SRui Ueyama         return EC;
18506adfac8SRafael Espindola       Characteristics = Section->Characteristics;
18675d1cf33SBenjamin Kramer     }
18706adfac8SRafael Espindola     if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
18806adfac8SRafael Espindola         ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
18906adfac8SRafael Espindola       Result = SymbolRef::ST_Data;
19075d1cf33SBenjamin Kramer   }
19175d1cf33SBenjamin Kramer   return object_error::success;
19275d1cf33SBenjamin Kramer }
19375d1cf33SBenjamin Kramer 
19420122a43SRafael Espindola uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
1958ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
19620122a43SRafael Espindola   uint32_t Result = SymbolRef::SF_None;
19775d1cf33SBenjamin Kramer 
198975e115eSRafael Espindola   // TODO: Correctly set SF_FormatSpecific, SF_Common
1997e4b976cSDavid Meyer 
20022fe9c1eSRafael Espindola   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
20122fe9c1eSRafael Espindola     if (Symb->Value == 0)
2027e4b976cSDavid Meyer       Result |= SymbolRef::SF_Undefined;
20322fe9c1eSRafael Espindola     else
20422fe9c1eSRafael Espindola       Result |= SymbolRef::SF_Common;
20522fe9c1eSRafael Espindola   }
20622fe9c1eSRafael Espindola 
2071df4b84dSDavid Meyer 
2081df4b84dSDavid Meyer   // TODO: This are certainly too restrictive.
2098ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
2101df4b84dSDavid Meyer     Result |= SymbolRef::SF_Global;
2111df4b84dSDavid Meyer 
2128ff24d25SRui Ueyama   if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
2131df4b84dSDavid Meyer     Result |= SymbolRef::SF_Weak;
2141df4b84dSDavid Meyer 
2158ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
2161df4b84dSDavid Meyer     Result |= SymbolRef::SF_Absolute;
2171df4b84dSDavid Meyer 
21820122a43SRafael Espindola   return Result;
21901759754SMichael J. Spencer }
22001759754SMichael J. Spencer 
2218ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
2221d6167fdSMichael J. Spencer                                          uint64_t &Result) const {
2238e90adafSMichael J. Spencer   // FIXME: Return the correct size. This requires looking at all the symbols
2248e90adafSMichael J. Spencer   //        in the same section as this symbol, and looking for either the next
2258e90adafSMichael J. Spencer   //        symbol, or the end of the section.
2268ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
2275ebaed24SMichael J. Spencer   const coff_section *Section = NULL;
2288ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, Section))
2298ff24d25SRui Ueyama     return EC;
230e62ab11fSRafael Espindola 
2318ff24d25SRui Ueyama   if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
2321d6167fdSMichael J. Spencer     Result = UnknownAddressOrSize;
2331d6167fdSMichael J. Spencer   else if (Section)
2348ff24d25SRui Ueyama     Result = Section->SizeOfRawData - Symb->Value;
2351d6167fdSMichael J. Spencer   else
2361d6167fdSMichael J. Spencer     Result = 0;
2371d6167fdSMichael J. Spencer   return object_error::success;
2388e90adafSMichael J. Spencer }
2398e90adafSMichael J. Spencer 
2408ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
24132173153SMichael J. Spencer                                             section_iterator &Result) const {
2428ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(Ref);
243f078eff3SRui Ueyama   if (COFF::isReservedSectionNumber(Symb->SectionNumber)) {
244b5155a57SRafael Espindola     Result = section_end();
245f078eff3SRui Ueyama   } else {
2468ff24d25SRui Ueyama     const coff_section *Sec = 0;
2478ff24d25SRui Ueyama     if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
2488ff24d25SRui Ueyama     DataRefImpl Ref;
2498ff24d25SRui Ueyama     Ref.p = reinterpret_cast<uintptr_t>(Sec);
2508ff24d25SRui Ueyama     Result = section_iterator(SectionRef(Ref, this));
25132173153SMichael J. Spencer   }
25232173153SMichael J. Spencer   return object_error::success;
25332173153SMichael J. Spencer }
25432173153SMichael J. Spencer 
2558ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
2564f223bf7STim Northover                                           uint64_t &Val) const {
2574f223bf7STim Northover   report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
2584f223bf7STim Northover }
2594f223bf7STim Northover 
2605e812afaSRafael Espindola void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
2618ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2628ff24d25SRui Ueyama   Sec += 1;
2638ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(Sec);
2648e90adafSMichael J. Spencer }
2658e90adafSMichael J. Spencer 
2668ff24d25SRui Ueyama error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
2671d6167fdSMichael J. Spencer                                           StringRef &Result) const {
2688ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2698ff24d25SRui Ueyama   return getSectionName(Sec, Result);
2708e90adafSMichael J. Spencer }
2718e90adafSMichael J. Spencer 
2728ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
2731d6167fdSMichael J. Spencer                                              uint64_t &Result) const {
2748ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2758ff24d25SRui Ueyama   Result = Sec->VirtualAddress;
2761d6167fdSMichael J. Spencer   return object_error::success;
2778e90adafSMichael J. Spencer }
2788e90adafSMichael J. Spencer 
2798ff24d25SRui Ueyama error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
2801d6167fdSMichael J. Spencer                                           uint64_t &Result) const {
2818ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2828ff24d25SRui Ueyama   Result = Sec->SizeOfRawData;
2831d6167fdSMichael J. Spencer   return object_error::success;
2848e90adafSMichael J. Spencer }
2858e90adafSMichael J. Spencer 
2868ff24d25SRui Ueyama error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
2871d6167fdSMichael J. Spencer                                               StringRef &Result) const {
2888ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2899da9e693SMichael J. Spencer   ArrayRef<uint8_t> Res;
2908ff24d25SRui Ueyama   error_code EC = getSectionContents(Sec, Res);
2919da9e693SMichael J. Spencer   Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
2929da9e693SMichael J. Spencer   return EC;
2938e90adafSMichael J. Spencer }
2948e90adafSMichael J. Spencer 
2958ff24d25SRui Ueyama error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
2967989460aSMichael J. Spencer                                                uint64_t &Res) const {
2978ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
2988ff24d25SRui Ueyama   if (!Sec)
2997989460aSMichael J. Spencer     return object_error::parse_failed;
3008ff24d25SRui Ueyama   Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
3017989460aSMichael J. Spencer   return object_error::success;
3027989460aSMichael J. Spencer }
3037989460aSMichael J. Spencer 
3048ff24d25SRui Ueyama error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
3051d6167fdSMichael J. Spencer                                          bool &Result) const {
3068ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3078ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
3081d6167fdSMichael J. Spencer   return object_error::success;
3098e90adafSMichael J. Spencer }
3108e90adafSMichael J. Spencer 
3118ff24d25SRui Ueyama error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
312800619f2SMichael J. Spencer                                          bool &Result) const {
3138ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3148ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
315800619f2SMichael J. Spencer   return object_error::success;
316800619f2SMichael J. Spencer }
317800619f2SMichael J. Spencer 
3188ff24d25SRui Ueyama error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
319800619f2SMichael J. Spencer                                         bool &Result) const {
3208ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3218ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
322800619f2SMichael J. Spencer   return object_error::success;
323800619f2SMichael J. Spencer }
324800619f2SMichael J. Spencer 
3258ff24d25SRui Ueyama error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
3262138ef6dSPreston Gurd                                                          bool &Result) const {
3272138ef6dSPreston Gurd   // FIXME: Unimplemented
3282138ef6dSPreston Gurd   Result = true;
3292138ef6dSPreston Gurd   return object_error::success;
3302138ef6dSPreston Gurd }
3312138ef6dSPreston Gurd 
3328ff24d25SRui Ueyama error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
3332138ef6dSPreston Gurd                                            bool &Result) const {
3348ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3358ff24d25SRui Ueyama   Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
3362138ef6dSPreston Gurd   return object_error::success;
3372138ef6dSPreston Gurd }
3382138ef6dSPreston Gurd 
3398ff24d25SRui Ueyama error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
3402138ef6dSPreston Gurd                                              bool &Result) const {
341b96a320aSAndrew Kaylor   // FIXME: Unimplemented.
3422138ef6dSPreston Gurd   Result = false;
3432138ef6dSPreston Gurd   return object_error::success;
3442138ef6dSPreston Gurd }
3452138ef6dSPreston Gurd 
3468ff24d25SRui Ueyama error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
3473f31fa05SAndrew Kaylor                                                 bool &Result) const {
3483f31fa05SAndrew Kaylor   // FIXME: Unimplemented.
3493f31fa05SAndrew Kaylor   Result = false;
3503f31fa05SAndrew Kaylor   return object_error::success;
3513f31fa05SAndrew Kaylor }
3523f31fa05SAndrew Kaylor 
3538ff24d25SRui Ueyama error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
3548ff24d25SRui Ueyama                                                  DataRefImpl SymbRef,
355f6f3e81cSBenjamin Kramer                                                  bool &Result) const {
3568ff24d25SRui Ueyama   const coff_section *Sec = toSec(SecRef);
3578ff24d25SRui Ueyama   const coff_symbol *Symb = toSymb(SymbRef);
3588ff24d25SRui Ueyama   const coff_section *SymbSec = 0;
3598ff24d25SRui Ueyama   if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
3608ff24d25SRui Ueyama   if (SymbSec == Sec)
3619a28851eSMichael J. Spencer     Result = true;
3629a28851eSMichael J. Spencer   else
363f6f3e81cSBenjamin Kramer     Result = false;
364f6f3e81cSBenjamin Kramer   return object_error::success;
365f6f3e81cSBenjamin Kramer }
366f6f3e81cSBenjamin Kramer 
3678ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
3688ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
3698ff24d25SRui Ueyama   DataRefImpl Ret;
370*827c8a2bSRui Ueyama   if (Sec->NumberOfRelocations == 0) {
3718ff24d25SRui Ueyama     Ret.p = 0;
372*827c8a2bSRui Ueyama   } else {
373*827c8a2bSRui Ueyama     auto begin = reinterpret_cast<const coff_relocation*>(
374*827c8a2bSRui Ueyama         base() + Sec->PointerToRelocations);
375*827c8a2bSRui Ueyama     if (Sec->hasExtendedRelocations()) {
376*827c8a2bSRui Ueyama       // Skip the first relocation entry repurposed to store the number of
377*827c8a2bSRui Ueyama       // relocations.
378*827c8a2bSRui Ueyama       begin++;
379*827c8a2bSRui Ueyama     }
380*827c8a2bSRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(begin);
381*827c8a2bSRui Ueyama   }
3828ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
383e5fd0047SMichael J. Spencer }
384e5fd0047SMichael J. Spencer 
385*827c8a2bSRui Ueyama static uint32_t getNumberOfRelocations(const coff_section *Sec,
386*827c8a2bSRui Ueyama                                        const uint8_t *base) {
387*827c8a2bSRui Ueyama   // The field for the number of relocations in COFF section table is only
388*827c8a2bSRui Ueyama   // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
389*827c8a2bSRui Ueyama   // NumberOfRelocations field, and the actual relocation count is stored in the
390*827c8a2bSRui Ueyama   // VirtualAddress field in the first relocation entry.
391*827c8a2bSRui Ueyama   if (Sec->hasExtendedRelocations()) {
392*827c8a2bSRui Ueyama     auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
393*827c8a2bSRui Ueyama         base + Sec->PointerToRelocations);
394*827c8a2bSRui Ueyama     return FirstReloc->VirtualAddress;
395*827c8a2bSRui Ueyama   }
396*827c8a2bSRui Ueyama   return Sec->NumberOfRelocations;
397*827c8a2bSRui Ueyama }
398*827c8a2bSRui Ueyama 
3998ff24d25SRui Ueyama relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
4008ff24d25SRui Ueyama   const coff_section *Sec = toSec(Ref);
4018ff24d25SRui Ueyama   DataRefImpl Ret;
402*827c8a2bSRui Ueyama   if (Sec->NumberOfRelocations == 0) {
4038ff24d25SRui Ueyama     Ret.p = 0;
404*827c8a2bSRui Ueyama   } else {
405*827c8a2bSRui Ueyama     auto begin = reinterpret_cast<const coff_relocation*>(
406*827c8a2bSRui Ueyama         base() + Sec->PointerToRelocations);
407*827c8a2bSRui Ueyama     uint32_t NumReloc = getNumberOfRelocations(Sec, base());
408*827c8a2bSRui Ueyama     Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
409*827c8a2bSRui Ueyama   }
4108ff24d25SRui Ueyama   return relocation_iterator(RelocationRef(Ret, this));
411e5fd0047SMichael J. Spencer }
412e5fd0047SMichael J. Spencer 
413c2bed429SRui Ueyama // Initialize the pointer to the symbol table.
414c2bed429SRui Ueyama error_code COFFObjectFile::initSymbolTablePtr() {
4158ff24d25SRui Ueyama   if (error_code EC = getObject(
416c2bed429SRui Ueyama           SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
417c2bed429SRui Ueyama           COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
4188ff24d25SRui Ueyama     return EC;
419c2bed429SRui Ueyama 
420c2bed429SRui Ueyama   // Find string table. The first four byte of the string table contains the
421c2bed429SRui Ueyama   // total size of the string table, including the size field itself. If the
422c2bed429SRui Ueyama   // string table is empty, the value of the first four byte would be 4.
423c2bed429SRui Ueyama   const uint8_t *StringTableAddr =
424c2bed429SRui Ueyama       base() + COFFHeader->PointerToSymbolTable +
425c2bed429SRui Ueyama       COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
426c2bed429SRui Ueyama   const ulittle32_t *StringTableSizePtr;
4278ff24d25SRui Ueyama   if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
4288ff24d25SRui Ueyama     return EC;
429c2bed429SRui Ueyama   StringTableSize = *StringTableSizePtr;
4308ff24d25SRui Ueyama   if (error_code EC =
431c2bed429SRui Ueyama       getObject(StringTable, Data, StringTableAddr, StringTableSize))
4328ff24d25SRui Ueyama     return EC;
433c2bed429SRui Ueyama 
434773a5795SNico Rieck   // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
435773a5795SNico Rieck   // tools like cvtres write a size of 0 for an empty table instead of 4.
436773a5795SNico Rieck   if (StringTableSize < 4)
437773a5795SNico Rieck       StringTableSize = 4;
438773a5795SNico Rieck 
439c2bed429SRui Ueyama   // Check that the string table is null terminated if has any in it.
440773a5795SNico Rieck   if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
441c2bed429SRui Ueyama     return  object_error::parse_failed;
442c2bed429SRui Ueyama   return object_error::success;
443c2bed429SRui Ueyama }
444c2bed429SRui Ueyama 
445215a586cSRui Ueyama // Returns the file offset for the given VA.
446b7a40081SRui Ueyama error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
447b6eb264aSRui Ueyama   uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
448b6eb264aSRui Ueyama                                   : (uint64_t)PE32PlusHeader->ImageBase;
449b7a40081SRui Ueyama   uint64_t Rva = Addr - ImageBase;
450b7a40081SRui Ueyama   assert(Rva <= UINT32_MAX);
451b7a40081SRui Ueyama   return getRvaPtr((uint32_t)Rva, Res);
452215a586cSRui Ueyama }
453215a586cSRui Ueyama 
454c2bed429SRui Ueyama // Returns the file offset for the given RVA.
455215a586cSRui Ueyama error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
45627dc8394SAlexey Samsonov   for (const SectionRef &S : sections()) {
45727dc8394SAlexey Samsonov     const coff_section *Section = getCOFFSection(S);
458c2bed429SRui Ueyama     uint32_t SectionStart = Section->VirtualAddress;
459c2bed429SRui Ueyama     uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
460215a586cSRui Ueyama     if (SectionStart <= Addr && Addr < SectionEnd) {
461215a586cSRui Ueyama       uint32_t Offset = Addr - SectionStart;
462c2bed429SRui Ueyama       Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
463c2bed429SRui Ueyama       return object_error::success;
464c2bed429SRui Ueyama     }
465c2bed429SRui Ueyama   }
466c2bed429SRui Ueyama   return object_error::parse_failed;
467c2bed429SRui Ueyama }
468c2bed429SRui Ueyama 
469c2bed429SRui Ueyama // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
470c2bed429SRui Ueyama // table entry.
471c2bed429SRui Ueyama error_code COFFObjectFile::
472c2bed429SRui Ueyama getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
473c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
4748ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(Rva, IntPtr))
4758ff24d25SRui Ueyama     return EC;
476c2bed429SRui Ueyama   const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
477c2bed429SRui Ueyama   Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
478c2bed429SRui Ueyama   Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
479c2bed429SRui Ueyama   return object_error::success;
480c2bed429SRui Ueyama }
481c2bed429SRui Ueyama 
482c2bed429SRui Ueyama // Find the import table.
483c2bed429SRui Ueyama error_code COFFObjectFile::initImportTablePtr() {
484c2bed429SRui Ueyama   // First, we get the RVA of the import table. If the file lacks a pointer to
485c2bed429SRui Ueyama   // the import table, do nothing.
486c2bed429SRui Ueyama   const data_directory *DataEntry;
487c2bed429SRui Ueyama   if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
488c2bed429SRui Ueyama     return object_error::success;
489c2bed429SRui Ueyama 
490c2bed429SRui Ueyama   // Do nothing if the pointer to import table is NULL.
491c2bed429SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
492c2bed429SRui Ueyama     return object_error::success;
493c2bed429SRui Ueyama 
494c2bed429SRui Ueyama   uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
495c2bed429SRui Ueyama   NumberOfImportDirectory = DataEntry->Size /
496c2bed429SRui Ueyama       sizeof(import_directory_table_entry);
497c2bed429SRui Ueyama 
498c2bed429SRui Ueyama   // Find the section that contains the RVA. This is needed because the RVA is
499c2bed429SRui Ueyama   // the import table's memory address which is different from its file offset.
500c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
5018ff24d25SRui Ueyama   if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
5028ff24d25SRui Ueyama     return EC;
503c2bed429SRui Ueyama   ImportDirectory = reinterpret_cast<
504c2bed429SRui Ueyama       const import_directory_table_entry *>(IntPtr);
505ad882ba8SRui Ueyama   return object_error::success;
506ad882ba8SRui Ueyama }
507c2bed429SRui Ueyama 
508ad882ba8SRui Ueyama // Find the export table.
509ad882ba8SRui Ueyama error_code COFFObjectFile::initExportTablePtr() {
510ad882ba8SRui Ueyama   // First, we get the RVA of the export table. If the file lacks a pointer to
511ad882ba8SRui Ueyama   // the export table, do nothing.
512ad882ba8SRui Ueyama   const data_directory *DataEntry;
513ad882ba8SRui Ueyama   if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
514ad882ba8SRui Ueyama     return object_error::success;
515ad882ba8SRui Ueyama 
516ad882ba8SRui Ueyama   // Do nothing if the pointer to export table is NULL.
517ad882ba8SRui Ueyama   if (DataEntry->RelativeVirtualAddress == 0)
518ad882ba8SRui Ueyama     return object_error::success;
519ad882ba8SRui Ueyama 
520ad882ba8SRui Ueyama   uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
521ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
522ad882ba8SRui Ueyama   if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
523ad882ba8SRui Ueyama     return EC;
52424fc2d64SRui Ueyama   ExportDirectory =
52524fc2d64SRui Ueyama       reinterpret_cast<const export_directory_table_entry *>(IntPtr);
526ad882ba8SRui Ueyama   return object_error::success;
527c2bed429SRui Ueyama }
528c2bed429SRui Ueyama 
529afcc3df7SRafael Espindola COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
530afcc3df7SRafael Espindola                                bool BufferOwned)
531afcc3df7SRafael Espindola     : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0),
53210ed9ddcSRui Ueyama       PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0),
53310ed9ddcSRui Ueyama       SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0),
534afcc3df7SRafael Espindola       NumberOfImportDirectory(0), ExportDirectory(0) {
5351d6167fdSMichael J. Spencer   // Check that we at least have enough room for a header.
5368ff24d25SRui Ueyama   if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
537ee066fc4SEric Christopher 
53882ebd8e3SRui Ueyama   // The current location in the file where we are looking at.
53982ebd8e3SRui Ueyama   uint64_t CurPtr = 0;
54082ebd8e3SRui Ueyama 
54182ebd8e3SRui Ueyama   // PE header is optional and is present only in executables. If it exists,
54282ebd8e3SRui Ueyama   // it is placed right after COFF header.
5438ff24d25SRui Ueyama   bool HasPEHeader = false;
544ee066fc4SEric Christopher 
5451d6167fdSMichael J. Spencer   // Check if this is a PE/COFF file.
546ec29b121SMichael J. Spencer   if (base()[0] == 0x4d && base()[1] == 0x5a) {
547ee066fc4SEric Christopher     // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
548ee066fc4SEric Christopher     // PE signature to find 'normal' COFF header.
5498ff24d25SRui Ueyama     if (!checkSize(Data, EC, 0x3c + 8)) return;
55082ebd8e3SRui Ueyama     CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
55182ebd8e3SRui Ueyama     // Check the PE magic bytes. ("PE\0\0")
55282ebd8e3SRui Ueyama     if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
5538ff24d25SRui Ueyama       EC = object_error::parse_failed;
5541d6167fdSMichael J. Spencer       return;
5551d6167fdSMichael J. Spencer     }
55682ebd8e3SRui Ueyama     CurPtr += 4; // Skip the PE magic bytes.
5578ff24d25SRui Ueyama     HasPEHeader = true;
558ee066fc4SEric Christopher   }
559ee066fc4SEric Christopher 
5608ff24d25SRui Ueyama   if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
5611d6167fdSMichael J. Spencer     return;
56282ebd8e3SRui Ueyama   CurPtr += sizeof(coff_file_header);
56382ebd8e3SRui Ueyama 
5648ff24d25SRui Ueyama   if (HasPEHeader) {
56510ed9ddcSRui Ueyama     const pe32_header *Header;
56610ed9ddcSRui Ueyama     if ((EC = getObject(Header, Data, base() + CurPtr)))
56782ebd8e3SRui Ueyama       return;
56810ed9ddcSRui Ueyama 
56910ed9ddcSRui Ueyama     const uint8_t *DataDirAddr;
57010ed9ddcSRui Ueyama     uint64_t DataDirSize;
57110ed9ddcSRui Ueyama     if (Header->Magic == 0x10b) {
57210ed9ddcSRui Ueyama       PE32Header = Header;
57310ed9ddcSRui Ueyama       DataDirAddr = base() + CurPtr + sizeof(pe32_header);
57410ed9ddcSRui Ueyama       DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
57510ed9ddcSRui Ueyama     } else if (Header->Magic == 0x20b) {
57610ed9ddcSRui Ueyama       PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
57710ed9ddcSRui Ueyama       DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
57810ed9ddcSRui Ueyama       DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
57910ed9ddcSRui Ueyama     } else {
58010ed9ddcSRui Ueyama       // It's neither PE32 nor PE32+.
58110ed9ddcSRui Ueyama       EC = object_error::parse_failed;
582ed64342bSRui Ueyama       return;
583ed64342bSRui Ueyama     }
58410ed9ddcSRui Ueyama     if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
58510ed9ddcSRui Ueyama       return;
58682ebd8e3SRui Ueyama     CurPtr += COFFHeader->SizeOfOptionalHeader;
58782ebd8e3SRui Ueyama   }
5881d6167fdSMichael J. Spencer 
589692410efSRafael Espindola   if (COFFHeader->isImportLibrary())
590692410efSRafael Espindola     return;
591692410efSRafael Espindola 
5928ff24d25SRui Ueyama   if ((EC = getObject(SectionTable, Data, base() + CurPtr,
593ed64342bSRui Ueyama                       COFFHeader->NumberOfSections * sizeof(coff_section))))
5941d6167fdSMichael J. Spencer     return;
5951d6167fdSMichael J. Spencer 
596c2bed429SRui Ueyama   // Initialize the pointer to the symbol table.
597c2bed429SRui Ueyama   if (COFFHeader->PointerToSymbolTable != 0)
5988ff24d25SRui Ueyama     if ((EC = initSymbolTablePtr()))
5991d6167fdSMichael J. Spencer       return;
6008e90adafSMichael J. Spencer 
601c2bed429SRui Ueyama   // Initialize the pointer to the beginning of the import table.
6028ff24d25SRui Ueyama   if ((EC = initImportTablePtr()))
603ed64342bSRui Ueyama     return;
6041d6167fdSMichael J. Spencer 
605ad882ba8SRui Ueyama   // Initialize the pointer to the export table.
6068ff24d25SRui Ueyama   if ((EC = initExportTablePtr()))
607ad882ba8SRui Ueyama     return;
608ad882ba8SRui Ueyama 
6098ff24d25SRui Ueyama   EC = object_error::success;
6108e90adafSMichael J. Spencer }
6118e90adafSMichael J. Spencer 
612f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
6138ff24d25SRui Ueyama   DataRefImpl Ret;
6148ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
615f12b8282SRafael Espindola   return basic_symbol_iterator(SymbolRef(Ret, this));
6168e90adafSMichael J. Spencer }
6178e90adafSMichael J. Spencer 
618f12b8282SRafael Espindola basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
6198e90adafSMichael J. Spencer   // The symbol table ends where the string table begins.
6208ff24d25SRui Ueyama   DataRefImpl Ret;
6218ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(StringTable);
622f12b8282SRafael Espindola   return basic_symbol_iterator(SymbolRef(Ret, this));
6238e90adafSMichael J. Spencer }
6248e90adafSMichael J. Spencer 
625b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_begin() const {
6262fc34c5fSDavid Meyer   // TODO: implement
6272fc34c5fSDavid Meyer   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
6282fc34c5fSDavid Meyer }
6292fc34c5fSDavid Meyer 
630b5155a57SRafael Espindola library_iterator COFFObjectFile::needed_library_end() const {
6312fc34c5fSDavid Meyer   // TODO: implement
6322fc34c5fSDavid Meyer   report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
6332fc34c5fSDavid Meyer }
6342fc34c5fSDavid Meyer 
635c429b80dSDavid Meyer StringRef COFFObjectFile::getLoadName() const {
636c429b80dSDavid Meyer   // COFF does not have this field.
637c429b80dSDavid Meyer   return "";
638c429b80dSDavid Meyer }
639c429b80dSDavid Meyer 
640bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_begin() const {
641a045b73aSRui Ueyama   return import_directory_iterator(
642a045b73aSRui Ueyama       ImportDirectoryEntryRef(ImportDirectory, 0, this));
643c2bed429SRui Ueyama }
644c2bed429SRui Ueyama 
645bc654b18SRui Ueyama import_directory_iterator COFFObjectFile::import_directory_end() const {
646a045b73aSRui Ueyama   return import_directory_iterator(
647a045b73aSRui Ueyama       ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
648c2bed429SRui Ueyama }
649c429b80dSDavid Meyer 
650ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_begin() const {
651ad882ba8SRui Ueyama   return export_directory_iterator(
652ad882ba8SRui Ueyama       ExportDirectoryEntryRef(ExportDirectory, 0, this));
653ad882ba8SRui Ueyama }
654ad882ba8SRui Ueyama 
655ad882ba8SRui Ueyama export_directory_iterator COFFObjectFile::export_directory_end() const {
656ad882ba8SRui Ueyama   if (ExportDirectory == 0)
657ad882ba8SRui Ueyama     return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
6588ff24d25SRui Ueyama   ExportDirectoryEntryRef Ref(ExportDirectory,
659ad882ba8SRui Ueyama                               ExportDirectory->AddressTableEntries, this);
6608ff24d25SRui Ueyama   return export_directory_iterator(Ref);
661ad882ba8SRui Ueyama }
662ad882ba8SRui Ueyama 
663b5155a57SRafael Espindola section_iterator COFFObjectFile::section_begin() const {
6648ff24d25SRui Ueyama   DataRefImpl Ret;
6658ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
6668ff24d25SRui Ueyama   return section_iterator(SectionRef(Ret, this));
6678e90adafSMichael J. Spencer }
6688e90adafSMichael J. Spencer 
669b5155a57SRafael Espindola section_iterator COFFObjectFile::section_end() const {
6708ff24d25SRui Ueyama   DataRefImpl Ret;
6718ff24d25SRui Ueyama   int NumSections = COFFHeader->isImportLibrary()
67215ba1e20SRui Ueyama       ? 0 : COFFHeader->NumberOfSections;
6738ff24d25SRui Ueyama   Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
6748ff24d25SRui Ueyama   return section_iterator(SectionRef(Ret, this));
6758e90adafSMichael J. Spencer }
6768e90adafSMichael J. Spencer 
6778e90adafSMichael J. Spencer uint8_t COFFObjectFile::getBytesInAddress() const {
6780324b672SMichael J. Spencer   return getArch() == Triple::x86_64 ? 8 : 4;
6798e90adafSMichael J. Spencer }
6808e90adafSMichael J. Spencer 
6818e90adafSMichael J. Spencer StringRef COFFObjectFile::getFileFormatName() const {
68282ebd8e3SRui Ueyama   switch(COFFHeader->Machine) {
6838e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
6848e90adafSMichael J. Spencer     return "COFF-i386";
6858e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
6868e90adafSMichael J. Spencer     return "COFF-x86-64";
6879b7c0af2SSaleem Abdulrasool   case COFF::IMAGE_FILE_MACHINE_ARMNT:
6889b7c0af2SSaleem Abdulrasool     return "COFF-ARM";
6898e90adafSMichael J. Spencer   default:
6908e90adafSMichael J. Spencer     return "COFF-<unknown arch>";
6918e90adafSMichael J. Spencer   }
6928e90adafSMichael J. Spencer }
6938e90adafSMichael J. Spencer 
6948e90adafSMichael J. Spencer unsigned COFFObjectFile::getArch() const {
69582ebd8e3SRui Ueyama   switch(COFFHeader->Machine) {
6968e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
6978e90adafSMichael J. Spencer     return Triple::x86;
6988e90adafSMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
6998e90adafSMichael J. Spencer     return Triple::x86_64;
7009b7c0af2SSaleem Abdulrasool   case COFF::IMAGE_FILE_MACHINE_ARMNT:
7019b7c0af2SSaleem Abdulrasool     return Triple::thumb;
7028e90adafSMichael J. Spencer   default:
7038e90adafSMichael J. Spencer     return Triple::UnknownArch;
7048e90adafSMichael J. Spencer   }
7058e90adafSMichael J. Spencer }
7068e90adafSMichael J. Spencer 
70782ebd8e3SRui Ueyama // This method is kept here because lld uses this. As soon as we make
70882ebd8e3SRui Ueyama // lld to use getCOFFHeader, this method will be removed.
70989a7a5eaSMichael J. Spencer error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
71082ebd8e3SRui Ueyama   return getCOFFHeader(Res);
71182ebd8e3SRui Ueyama }
71282ebd8e3SRui Ueyama 
71382ebd8e3SRui Ueyama error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
71482ebd8e3SRui Ueyama   Res = COFFHeader;
71582ebd8e3SRui Ueyama   return object_error::success;
71682ebd8e3SRui Ueyama }
71782ebd8e3SRui Ueyama 
71882ebd8e3SRui Ueyama error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
71982ebd8e3SRui Ueyama   Res = PE32Header;
72089a7a5eaSMichael J. Spencer   return object_error::success;
72189a7a5eaSMichael J. Spencer }
72289a7a5eaSMichael J. Spencer 
72310ed9ddcSRui Ueyama error_code
72410ed9ddcSRui Ueyama COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
72510ed9ddcSRui Ueyama   Res = PE32PlusHeader;
72610ed9ddcSRui Ueyama   return object_error::success;
72710ed9ddcSRui Ueyama }
72810ed9ddcSRui Ueyama 
7298ff24d25SRui Ueyama error_code COFFObjectFile::getDataDirectory(uint32_t Index,
730ed64342bSRui Ueyama                                             const data_directory *&Res) const {
731ed64342bSRui Ueyama   // Error if if there's no data directory or the index is out of range.
73210ed9ddcSRui Ueyama   if (!DataDirectory)
73310ed9ddcSRui Ueyama     return object_error::parse_failed;
73410ed9ddcSRui Ueyama   assert(PE32Header || PE32PlusHeader);
73510ed9ddcSRui Ueyama   uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
73610ed9ddcSRui Ueyama                                : PE32PlusHeader->NumberOfRvaAndSize;
73710ed9ddcSRui Ueyama   if (Index > NumEnt)
738ed64342bSRui Ueyama     return object_error::parse_failed;
7398ff24d25SRui Ueyama   Res = &DataDirectory[Index];
740ed64342bSRui Ueyama   return object_error::success;
741ed64342bSRui Ueyama }
742ed64342bSRui Ueyama 
7438ff24d25SRui Ueyama error_code COFFObjectFile::getSection(int32_t Index,
7441d6167fdSMichael J. Spencer                                       const coff_section *&Result) const {
7451d6167fdSMichael J. Spencer   // Check for special index values.
746f078eff3SRui Ueyama   if (COFF::isReservedSectionNumber(Index))
7471d6167fdSMichael J. Spencer     Result = NULL;
7488ff24d25SRui Ueyama   else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
7491d6167fdSMichael J. Spencer     // We already verified the section table data, so no need to check again.
7508ff24d25SRui Ueyama     Result = SectionTable + (Index - 1);
7511d6167fdSMichael J. Spencer   else
7521d6167fdSMichael J. Spencer     return object_error::parse_failed;
7531d6167fdSMichael J. Spencer   return object_error::success;
7548e90adafSMichael J. Spencer }
7558e90adafSMichael J. Spencer 
7568ff24d25SRui Ueyama error_code COFFObjectFile::getString(uint32_t Offset,
7571d6167fdSMichael J. Spencer                                      StringRef &Result) const {
7581d6167fdSMichael J. Spencer   if (StringTableSize <= 4)
7591d6167fdSMichael J. Spencer     // Tried to get a string from an empty string table.
7601d6167fdSMichael J. Spencer     return object_error::parse_failed;
7618ff24d25SRui Ueyama   if (Offset >= StringTableSize)
7621d6167fdSMichael J. Spencer     return object_error::unexpected_eof;
7638ff24d25SRui Ueyama   Result = StringRef(StringTable + Offset);
7641d6167fdSMichael J. Spencer   return object_error::success;
7658e90adafSMichael J. Spencer }
766022ecdf2SBenjamin Kramer 
7678ff24d25SRui Ueyama error_code COFFObjectFile::getSymbol(uint32_t Index,
768e5fd0047SMichael J. Spencer                                      const coff_symbol *&Result) const {
7698ff24d25SRui Ueyama   if (Index < COFFHeader->NumberOfSymbols)
7708ff24d25SRui Ueyama     Result = SymbolTable + Index;
771e5fd0047SMichael J. Spencer   else
772e5fd0047SMichael J. Spencer     return object_error::parse_failed;
773e5fd0047SMichael J. Spencer   return object_error::success;
774e5fd0047SMichael J. Spencer }
775e5fd0047SMichael J. Spencer 
7768ff24d25SRui Ueyama error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
77789a7a5eaSMichael J. Spencer                                          StringRef &Res) const {
77889a7a5eaSMichael J. Spencer   // Check for string table entry. First 4 bytes are 0.
7798ff24d25SRui Ueyama   if (Symbol->Name.Offset.Zeroes == 0) {
7808ff24d25SRui Ueyama     uint32_t Offset = Symbol->Name.Offset.Offset;
7818ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Res))
7828ff24d25SRui Ueyama       return EC;
78389a7a5eaSMichael J. Spencer     return object_error::success;
78489a7a5eaSMichael J. Spencer   }
78589a7a5eaSMichael J. Spencer 
7868ff24d25SRui Ueyama   if (Symbol->Name.ShortName[7] == 0)
78789a7a5eaSMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
7888ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName);
78989a7a5eaSMichael J. Spencer   else
79089a7a5eaSMichael J. Spencer     // Not null terminated, use all 8 bytes.
7918ff24d25SRui Ueyama     Res = StringRef(Symbol->Name.ShortName, 8);
79289a7a5eaSMichael J. Spencer   return object_error::success;
79389a7a5eaSMichael J. Spencer }
79489a7a5eaSMichael J. Spencer 
79571757ef3SMarshall Clow ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
7968ff24d25SRui Ueyama                                   const coff_symbol *Symbol) const {
7978ff24d25SRui Ueyama   const uint8_t *Aux = NULL;
79871757ef3SMarshall Clow 
7998ff24d25SRui Ueyama   if (Symbol->NumberOfAuxSymbols > 0) {
80071757ef3SMarshall Clow   // AUX data comes immediately after the symbol in COFF
8018ff24d25SRui Ueyama     Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
80271757ef3SMarshall Clow # ifndef NDEBUG
8038ff24d25SRui Ueyama     // Verify that the Aux symbol points to a valid entry in the symbol table.
8048ff24d25SRui Ueyama     uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
8058ff24d25SRui Ueyama     if (Offset < COFFHeader->PointerToSymbolTable
8068ff24d25SRui Ueyama         || Offset >= COFFHeader->PointerToSymbolTable
80782ebd8e3SRui Ueyama            + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
80871757ef3SMarshall Clow       report_fatal_error("Aux Symbol data was outside of symbol table.");
80971757ef3SMarshall Clow 
8108ff24d25SRui Ueyama     assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
81171757ef3SMarshall Clow          == 0 && "Aux Symbol data did not point to the beginning of a symbol");
81271757ef3SMarshall Clow # endif
813bfb85e67SMarshall Clow   }
81424fc2d64SRui Ueyama   return ArrayRef<uint8_t>(Aux,
81524fc2d64SRui Ueyama                            Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
81671757ef3SMarshall Clow }
81771757ef3SMarshall Clow 
81853c2d547SMichael J. Spencer error_code COFFObjectFile::getSectionName(const coff_section *Sec,
81953c2d547SMichael J. Spencer                                           StringRef &Res) const {
82053c2d547SMichael J. Spencer   StringRef Name;
82153c2d547SMichael J. Spencer   if (Sec->Name[7] == 0)
82253c2d547SMichael J. Spencer     // Null terminated, let ::strlen figure out the length.
82353c2d547SMichael J. Spencer     Name = Sec->Name;
82453c2d547SMichael J. Spencer   else
82553c2d547SMichael J. Spencer     // Not null terminated, use all 8 bytes.
82653c2d547SMichael J. Spencer     Name = StringRef(Sec->Name, 8);
82753c2d547SMichael J. Spencer 
82853c2d547SMichael J. Spencer   // Check for string table entry. First byte is '/'.
82953c2d547SMichael J. Spencer   if (Name[0] == '/') {
83053c2d547SMichael J. Spencer     uint32_t Offset;
8319d2c15efSNico Rieck     if (Name[1] == '/') {
8329d2c15efSNico Rieck       if (decodeBase64StringEntry(Name.substr(2), Offset))
8339d2c15efSNico Rieck         return object_error::parse_failed;
8349d2c15efSNico Rieck     } else {
83553c2d547SMichael J. Spencer       if (Name.substr(1).getAsInteger(10, Offset))
83653c2d547SMichael J. Spencer         return object_error::parse_failed;
8379d2c15efSNico Rieck     }
8388ff24d25SRui Ueyama     if (error_code EC = getString(Offset, Name))
8398ff24d25SRui Ueyama       return EC;
84053c2d547SMichael J. Spencer   }
84153c2d547SMichael J. Spencer 
84253c2d547SMichael J. Spencer   Res = Name;
84353c2d547SMichael J. Spencer   return object_error::success;
84453c2d547SMichael J. Spencer }
84553c2d547SMichael J. Spencer 
8469da9e693SMichael J. Spencer error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
8479da9e693SMichael J. Spencer                                               ArrayRef<uint8_t> &Res) const {
8489da9e693SMichael J. Spencer   // The only thing that we need to verify is that the contents is contained
8499da9e693SMichael J. Spencer   // within the file bounds. We don't need to make sure it doesn't cover other
8509da9e693SMichael J. Spencer   // data, as there's nothing that says that is not allowed.
8519da9e693SMichael J. Spencer   uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
8529da9e693SMichael J. Spencer   uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
8539da9e693SMichael J. Spencer   if (ConEnd > uintptr_t(Data->getBufferEnd()))
8549da9e693SMichael J. Spencer     return object_error::parse_failed;
8559da9e693SMichael J. Spencer   Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
8569da9e693SMichael J. Spencer                           Sec->SizeOfRawData);
8579da9e693SMichael J. Spencer   return object_error::success;
8589da9e693SMichael J. Spencer }
8599da9e693SMichael J. Spencer 
860022ecdf2SBenjamin Kramer const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
861e5fd0047SMichael J. Spencer   return reinterpret_cast<const coff_relocation*>(Rel.p);
862022ecdf2SBenjamin Kramer }
8638ff24d25SRui Ueyama 
8645e812afaSRafael Espindola void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
865e5fd0047SMichael J. Spencer   Rel.p = reinterpret_cast<uintptr_t>(
866e5fd0047SMichael J. Spencer             reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
867022ecdf2SBenjamin Kramer }
8688ff24d25SRui Ueyama 
869022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
870022ecdf2SBenjamin Kramer                                                 uint64_t &Res) const {
8711e483879SRafael Espindola   report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
872022ecdf2SBenjamin Kramer }
8738ff24d25SRui Ueyama 
874cbe72fc9SDanil Malyshev error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
875cbe72fc9SDanil Malyshev                                                uint64_t &Res) const {
876cbe72fc9SDanil Malyshev   Res = toRel(Rel)->VirtualAddress;
877cbe72fc9SDanil Malyshev   return object_error::success;
878cbe72fc9SDanil Malyshev }
8798ff24d25SRui Ueyama 
880806f0064SRafael Espindola symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
881022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
8828ff24d25SRui Ueyama   DataRefImpl Ref;
8838ff24d25SRui Ueyama   Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
8848ff24d25SRui Ueyama   return symbol_iterator(SymbolRef(Ref, this));
885022ecdf2SBenjamin Kramer }
8868ff24d25SRui Ueyama 
887022ecdf2SBenjamin Kramer error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
8887be76590SOwen Anderson                                              uint64_t &Res) const {
889022ecdf2SBenjamin Kramer   const coff_relocation* R = toRel(Rel);
890022ecdf2SBenjamin Kramer   Res = R->Type;
891022ecdf2SBenjamin Kramer   return object_error::success;
892022ecdf2SBenjamin Kramer }
893e5fd0047SMichael J. Spencer 
89427dc8394SAlexey Samsonov const coff_section *
89527dc8394SAlexey Samsonov COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
89627dc8394SAlexey Samsonov   return toSec(Section.getRawDataRefImpl());
89771757ef3SMarshall Clow }
89871757ef3SMarshall Clow 
89927dc8394SAlexey Samsonov const coff_symbol *
90027dc8394SAlexey Samsonov COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
90127dc8394SAlexey Samsonov   return toSymb(Symbol.getRawDataRefImpl());
90271757ef3SMarshall Clow }
90371757ef3SMarshall Clow 
904f12b8282SRafael Espindola const coff_relocation *
90527dc8394SAlexey Samsonov COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
90627dc8394SAlexey Samsonov   return toRel(Reloc.getRawDataRefImpl());
907d3e2a76cSMarshall Clow }
908d3e2a76cSMarshall Clow 
90927dc8394SAlexey Samsonov #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type)                           \
91027dc8394SAlexey Samsonov   case COFF::reloc_type:                                                       \
91127dc8394SAlexey Samsonov     Res = #reloc_type;                                                         \
91227dc8394SAlexey Samsonov     break;
913e5fd0047SMichael J. Spencer 
914e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
915e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
9168ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
9178ff24d25SRui Ueyama   StringRef Res;
91882ebd8e3SRui Ueyama   switch (COFFHeader->Machine) {
919e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_AMD64:
9208ff24d25SRui Ueyama     switch (Reloc->Type) {
921e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
922e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
923e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
924e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
925e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
926e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
927e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
928e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
929e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
930e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
931e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
932e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
933e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
934e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
935e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
936e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
937e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
938e5fd0047SMichael J. Spencer     default:
9398ff24d25SRui Ueyama       Res = "Unknown";
940e5fd0047SMichael J. Spencer     }
941e5fd0047SMichael J. Spencer     break;
942e5fd0047SMichael J. Spencer   case COFF::IMAGE_FILE_MACHINE_I386:
9438ff24d25SRui Ueyama     switch (Reloc->Type) {
944e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
945e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
946e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
947e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
948e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
949e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
950e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
951e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
952e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
953e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
954e5fd0047SMichael J. Spencer     LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
955e5fd0047SMichael J. Spencer     default:
9568ff24d25SRui Ueyama       Res = "Unknown";
957e5fd0047SMichael J. Spencer     }
958e5fd0047SMichael J. Spencer     break;
959e5fd0047SMichael J. Spencer   default:
9608ff24d25SRui Ueyama     Res = "Unknown";
961e5fd0047SMichael J. Spencer   }
9628ff24d25SRui Ueyama   Result.append(Res.begin(), Res.end());
963e5fd0047SMichael J. Spencer   return object_error::success;
964e5fd0047SMichael J. Spencer }
965e5fd0047SMichael J. Spencer 
966e5fd0047SMichael J. Spencer #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
967e5fd0047SMichael J. Spencer 
968e5fd0047SMichael J. Spencer error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
969e5fd0047SMichael J. Spencer                                           SmallVectorImpl<char> &Result) const {
9708ff24d25SRui Ueyama   const coff_relocation *Reloc = toRel(Rel);
9718ff24d25SRui Ueyama   const coff_symbol *Symb = 0;
9728ff24d25SRui Ueyama   if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
9738ff24d25SRui Ueyama   DataRefImpl Sym;
9748ff24d25SRui Ueyama   Sym.p = reinterpret_cast<uintptr_t>(Symb);
9758ff24d25SRui Ueyama   StringRef SymName;
9768ff24d25SRui Ueyama   if (error_code EC = getSymbolName(Sym, SymName)) return EC;
9778ff24d25SRui Ueyama   Result.append(SymName.begin(), SymName.end());
978e5fd0047SMichael J. Spencer   return object_error::success;
979022ecdf2SBenjamin Kramer }
9808e90adafSMichael J. Spencer 
9812fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
9822fc34c5fSDavid Meyer                                           LibraryRef &Result) const {
9832fc34c5fSDavid Meyer   report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
9842fc34c5fSDavid Meyer }
9852fc34c5fSDavid Meyer 
9862fc34c5fSDavid Meyer error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
9872fc34c5fSDavid Meyer                                           StringRef &Result) const {
9882fc34c5fSDavid Meyer   report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
9892fc34c5fSDavid Meyer }
9902fc34c5fSDavid Meyer 
991c2bed429SRui Ueyama bool ImportDirectoryEntryRef::
992c2bed429SRui Ueyama operator==(const ImportDirectoryEntryRef &Other) const {
993a045b73aSRui Ueyama   return ImportTable == Other.ImportTable && Index == Other.Index;
994c2bed429SRui Ueyama }
995c2bed429SRui Ueyama 
9965e812afaSRafael Espindola void ImportDirectoryEntryRef::moveNext() {
9975e812afaSRafael Espindola   ++Index;
998c2bed429SRui Ueyama }
999c2bed429SRui Ueyama 
1000c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::
1001c2bed429SRui Ueyama getImportTableEntry(const import_directory_table_entry *&Result) const {
1002a045b73aSRui Ueyama   Result = ImportTable;
1003c2bed429SRui Ueyama   return object_error::success;
1004c2bed429SRui Ueyama }
1005c2bed429SRui Ueyama 
1006c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1007c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
1008a045b73aSRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
1009a045b73aSRui Ueyama     return EC;
1010a045b73aSRui Ueyama   Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1011c2bed429SRui Ueyama   return object_error::success;
1012c2bed429SRui Ueyama }
1013c2bed429SRui Ueyama 
1014c2bed429SRui Ueyama error_code ImportDirectoryEntryRef::getImportLookupEntry(
1015c2bed429SRui Ueyama     const import_lookup_table_entry32 *&Result) const {
1016c2bed429SRui Ueyama   uintptr_t IntPtr = 0;
1017a045b73aSRui Ueyama   if (error_code EC =
1018a045b73aSRui Ueyama           OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
1019a045b73aSRui Ueyama     return EC;
1020c2bed429SRui Ueyama   Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1021c2bed429SRui Ueyama   return object_error::success;
1022c2bed429SRui Ueyama }
1023c2bed429SRui Ueyama 
1024ad882ba8SRui Ueyama bool ExportDirectoryEntryRef::
1025ad882ba8SRui Ueyama operator==(const ExportDirectoryEntryRef &Other) const {
1026ad882ba8SRui Ueyama   return ExportTable == Other.ExportTable && Index == Other.Index;
1027ad882ba8SRui Ueyama }
1028ad882ba8SRui Ueyama 
10295e812afaSRafael Espindola void ExportDirectoryEntryRef::moveNext() {
10305e812afaSRafael Espindola   ++Index;
1031ad882ba8SRui Ueyama }
1032ad882ba8SRui Ueyama 
1033da49d0d4SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only
1034da49d0d4SRui Ueyama // by ordinal, the empty string is set as a result.
1035da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1036da49d0d4SRui Ueyama   uintptr_t IntPtr = 0;
1037da49d0d4SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1038da49d0d4SRui Ueyama     return EC;
1039da49d0d4SRui Ueyama   Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1040da49d0d4SRui Ueyama   return object_error::success;
1041da49d0d4SRui Ueyama }
1042da49d0d4SRui Ueyama 
1043e5df6095SRui Ueyama // Returns the starting ordinal number.
1044e5df6095SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1045e5df6095SRui Ueyama   Result = ExportTable->OrdinalBase;
1046e5df6095SRui Ueyama   return object_error::success;
1047e5df6095SRui Ueyama }
1048e5df6095SRui Ueyama 
1049ad882ba8SRui Ueyama // Returns the export ordinal of the current export symbol.
1050ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1051ad882ba8SRui Ueyama   Result = ExportTable->OrdinalBase + Index;
1052ad882ba8SRui Ueyama   return object_error::success;
1053ad882ba8SRui Ueyama }
1054ad882ba8SRui Ueyama 
1055ad882ba8SRui Ueyama // Returns the address of the current export symbol.
1056ad882ba8SRui Ueyama error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1057ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
1058ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
1059ad882ba8SRui Ueyama           ExportTable->ExportAddressTableRVA, IntPtr))
1060ad882ba8SRui Ueyama     return EC;
106124fc2d64SRui Ueyama   const export_address_table_entry *entry =
106224fc2d64SRui Ueyama       reinterpret_cast<const export_address_table_entry *>(IntPtr);
1063ad882ba8SRui Ueyama   Result = entry[Index].ExportRVA;
1064ad882ba8SRui Ueyama   return object_error::success;
1065ad882ba8SRui Ueyama }
1066ad882ba8SRui Ueyama 
1067ad882ba8SRui Ueyama // Returns the name of the current export symbol. If the symbol is exported only
1068ad882ba8SRui Ueyama // by ordinal, the empty string is set as a result.
1069da49d0d4SRui Ueyama error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
1070ad882ba8SRui Ueyama   uintptr_t IntPtr = 0;
1071ad882ba8SRui Ueyama   if (error_code EC = OwningObject->getRvaPtr(
1072ad882ba8SRui Ueyama           ExportTable->OrdinalTableRVA, IntPtr))
1073ad882ba8SRui Ueyama     return EC;
1074ad882ba8SRui Ueyama   const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1075ad882ba8SRui Ueyama 
1076ad882ba8SRui Ueyama   uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1077ad882ba8SRui Ueyama   int Offset = 0;
1078ad882ba8SRui Ueyama   for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1079ad882ba8SRui Ueyama        I < E; ++I, ++Offset) {
1080ad882ba8SRui Ueyama     if (*I != Index)
1081ad882ba8SRui Ueyama       continue;
1082ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(
1083ad882ba8SRui Ueyama             ExportTable->NamePointerRVA, IntPtr))
1084ad882ba8SRui Ueyama       return EC;
1085ad882ba8SRui Ueyama     const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1086ad882ba8SRui Ueyama     if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1087ad882ba8SRui Ueyama       return EC;
1088ad882ba8SRui Ueyama     Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1089ad882ba8SRui Ueyama     return object_error::success;
1090ad882ba8SRui Ueyama   }
1091ad882ba8SRui Ueyama   Result = "";
1092ad882ba8SRui Ueyama   return object_error::success;
1093ad882ba8SRui Ueyama }
1094ad882ba8SRui Ueyama 
1095afcc3df7SRafael Espindola ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
1096afcc3df7SRafael Espindola                                                        bool BufferOwned) {
10978ff24d25SRui Ueyama   error_code EC;
109856440fd8SAhmed Charles   std::unique_ptr<COFFObjectFile> Ret(
109956440fd8SAhmed Charles       new COFFObjectFile(Object, EC, BufferOwned));
1100692410efSRafael Espindola   if (EC)
1101692410efSRafael Espindola     return EC;
110296c9d95fSAhmed Charles   return Ret.release();
1103686738e2SRui Ueyama }
1104