blob: a1fc51ad4ca5784af337f11ed00ded5a9b5b2a32 [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,
David Majnemer94751be2014-11-13 09:50:18 +000043 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000044 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
David Majnemer94751be2014-11-13 09:50:18 +0000411static const coff_relocation *
412getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
413 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
414 if (!NumRelocs)
415 return nullptr;
416 auto begin = reinterpret_cast<const coff_relocation *>(
417 Base + Sec->PointerToRelocations);
418 if (Sec->hasExtendedRelocations()) {
419 // Skip the first relocation entry repurposed to store the number of
420 // relocations.
421 begin++;
422 }
423 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
424 return nullptr;
425 return begin;
426}
427
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000428relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
429 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000430 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000431 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000432 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000433 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000434}
435
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000436relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
437 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000438 const coff_relocation *I = getFirstReloc(Sec, Data, base());
439 if (I)
440 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000441 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000442 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000443 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000444}
445
Rui Ueyamac2bed422013-09-27 21:04:00 +0000446// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000447std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000448 if (COFFHeader)
449 if (std::error_code EC =
450 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
451 getNumberOfSymbols() * getSymbolTableEntrySize()))
452 return EC;
453
454 if (COFFBigObjHeader)
455 if (std::error_code EC =
456 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
457 getNumberOfSymbols() * getSymbolTableEntrySize()))
458 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000459
460 // Find string table. The first four byte of the string table contains the
461 // total size of the string table, including the size field itself. If the
462 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000463 uint32_t StringTableOffset = getPointerToSymbolTable() +
464 getNumberOfSymbols() * getSymbolTableEntrySize();
465 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000467 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000468 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000469 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000470 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000471 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000472 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000473
Nico Rieck773a5792014-02-26 19:51:44 +0000474 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
475 // tools like cvtres write a size of 0 for an empty table instead of 4.
476 if (StringTableSize < 4)
477 StringTableSize = 4;
478
Rui Ueyamac2bed422013-09-27 21:04:00 +0000479 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000480 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000481 return object_error::parse_failed;
482 return object_error::success;
483}
484
Rui Ueyama215a5862014-02-20 06:51:07 +0000485// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000486std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000487 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
488 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000489 uint64_t Rva = Addr - ImageBase;
490 assert(Rva <= UINT32_MAX);
491 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000492}
493
Rui Ueyamac2bed422013-09-27 21:04:00 +0000494// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000495std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000496 for (const SectionRef &S : sections()) {
497 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000498 uint32_t SectionStart = Section->VirtualAddress;
499 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000500 if (SectionStart <= Addr && Addr < SectionEnd) {
501 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000502 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
503 return object_error::success;
504 }
505 }
506 return object_error::parse_failed;
507}
508
509// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
510// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000511std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
512 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000513 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000514 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000515 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000516 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
517 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
518 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
519 return object_error::success;
520}
521
522// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000523std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000524 // First, we get the RVA of the import table. If the file lacks a pointer to
525 // the import table, do nothing.
526 const data_directory *DataEntry;
527 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
528 return object_error::success;
529
530 // Do nothing if the pointer to import table is NULL.
531 if (DataEntry->RelativeVirtualAddress == 0)
532 return object_error::success;
533
534 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000535 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000536 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000537 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000538
539 // Find the section that contains the RVA. This is needed because the RVA is
540 // the import table's memory address which is different from its file offset.
541 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000542 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000543 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000544 ImportDirectory = reinterpret_cast<
545 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000546 return object_error::success;
547}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000548
Rui Ueyama15d99352014-10-03 00:41:58 +0000549// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
550std::error_code COFFObjectFile::initDelayImportTablePtr() {
551 const data_directory *DataEntry;
552 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
553 return object_error::success;
554 if (DataEntry->RelativeVirtualAddress == 0)
555 return object_error::success;
556
557 uint32_t RVA = DataEntry->RelativeVirtualAddress;
558 NumberOfDelayImportDirectory = DataEntry->Size /
559 sizeof(delay_import_directory_table_entry) - 1;
560
561 uintptr_t IntPtr = 0;
562 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
563 return EC;
564 DelayImportDirectory = reinterpret_cast<
565 const delay_import_directory_table_entry *>(IntPtr);
566 return object_error::success;
567}
568
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000569// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000570std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000571 // First, we get the RVA of the export table. If the file lacks a pointer to
572 // the export table, do nothing.
573 const data_directory *DataEntry;
574 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
575 return object_error::success;
576
577 // Do nothing if the pointer to export table is NULL.
578 if (DataEntry->RelativeVirtualAddress == 0)
579 return object_error::success;
580
581 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
582 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000583 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000584 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000585 ExportDirectory =
586 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000587 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000588}
589
Rafael Espindola48af1c22014-08-19 18:44:46 +0000590COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
591 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000592 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
593 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
594 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
595 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000596 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000597 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000598 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000599 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000600 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000601
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000602 // The current location in the file where we are looking at.
603 uint64_t CurPtr = 0;
604
605 // PE header is optional and is present only in executables. If it exists,
606 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000607 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000608
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000609 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000610 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000611 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
612 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000613 const auto *DH = reinterpret_cast<const dos_header *>(base());
614 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
615 CurPtr = DH->AddressOfNewExeHeader;
616 // Check the PE magic bytes. ("PE\0\0")
617 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
618 EC = object_error::parse_failed;
619 return;
620 }
621 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
622 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000623 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000624 }
625
Rafael Espindola48af1c22014-08-19 18:44:46 +0000626 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000627 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000628
629 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
630 // import libraries share a common prefix but bigobj is more restrictive.
631 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
632 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
633 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
634 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
635 return;
636
637 // Verify that we are dealing with bigobj.
638 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
639 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
640 sizeof(COFF::BigObjMagic)) == 0) {
641 COFFHeader = nullptr;
642 CurPtr += sizeof(coff_bigobj_file_header);
643 } else {
644 // It's not a bigobj.
645 COFFBigObjHeader = nullptr;
646 }
647 }
648 if (COFFHeader) {
649 // The prior checkSize call may have failed. This isn't a hard error
650 // because we were just trying to sniff out bigobj.
651 EC = object_error::success;
652 CurPtr += sizeof(coff_file_header);
653
654 if (COFFHeader->isImportLibrary())
655 return;
656 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000657
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000658 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000659 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000660 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000661 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000662
663 const uint8_t *DataDirAddr;
664 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000665 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000666 PE32Header = Header;
667 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
668 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000669 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000670 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
671 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
672 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
673 } else {
674 // It's neither PE32 nor PE32+.
675 EC = object_error::parse_failed;
676 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000677 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000678 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000679 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000680 CurPtr += COFFHeader->SizeOfOptionalHeader;
681 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000682
Rafael Espindola48af1c22014-08-19 18:44:46 +0000683 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000684 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000685 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000686
Rui Ueyamac2bed422013-09-27 21:04:00 +0000687 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000688 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000689 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000690 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000691
Rui Ueyamac2bed422013-09-27 21:04:00 +0000692 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000693 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000694 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000695 if ((EC = initDelayImportTablePtr()))
696 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000697
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000698 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000699 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000700 return;
701
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000702 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000703}
704
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000705basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000706 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000707 Ret.p = getSymbolTable();
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
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000711basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000712 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000713 DataRefImpl Ret;
714 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000715 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000716}
717
Rui Ueyamabc654b12013-09-27 21:47:05 +0000718import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000719 return import_directory_iterator(
720 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000721}
722
Rui Ueyamabc654b12013-09-27 21:47:05 +0000723import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000724 return import_directory_iterator(
725 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000726}
David Meyerc429b802012-03-01 22:19:54 +0000727
Rui Ueyama15d99352014-10-03 00:41:58 +0000728delay_import_directory_iterator
729COFFObjectFile::delay_import_directory_begin() const {
730 return delay_import_directory_iterator(
731 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
732}
733
734delay_import_directory_iterator
735COFFObjectFile::delay_import_directory_end() const {
736 return delay_import_directory_iterator(
737 DelayImportDirectoryEntryRef(
738 DelayImportDirectory, NumberOfDelayImportDirectory, this));
739}
740
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000741export_directory_iterator COFFObjectFile::export_directory_begin() const {
742 return export_directory_iterator(
743 ExportDirectoryEntryRef(ExportDirectory, 0, this));
744}
745
746export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000747 if (!ExportDirectory)
748 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000749 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000750 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000751 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000752}
753
Rafael Espindolab5155a52014-02-10 20:24:04 +0000754section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000755 DataRefImpl Ret;
756 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
757 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000758}
759
Rafael Espindolab5155a52014-02-10 20:24:04 +0000760section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000761 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000762 int NumSections =
763 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000764 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
765 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000766}
767
768uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000769 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000770}
771
772StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000773 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000774 case COFF::IMAGE_FILE_MACHINE_I386:
775 return "COFF-i386";
776 case COFF::IMAGE_FILE_MACHINE_AMD64:
777 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000778 case COFF::IMAGE_FILE_MACHINE_ARMNT:
779 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000780 default:
781 return "COFF-<unknown arch>";
782 }
783}
784
785unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000786 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000787 case COFF::IMAGE_FILE_MACHINE_I386:
788 return Triple::x86;
789 case COFF::IMAGE_FILE_MACHINE_AMD64:
790 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000791 case COFF::IMAGE_FILE_MACHINE_ARMNT:
792 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000793 default:
794 return Triple::UnknownArch;
795 }
796}
797
Rui Ueyama979fb402014-10-09 02:16:38 +0000798iterator_range<import_directory_iterator>
799COFFObjectFile::import_directories() const {
800 return make_range(import_directory_begin(), import_directory_end());
801}
802
803iterator_range<delay_import_directory_iterator>
804COFFObjectFile::delay_import_directories() const {
805 return make_range(delay_import_directory_begin(),
806 delay_import_directory_end());
807}
808
809iterator_range<export_directory_iterator>
810COFFObjectFile::export_directories() const {
811 return make_range(export_directory_begin(), export_directory_end());
812}
813
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000814std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000815 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000816 return object_error::success;
817}
818
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000819std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000820COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
821 Res = PE32PlusHeader;
822 return object_error::success;
823}
824
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000825std::error_code
826COFFObjectFile::getDataDirectory(uint32_t Index,
827 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000828 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000829 if (!DataDirectory) {
830 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000831 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000832 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000833 assert(PE32Header || PE32PlusHeader);
834 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
835 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000836 if (Index >= NumEnt) {
837 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000838 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000839 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000840 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000841 return object_error::success;
842}
843
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000844std::error_code COFFObjectFile::getSection(int32_t Index,
845 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000846 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000847 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000848 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000849 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000850 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000851 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000852 else
853 return object_error::parse_failed;
854 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000855}
856
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000857std::error_code COFFObjectFile::getString(uint32_t Offset,
858 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000859 if (StringTableSize <= 4)
860 // Tried to get a string from an empty string table.
861 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000862 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000863 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000864 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000865 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000866}
867
David Majnemer44f51e52014-09-10 12:51:52 +0000868std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000869 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000870 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000871 if (Symbol.getStringTableOffset().Zeroes == 0) {
872 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000873 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000874 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000875 return object_error::success;
876 }
877
David Majnemer44f51e52014-09-10 12:51:52 +0000878 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000879 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000880 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000881 else
882 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000883 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000884 return object_error::success;
885}
886
David Majnemer44f51e52014-09-10 12:51:52 +0000887ArrayRef<uint8_t>
888COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000889 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000890
David Majnemer44f51e52014-09-10 12:51:52 +0000891 size_t SymbolSize = getSymbolTableEntrySize();
892 if (Symbol.getNumberOfAuxSymbols() > 0) {
893 // AUX data comes immediately after the symbol in COFF
894 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000895# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000896 // Verify that the Aux symbol points to a valid entry in the symbol table.
897 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000898 if (Offset < getPointerToSymbolTable() ||
899 Offset >=
900 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000901 report_fatal_error("Aux Symbol data was outside of symbol table.");
902
David Majnemer44f51e52014-09-10 12:51:52 +0000903 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
904 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000905# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000906 }
David Majnemer44f51e52014-09-10 12:51:52 +0000907 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000908}
909
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000910std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
911 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000912 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000913 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000914 // Null terminated, let ::strlen figure out the length.
915 Name = Sec->Name;
916 else
917 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000918 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000919
920 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000921 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000922 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000923 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000924 if (decodeBase64StringEntry(Name.substr(2), Offset))
925 return object_error::parse_failed;
926 } else {
927 if (Name.substr(1).getAsInteger(10, Offset))
928 return object_error::parse_failed;
929 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000930 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000931 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000932 }
933
934 Res = Name;
935 return object_error::success;
936}
937
David Majnemera9ee5c02014-10-09 08:42:31 +0000938uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
939 // SizeOfRawData and VirtualSize change what they represent depending on
940 // whether or not we have an executable image.
941 //
942 // For object files, SizeOfRawData contains the size of section's data;
943 // VirtualSize is always zero.
944 //
945 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
946 // actual section size is in VirtualSize. It is possible for VirtualSize to
947 // be greater than SizeOfRawData; the contents past that point should be
948 // considered to be zero.
949 uint32_t SectionSize;
950 if (Sec->VirtualSize)
951 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
952 else
953 SectionSize = Sec->SizeOfRawData;
954
955 return SectionSize;
956}
957
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000958std::error_code
959COFFObjectFile::getSectionContents(const coff_section *Sec,
960 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000961 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
962 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000963 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
964 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000965 // The only thing that we need to verify is that the contents is contained
966 // within the file bounds. We don't need to make sure it doesn't cover other
967 // data, as there's nothing that says that is not allowed.
968 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000969 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000970 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000971 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000972 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000973 return object_error::success;
974}
975
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000976const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000977 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000978}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000979
Rafael Espindola5e812af2014-01-30 02:49:50 +0000980void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000981 Rel.p = reinterpret_cast<uintptr_t>(
982 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000983}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000984
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000985std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
986 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000987 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000988}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000989
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000990std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
991 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000992 const coff_relocation *R = toRel(Rel);
993 const support::ulittle32_t *VirtualAddressPtr;
994 if (std::error_code EC =
995 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
996 return EC;
997 Res = *VirtualAddressPtr;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000998 return object_error::success;
999}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001000
Rafael Espindola806f0062013-06-05 01:33:53 +00001001symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001002 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001003 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +00001004 if (SymbolTable16)
1005 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1006 else if (SymbolTable32)
1007 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1008 else
David Majnemer1f80b0a2014-11-13 07:42:11 +00001009 return symbol_end();
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001010 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001011}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001012
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001013std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1014 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001015 const coff_relocation* R = toRel(Rel);
1016 Res = R->Type;
1017 return object_error::success;
1018}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001019
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001020const coff_section *
1021COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1022 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001023}
1024
David Majnemer44f51e52014-09-10 12:51:52 +00001025COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1026 if (SymbolTable16)
1027 return toSymb<coff_symbol16>(Ref);
1028 if (SymbolTable32)
1029 return toSymb<coff_symbol32>(Ref);
1030 llvm_unreachable("no symbol table pointer!");
1031}
1032
1033COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1034 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001035}
1036
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001037const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001038COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1039 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001040}
1041
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001042#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1043 case COFF::reloc_type: \
1044 Res = #reloc_type; \
1045 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001046
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001047std::error_code
1048COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1049 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001050 const coff_relocation *Reloc = toRel(Rel);
1051 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001052 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001053 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001054 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1072 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001073 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001074 }
1075 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001076 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1077 switch (Reloc->Type) {
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1093 default:
1094 Res = "Unknown";
1095 }
1096 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001097 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001098 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001099 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1100 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1101 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1102 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1103 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1104 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1105 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1106 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1107 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1108 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1109 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1110 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001111 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001112 }
1113 break;
1114 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001115 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001116 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001117 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001118 return object_error::success;
1119}
1120
1121#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1122
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001123std::error_code
1124COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1125 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001126 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001127 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001128 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1129 if (std::error_code EC = Symb.getError())
1130 return EC;
1131 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001132 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001133 if (std::error_code EC = getSymbolName(Sym, SymName))
1134 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001135 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001136 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001137}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001138
Rafael Espindolac66d7612014-08-17 19:09:37 +00001139bool COFFObjectFile::isRelocatableObject() const {
1140 return !DataDirectory;
1141}
1142
Rui Ueyamac2bed422013-09-27 21:04:00 +00001143bool ImportDirectoryEntryRef::
1144operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001145 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001146}
1147
Rafael Espindola5e812af2014-01-30 02:49:50 +00001148void ImportDirectoryEntryRef::moveNext() {
1149 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001150}
1151
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001152std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1153 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001154 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001155 return object_error::success;
1156}
1157
Rui Ueyama861021f2014-10-02 22:05:29 +00001158static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001159makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001160 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001161 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001162 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001163 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001164 }
1165 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001166 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001167}
1168
Rui Ueyama15d99352014-10-03 00:41:58 +00001169static imported_symbol_iterator
1170importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001171 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001172 Object->getRvaPtr(RVA, IntPtr);
1173 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001174}
1175
Rui Ueyama15d99352014-10-03 00:41:58 +00001176static imported_symbol_iterator
1177importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001178 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001179 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001180 // Forward the pointer to the last entry which is null.
1181 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001182 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001183 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1184 while (*Entry++)
1185 ++Index;
1186 } else {
1187 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1188 while (*Entry++)
1189 ++Index;
1190 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001191 return makeImportedSymbolIterator(Object, IntPtr, Index);
1192}
1193
1194imported_symbol_iterator
1195ImportDirectoryEntryRef::imported_symbol_begin() const {
1196 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1197 OwningObject);
1198}
1199
1200imported_symbol_iterator
1201ImportDirectoryEntryRef::imported_symbol_end() const {
1202 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1203 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001204}
1205
Rui Ueyama979fb402014-10-09 02:16:38 +00001206iterator_range<imported_symbol_iterator>
1207ImportDirectoryEntryRef::imported_symbols() const {
1208 return make_range(imported_symbol_begin(), imported_symbol_end());
1209}
1210
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001211std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001212 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001213 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001214 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001215 return EC;
1216 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001217 return object_error::success;
1218}
1219
Rui Ueyama1e152d52014-10-02 17:02:18 +00001220std::error_code
1221ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1222 Result = ImportTable[Index].ImportLookupTableRVA;
1223 return object_error::success;
1224}
1225
1226std::error_code
1227ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1228 Result = ImportTable[Index].ImportAddressTableRVA;
1229 return object_error::success;
1230}
1231
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001232std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001233 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001234 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001235 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1236 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001237 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001238 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1239 return object_error::success;
1240}
1241
Rui Ueyama15d99352014-10-03 00:41:58 +00001242bool DelayImportDirectoryEntryRef::
1243operator==(const DelayImportDirectoryEntryRef &Other) const {
1244 return Table == Other.Table && Index == Other.Index;
1245}
1246
1247void DelayImportDirectoryEntryRef::moveNext() {
1248 ++Index;
1249}
1250
1251imported_symbol_iterator
1252DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1253 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1254 OwningObject);
1255}
1256
1257imported_symbol_iterator
1258DelayImportDirectoryEntryRef::imported_symbol_end() const {
1259 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1260 OwningObject);
1261}
1262
Rui Ueyama979fb402014-10-09 02:16:38 +00001263iterator_range<imported_symbol_iterator>
1264DelayImportDirectoryEntryRef::imported_symbols() const {
1265 return make_range(imported_symbol_begin(), imported_symbol_end());
1266}
1267
Rui Ueyama15d99352014-10-03 00:41:58 +00001268std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1269 uintptr_t IntPtr = 0;
1270 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1271 return EC;
1272 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1273 return object_error::success;
1274}
1275
Rui Ueyama1af08652014-10-03 18:07:18 +00001276std::error_code DelayImportDirectoryEntryRef::
1277getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1278 Result = Table;
1279 return object_error::success;
1280}
1281
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001282std::error_code DelayImportDirectoryEntryRef::
1283getImportAddress(int AddrIndex, uint64_t &Result) const {
1284 uint32_t RVA = Table[Index].DelayImportAddressTable +
1285 AddrIndex * (OwningObject->is64() ? 8 : 4);
1286 uintptr_t IntPtr = 0;
1287 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1288 return EC;
1289 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001290 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001291 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001292 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001293 return object_error::success;
1294}
1295
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001296bool ExportDirectoryEntryRef::
1297operator==(const ExportDirectoryEntryRef &Other) const {
1298 return ExportTable == Other.ExportTable && Index == Other.Index;
1299}
1300
Rafael Espindola5e812af2014-01-30 02:49:50 +00001301void ExportDirectoryEntryRef::moveNext() {
1302 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001303}
1304
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001305// Returns the name of the current export symbol. If the symbol is exported only
1306// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001307std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001308 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001309 if (std::error_code EC =
1310 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001311 return EC;
1312 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1313 return object_error::success;
1314}
1315
Rui Ueyamae5df6092014-01-17 22:02:24 +00001316// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001317std::error_code
1318ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001319 Result = ExportTable->OrdinalBase;
1320 return object_error::success;
1321}
1322
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001323// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001324std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001325 Result = ExportTable->OrdinalBase + Index;
1326 return object_error::success;
1327}
1328
1329// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001330std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001331 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001332 if (std::error_code EC =
1333 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001334 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001335 const export_address_table_entry *entry =
1336 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001337 Result = entry[Index].ExportRVA;
1338 return object_error::success;
1339}
1340
1341// Returns the name of the current export symbol. If the symbol is exported only
1342// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001343std::error_code
1344ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001345 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001346 if (std::error_code EC =
1347 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001348 return EC;
1349 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1350
1351 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1352 int Offset = 0;
1353 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1354 I < E; ++I, ++Offset) {
1355 if (*I != Index)
1356 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001357 if (std::error_code EC =
1358 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001359 return EC;
1360 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001361 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001362 return EC;
1363 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1364 return object_error::success;
1365 }
1366 Result = "";
1367 return object_error::success;
1368}
1369
Rui Ueyama861021f2014-10-02 22:05:29 +00001370bool ImportedSymbolRef::
1371operator==(const ImportedSymbolRef &Other) const {
1372 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1373 && Index == Other.Index;
1374}
1375
1376void ImportedSymbolRef::moveNext() {
1377 ++Index;
1378}
1379
1380std::error_code
1381ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1382 uint32_t RVA;
1383 if (Entry32) {
1384 // If a symbol is imported only by ordinal, it has no name.
1385 if (Entry32[Index].isOrdinal())
1386 return object_error::success;
1387 RVA = Entry32[Index].getHintNameRVA();
1388 } else {
1389 if (Entry64[Index].isOrdinal())
1390 return object_error::success;
1391 RVA = Entry64[Index].getHintNameRVA();
1392 }
1393 uintptr_t IntPtr = 0;
1394 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1395 return EC;
1396 // +2 because the first two bytes is hint.
1397 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1398 return object_error::success;
1399}
1400
1401std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1402 uint32_t RVA;
1403 if (Entry32) {
1404 if (Entry32[Index].isOrdinal()) {
1405 Result = Entry32[Index].getOrdinal();
1406 return object_error::success;
1407 }
1408 RVA = Entry32[Index].getHintNameRVA();
1409 } else {
1410 if (Entry64[Index].isOrdinal()) {
1411 Result = Entry64[Index].getOrdinal();
1412 return object_error::success;
1413 }
1414 RVA = Entry64[Index].getHintNameRVA();
1415 }
1416 uintptr_t IntPtr = 0;
1417 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1418 return EC;
1419 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1420 return object_error::success;
1421}
1422
Rafael Espindola437b0d52014-07-31 03:12:45 +00001423ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001424ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001425 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001426 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001427 if (EC)
1428 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001429 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001430}