blob: b8e7d5e9e356197aa895b5c48388baa334ec9d6e [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::ulittle16_t;
29using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000030using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000031using 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.
Rafael Espindola48af1c22014-08-19 18:44:46 +000034static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000035 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000036 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
David Majnemere830c602014-11-13 08:46:37 +000042static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
43 const size_t Size) {
44 if (Addr + Size < Addr || Addr + Size < Size ||
45 Addr + Size > uintptr_t(M.getBufferEnd()) ||
46 Addr < uintptr_t(M.getBufferStart())) {
47 return object_error::unexpected_eof;
48 }
49 return object_error::success;
50}
51
Rui Ueyamaed64342b2013-07-19 23:23:29 +000052// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
53// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000054template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000055static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000056 const void *Ptr,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000057 const size_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000058 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000059 if (std::error_code EC = checkOffset(M, Addr, Size))
60 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000061 Obj = reinterpret_cast<const T *>(Addr);
62 return object_error::success;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000063}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064
Nico Rieck9d2c15e2014-02-22 16:12:20 +000065// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
66// prefixed slashes.
67static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
68 assert(Str.size() <= 6 && "String too long, possible overflow.");
69 if (Str.size() > 6)
70 return true;
71
72 uint64_t Value = 0;
73 while (!Str.empty()) {
74 unsigned CharVal;
75 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
76 CharVal = Str[0] - 'A';
77 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
78 CharVal = Str[0] - 'a' + 26;
79 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
80 CharVal = Str[0] - '0' + 52;
81 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000082 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000083 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000084 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000085 else
86 return true;
87
88 Value = (Value * 64) + CharVal;
89 Str = Str.substr(1);
90 }
91
92 if (Value > std::numeric_limits<uint32_t>::max())
93 return true;
94
95 Result = static_cast<uint32_t>(Value);
96 return false;
97}
98
David Majnemer44f51e52014-09-10 12:51:52 +000099template <typename coff_symbol_type>
100const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
101 const coff_symbol_type *Addr =
102 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103
David Majnemer44f51e52014-09-10 12:51:52 +0000104#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000105 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000106 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000107 if (Offset < getPointerToSymbolTable() ||
108 Offset >= getPointerToSymbolTable() +
109 (getNumberOfSymbols() * sizeof(coff_symbol_type)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000110 report_fatal_error("Symbol was outside of symbol table.");
111
David Majnemer44f51e52014-09-10 12:51:52 +0000112 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
113 "Symbol did not point to the beginning of a symbol");
114#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000115
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000116 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000117}
118
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000119const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
120 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000121
122# ifndef NDEBUG
123 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000124 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000125 report_fatal_error("Section was outside of section table.");
126
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000127 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
128 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000129 "Section did not point to the beginning of a section");
130# endif
131
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000132 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000133}
134
Rafael Espindola5e812af2014-01-30 02:49:50 +0000135void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000136 if (SymbolTable16) {
137 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
138 Symb += 1 + Symb->NumberOfAuxSymbols;
139 Ref.p = reinterpret_cast<uintptr_t>(Symb);
140 } else if (SymbolTable32) {
141 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
142 Symb += 1 + Symb->NumberOfAuxSymbols;
143 Ref.p = reinterpret_cast<uintptr_t>(Symb);
144 } else {
145 llvm_unreachable("no symbol table pointer!");
146 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000147}
148
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000149std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
150 StringRef &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000151 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000152 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000153}
154
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000155std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
156 uint64_t &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000157 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolae62ab112013-11-02 18:07:48 +0000158
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000159 if (Symb.isAnyUndefined()) {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000160 Result = UnknownAddressOrSize;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000161 return object_error::success;
162 }
163 if (Symb.isCommon()) {
164 Result = UnknownAddressOrSize;
165 return object_error::success;
166 }
167 int32_t SectionNumber = Symb.getSectionNumber();
168 if (!COFF::isReservedSectionNumber(SectionNumber)) {
169 const coff_section *Section = nullptr;
170 if (std::error_code EC = getSection(SectionNumber, Section))
171 return EC;
172
David Majnemer44f51e52014-09-10 12:51:52 +0000173 Result = Section->VirtualAddress + Symb.getValue();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000174 return object_error::success;
175 }
176
177 Result = Symb.getValue();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000178 return object_error::success;
179}
180
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000181std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
182 SymbolRef::Type &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000183 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000184 int32_t SectionNumber = Symb.getSectionNumber();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000185 Result = SymbolRef::ST_Other;
David Majnemer44f51e52014-09-10 12:51:52 +0000186
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000187 if (Symb.isAnyUndefined()) {
David Meyer7e4b9762012-02-29 02:11:55 +0000188 Result = SymbolRef::ST_Unknown;
David Majnemer44f51e52014-09-10 12:51:52 +0000189 } else if (Symb.isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000190 Result = SymbolRef::ST_Function;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000191 } else if (Symb.isCommon()) {
192 Result = SymbolRef::ST_Data;
193 } else if (Symb.isFileRecord()) {
194 Result = SymbolRef::ST_File;
195 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG) {
196 Result = SymbolRef::ST_Debug;
197 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
198 const coff_section *Section = nullptr;
199 if (std::error_code EC = getSection(SectionNumber, Section))
200 return EC;
201 uint32_t Characteristics = Section->Characteristics;
202 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
203 Result = SymbolRef::ST_Function;
204 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
205 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
Rui Ueyama5efa6652014-01-16 20:22:55 +0000206 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000207 }
208 return object_error::success;
209}
210
Rafael Espindola20122a42014-01-31 20:57:12 +0000211uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000212 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000213 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000214
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000215 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000216 Result |= SymbolRef::SF_Global;
217
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000218 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000219 Result |= SymbolRef::SF_Weak;
220
David Majnemer44f51e52014-09-10 12:51:52 +0000221 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000222 Result |= SymbolRef::SF_Absolute;
223
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000224 if (Symb.isFileRecord())
225 Result |= SymbolRef::SF_FormatSpecific;
226
227 if (Symb.isSectionDefinition())
228 Result |= SymbolRef::SF_FormatSpecific;
229
230 if (Symb.isCommon())
231 Result |= SymbolRef::SF_Common;
232
233 if (Symb.isAnyUndefined())
234 Result |= SymbolRef::SF_Undefined;
235
Rafael Espindola20122a42014-01-31 20:57:12 +0000236 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000237}
238
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000239std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
240 uint64_t &Result) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000241 COFFSymbolRef Symb = getCOFFSymbol(Ref);
242
243 if (Symb.isAnyUndefined()) {
244 Result = UnknownAddressOrSize;
245 return object_error::success;
246 }
247 if (Symb.isCommon()) {
248 Result = Symb.getValue();
249 return object_error::success;
250 }
David Majnemer51ff5592014-11-06 08:10:41 +0000251
252 // Let's attempt to get the size of the symbol by looking at the address of
253 // the symbol after the symbol in question.
254 uint64_t SymbAddr;
255 if (std::error_code EC = getSymbolAddress(Ref, SymbAddr))
256 return EC;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000257 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer51ff5592014-11-06 08:10:41 +0000258 if (COFF::isReservedSectionNumber(SectionNumber)) {
259 // Absolute and debug symbols aren't sorted in any interesting way.
260 Result = 0;
261 return object_error::success;
262 }
263 const section_iterator SecEnd = section_end();
264 uint64_t AfterAddr = UnknownAddressOrSize;
265 for (const symbol_iterator &SymbI : symbols()) {
266 section_iterator SecI = SecEnd;
267 if (std::error_code EC = SymbI->getSection(SecI))
268 return EC;
269 // Check the symbol's section, skip it if it's in the wrong section.
270 // First, make sure it is in any section.
271 if (SecI == SecEnd)
272 continue;
273 // Second, make sure it is in the same section as the symbol in question.
274 if (!sectionContainsSymbol(SecI->getRawDataRefImpl(), Ref))
275 continue;
276 uint64_t Addr;
277 if (std::error_code EC = SymbI->getAddress(Addr))
278 return EC;
279 // We want to compare our symbol in question with the closest possible
280 // symbol that comes after.
281 if (AfterAddr > Addr && Addr > SymbAddr)
282 AfterAddr = Addr;
283 }
284 if (AfterAddr == UnknownAddressOrSize) {
285 // No symbol comes after this one, assume that everything after our symbol
286 // is part of it.
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000287 const coff_section *Section = nullptr;
288 if (std::error_code EC = getSection(SectionNumber, Section))
289 return EC;
David Majnemer44f51e52014-09-10 12:51:52 +0000290 Result = Section->SizeOfRawData - Symb.getValue();
David Majnemer51ff5592014-11-06 08:10:41 +0000291 } else {
292 // Take the difference between our symbol and the symbol that comes after
293 // our symbol.
294 Result = AfterAddr - SymbAddr;
Rafael Espindola8280fbb2014-10-08 17:37:19 +0000295 }
296
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000297 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000298}
299
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000300std::error_code
301COFFObjectFile::getSymbolSection(DataRefImpl Ref,
302 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000303 COFFSymbolRef Symb = getCOFFSymbol(Ref);
304 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000305 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000306 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000307 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000308 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000309 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000310 DataRefImpl Ref;
311 Ref.p = reinterpret_cast<uintptr_t>(Sec);
312 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000313 }
314 return object_error::success;
315}
316
Rafael Espindola5e812af2014-01-30 02:49:50 +0000317void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000318 const coff_section *Sec = toSec(Ref);
319 Sec += 1;
320 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000321}
322
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000323std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
324 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000325 const coff_section *Sec = toSec(Ref);
326 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000327}
328
Rafael Espindola80291272014-10-08 15:28:58 +0000329uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000330 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000331 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000332}
333
Rafael Espindola80291272014-10-08 15:28:58 +0000334uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000335 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000336}
337
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000338std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
339 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000340 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000341 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000342 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000343 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
344 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000345}
346
Rafael Espindola80291272014-10-08 15:28:58 +0000347uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000348 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000349 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000350}
351
Rafael Espindola80291272014-10-08 15:28:58 +0000352bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000353 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000354 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000355}
356
Rafael Espindola80291272014-10-08 15:28:58 +0000357bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000358 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000359 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000360}
361
Rafael Espindola80291272014-10-08 15:28:58 +0000362bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000363 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000364 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000365}
366
Rafael Espindola80291272014-10-08 15:28:58 +0000367bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
Preston Gurd2138ef62012-04-12 20:13:57 +0000368 // FIXME: Unimplemented
Rafael Espindola80291272014-10-08 15:28:58 +0000369 return true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000370}
371
Rafael Espindola80291272014-10-08 15:28:58 +0000372bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000373 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000374 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000375}
376
Rafael Espindola80291272014-10-08 15:28:58 +0000377bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000378 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000379 return false;
Preston Gurd2138ef62012-04-12 20:13:57 +0000380}
381
Rafael Espindola80291272014-10-08 15:28:58 +0000382bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000383 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000384 return false;
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000385}
386
Rafael Espindola80291272014-10-08 15:28:58 +0000387bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
388 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000390 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000391 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000392 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000393}
394
David Majnemere830c602014-11-13 08:46:37 +0000395static uint32_t getNumberOfRelocations(const coff_section *Sec,
396 MemoryBufferRef M, const uint8_t *base) {
397 // The field for the number of relocations in COFF section table is only
398 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
399 // NumberOfRelocations field, and the actual relocation count is stored in the
400 // VirtualAddress field in the first relocation entry.
401 if (Sec->hasExtendedRelocations()) {
402 const coff_relocation *FirstReloc;
403 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
404 base + Sec->PointerToRelocations)))
405 return 0;
406 return FirstReloc->VirtualAddress;
407 }
408 return Sec->NumberOfRelocations;
409}
410
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000411relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
412 const coff_section *Sec = toSec(Ref);
413 DataRefImpl Ret;
David Majnemere830c602014-11-13 08:46:37 +0000414 if (getNumberOfRelocations(Sec, Data, base()) == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000415 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000416 } else {
417 auto begin = reinterpret_cast<const coff_relocation*>(
418 base() + Sec->PointerToRelocations);
419 if (Sec->hasExtendedRelocations()) {
420 // Skip the first relocation entry repurposed to store the number of
421 // relocations.
422 begin++;
423 }
424 Ret.p = reinterpret_cast<uintptr_t>(begin);
425 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000426 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000427}
428
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000429relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
430 const coff_section *Sec = toSec(Ref);
431 DataRefImpl Ret;
David Majnemere830c602014-11-13 08:46:37 +0000432 if (getNumberOfRelocations(Sec, Data, base()) == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000433 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000434 } else {
435 auto begin = reinterpret_cast<const coff_relocation*>(
436 base() + Sec->PointerToRelocations);
David Majnemer58323a92014-11-13 07:42:07 +0000437 if (Sec->hasExtendedRelocations()) {
438 // Skip the first relocation entry repurposed to store the number of
439 // relocations.
440 begin++;
441 }
David Majnemere830c602014-11-13 08:46:37 +0000442 uint32_t NumReloc = getNumberOfRelocations(Sec, Data, base());
Rui Ueyama827c8a22014-03-21 00:44:19 +0000443 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
444 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000445 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000446}
447
Rui Ueyamac2bed422013-09-27 21:04:00 +0000448// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000449std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000450 if (COFFHeader)
451 if (std::error_code EC =
452 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
453 getNumberOfSymbols() * getSymbolTableEntrySize()))
454 return EC;
455
456 if (COFFBigObjHeader)
457 if (std::error_code EC =
458 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
459 getNumberOfSymbols() * getSymbolTableEntrySize()))
460 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000461
462 // Find string table. The first four byte of the string table contains the
463 // total size of the string table, including the size field itself. If the
464 // string table is empty, the value of the first four byte would be 4.
465 const uint8_t *StringTableAddr =
David Majnemer44f51e52014-09-10 12:51:52 +0000466 base() + getPointerToSymbolTable() +
467 getNumberOfSymbols() * getSymbolTableEntrySize();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000469 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000470 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000471 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000472 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000473 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000474 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000475
Nico Rieck773a5792014-02-26 19:51:44 +0000476 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
477 // tools like cvtres write a size of 0 for an empty table instead of 4.
478 if (StringTableSize < 4)
479 StringTableSize = 4;
480
Rui Ueyamac2bed422013-09-27 21:04:00 +0000481 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000482 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000483 return object_error::parse_failed;
484 return object_error::success;
485}
486
Rui Ueyama215a5862014-02-20 06:51:07 +0000487// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000488std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000489 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
490 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000491 uint64_t Rva = Addr - ImageBase;
492 assert(Rva <= UINT32_MAX);
493 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000494}
495
Rui Ueyamac2bed422013-09-27 21:04:00 +0000496// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000497std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000498 for (const SectionRef &S : sections()) {
499 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000500 uint32_t SectionStart = Section->VirtualAddress;
501 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000502 if (SectionStart <= Addr && Addr < SectionEnd) {
503 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000504 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
505 return object_error::success;
506 }
507 }
508 return object_error::parse_failed;
509}
510
511// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
512// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000513std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
514 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000515 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000516 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000517 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000518 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
519 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
520 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
521 return object_error::success;
522}
523
524// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000525std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000526 // First, we get the RVA of the import table. If the file lacks a pointer to
527 // the import table, do nothing.
528 const data_directory *DataEntry;
529 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
530 return object_error::success;
531
532 // Do nothing if the pointer to import table is NULL.
533 if (DataEntry->RelativeVirtualAddress == 0)
534 return object_error::success;
535
536 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000537 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000538 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000539 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000540
541 // Find the section that contains the RVA. This is needed because the RVA is
542 // the import table's memory address which is different from its file offset.
543 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000544 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000545 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000546 ImportDirectory = reinterpret_cast<
547 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000548 return object_error::success;
549}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000550
Rui Ueyama15d99352014-10-03 00:41:58 +0000551// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
552std::error_code COFFObjectFile::initDelayImportTablePtr() {
553 const data_directory *DataEntry;
554 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
555 return object_error::success;
556 if (DataEntry->RelativeVirtualAddress == 0)
557 return object_error::success;
558
559 uint32_t RVA = DataEntry->RelativeVirtualAddress;
560 NumberOfDelayImportDirectory = DataEntry->Size /
561 sizeof(delay_import_directory_table_entry) - 1;
562
563 uintptr_t IntPtr = 0;
564 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
565 return EC;
566 DelayImportDirectory = reinterpret_cast<
567 const delay_import_directory_table_entry *>(IntPtr);
568 return object_error::success;
569}
570
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000571// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000572std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000573 // First, we get the RVA of the export table. If the file lacks a pointer to
574 // the export table, do nothing.
575 const data_directory *DataEntry;
576 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
577 return object_error::success;
578
579 // Do nothing if the pointer to export table is NULL.
580 if (DataEntry->RelativeVirtualAddress == 0)
581 return object_error::success;
582
583 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
584 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000585 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000586 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000587 ExportDirectory =
588 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000589 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000590}
591
Rafael Espindola48af1c22014-08-19 18:44:46 +0000592COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
593 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000594 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
595 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
596 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
597 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000598 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000599 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000600 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000601 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000602 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000603
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000604 // The current location in the file where we are looking at.
605 uint64_t CurPtr = 0;
606
607 // PE header is optional and is present only in executables. If it exists,
608 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000609 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000610
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000611 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000612 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000613 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
614 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000615 const auto *DH = reinterpret_cast<const dos_header *>(base());
616 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
617 CurPtr = DH->AddressOfNewExeHeader;
618 // Check the PE magic bytes. ("PE\0\0")
619 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
620 EC = object_error::parse_failed;
621 return;
622 }
623 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
624 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000625 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000626 }
627
Rafael Espindola48af1c22014-08-19 18:44:46 +0000628 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000629 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000630
631 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
632 // import libraries share a common prefix but bigobj is more restrictive.
633 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
634 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
635 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
636 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
637 return;
638
639 // Verify that we are dealing with bigobj.
640 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
641 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
642 sizeof(COFF::BigObjMagic)) == 0) {
643 COFFHeader = nullptr;
644 CurPtr += sizeof(coff_bigobj_file_header);
645 } else {
646 // It's not a bigobj.
647 COFFBigObjHeader = nullptr;
648 }
649 }
650 if (COFFHeader) {
651 // The prior checkSize call may have failed. This isn't a hard error
652 // because we were just trying to sniff out bigobj.
653 EC = object_error::success;
654 CurPtr += sizeof(coff_file_header);
655
656 if (COFFHeader->isImportLibrary())
657 return;
658 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000659
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000660 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000661 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000662 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000663 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000664
665 const uint8_t *DataDirAddr;
666 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000667 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000668 PE32Header = Header;
669 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
670 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000671 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000672 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
673 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
674 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
675 } else {
676 // It's neither PE32 nor PE32+.
677 EC = object_error::parse_failed;
678 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000679 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000680 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000681 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000682 CurPtr += COFFHeader->SizeOfOptionalHeader;
683 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000684
Rafael Espindola48af1c22014-08-19 18:44:46 +0000685 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000686 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000687 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000688
Rui Ueyamac2bed422013-09-27 21:04:00 +0000689 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000690 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000691 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000692 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000693
Rui Ueyamac2bed422013-09-27 21:04:00 +0000694 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000695 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000696 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000697 if ((EC = initDelayImportTablePtr()))
698 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000699
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000700 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000701 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000702 return;
703
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000704 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000705}
706
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000707basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000708 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000709 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000710 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000711}
712
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000713basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000714 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000715 DataRefImpl Ret;
716 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000717 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000718}
719
Rui Ueyamabc654b12013-09-27 21:47:05 +0000720import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000721 return import_directory_iterator(
722 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000723}
724
Rui Ueyamabc654b12013-09-27 21:47:05 +0000725import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000726 return import_directory_iterator(
727 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000728}
David Meyerc429b802012-03-01 22:19:54 +0000729
Rui Ueyama15d99352014-10-03 00:41:58 +0000730delay_import_directory_iterator
731COFFObjectFile::delay_import_directory_begin() const {
732 return delay_import_directory_iterator(
733 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
734}
735
736delay_import_directory_iterator
737COFFObjectFile::delay_import_directory_end() const {
738 return delay_import_directory_iterator(
739 DelayImportDirectoryEntryRef(
740 DelayImportDirectory, NumberOfDelayImportDirectory, this));
741}
742
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000743export_directory_iterator COFFObjectFile::export_directory_begin() const {
744 return export_directory_iterator(
745 ExportDirectoryEntryRef(ExportDirectory, 0, this));
746}
747
748export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000749 if (!ExportDirectory)
750 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000751 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000752 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000753 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000754}
755
Rafael Espindolab5155a52014-02-10 20:24:04 +0000756section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000757 DataRefImpl Ret;
758 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
759 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000760}
761
Rafael Espindolab5155a52014-02-10 20:24:04 +0000762section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000763 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000764 int NumSections =
765 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000766 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
767 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000768}
769
770uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000771 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000772}
773
774StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000775 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000776 case COFF::IMAGE_FILE_MACHINE_I386:
777 return "COFF-i386";
778 case COFF::IMAGE_FILE_MACHINE_AMD64:
779 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000780 case COFF::IMAGE_FILE_MACHINE_ARMNT:
781 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000782 default:
783 return "COFF-<unknown arch>";
784 }
785}
786
787unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000788 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000789 case COFF::IMAGE_FILE_MACHINE_I386:
790 return Triple::x86;
791 case COFF::IMAGE_FILE_MACHINE_AMD64:
792 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000793 case COFF::IMAGE_FILE_MACHINE_ARMNT:
794 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000795 default:
796 return Triple::UnknownArch;
797 }
798}
799
Rui Ueyama979fb402014-10-09 02:16:38 +0000800iterator_range<import_directory_iterator>
801COFFObjectFile::import_directories() const {
802 return make_range(import_directory_begin(), import_directory_end());
803}
804
805iterator_range<delay_import_directory_iterator>
806COFFObjectFile::delay_import_directories() const {
807 return make_range(delay_import_directory_begin(),
808 delay_import_directory_end());
809}
810
811iterator_range<export_directory_iterator>
812COFFObjectFile::export_directories() const {
813 return make_range(export_directory_begin(), export_directory_end());
814}
815
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000816std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000817 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000818 return object_error::success;
819}
820
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000821std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000822COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
823 Res = PE32PlusHeader;
824 return object_error::success;
825}
826
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000827std::error_code
828COFFObjectFile::getDataDirectory(uint32_t Index,
829 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000830 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000831 if (!DataDirectory)
832 return object_error::parse_failed;
833 assert(PE32Header || PE32PlusHeader);
834 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
835 : PE32PlusHeader->NumberOfRvaAndSize;
836 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000837 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000838 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000839 return object_error::success;
840}
841
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000842std::error_code COFFObjectFile::getSection(int32_t Index,
843 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000844 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000845 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000846 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000847 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000848 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000849 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000850 else
851 return object_error::parse_failed;
852 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000853}
854
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000855std::error_code COFFObjectFile::getString(uint32_t Offset,
856 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000857 if (StringTableSize <= 4)
858 // Tried to get a string from an empty string table.
859 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000860 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000861 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000862 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000863 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000864}
865
David Majnemer44f51e52014-09-10 12:51:52 +0000866std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000867 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000868 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000869 if (Symbol.getStringTableOffset().Zeroes == 0) {
870 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000871 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000872 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000873 return object_error::success;
874 }
875
David Majnemer44f51e52014-09-10 12:51:52 +0000876 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000877 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000878 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000879 else
880 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000881 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000882 return object_error::success;
883}
884
David Majnemer44f51e52014-09-10 12:51:52 +0000885ArrayRef<uint8_t>
886COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000887 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000888
David Majnemer44f51e52014-09-10 12:51:52 +0000889 size_t SymbolSize = getSymbolTableEntrySize();
890 if (Symbol.getNumberOfAuxSymbols() > 0) {
891 // AUX data comes immediately after the symbol in COFF
892 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000893# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000894 // Verify that the Aux symbol points to a valid entry in the symbol table.
895 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000896 if (Offset < getPointerToSymbolTable() ||
897 Offset >=
898 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000899 report_fatal_error("Aux Symbol data was outside of symbol table.");
900
David Majnemer44f51e52014-09-10 12:51:52 +0000901 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
902 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000903# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000904 }
David Majnemer44f51e52014-09-10 12:51:52 +0000905 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000906}
907
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000908std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
909 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000910 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000911 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000912 // Null terminated, let ::strlen figure out the length.
913 Name = Sec->Name;
914 else
915 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000916 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000917
918 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000919 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000920 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000921 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000922 if (decodeBase64StringEntry(Name.substr(2), Offset))
923 return object_error::parse_failed;
924 } else {
925 if (Name.substr(1).getAsInteger(10, Offset))
926 return object_error::parse_failed;
927 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000928 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000929 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000930 }
931
932 Res = Name;
933 return object_error::success;
934}
935
David Majnemera9ee5c02014-10-09 08:42:31 +0000936uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
937 // SizeOfRawData and VirtualSize change what they represent depending on
938 // whether or not we have an executable image.
939 //
940 // For object files, SizeOfRawData contains the size of section's data;
941 // VirtualSize is always zero.
942 //
943 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
944 // actual section size is in VirtualSize. It is possible for VirtualSize to
945 // be greater than SizeOfRawData; the contents past that point should be
946 // considered to be zero.
947 uint32_t SectionSize;
948 if (Sec->VirtualSize)
949 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
950 else
951 SectionSize = Sec->SizeOfRawData;
952
953 return SectionSize;
954}
955
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000956std::error_code
957COFFObjectFile::getSectionContents(const coff_section *Sec,
958 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000959 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
960 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000961 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
962 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000963 // The only thing that we need to verify is that the contents is contained
964 // within the file bounds. We don't need to make sure it doesn't cover other
965 // data, as there's nothing that says that is not allowed.
966 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000967 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000968 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000969 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000970 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000971 return object_error::success;
972}
973
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000974const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000975 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000976}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000977
Rafael Espindola5e812af2014-01-30 02:49:50 +0000978void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000979 Rel.p = reinterpret_cast<uintptr_t>(
980 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000981}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000982
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000983std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
984 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000985 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000986}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000987
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000988std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
989 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000990 const coff_relocation *R = toRel(Rel);
991 const support::ulittle32_t *VirtualAddressPtr;
992 if (std::error_code EC =
993 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
994 return EC;
995 Res = *VirtualAddressPtr;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000996 return object_error::success;
997}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000998
Rafael Espindola806f0062013-06-05 01:33:53 +0000999symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001000 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001001 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +00001002 if (SymbolTable16)
1003 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1004 else if (SymbolTable32)
1005 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1006 else
David Majnemer1f80b0a2014-11-13 07:42:11 +00001007 return symbol_end();
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001008 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001009}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001010
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001011std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1012 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001013 const coff_relocation* R = toRel(Rel);
1014 Res = R->Type;
1015 return object_error::success;
1016}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001017
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001018const coff_section *
1019COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1020 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001021}
1022
David Majnemer44f51e52014-09-10 12:51:52 +00001023COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1024 if (SymbolTable16)
1025 return toSymb<coff_symbol16>(Ref);
1026 if (SymbolTable32)
1027 return toSymb<coff_symbol32>(Ref);
1028 llvm_unreachable("no symbol table pointer!");
1029}
1030
1031COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1032 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001033}
1034
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001035const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001036COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1037 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001038}
1039
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001040#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1041 case COFF::reloc_type: \
1042 Res = #reloc_type; \
1043 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001044
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001045std::error_code
1046COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1047 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001048 const coff_relocation *Reloc = toRel(Rel);
1049 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001050 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001051 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001052 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1070 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001071 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001072 }
1073 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001074 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1075 switch (Reloc->Type) {
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1091 default:
1092 Res = "Unknown";
1093 }
1094 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001095 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001096 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1098 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1099 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1100 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1101 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1102 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1103 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1104 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1105 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1106 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1107 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1108 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001109 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001110 }
1111 break;
1112 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001113 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001114 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001115 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001116 return object_error::success;
1117}
1118
1119#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1120
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001121std::error_code
1122COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1123 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001124 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001125 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001126 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1127 if (std::error_code EC = Symb.getError())
1128 return EC;
1129 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001130 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001131 if (std::error_code EC = getSymbolName(Sym, SymName))
1132 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001133 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001134 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001135}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001136
Rafael Espindolac66d7612014-08-17 19:09:37 +00001137bool COFFObjectFile::isRelocatableObject() const {
1138 return !DataDirectory;
1139}
1140
Rui Ueyamac2bed422013-09-27 21:04:00 +00001141bool ImportDirectoryEntryRef::
1142operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001143 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001144}
1145
Rafael Espindola5e812af2014-01-30 02:49:50 +00001146void ImportDirectoryEntryRef::moveNext() {
1147 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001148}
1149
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001150std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1151 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001152 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001153 return object_error::success;
1154}
1155
Rui Ueyama861021f2014-10-02 22:05:29 +00001156static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001157makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001158 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001159 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001160 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001161 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001162 }
1163 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001164 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001165}
1166
Rui Ueyama15d99352014-10-03 00:41:58 +00001167static imported_symbol_iterator
1168importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001169 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001170 Object->getRvaPtr(RVA, IntPtr);
1171 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001172}
1173
Rui Ueyama15d99352014-10-03 00:41:58 +00001174static imported_symbol_iterator
1175importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001176 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001177 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001178 // Forward the pointer to the last entry which is null.
1179 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001180 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001181 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1182 while (*Entry++)
1183 ++Index;
1184 } else {
1185 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1186 while (*Entry++)
1187 ++Index;
1188 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001189 return makeImportedSymbolIterator(Object, IntPtr, Index);
1190}
1191
1192imported_symbol_iterator
1193ImportDirectoryEntryRef::imported_symbol_begin() const {
1194 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1195 OwningObject);
1196}
1197
1198imported_symbol_iterator
1199ImportDirectoryEntryRef::imported_symbol_end() const {
1200 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1201 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001202}
1203
Rui Ueyama979fb402014-10-09 02:16:38 +00001204iterator_range<imported_symbol_iterator>
1205ImportDirectoryEntryRef::imported_symbols() const {
1206 return make_range(imported_symbol_begin(), imported_symbol_end());
1207}
1208
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001209std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001210 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001211 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001212 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001213 return EC;
1214 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001215 return object_error::success;
1216}
1217
Rui Ueyama1e152d52014-10-02 17:02:18 +00001218std::error_code
1219ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1220 Result = ImportTable[Index].ImportLookupTableRVA;
1221 return object_error::success;
1222}
1223
1224std::error_code
1225ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1226 Result = ImportTable[Index].ImportAddressTableRVA;
1227 return object_error::success;
1228}
1229
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001230std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001231 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001232 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001233 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1234 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001235 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001236 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1237 return object_error::success;
1238}
1239
Rui Ueyama15d99352014-10-03 00:41:58 +00001240bool DelayImportDirectoryEntryRef::
1241operator==(const DelayImportDirectoryEntryRef &Other) const {
1242 return Table == Other.Table && Index == Other.Index;
1243}
1244
1245void DelayImportDirectoryEntryRef::moveNext() {
1246 ++Index;
1247}
1248
1249imported_symbol_iterator
1250DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1251 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1252 OwningObject);
1253}
1254
1255imported_symbol_iterator
1256DelayImportDirectoryEntryRef::imported_symbol_end() const {
1257 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1258 OwningObject);
1259}
1260
Rui Ueyama979fb402014-10-09 02:16:38 +00001261iterator_range<imported_symbol_iterator>
1262DelayImportDirectoryEntryRef::imported_symbols() const {
1263 return make_range(imported_symbol_begin(), imported_symbol_end());
1264}
1265
Rui Ueyama15d99352014-10-03 00:41:58 +00001266std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1267 uintptr_t IntPtr = 0;
1268 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1269 return EC;
1270 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1271 return object_error::success;
1272}
1273
Rui Ueyama1af08652014-10-03 18:07:18 +00001274std::error_code DelayImportDirectoryEntryRef::
1275getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1276 Result = Table;
1277 return object_error::success;
1278}
1279
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001280std::error_code DelayImportDirectoryEntryRef::
1281getImportAddress(int AddrIndex, uint64_t &Result) const {
1282 uint32_t RVA = Table[Index].DelayImportAddressTable +
1283 AddrIndex * (OwningObject->is64() ? 8 : 4);
1284 uintptr_t IntPtr = 0;
1285 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1286 return EC;
1287 if (OwningObject->is64())
1288 Result = *reinterpret_cast<const uint64_t *>(IntPtr);
1289 else
1290 Result = *reinterpret_cast<const uint32_t *>(IntPtr);
1291 return object_error::success;
1292}
1293
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001294bool ExportDirectoryEntryRef::
1295operator==(const ExportDirectoryEntryRef &Other) const {
1296 return ExportTable == Other.ExportTable && Index == Other.Index;
1297}
1298
Rafael Espindola5e812af2014-01-30 02:49:50 +00001299void ExportDirectoryEntryRef::moveNext() {
1300 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001301}
1302
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001303// Returns the name of the current export symbol. If the symbol is exported only
1304// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001305std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001306 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001307 if (std::error_code EC =
1308 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001309 return EC;
1310 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1311 return object_error::success;
1312}
1313
Rui Ueyamae5df6092014-01-17 22:02:24 +00001314// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001315std::error_code
1316ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001317 Result = ExportTable->OrdinalBase;
1318 return object_error::success;
1319}
1320
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001321// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001322std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001323 Result = ExportTable->OrdinalBase + Index;
1324 return object_error::success;
1325}
1326
1327// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001328std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001329 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001330 if (std::error_code EC =
1331 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001332 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001333 const export_address_table_entry *entry =
1334 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001335 Result = entry[Index].ExportRVA;
1336 return object_error::success;
1337}
1338
1339// Returns the name of the current export symbol. If the symbol is exported only
1340// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001341std::error_code
1342ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001343 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001344 if (std::error_code EC =
1345 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001346 return EC;
1347 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1348
1349 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1350 int Offset = 0;
1351 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1352 I < E; ++I, ++Offset) {
1353 if (*I != Index)
1354 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001355 if (std::error_code EC =
1356 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001357 return EC;
1358 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001359 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001360 return EC;
1361 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1362 return object_error::success;
1363 }
1364 Result = "";
1365 return object_error::success;
1366}
1367
Rui Ueyama861021f2014-10-02 22:05:29 +00001368bool ImportedSymbolRef::
1369operator==(const ImportedSymbolRef &Other) const {
1370 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1371 && Index == Other.Index;
1372}
1373
1374void ImportedSymbolRef::moveNext() {
1375 ++Index;
1376}
1377
1378std::error_code
1379ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1380 uint32_t RVA;
1381 if (Entry32) {
1382 // If a symbol is imported only by ordinal, it has no name.
1383 if (Entry32[Index].isOrdinal())
1384 return object_error::success;
1385 RVA = Entry32[Index].getHintNameRVA();
1386 } else {
1387 if (Entry64[Index].isOrdinal())
1388 return object_error::success;
1389 RVA = Entry64[Index].getHintNameRVA();
1390 }
1391 uintptr_t IntPtr = 0;
1392 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1393 return EC;
1394 // +2 because the first two bytes is hint.
1395 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1396 return object_error::success;
1397}
1398
1399std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1400 uint32_t RVA;
1401 if (Entry32) {
1402 if (Entry32[Index].isOrdinal()) {
1403 Result = Entry32[Index].getOrdinal();
1404 return object_error::success;
1405 }
1406 RVA = Entry32[Index].getHintNameRVA();
1407 } else {
1408 if (Entry64[Index].isOrdinal()) {
1409 Result = Entry64[Index].getOrdinal();
1410 return object_error::success;
1411 }
1412 RVA = Entry64[Index].getHintNameRVA();
1413 }
1414 uintptr_t IntPtr = 0;
1415 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1416 return EC;
1417 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1418 return object_error::success;
1419}
1420
Rafael Espindola437b0d52014-07-31 03:12:45 +00001421ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001422ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001423 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001424 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001425 if (EC)
1426 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001427 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001428}