blob: 92f920da1fad9efe9873acd6d583304994f02d06 [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::isSectionRequiredForExecution(DataRefImpl Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000366 // Sections marked 'Info', 'Remove', or 'Discardable' aren't required for
367 // execution.
368 const coff_section *Sec = toSec(Ref);
369 return !(Sec->Characteristics &
370 (COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE |
371 COFF::IMAGE_SCN_MEM_DISCARDABLE));
Preston Gurd2138ef62012-04-12 20:13:57 +0000372}
373
Rafael Espindola80291272014-10-08 15:28:58 +0000374bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000376 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000377}
378
Rafael Espindola80291272014-10-08 15:28:58 +0000379bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000380 const coff_section *Sec = toSec(Ref);
381 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000382}
383
Rafael Espindola80291272014-10-08 15:28:58 +0000384bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000385 const coff_section *Sec = toSec(Ref);
386 // Check if it's any sort of data section.
387 if (!(Sec->Characteristics & (COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
388 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)))
389 return false;
390 // If it's writable or executable or contains code, it isn't read-only data.
391 if (Sec->Characteristics &
392 (COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
393 COFF::IMAGE_SCN_MEM_WRITE))
394 return false;
395 return true;
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000396}
397
Rafael Espindola80291272014-10-08 15:28:58 +0000398bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
399 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000400 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000401 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000402 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000403 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000404}
405
David Majnemere830c602014-11-13 08:46:37 +0000406static uint32_t getNumberOfRelocations(const coff_section *Sec,
407 MemoryBufferRef M, const uint8_t *base) {
408 // The field for the number of relocations in COFF section table is only
409 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
410 // NumberOfRelocations field, and the actual relocation count is stored in the
411 // VirtualAddress field in the first relocation entry.
412 if (Sec->hasExtendedRelocations()) {
413 const coff_relocation *FirstReloc;
414 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
415 base + Sec->PointerToRelocations)))
416 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000417 // -1 to exclude this first relocation entry.
418 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000419 }
420 return Sec->NumberOfRelocations;
421}
422
David Majnemer94751be2014-11-13 09:50:18 +0000423static const coff_relocation *
424getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
425 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
426 if (!NumRelocs)
427 return nullptr;
428 auto begin = reinterpret_cast<const coff_relocation *>(
429 Base + Sec->PointerToRelocations);
430 if (Sec->hasExtendedRelocations()) {
431 // Skip the first relocation entry repurposed to store the number of
432 // relocations.
433 begin++;
434 }
435 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
436 return nullptr;
437 return begin;
438}
439
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000440relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
441 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000442 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000443 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000444 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000445 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000446}
447
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000448relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
449 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000450 const coff_relocation *I = getFirstReloc(Sec, Data, base());
451 if (I)
452 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000453 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000454 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000455 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000456}
457
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000459std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000460 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000461 if (std::error_code EC = getObject(
462 SymbolTable16, Data, base() + getPointerToSymbolTable(),
463 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000464 return EC;
465
466 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000467 if (std::error_code EC = getObject(
468 SymbolTable32, Data, base() + getPointerToSymbolTable(),
469 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000470 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000471
472 // Find string table. The first four byte of the string table contains the
473 // total size of the string table, including the size field itself. If the
474 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000475 uint32_t StringTableOffset = getPointerToSymbolTable() +
476 getNumberOfSymbols() * getSymbolTableEntrySize();
477 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000478 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000479 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000480 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000481 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000482 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000483 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000484 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000485
Nico Rieck773a5792014-02-26 19:51:44 +0000486 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
487 // tools like cvtres write a size of 0 for an empty table instead of 4.
488 if (StringTableSize < 4)
489 StringTableSize = 4;
490
Rui Ueyamac2bed422013-09-27 21:04:00 +0000491 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000492 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000493 return object_error::parse_failed;
494 return object_error::success;
495}
496
Rui Ueyama215a5862014-02-20 06:51:07 +0000497// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000498std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000499 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
500 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000501 uint64_t Rva = Addr - ImageBase;
502 assert(Rva <= UINT32_MAX);
503 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000504}
505
Rui Ueyamac2bed422013-09-27 21:04:00 +0000506// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000507std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000508 for (const SectionRef &S : sections()) {
509 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000510 uint32_t SectionStart = Section->VirtualAddress;
511 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000512 if (SectionStart <= Addr && Addr < SectionEnd) {
513 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000514 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
515 return object_error::success;
516 }
517 }
518 return object_error::parse_failed;
519}
520
521// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
522// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000523std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
524 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000525 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000526 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000527 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000528 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
529 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
530 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
531 return object_error::success;
532}
533
534// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000535std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000536 // First, we get the RVA of the import table. If the file lacks a pointer to
537 // the import table, do nothing.
538 const data_directory *DataEntry;
539 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
540 return object_error::success;
541
542 // Do nothing if the pointer to import table is NULL.
543 if (DataEntry->RelativeVirtualAddress == 0)
544 return object_error::success;
545
546 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000547 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000548 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000549 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000550
551 // Find the section that contains the RVA. This is needed because the RVA is
552 // the import table's memory address which is different from its file offset.
553 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000554 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000555 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000556 ImportDirectory = reinterpret_cast<
557 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000558 return object_error::success;
559}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000560
Rui Ueyama15d99352014-10-03 00:41:58 +0000561// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
562std::error_code COFFObjectFile::initDelayImportTablePtr() {
563 const data_directory *DataEntry;
564 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
565 return object_error::success;
566 if (DataEntry->RelativeVirtualAddress == 0)
567 return object_error::success;
568
569 uint32_t RVA = DataEntry->RelativeVirtualAddress;
570 NumberOfDelayImportDirectory = DataEntry->Size /
571 sizeof(delay_import_directory_table_entry) - 1;
572
573 uintptr_t IntPtr = 0;
574 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
575 return EC;
576 DelayImportDirectory = reinterpret_cast<
577 const delay_import_directory_table_entry *>(IntPtr);
578 return object_error::success;
579}
580
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000581// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000582std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000583 // First, we get the RVA of the export table. If the file lacks a pointer to
584 // the export table, do nothing.
585 const data_directory *DataEntry;
586 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
587 return object_error::success;
588
589 // Do nothing if the pointer to export table is NULL.
590 if (DataEntry->RelativeVirtualAddress == 0)
591 return object_error::success;
592
593 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
594 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000595 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000596 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000597 ExportDirectory =
598 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000599 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000600}
601
Rui Ueyama74e85132014-11-19 00:18:07 +0000602std::error_code COFFObjectFile::initBaseRelocPtr() {
603 const data_directory *DataEntry;
604 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
605 return object_error::success;
606 if (DataEntry->RelativeVirtualAddress == 0)
607 return object_error::success;
608
609 uintptr_t IntPtr = 0;
610 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
611 return EC;
612 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
613 IntPtr);
614 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
615 IntPtr + DataEntry->Size);
616 return object_error::success;
617}
618
Rafael Espindola48af1c22014-08-19 18:44:46 +0000619COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
620 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000621 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
622 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
623 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
624 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000625 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000626 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
627 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000628 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000629 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000630 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000631
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000632 // The current location in the file where we are looking at.
633 uint64_t CurPtr = 0;
634
635 // PE header is optional and is present only in executables. If it exists,
636 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000637 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000638
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000639 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000640 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000641 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
642 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000643 const auto *DH = reinterpret_cast<const dos_header *>(base());
644 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
645 CurPtr = DH->AddressOfNewExeHeader;
646 // Check the PE magic bytes. ("PE\0\0")
647 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
648 EC = object_error::parse_failed;
649 return;
650 }
651 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
652 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000653 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000654 }
655
Rafael Espindola48af1c22014-08-19 18:44:46 +0000656 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000657 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000658
659 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
660 // import libraries share a common prefix but bigobj is more restrictive.
661 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
662 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
663 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
664 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
665 return;
666
667 // Verify that we are dealing with bigobj.
668 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
669 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
670 sizeof(COFF::BigObjMagic)) == 0) {
671 COFFHeader = nullptr;
672 CurPtr += sizeof(coff_bigobj_file_header);
673 } else {
674 // It's not a bigobj.
675 COFFBigObjHeader = nullptr;
676 }
677 }
678 if (COFFHeader) {
679 // The prior checkSize call may have failed. This isn't a hard error
680 // because we were just trying to sniff out bigobj.
681 EC = object_error::success;
682 CurPtr += sizeof(coff_file_header);
683
684 if (COFFHeader->isImportLibrary())
685 return;
686 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000687
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000688 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000689 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000690 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000691 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000692
693 const uint8_t *DataDirAddr;
694 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000695 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000696 PE32Header = Header;
697 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
698 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000699 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000700 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
701 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
702 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
703 } else {
704 // It's neither PE32 nor PE32+.
705 EC = object_error::parse_failed;
706 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000707 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000708 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000709 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000710 CurPtr += COFFHeader->SizeOfOptionalHeader;
711 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000712
Rafael Espindola48af1c22014-08-19 18:44:46 +0000713 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000714 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000715 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000716
Rui Ueyamac2bed422013-09-27 21:04:00 +0000717 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000718 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000719 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000720 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000721 } else {
722 // We had better not have any symbols if we don't have a symbol table.
723 if (getNumberOfSymbols() != 0) {
724 EC = object_error::parse_failed;
725 return;
726 }
727 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000728
Rui Ueyamac2bed422013-09-27 21:04:00 +0000729 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000730 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000731 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000732 if ((EC = initDelayImportTablePtr()))
733 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000734
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000735 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000736 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000737 return;
738
Rui Ueyama74e85132014-11-19 00:18:07 +0000739 // Initialize the pointer to the base relocation table.
740 if ((EC = initBaseRelocPtr()))
741 return;
742
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000743 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000744}
745
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000746basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000747 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000748 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000749 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000750}
751
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000752basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000753 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000754 DataRefImpl Ret;
755 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000756 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000757}
758
Rui Ueyamabc654b12013-09-27 21:47:05 +0000759import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000760 return import_directory_iterator(
761 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000762}
763
Rui Ueyamabc654b12013-09-27 21:47:05 +0000764import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000765 return import_directory_iterator(
766 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000767}
David Meyerc429b802012-03-01 22:19:54 +0000768
Rui Ueyama15d99352014-10-03 00:41:58 +0000769delay_import_directory_iterator
770COFFObjectFile::delay_import_directory_begin() const {
771 return delay_import_directory_iterator(
772 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
773}
774
775delay_import_directory_iterator
776COFFObjectFile::delay_import_directory_end() const {
777 return delay_import_directory_iterator(
778 DelayImportDirectoryEntryRef(
779 DelayImportDirectory, NumberOfDelayImportDirectory, this));
780}
781
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000782export_directory_iterator COFFObjectFile::export_directory_begin() const {
783 return export_directory_iterator(
784 ExportDirectoryEntryRef(ExportDirectory, 0, this));
785}
786
787export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000788 if (!ExportDirectory)
789 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000790 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000791 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000792 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000793}
794
Rafael Espindolab5155a52014-02-10 20:24:04 +0000795section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000796 DataRefImpl Ret;
797 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
798 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000799}
800
Rafael Espindolab5155a52014-02-10 20:24:04 +0000801section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000802 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000803 int NumSections =
804 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000805 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
806 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000807}
808
Rui Ueyama74e85132014-11-19 00:18:07 +0000809base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
810 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
811}
812
813base_reloc_iterator COFFObjectFile::base_reloc_end() const {
814 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
815}
816
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000817uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000818 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000819}
820
821StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000822 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000823 case COFF::IMAGE_FILE_MACHINE_I386:
824 return "COFF-i386";
825 case COFF::IMAGE_FILE_MACHINE_AMD64:
826 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000827 case COFF::IMAGE_FILE_MACHINE_ARMNT:
828 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000829 default:
830 return "COFF-<unknown arch>";
831 }
832}
833
834unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000835 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000836 case COFF::IMAGE_FILE_MACHINE_I386:
837 return Triple::x86;
838 case COFF::IMAGE_FILE_MACHINE_AMD64:
839 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000840 case COFF::IMAGE_FILE_MACHINE_ARMNT:
841 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000842 default:
843 return Triple::UnknownArch;
844 }
845}
846
Rui Ueyama979fb402014-10-09 02:16:38 +0000847iterator_range<import_directory_iterator>
848COFFObjectFile::import_directories() const {
849 return make_range(import_directory_begin(), import_directory_end());
850}
851
852iterator_range<delay_import_directory_iterator>
853COFFObjectFile::delay_import_directories() const {
854 return make_range(delay_import_directory_begin(),
855 delay_import_directory_end());
856}
857
858iterator_range<export_directory_iterator>
859COFFObjectFile::export_directories() const {
860 return make_range(export_directory_begin(), export_directory_end());
861}
862
Rui Ueyama74e85132014-11-19 00:18:07 +0000863iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
864 return make_range(base_reloc_begin(), base_reloc_end());
865}
866
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000867std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000868 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000869 return object_error::success;
870}
871
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000872std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000873COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
874 Res = PE32PlusHeader;
875 return object_error::success;
876}
877
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000878std::error_code
879COFFObjectFile::getDataDirectory(uint32_t Index,
880 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000881 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000882 if (!DataDirectory) {
883 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000884 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000885 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000886 assert(PE32Header || PE32PlusHeader);
887 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
888 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000889 if (Index >= NumEnt) {
890 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000891 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000892 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000893 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000894 return object_error::success;
895}
896
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000897std::error_code COFFObjectFile::getSection(int32_t Index,
898 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000899 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000900 if (COFF::isReservedSectionNumber(Index))
David Majnemer236b0ca2014-11-17 11:17:17 +0000901 return object_error::success;
902 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000903 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000904 Result = SectionTable + (Index - 1);
David Majnemer236b0ca2014-11-17 11:17:17 +0000905 return object_error::success;
906 }
907 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000908}
909
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000910std::error_code COFFObjectFile::getString(uint32_t Offset,
911 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000912 if (StringTableSize <= 4)
913 // Tried to get a string from an empty string table.
914 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000915 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000916 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000917 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000918 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000919}
920
David Majnemer44f51e52014-09-10 12:51:52 +0000921std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000922 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000923 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000924 if (Symbol.getStringTableOffset().Zeroes == 0) {
925 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000926 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000927 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000928 return object_error::success;
929 }
930
David Majnemer44f51e52014-09-10 12:51:52 +0000931 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000932 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000933 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000934 else
935 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000936 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000937 return object_error::success;
938}
939
David Majnemer44f51e52014-09-10 12:51:52 +0000940ArrayRef<uint8_t>
941COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000942 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000943
David Majnemer44f51e52014-09-10 12:51:52 +0000944 size_t SymbolSize = getSymbolTableEntrySize();
945 if (Symbol.getNumberOfAuxSymbols() > 0) {
946 // AUX data comes immediately after the symbol in COFF
947 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000948# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000949 // Verify that the Aux symbol points to a valid entry in the symbol table.
950 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000951 if (Offset < getPointerToSymbolTable() ||
952 Offset >=
953 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000954 report_fatal_error("Aux Symbol data was outside of symbol table.");
955
David Majnemer44f51e52014-09-10 12:51:52 +0000956 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
957 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000958# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000959 }
David Majnemer44f51e52014-09-10 12:51:52 +0000960 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000961}
962
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000963std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
964 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000965 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000966 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000967 // Null terminated, let ::strlen figure out the length.
968 Name = Sec->Name;
969 else
970 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000971 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000972
973 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000974 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000975 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000976 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000977 if (decodeBase64StringEntry(Name.substr(2), Offset))
978 return object_error::parse_failed;
979 } else {
980 if (Name.substr(1).getAsInteger(10, Offset))
981 return object_error::parse_failed;
982 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000983 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000984 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000985 }
986
987 Res = Name;
988 return object_error::success;
989}
990
David Majnemera9ee5c02014-10-09 08:42:31 +0000991uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
992 // SizeOfRawData and VirtualSize change what they represent depending on
993 // whether or not we have an executable image.
994 //
995 // For object files, SizeOfRawData contains the size of section's data;
996 // VirtualSize is always zero.
997 //
998 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
999 // actual section size is in VirtualSize. It is possible for VirtualSize to
1000 // be greater than SizeOfRawData; the contents past that point should be
1001 // considered to be zero.
1002 uint32_t SectionSize;
1003 if (Sec->VirtualSize)
1004 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1005 else
1006 SectionSize = Sec->SizeOfRawData;
1007
1008 return SectionSize;
1009}
1010
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001011std::error_code
1012COFFObjectFile::getSectionContents(const coff_section *Sec,
1013 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +00001014 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
1015 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +00001016 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
1017 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001018 // The only thing that we need to verify is that the contents is contained
1019 // within the file bounds. We don't need to make sure it doesn't cover other
1020 // data, as there's nothing that says that is not allowed.
1021 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001022 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001023 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001024 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001025 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001026 return object_error::success;
1027}
1028
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001029const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001030 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001031}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001032
Rafael Espindola5e812af2014-01-30 02:49:50 +00001033void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001034 Rel.p = reinterpret_cast<uintptr_t>(
1035 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001036}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001037
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001038std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
1039 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +00001040 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001041}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001042
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001043std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
1044 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +00001045 const coff_relocation *R = toRel(Rel);
1046 const support::ulittle32_t *VirtualAddressPtr;
1047 if (std::error_code EC =
1048 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
1049 return EC;
1050 Res = *VirtualAddressPtr;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001051 return object_error::success;
1052}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001053
Rafael Espindola806f0062013-06-05 01:33:53 +00001054symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001055 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001056 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001057 if (R->SymbolTableIndex >= getNumberOfSymbols())
1058 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001059 if (SymbolTable16)
1060 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1061 else if (SymbolTable32)
1062 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1063 else
David Majnemerc7353b52014-11-25 07:43:14 +00001064 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001065 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001066}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001067
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001068std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1069 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001070 const coff_relocation* R = toRel(Rel);
1071 Res = R->Type;
1072 return object_error::success;
1073}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001074
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001075const coff_section *
1076COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1077 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001078}
1079
David Majnemer44f51e52014-09-10 12:51:52 +00001080COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1081 if (SymbolTable16)
1082 return toSymb<coff_symbol16>(Ref);
1083 if (SymbolTable32)
1084 return toSymb<coff_symbol32>(Ref);
1085 llvm_unreachable("no symbol table pointer!");
1086}
1087
1088COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1089 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001090}
1091
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001092const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001093COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1094 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001095}
1096
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001097#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1098 case COFF::reloc_type: \
1099 Res = #reloc_type; \
1100 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001101
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001102std::error_code
1103COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1104 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001105 const coff_relocation *Reloc = toRel(Rel);
1106 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001107 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001108 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001109 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001110 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1111 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1112 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1113 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1114 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1115 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1116 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1117 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1118 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1119 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1120 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1121 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1122 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1123 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1124 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1125 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1126 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1127 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001128 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001129 }
1130 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001131 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1132 switch (Reloc->Type) {
1133 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1134 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1135 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1136 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1137 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1138 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1139 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1140 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1141 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1142 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1143 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1144 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1145 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1146 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1147 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1148 default:
1149 Res = "Unknown";
1150 }
1151 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001152 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001153 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001154 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1155 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1156 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1157 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1158 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1159 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1160 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1161 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1162 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1163 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1164 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1165 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001166 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001167 }
1168 break;
1169 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001170 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001171 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001172 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001173 return object_error::success;
1174}
1175
1176#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1177
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001178std::error_code
1179COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1180 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001181 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001182 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001183 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1184 if (std::error_code EC = Symb.getError())
1185 return EC;
1186 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001187 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001188 if (std::error_code EC = getSymbolName(Sym, SymName))
1189 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001190 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001191 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001192}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001193
Rafael Espindolac66d7612014-08-17 19:09:37 +00001194bool COFFObjectFile::isRelocatableObject() const {
1195 return !DataDirectory;
1196}
1197
Rui Ueyamac2bed422013-09-27 21:04:00 +00001198bool ImportDirectoryEntryRef::
1199operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001200 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001201}
1202
Rafael Espindola5e812af2014-01-30 02:49:50 +00001203void ImportDirectoryEntryRef::moveNext() {
1204 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001205}
1206
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001207std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1208 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001209 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001210 return object_error::success;
1211}
1212
Rui Ueyama861021f2014-10-02 22:05:29 +00001213static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001214makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001215 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001216 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001217 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001218 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001219 }
1220 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001221 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001222}
1223
Rui Ueyama15d99352014-10-03 00:41:58 +00001224static imported_symbol_iterator
1225importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001226 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001227 Object->getRvaPtr(RVA, IntPtr);
1228 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001229}
1230
Rui Ueyama15d99352014-10-03 00:41:58 +00001231static imported_symbol_iterator
1232importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001233 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001234 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001235 // Forward the pointer to the last entry which is null.
1236 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001237 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001238 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1239 while (*Entry++)
1240 ++Index;
1241 } else {
1242 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1243 while (*Entry++)
1244 ++Index;
1245 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001246 return makeImportedSymbolIterator(Object, IntPtr, Index);
1247}
1248
1249imported_symbol_iterator
1250ImportDirectoryEntryRef::imported_symbol_begin() const {
1251 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1252 OwningObject);
1253}
1254
1255imported_symbol_iterator
1256ImportDirectoryEntryRef::imported_symbol_end() const {
1257 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1258 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001259}
1260
Rui Ueyama979fb402014-10-09 02:16:38 +00001261iterator_range<imported_symbol_iterator>
1262ImportDirectoryEntryRef::imported_symbols() const {
1263 return make_range(imported_symbol_begin(), imported_symbol_end());
1264}
1265
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001266std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001267 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001268 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001269 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001270 return EC;
1271 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001272 return object_error::success;
1273}
1274
Rui Ueyama1e152d52014-10-02 17:02:18 +00001275std::error_code
1276ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1277 Result = ImportTable[Index].ImportLookupTableRVA;
1278 return object_error::success;
1279}
1280
1281std::error_code
1282ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1283 Result = ImportTable[Index].ImportAddressTableRVA;
1284 return object_error::success;
1285}
1286
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001287std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001288 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001289 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001290 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1291 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001292 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001293 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1294 return object_error::success;
1295}
1296
Rui Ueyama15d99352014-10-03 00:41:58 +00001297bool DelayImportDirectoryEntryRef::
1298operator==(const DelayImportDirectoryEntryRef &Other) const {
1299 return Table == Other.Table && Index == Other.Index;
1300}
1301
1302void DelayImportDirectoryEntryRef::moveNext() {
1303 ++Index;
1304}
1305
1306imported_symbol_iterator
1307DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1308 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1309 OwningObject);
1310}
1311
1312imported_symbol_iterator
1313DelayImportDirectoryEntryRef::imported_symbol_end() const {
1314 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1315 OwningObject);
1316}
1317
Rui Ueyama979fb402014-10-09 02:16:38 +00001318iterator_range<imported_symbol_iterator>
1319DelayImportDirectoryEntryRef::imported_symbols() const {
1320 return make_range(imported_symbol_begin(), imported_symbol_end());
1321}
1322
Rui Ueyama15d99352014-10-03 00:41:58 +00001323std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1324 uintptr_t IntPtr = 0;
1325 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1326 return EC;
1327 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1328 return object_error::success;
1329}
1330
Rui Ueyama1af08652014-10-03 18:07:18 +00001331std::error_code DelayImportDirectoryEntryRef::
1332getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1333 Result = Table;
1334 return object_error::success;
1335}
1336
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001337std::error_code DelayImportDirectoryEntryRef::
1338getImportAddress(int AddrIndex, uint64_t &Result) const {
1339 uint32_t RVA = Table[Index].DelayImportAddressTable +
1340 AddrIndex * (OwningObject->is64() ? 8 : 4);
1341 uintptr_t IntPtr = 0;
1342 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1343 return EC;
1344 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001345 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001346 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001347 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001348 return object_error::success;
1349}
1350
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001351bool ExportDirectoryEntryRef::
1352operator==(const ExportDirectoryEntryRef &Other) const {
1353 return ExportTable == Other.ExportTable && Index == Other.Index;
1354}
1355
Rafael Espindola5e812af2014-01-30 02:49:50 +00001356void ExportDirectoryEntryRef::moveNext() {
1357 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001358}
1359
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001360// Returns the name of the current export symbol. If the symbol is exported only
1361// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001362std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001363 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001364 if (std::error_code EC =
1365 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001366 return EC;
1367 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1368 return object_error::success;
1369}
1370
Rui Ueyamae5df6092014-01-17 22:02:24 +00001371// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001372std::error_code
1373ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001374 Result = ExportTable->OrdinalBase;
1375 return object_error::success;
1376}
1377
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001378// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001379std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001380 Result = ExportTable->OrdinalBase + Index;
1381 return object_error::success;
1382}
1383
1384// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001385std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001386 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001387 if (std::error_code EC =
1388 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001389 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001390 const export_address_table_entry *entry =
1391 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001392 Result = entry[Index].ExportRVA;
1393 return object_error::success;
1394}
1395
1396// Returns the name of the current export symbol. If the symbol is exported only
1397// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001398std::error_code
1399ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001400 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001401 if (std::error_code EC =
1402 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001403 return EC;
1404 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1405
1406 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1407 int Offset = 0;
1408 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1409 I < E; ++I, ++Offset) {
1410 if (*I != Index)
1411 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001412 if (std::error_code EC =
1413 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001414 return EC;
1415 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001416 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001417 return EC;
1418 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1419 return object_error::success;
1420 }
1421 Result = "";
1422 return object_error::success;
1423}
1424
Rui Ueyama861021f2014-10-02 22:05:29 +00001425bool ImportedSymbolRef::
1426operator==(const ImportedSymbolRef &Other) const {
1427 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1428 && Index == Other.Index;
1429}
1430
1431void ImportedSymbolRef::moveNext() {
1432 ++Index;
1433}
1434
1435std::error_code
1436ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1437 uint32_t RVA;
1438 if (Entry32) {
1439 // If a symbol is imported only by ordinal, it has no name.
1440 if (Entry32[Index].isOrdinal())
1441 return object_error::success;
1442 RVA = Entry32[Index].getHintNameRVA();
1443 } else {
1444 if (Entry64[Index].isOrdinal())
1445 return object_error::success;
1446 RVA = Entry64[Index].getHintNameRVA();
1447 }
1448 uintptr_t IntPtr = 0;
1449 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1450 return EC;
1451 // +2 because the first two bytes is hint.
1452 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1453 return object_error::success;
1454}
1455
1456std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1457 uint32_t RVA;
1458 if (Entry32) {
1459 if (Entry32[Index].isOrdinal()) {
1460 Result = Entry32[Index].getOrdinal();
1461 return object_error::success;
1462 }
1463 RVA = Entry32[Index].getHintNameRVA();
1464 } else {
1465 if (Entry64[Index].isOrdinal()) {
1466 Result = Entry64[Index].getOrdinal();
1467 return object_error::success;
1468 }
1469 RVA = Entry64[Index].getHintNameRVA();
1470 }
1471 uintptr_t IntPtr = 0;
1472 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1473 return EC;
1474 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1475 return object_error::success;
1476}
1477
Rafael Espindola437b0d52014-07-31 03:12:45 +00001478ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001479ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001480 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001481 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001482 if (EC)
1483 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001484 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001485}
Rui Ueyama74e85132014-11-19 00:18:07 +00001486
1487bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1488 return Header == Other.Header && Index == Other.Index;
1489}
1490
1491void BaseRelocRef::moveNext() {
1492 // Header->BlockSize is the size of the current block, including the
1493 // size of the header itself.
1494 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001495 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001496 if (Size == Header->BlockSize) {
1497 // .reloc contains a list of base relocation blocks. Each block
1498 // consists of the header followed by entries. The header contains
1499 // how many entories will follow. When we reach the end of the
1500 // current block, proceed to the next block.
1501 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1502 reinterpret_cast<const uint8_t *>(Header) + Size);
1503 Index = 0;
1504 } else {
1505 ++Index;
1506 }
1507}
1508
1509std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1510 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1511 Type = Entry[Index].getType();
1512 return object_error::success;
1513}
1514
1515std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1516 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1517 Result = Header->PageRVA + Entry[Index].getOffset();
1518 return object_error::success;
1519}