blob: 1fdeae6342f2fe7362a03a381b62c1f744cab2b8 [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
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.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000044template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000045static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000046 const void *Ptr,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000047 const size_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000048 uintptr_t Addr = uintptr_t(Ptr);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000049 if (Addr + Size < Addr || Addr + Size < Size ||
David Majnemer58323a92014-11-13 07:42:07 +000050 Addr + Size > uintptr_t(M.getBufferEnd()) ||
51 Addr < uintptr_t(M.getBufferStart())) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000052 return object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000053 }
Rui Ueyamaed64342b2013-07-19 23:23:29 +000054 Obj = reinterpret_cast<const T *>(Addr);
55 return object_error::success;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000056}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000057
Nico Rieck9d2c15e2014-02-22 16:12:20 +000058// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
59// prefixed slashes.
60static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
61 assert(Str.size() <= 6 && "String too long, possible overflow.");
62 if (Str.size() > 6)
63 return true;
64
65 uint64_t Value = 0;
66 while (!Str.empty()) {
67 unsigned CharVal;
68 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
69 CharVal = Str[0] - 'A';
70 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
71 CharVal = Str[0] - 'a' + 26;
72 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
73 CharVal = Str[0] - '0' + 52;
74 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000075 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000076 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000077 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000078 else
79 return true;
80
81 Value = (Value * 64) + CharVal;
82 Str = Str.substr(1);
83 }
84
85 if (Value > std::numeric_limits<uint32_t>::max())
86 return true;
87
88 Result = static_cast<uint32_t>(Value);
89 return false;
90}
91
David Majnemer44f51e52014-09-10 12:51:52 +000092template <typename coff_symbol_type>
93const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
94 const coff_symbol_type *Addr =
95 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000096
David Majnemer44f51e52014-09-10 12:51:52 +000097#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000098 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +000099 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000100 if (Offset < getPointerToSymbolTable() ||
101 Offset >= getPointerToSymbolTable() +
102 (getNumberOfSymbols() * sizeof(coff_symbol_type)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103 report_fatal_error("Symbol was outside of symbol table.");
104
David Majnemer44f51e52014-09-10 12:51:52 +0000105 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
106 "Symbol did not point to the beginning of a symbol");
107#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000108
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000109 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000110}
111
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000112const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
113 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000114
115# ifndef NDEBUG
116 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000117 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000118 report_fatal_error("Section was outside of section table.");
119
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000120 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
121 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000122 "Section did not point to the beginning of a section");
123# endif
124
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000125 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000126}
127
Rafael Espindola5e812af2014-01-30 02:49:50 +0000128void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000129 if (SymbolTable16) {
130 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
131 Symb += 1 + Symb->NumberOfAuxSymbols;
132 Ref.p = reinterpret_cast<uintptr_t>(Symb);
133 } else if (SymbolTable32) {
134 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
135 Symb += 1 + Symb->NumberOfAuxSymbols;
136 Ref.p = reinterpret_cast<uintptr_t>(Symb);
137 } else {
138 llvm_unreachable("no symbol table pointer!");
139 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000140}
141
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000142std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
143 StringRef &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000144 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000145 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000146}
147
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000148std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
149 uint64_t &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000150 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolae62ab112013-11-02 18:07:48 +0000151
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000152 if (Symb.isAnyUndefined()) {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000153 Result = UnknownAddressOrSize;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000154 return object_error::success;
155 }
156 if (Symb.isCommon()) {
157 Result = UnknownAddressOrSize;
158 return object_error::success;
159 }
160 int32_t SectionNumber = Symb.getSectionNumber();
161 if (!COFF::isReservedSectionNumber(SectionNumber)) {
162 const coff_section *Section = nullptr;
163 if (std::error_code EC = getSection(SectionNumber, Section))
164 return EC;
165
David Majnemer44f51e52014-09-10 12:51:52 +0000166 Result = Section->VirtualAddress + Symb.getValue();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000167 return object_error::success;
168 }
169
170 Result = Symb.getValue();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000171 return object_error::success;
172}
173
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000174std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
175 SymbolRef::Type &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000176 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000177 int32_t SectionNumber = Symb.getSectionNumber();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000178 Result = SymbolRef::ST_Other;
David Majnemer44f51e52014-09-10 12:51:52 +0000179
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000180 if (Symb.isAnyUndefined()) {
David Meyer7e4b9762012-02-29 02:11:55 +0000181 Result = SymbolRef::ST_Unknown;
David Majnemer44f51e52014-09-10 12:51:52 +0000182 } else if (Symb.isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000183 Result = SymbolRef::ST_Function;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000184 } else if (Symb.isCommon()) {
185 Result = SymbolRef::ST_Data;
186 } else if (Symb.isFileRecord()) {
187 Result = SymbolRef::ST_File;
188 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG) {
189 Result = SymbolRef::ST_Debug;
190 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
191 const coff_section *Section = nullptr;
192 if (std::error_code EC = getSection(SectionNumber, Section))
193 return EC;
194 uint32_t Characteristics = Section->Characteristics;
195 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
196 Result = SymbolRef::ST_Function;
197 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
198 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
Rui Ueyama5efa6652014-01-16 20:22:55 +0000199 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000200 }
201 return object_error::success;
202}
203
Rafael Espindola20122a42014-01-31 20:57:12 +0000204uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000205 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000206 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000207
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000208 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000209 Result |= SymbolRef::SF_Global;
210
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000211 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000212 Result |= SymbolRef::SF_Weak;
213
David Majnemer44f51e52014-09-10 12:51:52 +0000214 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000215 Result |= SymbolRef::SF_Absolute;
216
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000217 if (Symb.isFileRecord())
218 Result |= SymbolRef::SF_FormatSpecific;
219
220 if (Symb.isSectionDefinition())
221 Result |= SymbolRef::SF_FormatSpecific;
222
223 if (Symb.isCommon())
224 Result |= SymbolRef::SF_Common;
225
226 if (Symb.isAnyUndefined())
227 Result |= SymbolRef::SF_Undefined;
228
Rafael Espindola20122a42014-01-31 20:57:12 +0000229 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000230}
231
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000232std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
233 uint64_t &Result) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000234 COFFSymbolRef Symb = getCOFFSymbol(Ref);
235
236 if (Symb.isAnyUndefined()) {
237 Result = UnknownAddressOrSize;
238 return object_error::success;
239 }
240 if (Symb.isCommon()) {
241 Result = Symb.getValue();
242 return object_error::success;
243 }
David Majnemer51ff5592014-11-06 08:10:41 +0000244
245 // Let's attempt to get the size of the symbol by looking at the address of
246 // the symbol after the symbol in question.
247 uint64_t SymbAddr;
248 if (std::error_code EC = getSymbolAddress(Ref, SymbAddr))
249 return EC;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000250 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer51ff5592014-11-06 08:10:41 +0000251 if (COFF::isReservedSectionNumber(SectionNumber)) {
252 // Absolute and debug symbols aren't sorted in any interesting way.
253 Result = 0;
254 return object_error::success;
255 }
256 const section_iterator SecEnd = section_end();
257 uint64_t AfterAddr = UnknownAddressOrSize;
258 for (const symbol_iterator &SymbI : symbols()) {
259 section_iterator SecI = SecEnd;
260 if (std::error_code EC = SymbI->getSection(SecI))
261 return EC;
262 // Check the symbol's section, skip it if it's in the wrong section.
263 // First, make sure it is in any section.
264 if (SecI == SecEnd)
265 continue;
266 // Second, make sure it is in the same section as the symbol in question.
267 if (!sectionContainsSymbol(SecI->getRawDataRefImpl(), Ref))
268 continue;
269 uint64_t Addr;
270 if (std::error_code EC = SymbI->getAddress(Addr))
271 return EC;
272 // We want to compare our symbol in question with the closest possible
273 // symbol that comes after.
274 if (AfterAddr > Addr && Addr > SymbAddr)
275 AfterAddr = Addr;
276 }
277 if (AfterAddr == UnknownAddressOrSize) {
278 // No symbol comes after this one, assume that everything after our symbol
279 // is part of it.
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000280 const coff_section *Section = nullptr;
281 if (std::error_code EC = getSection(SectionNumber, Section))
282 return EC;
David Majnemer44f51e52014-09-10 12:51:52 +0000283 Result = Section->SizeOfRawData - Symb.getValue();
David Majnemer51ff5592014-11-06 08:10:41 +0000284 } else {
285 // Take the difference between our symbol and the symbol that comes after
286 // our symbol.
287 Result = AfterAddr - SymbAddr;
Rafael Espindola8280fbb2014-10-08 17:37:19 +0000288 }
289
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000290 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000291}
292
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000293std::error_code
294COFFObjectFile::getSymbolSection(DataRefImpl Ref,
295 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000296 COFFSymbolRef Symb = getCOFFSymbol(Ref);
297 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000298 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000299 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000300 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000301 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000302 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000303 DataRefImpl Ref;
304 Ref.p = reinterpret_cast<uintptr_t>(Sec);
305 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000306 }
307 return object_error::success;
308}
309
Rafael Espindola5e812af2014-01-30 02:49:50 +0000310void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000311 const coff_section *Sec = toSec(Ref);
312 Sec += 1;
313 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000314}
315
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000316std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
317 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000318 const coff_section *Sec = toSec(Ref);
319 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000320}
321
Rafael Espindola80291272014-10-08 15:28:58 +0000322uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000323 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000324 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000325}
326
Rafael Espindola80291272014-10-08 15:28:58 +0000327uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000328 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000329}
330
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000331std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
332 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000333 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000334 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000335 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000336 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
337 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000338}
339
Rafael Espindola80291272014-10-08 15:28:58 +0000340uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000341 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000342 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000343}
344
Rafael Espindola80291272014-10-08 15:28:58 +0000345bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000346 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000347 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000348}
349
Rafael Espindola80291272014-10-08 15:28:58 +0000350bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000351 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000352 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000353}
354
Rafael Espindola80291272014-10-08 15:28:58 +0000355bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000356 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000357 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000358}
359
Rafael Espindola80291272014-10-08 15:28:58 +0000360bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
Preston Gurd2138ef62012-04-12 20:13:57 +0000361 // FIXME: Unimplemented
Rafael Espindola80291272014-10-08 15:28:58 +0000362 return true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000363}
364
Rafael Espindola80291272014-10-08 15:28:58 +0000365bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000366 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000367 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000368}
369
Rafael Espindola80291272014-10-08 15:28:58 +0000370bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000371 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000372 return false;
Preston Gurd2138ef62012-04-12 20:13:57 +0000373}
374
Rafael Espindola80291272014-10-08 15:28:58 +0000375bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000376 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000377 return false;
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000378}
379
Rafael Espindola80291272014-10-08 15:28:58 +0000380bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
381 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000382 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000383 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000384 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000385 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000386}
387
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000388relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
389 const coff_section *Sec = toSec(Ref);
390 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000391 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000392 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000393 } else {
394 auto begin = reinterpret_cast<const coff_relocation*>(
395 base() + Sec->PointerToRelocations);
396 if (Sec->hasExtendedRelocations()) {
397 // Skip the first relocation entry repurposed to store the number of
398 // relocations.
399 begin++;
400 }
401 Ret.p = reinterpret_cast<uintptr_t>(begin);
402 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000404}
405
Rui Ueyama827c8a22014-03-21 00:44:19 +0000406static uint32_t getNumberOfRelocations(const coff_section *Sec,
407 const uint8_t *base) {
408 // The field for the number of relocations in COFF section table is only
409 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
410 // NumberOfRelocations field, and the actual relocation count is stored in the
411 // VirtualAddress field in the first relocation entry.
412 if (Sec->hasExtendedRelocations()) {
413 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
414 base + Sec->PointerToRelocations);
415 return FirstReloc->VirtualAddress;
416 }
417 return Sec->NumberOfRelocations;
418}
419
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000420relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
421 const coff_section *Sec = toSec(Ref);
422 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000423 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000424 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000425 } else {
426 auto begin = reinterpret_cast<const coff_relocation*>(
427 base() + Sec->PointerToRelocations);
David Majnemer58323a92014-11-13 07:42:07 +0000428 if (Sec->hasExtendedRelocations()) {
429 // Skip the first relocation entry repurposed to store the number of
430 // relocations.
431 begin++;
432 }
Rui Ueyama827c8a22014-03-21 00:44:19 +0000433 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
434 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
435 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000436 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000437}
438
Rui Ueyamac2bed422013-09-27 21:04:00 +0000439// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000440std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000441 if (COFFHeader)
442 if (std::error_code EC =
443 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
444 getNumberOfSymbols() * getSymbolTableEntrySize()))
445 return EC;
446
447 if (COFFBigObjHeader)
448 if (std::error_code EC =
449 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
450 getNumberOfSymbols() * getSymbolTableEntrySize()))
451 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000452
453 // Find string table. The first four byte of the string table contains the
454 // total size of the string table, including the size field itself. If the
455 // string table is empty, the value of the first four byte would be 4.
456 const uint8_t *StringTableAddr =
David Majnemer44f51e52014-09-10 12:51:52 +0000457 base() + getPointerToSymbolTable() +
458 getNumberOfSymbols() * getSymbolTableEntrySize();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000459 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000460 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000461 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000463 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000464 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000465 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466
Nico Rieck773a5792014-02-26 19:51:44 +0000467 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
468 // tools like cvtres write a size of 0 for an empty table instead of 4.
469 if (StringTableSize < 4)
470 StringTableSize = 4;
471
Rui Ueyamac2bed422013-09-27 21:04:00 +0000472 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000473 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000474 return object_error::parse_failed;
475 return object_error::success;
476}
477
Rui Ueyama215a5862014-02-20 06:51:07 +0000478// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000479std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000480 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
481 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000482 uint64_t Rva = Addr - ImageBase;
483 assert(Rva <= UINT32_MAX);
484 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000485}
486
Rui Ueyamac2bed422013-09-27 21:04:00 +0000487// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000488std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000489 for (const SectionRef &S : sections()) {
490 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000491 uint32_t SectionStart = Section->VirtualAddress;
492 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000493 if (SectionStart <= Addr && Addr < SectionEnd) {
494 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000495 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
496 return object_error::success;
497 }
498 }
499 return object_error::parse_failed;
500}
501
502// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
503// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000504std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
505 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000506 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000507 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000508 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000509 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
510 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
511 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
512 return object_error::success;
513}
514
515// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000516std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000517 // First, we get the RVA of the import table. If the file lacks a pointer to
518 // the import table, do nothing.
519 const data_directory *DataEntry;
520 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
521 return object_error::success;
522
523 // Do nothing if the pointer to import table is NULL.
524 if (DataEntry->RelativeVirtualAddress == 0)
525 return object_error::success;
526
527 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000528 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000529 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000530 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000531
532 // Find the section that contains the RVA. This is needed because the RVA is
533 // the import table's memory address which is different from its file offset.
534 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000535 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000536 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000537 ImportDirectory = reinterpret_cast<
538 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000539 return object_error::success;
540}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000541
Rui Ueyama15d99352014-10-03 00:41:58 +0000542// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
543std::error_code COFFObjectFile::initDelayImportTablePtr() {
544 const data_directory *DataEntry;
545 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
546 return object_error::success;
547 if (DataEntry->RelativeVirtualAddress == 0)
548 return object_error::success;
549
550 uint32_t RVA = DataEntry->RelativeVirtualAddress;
551 NumberOfDelayImportDirectory = DataEntry->Size /
552 sizeof(delay_import_directory_table_entry) - 1;
553
554 uintptr_t IntPtr = 0;
555 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
556 return EC;
557 DelayImportDirectory = reinterpret_cast<
558 const delay_import_directory_table_entry *>(IntPtr);
559 return object_error::success;
560}
561
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000562// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000563std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000564 // First, we get the RVA of the export table. If the file lacks a pointer to
565 // the export table, do nothing.
566 const data_directory *DataEntry;
567 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
568 return object_error::success;
569
570 // Do nothing if the pointer to export table is NULL.
571 if (DataEntry->RelativeVirtualAddress == 0)
572 return object_error::success;
573
574 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
575 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000576 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000577 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000578 ExportDirectory =
579 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000580 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000581}
582
Rafael Espindola48af1c22014-08-19 18:44:46 +0000583COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
584 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000585 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
586 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
587 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
588 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000589 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000590 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000591 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000592 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000593 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000594
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000595 // The current location in the file where we are looking at.
596 uint64_t CurPtr = 0;
597
598 // PE header is optional and is present only in executables. If it exists,
599 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000600 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000601
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000602 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000603 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000604 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
605 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000606 const auto *DH = reinterpret_cast<const dos_header *>(base());
607 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
608 CurPtr = DH->AddressOfNewExeHeader;
609 // Check the PE magic bytes. ("PE\0\0")
610 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
611 EC = object_error::parse_failed;
612 return;
613 }
614 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
615 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000616 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000617 }
618
Rafael Espindola48af1c22014-08-19 18:44:46 +0000619 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000620 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000621
622 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
623 // import libraries share a common prefix but bigobj is more restrictive.
624 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
625 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
626 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
627 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
628 return;
629
630 // Verify that we are dealing with bigobj.
631 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
632 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
633 sizeof(COFF::BigObjMagic)) == 0) {
634 COFFHeader = nullptr;
635 CurPtr += sizeof(coff_bigobj_file_header);
636 } else {
637 // It's not a bigobj.
638 COFFBigObjHeader = nullptr;
639 }
640 }
641 if (COFFHeader) {
642 // The prior checkSize call may have failed. This isn't a hard error
643 // because we were just trying to sniff out bigobj.
644 EC = object_error::success;
645 CurPtr += sizeof(coff_file_header);
646
647 if (COFFHeader->isImportLibrary())
648 return;
649 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000650
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000651 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000652 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000653 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000654 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000655
656 const uint8_t *DataDirAddr;
657 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000658 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000659 PE32Header = Header;
660 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
661 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000662 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000663 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
664 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
665 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
666 } else {
667 // It's neither PE32 nor PE32+.
668 EC = object_error::parse_failed;
669 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000670 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000671 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000672 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000673 CurPtr += COFFHeader->SizeOfOptionalHeader;
674 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000675
Rafael Espindola48af1c22014-08-19 18:44:46 +0000676 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000677 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000678 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000679
Rui Ueyamac2bed422013-09-27 21:04:00 +0000680 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000681 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000682 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000683 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000684
Rui Ueyamac2bed422013-09-27 21:04:00 +0000685 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000686 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000687 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000688 if ((EC = initDelayImportTablePtr()))
689 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000690
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000691 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000692 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000693 return;
694
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000695 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000696}
697
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000698basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000699 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000700 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000701 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000702}
703
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000704basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000705 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000706 DataRefImpl Ret;
707 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000708 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000709}
710
Rui Ueyamabc654b12013-09-27 21:47:05 +0000711import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000712 return import_directory_iterator(
713 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000714}
715
Rui Ueyamabc654b12013-09-27 21:47:05 +0000716import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000717 return import_directory_iterator(
718 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000719}
David Meyerc429b802012-03-01 22:19:54 +0000720
Rui Ueyama15d99352014-10-03 00:41:58 +0000721delay_import_directory_iterator
722COFFObjectFile::delay_import_directory_begin() const {
723 return delay_import_directory_iterator(
724 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
725}
726
727delay_import_directory_iterator
728COFFObjectFile::delay_import_directory_end() const {
729 return delay_import_directory_iterator(
730 DelayImportDirectoryEntryRef(
731 DelayImportDirectory, NumberOfDelayImportDirectory, this));
732}
733
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000734export_directory_iterator COFFObjectFile::export_directory_begin() const {
735 return export_directory_iterator(
736 ExportDirectoryEntryRef(ExportDirectory, 0, this));
737}
738
739export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000740 if (!ExportDirectory)
741 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000742 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000743 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000744 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000745}
746
Rafael Espindolab5155a52014-02-10 20:24:04 +0000747section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000748 DataRefImpl Ret;
749 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
750 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000751}
752
Rafael Espindolab5155a52014-02-10 20:24:04 +0000753section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000754 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000755 int NumSections =
756 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000757 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
758 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000759}
760
761uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000762 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000763}
764
765StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000766 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000767 case COFF::IMAGE_FILE_MACHINE_I386:
768 return "COFF-i386";
769 case COFF::IMAGE_FILE_MACHINE_AMD64:
770 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000771 case COFF::IMAGE_FILE_MACHINE_ARMNT:
772 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000773 default:
774 return "COFF-<unknown arch>";
775 }
776}
777
778unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000779 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000780 case COFF::IMAGE_FILE_MACHINE_I386:
781 return Triple::x86;
782 case COFF::IMAGE_FILE_MACHINE_AMD64:
783 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000784 case COFF::IMAGE_FILE_MACHINE_ARMNT:
785 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000786 default:
787 return Triple::UnknownArch;
788 }
789}
790
Rui Ueyama979fb402014-10-09 02:16:38 +0000791iterator_range<import_directory_iterator>
792COFFObjectFile::import_directories() const {
793 return make_range(import_directory_begin(), import_directory_end());
794}
795
796iterator_range<delay_import_directory_iterator>
797COFFObjectFile::delay_import_directories() const {
798 return make_range(delay_import_directory_begin(),
799 delay_import_directory_end());
800}
801
802iterator_range<export_directory_iterator>
803COFFObjectFile::export_directories() const {
804 return make_range(export_directory_begin(), export_directory_end());
805}
806
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000807std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000808 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000809 return object_error::success;
810}
811
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000812std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000813COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
814 Res = PE32PlusHeader;
815 return object_error::success;
816}
817
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000818std::error_code
819COFFObjectFile::getDataDirectory(uint32_t Index,
820 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000821 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000822 if (!DataDirectory)
823 return object_error::parse_failed;
824 assert(PE32Header || PE32PlusHeader);
825 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
826 : PE32PlusHeader->NumberOfRvaAndSize;
827 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000828 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000829 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000830 return object_error::success;
831}
832
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000833std::error_code COFFObjectFile::getSection(int32_t Index,
834 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000835 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000836 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000837 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000838 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000839 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000840 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000841 else
842 return object_error::parse_failed;
843 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000844}
845
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000846std::error_code COFFObjectFile::getString(uint32_t Offset,
847 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000848 if (StringTableSize <= 4)
849 // Tried to get a string from an empty string table.
850 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000851 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000852 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000853 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000854 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000855}
856
David Majnemer44f51e52014-09-10 12:51:52 +0000857std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000858 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000859 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000860 if (Symbol.getStringTableOffset().Zeroes == 0) {
861 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000862 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000863 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000864 return object_error::success;
865 }
866
David Majnemer44f51e52014-09-10 12:51:52 +0000867 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000868 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000869 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000870 else
871 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000872 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000873 return object_error::success;
874}
875
David Majnemer44f51e52014-09-10 12:51:52 +0000876ArrayRef<uint8_t>
877COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000878 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000879
David Majnemer44f51e52014-09-10 12:51:52 +0000880 size_t SymbolSize = getSymbolTableEntrySize();
881 if (Symbol.getNumberOfAuxSymbols() > 0) {
882 // AUX data comes immediately after the symbol in COFF
883 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000884# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000885 // Verify that the Aux symbol points to a valid entry in the symbol table.
886 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000887 if (Offset < getPointerToSymbolTable() ||
888 Offset >=
889 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000890 report_fatal_error("Aux Symbol data was outside of symbol table.");
891
David Majnemer44f51e52014-09-10 12:51:52 +0000892 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
893 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000894# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000895 }
David Majnemer44f51e52014-09-10 12:51:52 +0000896 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000897}
898
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000899std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
900 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000901 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000902 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000903 // Null terminated, let ::strlen figure out the length.
904 Name = Sec->Name;
905 else
906 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000907 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000908
909 // Check for string table entry. First byte is '/'.
910 if (Name[0] == '/') {
911 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000912 if (Name[1] == '/') {
913 if (decodeBase64StringEntry(Name.substr(2), Offset))
914 return object_error::parse_failed;
915 } else {
916 if (Name.substr(1).getAsInteger(10, Offset))
917 return object_error::parse_failed;
918 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000919 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000920 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000921 }
922
923 Res = Name;
924 return object_error::success;
925}
926
David Majnemera9ee5c02014-10-09 08:42:31 +0000927uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
928 // SizeOfRawData and VirtualSize change what they represent depending on
929 // whether or not we have an executable image.
930 //
931 // For object files, SizeOfRawData contains the size of section's data;
932 // VirtualSize is always zero.
933 //
934 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
935 // actual section size is in VirtualSize. It is possible for VirtualSize to
936 // be greater than SizeOfRawData; the contents past that point should be
937 // considered to be zero.
938 uint32_t SectionSize;
939 if (Sec->VirtualSize)
940 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
941 else
942 SectionSize = Sec->SizeOfRawData;
943
944 return SectionSize;
945}
946
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000947std::error_code
948COFFObjectFile::getSectionContents(const coff_section *Sec,
949 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000950 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
951 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000952 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
953 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000954 // The only thing that we need to verify is that the contents is contained
955 // within the file bounds. We don't need to make sure it doesn't cover other
956 // data, as there's nothing that says that is not allowed.
957 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000958 uint32_t SectionSize = getSectionSize(Sec);
959 uintptr_t ConEnd = ConStart + SectionSize;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000960 if (ConEnd > uintptr_t(Data.getBufferEnd()))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000961 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000962 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000963 return object_error::success;
964}
965
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000966const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000967 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000968}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000969
Rafael Espindola5e812af2014-01-30 02:49:50 +0000970void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000971 Rel.p = reinterpret_cast<uintptr_t>(
972 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000973}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000974
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000975std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
976 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000977 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000978}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000979
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000980std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
981 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000982 const coff_relocation *R = toRel(Rel);
983 const support::ulittle32_t *VirtualAddressPtr;
984 if (std::error_code EC =
985 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
986 return EC;
987 Res = *VirtualAddressPtr;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000988 return object_error::success;
989}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000990
Rafael Espindola806f0062013-06-05 01:33:53 +0000991symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000992 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000993 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +0000994 if (SymbolTable16)
995 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
996 else if (SymbolTable32)
997 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
998 else
999 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001000 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001001}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001002
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001003std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1004 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001005 const coff_relocation* R = toRel(Rel);
1006 Res = R->Type;
1007 return object_error::success;
1008}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001009
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001010const coff_section *
1011COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1012 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001013}
1014
David Majnemer44f51e52014-09-10 12:51:52 +00001015COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1016 if (SymbolTable16)
1017 return toSymb<coff_symbol16>(Ref);
1018 if (SymbolTable32)
1019 return toSymb<coff_symbol32>(Ref);
1020 llvm_unreachable("no symbol table pointer!");
1021}
1022
1023COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1024 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001025}
1026
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001027const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001028COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1029 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001030}
1031
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001032#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1033 case COFF::reloc_type: \
1034 Res = #reloc_type; \
1035 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001036
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001037std::error_code
1038COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1039 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001040 const coff_relocation *Reloc = toRel(Rel);
1041 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001042 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001043 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001044 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1062 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001063 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001064 }
1065 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001066 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1067 switch (Reloc->Type) {
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1083 default:
1084 Res = "Unknown";
1085 }
1086 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001087 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001088 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1096 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1098 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1099 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1100 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001101 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001102 }
1103 break;
1104 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001105 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001106 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001107 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001108 return object_error::success;
1109}
1110
1111#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1112
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001113std::error_code
1114COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1115 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001116 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001117 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001118 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1119 if (std::error_code EC = Symb.getError())
1120 return EC;
1121 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001122 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001123 if (std::error_code EC = getSymbolName(Sym, SymName))
1124 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001125 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001126 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001127}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001128
Rafael Espindolac66d7612014-08-17 19:09:37 +00001129bool COFFObjectFile::isRelocatableObject() const {
1130 return !DataDirectory;
1131}
1132
Rui Ueyamac2bed422013-09-27 21:04:00 +00001133bool ImportDirectoryEntryRef::
1134operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001135 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001136}
1137
Rafael Espindola5e812af2014-01-30 02:49:50 +00001138void ImportDirectoryEntryRef::moveNext() {
1139 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001140}
1141
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001142std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1143 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001144 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001145 return object_error::success;
1146}
1147
Rui Ueyama861021f2014-10-02 22:05:29 +00001148static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001149makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001150 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001151 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001152 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001153 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001154 }
1155 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001156 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001157}
1158
Rui Ueyama15d99352014-10-03 00:41:58 +00001159static imported_symbol_iterator
1160importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001161 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001162 Object->getRvaPtr(RVA, IntPtr);
1163 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001164}
1165
Rui Ueyama15d99352014-10-03 00:41:58 +00001166static imported_symbol_iterator
1167importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001168 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001169 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001170 // Forward the pointer to the last entry which is null.
1171 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001172 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001173 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1174 while (*Entry++)
1175 ++Index;
1176 } else {
1177 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1178 while (*Entry++)
1179 ++Index;
1180 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001181 return makeImportedSymbolIterator(Object, IntPtr, Index);
1182}
1183
1184imported_symbol_iterator
1185ImportDirectoryEntryRef::imported_symbol_begin() const {
1186 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1187 OwningObject);
1188}
1189
1190imported_symbol_iterator
1191ImportDirectoryEntryRef::imported_symbol_end() const {
1192 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1193 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001194}
1195
Rui Ueyama979fb402014-10-09 02:16:38 +00001196iterator_range<imported_symbol_iterator>
1197ImportDirectoryEntryRef::imported_symbols() const {
1198 return make_range(imported_symbol_begin(), imported_symbol_end());
1199}
1200
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001201std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001202 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001203 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001204 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001205 return EC;
1206 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001207 return object_error::success;
1208}
1209
Rui Ueyama1e152d52014-10-02 17:02:18 +00001210std::error_code
1211ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1212 Result = ImportTable[Index].ImportLookupTableRVA;
1213 return object_error::success;
1214}
1215
1216std::error_code
1217ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1218 Result = ImportTable[Index].ImportAddressTableRVA;
1219 return object_error::success;
1220}
1221
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001222std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001223 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001224 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001225 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1226 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001227 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001228 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1229 return object_error::success;
1230}
1231
Rui Ueyama15d99352014-10-03 00:41:58 +00001232bool DelayImportDirectoryEntryRef::
1233operator==(const DelayImportDirectoryEntryRef &Other) const {
1234 return Table == Other.Table && Index == Other.Index;
1235}
1236
1237void DelayImportDirectoryEntryRef::moveNext() {
1238 ++Index;
1239}
1240
1241imported_symbol_iterator
1242DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1243 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1244 OwningObject);
1245}
1246
1247imported_symbol_iterator
1248DelayImportDirectoryEntryRef::imported_symbol_end() const {
1249 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1250 OwningObject);
1251}
1252
Rui Ueyama979fb402014-10-09 02:16:38 +00001253iterator_range<imported_symbol_iterator>
1254DelayImportDirectoryEntryRef::imported_symbols() const {
1255 return make_range(imported_symbol_begin(), imported_symbol_end());
1256}
1257
Rui Ueyama15d99352014-10-03 00:41:58 +00001258std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1259 uintptr_t IntPtr = 0;
1260 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1261 return EC;
1262 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1263 return object_error::success;
1264}
1265
Rui Ueyama1af08652014-10-03 18:07:18 +00001266std::error_code DelayImportDirectoryEntryRef::
1267getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1268 Result = Table;
1269 return object_error::success;
1270}
1271
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001272std::error_code DelayImportDirectoryEntryRef::
1273getImportAddress(int AddrIndex, uint64_t &Result) const {
1274 uint32_t RVA = Table[Index].DelayImportAddressTable +
1275 AddrIndex * (OwningObject->is64() ? 8 : 4);
1276 uintptr_t IntPtr = 0;
1277 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1278 return EC;
1279 if (OwningObject->is64())
1280 Result = *reinterpret_cast<const uint64_t *>(IntPtr);
1281 else
1282 Result = *reinterpret_cast<const uint32_t *>(IntPtr);
1283 return object_error::success;
1284}
1285
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001286bool ExportDirectoryEntryRef::
1287operator==(const ExportDirectoryEntryRef &Other) const {
1288 return ExportTable == Other.ExportTable && Index == Other.Index;
1289}
1290
Rafael Espindola5e812af2014-01-30 02:49:50 +00001291void ExportDirectoryEntryRef::moveNext() {
1292 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001293}
1294
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001295// Returns the name of the current export symbol. If the symbol is exported only
1296// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001297std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001298 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001299 if (std::error_code EC =
1300 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001301 return EC;
1302 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1303 return object_error::success;
1304}
1305
Rui Ueyamae5df6092014-01-17 22:02:24 +00001306// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001307std::error_code
1308ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001309 Result = ExportTable->OrdinalBase;
1310 return object_error::success;
1311}
1312
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001313// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001314std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001315 Result = ExportTable->OrdinalBase + Index;
1316 return object_error::success;
1317}
1318
1319// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001320std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001321 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001322 if (std::error_code EC =
1323 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001324 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001325 const export_address_table_entry *entry =
1326 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001327 Result = entry[Index].ExportRVA;
1328 return object_error::success;
1329}
1330
1331// Returns the name of the current export symbol. If the symbol is exported only
1332// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001333std::error_code
1334ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001335 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001336 if (std::error_code EC =
1337 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001338 return EC;
1339 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1340
1341 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1342 int Offset = 0;
1343 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1344 I < E; ++I, ++Offset) {
1345 if (*I != Index)
1346 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001347 if (std::error_code EC =
1348 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001349 return EC;
1350 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001351 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001352 return EC;
1353 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1354 return object_error::success;
1355 }
1356 Result = "";
1357 return object_error::success;
1358}
1359
Rui Ueyama861021f2014-10-02 22:05:29 +00001360bool ImportedSymbolRef::
1361operator==(const ImportedSymbolRef &Other) const {
1362 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1363 && Index == Other.Index;
1364}
1365
1366void ImportedSymbolRef::moveNext() {
1367 ++Index;
1368}
1369
1370std::error_code
1371ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1372 uint32_t RVA;
1373 if (Entry32) {
1374 // If a symbol is imported only by ordinal, it has no name.
1375 if (Entry32[Index].isOrdinal())
1376 return object_error::success;
1377 RVA = Entry32[Index].getHintNameRVA();
1378 } else {
1379 if (Entry64[Index].isOrdinal())
1380 return object_error::success;
1381 RVA = Entry64[Index].getHintNameRVA();
1382 }
1383 uintptr_t IntPtr = 0;
1384 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1385 return EC;
1386 // +2 because the first two bytes is hint.
1387 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1388 return object_error::success;
1389}
1390
1391std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1392 uint32_t RVA;
1393 if (Entry32) {
1394 if (Entry32[Index].isOrdinal()) {
1395 Result = Entry32[Index].getOrdinal();
1396 return object_error::success;
1397 }
1398 RVA = Entry32[Index].getHintNameRVA();
1399 } else {
1400 if (Entry64[Index].isOrdinal()) {
1401 Result = Entry64[Index].getOrdinal();
1402 return object_error::success;
1403 }
1404 RVA = Entry64[Index].getHintNameRVA();
1405 }
1406 uintptr_t IntPtr = 0;
1407 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1408 return EC;
1409 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1410 return object_error::success;
1411}
1412
Rafael Espindola437b0d52014-07-31 03:12:45 +00001413ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001414ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001415 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001416 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001417 if (EC)
1418 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001419 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001420}