blob: 5ad8f8443f784d4f5f41b8e8cabda5a93ce01ca9 [file] [log] [blame]
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencere5fd0042011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000019#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000022#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000023#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000024
25using namespace llvm;
26using namespace object;
27
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000028using support::ulittle8_t;
29using support::ulittle16_t;
30using support::ulittle32_t;
31using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000033// Returns false if size is greater than the buffer size. And sets ec.
Rui Ueyama686738e2014-01-16 20:30:36 +000034static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000035 if (M->getBufferSize() < Size) {
36 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000037 return false;
38 }
39 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000040}
41
Rui Ueyamaed64342b2013-07-19 23:23:29 +000042// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
43// Returns unexpected_eof if error.
44template<typename T>
Rui Ueyama686738e2014-01-16 20:30:36 +000045static error_code getObject(const T *&Obj, const MemoryBuffer *M,
46 const uint8_t *Ptr, const size_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000047 uintptr_t Addr = uintptr_t(Ptr);
48 if (Addr + Size < Addr ||
49 Addr + Size < Size ||
50 Addr + Size > uintptr_t(M->getBufferEnd())) {
51 return object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000052 }
Rui Ueyamaed64342b2013-07-19 23:23:29 +000053 Obj = reinterpret_cast<const T *>(Addr);
54 return object_error::success;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000055}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000056
Nico Rieck9d2c15e2014-02-22 16:12:20 +000057// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
58// prefixed slashes.
59static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
60 assert(Str.size() <= 6 && "String too long, possible overflow.");
61 if (Str.size() > 6)
62 return true;
63
64 uint64_t Value = 0;
65 while (!Str.empty()) {
66 unsigned CharVal;
67 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
68 CharVal = Str[0] - 'A';
69 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
70 CharVal = Str[0] - 'a' + 26;
71 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
72 CharVal = Str[0] - '0' + 52;
73 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000074 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000075 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000076 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000077 else
78 return true;
79
80 Value = (Value * 64) + CharVal;
81 Str = Str.substr(1);
82 }
83
84 if (Value > std::numeric_limits<uint32_t>::max())
85 return true;
86
87 Result = static_cast<uint32_t>(Value);
88 return false;
89}
90
Rui Ueyama8ff24d22014-01-16 20:11:48 +000091const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
92 const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000093
94# ifndef NDEBUG
95 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +000096 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
97 if (Offset < COFFHeader->PointerToSymbolTable
98 || Offset >= COFFHeader->PointerToSymbolTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +000099 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000100 report_fatal_error("Symbol was outside of symbol table.");
101
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000102 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103 == 0 && "Symbol did not point to the beginning of a symbol");
104# endif
105
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000106 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000107}
108
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000109const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
110 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000111
112# ifndef NDEBUG
113 // Verify that the section points to a valid entry in the section table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000114 if (Addr < SectionTable
115 || Addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000116 report_fatal_error("Section was outside of section table.");
117
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000118 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
119 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000120 "Section did not point to the beginning of a section");
121# endif
122
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000123 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000124}
125
Rafael Espindola5e812af2014-01-30 02:49:50 +0000126void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000127 const coff_symbol *Symb = toSymb(Ref);
128 Symb += 1 + Symb->NumberOfAuxSymbols;
129 Ref.p = reinterpret_cast<uintptr_t>(Symb);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000130}
131
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000132error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
133 StringRef &Result) const {
134 const coff_symbol *Symb = toSymb(Ref);
135 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000136}
137
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000138error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000139 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000140 const coff_symbol *Symb = toSymb(Ref);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000141 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000142 if (error_code EC = getSection(Symb->SectionNumber, Section))
143 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000144
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000145 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000146 Result = UnknownAddressOrSize;
147 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000148 Result = Section->PointerToRawData + Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000149 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000150 Result = Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000151 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000152}
153
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000154error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000155 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000156 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000157 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000158 if (error_code EC = getSection(Symb->SectionNumber, Section))
159 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000160
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000161 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000162 Result = UnknownAddressOrSize;
163 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000164 Result = Section->VirtualAddress + Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000165 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000166 Result = Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000167 return object_error::success;
168}
169
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000170error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
Michael J. Spencerd3946672011-10-17 20:19:29 +0000171 SymbolRef::Type &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000172 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000173 Result = SymbolRef::ST_Other;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000174 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
175 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer7e4b9762012-02-29 02:11:55 +0000176 Result = SymbolRef::ST_Unknown;
David Majnemerddf28f22014-03-19 04:47:47 +0000177 } else if (Symb->isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000178 Result = SymbolRef::ST_Function;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000179 } else {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000180 uint32_t Characteristics = 0;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000181 if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000182 const coff_section *Section = NULL;
183 if (error_code EC = getSection(Symb->SectionNumber, Section))
184 return EC;
185 Characteristics = Section->Characteristics;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000186 }
Rui Ueyama5efa6652014-01-16 20:22:55 +0000187 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
188 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
189 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000190 }
191 return object_error::success;
192}
193
Rafael Espindola20122a42014-01-31 20:57:12 +0000194uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000195 const coff_symbol *Symb = toSymb(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000196 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000197
Rafael Espindola975e1152014-02-04 22:50:47 +0000198 // TODO: Correctly set SF_FormatSpecific, SF_Common
David Meyer7e4b9762012-02-29 02:11:55 +0000199
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000200 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
201 if (Symb->Value == 0)
202 Result |= SymbolRef::SF_Undefined;
203 else
204 Result |= SymbolRef::SF_Common;
205 }
206
David Meyer1df4b842012-02-28 23:47:53 +0000207
208 // TODO: This are certainly too restrictive.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000209 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000210 Result |= SymbolRef::SF_Global;
211
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000212 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000213 Result |= SymbolRef::SF_Weak;
214
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000215 if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000216 Result |= SymbolRef::SF_Absolute;
217
Rafael Espindola20122a42014-01-31 20:57:12 +0000218 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000219}
220
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000221error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000222 uint64_t &Result) const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000223 // FIXME: Return the correct size. This requires looking at all the symbols
224 // in the same section as this symbol, and looking for either the next
225 // symbol, or the end of the section.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000226 const coff_symbol *Symb = toSymb(Ref);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000227 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000228 if (error_code EC = getSection(Symb->SectionNumber, Section))
229 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000230
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000231 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000232 Result = UnknownAddressOrSize;
233 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000234 Result = Section->SizeOfRawData - Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000235 else
236 Result = 0;
237 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000238}
239
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000240error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
Michael J. Spencer3217315392011-10-17 23:54:46 +0000241 section_iterator &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000242 const coff_symbol *Symb = toSymb(Ref);
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000243 if (COFF::isReservedSectionNumber(Symb->SectionNumber)) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000244 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000245 } else {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000246 const coff_section *Sec = 0;
247 if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
248 DataRefImpl Ref;
249 Ref.p = reinterpret_cast<uintptr_t>(Sec);
250 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000251 }
252 return object_error::success;
253}
254
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000255error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
Tim Northover4f223bf72012-10-29 10:47:00 +0000256 uint64_t &Val) const {
257 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
258}
259
Rafael Espindola5e812af2014-01-30 02:49:50 +0000260void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000261 const coff_section *Sec = toSec(Ref);
262 Sec += 1;
263 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000264}
265
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000266error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000267 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000268 const coff_section *Sec = toSec(Ref);
269 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000270}
271
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000272error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000273 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000274 const coff_section *Sec = toSec(Ref);
275 Result = Sec->VirtualAddress;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000276 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000277}
278
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000279error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000280 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000281 const coff_section *Sec = toSec(Ref);
282 Result = Sec->SizeOfRawData;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000283 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000284}
285
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000286error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000287 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000288 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000289 ArrayRef<uint8_t> Res;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000290 error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000291 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
292 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000293}
294
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000295error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
Michael J. Spencer79894602011-10-10 21:55:43 +0000296 uint64_t &Res) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000297 const coff_section *Sec = toSec(Ref);
298 if (!Sec)
Michael J. Spencer79894602011-10-10 21:55:43 +0000299 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000300 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000301 return object_error::success;
302}
303
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000304error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000305 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000306 const coff_section *Sec = toSec(Ref);
307 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000308 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000309}
310
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000311error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000312 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000313 const coff_section *Sec = toSec(Ref);
314 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000315 return object_error::success;
316}
317
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000318error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000319 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000320 const coff_section *Sec = toSec(Ref);
321 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000322 return object_error::success;
323}
324
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000325error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000326 bool &Result) const {
327 // FIXME: Unimplemented
328 Result = true;
329 return object_error::success;
330}
331
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000332error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000333 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000334 const coff_section *Sec = toSec(Ref);
335 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000336 return object_error::success;
337}
338
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000339error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000340 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000341 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000342 Result = false;
343 return object_error::success;
344}
345
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000346error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000347 bool &Result) const {
348 // FIXME: Unimplemented.
349 Result = false;
350 return object_error::success;
351}
352
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000353error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
354 DataRefImpl SymbRef,
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000355 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000356 const coff_section *Sec = toSec(SecRef);
357 const coff_symbol *Symb = toSymb(SymbRef);
358 const coff_section *SymbSec = 0;
359 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
360 if (SymbSec == Sec)
Michael J. Spencer9a288512011-10-13 20:36:54 +0000361 Result = true;
362 else
363 Result = false;
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000364 return object_error::success;
365}
366
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000367relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
368 const coff_section *Sec = toSec(Ref);
369 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000370 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000371 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000372 } else {
373 auto begin = reinterpret_cast<const coff_relocation*>(
374 base() + Sec->PointerToRelocations);
375 if (Sec->hasExtendedRelocations()) {
376 // Skip the first relocation entry repurposed to store the number of
377 // relocations.
378 begin++;
379 }
380 Ret.p = reinterpret_cast<uintptr_t>(begin);
381 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000382 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000383}
384
Rui Ueyama827c8a22014-03-21 00:44:19 +0000385static uint32_t getNumberOfRelocations(const coff_section *Sec,
386 const uint8_t *base) {
387 // The field for the number of relocations in COFF section table is only
388 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
389 // NumberOfRelocations field, and the actual relocation count is stored in the
390 // VirtualAddress field in the first relocation entry.
391 if (Sec->hasExtendedRelocations()) {
392 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
393 base + Sec->PointerToRelocations);
394 return FirstReloc->VirtualAddress;
395 }
396 return Sec->NumberOfRelocations;
397}
398
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000399relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
400 const coff_section *Sec = toSec(Ref);
401 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000402 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000404 } else {
405 auto begin = reinterpret_cast<const coff_relocation*>(
406 base() + Sec->PointerToRelocations);
407 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
408 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
409 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000410 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000411}
412
Rui Ueyamac2bed422013-09-27 21:04:00 +0000413// Initialize the pointer to the symbol table.
414error_code COFFObjectFile::initSymbolTablePtr() {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000415 if (error_code EC = getObject(
Rui Ueyamac2bed422013-09-27 21:04:00 +0000416 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
417 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000418 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000419
420 // Find string table. The first four byte of the string table contains the
421 // total size of the string table, including the size field itself. If the
422 // string table is empty, the value of the first four byte would be 4.
423 const uint8_t *StringTableAddr =
424 base() + COFFHeader->PointerToSymbolTable +
425 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
426 const ulittle32_t *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000427 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
428 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429 StringTableSize = *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000430 if (error_code EC =
Rui Ueyamac2bed422013-09-27 21:04:00 +0000431 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000432 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000433
Nico Rieck773a5792014-02-26 19:51:44 +0000434 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
435 // tools like cvtres write a size of 0 for an empty table instead of 4.
436 if (StringTableSize < 4)
437 StringTableSize = 4;
438
Rui Ueyamac2bed422013-09-27 21:04:00 +0000439 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000440 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441 return object_error::parse_failed;
442 return object_error::success;
443}
444
Rui Ueyama215a5862014-02-20 06:51:07 +0000445// Returns the file offset for the given VA.
Rui Ueyamab7a40082014-02-20 19:14:56 +0000446error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000447 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
448 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000449 uint64_t Rva = Addr - ImageBase;
450 assert(Rva <= UINT32_MAX);
451 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000452}
453
Rui Ueyamac2bed422013-09-27 21:04:00 +0000454// Returns the file offset for the given RVA.
Rui Ueyama215a5862014-02-20 06:51:07 +0000455error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000456 for (const SectionRef &S : sections()) {
457 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458 uint32_t SectionStart = Section->VirtualAddress;
459 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000460 if (SectionStart <= Addr && Addr < SectionEnd) {
461 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
463 return object_error::success;
464 }
465 }
466 return object_error::parse_failed;
467}
468
469// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
470// table entry.
471error_code COFFObjectFile::
472getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
473 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000474 if (error_code EC = getRvaPtr(Rva, IntPtr))
475 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000476 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
477 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
478 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
479 return object_error::success;
480}
481
482// Find the import table.
483error_code COFFObjectFile::initImportTablePtr() {
484 // First, we get the RVA of the import table. If the file lacks a pointer to
485 // the import table, do nothing.
486 const data_directory *DataEntry;
487 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
488 return object_error::success;
489
490 // Do nothing if the pointer to import table is NULL.
491 if (DataEntry->RelativeVirtualAddress == 0)
492 return object_error::success;
493
494 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
495 NumberOfImportDirectory = DataEntry->Size /
496 sizeof(import_directory_table_entry);
497
498 // Find the section that contains the RVA. This is needed because the RVA is
499 // the import table's memory address which is different from its file offset.
500 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000501 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
502 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000503 ImportDirectory = reinterpret_cast<
504 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000505 return object_error::success;
506}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000507
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000508// Find the export table.
509error_code COFFObjectFile::initExportTablePtr() {
510 // First, we get the RVA of the export table. If the file lacks a pointer to
511 // the export table, do nothing.
512 const data_directory *DataEntry;
513 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
514 return object_error::success;
515
516 // Do nothing if the pointer to export table is NULL.
517 if (DataEntry->RelativeVirtualAddress == 0)
518 return object_error::success;
519
520 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
521 uintptr_t IntPtr = 0;
522 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
523 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000524 ExportDirectory =
525 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000526 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000527}
528
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000529COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
530 bool BufferOwned)
531 : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0),
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000532 PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0),
533 SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0),
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000534 NumberOfImportDirectory(0), ExportDirectory(0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000535 // Check that we at least have enough room for a header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000536 if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000537
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000538 // The current location in the file where we are looking at.
539 uint64_t CurPtr = 0;
540
541 // PE header is optional and is present only in executables. If it exists,
542 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000543 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000544
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000545 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000546 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000547 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
548 // PE signature to find 'normal' COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000549 if (!checkSize(Data, EC, 0x3c + 8)) return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000550 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
551 // Check the PE magic bytes. ("PE\0\0")
552 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000553 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000554 return;
555 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000556 CurPtr += 4; // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000557 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000558 }
559
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000560 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000561 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000562 CurPtr += sizeof(coff_file_header);
563
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000564 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000565 const pe32_header *Header;
566 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000567 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000568
569 const uint8_t *DataDirAddr;
570 uint64_t DataDirSize;
571 if (Header->Magic == 0x10b) {
572 PE32Header = Header;
573 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
574 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
575 } else if (Header->Magic == 0x20b) {
576 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
577 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
578 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
579 } else {
580 // It's neither PE32 nor PE32+.
581 EC = object_error::parse_failed;
582 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000583 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000584 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
585 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000586 CurPtr += COFFHeader->SizeOfOptionalHeader;
587 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000588
Rafael Espindola692410e2014-01-21 23:06:54 +0000589 if (COFFHeader->isImportLibrary())
590 return;
591
592 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
593 COFFHeader->NumberOfSections * sizeof(coff_section))))
594 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000595
Rui Ueyamac2bed422013-09-27 21:04:00 +0000596 // Initialize the pointer to the symbol table.
597 if (COFFHeader->PointerToSymbolTable != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000598 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000599 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000600
Rui Ueyamac2bed422013-09-27 21:04:00 +0000601 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000602 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000603 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000604
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000605 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000606 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000607 return;
608
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000609 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000610}
611
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000612basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000613 DataRefImpl Ret;
614 Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000615 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000616}
617
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000618basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000619 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000620 DataRefImpl Ret;
621 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000622 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000623}
624
Rafael Espindolab5155a52014-02-10 20:24:04 +0000625library_iterator COFFObjectFile::needed_library_begin() const {
David Meyer2fc34c52012-03-01 01:36:50 +0000626 // TODO: implement
627 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
628}
629
Rafael Espindolab5155a52014-02-10 20:24:04 +0000630library_iterator COFFObjectFile::needed_library_end() const {
David Meyer2fc34c52012-03-01 01:36:50 +0000631 // TODO: implement
632 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
633}
634
David Meyerc429b802012-03-01 22:19:54 +0000635StringRef COFFObjectFile::getLoadName() const {
636 // COFF does not have this field.
637 return "";
638}
639
Rui Ueyamabc654b12013-09-27 21:47:05 +0000640import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000641 return import_directory_iterator(
642 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000643}
644
Rui Ueyamabc654b12013-09-27 21:47:05 +0000645import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000646 return import_directory_iterator(
647 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000648}
David Meyerc429b802012-03-01 22:19:54 +0000649
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000650export_directory_iterator COFFObjectFile::export_directory_begin() const {
651 return export_directory_iterator(
652 ExportDirectoryEntryRef(ExportDirectory, 0, this));
653}
654
655export_directory_iterator COFFObjectFile::export_directory_end() const {
656 if (ExportDirectory == 0)
657 return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000658 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000659 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000660 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000661}
662
Rafael Espindolab5155a52014-02-10 20:24:04 +0000663section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000664 DataRefImpl Ret;
665 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
666 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000667}
668
Rafael Espindolab5155a52014-02-10 20:24:04 +0000669section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000670 DataRefImpl Ret;
671 int NumSections = COFFHeader->isImportLibrary()
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000672 ? 0 : COFFHeader->NumberOfSections;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000673 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
674 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000675}
676
677uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000678 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000679}
680
681StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000682 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000683 case COFF::IMAGE_FILE_MACHINE_I386:
684 return "COFF-i386";
685 case COFF::IMAGE_FILE_MACHINE_AMD64:
686 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000687 case COFF::IMAGE_FILE_MACHINE_ARMNT:
688 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000689 default:
690 return "COFF-<unknown arch>";
691 }
692}
693
694unsigned COFFObjectFile::getArch() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000695 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000696 case COFF::IMAGE_FILE_MACHINE_I386:
697 return Triple::x86;
698 case COFF::IMAGE_FILE_MACHINE_AMD64:
699 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000700 case COFF::IMAGE_FILE_MACHINE_ARMNT:
701 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000702 default:
703 return Triple::UnknownArch;
704 }
705}
706
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000707// This method is kept here because lld uses this. As soon as we make
708// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000709error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000710 return getCOFFHeader(Res);
711}
712
713error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
714 Res = COFFHeader;
715 return object_error::success;
716}
717
718error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
719 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000720 return object_error::success;
721}
722
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000723error_code
724COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
725 Res = PE32PlusHeader;
726 return object_error::success;
727}
728
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000729error_code COFFObjectFile::getDataDirectory(uint32_t Index,
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000730 const data_directory *&Res) const {
731 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000732 if (!DataDirectory)
733 return object_error::parse_failed;
734 assert(PE32Header || PE32PlusHeader);
735 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
736 : PE32PlusHeader->NumberOfRvaAndSize;
737 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000738 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000739 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000740 return object_error::success;
741}
742
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000743error_code COFFObjectFile::getSection(int32_t Index,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000744 const coff_section *&Result) const {
745 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000746 if (COFF::isReservedSectionNumber(Index))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000747 Result = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000748 else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000749 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000750 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000751 else
752 return object_error::parse_failed;
753 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000754}
755
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000756error_code COFFObjectFile::getString(uint32_t Offset,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000757 StringRef &Result) const {
758 if (StringTableSize <= 4)
759 // Tried to get a string from an empty string table.
760 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000761 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000762 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000763 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000764 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000765}
766
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000767error_code COFFObjectFile::getSymbol(uint32_t Index,
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000768 const coff_symbol *&Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000769 if (Index < COFFHeader->NumberOfSymbols)
770 Result = SymbolTable + Index;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000771 else
772 return object_error::parse_failed;
773 return object_error::success;
774}
775
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000776error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000777 StringRef &Res) const {
778 // Check for string table entry. First 4 bytes are 0.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000779 if (Symbol->Name.Offset.Zeroes == 0) {
780 uint32_t Offset = Symbol->Name.Offset.Offset;
781 if (error_code EC = getString(Offset, Res))
782 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000783 return object_error::success;
784 }
785
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000786 if (Symbol->Name.ShortName[7] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000787 // Null terminated, let ::strlen figure out the length.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000788 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000789 else
790 // Not null terminated, use all 8 bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000791 Res = StringRef(Symbol->Name.ShortName, 8);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000792 return object_error::success;
793}
794
Marshall Clow71757ef2012-06-15 01:08:25 +0000795ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000796 const coff_symbol *Symbol) const {
797 const uint8_t *Aux = NULL;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000798
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000799 if (Symbol->NumberOfAuxSymbols > 0) {
Marshall Clow71757ef2012-06-15 01:08:25 +0000800 // AUX data comes immediately after the symbol in COFF
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000801 Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
Marshall Clow71757ef2012-06-15 01:08:25 +0000802# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000803 // Verify that the Aux symbol points to a valid entry in the symbol table.
804 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
805 if (Offset < COFFHeader->PointerToSymbolTable
806 || Offset >= COFFHeader->PointerToSymbolTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000807 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clow71757ef2012-06-15 01:08:25 +0000808 report_fatal_error("Aux Symbol data was outside of symbol table.");
809
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000810 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clow71757ef2012-06-15 01:08:25 +0000811 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000812# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000813 }
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000814 return ArrayRef<uint8_t>(Aux,
815 Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
Marshall Clow71757ef2012-06-15 01:08:25 +0000816}
817
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000818error_code COFFObjectFile::getSectionName(const coff_section *Sec,
819 StringRef &Res) const {
820 StringRef Name;
821 if (Sec->Name[7] == 0)
822 // Null terminated, let ::strlen figure out the length.
823 Name = Sec->Name;
824 else
825 // Not null terminated, use all 8 bytes.
826 Name = StringRef(Sec->Name, 8);
827
828 // Check for string table entry. First byte is '/'.
829 if (Name[0] == '/') {
830 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000831 if (Name[1] == '/') {
832 if (decodeBase64StringEntry(Name.substr(2), Offset))
833 return object_error::parse_failed;
834 } else {
835 if (Name.substr(1).getAsInteger(10, Offset))
836 return object_error::parse_failed;
837 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000838 if (error_code EC = getString(Offset, Name))
839 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000840 }
841
842 Res = Name;
843 return object_error::success;
844}
845
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000846error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
847 ArrayRef<uint8_t> &Res) const {
848 // The only thing that we need to verify is that the contents is contained
849 // within the file bounds. We don't need to make sure it doesn't cover other
850 // data, as there's nothing that says that is not allowed.
851 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
852 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
853 if (ConEnd > uintptr_t(Data->getBufferEnd()))
854 return object_error::parse_failed;
855 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
856 Sec->SizeOfRawData);
857 return object_error::success;
858}
859
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000860const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000861 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000862}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000863
Rafael Espindola5e812af2014-01-30 02:49:50 +0000864void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000865 Rel.p = reinterpret_cast<uintptr_t>(
866 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000867}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000868
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000869error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
870 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000871 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000872}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000873
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000874error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
875 uint64_t &Res) const {
876 Res = toRel(Rel)->VirtualAddress;
877 return object_error::success;
878}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000879
Rafael Espindola806f0062013-06-05 01:33:53 +0000880symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000881 const coff_relocation* R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000882 DataRefImpl Ref;
883 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
884 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000885}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000886
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000887error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson7be76592011-10-26 17:08:49 +0000888 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000889 const coff_relocation* R = toRel(Rel);
890 Res = R->Type;
891 return object_error::success;
892}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000893
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000894const coff_section *
895COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
896 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000897}
898
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000899const coff_symbol *
900COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
901 return toSymb(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000902}
903
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000904const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000905COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
906 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +0000907}
908
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000909#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
910 case COFF::reloc_type: \
911 Res = #reloc_type; \
912 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000913
914error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
915 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000916 const coff_relocation *Reloc = toRel(Rel);
917 StringRef Res;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000918 switch (COFFHeader->Machine) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000919 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000920 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000921 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
922 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
923 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
924 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
925 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
926 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
927 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
928 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
929 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
930 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
931 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
932 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
933 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
934 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
935 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
936 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
937 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
938 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000939 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000940 }
941 break;
942 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000943 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000944 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
945 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
946 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
947 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
948 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
949 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
950 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
951 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
952 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
953 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
954 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
955 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000956 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000957 }
958 break;
959 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000960 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000961 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000962 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000963 return object_error::success;
964}
965
966#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
967
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000968error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
969 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000970 const coff_relocation *Reloc = toRel(Rel);
971 const coff_symbol *Symb = 0;
972 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
973 DataRefImpl Sym;
974 Sym.p = reinterpret_cast<uintptr_t>(Symb);
975 StringRef SymName;
976 if (error_code EC = getSymbolName(Sym, SymName)) return EC;
977 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000978 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000979}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000980
David Meyer2fc34c52012-03-01 01:36:50 +0000981error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
982 LibraryRef &Result) const {
983 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
984}
985
986error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
987 StringRef &Result) const {
988 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
989}
990
Rui Ueyamac2bed422013-09-27 21:04:00 +0000991bool ImportDirectoryEntryRef::
992operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000993 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000994}
995
Rafael Espindola5e812af2014-01-30 02:49:50 +0000996void ImportDirectoryEntryRef::moveNext() {
997 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000998}
999
1000error_code ImportDirectoryEntryRef::
1001getImportTableEntry(const import_directory_table_entry *&Result) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001002 Result = ImportTable;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001003 return object_error::success;
1004}
1005
1006error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001007 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +00001008 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
1009 return EC;
1010 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001011 return object_error::success;
1012}
1013
1014error_code ImportDirectoryEntryRef::getImportLookupEntry(
1015 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001016 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +00001017 if (error_code EC =
1018 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
1019 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001020 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1021 return object_error::success;
1022}
1023
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001024bool ExportDirectoryEntryRef::
1025operator==(const ExportDirectoryEntryRef &Other) const {
1026 return ExportTable == Other.ExportTable && Index == Other.Index;
1027}
1028
Rafael Espindola5e812af2014-01-30 02:49:50 +00001029void ExportDirectoryEntryRef::moveNext() {
1030 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001031}
1032
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001033// Returns the name of the current export symbol. If the symbol is exported only
1034// by ordinal, the empty string is set as a result.
1035error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1036 uintptr_t IntPtr = 0;
1037 if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1038 return EC;
1039 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1040 return object_error::success;
1041}
1042
Rui Ueyamae5df6092014-01-17 22:02:24 +00001043// Returns the starting ordinal number.
1044error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1045 Result = ExportTable->OrdinalBase;
1046 return object_error::success;
1047}
1048
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001049// Returns the export ordinal of the current export symbol.
1050error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1051 Result = ExportTable->OrdinalBase + Index;
1052 return object_error::success;
1053}
1054
1055// Returns the address of the current export symbol.
1056error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1057 uintptr_t IntPtr = 0;
1058 if (error_code EC = OwningObject->getRvaPtr(
1059 ExportTable->ExportAddressTableRVA, IntPtr))
1060 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001061 const export_address_table_entry *entry =
1062 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001063 Result = entry[Index].ExportRVA;
1064 return object_error::success;
1065}
1066
1067// Returns the name of the current export symbol. If the symbol is exported only
1068// by ordinal, the empty string is set as a result.
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001069error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001070 uintptr_t IntPtr = 0;
1071 if (error_code EC = OwningObject->getRvaPtr(
1072 ExportTable->OrdinalTableRVA, IntPtr))
1073 return EC;
1074 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1075
1076 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1077 int Offset = 0;
1078 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1079 I < E; ++I, ++Offset) {
1080 if (*I != Index)
1081 continue;
1082 if (error_code EC = OwningObject->getRvaPtr(
1083 ExportTable->NamePointerRVA, IntPtr))
1084 return EC;
1085 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1086 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1087 return EC;
1088 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1089 return object_error::success;
1090 }
1091 Result = "";
1092 return object_error::success;
1093}
1094
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001095ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
1096 bool BufferOwned) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001097 error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001098 std::unique_ptr<COFFObjectFile> Ret(
1099 new COFFObjectFile(Object, EC, BufferOwned));
Rafael Espindola692410e2014-01-21 23:06:54 +00001100 if (EC)
1101 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001102 return Ret.release();
Rui Ueyama686738e2014-01-16 20:30:36 +00001103}