blob: a00d0c43e504b1ccbfb1a985b9be6901ee86432a [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. Spencer8e90ada2011-01-20 06:38:34 +000016#include "llvm/ADT/StringSwitch.h"
17#include "llvm/ADT/Triple.h"
Rui Ueyama6a75acb2015-06-25 00:07:39 +000018#include "llvm/ADT/iterator_range.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 }
Rui Ueyama7d099192015-06-09 15:20:42 +000049 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000050}
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);
Rui Ueyama7d099192015-06-09 15:20:42 +000062 return std::error_code();
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
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000147Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000148 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000149 StringRef Result;
150 std::error_code EC = getSymbolName(Symb, Result);
151 if (EC)
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000152 return errorCodeToError(EC);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000153 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000154}
155
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000156uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
157 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000158}
159
Rafael Espindolaed067c42015-07-03 18:19:00 +0000160ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
161 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000162 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000163 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000164
165 if (Symb.isAnyUndefined() || Symb.isCommon() ||
166 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000167 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000168
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000169 const coff_section *Section = nullptr;
170 if (std::error_code EC = getSection(SectionNumber, Section))
171 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000172 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000173
174 // The section VirtualAddress does not include ImageBase, and we want to
175 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000176 Result += getImageBase();
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000177
Rafael Espindolaed067c42015-07-03 18:19:00 +0000178 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000179}
180
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000181ErrorOr<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000182 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000183 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000184
Peter Collingbournee834f422015-08-06 05:26:35 +0000185 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
186 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000187 if (Symb.isAnyUndefined())
188 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000189 if (Symb.isCommon())
190 return SymbolRef::ST_Data;
191 if (Symb.isFileRecord())
192 return SymbolRef::ST_File;
193
194 // TODO: perhaps we need a new symbol type ST_Section.
195 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
196 return SymbolRef::ST_Debug;
197
198 if (!COFF::isReservedSectionNumber(SectionNumber))
199 return SymbolRef::ST_Data;
200
201 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000202}
203
Rafael Espindola20122a42014-01-31 20:57:12 +0000204uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000205 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000206 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000207
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000208 if (Symb.isExternal() || Symb.isWeakExternal())
Lang Hames9dc0eb42016-01-25 01:21:45 +0000209 Result |= SymbolRef::SF_Global;
David Meyer1df4b842012-02-28 23:47:53 +0000210
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000211 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000212 Result |= SymbolRef::SF_Weak;
213
David Majnemer44f51e52014-09-10 12:51:52 +0000214 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000215 Result |= SymbolRef::SF_Absolute;
216
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000217 if (Symb.isFileRecord())
218 Result |= SymbolRef::SF_FormatSpecific;
219
220 if (Symb.isSectionDefinition())
221 Result |= SymbolRef::SF_FormatSpecific;
222
223 if (Symb.isCommon())
224 Result |= SymbolRef::SF_Common;
225
226 if (Symb.isAnyUndefined())
227 Result |= SymbolRef::SF_Undefined;
228
Rafael Espindola20122a42014-01-31 20:57:12 +0000229 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000230}
231
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000232uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000233 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000234 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000235}
236
Rafael Espindola8bab8892015-08-07 23:27:14 +0000237ErrorOr<section_iterator>
238COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000239 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000240 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
241 return section_end();
242 const coff_section *Sec = nullptr;
243 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
244 return EC;
245 DataRefImpl Ret;
246 Ret.p = reinterpret_cast<uintptr_t>(Sec);
247 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000248}
249
Rafael Espindola6bf32212015-06-24 19:57:32 +0000250unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
251 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
252 return Symb.getSectionNumber();
253}
254
Rafael Espindola5e812af2014-01-30 02:49:50 +0000255void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000256 const coff_section *Sec = toSec(Ref);
257 Sec += 1;
258 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000259}
260
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000261std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
262 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000263 const coff_section *Sec = toSec(Ref);
264 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000265}
266
Rafael Espindola80291272014-10-08 15:28:58 +0000267uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000268 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000269 uint64_t Result = Sec->VirtualAddress;
270
271 // The section VirtualAddress does not include ImageBase, and we want to
272 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000273 Result += getImageBase();
David Majnemer7c6a0712015-07-31 17:40:24 +0000274 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000275}
276
Rafael Espindola80291272014-10-08 15:28:58 +0000277uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000278 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000279}
280
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000281std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
282 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000283 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000284 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000285 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000286 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
287 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000288}
289
Rafael Espindola80291272014-10-08 15:28:58 +0000290uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000291 const coff_section *Sec = toSec(Ref);
David Majnemer511391f2016-03-17 16:55:18 +0000292 return Sec->getAlignment();
Michael J. Spencer79894602011-10-10 21:55:43 +0000293}
294
Rafael Espindola80291272014-10-08 15:28:58 +0000295bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000296 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000297 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000298}
299
Rafael Espindola80291272014-10-08 15:28:58 +0000300bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000301 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000302 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000303}
304
Rafael Espindola80291272014-10-08 15:28:58 +0000305bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000306 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000307 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
308 COFF::IMAGE_SCN_MEM_READ |
309 COFF::IMAGE_SCN_MEM_WRITE;
310 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000311}
312
Rafael Espindola6bf32212015-06-24 19:57:32 +0000313unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
314 uintptr_t Offset =
315 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
316 assert((Offset % sizeof(coff_section)) == 0);
317 return (Offset / sizeof(coff_section)) + 1;
318}
319
Rafael Espindola80291272014-10-08 15:28:58 +0000320bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000321 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000322 // In COFF, a virtual section won't have any in-file
323 // content, so the file pointer to the content will be zero.
324 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000325}
326
David Majnemere830c602014-11-13 08:46:37 +0000327static uint32_t getNumberOfRelocations(const coff_section *Sec,
328 MemoryBufferRef M, const uint8_t *base) {
329 // The field for the number of relocations in COFF section table is only
330 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
331 // NumberOfRelocations field, and the actual relocation count is stored in the
332 // VirtualAddress field in the first relocation entry.
333 if (Sec->hasExtendedRelocations()) {
334 const coff_relocation *FirstReloc;
335 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
336 base + Sec->PointerToRelocations)))
337 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000338 // -1 to exclude this first relocation entry.
339 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000340 }
341 return Sec->NumberOfRelocations;
342}
343
David Majnemer94751be2014-11-13 09:50:18 +0000344static const coff_relocation *
345getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
346 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
347 if (!NumRelocs)
348 return nullptr;
349 auto begin = reinterpret_cast<const coff_relocation *>(
350 Base + Sec->PointerToRelocations);
351 if (Sec->hasExtendedRelocations()) {
352 // Skip the first relocation entry repurposed to store the number of
353 // relocations.
354 begin++;
355 }
356 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
357 return nullptr;
358 return begin;
359}
360
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000361relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
362 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000363 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000364 if (begin && Sec->VirtualAddress != 0)
365 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000366 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000367 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000368 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000369}
370
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000371relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
372 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000373 const coff_relocation *I = getFirstReloc(Sec, Data, base());
374 if (I)
375 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000376 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000377 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000378 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000379}
380
Rui Ueyamac2bed422013-09-27 21:04:00 +0000381// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000382std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000383 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000384 if (std::error_code EC = getObject(
385 SymbolTable16, Data, base() + getPointerToSymbolTable(),
386 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000387 return EC;
388
389 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000390 if (std::error_code EC = getObject(
391 SymbolTable32, Data, base() + getPointerToSymbolTable(),
392 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000393 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000394
395 // Find string table. The first four byte of the string table contains the
396 // total size of the string table, including the size field itself. If the
397 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000398 uint32_t StringTableOffset = getPointerToSymbolTable() +
399 getNumberOfSymbols() * getSymbolTableEntrySize();
400 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000401 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000402 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000404 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000405 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000406 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000407 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408
Nico Rieck773a5792014-02-26 19:51:44 +0000409 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
410 // tools like cvtres write a size of 0 for an empty table instead of 4.
411 if (StringTableSize < 4)
412 StringTableSize = 4;
413
Rui Ueyamac2bed422013-09-27 21:04:00 +0000414 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000415 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000416 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000417 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000418}
419
Reid Kleckner21427ad2015-10-09 00:15:08 +0000420uint64_t COFFObjectFile::getImageBase() const {
Reid Klecknere94fef72015-10-09 00:15:01 +0000421 if (PE32Header)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000422 return PE32Header->ImageBase;
Reid Klecknere94fef72015-10-09 00:15:01 +0000423 else if (PE32PlusHeader)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000424 return PE32PlusHeader->ImageBase;
425 // This actually comes up in practice.
426 return 0;
Reid Klecknere94fef72015-10-09 00:15:01 +0000427}
428
Rui Ueyama215a5862014-02-20 06:51:07 +0000429// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000430std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner21427ad2015-10-09 00:15:08 +0000431 uint64_t ImageBase = getImageBase();
Rui Ueyamab7a40082014-02-20 19:14:56 +0000432 uint64_t Rva = Addr - ImageBase;
433 assert(Rva <= UINT32_MAX);
434 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000435}
436
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000438std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000439 for (const SectionRef &S : sections()) {
440 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441 uint32_t SectionStart = Section->VirtualAddress;
442 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000443 if (SectionStart <= Addr && Addr < SectionEnd) {
444 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000445 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000446 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000447 }
448 }
449 return object_error::parse_failed;
450}
451
452// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
453// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000454std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
455 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000456 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000457 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000458 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000459 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
460 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
461 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000462 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000463}
464
465// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000466std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000467 // First, we get the RVA of the import table. If the file lacks a pointer to
468 // the import table, do nothing.
469 const data_directory *DataEntry;
470 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000471 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000472
473 // Do nothing if the pointer to import table is NULL.
474 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000475 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000476
477 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000478 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000479 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000480 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000481
482 // Find the section that contains the RVA. This is needed because the RVA is
483 // the import table's memory address which is different from its file offset.
484 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000485 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000486 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000487 ImportDirectory = reinterpret_cast<
488 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000489 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000490}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000491
Rui Ueyama15d99352014-10-03 00:41:58 +0000492// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
493std::error_code COFFObjectFile::initDelayImportTablePtr() {
494 const data_directory *DataEntry;
495 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000496 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000497 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000498 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000499
500 uint32_t RVA = DataEntry->RelativeVirtualAddress;
501 NumberOfDelayImportDirectory = DataEntry->Size /
502 sizeof(delay_import_directory_table_entry) - 1;
503
504 uintptr_t IntPtr = 0;
505 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
506 return EC;
507 DelayImportDirectory = reinterpret_cast<
508 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000509 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000510}
511
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000512// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000513std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000514 // First, we get the RVA of the export table. If the file lacks a pointer to
515 // the export table, do nothing.
516 const data_directory *DataEntry;
517 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000518 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000519
520 // Do nothing if the pointer to export table is NULL.
521 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000522 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000523
524 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
525 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000526 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000527 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000528 ExportDirectory =
529 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000530 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000531}
532
Rui Ueyama74e85132014-11-19 00:18:07 +0000533std::error_code COFFObjectFile::initBaseRelocPtr() {
534 const data_directory *DataEntry;
535 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000536 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000537 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000538 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000539
540 uintptr_t IntPtr = 0;
541 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
542 return EC;
543 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
544 IntPtr);
545 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
546 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000547 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000548}
549
Rafael Espindola48af1c22014-08-19 18:44:46 +0000550COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
551 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000552 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
553 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
554 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
555 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000556 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000557 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
558 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000559 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000560 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000561 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000562
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000563 // The current location in the file where we are looking at.
564 uint64_t CurPtr = 0;
565
566 // PE header is optional and is present only in executables. If it exists,
567 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000568 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000569
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000570 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000571 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000572 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
573 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000574 const auto *DH = reinterpret_cast<const dos_header *>(base());
575 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
576 CurPtr = DH->AddressOfNewExeHeader;
577 // Check the PE magic bytes. ("PE\0\0")
578 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
579 EC = object_error::parse_failed;
580 return;
581 }
582 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
583 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000584 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000585 }
586
Rafael Espindola48af1c22014-08-19 18:44:46 +0000587 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000588 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000589
590 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
591 // import libraries share a common prefix but bigobj is more restrictive.
592 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
593 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
594 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
595 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
596 return;
597
598 // Verify that we are dealing with bigobj.
599 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
600 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
601 sizeof(COFF::BigObjMagic)) == 0) {
602 COFFHeader = nullptr;
603 CurPtr += sizeof(coff_bigobj_file_header);
604 } else {
605 // It's not a bigobj.
606 COFFBigObjHeader = nullptr;
607 }
608 }
609 if (COFFHeader) {
610 // The prior checkSize call may have failed. This isn't a hard error
611 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000612 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000613 CurPtr += sizeof(coff_file_header);
614
615 if (COFFHeader->isImportLibrary())
616 return;
617 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000618
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000619 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000620 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000621 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000622 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000623
624 const uint8_t *DataDirAddr;
625 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000626 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000627 PE32Header = Header;
628 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
629 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000630 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000631 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
632 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
633 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
634 } else {
635 // It's neither PE32 nor PE32+.
636 EC = object_error::parse_failed;
637 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000638 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000639 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000640 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000641 CurPtr += COFFHeader->SizeOfOptionalHeader;
642 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000643
Rafael Espindola48af1c22014-08-19 18:44:46 +0000644 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000645 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000646 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000647
Rui Ueyamac2bed422013-09-27 21:04:00 +0000648 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000649 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000650 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000651 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000652 } else {
653 // We had better not have any symbols if we don't have a symbol table.
654 if (getNumberOfSymbols() != 0) {
655 EC = object_error::parse_failed;
656 return;
657 }
658 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000659
Rui Ueyamac2bed422013-09-27 21:04:00 +0000660 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000661 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000662 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000663 if ((EC = initDelayImportTablePtr()))
664 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000665
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000666 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000667 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000668 return;
669
Rui Ueyama74e85132014-11-19 00:18:07 +0000670 // Initialize the pointer to the base relocation table.
671 if ((EC = initBaseRelocPtr()))
672 return;
673
Rui Ueyama7d099192015-06-09 15:20:42 +0000674 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000675}
676
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000677basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000678 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000679 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000680 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000681}
682
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000683basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000684 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000685 DataRefImpl Ret;
686 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000687 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000688}
689
Rui Ueyamabc654b12013-09-27 21:47:05 +0000690import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000691 return import_directory_iterator(
692 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000693}
694
Rui Ueyamabc654b12013-09-27 21:47:05 +0000695import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000696 return import_directory_iterator(
697 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000698}
David Meyerc429b802012-03-01 22:19:54 +0000699
Rui Ueyama15d99352014-10-03 00:41:58 +0000700delay_import_directory_iterator
701COFFObjectFile::delay_import_directory_begin() const {
702 return delay_import_directory_iterator(
703 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
704}
705
706delay_import_directory_iterator
707COFFObjectFile::delay_import_directory_end() const {
708 return delay_import_directory_iterator(
709 DelayImportDirectoryEntryRef(
710 DelayImportDirectory, NumberOfDelayImportDirectory, this));
711}
712
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000713export_directory_iterator COFFObjectFile::export_directory_begin() const {
714 return export_directory_iterator(
715 ExportDirectoryEntryRef(ExportDirectory, 0, this));
716}
717
718export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000719 if (!ExportDirectory)
720 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000721 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000722 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000723 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000724}
725
Rafael Espindolab5155a52014-02-10 20:24:04 +0000726section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000727 DataRefImpl Ret;
728 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
729 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000730}
731
Rafael Espindolab5155a52014-02-10 20:24:04 +0000732section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000733 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000734 int NumSections =
735 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000736 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
737 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000738}
739
Rui Ueyama74e85132014-11-19 00:18:07 +0000740base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
741 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
742}
743
744base_reloc_iterator COFFObjectFile::base_reloc_end() const {
745 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
746}
747
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000748uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000749 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000750}
751
752StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000753 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000754 case COFF::IMAGE_FILE_MACHINE_I386:
755 return "COFF-i386";
756 case COFF::IMAGE_FILE_MACHINE_AMD64:
757 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000758 case COFF::IMAGE_FILE_MACHINE_ARMNT:
759 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000760 case COFF::IMAGE_FILE_MACHINE_ARM64:
761 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000762 default:
763 return "COFF-<unknown arch>";
764 }
765}
766
767unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000768 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000769 case COFF::IMAGE_FILE_MACHINE_I386:
770 return Triple::x86;
771 case COFF::IMAGE_FILE_MACHINE_AMD64:
772 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000773 case COFF::IMAGE_FILE_MACHINE_ARMNT:
774 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000775 case COFF::IMAGE_FILE_MACHINE_ARM64:
776 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000777 default:
778 return Triple::UnknownArch;
779 }
780}
781
Rui Ueyama979fb402014-10-09 02:16:38 +0000782iterator_range<import_directory_iterator>
783COFFObjectFile::import_directories() const {
784 return make_range(import_directory_begin(), import_directory_end());
785}
786
787iterator_range<delay_import_directory_iterator>
788COFFObjectFile::delay_import_directories() const {
789 return make_range(delay_import_directory_begin(),
790 delay_import_directory_end());
791}
792
793iterator_range<export_directory_iterator>
794COFFObjectFile::export_directories() const {
795 return make_range(export_directory_begin(), export_directory_end());
796}
797
Rui Ueyama74e85132014-11-19 00:18:07 +0000798iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
799 return make_range(base_reloc_begin(), base_reloc_end());
800}
801
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000802std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000803 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000804 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000805}
806
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000807std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000808COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
809 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000810 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000811}
812
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000813std::error_code
814COFFObjectFile::getDataDirectory(uint32_t Index,
815 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000816 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000817 if (!DataDirectory) {
818 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000819 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000820 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000821 assert(PE32Header || PE32PlusHeader);
822 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
823 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000824 if (Index >= NumEnt) {
825 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000826 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000827 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000828 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000829 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000830}
831
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000832std::error_code COFFObjectFile::getSection(int32_t Index,
833 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000834 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000835 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000836 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000837 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000838 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000839 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000840 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000841 }
842 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000843}
844
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000845std::error_code COFFObjectFile::getString(uint32_t Offset,
846 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000847 if (StringTableSize <= 4)
848 // Tried to get a string from an empty string table.
849 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000850 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000851 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000852 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000853 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000854}
855
David Majnemer44f51e52014-09-10 12:51:52 +0000856std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000857 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000858 return getSymbolName(Symbol.getGeneric(), Res);
859}
860
861std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
862 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000863 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000864 if (Symbol->Name.Offset.Zeroes == 0) {
865 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000866 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000867 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000868 }
869
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000870 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000871 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000872 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000873 else
874 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000875 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000876 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000877}
878
David Majnemer44f51e52014-09-10 12:51:52 +0000879ArrayRef<uint8_t>
880COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000881 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000882
David Majnemer44f51e52014-09-10 12:51:52 +0000883 size_t SymbolSize = getSymbolTableEntrySize();
884 if (Symbol.getNumberOfAuxSymbols() > 0) {
885 // AUX data comes immediately after the symbol in COFF
886 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000887# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000888 // Verify that the Aux symbol points to a valid entry in the symbol table.
889 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000890 if (Offset < getPointerToSymbolTable() ||
891 Offset >=
892 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000893 report_fatal_error("Aux Symbol data was outside of symbol table.");
894
David Majnemer44f51e52014-09-10 12:51:52 +0000895 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
896 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000897# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000898 }
David Majnemer44f51e52014-09-10 12:51:52 +0000899 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000900}
901
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000902std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
903 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000904 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000905 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000906 // Null terminated, let ::strlen figure out the length.
907 Name = Sec->Name;
908 else
909 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000910 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000911
912 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000913 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000914 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000915 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000916 if (decodeBase64StringEntry(Name.substr(2), Offset))
917 return object_error::parse_failed;
918 } else {
919 if (Name.substr(1).getAsInteger(10, Offset))
920 return object_error::parse_failed;
921 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000922 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000923 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000924 }
925
926 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000927 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000928}
929
David Majnemera9ee5c02014-10-09 08:42:31 +0000930uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
931 // SizeOfRawData and VirtualSize change what they represent depending on
932 // whether or not we have an executable image.
933 //
934 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000935 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +0000936 //
937 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
938 // actual section size is in VirtualSize. It is possible for VirtualSize to
939 // be greater than SizeOfRawData; the contents past that point should be
940 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000941 if (getDOSHeader())
942 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
943 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000944}
945
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000946std::error_code
947COFFObjectFile::getSectionContents(const coff_section *Sec,
948 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000949 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
950 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000951 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
952 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000953 // The only thing that we need to verify is that the contents is contained
954 // within the file bounds. We don't need to make sure it doesn't cover other
955 // data, as there's nothing that says that is not allowed.
956 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000957 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000958 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000959 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000960 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000961 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000962}
963
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000964const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000965 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000966}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000967
Rafael Espindola5e812af2014-01-30 02:49:50 +0000968void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000969 Rel.p = reinterpret_cast<uintptr_t>(
970 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000971}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000972
Rafael Espindola96d071c2015-06-29 23:29:12 +0000973uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +0000974 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000975 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000976}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000977
Rafael Espindola806f0062013-06-05 01:33:53 +0000978symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000979 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000980 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000981 if (R->SymbolTableIndex >= getNumberOfSymbols())
982 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000983 if (SymbolTable16)
984 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
985 else if (SymbolTable32)
986 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
987 else
David Majnemerc7353b52014-11-25 07:43:14 +0000988 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000989 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000990}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000991
Rafael Espindola99c041b2015-06-30 01:53:01 +0000992uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000993 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +0000994 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000995}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000996
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000997const coff_section *
998COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
999 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001000}
1001
David Majnemer44f51e52014-09-10 12:51:52 +00001002COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1003 if (SymbolTable16)
1004 return toSymb<coff_symbol16>(Ref);
1005 if (SymbolTable32)
1006 return toSymb<coff_symbol32>(Ref);
1007 llvm_unreachable("no symbol table pointer!");
1008}
1009
1010COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1011 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001012}
1013
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001014const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001015COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1016 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001017}
1018
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001019iterator_range<const coff_relocation *>
1020COFFObjectFile::getRelocations(const coff_section *Sec) const {
1021 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1022 const coff_relocation *E = I;
1023 if (I)
1024 E += getNumberOfRelocations(Sec, Data, base());
1025 return make_range(I, E);
1026}
1027
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001028#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1029 case COFF::reloc_type: \
1030 Res = #reloc_type; \
1031 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001032
Rafael Espindola41bb4322015-06-30 04:08:37 +00001033void COFFObjectFile::getRelocationTypeName(
1034 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001035 const coff_relocation *Reloc = toRel(Rel);
1036 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001037 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001038 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001039 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001040 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1057 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001058 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001059 }
1060 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001061 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1062 switch (Reloc->Type) {
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1078 default:
1079 Res = "Unknown";
1080 }
1081 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001082 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001083 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1095 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001096 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001097 }
1098 break;
1099 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001100 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001101 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001102 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001103}
1104
1105#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1106
Rafael Espindolac66d7612014-08-17 19:09:37 +00001107bool COFFObjectFile::isRelocatableObject() const {
1108 return !DataDirectory;
1109}
1110
Rui Ueyamac2bed422013-09-27 21:04:00 +00001111bool ImportDirectoryEntryRef::
1112operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001113 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001114}
1115
Rafael Espindola5e812af2014-01-30 02:49:50 +00001116void ImportDirectoryEntryRef::moveNext() {
1117 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001118}
1119
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001120std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1121 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001122 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001123 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001124}
1125
Rui Ueyama861021f2014-10-02 22:05:29 +00001126static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001127makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001128 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001129 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001130 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001131 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001132 }
1133 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001134 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001135}
1136
Rui Ueyama15d99352014-10-03 00:41:58 +00001137static imported_symbol_iterator
1138importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001139 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001140 Object->getRvaPtr(RVA, IntPtr);
1141 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001142}
1143
Rui Ueyama15d99352014-10-03 00:41:58 +00001144static imported_symbol_iterator
1145importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001146 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001147 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001148 // Forward the pointer to the last entry which is null.
1149 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001150 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001151 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1152 while (*Entry++)
1153 ++Index;
1154 } else {
1155 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1156 while (*Entry++)
1157 ++Index;
1158 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001159 return makeImportedSymbolIterator(Object, IntPtr, Index);
1160}
1161
1162imported_symbol_iterator
1163ImportDirectoryEntryRef::imported_symbol_begin() const {
1164 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1165 OwningObject);
1166}
1167
1168imported_symbol_iterator
1169ImportDirectoryEntryRef::imported_symbol_end() const {
1170 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1171 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001172}
1173
Rui Ueyama979fb402014-10-09 02:16:38 +00001174iterator_range<imported_symbol_iterator>
1175ImportDirectoryEntryRef::imported_symbols() const {
1176 return make_range(imported_symbol_begin(), imported_symbol_end());
1177}
1178
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001179std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001180 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001181 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001182 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001183 return EC;
1184 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001185 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001186}
1187
Rui Ueyama1e152d52014-10-02 17:02:18 +00001188std::error_code
1189ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1190 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001191 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001192}
1193
1194std::error_code
1195ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1196 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001197 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001198}
1199
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001200std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001201 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001202 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001203 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1204 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001205 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001206 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001207 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001208}
1209
Rui Ueyama15d99352014-10-03 00:41:58 +00001210bool DelayImportDirectoryEntryRef::
1211operator==(const DelayImportDirectoryEntryRef &Other) const {
1212 return Table == Other.Table && Index == Other.Index;
1213}
1214
1215void DelayImportDirectoryEntryRef::moveNext() {
1216 ++Index;
1217}
1218
1219imported_symbol_iterator
1220DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1221 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1222 OwningObject);
1223}
1224
1225imported_symbol_iterator
1226DelayImportDirectoryEntryRef::imported_symbol_end() const {
1227 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1228 OwningObject);
1229}
1230
Rui Ueyama979fb402014-10-09 02:16:38 +00001231iterator_range<imported_symbol_iterator>
1232DelayImportDirectoryEntryRef::imported_symbols() const {
1233 return make_range(imported_symbol_begin(), imported_symbol_end());
1234}
1235
Rui Ueyama15d99352014-10-03 00:41:58 +00001236std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1237 uintptr_t IntPtr = 0;
1238 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1239 return EC;
1240 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001241 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001242}
1243
Rui Ueyama1af08652014-10-03 18:07:18 +00001244std::error_code DelayImportDirectoryEntryRef::
1245getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1246 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001247 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001248}
1249
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001250std::error_code DelayImportDirectoryEntryRef::
1251getImportAddress(int AddrIndex, uint64_t &Result) const {
1252 uint32_t RVA = Table[Index].DelayImportAddressTable +
1253 AddrIndex * (OwningObject->is64() ? 8 : 4);
1254 uintptr_t IntPtr = 0;
1255 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1256 return EC;
1257 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001258 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001259 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001260 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001261 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001262}
1263
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001264bool ExportDirectoryEntryRef::
1265operator==(const ExportDirectoryEntryRef &Other) const {
1266 return ExportTable == Other.ExportTable && Index == Other.Index;
1267}
1268
Rafael Espindola5e812af2014-01-30 02:49:50 +00001269void ExportDirectoryEntryRef::moveNext() {
1270 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001271}
1272
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001273// Returns the name of the current export symbol. If the symbol is exported only
1274// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001275std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001276 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001277 if (std::error_code EC =
1278 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001279 return EC;
1280 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001281 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001282}
1283
Rui Ueyamae5df6092014-01-17 22:02:24 +00001284// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001285std::error_code
1286ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001287 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001288 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001289}
1290
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001291// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001292std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001293 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001294 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001295}
1296
1297// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001298std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001299 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001300 if (std::error_code EC =
1301 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001302 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001303 const export_address_table_entry *entry =
1304 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001305 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001306 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001307}
1308
1309// Returns the name of the current export symbol. If the symbol is exported only
1310// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001311std::error_code
1312ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001313 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001314 if (std::error_code EC =
1315 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001316 return EC;
1317 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1318
1319 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1320 int Offset = 0;
1321 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1322 I < E; ++I, ++Offset) {
1323 if (*I != Index)
1324 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001325 if (std::error_code EC =
1326 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001327 return EC;
1328 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001329 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001330 return EC;
1331 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001332 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001333 }
1334 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001335 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001336}
1337
Rui Ueyama6161b382016-01-12 23:28:42 +00001338std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1339 const data_directory *DataEntry;
1340 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1341 return EC;
1342 uint32_t RVA;
1343 if (auto EC = getExportRVA(RVA))
1344 return EC;
1345 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1346 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1347 Result = (Begin <= RVA && RVA < End);
1348 return std::error_code();
1349}
1350
1351std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1352 uint32_t RVA;
1353 if (auto EC = getExportRVA(RVA))
1354 return EC;
1355 uintptr_t IntPtr = 0;
1356 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1357 return EC;
1358 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1359 return std::error_code();
1360}
1361
Rui Ueyama861021f2014-10-02 22:05:29 +00001362bool ImportedSymbolRef::
1363operator==(const ImportedSymbolRef &Other) const {
1364 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1365 && Index == Other.Index;
1366}
1367
1368void ImportedSymbolRef::moveNext() {
1369 ++Index;
1370}
1371
1372std::error_code
1373ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1374 uint32_t RVA;
1375 if (Entry32) {
1376 // If a symbol is imported only by ordinal, it has no name.
1377 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001378 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001379 RVA = Entry32[Index].getHintNameRVA();
1380 } else {
1381 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001382 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001383 RVA = Entry64[Index].getHintNameRVA();
1384 }
1385 uintptr_t IntPtr = 0;
1386 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1387 return EC;
1388 // +2 because the first two bytes is hint.
1389 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001390 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001391}
1392
1393std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1394 uint32_t RVA;
1395 if (Entry32) {
1396 if (Entry32[Index].isOrdinal()) {
1397 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001398 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001399 }
1400 RVA = Entry32[Index].getHintNameRVA();
1401 } else {
1402 if (Entry64[Index].isOrdinal()) {
1403 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001404 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001405 }
1406 RVA = Entry64[Index].getHintNameRVA();
1407 }
1408 uintptr_t IntPtr = 0;
1409 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1410 return EC;
1411 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001412 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001413}
1414
Rafael Espindola437b0d52014-07-31 03:12:45 +00001415ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001416ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001417 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001418 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001419 if (EC)
1420 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001421 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001422}
Rui Ueyama74e85132014-11-19 00:18:07 +00001423
1424bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1425 return Header == Other.Header && Index == Other.Index;
1426}
1427
1428void BaseRelocRef::moveNext() {
1429 // Header->BlockSize is the size of the current block, including the
1430 // size of the header itself.
1431 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001432 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001433 if (Size == Header->BlockSize) {
1434 // .reloc contains a list of base relocation blocks. Each block
1435 // consists of the header followed by entries. The header contains
1436 // how many entories will follow. When we reach the end of the
1437 // current block, proceed to the next block.
1438 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1439 reinterpret_cast<const uint8_t *>(Header) + Size);
1440 Index = 0;
1441 } else {
1442 ++Index;
1443 }
1444}
1445
1446std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1447 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1448 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001449 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001450}
1451
1452std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1453 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1454 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001455 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001456}