blob: 64bb0d5c636d0163c3b3ed530c92d6d9a2106579 [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 Espindola991af662015-06-24 19:11:10 +0000157uint64_t COFFObjectFile::getSymbolValue(DataRefImpl Ref) const {
158 COFFSymbolRef Sym = getCOFFSymbol(Ref);
159
160 if (Sym.isAnyUndefined() || Sym.isCommon())
161 return UnknownAddress;
162
163 return Sym.getValue();
164}
165
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000166std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
167 uint64_t &Result) const {
Rafael Espindola991af662015-06-24 19:11:10 +0000168 Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000169 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000170 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000171
172 if (Symb.isAnyUndefined() || Symb.isCommon() ||
173 COFF::isReservedSectionNumber(SectionNumber))
Rui Ueyama7d099192015-06-09 15:20:42 +0000174 return std::error_code();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000175
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000176 const coff_section *Section = nullptr;
177 if (std::error_code EC = getSection(SectionNumber, Section))
178 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000179 Result += Section->VirtualAddress;
Rui Ueyama7d099192015-06-09 15:20:42 +0000180 return std::error_code();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000181}
182
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000183SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000184 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000185 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000186
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000187 if (Symb.isAnyUndefined())
188 return SymbolRef::ST_Unknown;
189 if (Symb.isFunctionDefinition())
190 return SymbolRef::ST_Function;
191 if (Symb.isCommon())
192 return SymbolRef::ST_Data;
193 if (Symb.isFileRecord())
194 return SymbolRef::ST_File;
195
196 // TODO: perhaps we need a new symbol type ST_Section.
197 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
198 return SymbolRef::ST_Debug;
199
200 if (!COFF::isReservedSectionNumber(SectionNumber))
201 return SymbolRef::ST_Data;
202
203 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000204}
205
Rafael Espindola20122a42014-01-31 20:57:12 +0000206uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000207 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000208 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000209
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000210 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000211 Result |= SymbolRef::SF_Global;
212
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000213 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000214 Result |= SymbolRef::SF_Weak;
215
David Majnemer44f51e52014-09-10 12:51:52 +0000216 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000217 Result |= SymbolRef::SF_Absolute;
218
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000219 if (Symb.isFileRecord())
220 Result |= SymbolRef::SF_FormatSpecific;
221
222 if (Symb.isSectionDefinition())
223 Result |= SymbolRef::SF_FormatSpecific;
224
225 if (Symb.isCommon())
226 Result |= SymbolRef::SF_Common;
227
228 if (Symb.isAnyUndefined())
229 Result |= SymbolRef::SF_Undefined;
230
Rafael Espindola20122a42014-01-31 20:57:12 +0000231 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000232}
233
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000234uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000235 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000236 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000237}
238
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000239std::error_code
240COFFObjectFile::getSymbolSection(DataRefImpl Ref,
241 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000242 COFFSymbolRef Symb = getCOFFSymbol(Ref);
243 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000244 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000245 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000246 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000247 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000248 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000249 DataRefImpl Ref;
250 Ref.p = reinterpret_cast<uintptr_t>(Sec);
251 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000252 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000253 return std::error_code();
Michael J. Spencer3217315392011-10-17 23:54:46 +0000254}
255
Rafael Espindola6bf32212015-06-24 19:57:32 +0000256unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
257 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
258 return Symb.getSectionNumber();
259}
260
Rafael Espindola5e812af2014-01-30 02:49:50 +0000261void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000262 const coff_section *Sec = toSec(Ref);
263 Sec += 1;
264 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000265}
266
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000267std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
268 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000269 const coff_section *Sec = toSec(Ref);
270 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000271}
272
Rafael Espindola80291272014-10-08 15:28:58 +0000273uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000274 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000275 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000276}
277
Rafael Espindola80291272014-10-08 15:28:58 +0000278uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000279 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000280}
281
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000282std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
283 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000284 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000285 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000286 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000287 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
288 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000289}
290
Rafael Espindola80291272014-10-08 15:28:58 +0000291uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000292 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000293 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000294}
295
Rafael Espindola80291272014-10-08 15:28:58 +0000296bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000297 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000298 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000299}
300
Rafael Espindola80291272014-10-08 15:28:58 +0000301bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000302 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000303 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000304}
305
Rafael Espindola80291272014-10-08 15:28:58 +0000306bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000307 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000308 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
309 COFF::IMAGE_SCN_MEM_READ |
310 COFF::IMAGE_SCN_MEM_WRITE;
311 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000312}
313
Rafael Espindola6bf32212015-06-24 19:57:32 +0000314unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
315 uintptr_t Offset =
316 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
317 assert((Offset % sizeof(coff_section)) == 0);
318 return (Offset / sizeof(coff_section)) + 1;
319}
320
Rafael Espindola80291272014-10-08 15:28:58 +0000321bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000322 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000323 // In COFF, a virtual section won't have any in-file
324 // content, so the file pointer to the content will be zero.
325 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000326}
327
David Majnemere830c602014-11-13 08:46:37 +0000328static uint32_t getNumberOfRelocations(const coff_section *Sec,
329 MemoryBufferRef M, const uint8_t *base) {
330 // The field for the number of relocations in COFF section table is only
331 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
332 // NumberOfRelocations field, and the actual relocation count is stored in the
333 // VirtualAddress field in the first relocation entry.
334 if (Sec->hasExtendedRelocations()) {
335 const coff_relocation *FirstReloc;
336 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
337 base + Sec->PointerToRelocations)))
338 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000339 // -1 to exclude this first relocation entry.
340 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000341 }
342 return Sec->NumberOfRelocations;
343}
344
David Majnemer94751be2014-11-13 09:50:18 +0000345static const coff_relocation *
346getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
347 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
348 if (!NumRelocs)
349 return nullptr;
350 auto begin = reinterpret_cast<const coff_relocation *>(
351 Base + Sec->PointerToRelocations);
352 if (Sec->hasExtendedRelocations()) {
353 // Skip the first relocation entry repurposed to store the number of
354 // relocations.
355 begin++;
356 }
357 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
358 return nullptr;
359 return begin;
360}
361
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000362relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
363 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000364 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000365 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000366 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000367 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000368}
369
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000370relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
371 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000372 const coff_relocation *I = getFirstReloc(Sec, Data, base());
373 if (I)
374 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000376 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000377 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000378}
379
Rui Ueyamac2bed422013-09-27 21:04:00 +0000380// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000381std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000382 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000383 if (std::error_code EC = getObject(
384 SymbolTable16, Data, base() + getPointerToSymbolTable(),
385 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000386 return EC;
387
388 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000389 if (std::error_code EC = getObject(
390 SymbolTable32, Data, base() + getPointerToSymbolTable(),
391 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000392 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000393
394 // Find string table. The first four byte of the string table contains the
395 // total size of the string table, including the size field itself. If the
396 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000397 uint32_t StringTableOffset = getPointerToSymbolTable() +
398 getNumberOfSymbols() * getSymbolTableEntrySize();
399 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000400 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000401 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000402 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000403 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000404 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000405 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000406 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000407
Nico Rieck773a5792014-02-26 19:51:44 +0000408 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
409 // tools like cvtres write a size of 0 for an empty table instead of 4.
410 if (StringTableSize < 4)
411 StringTableSize = 4;
412
Rui Ueyamac2bed422013-09-27 21:04:00 +0000413 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000414 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000415 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000416 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000417}
418
Rui Ueyama215a5862014-02-20 06:51:07 +0000419// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000420std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000421 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
422 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000423 uint64_t Rva = Addr - ImageBase;
424 assert(Rva <= UINT32_MAX);
425 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000426}
427
Rui Ueyamac2bed422013-09-27 21:04:00 +0000428// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000429std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000430 for (const SectionRef &S : sections()) {
431 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000432 uint32_t SectionStart = Section->VirtualAddress;
433 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000434 if (SectionStart <= Addr && Addr < SectionEnd) {
435 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000436 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000437 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000438 }
439 }
440 return object_error::parse_failed;
441}
442
443// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
444// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000445std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
446 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000447 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000448 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000449 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000450 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
451 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
452 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000453 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000454}
455
456// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000457std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458 // First, we get the RVA of the import table. If the file lacks a pointer to
459 // the import table, do nothing.
460 const data_directory *DataEntry;
461 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000462 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000463
464 // Do nothing if the pointer to import table is NULL.
465 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000466 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000467
468 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000469 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000470 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000471 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000472
473 // Find the section that contains the RVA. This is needed because the RVA is
474 // the import table's memory address which is different from its file offset.
475 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000476 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000477 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000478 ImportDirectory = reinterpret_cast<
479 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000480 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000481}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000482
Rui Ueyama15d99352014-10-03 00:41:58 +0000483// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
484std::error_code COFFObjectFile::initDelayImportTablePtr() {
485 const data_directory *DataEntry;
486 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000487 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000488 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000489 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000490
491 uint32_t RVA = DataEntry->RelativeVirtualAddress;
492 NumberOfDelayImportDirectory = DataEntry->Size /
493 sizeof(delay_import_directory_table_entry) - 1;
494
495 uintptr_t IntPtr = 0;
496 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
497 return EC;
498 DelayImportDirectory = reinterpret_cast<
499 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000500 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000501}
502
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000503// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000504std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000505 // First, we get the RVA of the export table. If the file lacks a pointer to
506 // the export table, do nothing.
507 const data_directory *DataEntry;
508 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000509 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000510
511 // Do nothing if the pointer to export table is NULL.
512 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000513 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000514
515 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
516 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000517 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000518 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000519 ExportDirectory =
520 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000521 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000522}
523
Rui Ueyama74e85132014-11-19 00:18:07 +0000524std::error_code COFFObjectFile::initBaseRelocPtr() {
525 const data_directory *DataEntry;
526 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000527 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000528 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000529 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000530
531 uintptr_t IntPtr = 0;
532 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
533 return EC;
534 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
535 IntPtr);
536 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
537 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000538 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000539}
540
Rafael Espindola48af1c22014-08-19 18:44:46 +0000541COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
542 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000543 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
544 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
545 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
546 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000547 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000548 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
549 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000550 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000551 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000552 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000553
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000554 // The current location in the file where we are looking at.
555 uint64_t CurPtr = 0;
556
557 // PE header is optional and is present only in executables. If it exists,
558 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000559 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000560
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000561 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000562 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000563 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
564 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000565 const auto *DH = reinterpret_cast<const dos_header *>(base());
566 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
567 CurPtr = DH->AddressOfNewExeHeader;
568 // Check the PE magic bytes. ("PE\0\0")
569 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
570 EC = object_error::parse_failed;
571 return;
572 }
573 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
574 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000575 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000576 }
577
Rafael Espindola48af1c22014-08-19 18:44:46 +0000578 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000579 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000580
581 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
582 // import libraries share a common prefix but bigobj is more restrictive.
583 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
584 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
585 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
586 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
587 return;
588
589 // Verify that we are dealing with bigobj.
590 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
591 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
592 sizeof(COFF::BigObjMagic)) == 0) {
593 COFFHeader = nullptr;
594 CurPtr += sizeof(coff_bigobj_file_header);
595 } else {
596 // It's not a bigobj.
597 COFFBigObjHeader = nullptr;
598 }
599 }
600 if (COFFHeader) {
601 // The prior checkSize call may have failed. This isn't a hard error
602 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000603 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000604 CurPtr += sizeof(coff_file_header);
605
606 if (COFFHeader->isImportLibrary())
607 return;
608 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000609
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000610 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000611 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000612 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000613 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000614
615 const uint8_t *DataDirAddr;
616 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000617 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000618 PE32Header = Header;
619 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
620 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000621 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000622 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
623 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
624 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
625 } else {
626 // It's neither PE32 nor PE32+.
627 EC = object_error::parse_failed;
628 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000629 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000630 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000631 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000632 CurPtr += COFFHeader->SizeOfOptionalHeader;
633 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000634
Rafael Espindola48af1c22014-08-19 18:44:46 +0000635 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000636 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000637 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000638
Rui Ueyamac2bed422013-09-27 21:04:00 +0000639 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000640 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000641 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000642 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000643 } else {
644 // We had better not have any symbols if we don't have a symbol table.
645 if (getNumberOfSymbols() != 0) {
646 EC = object_error::parse_failed;
647 return;
648 }
649 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000650
Rui Ueyamac2bed422013-09-27 21:04:00 +0000651 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000652 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000653 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000654 if ((EC = initDelayImportTablePtr()))
655 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000656
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000657 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000658 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000659 return;
660
Rui Ueyama74e85132014-11-19 00:18:07 +0000661 // Initialize the pointer to the base relocation table.
662 if ((EC = initBaseRelocPtr()))
663 return;
664
Rui Ueyama7d099192015-06-09 15:20:42 +0000665 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000666}
667
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000668basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000669 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000670 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000671 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000672}
673
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000674basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000675 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000676 DataRefImpl Ret;
677 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000678 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000679}
680
Rui Ueyamabc654b12013-09-27 21:47:05 +0000681import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000682 return import_directory_iterator(
683 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000684}
685
Rui Ueyamabc654b12013-09-27 21:47:05 +0000686import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000687 return import_directory_iterator(
688 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000689}
David Meyerc429b802012-03-01 22:19:54 +0000690
Rui Ueyama15d99352014-10-03 00:41:58 +0000691delay_import_directory_iterator
692COFFObjectFile::delay_import_directory_begin() const {
693 return delay_import_directory_iterator(
694 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
695}
696
697delay_import_directory_iterator
698COFFObjectFile::delay_import_directory_end() const {
699 return delay_import_directory_iterator(
700 DelayImportDirectoryEntryRef(
701 DelayImportDirectory, NumberOfDelayImportDirectory, this));
702}
703
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000704export_directory_iterator COFFObjectFile::export_directory_begin() const {
705 return export_directory_iterator(
706 ExportDirectoryEntryRef(ExportDirectory, 0, this));
707}
708
709export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000710 if (!ExportDirectory)
711 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000712 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000713 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000714 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000715}
716
Rafael Espindolab5155a52014-02-10 20:24:04 +0000717section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000718 DataRefImpl Ret;
719 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
720 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000721}
722
Rafael Espindolab5155a52014-02-10 20:24:04 +0000723section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000724 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000725 int NumSections =
726 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000727 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
728 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000729}
730
Rui Ueyama74e85132014-11-19 00:18:07 +0000731base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
732 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
733}
734
735base_reloc_iterator COFFObjectFile::base_reloc_end() const {
736 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
737}
738
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000739uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000740 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000741}
742
743StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000744 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000745 case COFF::IMAGE_FILE_MACHINE_I386:
746 return "COFF-i386";
747 case COFF::IMAGE_FILE_MACHINE_AMD64:
748 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000749 case COFF::IMAGE_FILE_MACHINE_ARMNT:
750 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000751 default:
752 return "COFF-<unknown arch>";
753 }
754}
755
756unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000757 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000758 case COFF::IMAGE_FILE_MACHINE_I386:
759 return Triple::x86;
760 case COFF::IMAGE_FILE_MACHINE_AMD64:
761 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000762 case COFF::IMAGE_FILE_MACHINE_ARMNT:
763 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000764 default:
765 return Triple::UnknownArch;
766 }
767}
768
Rui Ueyama979fb402014-10-09 02:16:38 +0000769iterator_range<import_directory_iterator>
770COFFObjectFile::import_directories() const {
771 return make_range(import_directory_begin(), import_directory_end());
772}
773
774iterator_range<delay_import_directory_iterator>
775COFFObjectFile::delay_import_directories() const {
776 return make_range(delay_import_directory_begin(),
777 delay_import_directory_end());
778}
779
780iterator_range<export_directory_iterator>
781COFFObjectFile::export_directories() const {
782 return make_range(export_directory_begin(), export_directory_end());
783}
784
Rui Ueyama74e85132014-11-19 00:18:07 +0000785iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
786 return make_range(base_reloc_begin(), base_reloc_end());
787}
788
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000789std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000790 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000791 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000792}
793
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000794std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000795COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
796 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000797 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000798}
799
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000800std::error_code
801COFFObjectFile::getDataDirectory(uint32_t Index,
802 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000803 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000804 if (!DataDirectory) {
805 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000806 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000807 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000808 assert(PE32Header || PE32PlusHeader);
809 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
810 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000811 if (Index >= NumEnt) {
812 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000813 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000814 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000815 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000816 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000817}
818
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000819std::error_code COFFObjectFile::getSection(int32_t Index,
820 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000821 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000822 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000823 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000824 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000825 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000826 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000827 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000828 }
829 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000830}
831
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000832std::error_code COFFObjectFile::getString(uint32_t Offset,
833 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000834 if (StringTableSize <= 4)
835 // Tried to get a string from an empty string table.
836 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000837 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000838 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000839 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000840 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000841}
842
David Majnemer44f51e52014-09-10 12:51:52 +0000843std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000844 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000845 return getSymbolName(Symbol.getGeneric(), Res);
846}
847
848std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
849 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000850 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000851 if (Symbol->Name.Offset.Zeroes == 0) {
852 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000853 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000854 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000855 }
856
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000857 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000858 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000859 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000860 else
861 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000862 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000863 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000864}
865
David Majnemer44f51e52014-09-10 12:51:52 +0000866ArrayRef<uint8_t>
867COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000868 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000869
David Majnemer44f51e52014-09-10 12:51:52 +0000870 size_t SymbolSize = getSymbolTableEntrySize();
871 if (Symbol.getNumberOfAuxSymbols() > 0) {
872 // AUX data comes immediately after the symbol in COFF
873 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000874# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000875 // Verify that the Aux symbol points to a valid entry in the symbol table.
876 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000877 if (Offset < getPointerToSymbolTable() ||
878 Offset >=
879 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000880 report_fatal_error("Aux Symbol data was outside of symbol table.");
881
David Majnemer44f51e52014-09-10 12:51:52 +0000882 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
883 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000884# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000885 }
David Majnemer44f51e52014-09-10 12:51:52 +0000886 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000887}
888
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000889std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
890 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000891 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000892 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000893 // Null terminated, let ::strlen figure out the length.
894 Name = Sec->Name;
895 else
896 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000897 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000898
899 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000900 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000901 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000902 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000903 if (decodeBase64StringEntry(Name.substr(2), Offset))
904 return object_error::parse_failed;
905 } else {
906 if (Name.substr(1).getAsInteger(10, Offset))
907 return object_error::parse_failed;
908 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000909 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000910 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000911 }
912
913 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000914 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000915}
916
David Majnemera9ee5c02014-10-09 08:42:31 +0000917uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
918 // SizeOfRawData and VirtualSize change what they represent depending on
919 // whether or not we have an executable image.
920 //
921 // For object files, SizeOfRawData contains the size of section's data;
922 // VirtualSize is always zero.
923 //
924 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
925 // actual section size is in VirtualSize. It is possible for VirtualSize to
926 // be greater than SizeOfRawData; the contents past that point should be
927 // considered to be zero.
928 uint32_t SectionSize;
929 if (Sec->VirtualSize)
930 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
931 else
932 SectionSize = Sec->SizeOfRawData;
933
934 return SectionSize;
935}
936
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000937std::error_code
938COFFObjectFile::getSectionContents(const coff_section *Sec,
939 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000940 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
941 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000942 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
943 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000944 // The only thing that we need to verify is that the contents is contained
945 // within the file bounds. We don't need to make sure it doesn't cover other
946 // data, as there's nothing that says that is not allowed.
947 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000948 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000949 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000950 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000951 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000952 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000953}
954
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000955const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000956 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000957}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000958
Rafael Espindola5e812af2014-01-30 02:49:50 +0000959void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000960 Rel.p = reinterpret_cast<uintptr_t>(
961 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000962}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000963
Rafael Espindola10fcac72015-06-30 20:32:26 +0000964ErrorOr<uint64_t> COFFObjectFile::getRelocationAddress(DataRefImpl Rel) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000965 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000966}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000967
Rafael Espindola96d071c2015-06-29 23:29:12 +0000968uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +0000969 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000970 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000971}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000972
Rafael Espindola806f0062013-06-05 01:33:53 +0000973symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000974 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000975 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000976 if (R->SymbolTableIndex >= getNumberOfSymbols())
977 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000978 if (SymbolTable16)
979 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
980 else if (SymbolTable32)
981 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
982 else
David Majnemerc7353b52014-11-25 07:43:14 +0000983 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000984 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000985}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000986
Rafael Espindola99c041b2015-06-30 01:53:01 +0000987uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000988 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +0000989 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000990}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000991
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000992const coff_section *
993COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
994 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000995}
996
David Majnemer44f51e52014-09-10 12:51:52 +0000997COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
998 if (SymbolTable16)
999 return toSymb<coff_symbol16>(Ref);
1000 if (SymbolTable32)
1001 return toSymb<coff_symbol32>(Ref);
1002 llvm_unreachable("no symbol table pointer!");
1003}
1004
1005COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1006 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001007}
1008
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001009const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001010COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1011 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001012}
1013
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001014iterator_range<const coff_relocation *>
1015COFFObjectFile::getRelocations(const coff_section *Sec) const {
1016 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1017 const coff_relocation *E = I;
1018 if (I)
1019 E += getNumberOfRelocations(Sec, Data, base());
1020 return make_range(I, E);
1021}
1022
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001023#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1024 case COFF::reloc_type: \
1025 Res = #reloc_type; \
1026 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001027
Rafael Espindola41bb4322015-06-30 04:08:37 +00001028void COFFObjectFile::getRelocationTypeName(
1029 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001030 const coff_relocation *Reloc = toRel(Rel);
1031 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001032 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001033 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001034 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001035 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1036 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1037 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1038 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1039 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1040 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1052 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001053 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001054 }
1055 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001056 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1057 switch (Reloc->Type) {
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1073 default:
1074 Res = "Unknown";
1075 }
1076 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001077 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001078 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1090 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001091 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001092 }
1093 break;
1094 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001095 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001096 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001097 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001098}
1099
1100#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1101
Rafael Espindolac66d7612014-08-17 19:09:37 +00001102bool COFFObjectFile::isRelocatableObject() const {
1103 return !DataDirectory;
1104}
1105
Rui Ueyamac2bed422013-09-27 21:04:00 +00001106bool ImportDirectoryEntryRef::
1107operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001108 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001109}
1110
Rafael Espindola5e812af2014-01-30 02:49:50 +00001111void ImportDirectoryEntryRef::moveNext() {
1112 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001113}
1114
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001115std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1116 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001117 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001118 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001119}
1120
Rui Ueyama861021f2014-10-02 22:05:29 +00001121static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001122makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001123 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001124 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001125 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001126 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001127 }
1128 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001129 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001130}
1131
Rui Ueyama15d99352014-10-03 00:41:58 +00001132static imported_symbol_iterator
1133importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001134 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001135 Object->getRvaPtr(RVA, IntPtr);
1136 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001137}
1138
Rui Ueyama15d99352014-10-03 00:41:58 +00001139static imported_symbol_iterator
1140importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001141 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001142 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001143 // Forward the pointer to the last entry which is null.
1144 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001145 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001146 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1147 while (*Entry++)
1148 ++Index;
1149 } else {
1150 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1151 while (*Entry++)
1152 ++Index;
1153 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001154 return makeImportedSymbolIterator(Object, IntPtr, Index);
1155}
1156
1157imported_symbol_iterator
1158ImportDirectoryEntryRef::imported_symbol_begin() const {
1159 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1160 OwningObject);
1161}
1162
1163imported_symbol_iterator
1164ImportDirectoryEntryRef::imported_symbol_end() const {
1165 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1166 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001167}
1168
Rui Ueyama979fb402014-10-09 02:16:38 +00001169iterator_range<imported_symbol_iterator>
1170ImportDirectoryEntryRef::imported_symbols() const {
1171 return make_range(imported_symbol_begin(), imported_symbol_end());
1172}
1173
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001174std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001175 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001176 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001177 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001178 return EC;
1179 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001180 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001181}
1182
Rui Ueyama1e152d52014-10-02 17:02:18 +00001183std::error_code
1184ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1185 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001186 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001187}
1188
1189std::error_code
1190ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1191 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001192 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001193}
1194
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001195std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001196 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001197 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001198 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1199 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001200 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001201 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001202 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001203}
1204
Rui Ueyama15d99352014-10-03 00:41:58 +00001205bool DelayImportDirectoryEntryRef::
1206operator==(const DelayImportDirectoryEntryRef &Other) const {
1207 return Table == Other.Table && Index == Other.Index;
1208}
1209
1210void DelayImportDirectoryEntryRef::moveNext() {
1211 ++Index;
1212}
1213
1214imported_symbol_iterator
1215DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1216 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1217 OwningObject);
1218}
1219
1220imported_symbol_iterator
1221DelayImportDirectoryEntryRef::imported_symbol_end() const {
1222 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1223 OwningObject);
1224}
1225
Rui Ueyama979fb402014-10-09 02:16:38 +00001226iterator_range<imported_symbol_iterator>
1227DelayImportDirectoryEntryRef::imported_symbols() const {
1228 return make_range(imported_symbol_begin(), imported_symbol_end());
1229}
1230
Rui Ueyama15d99352014-10-03 00:41:58 +00001231std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1232 uintptr_t IntPtr = 0;
1233 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1234 return EC;
1235 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001236 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001237}
1238
Rui Ueyama1af08652014-10-03 18:07:18 +00001239std::error_code DelayImportDirectoryEntryRef::
1240getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1241 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001242 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001243}
1244
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001245std::error_code DelayImportDirectoryEntryRef::
1246getImportAddress(int AddrIndex, uint64_t &Result) const {
1247 uint32_t RVA = Table[Index].DelayImportAddressTable +
1248 AddrIndex * (OwningObject->is64() ? 8 : 4);
1249 uintptr_t IntPtr = 0;
1250 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1251 return EC;
1252 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001253 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001254 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001255 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001256 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001257}
1258
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001259bool ExportDirectoryEntryRef::
1260operator==(const ExportDirectoryEntryRef &Other) const {
1261 return ExportTable == Other.ExportTable && Index == Other.Index;
1262}
1263
Rafael Espindola5e812af2014-01-30 02:49:50 +00001264void ExportDirectoryEntryRef::moveNext() {
1265 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001266}
1267
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001268// Returns the name of the current export symbol. If the symbol is exported only
1269// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001270std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001271 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001272 if (std::error_code EC =
1273 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001274 return EC;
1275 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001276 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001277}
1278
Rui Ueyamae5df6092014-01-17 22:02:24 +00001279// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001280std::error_code
1281ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001282 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001283 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001284}
1285
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001286// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001287std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001288 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001289 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001290}
1291
1292// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001293std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001294 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001295 if (std::error_code EC =
1296 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001297 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001298 const export_address_table_entry *entry =
1299 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001300 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001301 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001302}
1303
1304// Returns the name of the current export symbol. If the symbol is exported only
1305// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001306std::error_code
1307ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001308 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001309 if (std::error_code EC =
1310 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001311 return EC;
1312 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1313
1314 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1315 int Offset = 0;
1316 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1317 I < E; ++I, ++Offset) {
1318 if (*I != Index)
1319 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001320 if (std::error_code EC =
1321 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001322 return EC;
1323 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001324 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001325 return EC;
1326 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001327 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001328 }
1329 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001330 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001331}
1332
Rui Ueyama861021f2014-10-02 22:05:29 +00001333bool ImportedSymbolRef::
1334operator==(const ImportedSymbolRef &Other) const {
1335 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1336 && Index == Other.Index;
1337}
1338
1339void ImportedSymbolRef::moveNext() {
1340 ++Index;
1341}
1342
1343std::error_code
1344ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1345 uint32_t RVA;
1346 if (Entry32) {
1347 // If a symbol is imported only by ordinal, it has no name.
1348 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001349 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001350 RVA = Entry32[Index].getHintNameRVA();
1351 } else {
1352 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001353 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001354 RVA = Entry64[Index].getHintNameRVA();
1355 }
1356 uintptr_t IntPtr = 0;
1357 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1358 return EC;
1359 // +2 because the first two bytes is hint.
1360 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001361 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001362}
1363
1364std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1365 uint32_t RVA;
1366 if (Entry32) {
1367 if (Entry32[Index].isOrdinal()) {
1368 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001369 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001370 }
1371 RVA = Entry32[Index].getHintNameRVA();
1372 } else {
1373 if (Entry64[Index].isOrdinal()) {
1374 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001375 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001376 }
1377 RVA = Entry64[Index].getHintNameRVA();
1378 }
1379 uintptr_t IntPtr = 0;
1380 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1381 return EC;
1382 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001383 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001384}
1385
Rafael Espindola437b0d52014-07-31 03:12:45 +00001386ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001387ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001388 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001389 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001390 if (EC)
1391 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001392 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001393}
Rui Ueyama74e85132014-11-19 00:18:07 +00001394
1395bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1396 return Header == Other.Header && Index == Other.Index;
1397}
1398
1399void BaseRelocRef::moveNext() {
1400 // Header->BlockSize is the size of the current block, including the
1401 // size of the header itself.
1402 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001403 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001404 if (Size == Header->BlockSize) {
1405 // .reloc contains a list of base relocation blocks. Each block
1406 // consists of the header followed by entries. The header contains
1407 // how many entories will follow. When we reach the end of the
1408 // current block, proceed to the next block.
1409 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1410 reinterpret_cast<const uint8_t *>(Header) + Size);
1411 Index = 0;
1412 } else {
1413 ++Index;
1414 }
1415}
1416
1417std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1418 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1419 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001420 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001421}
1422
1423std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1424 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1425 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001426 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001427}