blob: cde6fdc5f88b91a1c4c5de7e0ff8a3d761eaf5bd [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,
David Majnemer236b0ca2014-11-17 11:17:17 +000057 const uint64_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 Majnemer236b0ca2014-11-17 11:17:17 +0000104 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000105#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000106 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000107 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000108
David Majnemer44f51e52014-09-10 12:51:52 +0000109 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
110 "Symbol did not point to the beginning of a symbol");
111#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000112
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000113 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000114}
115
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000116const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
117 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000118
119# ifndef NDEBUG
120 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000121 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000122 report_fatal_error("Section was outside of section table.");
123
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000124 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
125 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000126 "Section did not point to the beginning of a section");
127# endif
128
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000129 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000130}
131
Rafael Espindola5e812af2014-01-30 02:49:50 +0000132void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000133 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000134 if (SymbolTable16) {
135 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
136 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000137 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000138 } else if (SymbolTable32) {
139 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
140 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000141 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000142 } else {
143 llvm_unreachable("no symbol table pointer!");
144 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000145}
146
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000147std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
148 StringRef &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000150 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000151}
152
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000153std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
154 uint64_t &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000155 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolae62ab112013-11-02 18:07:48 +0000156
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000157 if (Symb.isAnyUndefined()) {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000158 Result = UnknownAddressOrSize;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000159 return object_error::success;
160 }
161 if (Symb.isCommon()) {
162 Result = UnknownAddressOrSize;
163 return object_error::success;
164 }
165 int32_t SectionNumber = Symb.getSectionNumber();
166 if (!COFF::isReservedSectionNumber(SectionNumber)) {
167 const coff_section *Section = nullptr;
168 if (std::error_code EC = getSection(SectionNumber, Section))
169 return EC;
170
David Majnemer44f51e52014-09-10 12:51:52 +0000171 Result = Section->VirtualAddress + Symb.getValue();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000172 return object_error::success;
173 }
174
175 Result = Symb.getValue();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000176 return object_error::success;
177}
178
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000179std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
180 SymbolRef::Type &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000181 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000182 int32_t SectionNumber = Symb.getSectionNumber();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000183 Result = SymbolRef::ST_Other;
David Majnemer44f51e52014-09-10 12:51:52 +0000184
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000185 if (Symb.isAnyUndefined()) {
David Meyer7e4b9762012-02-29 02:11:55 +0000186 Result = SymbolRef::ST_Unknown;
David Majnemer44f51e52014-09-10 12:51:52 +0000187 } else if (Symb.isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000188 Result = SymbolRef::ST_Function;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000189 } else if (Symb.isCommon()) {
190 Result = SymbolRef::ST_Data;
191 } else if (Symb.isFileRecord()) {
192 Result = SymbolRef::ST_File;
193 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG) {
194 Result = SymbolRef::ST_Debug;
195 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
196 const coff_section *Section = nullptr;
197 if (std::error_code EC = getSection(SectionNumber, Section))
198 return EC;
199 uint32_t Characteristics = Section->Characteristics;
200 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
201 Result = SymbolRef::ST_Function;
202 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
203 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
Rui Ueyama5efa6652014-01-16 20:22:55 +0000204 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000205 }
206 return object_error::success;
207}
208
Rafael Espindola20122a42014-01-31 20:57:12 +0000209uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000210 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000211 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000212
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000213 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000214 Result |= SymbolRef::SF_Global;
215
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000216 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000217 Result |= SymbolRef::SF_Weak;
218
David Majnemer44f51e52014-09-10 12:51:52 +0000219 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000220 Result |= SymbolRef::SF_Absolute;
221
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000222 if (Symb.isFileRecord())
223 Result |= SymbolRef::SF_FormatSpecific;
224
225 if (Symb.isSectionDefinition())
226 Result |= SymbolRef::SF_FormatSpecific;
227
228 if (Symb.isCommon())
229 Result |= SymbolRef::SF_Common;
230
231 if (Symb.isAnyUndefined())
232 Result |= SymbolRef::SF_Undefined;
233
Rafael Espindola20122a42014-01-31 20:57:12 +0000234 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000235}
236
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000237std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
238 uint64_t &Result) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000239 COFFSymbolRef Symb = getCOFFSymbol(Ref);
240
241 if (Symb.isAnyUndefined()) {
242 Result = UnknownAddressOrSize;
243 return object_error::success;
244 }
245 if (Symb.isCommon()) {
246 Result = Symb.getValue();
247 return object_error::success;
248 }
David Majnemer51ff5592014-11-06 08:10:41 +0000249
250 // Let's attempt to get the size of the symbol by looking at the address of
251 // the symbol after the symbol in question.
252 uint64_t SymbAddr;
253 if (std::error_code EC = getSymbolAddress(Ref, SymbAddr))
254 return EC;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000255 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer51ff5592014-11-06 08:10:41 +0000256 if (COFF::isReservedSectionNumber(SectionNumber)) {
257 // Absolute and debug symbols aren't sorted in any interesting way.
258 Result = 0;
259 return object_error::success;
260 }
261 const section_iterator SecEnd = section_end();
262 uint64_t AfterAddr = UnknownAddressOrSize;
263 for (const symbol_iterator &SymbI : symbols()) {
264 section_iterator SecI = SecEnd;
265 if (std::error_code EC = SymbI->getSection(SecI))
266 return EC;
267 // Check the symbol's section, skip it if it's in the wrong section.
268 // First, make sure it is in any section.
269 if (SecI == SecEnd)
270 continue;
271 // Second, make sure it is in the same section as the symbol in question.
272 if (!sectionContainsSymbol(SecI->getRawDataRefImpl(), Ref))
273 continue;
274 uint64_t Addr;
275 if (std::error_code EC = SymbI->getAddress(Addr))
276 return EC;
277 // We want to compare our symbol in question with the closest possible
278 // symbol that comes after.
279 if (AfterAddr > Addr && Addr > SymbAddr)
280 AfterAddr = Addr;
281 }
282 if (AfterAddr == UnknownAddressOrSize) {
283 // No symbol comes after this one, assume that everything after our symbol
284 // is part of it.
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000285 const coff_section *Section = nullptr;
286 if (std::error_code EC = getSection(SectionNumber, Section))
287 return EC;
David Majnemer44f51e52014-09-10 12:51:52 +0000288 Result = Section->SizeOfRawData - Symb.getValue();
David Majnemer51ff5592014-11-06 08:10:41 +0000289 } else {
290 // Take the difference between our symbol and the symbol that comes after
291 // our symbol.
292 Result = AfterAddr - SymbAddr;
Rafael Espindola8280fbb2014-10-08 17:37:19 +0000293 }
294
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000295 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000296}
297
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000298std::error_code
299COFFObjectFile::getSymbolSection(DataRefImpl Ref,
300 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000301 COFFSymbolRef Symb = getCOFFSymbol(Ref);
302 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000303 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000304 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000305 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000306 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000307 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000308 DataRefImpl Ref;
309 Ref.p = reinterpret_cast<uintptr_t>(Sec);
310 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000311 }
312 return object_error::success;
313}
314
Rafael Espindola5e812af2014-01-30 02:49:50 +0000315void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000316 const coff_section *Sec = toSec(Ref);
317 Sec += 1;
318 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000319}
320
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000321std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
322 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000323 const coff_section *Sec = toSec(Ref);
324 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000325}
326
Rafael Espindola80291272014-10-08 15:28:58 +0000327uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000328 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000329 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000330}
331
Rafael Espindola80291272014-10-08 15:28:58 +0000332uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000333 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000334}
335
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000336std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
337 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000338 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000339 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000340 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000341 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
342 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000343}
344
Rafael Espindola80291272014-10-08 15:28:58 +0000345uint64_t COFFObjectFile::getSectionAlignment(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 uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000348}
349
Rafael Espindola80291272014-10-08 15:28:58 +0000350bool COFFObjectFile::isSectionText(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_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000353}
354
Rafael Espindola80291272014-10-08 15:28:58 +0000355bool COFFObjectFile::isSectionData(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_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000358}
359
Rafael Espindola80291272014-10-08 15:28:58 +0000360bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000361 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000362 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +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::sectionContainsSymbol(DataRefImpl SecRef,
371 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000372 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000373 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000374 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000375 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000376}
377
David Majnemere830c602014-11-13 08:46:37 +0000378static uint32_t getNumberOfRelocations(const coff_section *Sec,
379 MemoryBufferRef M, const uint8_t *base) {
380 // The field for the number of relocations in COFF section table is only
381 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
382 // NumberOfRelocations field, and the actual relocation count is stored in the
383 // VirtualAddress field in the first relocation entry.
384 if (Sec->hasExtendedRelocations()) {
385 const coff_relocation *FirstReloc;
386 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
387 base + Sec->PointerToRelocations)))
388 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000389 // -1 to exclude this first relocation entry.
390 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000391 }
392 return Sec->NumberOfRelocations;
393}
394
David Majnemer94751be2014-11-13 09:50:18 +0000395static const coff_relocation *
396getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
397 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
398 if (!NumRelocs)
399 return nullptr;
400 auto begin = reinterpret_cast<const coff_relocation *>(
401 Base + Sec->PointerToRelocations);
402 if (Sec->hasExtendedRelocations()) {
403 // Skip the first relocation entry repurposed to store the number of
404 // relocations.
405 begin++;
406 }
407 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
408 return nullptr;
409 return begin;
410}
411
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000412relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
413 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000414 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000415 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000416 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000417 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000418}
419
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000420relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
421 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000422 const coff_relocation *I = getFirstReloc(Sec, Data, base());
423 if (I)
424 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000425 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000426 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000427 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000428}
429
Rui Ueyamac2bed422013-09-27 21:04:00 +0000430// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000431std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000432 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000433 if (std::error_code EC = getObject(
434 SymbolTable16, Data, base() + getPointerToSymbolTable(),
435 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000436 return EC;
437
438 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000439 if (std::error_code EC = getObject(
440 SymbolTable32, Data, base() + getPointerToSymbolTable(),
441 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000442 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000443
444 // Find string table. The first four byte of the string table contains the
445 // total size of the string table, including the size field itself. If the
446 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000447 uint32_t StringTableOffset = getPointerToSymbolTable() +
448 getNumberOfSymbols() * getSymbolTableEntrySize();
449 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000450 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000451 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000452 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000453 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000454 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000455 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000456 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000457
Nico Rieck773a5792014-02-26 19:51:44 +0000458 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
459 // tools like cvtres write a size of 0 for an empty table instead of 4.
460 if (StringTableSize < 4)
461 StringTableSize = 4;
462
Rui Ueyamac2bed422013-09-27 21:04:00 +0000463 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000464 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000465 return object_error::parse_failed;
466 return object_error::success;
467}
468
Rui Ueyama215a5862014-02-20 06:51:07 +0000469// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000470std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000471 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
472 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000473 uint64_t Rva = Addr - ImageBase;
474 assert(Rva <= UINT32_MAX);
475 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000476}
477
Rui Ueyamac2bed422013-09-27 21:04:00 +0000478// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000479std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000480 for (const SectionRef &S : sections()) {
481 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000482 uint32_t SectionStart = Section->VirtualAddress;
483 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000484 if (SectionStart <= Addr && Addr < SectionEnd) {
485 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000486 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
487 return object_error::success;
488 }
489 }
490 return object_error::parse_failed;
491}
492
493// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
494// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000495std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
496 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000497 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000498 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000499 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000500 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
501 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
502 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
503 return object_error::success;
504}
505
506// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000507std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000508 // First, we get the RVA of the import table. If the file lacks a pointer to
509 // the import table, do nothing.
510 const data_directory *DataEntry;
511 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
512 return object_error::success;
513
514 // Do nothing if the pointer to import table is NULL.
515 if (DataEntry->RelativeVirtualAddress == 0)
516 return object_error::success;
517
518 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000519 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000520 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000521 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000522
523 // Find the section that contains the RVA. This is needed because the RVA is
524 // the import table's memory address which is different from its file offset.
525 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000526 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000527 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000528 ImportDirectory = reinterpret_cast<
529 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000530 return object_error::success;
531}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000532
Rui Ueyama15d99352014-10-03 00:41:58 +0000533// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
534std::error_code COFFObjectFile::initDelayImportTablePtr() {
535 const data_directory *DataEntry;
536 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
537 return object_error::success;
538 if (DataEntry->RelativeVirtualAddress == 0)
539 return object_error::success;
540
541 uint32_t RVA = DataEntry->RelativeVirtualAddress;
542 NumberOfDelayImportDirectory = DataEntry->Size /
543 sizeof(delay_import_directory_table_entry) - 1;
544
545 uintptr_t IntPtr = 0;
546 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
547 return EC;
548 DelayImportDirectory = reinterpret_cast<
549 const delay_import_directory_table_entry *>(IntPtr);
550 return object_error::success;
551}
552
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000553// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000554std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000555 // First, we get the RVA of the export table. If the file lacks a pointer to
556 // the export table, do nothing.
557 const data_directory *DataEntry;
558 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
559 return object_error::success;
560
561 // Do nothing if the pointer to export table is NULL.
562 if (DataEntry->RelativeVirtualAddress == 0)
563 return object_error::success;
564
565 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
566 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000567 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000568 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000569 ExportDirectory =
570 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000571 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000572}
573
Rui Ueyama74e85132014-11-19 00:18:07 +0000574std::error_code COFFObjectFile::initBaseRelocPtr() {
575 const data_directory *DataEntry;
576 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
577 return object_error::success;
578 if (DataEntry->RelativeVirtualAddress == 0)
579 return object_error::success;
580
581 uintptr_t IntPtr = 0;
582 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
583 return EC;
584 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
585 IntPtr);
586 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
587 IntPtr + DataEntry->Size);
588 return object_error::success;
589}
590
Rafael Espindola48af1c22014-08-19 18:44:46 +0000591COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
592 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000593 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
594 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
595 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
596 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000597 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000598 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
599 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000600 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000601 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000602 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000603
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000604 // The current location in the file where we are looking at.
605 uint64_t CurPtr = 0;
606
607 // PE header is optional and is present only in executables. If it exists,
608 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000609 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000610
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000611 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000612 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000613 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
614 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000615 const auto *DH = reinterpret_cast<const dos_header *>(base());
616 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
617 CurPtr = DH->AddressOfNewExeHeader;
618 // Check the PE magic bytes. ("PE\0\0")
619 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
620 EC = object_error::parse_failed;
621 return;
622 }
623 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
624 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000625 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000626 }
627
Rafael Espindola48af1c22014-08-19 18:44:46 +0000628 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000629 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000630
631 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
632 // import libraries share a common prefix but bigobj is more restrictive.
633 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
634 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
635 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
636 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
637 return;
638
639 // Verify that we are dealing with bigobj.
640 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
641 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
642 sizeof(COFF::BigObjMagic)) == 0) {
643 COFFHeader = nullptr;
644 CurPtr += sizeof(coff_bigobj_file_header);
645 } else {
646 // It's not a bigobj.
647 COFFBigObjHeader = nullptr;
648 }
649 }
650 if (COFFHeader) {
651 // The prior checkSize call may have failed. This isn't a hard error
652 // because we were just trying to sniff out bigobj.
653 EC = object_error::success;
654 CurPtr += sizeof(coff_file_header);
655
656 if (COFFHeader->isImportLibrary())
657 return;
658 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000659
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000660 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000661 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000662 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000663 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000664
665 const uint8_t *DataDirAddr;
666 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000667 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000668 PE32Header = Header;
669 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
670 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000671 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000672 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
673 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
674 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
675 } else {
676 // It's neither PE32 nor PE32+.
677 EC = object_error::parse_failed;
678 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000679 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000680 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000681 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000682 CurPtr += COFFHeader->SizeOfOptionalHeader;
683 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000684
Rafael Espindola48af1c22014-08-19 18:44:46 +0000685 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000686 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000687 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000688
Rui Ueyamac2bed422013-09-27 21:04:00 +0000689 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000690 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000691 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000692 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000693 } else {
694 // We had better not have any symbols if we don't have a symbol table.
695 if (getNumberOfSymbols() != 0) {
696 EC = object_error::parse_failed;
697 return;
698 }
699 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000700
Rui Ueyamac2bed422013-09-27 21:04:00 +0000701 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000702 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000703 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000704 if ((EC = initDelayImportTablePtr()))
705 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000706
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000707 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000708 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000709 return;
710
Rui Ueyama74e85132014-11-19 00:18:07 +0000711 // Initialize the pointer to the base relocation table.
712 if ((EC = initBaseRelocPtr()))
713 return;
714
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000715 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000716}
717
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000718basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000719 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000720 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000721 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000722}
723
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000724basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000725 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000726 DataRefImpl Ret;
727 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000728 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000729}
730
Rui Ueyamabc654b12013-09-27 21:47:05 +0000731import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000732 return import_directory_iterator(
733 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000734}
735
Rui Ueyamabc654b12013-09-27 21:47:05 +0000736import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000737 return import_directory_iterator(
738 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000739}
David Meyerc429b802012-03-01 22:19:54 +0000740
Rui Ueyama15d99352014-10-03 00:41:58 +0000741delay_import_directory_iterator
742COFFObjectFile::delay_import_directory_begin() const {
743 return delay_import_directory_iterator(
744 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
745}
746
747delay_import_directory_iterator
748COFFObjectFile::delay_import_directory_end() const {
749 return delay_import_directory_iterator(
750 DelayImportDirectoryEntryRef(
751 DelayImportDirectory, NumberOfDelayImportDirectory, this));
752}
753
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000754export_directory_iterator COFFObjectFile::export_directory_begin() const {
755 return export_directory_iterator(
756 ExportDirectoryEntryRef(ExportDirectory, 0, this));
757}
758
759export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000760 if (!ExportDirectory)
761 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000762 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000763 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000764 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000765}
766
Rafael Espindolab5155a52014-02-10 20:24:04 +0000767section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000768 DataRefImpl Ret;
769 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
770 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000771}
772
Rafael Espindolab5155a52014-02-10 20:24:04 +0000773section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000774 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000775 int NumSections =
776 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000777 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
778 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000779}
780
Rui Ueyama74e85132014-11-19 00:18:07 +0000781base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
782 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
783}
784
785base_reloc_iterator COFFObjectFile::base_reloc_end() const {
786 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
787}
788
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000789uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000790 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000791}
792
793StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000794 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000795 case COFF::IMAGE_FILE_MACHINE_I386:
796 return "COFF-i386";
797 case COFF::IMAGE_FILE_MACHINE_AMD64:
798 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000799 case COFF::IMAGE_FILE_MACHINE_ARMNT:
800 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000801 default:
802 return "COFF-<unknown arch>";
803 }
804}
805
806unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000807 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000808 case COFF::IMAGE_FILE_MACHINE_I386:
809 return Triple::x86;
810 case COFF::IMAGE_FILE_MACHINE_AMD64:
811 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000812 case COFF::IMAGE_FILE_MACHINE_ARMNT:
813 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000814 default:
815 return Triple::UnknownArch;
816 }
817}
818
Rui Ueyama979fb402014-10-09 02:16:38 +0000819iterator_range<import_directory_iterator>
820COFFObjectFile::import_directories() const {
821 return make_range(import_directory_begin(), import_directory_end());
822}
823
824iterator_range<delay_import_directory_iterator>
825COFFObjectFile::delay_import_directories() const {
826 return make_range(delay_import_directory_begin(),
827 delay_import_directory_end());
828}
829
830iterator_range<export_directory_iterator>
831COFFObjectFile::export_directories() const {
832 return make_range(export_directory_begin(), export_directory_end());
833}
834
Rui Ueyama74e85132014-11-19 00:18:07 +0000835iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
836 return make_range(base_reloc_begin(), base_reloc_end());
837}
838
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000839std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000840 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000841 return object_error::success;
842}
843
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000844std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000845COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
846 Res = PE32PlusHeader;
847 return object_error::success;
848}
849
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000850std::error_code
851COFFObjectFile::getDataDirectory(uint32_t Index,
852 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000853 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000854 if (!DataDirectory) {
855 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000856 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000857 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000858 assert(PE32Header || PE32PlusHeader);
859 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
860 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000861 if (Index >= NumEnt) {
862 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000863 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000864 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000865 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000866 return object_error::success;
867}
868
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000869std::error_code COFFObjectFile::getSection(int32_t Index,
870 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000871 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000872 if (COFF::isReservedSectionNumber(Index))
David Majnemer236b0ca2014-11-17 11:17:17 +0000873 return object_error::success;
874 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000875 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000876 Result = SectionTable + (Index - 1);
David Majnemer236b0ca2014-11-17 11:17:17 +0000877 return object_error::success;
878 }
879 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000880}
881
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000882std::error_code COFFObjectFile::getString(uint32_t Offset,
883 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000884 if (StringTableSize <= 4)
885 // Tried to get a string from an empty string table.
886 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000887 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000888 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000889 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000890 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000891}
892
David Majnemer44f51e52014-09-10 12:51:52 +0000893std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000894 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000895 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000896 if (Symbol.getStringTableOffset().Zeroes == 0) {
897 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000898 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000899 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000900 return object_error::success;
901 }
902
David Majnemer44f51e52014-09-10 12:51:52 +0000903 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000904 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000905 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000906 else
907 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000908 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000909 return object_error::success;
910}
911
David Majnemer44f51e52014-09-10 12:51:52 +0000912ArrayRef<uint8_t>
913COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000914 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000915
David Majnemer44f51e52014-09-10 12:51:52 +0000916 size_t SymbolSize = getSymbolTableEntrySize();
917 if (Symbol.getNumberOfAuxSymbols() > 0) {
918 // AUX data comes immediately after the symbol in COFF
919 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000920# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000921 // Verify that the Aux symbol points to a valid entry in the symbol table.
922 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000923 if (Offset < getPointerToSymbolTable() ||
924 Offset >=
925 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000926 report_fatal_error("Aux Symbol data was outside of symbol table.");
927
David Majnemer44f51e52014-09-10 12:51:52 +0000928 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
929 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000930# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000931 }
David Majnemer44f51e52014-09-10 12:51:52 +0000932 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000933}
934
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000935std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
936 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000937 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000938 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000939 // Null terminated, let ::strlen figure out the length.
940 Name = Sec->Name;
941 else
942 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000943 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000944
945 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000946 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000947 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000948 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000949 if (decodeBase64StringEntry(Name.substr(2), Offset))
950 return object_error::parse_failed;
951 } else {
952 if (Name.substr(1).getAsInteger(10, Offset))
953 return object_error::parse_failed;
954 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000955 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000956 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000957 }
958
959 Res = Name;
960 return object_error::success;
961}
962
David Majnemera9ee5c02014-10-09 08:42:31 +0000963uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
964 // SizeOfRawData and VirtualSize change what they represent depending on
965 // whether or not we have an executable image.
966 //
967 // For object files, SizeOfRawData contains the size of section's data;
968 // VirtualSize is always zero.
969 //
970 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
971 // actual section size is in VirtualSize. It is possible for VirtualSize to
972 // be greater than SizeOfRawData; the contents past that point should be
973 // considered to be zero.
974 uint32_t SectionSize;
975 if (Sec->VirtualSize)
976 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
977 else
978 SectionSize = Sec->SizeOfRawData;
979
980 return SectionSize;
981}
982
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000983std::error_code
984COFFObjectFile::getSectionContents(const coff_section *Sec,
985 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000986 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
987 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000988 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
989 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000990 // The only thing that we need to verify is that the contents is contained
991 // within the file bounds. We don't need to make sure it doesn't cover other
992 // data, as there's nothing that says that is not allowed.
993 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000994 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000995 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000996 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000997 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000998 return object_error::success;
999}
1000
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001001const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001002 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001003}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001004
Rafael Espindola5e812af2014-01-30 02:49:50 +00001005void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001006 Rel.p = reinterpret_cast<uintptr_t>(
1007 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001008}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001009
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001010std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
1011 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +00001012 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001013}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001014
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001015std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
1016 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +00001017 const coff_relocation *R = toRel(Rel);
1018 const support::ulittle32_t *VirtualAddressPtr;
1019 if (std::error_code EC =
1020 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
1021 return EC;
1022 Res = *VirtualAddressPtr;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001023 return object_error::success;
1024}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001025
Rafael Espindola806f0062013-06-05 01:33:53 +00001026symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001027 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001028 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001029 if (R->SymbolTableIndex >= getNumberOfSymbols())
1030 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001031 if (SymbolTable16)
1032 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1033 else if (SymbolTable32)
1034 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1035 else
David Majnemerc7353b52014-11-25 07:43:14 +00001036 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001037 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001038}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001039
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001040std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1041 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001042 const coff_relocation* R = toRel(Rel);
1043 Res = R->Type;
1044 return object_error::success;
1045}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001046
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001047const coff_section *
1048COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1049 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001050}
1051
David Majnemer44f51e52014-09-10 12:51:52 +00001052COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1053 if (SymbolTable16)
1054 return toSymb<coff_symbol16>(Ref);
1055 if (SymbolTable32)
1056 return toSymb<coff_symbol32>(Ref);
1057 llvm_unreachable("no symbol table pointer!");
1058}
1059
1060COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1061 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001062}
1063
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001064const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001065COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1066 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001067}
1068
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001069#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1070 case COFF::reloc_type: \
1071 Res = #reloc_type; \
1072 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001073
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001074std::error_code
1075COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1076 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001077 const coff_relocation *Reloc = toRel(Rel);
1078 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001079 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001080 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001081 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1096 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1098 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1099 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001100 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001101 }
1102 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001103 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1104 switch (Reloc->Type) {
1105 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1106 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1107 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1108 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1109 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1110 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1111 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1112 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1113 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1114 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1115 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1116 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1117 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1118 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1119 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1120 default:
1121 Res = "Unknown";
1122 }
1123 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001124 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001125 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001126 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1127 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1128 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1129 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1130 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1131 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1132 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1133 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1134 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1135 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1136 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1137 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001138 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001139 }
1140 break;
1141 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001142 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001143 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001144 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001145 return object_error::success;
1146}
1147
1148#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1149
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001150std::error_code
1151COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1152 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001153 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001154 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001155 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1156 if (std::error_code EC = Symb.getError())
1157 return EC;
1158 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001159 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001160 if (std::error_code EC = getSymbolName(Sym, SymName))
1161 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001162 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001163 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001164}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001165
Rafael Espindolac66d7612014-08-17 19:09:37 +00001166bool COFFObjectFile::isRelocatableObject() const {
1167 return !DataDirectory;
1168}
1169
Rui Ueyamac2bed422013-09-27 21:04:00 +00001170bool ImportDirectoryEntryRef::
1171operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001172 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001173}
1174
Rafael Espindola5e812af2014-01-30 02:49:50 +00001175void ImportDirectoryEntryRef::moveNext() {
1176 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001177}
1178
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001179std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1180 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001181 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001182 return object_error::success;
1183}
1184
Rui Ueyama861021f2014-10-02 22:05:29 +00001185static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001186makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001187 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001188 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001189 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001190 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001191 }
1192 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001193 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001194}
1195
Rui Ueyama15d99352014-10-03 00:41:58 +00001196static imported_symbol_iterator
1197importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001198 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001199 Object->getRvaPtr(RVA, IntPtr);
1200 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001201}
1202
Rui Ueyama15d99352014-10-03 00:41:58 +00001203static imported_symbol_iterator
1204importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001205 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001206 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001207 // Forward the pointer to the last entry which is null.
1208 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001209 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001210 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1211 while (*Entry++)
1212 ++Index;
1213 } else {
1214 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1215 while (*Entry++)
1216 ++Index;
1217 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001218 return makeImportedSymbolIterator(Object, IntPtr, Index);
1219}
1220
1221imported_symbol_iterator
1222ImportDirectoryEntryRef::imported_symbol_begin() const {
1223 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1224 OwningObject);
1225}
1226
1227imported_symbol_iterator
1228ImportDirectoryEntryRef::imported_symbol_end() const {
1229 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1230 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001231}
1232
Rui Ueyama979fb402014-10-09 02:16:38 +00001233iterator_range<imported_symbol_iterator>
1234ImportDirectoryEntryRef::imported_symbols() const {
1235 return make_range(imported_symbol_begin(), imported_symbol_end());
1236}
1237
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001238std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001239 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001240 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001241 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001242 return EC;
1243 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001244 return object_error::success;
1245}
1246
Rui Ueyama1e152d52014-10-02 17:02:18 +00001247std::error_code
1248ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1249 Result = ImportTable[Index].ImportLookupTableRVA;
1250 return object_error::success;
1251}
1252
1253std::error_code
1254ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1255 Result = ImportTable[Index].ImportAddressTableRVA;
1256 return object_error::success;
1257}
1258
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001259std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001260 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001261 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001262 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1263 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001264 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001265 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1266 return object_error::success;
1267}
1268
Rui Ueyama15d99352014-10-03 00:41:58 +00001269bool DelayImportDirectoryEntryRef::
1270operator==(const DelayImportDirectoryEntryRef &Other) const {
1271 return Table == Other.Table && Index == Other.Index;
1272}
1273
1274void DelayImportDirectoryEntryRef::moveNext() {
1275 ++Index;
1276}
1277
1278imported_symbol_iterator
1279DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1280 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1281 OwningObject);
1282}
1283
1284imported_symbol_iterator
1285DelayImportDirectoryEntryRef::imported_symbol_end() const {
1286 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1287 OwningObject);
1288}
1289
Rui Ueyama979fb402014-10-09 02:16:38 +00001290iterator_range<imported_symbol_iterator>
1291DelayImportDirectoryEntryRef::imported_symbols() const {
1292 return make_range(imported_symbol_begin(), imported_symbol_end());
1293}
1294
Rui Ueyama15d99352014-10-03 00:41:58 +00001295std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1296 uintptr_t IntPtr = 0;
1297 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1298 return EC;
1299 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1300 return object_error::success;
1301}
1302
Rui Ueyama1af08652014-10-03 18:07:18 +00001303std::error_code DelayImportDirectoryEntryRef::
1304getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1305 Result = Table;
1306 return object_error::success;
1307}
1308
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001309std::error_code DelayImportDirectoryEntryRef::
1310getImportAddress(int AddrIndex, uint64_t &Result) const {
1311 uint32_t RVA = Table[Index].DelayImportAddressTable +
1312 AddrIndex * (OwningObject->is64() ? 8 : 4);
1313 uintptr_t IntPtr = 0;
1314 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1315 return EC;
1316 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001317 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001318 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001319 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001320 return object_error::success;
1321}
1322
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001323bool ExportDirectoryEntryRef::
1324operator==(const ExportDirectoryEntryRef &Other) const {
1325 return ExportTable == Other.ExportTable && Index == Other.Index;
1326}
1327
Rafael Espindola5e812af2014-01-30 02:49:50 +00001328void ExportDirectoryEntryRef::moveNext() {
1329 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001330}
1331
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001332// Returns the name of the current export symbol. If the symbol is exported only
1333// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001334std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001335 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001336 if (std::error_code EC =
1337 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001338 return EC;
1339 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1340 return object_error::success;
1341}
1342
Rui Ueyamae5df6092014-01-17 22:02:24 +00001343// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001344std::error_code
1345ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001346 Result = ExportTable->OrdinalBase;
1347 return object_error::success;
1348}
1349
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001350// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001351std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001352 Result = ExportTable->OrdinalBase + Index;
1353 return object_error::success;
1354}
1355
1356// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001357std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001358 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001359 if (std::error_code EC =
1360 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001361 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001362 const export_address_table_entry *entry =
1363 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001364 Result = entry[Index].ExportRVA;
1365 return object_error::success;
1366}
1367
1368// Returns the name of the current export symbol. If the symbol is exported only
1369// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001370std::error_code
1371ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001372 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001373 if (std::error_code EC =
1374 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001375 return EC;
1376 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1377
1378 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1379 int Offset = 0;
1380 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1381 I < E; ++I, ++Offset) {
1382 if (*I != Index)
1383 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001384 if (std::error_code EC =
1385 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001386 return EC;
1387 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001388 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001389 return EC;
1390 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1391 return object_error::success;
1392 }
1393 Result = "";
1394 return object_error::success;
1395}
1396
Rui Ueyama861021f2014-10-02 22:05:29 +00001397bool ImportedSymbolRef::
1398operator==(const ImportedSymbolRef &Other) const {
1399 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1400 && Index == Other.Index;
1401}
1402
1403void ImportedSymbolRef::moveNext() {
1404 ++Index;
1405}
1406
1407std::error_code
1408ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1409 uint32_t RVA;
1410 if (Entry32) {
1411 // If a symbol is imported only by ordinal, it has no name.
1412 if (Entry32[Index].isOrdinal())
1413 return object_error::success;
1414 RVA = Entry32[Index].getHintNameRVA();
1415 } else {
1416 if (Entry64[Index].isOrdinal())
1417 return object_error::success;
1418 RVA = Entry64[Index].getHintNameRVA();
1419 }
1420 uintptr_t IntPtr = 0;
1421 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1422 return EC;
1423 // +2 because the first two bytes is hint.
1424 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1425 return object_error::success;
1426}
1427
1428std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1429 uint32_t RVA;
1430 if (Entry32) {
1431 if (Entry32[Index].isOrdinal()) {
1432 Result = Entry32[Index].getOrdinal();
1433 return object_error::success;
1434 }
1435 RVA = Entry32[Index].getHintNameRVA();
1436 } else {
1437 if (Entry64[Index].isOrdinal()) {
1438 Result = Entry64[Index].getOrdinal();
1439 return object_error::success;
1440 }
1441 RVA = Entry64[Index].getHintNameRVA();
1442 }
1443 uintptr_t IntPtr = 0;
1444 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1445 return EC;
1446 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1447 return object_error::success;
1448}
1449
Rafael Espindola437b0d52014-07-31 03:12:45 +00001450ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001451ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001452 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001453 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001454 if (EC)
1455 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001456 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001457}
Rui Ueyama74e85132014-11-19 00:18:07 +00001458
1459bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1460 return Header == Other.Header && Index == Other.Index;
1461}
1462
1463void BaseRelocRef::moveNext() {
1464 // Header->BlockSize is the size of the current block, including the
1465 // size of the header itself.
1466 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001467 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001468 if (Size == Header->BlockSize) {
1469 // .reloc contains a list of base relocation blocks. Each block
1470 // consists of the header followed by entries. The header contains
1471 // how many entories will follow. When we reach the end of the
1472 // current block, proceed to the next block.
1473 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1474 reinterpret_cast<const uint8_t *>(Header) + Size);
1475 Index = 0;
1476 } else {
1477 ++Index;
1478 }
1479}
1480
1481std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1482 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1483 Type = Entry[Index].getType();
1484 return object_error::success;
1485}
1486
1487std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1488 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1489 Result = Header->PageRVA + Entry[Index].getOffset();
1490 return object_error::success;
1491}