blob: 9c9a6df368982fd8dfe128e81fd74e27ceff5988 [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 Espindolaed067c42015-07-03 18:19:00 +0000166ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
167 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000168 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000169 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000170
171 if (Symb.isAnyUndefined() || Symb.isCommon() ||
172 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000173 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000174
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000175 const coff_section *Section = nullptr;
176 if (std::error_code EC = getSection(SectionNumber, Section))
177 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000178 Result += Section->VirtualAddress;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000179 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000180}
181
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000182SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000183 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000184 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000185
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000186 if (Symb.isAnyUndefined())
187 return SymbolRef::ST_Unknown;
188 if (Symb.isFunctionDefinition())
189 return SymbolRef::ST_Function;
190 if (Symb.isCommon())
191 return SymbolRef::ST_Data;
192 if (Symb.isFileRecord())
193 return SymbolRef::ST_File;
194
195 // TODO: perhaps we need a new symbol type ST_Section.
196 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
197 return SymbolRef::ST_Debug;
198
199 if (!COFF::isReservedSectionNumber(SectionNumber))
200 return SymbolRef::ST_Data;
201
202 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000203}
204
Rafael Espindola20122a42014-01-31 20:57:12 +0000205uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000206 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000207 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000208
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000209 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000210 Result |= SymbolRef::SF_Global;
211
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000212 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000213 Result |= SymbolRef::SF_Weak;
214
David Majnemer44f51e52014-09-10 12:51:52 +0000215 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000216 Result |= SymbolRef::SF_Absolute;
217
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000218 if (Symb.isFileRecord())
219 Result |= SymbolRef::SF_FormatSpecific;
220
221 if (Symb.isSectionDefinition())
222 Result |= SymbolRef::SF_FormatSpecific;
223
224 if (Symb.isCommon())
225 Result |= SymbolRef::SF_Common;
226
227 if (Symb.isAnyUndefined())
228 Result |= SymbolRef::SF_Undefined;
229
Rafael Espindola20122a42014-01-31 20:57:12 +0000230 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000231}
232
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000233uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000234 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000235 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000236}
237
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000238std::error_code
239COFFObjectFile::getSymbolSection(DataRefImpl Ref,
240 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000241 COFFSymbolRef Symb = getCOFFSymbol(Ref);
242 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000243 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000244 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000245 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000246 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000247 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000248 DataRefImpl Ref;
249 Ref.p = reinterpret_cast<uintptr_t>(Sec);
250 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000251 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000252 return std::error_code();
Michael J. Spencer3217315392011-10-17 23:54:46 +0000253}
254
Rafael Espindola6bf32212015-06-24 19:57:32 +0000255unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
256 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
257 return Symb.getSectionNumber();
258}
259
Rafael Espindola5e812af2014-01-30 02:49:50 +0000260void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000261 const coff_section *Sec = toSec(Ref);
262 Sec += 1;
263 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000264}
265
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000266std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
267 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000268 const coff_section *Sec = toSec(Ref);
269 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000270}
271
Rafael Espindola80291272014-10-08 15:28:58 +0000272uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000273 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000274 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000275}
276
Rafael Espindola80291272014-10-08 15:28:58 +0000277uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000278 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000279}
280
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000281std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
282 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000283 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000284 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000285 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000286 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
287 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000288}
289
Rafael Espindola80291272014-10-08 15:28:58 +0000290uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000291 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000292 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000293}
294
Rafael Espindola80291272014-10-08 15:28:58 +0000295bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000296 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000297 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000298}
299
Rafael Espindola80291272014-10-08 15:28:58 +0000300bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000301 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000302 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000303}
304
Rafael Espindola80291272014-10-08 15:28:58 +0000305bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000306 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000307 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
308 COFF::IMAGE_SCN_MEM_READ |
309 COFF::IMAGE_SCN_MEM_WRITE;
310 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000311}
312
Rafael Espindola6bf32212015-06-24 19:57:32 +0000313unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
314 uintptr_t Offset =
315 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
316 assert((Offset % sizeof(coff_section)) == 0);
317 return (Offset / sizeof(coff_section)) + 1;
318}
319
Rafael Espindola80291272014-10-08 15:28:58 +0000320bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000321 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000322 // In COFF, a virtual section won't have any in-file
323 // content, so the file pointer to the content will be zero.
324 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000325}
326
David Majnemere830c602014-11-13 08:46:37 +0000327static uint32_t getNumberOfRelocations(const coff_section *Sec,
328 MemoryBufferRef M, const uint8_t *base) {
329 // The field for the number of relocations in COFF section table is only
330 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
331 // NumberOfRelocations field, and the actual relocation count is stored in the
332 // VirtualAddress field in the first relocation entry.
333 if (Sec->hasExtendedRelocations()) {
334 const coff_relocation *FirstReloc;
335 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
336 base + Sec->PointerToRelocations)))
337 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000338 // -1 to exclude this first relocation entry.
339 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000340 }
341 return Sec->NumberOfRelocations;
342}
343
David Majnemer94751be2014-11-13 09:50:18 +0000344static const coff_relocation *
345getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
346 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
347 if (!NumRelocs)
348 return nullptr;
349 auto begin = reinterpret_cast<const coff_relocation *>(
350 Base + Sec->PointerToRelocations);
351 if (Sec->hasExtendedRelocations()) {
352 // Skip the first relocation entry repurposed to store the number of
353 // relocations.
354 begin++;
355 }
356 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
357 return nullptr;
358 return begin;
359}
360
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000361relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
362 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000363 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000364 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000365 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000366 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000367}
368
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000369relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
370 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000371 const coff_relocation *I = getFirstReloc(Sec, Data, base());
372 if (I)
373 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000374 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000375 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000376 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000377}
378
Rui Ueyamac2bed422013-09-27 21:04:00 +0000379// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000380std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000381 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000382 if (std::error_code EC = getObject(
383 SymbolTable16, Data, base() + getPointerToSymbolTable(),
384 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000385 return EC;
386
387 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000388 if (std::error_code EC = getObject(
389 SymbolTable32, Data, base() + getPointerToSymbolTable(),
390 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000391 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000392
393 // Find string table. The first four byte of the string table contains the
394 // total size of the string table, including the size field itself. If the
395 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000396 uint32_t StringTableOffset = getPointerToSymbolTable() +
397 getNumberOfSymbols() * getSymbolTableEntrySize();
398 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000399 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000400 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000401 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000402 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000403 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000404 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000405 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000406
Nico Rieck773a5792014-02-26 19:51:44 +0000407 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
408 // tools like cvtres write a size of 0 for an empty table instead of 4.
409 if (StringTableSize < 4)
410 StringTableSize = 4;
411
Rui Ueyamac2bed422013-09-27 21:04:00 +0000412 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000413 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000414 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000415 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000416}
417
Rui Ueyama215a5862014-02-20 06:51:07 +0000418// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000419std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000420 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
421 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000422 uint64_t Rva = Addr - ImageBase;
423 assert(Rva <= UINT32_MAX);
424 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000425}
426
Rui Ueyamac2bed422013-09-27 21:04:00 +0000427// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000428std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000429 for (const SectionRef &S : sections()) {
430 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000431 uint32_t SectionStart = Section->VirtualAddress;
432 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000433 if (SectionStart <= Addr && Addr < SectionEnd) {
434 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000435 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000436 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437 }
438 }
439 return object_error::parse_failed;
440}
441
442// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
443// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000444std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
445 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000446 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000447 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000448 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000449 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
450 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
451 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000452 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000453}
454
455// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000456std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000457 // First, we get the RVA of the import table. If the file lacks a pointer to
458 // the import table, do nothing.
459 const data_directory *DataEntry;
460 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000461 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462
463 // Do nothing if the pointer to import table is NULL.
464 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000465 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466
467 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000468 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000469 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000470 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000471
472 // Find the section that contains the RVA. This is needed because the RVA is
473 // the import table's memory address which is different from its file offset.
474 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000475 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000476 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000477 ImportDirectory = reinterpret_cast<
478 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000479 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000480}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000481
Rui Ueyama15d99352014-10-03 00:41:58 +0000482// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
483std::error_code COFFObjectFile::initDelayImportTablePtr() {
484 const data_directory *DataEntry;
485 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000486 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000487 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000488 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000489
490 uint32_t RVA = DataEntry->RelativeVirtualAddress;
491 NumberOfDelayImportDirectory = DataEntry->Size /
492 sizeof(delay_import_directory_table_entry) - 1;
493
494 uintptr_t IntPtr = 0;
495 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
496 return EC;
497 DelayImportDirectory = reinterpret_cast<
498 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000499 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000500}
501
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000502// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000503std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000504 // First, we get the RVA of the export table. If the file lacks a pointer to
505 // the export table, do nothing.
506 const data_directory *DataEntry;
507 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000508 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000509
510 // Do nothing if the pointer to export table is NULL.
511 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000512 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000513
514 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
515 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000516 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000517 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000518 ExportDirectory =
519 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000520 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000521}
522
Rui Ueyama74e85132014-11-19 00:18:07 +0000523std::error_code COFFObjectFile::initBaseRelocPtr() {
524 const data_directory *DataEntry;
525 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000526 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000527 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000528 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000529
530 uintptr_t IntPtr = 0;
531 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
532 return EC;
533 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
534 IntPtr);
535 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
536 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000537 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000538}
539
Rafael Espindola48af1c22014-08-19 18:44:46 +0000540COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
541 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000542 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
543 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
544 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
545 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000546 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000547 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
548 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000549 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000550 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000551 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000552
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000553 // The current location in the file where we are looking at.
554 uint64_t CurPtr = 0;
555
556 // PE header is optional and is present only in executables. If it exists,
557 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000558 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000559
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000560 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000561 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000562 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
563 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000564 const auto *DH = reinterpret_cast<const dos_header *>(base());
565 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
566 CurPtr = DH->AddressOfNewExeHeader;
567 // Check the PE magic bytes. ("PE\0\0")
568 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
569 EC = object_error::parse_failed;
570 return;
571 }
572 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
573 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000574 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000575 }
576
Rafael Espindola48af1c22014-08-19 18:44:46 +0000577 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000578 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000579
580 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
581 // import libraries share a common prefix but bigobj is more restrictive.
582 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
583 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
584 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
585 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
586 return;
587
588 // Verify that we are dealing with bigobj.
589 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
590 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
591 sizeof(COFF::BigObjMagic)) == 0) {
592 COFFHeader = nullptr;
593 CurPtr += sizeof(coff_bigobj_file_header);
594 } else {
595 // It's not a bigobj.
596 COFFBigObjHeader = nullptr;
597 }
598 }
599 if (COFFHeader) {
600 // The prior checkSize call may have failed. This isn't a hard error
601 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000602 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000603 CurPtr += sizeof(coff_file_header);
604
605 if (COFFHeader->isImportLibrary())
606 return;
607 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000608
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000609 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000610 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000611 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000612 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000613
614 const uint8_t *DataDirAddr;
615 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000616 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000617 PE32Header = Header;
618 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
619 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000620 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000621 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
622 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
623 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
624 } else {
625 // It's neither PE32 nor PE32+.
626 EC = object_error::parse_failed;
627 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000628 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000629 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000630 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000631 CurPtr += COFFHeader->SizeOfOptionalHeader;
632 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000633
Rafael Espindola48af1c22014-08-19 18:44:46 +0000634 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000635 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000636 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000637
Rui Ueyamac2bed422013-09-27 21:04:00 +0000638 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000639 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000640 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000641 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000642 } else {
643 // We had better not have any symbols if we don't have a symbol table.
644 if (getNumberOfSymbols() != 0) {
645 EC = object_error::parse_failed;
646 return;
647 }
648 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000649
Rui Ueyamac2bed422013-09-27 21:04:00 +0000650 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000651 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000652 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000653 if ((EC = initDelayImportTablePtr()))
654 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000655
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000656 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000657 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000658 return;
659
Rui Ueyama74e85132014-11-19 00:18:07 +0000660 // Initialize the pointer to the base relocation table.
661 if ((EC = initBaseRelocPtr()))
662 return;
663
Rui Ueyama7d099192015-06-09 15:20:42 +0000664 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000665}
666
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000667basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000668 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000669 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000670 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000671}
672
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000673basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000674 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000675 DataRefImpl Ret;
676 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000677 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000678}
679
Rui Ueyamabc654b12013-09-27 21:47:05 +0000680import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000681 return import_directory_iterator(
682 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000683}
684
Rui Ueyamabc654b12013-09-27 21:47:05 +0000685import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000686 return import_directory_iterator(
687 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000688}
David Meyerc429b802012-03-01 22:19:54 +0000689
Rui Ueyama15d99352014-10-03 00:41:58 +0000690delay_import_directory_iterator
691COFFObjectFile::delay_import_directory_begin() const {
692 return delay_import_directory_iterator(
693 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
694}
695
696delay_import_directory_iterator
697COFFObjectFile::delay_import_directory_end() const {
698 return delay_import_directory_iterator(
699 DelayImportDirectoryEntryRef(
700 DelayImportDirectory, NumberOfDelayImportDirectory, this));
701}
702
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000703export_directory_iterator COFFObjectFile::export_directory_begin() const {
704 return export_directory_iterator(
705 ExportDirectoryEntryRef(ExportDirectory, 0, this));
706}
707
708export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000709 if (!ExportDirectory)
710 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000711 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000712 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000713 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000714}
715
Rafael Espindolab5155a52014-02-10 20:24:04 +0000716section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000717 DataRefImpl Ret;
718 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
719 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000720}
721
Rafael Espindolab5155a52014-02-10 20:24:04 +0000722section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000723 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000724 int NumSections =
725 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000726 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
727 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000728}
729
Rui Ueyama74e85132014-11-19 00:18:07 +0000730base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
731 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
732}
733
734base_reloc_iterator COFFObjectFile::base_reloc_end() const {
735 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
736}
737
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000738uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000739 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000740}
741
742StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000743 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000744 case COFF::IMAGE_FILE_MACHINE_I386:
745 return "COFF-i386";
746 case COFF::IMAGE_FILE_MACHINE_AMD64:
747 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000748 case COFF::IMAGE_FILE_MACHINE_ARMNT:
749 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000750 default:
751 return "COFF-<unknown arch>";
752 }
753}
754
755unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000756 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000757 case COFF::IMAGE_FILE_MACHINE_I386:
758 return Triple::x86;
759 case COFF::IMAGE_FILE_MACHINE_AMD64:
760 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000761 case COFF::IMAGE_FILE_MACHINE_ARMNT:
762 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000763 default:
764 return Triple::UnknownArch;
765 }
766}
767
Rui Ueyama979fb402014-10-09 02:16:38 +0000768iterator_range<import_directory_iterator>
769COFFObjectFile::import_directories() const {
770 return make_range(import_directory_begin(), import_directory_end());
771}
772
773iterator_range<delay_import_directory_iterator>
774COFFObjectFile::delay_import_directories() const {
775 return make_range(delay_import_directory_begin(),
776 delay_import_directory_end());
777}
778
779iterator_range<export_directory_iterator>
780COFFObjectFile::export_directories() const {
781 return make_range(export_directory_begin(), export_directory_end());
782}
783
Rui Ueyama74e85132014-11-19 00:18:07 +0000784iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
785 return make_range(base_reloc_begin(), base_reloc_end());
786}
787
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000788std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000789 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000790 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000791}
792
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000793std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000794COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
795 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000796 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000797}
798
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000799std::error_code
800COFFObjectFile::getDataDirectory(uint32_t Index,
801 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000802 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000803 if (!DataDirectory) {
804 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000805 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000806 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000807 assert(PE32Header || PE32PlusHeader);
808 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
809 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000810 if (Index >= NumEnt) {
811 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000812 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000813 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000814 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000815 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000816}
817
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000818std::error_code COFFObjectFile::getSection(int32_t Index,
819 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000820 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000821 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000822 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000823 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000824 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000825 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000826 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000827 }
828 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000829}
830
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000831std::error_code COFFObjectFile::getString(uint32_t Offset,
832 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000833 if (StringTableSize <= 4)
834 // Tried to get a string from an empty string table.
835 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000836 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000837 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000838 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000839 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000840}
841
David Majnemer44f51e52014-09-10 12:51:52 +0000842std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000843 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000844 return getSymbolName(Symbol.getGeneric(), Res);
845}
846
847std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
848 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000849 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000850 if (Symbol->Name.Offset.Zeroes == 0) {
851 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000852 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000853 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000854 }
855
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000856 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000857 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000858 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000859 else
860 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000861 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000862 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000863}
864
David Majnemer44f51e52014-09-10 12:51:52 +0000865ArrayRef<uint8_t>
866COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000867 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000868
David Majnemer44f51e52014-09-10 12:51:52 +0000869 size_t SymbolSize = getSymbolTableEntrySize();
870 if (Symbol.getNumberOfAuxSymbols() > 0) {
871 // AUX data comes immediately after the symbol in COFF
872 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000873# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000874 // Verify that the Aux symbol points to a valid entry in the symbol table.
875 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000876 if (Offset < getPointerToSymbolTable() ||
877 Offset >=
878 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000879 report_fatal_error("Aux Symbol data was outside of symbol table.");
880
David Majnemer44f51e52014-09-10 12:51:52 +0000881 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
882 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000883# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000884 }
David Majnemer44f51e52014-09-10 12:51:52 +0000885 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000886}
887
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000888std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
889 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000890 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000891 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000892 // Null terminated, let ::strlen figure out the length.
893 Name = Sec->Name;
894 else
895 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000896 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000897
898 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000899 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000900 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000901 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000902 if (decodeBase64StringEntry(Name.substr(2), Offset))
903 return object_error::parse_failed;
904 } else {
905 if (Name.substr(1).getAsInteger(10, Offset))
906 return object_error::parse_failed;
907 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000908 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000909 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000910 }
911
912 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000913 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000914}
915
David Majnemera9ee5c02014-10-09 08:42:31 +0000916uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
917 // SizeOfRawData and VirtualSize change what they represent depending on
918 // whether or not we have an executable image.
919 //
920 // For object files, SizeOfRawData contains the size of section's data;
921 // VirtualSize is always zero.
922 //
923 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
924 // actual section size is in VirtualSize. It is possible for VirtualSize to
925 // be greater than SizeOfRawData; the contents past that point should be
926 // considered to be zero.
927 uint32_t SectionSize;
928 if (Sec->VirtualSize)
929 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
930 else
931 SectionSize = Sec->SizeOfRawData;
932
933 return SectionSize;
934}
935
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000936std::error_code
937COFFObjectFile::getSectionContents(const coff_section *Sec,
938 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000939 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
940 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000941 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
942 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000943 // The only thing that we need to verify is that the contents is contained
944 // within the file bounds. We don't need to make sure it doesn't cover other
945 // data, as there's nothing that says that is not allowed.
946 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000947 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000948 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000949 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000950 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000951 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000952}
953
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000954const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000955 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000956}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000957
Rafael Espindola5e812af2014-01-30 02:49:50 +0000958void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000959 Rel.p = reinterpret_cast<uintptr_t>(
960 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000961}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000962
Rafael Espindola10fcac72015-06-30 20:32:26 +0000963ErrorOr<uint64_t> COFFObjectFile::getRelocationAddress(DataRefImpl Rel) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000964 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000965}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000966
Rafael Espindola96d071c2015-06-29 23:29:12 +0000967uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +0000968 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000969 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000970}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000971
Rafael Espindola806f0062013-06-05 01:33:53 +0000972symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000973 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000974 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000975 if (R->SymbolTableIndex >= getNumberOfSymbols())
976 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000977 if (SymbolTable16)
978 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
979 else if (SymbolTable32)
980 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
981 else
David Majnemerc7353b52014-11-25 07:43:14 +0000982 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000983 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000984}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000985
Rafael Espindola99c041b2015-06-30 01:53:01 +0000986uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000987 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +0000988 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000989}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000990
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000991const coff_section *
992COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
993 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000994}
995
David Majnemer44f51e52014-09-10 12:51:52 +0000996COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
997 if (SymbolTable16)
998 return toSymb<coff_symbol16>(Ref);
999 if (SymbolTable32)
1000 return toSymb<coff_symbol32>(Ref);
1001 llvm_unreachable("no symbol table pointer!");
1002}
1003
1004COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1005 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001006}
1007
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001008const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001009COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1010 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001011}
1012
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001013iterator_range<const coff_relocation *>
1014COFFObjectFile::getRelocations(const coff_section *Sec) const {
1015 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1016 const coff_relocation *E = I;
1017 if (I)
1018 E += getNumberOfRelocations(Sec, Data, base());
1019 return make_range(I, E);
1020}
1021
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001022#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1023 case COFF::reloc_type: \
1024 Res = #reloc_type; \
1025 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001026
Rafael Espindola41bb4322015-06-30 04:08:37 +00001027void COFFObjectFile::getRelocationTypeName(
1028 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001029 const coff_relocation *Reloc = toRel(Rel);
1030 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001031 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001032 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001033 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001034 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1035 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1036 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1037 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1038 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1039 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1040 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1051 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001052 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001053 }
1054 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001055 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1056 switch (Reloc->Type) {
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1072 default:
1073 Res = "Unknown";
1074 }
1075 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001076 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001077 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1089 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001090 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001091 }
1092 break;
1093 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001094 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001095 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001096 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001097}
1098
1099#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1100
Rafael Espindolac66d7612014-08-17 19:09:37 +00001101bool COFFObjectFile::isRelocatableObject() const {
1102 return !DataDirectory;
1103}
1104
Rui Ueyamac2bed422013-09-27 21:04:00 +00001105bool ImportDirectoryEntryRef::
1106operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001107 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001108}
1109
Rafael Espindola5e812af2014-01-30 02:49:50 +00001110void ImportDirectoryEntryRef::moveNext() {
1111 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001112}
1113
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001114std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1115 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001116 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001117 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001118}
1119
Rui Ueyama861021f2014-10-02 22:05:29 +00001120static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001121makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001122 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001123 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001124 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001125 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001126 }
1127 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001128 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001129}
1130
Rui Ueyama15d99352014-10-03 00:41:58 +00001131static imported_symbol_iterator
1132importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001133 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001134 Object->getRvaPtr(RVA, IntPtr);
1135 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001136}
1137
Rui Ueyama15d99352014-10-03 00:41:58 +00001138static imported_symbol_iterator
1139importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001140 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001141 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001142 // Forward the pointer to the last entry which is null.
1143 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001144 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001145 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1146 while (*Entry++)
1147 ++Index;
1148 } else {
1149 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1150 while (*Entry++)
1151 ++Index;
1152 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001153 return makeImportedSymbolIterator(Object, IntPtr, Index);
1154}
1155
1156imported_symbol_iterator
1157ImportDirectoryEntryRef::imported_symbol_begin() const {
1158 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1159 OwningObject);
1160}
1161
1162imported_symbol_iterator
1163ImportDirectoryEntryRef::imported_symbol_end() const {
1164 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1165 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001166}
1167
Rui Ueyama979fb402014-10-09 02:16:38 +00001168iterator_range<imported_symbol_iterator>
1169ImportDirectoryEntryRef::imported_symbols() const {
1170 return make_range(imported_symbol_begin(), imported_symbol_end());
1171}
1172
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001173std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001174 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001175 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001176 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001177 return EC;
1178 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001179 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001180}
1181
Rui Ueyama1e152d52014-10-02 17:02:18 +00001182std::error_code
1183ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1184 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001185 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001186}
1187
1188std::error_code
1189ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1190 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001191 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001192}
1193
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001194std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001195 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001196 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001197 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1198 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001199 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001200 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001201 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001202}
1203
Rui Ueyama15d99352014-10-03 00:41:58 +00001204bool DelayImportDirectoryEntryRef::
1205operator==(const DelayImportDirectoryEntryRef &Other) const {
1206 return Table == Other.Table && Index == Other.Index;
1207}
1208
1209void DelayImportDirectoryEntryRef::moveNext() {
1210 ++Index;
1211}
1212
1213imported_symbol_iterator
1214DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1215 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1216 OwningObject);
1217}
1218
1219imported_symbol_iterator
1220DelayImportDirectoryEntryRef::imported_symbol_end() const {
1221 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1222 OwningObject);
1223}
1224
Rui Ueyama979fb402014-10-09 02:16:38 +00001225iterator_range<imported_symbol_iterator>
1226DelayImportDirectoryEntryRef::imported_symbols() const {
1227 return make_range(imported_symbol_begin(), imported_symbol_end());
1228}
1229
Rui Ueyama15d99352014-10-03 00:41:58 +00001230std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1231 uintptr_t IntPtr = 0;
1232 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1233 return EC;
1234 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001235 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001236}
1237
Rui Ueyama1af08652014-10-03 18:07:18 +00001238std::error_code DelayImportDirectoryEntryRef::
1239getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1240 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001241 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001242}
1243
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001244std::error_code DelayImportDirectoryEntryRef::
1245getImportAddress(int AddrIndex, uint64_t &Result) const {
1246 uint32_t RVA = Table[Index].DelayImportAddressTable +
1247 AddrIndex * (OwningObject->is64() ? 8 : 4);
1248 uintptr_t IntPtr = 0;
1249 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1250 return EC;
1251 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001252 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001253 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001254 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001255 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001256}
1257
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001258bool ExportDirectoryEntryRef::
1259operator==(const ExportDirectoryEntryRef &Other) const {
1260 return ExportTable == Other.ExportTable && Index == Other.Index;
1261}
1262
Rafael Espindola5e812af2014-01-30 02:49:50 +00001263void ExportDirectoryEntryRef::moveNext() {
1264 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001265}
1266
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001267// Returns the name of the current export symbol. If the symbol is exported only
1268// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001269std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001270 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001271 if (std::error_code EC =
1272 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001273 return EC;
1274 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001275 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001276}
1277
Rui Ueyamae5df6092014-01-17 22:02:24 +00001278// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001279std::error_code
1280ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001281 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001282 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001283}
1284
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001285// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001286std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001287 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001288 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001289}
1290
1291// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001292std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001293 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001294 if (std::error_code EC =
1295 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001296 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001297 const export_address_table_entry *entry =
1298 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001299 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001300 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001301}
1302
1303// Returns the name of the current export symbol. If the symbol is exported only
1304// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001305std::error_code
1306ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001307 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001308 if (std::error_code EC =
1309 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001310 return EC;
1311 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1312
1313 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1314 int Offset = 0;
1315 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1316 I < E; ++I, ++Offset) {
1317 if (*I != Index)
1318 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001319 if (std::error_code EC =
1320 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001321 return EC;
1322 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001323 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001324 return EC;
1325 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001326 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001327 }
1328 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001329 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001330}
1331
Rui Ueyama861021f2014-10-02 22:05:29 +00001332bool ImportedSymbolRef::
1333operator==(const ImportedSymbolRef &Other) const {
1334 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1335 && Index == Other.Index;
1336}
1337
1338void ImportedSymbolRef::moveNext() {
1339 ++Index;
1340}
1341
1342std::error_code
1343ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1344 uint32_t RVA;
1345 if (Entry32) {
1346 // If a symbol is imported only by ordinal, it has no name.
1347 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001348 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001349 RVA = Entry32[Index].getHintNameRVA();
1350 } else {
1351 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001352 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001353 RVA = Entry64[Index].getHintNameRVA();
1354 }
1355 uintptr_t IntPtr = 0;
1356 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1357 return EC;
1358 // +2 because the first two bytes is hint.
1359 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001360 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001361}
1362
1363std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1364 uint32_t RVA;
1365 if (Entry32) {
1366 if (Entry32[Index].isOrdinal()) {
1367 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001368 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001369 }
1370 RVA = Entry32[Index].getHintNameRVA();
1371 } else {
1372 if (Entry64[Index].isOrdinal()) {
1373 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001374 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001375 }
1376 RVA = Entry64[Index].getHintNameRVA();
1377 }
1378 uintptr_t IntPtr = 0;
1379 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1380 return EC;
1381 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001382 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001383}
1384
Rafael Espindola437b0d52014-07-31 03:12:45 +00001385ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001386ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001387 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001388 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001389 if (EC)
1390 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001391 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001392}
Rui Ueyama74e85132014-11-19 00:18:07 +00001393
1394bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1395 return Header == Other.Header && Index == Other.Index;
1396}
1397
1398void BaseRelocRef::moveNext() {
1399 // Header->BlockSize is the size of the current block, including the
1400 // size of the header itself.
1401 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001402 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001403 if (Size == Header->BlockSize) {
1404 // .reloc contains a list of base relocation blocks. Each block
1405 // consists of the header followed by entries. The header contains
1406 // how many entories will follow. When we reach the end of the
1407 // current block, proceed to the next block.
1408 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1409 reinterpret_cast<const uint8_t *>(Header) + Size);
1410 Index = 0;
1411 } else {
1412 ++Index;
1413 }
1414}
1415
1416std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1417 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1418 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001419 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001420}
1421
1422std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1423 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1424 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001425 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001426}