blob: d3f604a8d359154ab260291a73abaa9ec988e898 [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 Ueyama6a75acb2015-06-25 00:07:39 +000019#include "llvm/ADT/iterator_range.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000020#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000023#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000024#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000025
26using namespace llvm;
27using namespace object;
28
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000029using support::ulittle16_t;
30using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000031using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000033
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000034// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000035static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000036 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000037 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000038 return false;
39 }
40 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000041}
42
David Majnemere830c602014-11-13 08:46:37 +000043static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
David Majnemer94751be2014-11-13 09:50:18 +000044 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000045 if (Addr + Size < Addr || Addr + Size < Size ||
46 Addr + Size > uintptr_t(M.getBufferEnd()) ||
47 Addr < uintptr_t(M.getBufferStart())) {
48 return object_error::unexpected_eof;
49 }
Rui Ueyama7d099192015-06-09 15:20:42 +000050 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000051}
52
Rui Ueyamaed64342b2013-07-19 23:23:29 +000053// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
54// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000055template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000056static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000057 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000058 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000059 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000060 if (std::error_code EC = checkOffset(M, Addr, Size))
61 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000062 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyama7d099192015-06-09 15:20:42 +000063 return std::error_code();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000065
Nico Rieck9d2c15e2014-02-22 16:12:20 +000066// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
67// prefixed slashes.
68static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
69 assert(Str.size() <= 6 && "String too long, possible overflow.");
70 if (Str.size() > 6)
71 return true;
72
73 uint64_t Value = 0;
74 while (!Str.empty()) {
75 unsigned CharVal;
76 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
77 CharVal = Str[0] - 'A';
78 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
79 CharVal = Str[0] - 'a' + 26;
80 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
81 CharVal = Str[0] - '0' + 52;
82 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000083 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000084 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000085 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000086 else
87 return true;
88
89 Value = (Value * 64) + CharVal;
90 Str = Str.substr(1);
91 }
92
93 if (Value > std::numeric_limits<uint32_t>::max())
94 return true;
95
96 Result = static_cast<uint32_t>(Value);
97 return false;
98}
99
David Majnemer44f51e52014-09-10 12:51:52 +0000100template <typename coff_symbol_type>
101const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
102 const coff_symbol_type *Addr =
103 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000104
David Majnemer236b0ca2014-11-17 11:17:17 +0000105 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000106#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000107 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000108 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000109
David Majnemer44f51e52014-09-10 12:51:52 +0000110 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
111 "Symbol did not point to the beginning of a symbol");
112#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000114 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000115}
116
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000117const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
118 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000119
120# ifndef NDEBUG
121 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000122 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000123 report_fatal_error("Section was outside of section table.");
124
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000125 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
126 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000127 "Section did not point to the beginning of a section");
128# endif
129
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000130 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000131}
132
Rafael Espindola5e812af2014-01-30 02:49:50 +0000133void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000134 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000135 if (SymbolTable16) {
136 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
137 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000138 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000139 } else if (SymbolTable32) {
140 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
141 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000142 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000143 } else {
144 llvm_unreachable("no symbol table pointer!");
145 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000146}
147
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000148ErrorOr<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000150 StringRef Result;
151 std::error_code EC = getSymbolName(Symb, Result);
152 if (EC)
153 return EC;
154 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000155}
156
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000157uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
158 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000159}
160
Rafael Espindolaed067c42015-07-03 18:19:00 +0000161ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
162 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000163 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000164 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000165
166 if (Symb.isAnyUndefined() || Symb.isCommon() ||
167 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000168 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000169
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000170 const coff_section *Section = nullptr;
171 if (std::error_code EC = getSection(SectionNumber, Section))
172 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000173 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000174
175 // The section VirtualAddress does not include ImageBase, and we want to
176 // return virtual addresses.
177 if (PE32Header)
178 Result += PE32Header->ImageBase;
179 else if (PE32PlusHeader)
David Majnemer7c6a0712015-07-31 17:40:24 +0000180 Result += PE32PlusHeader->ImageBase;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000181
Rafael Espindolaed067c42015-07-03 18:19:00 +0000182 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000183}
184
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000185SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000186 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000187 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000188
Peter Collingbournee834f422015-08-06 05:26:35 +0000189 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
190 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000191 if (Symb.isAnyUndefined())
192 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000193 if (Symb.isCommon())
194 return SymbolRef::ST_Data;
195 if (Symb.isFileRecord())
196 return SymbolRef::ST_File;
197
198 // TODO: perhaps we need a new symbol type ST_Section.
199 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
200 return SymbolRef::ST_Debug;
201
202 if (!COFF::isReservedSectionNumber(SectionNumber))
203 return SymbolRef::ST_Data;
204
205 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000206}
207
Rafael Espindola20122a42014-01-31 20:57:12 +0000208uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000209 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000210 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000211
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000212 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000213 Result |= SymbolRef::SF_Global;
214
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000215 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000216 Result |= SymbolRef::SF_Weak;
217
David Majnemer44f51e52014-09-10 12:51:52 +0000218 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000219 Result |= SymbolRef::SF_Absolute;
220
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000221 if (Symb.isFileRecord())
222 Result |= SymbolRef::SF_FormatSpecific;
223
224 if (Symb.isSectionDefinition())
225 Result |= SymbolRef::SF_FormatSpecific;
226
227 if (Symb.isCommon())
228 Result |= SymbolRef::SF_Common;
229
230 if (Symb.isAnyUndefined())
231 Result |= SymbolRef::SF_Undefined;
232
Rafael Espindola20122a42014-01-31 20:57:12 +0000233 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000234}
235
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000236uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000237 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000238 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000239}
240
Rafael Espindola8bab8892015-08-07 23:27:14 +0000241ErrorOr<section_iterator>
242COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000243 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000244 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
245 return section_end();
246 const coff_section *Sec = nullptr;
247 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
248 return EC;
249 DataRefImpl Ret;
250 Ret.p = reinterpret_cast<uintptr_t>(Sec);
251 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000252}
253
Rafael Espindola6bf32212015-06-24 19:57:32 +0000254unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
255 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
256 return Symb.getSectionNumber();
257}
258
Rafael Espindola5e812af2014-01-30 02:49:50 +0000259void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000260 const coff_section *Sec = toSec(Ref);
261 Sec += 1;
262 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000263}
264
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000265std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
266 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000267 const coff_section *Sec = toSec(Ref);
268 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000269}
270
Rafael Espindola80291272014-10-08 15:28:58 +0000271uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000272 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000273 uint64_t Result = Sec->VirtualAddress;
274
275 // The section VirtualAddress does not include ImageBase, and we want to
276 // return virtual addresses.
277 if (PE32Header)
278 Result += PE32Header->ImageBase;
279 else if (PE32PlusHeader)
280 Result += PE32PlusHeader->ImageBase;
281 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000282}
283
Rafael Espindola80291272014-10-08 15:28:58 +0000284uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000285 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000286}
287
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000288std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
289 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000290 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000291 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000292 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000293 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
294 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000295}
296
Rafael Espindola80291272014-10-08 15:28:58 +0000297uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000298 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000299 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000300}
301
Rafael Espindola80291272014-10-08 15:28:58 +0000302bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000303 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000304 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000305}
306
Rafael Espindola80291272014-10-08 15:28:58 +0000307bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000308 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000309 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000310}
311
Rafael Espindola80291272014-10-08 15:28:58 +0000312bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000313 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000314 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
315 COFF::IMAGE_SCN_MEM_READ |
316 COFF::IMAGE_SCN_MEM_WRITE;
317 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000318}
319
Rafael Espindola6bf32212015-06-24 19:57:32 +0000320unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
321 uintptr_t Offset =
322 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
323 assert((Offset % sizeof(coff_section)) == 0);
324 return (Offset / sizeof(coff_section)) + 1;
325}
326
Rafael Espindola80291272014-10-08 15:28:58 +0000327bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000328 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000329 // In COFF, a virtual section won't have any in-file
330 // content, so the file pointer to the content will be zero.
331 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000332}
333
David Majnemere830c602014-11-13 08:46:37 +0000334static uint32_t getNumberOfRelocations(const coff_section *Sec,
335 MemoryBufferRef M, const uint8_t *base) {
336 // The field for the number of relocations in COFF section table is only
337 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
338 // NumberOfRelocations field, and the actual relocation count is stored in the
339 // VirtualAddress field in the first relocation entry.
340 if (Sec->hasExtendedRelocations()) {
341 const coff_relocation *FirstReloc;
342 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
343 base + Sec->PointerToRelocations)))
344 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000345 // -1 to exclude this first relocation entry.
346 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000347 }
348 return Sec->NumberOfRelocations;
349}
350
David Majnemer94751be2014-11-13 09:50:18 +0000351static const coff_relocation *
352getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
353 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
354 if (!NumRelocs)
355 return nullptr;
356 auto begin = reinterpret_cast<const coff_relocation *>(
357 Base + Sec->PointerToRelocations);
358 if (Sec->hasExtendedRelocations()) {
359 // Skip the first relocation entry repurposed to store the number of
360 // relocations.
361 begin++;
362 }
363 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
364 return nullptr;
365 return begin;
366}
367
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000368relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
369 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000370 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000371 if (begin && Sec->VirtualAddress != 0)
372 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000373 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000374 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000376}
377
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000378relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
379 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000380 const coff_relocation *I = getFirstReloc(Sec, Data, base());
381 if (I)
382 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000383 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000384 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000385 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000386}
387
Rui Ueyamac2bed422013-09-27 21:04:00 +0000388// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000389std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000390 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000391 if (std::error_code EC = getObject(
392 SymbolTable16, Data, base() + getPointerToSymbolTable(),
393 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000394 return EC;
395
396 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000397 if (std::error_code EC = getObject(
398 SymbolTable32, Data, base() + getPointerToSymbolTable(),
399 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000400 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000401
402 // Find string table. The first four byte of the string table contains the
403 // total size of the string table, including the size field itself. If the
404 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000405 uint32_t StringTableOffset = getPointerToSymbolTable() +
406 getNumberOfSymbols() * getSymbolTableEntrySize();
407 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000409 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000410 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000411 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000412 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000413 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000414 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000415
Nico Rieck773a5792014-02-26 19:51:44 +0000416 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
417 // tools like cvtres write a size of 0 for an empty table instead of 4.
418 if (StringTableSize < 4)
419 StringTableSize = 4;
420
Rui Ueyamac2bed422013-09-27 21:04:00 +0000421 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000422 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000423 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000424 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000425}
426
Rui Ueyama215a5862014-02-20 06:51:07 +0000427// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000428std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000429 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
430 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000431 uint64_t Rva = Addr - ImageBase;
432 assert(Rva <= UINT32_MAX);
433 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000434}
435
Rui Ueyamac2bed422013-09-27 21:04:00 +0000436// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000437std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000438 for (const SectionRef &S : sections()) {
439 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000440 uint32_t SectionStart = Section->VirtualAddress;
441 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000442 if (SectionStart <= Addr && Addr < SectionEnd) {
443 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000444 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000445 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000446 }
447 }
448 return object_error::parse_failed;
449}
450
451// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
452// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000453std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
454 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000455 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000456 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000457 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
459 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
460 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000461 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462}
463
464// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000465std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466 // First, we get the RVA of the import table. If the file lacks a pointer to
467 // the import table, do nothing.
468 const data_directory *DataEntry;
469 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000470 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000471
472 // Do nothing if the pointer to import table is NULL.
473 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000474 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000475
476 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000477 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000478 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000479 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000480
481 // Find the section that contains the RVA. This is needed because the RVA is
482 // the import table's memory address which is different from its file offset.
483 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000484 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000485 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000486 ImportDirectory = reinterpret_cast<
487 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000488 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000489}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000490
Rui Ueyama15d99352014-10-03 00:41:58 +0000491// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
492std::error_code COFFObjectFile::initDelayImportTablePtr() {
493 const data_directory *DataEntry;
494 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000495 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000496 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000497 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000498
499 uint32_t RVA = DataEntry->RelativeVirtualAddress;
500 NumberOfDelayImportDirectory = DataEntry->Size /
501 sizeof(delay_import_directory_table_entry) - 1;
502
503 uintptr_t IntPtr = 0;
504 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
505 return EC;
506 DelayImportDirectory = reinterpret_cast<
507 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000508 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000509}
510
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000511// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000512std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000513 // First, we get the RVA of the export table. If the file lacks a pointer to
514 // the export table, do nothing.
515 const data_directory *DataEntry;
516 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000517 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000518
519 // Do nothing if the pointer to export table is NULL.
520 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000521 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000522
523 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
524 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000525 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000526 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000527 ExportDirectory =
528 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000529 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000530}
531
Rui Ueyama74e85132014-11-19 00:18:07 +0000532std::error_code COFFObjectFile::initBaseRelocPtr() {
533 const data_directory *DataEntry;
534 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000535 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000536 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000537 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000538
539 uintptr_t IntPtr = 0;
540 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
541 return EC;
542 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
543 IntPtr);
544 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
545 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000546 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000547}
548
Rafael Espindola48af1c22014-08-19 18:44:46 +0000549COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
550 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000551 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
552 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
553 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
554 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000555 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000556 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
557 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000558 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000559 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000560 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000561
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000562 // The current location in the file where we are looking at.
563 uint64_t CurPtr = 0;
564
565 // PE header is optional and is present only in executables. If it exists,
566 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000567 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000568
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000569 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000570 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000571 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
572 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000573 const auto *DH = reinterpret_cast<const dos_header *>(base());
574 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
575 CurPtr = DH->AddressOfNewExeHeader;
576 // Check the PE magic bytes. ("PE\0\0")
577 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
578 EC = object_error::parse_failed;
579 return;
580 }
581 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
582 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000583 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000584 }
585
Rafael Espindola48af1c22014-08-19 18:44:46 +0000586 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000587 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000588
589 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
590 // import libraries share a common prefix but bigobj is more restrictive.
591 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
592 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
593 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
594 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
595 return;
596
597 // Verify that we are dealing with bigobj.
598 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
599 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
600 sizeof(COFF::BigObjMagic)) == 0) {
601 COFFHeader = nullptr;
602 CurPtr += sizeof(coff_bigobj_file_header);
603 } else {
604 // It's not a bigobj.
605 COFFBigObjHeader = nullptr;
606 }
607 }
608 if (COFFHeader) {
609 // The prior checkSize call may have failed. This isn't a hard error
610 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000611 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000612 CurPtr += sizeof(coff_file_header);
613
614 if (COFFHeader->isImportLibrary())
615 return;
616 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000617
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000618 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000619 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000620 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000621 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000622
623 const uint8_t *DataDirAddr;
624 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000625 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000626 PE32Header = Header;
627 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
628 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000629 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000630 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
631 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
632 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
633 } else {
634 // It's neither PE32 nor PE32+.
635 EC = object_error::parse_failed;
636 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000637 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000638 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000639 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000640 CurPtr += COFFHeader->SizeOfOptionalHeader;
641 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000642
Rafael Espindola48af1c22014-08-19 18:44:46 +0000643 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000644 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000645 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000646
Rui Ueyamac2bed422013-09-27 21:04:00 +0000647 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000648 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000649 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000650 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000651 } else {
652 // We had better not have any symbols if we don't have a symbol table.
653 if (getNumberOfSymbols() != 0) {
654 EC = object_error::parse_failed;
655 return;
656 }
657 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000658
Rui Ueyamac2bed422013-09-27 21:04:00 +0000659 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000660 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000661 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000662 if ((EC = initDelayImportTablePtr()))
663 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000664
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000665 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000666 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000667 return;
668
Rui Ueyama74e85132014-11-19 00:18:07 +0000669 // Initialize the pointer to the base relocation table.
670 if ((EC = initBaseRelocPtr()))
671 return;
672
Rui Ueyama7d099192015-06-09 15:20:42 +0000673 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000674}
675
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000676basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000677 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000678 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000679 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000680}
681
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000682basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000683 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000684 DataRefImpl Ret;
685 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000686 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000687}
688
Rui Ueyamabc654b12013-09-27 21:47:05 +0000689import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000690 return import_directory_iterator(
691 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000692}
693
Rui Ueyamabc654b12013-09-27 21:47:05 +0000694import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000695 return import_directory_iterator(
696 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000697}
David Meyerc429b802012-03-01 22:19:54 +0000698
Rui Ueyama15d99352014-10-03 00:41:58 +0000699delay_import_directory_iterator
700COFFObjectFile::delay_import_directory_begin() const {
701 return delay_import_directory_iterator(
702 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
703}
704
705delay_import_directory_iterator
706COFFObjectFile::delay_import_directory_end() const {
707 return delay_import_directory_iterator(
708 DelayImportDirectoryEntryRef(
709 DelayImportDirectory, NumberOfDelayImportDirectory, this));
710}
711
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000712export_directory_iterator COFFObjectFile::export_directory_begin() const {
713 return export_directory_iterator(
714 ExportDirectoryEntryRef(ExportDirectory, 0, this));
715}
716
717export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000718 if (!ExportDirectory)
719 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000720 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000721 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000722 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000723}
724
Rafael Espindolab5155a52014-02-10 20:24:04 +0000725section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000726 DataRefImpl Ret;
727 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
728 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000729}
730
Rafael Espindolab5155a52014-02-10 20:24:04 +0000731section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000732 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000733 int NumSections =
734 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000735 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
736 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000737}
738
Rui Ueyama74e85132014-11-19 00:18:07 +0000739base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
740 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
741}
742
743base_reloc_iterator COFFObjectFile::base_reloc_end() const {
744 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
745}
746
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000747uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000748 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000749}
750
751StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000752 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000753 case COFF::IMAGE_FILE_MACHINE_I386:
754 return "COFF-i386";
755 case COFF::IMAGE_FILE_MACHINE_AMD64:
756 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000757 case COFF::IMAGE_FILE_MACHINE_ARMNT:
758 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000759 case COFF::IMAGE_FILE_MACHINE_ARM64:
760 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000761 default:
762 return "COFF-<unknown arch>";
763 }
764}
765
766unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000767 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000768 case COFF::IMAGE_FILE_MACHINE_I386:
769 return Triple::x86;
770 case COFF::IMAGE_FILE_MACHINE_AMD64:
771 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000772 case COFF::IMAGE_FILE_MACHINE_ARMNT:
773 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000774 case COFF::IMAGE_FILE_MACHINE_ARM64:
775 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000776 default:
777 return Triple::UnknownArch;
778 }
779}
780
Rui Ueyama979fb402014-10-09 02:16:38 +0000781iterator_range<import_directory_iterator>
782COFFObjectFile::import_directories() const {
783 return make_range(import_directory_begin(), import_directory_end());
784}
785
786iterator_range<delay_import_directory_iterator>
787COFFObjectFile::delay_import_directories() const {
788 return make_range(delay_import_directory_begin(),
789 delay_import_directory_end());
790}
791
792iterator_range<export_directory_iterator>
793COFFObjectFile::export_directories() const {
794 return make_range(export_directory_begin(), export_directory_end());
795}
796
Rui Ueyama74e85132014-11-19 00:18:07 +0000797iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
798 return make_range(base_reloc_begin(), base_reloc_end());
799}
800
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000801std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000802 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000803 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000804}
805
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000806std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000807COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
808 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000809 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000810}
811
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000812std::error_code
813COFFObjectFile::getDataDirectory(uint32_t Index,
814 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000815 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000816 if (!DataDirectory) {
817 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000818 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000819 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000820 assert(PE32Header || PE32PlusHeader);
821 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
822 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000823 if (Index >= NumEnt) {
824 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000825 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000826 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000827 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000828 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000829}
830
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000831std::error_code COFFObjectFile::getSection(int32_t Index,
832 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000833 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000834 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000835 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000836 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000837 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000838 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000839 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000840 }
841 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000842}
843
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000844std::error_code COFFObjectFile::getString(uint32_t Offset,
845 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000846 if (StringTableSize <= 4)
847 // Tried to get a string from an empty string table.
848 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000849 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000850 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000851 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000852 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000853}
854
David Majnemer44f51e52014-09-10 12:51:52 +0000855std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000856 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000857 return getSymbolName(Symbol.getGeneric(), Res);
858}
859
860std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
861 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000862 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000863 if (Symbol->Name.Offset.Zeroes == 0) {
864 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000865 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000866 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000867 }
868
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000869 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000870 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000871 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000872 else
873 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000874 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000875 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000876}
877
David Majnemer44f51e52014-09-10 12:51:52 +0000878ArrayRef<uint8_t>
879COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000880 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000881
David Majnemer44f51e52014-09-10 12:51:52 +0000882 size_t SymbolSize = getSymbolTableEntrySize();
883 if (Symbol.getNumberOfAuxSymbols() > 0) {
884 // AUX data comes immediately after the symbol in COFF
885 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000886# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000887 // Verify that the Aux symbol points to a valid entry in the symbol table.
888 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000889 if (Offset < getPointerToSymbolTable() ||
890 Offset >=
891 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000892 report_fatal_error("Aux Symbol data was outside of symbol table.");
893
David Majnemer44f51e52014-09-10 12:51:52 +0000894 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
895 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000896# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000897 }
David Majnemer44f51e52014-09-10 12:51:52 +0000898 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000899}
900
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000901std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
902 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000903 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000904 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000905 // Null terminated, let ::strlen figure out the length.
906 Name = Sec->Name;
907 else
908 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000909 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000910
911 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000912 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000913 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000914 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000915 if (decodeBase64StringEntry(Name.substr(2), Offset))
916 return object_error::parse_failed;
917 } else {
918 if (Name.substr(1).getAsInteger(10, Offset))
919 return object_error::parse_failed;
920 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000921 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000922 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000923 }
924
925 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000926 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000927}
928
David Majnemera9ee5c02014-10-09 08:42:31 +0000929uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
930 // SizeOfRawData and VirtualSize change what they represent depending on
931 // whether or not we have an executable image.
932 //
933 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000934 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +0000935 //
936 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
937 // actual section size is in VirtualSize. It is possible for VirtualSize to
938 // be greater than SizeOfRawData; the contents past that point should be
939 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000940 if (getDOSHeader())
941 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
942 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000943}
944
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000945std::error_code
946COFFObjectFile::getSectionContents(const coff_section *Sec,
947 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000948 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
949 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000950 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
951 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000952 // The only thing that we need to verify is that the contents is contained
953 // within the file bounds. We don't need to make sure it doesn't cover other
954 // data, as there's nothing that says that is not allowed.
955 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000956 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000957 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000958 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000959 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000960 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000961}
962
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000963const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000964 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000965}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000966
Rafael Espindola5e812af2014-01-30 02:49:50 +0000967void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000968 Rel.p = reinterpret_cast<uintptr_t>(
969 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000970}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000971
Rafael Espindola96d071c2015-06-29 23:29:12 +0000972uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +0000973 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000974 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000975}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000976
Rafael Espindola806f0062013-06-05 01:33:53 +0000977symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000978 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000979 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000980 if (R->SymbolTableIndex >= getNumberOfSymbols())
981 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000982 if (SymbolTable16)
983 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
984 else if (SymbolTable32)
985 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
986 else
David Majnemerc7353b52014-11-25 07:43:14 +0000987 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000988 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000989}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000990
Rafael Espindola99c041b2015-06-30 01:53:01 +0000991uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000992 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +0000993 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000994}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000995
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000996const coff_section *
997COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
998 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000999}
1000
David Majnemer44f51e52014-09-10 12:51:52 +00001001COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1002 if (SymbolTable16)
1003 return toSymb<coff_symbol16>(Ref);
1004 if (SymbolTable32)
1005 return toSymb<coff_symbol32>(Ref);
1006 llvm_unreachable("no symbol table pointer!");
1007}
1008
1009COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1010 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001011}
1012
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001013const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001014COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1015 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001016}
1017
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001018iterator_range<const coff_relocation *>
1019COFFObjectFile::getRelocations(const coff_section *Sec) const {
1020 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1021 const coff_relocation *E = I;
1022 if (I)
1023 E += getNumberOfRelocations(Sec, Data, base());
1024 return make_range(I, E);
1025}
1026
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001027#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1028 case COFF::reloc_type: \
1029 Res = #reloc_type; \
1030 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001031
Rafael Espindola41bb4322015-06-30 04:08:37 +00001032void COFFObjectFile::getRelocationTypeName(
1033 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001034 const coff_relocation *Reloc = toRel(Rel);
1035 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001036 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001037 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001038 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001039 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1040 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1056 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001057 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001058 }
1059 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001060 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1061 switch (Reloc->Type) {
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1077 default:
1078 Res = "Unknown";
1079 }
1080 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001081 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001082 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1094 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001095 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001096 }
1097 break;
1098 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001099 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001100 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001101 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001102}
1103
1104#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1105
Rafael Espindolac66d7612014-08-17 19:09:37 +00001106bool COFFObjectFile::isRelocatableObject() const {
1107 return !DataDirectory;
1108}
1109
Rui Ueyamac2bed422013-09-27 21:04:00 +00001110bool ImportDirectoryEntryRef::
1111operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001112 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001113}
1114
Rafael Espindola5e812af2014-01-30 02:49:50 +00001115void ImportDirectoryEntryRef::moveNext() {
1116 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001117}
1118
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001119std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1120 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001121 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001122 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001123}
1124
Rui Ueyama861021f2014-10-02 22:05:29 +00001125static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001126makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001127 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001128 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001129 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001130 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001131 }
1132 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001133 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001134}
1135
Rui Ueyama15d99352014-10-03 00:41:58 +00001136static imported_symbol_iterator
1137importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001138 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001139 Object->getRvaPtr(RVA, IntPtr);
1140 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001141}
1142
Rui Ueyama15d99352014-10-03 00:41:58 +00001143static imported_symbol_iterator
1144importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001145 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001146 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001147 // Forward the pointer to the last entry which is null.
1148 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001149 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001150 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1151 while (*Entry++)
1152 ++Index;
1153 } else {
1154 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1155 while (*Entry++)
1156 ++Index;
1157 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001158 return makeImportedSymbolIterator(Object, IntPtr, Index);
1159}
1160
1161imported_symbol_iterator
1162ImportDirectoryEntryRef::imported_symbol_begin() const {
1163 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1164 OwningObject);
1165}
1166
1167imported_symbol_iterator
1168ImportDirectoryEntryRef::imported_symbol_end() const {
1169 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1170 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001171}
1172
Rui Ueyama979fb402014-10-09 02:16:38 +00001173iterator_range<imported_symbol_iterator>
1174ImportDirectoryEntryRef::imported_symbols() const {
1175 return make_range(imported_symbol_begin(), imported_symbol_end());
1176}
1177
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001178std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001179 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001180 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001181 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001182 return EC;
1183 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001184 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001185}
1186
Rui Ueyama1e152d52014-10-02 17:02:18 +00001187std::error_code
1188ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1189 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001190 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001191}
1192
1193std::error_code
1194ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1195 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001196 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001197}
1198
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001199std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001200 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001201 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001202 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1203 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001204 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001205 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001206 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001207}
1208
Rui Ueyama15d99352014-10-03 00:41:58 +00001209bool DelayImportDirectoryEntryRef::
1210operator==(const DelayImportDirectoryEntryRef &Other) const {
1211 return Table == Other.Table && Index == Other.Index;
1212}
1213
1214void DelayImportDirectoryEntryRef::moveNext() {
1215 ++Index;
1216}
1217
1218imported_symbol_iterator
1219DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1220 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1221 OwningObject);
1222}
1223
1224imported_symbol_iterator
1225DelayImportDirectoryEntryRef::imported_symbol_end() const {
1226 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1227 OwningObject);
1228}
1229
Rui Ueyama979fb402014-10-09 02:16:38 +00001230iterator_range<imported_symbol_iterator>
1231DelayImportDirectoryEntryRef::imported_symbols() const {
1232 return make_range(imported_symbol_begin(), imported_symbol_end());
1233}
1234
Rui Ueyama15d99352014-10-03 00:41:58 +00001235std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1236 uintptr_t IntPtr = 0;
1237 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1238 return EC;
1239 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001240 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001241}
1242
Rui Ueyama1af08652014-10-03 18:07:18 +00001243std::error_code DelayImportDirectoryEntryRef::
1244getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1245 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001246 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001247}
1248
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001249std::error_code DelayImportDirectoryEntryRef::
1250getImportAddress(int AddrIndex, uint64_t &Result) const {
1251 uint32_t RVA = Table[Index].DelayImportAddressTable +
1252 AddrIndex * (OwningObject->is64() ? 8 : 4);
1253 uintptr_t IntPtr = 0;
1254 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1255 return EC;
1256 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001257 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001258 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001259 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001260 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001261}
1262
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001263bool ExportDirectoryEntryRef::
1264operator==(const ExportDirectoryEntryRef &Other) const {
1265 return ExportTable == Other.ExportTable && Index == Other.Index;
1266}
1267
Rafael Espindola5e812af2014-01-30 02:49:50 +00001268void ExportDirectoryEntryRef::moveNext() {
1269 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001270}
1271
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001272// Returns the name of the current export symbol. If the symbol is exported only
1273// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001274std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001275 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001276 if (std::error_code EC =
1277 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001278 return EC;
1279 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001280 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001281}
1282
Rui Ueyamae5df6092014-01-17 22:02:24 +00001283// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001284std::error_code
1285ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001286 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001287 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001288}
1289
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001290// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001291std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001292 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001293 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001294}
1295
1296// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001297std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001298 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001299 if (std::error_code EC =
1300 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001301 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001302 const export_address_table_entry *entry =
1303 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001304 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001305 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001306}
1307
1308// Returns the name of the current export symbol. If the symbol is exported only
1309// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001310std::error_code
1311ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001312 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001313 if (std::error_code EC =
1314 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001315 return EC;
1316 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1317
1318 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1319 int Offset = 0;
1320 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1321 I < E; ++I, ++Offset) {
1322 if (*I != Index)
1323 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001324 if (std::error_code EC =
1325 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001326 return EC;
1327 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001328 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001329 return EC;
1330 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001331 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001332 }
1333 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001334 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001335}
1336
Rui Ueyama861021f2014-10-02 22:05:29 +00001337bool ImportedSymbolRef::
1338operator==(const ImportedSymbolRef &Other) const {
1339 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1340 && Index == Other.Index;
1341}
1342
1343void ImportedSymbolRef::moveNext() {
1344 ++Index;
1345}
1346
1347std::error_code
1348ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1349 uint32_t RVA;
1350 if (Entry32) {
1351 // If a symbol is imported only by ordinal, it has no name.
1352 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001353 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001354 RVA = Entry32[Index].getHintNameRVA();
1355 } else {
1356 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001357 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001358 RVA = Entry64[Index].getHintNameRVA();
1359 }
1360 uintptr_t IntPtr = 0;
1361 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1362 return EC;
1363 // +2 because the first two bytes is hint.
1364 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001365 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001366}
1367
1368std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1369 uint32_t RVA;
1370 if (Entry32) {
1371 if (Entry32[Index].isOrdinal()) {
1372 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001373 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001374 }
1375 RVA = Entry32[Index].getHintNameRVA();
1376 } else {
1377 if (Entry64[Index].isOrdinal()) {
1378 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001379 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001380 }
1381 RVA = Entry64[Index].getHintNameRVA();
1382 }
1383 uintptr_t IntPtr = 0;
1384 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1385 return EC;
1386 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001387 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001388}
1389
Rafael Espindola437b0d52014-07-31 03:12:45 +00001390ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001391ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001392 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001393 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001394 if (EC)
1395 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001396 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001397}
Rui Ueyama74e85132014-11-19 00:18:07 +00001398
1399bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1400 return Header == Other.Header && Index == Other.Index;
1401}
1402
1403void BaseRelocRef::moveNext() {
1404 // Header->BlockSize is the size of the current block, including the
1405 // size of the header itself.
1406 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001407 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001408 if (Size == Header->BlockSize) {
1409 // .reloc contains a list of base relocation blocks. Each block
1410 // consists of the header followed by entries. The header contains
1411 // how many entories will follow. When we reach the end of the
1412 // current block, proceed to the next block.
1413 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1414 reinterpret_cast<const uint8_t *>(Header) + Size);
1415 Index = 0;
1416 } else {
1417 ++Index;
1418 }
1419}
1420
1421std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1422 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1423 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001424 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001425}
1426
1427std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1428 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1429 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001430 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001431}