blob: c95037f4540bbb2a7ea967f02e6b592876c855b3 [file] [log] [blame]
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000016#include "llvm/ADT/StringSwitch.h"
17#include "llvm/ADT/Triple.h"
Rui Ueyama6a75acb2015-06-25 00:07:39 +000018#include "llvm/ADT/iterator_range.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000019#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000022#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000023#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000024
25using namespace llvm;
26using namespace object;
27
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000028using support::ulittle16_t;
29using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000030using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000031using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000033// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000034static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000035 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000036 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000037 return false;
38 }
39 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000040}
41
David Majnemere830c602014-11-13 08:46:37 +000042static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
David Majnemer94751be2014-11-13 09:50:18 +000043 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000044 if (Addr + Size < Addr || Addr + Size < Size ||
45 Addr + Size > uintptr_t(M.getBufferEnd()) ||
46 Addr < uintptr_t(M.getBufferStart())) {
47 return object_error::unexpected_eof;
48 }
Rui Ueyama7d099192015-06-09 15:20:42 +000049 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000050}
51
Rui Ueyamaed64342b2013-07-19 23:23:29 +000052// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
53// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000054template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000055static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000056 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000057 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000058 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000059 if (std::error_code EC = checkOffset(M, Addr, Size))
60 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000061 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyama7d099192015-06-09 15:20:42 +000062 return std::error_code();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000063}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064
Nico Rieck9d2c15e2014-02-22 16:12:20 +000065// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
66// prefixed slashes.
67static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
68 assert(Str.size() <= 6 && "String too long, possible overflow.");
69 if (Str.size() > 6)
70 return true;
71
72 uint64_t Value = 0;
73 while (!Str.empty()) {
74 unsigned CharVal;
75 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
76 CharVal = Str[0] - 'A';
77 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
78 CharVal = Str[0] - 'a' + 26;
79 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
80 CharVal = Str[0] - '0' + 52;
81 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000082 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000083 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000084 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000085 else
86 return true;
87
88 Value = (Value * 64) + CharVal;
89 Str = Str.substr(1);
90 }
91
92 if (Value > std::numeric_limits<uint32_t>::max())
93 return true;
94
95 Result = static_cast<uint32_t>(Value);
96 return false;
97}
98
David Majnemer44f51e52014-09-10 12:51:52 +000099template <typename coff_symbol_type>
100const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
101 const coff_symbol_type *Addr =
102 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103
David Majnemer236b0ca2014-11-17 11:17:17 +0000104 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000105#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000106 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000107 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000108
David Majnemer44f51e52014-09-10 12:51:52 +0000109 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
110 "Symbol did not point to the beginning of a symbol");
111#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000112
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000113 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000114}
115
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000116const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
117 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000118
119# ifndef NDEBUG
120 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000121 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000122 report_fatal_error("Section was outside of section table.");
123
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000124 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
125 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000126 "Section did not point to the beginning of a section");
127# endif
128
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000129 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000130}
131
Rafael Espindola5e812af2014-01-30 02:49:50 +0000132void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000133 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000134 if (SymbolTable16) {
135 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
136 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000137 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000138 } else if (SymbolTable32) {
139 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
140 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000141 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000142 } else {
143 llvm_unreachable("no symbol table pointer!");
144 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000145}
146
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000147Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000148 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000149 StringRef Result;
150 std::error_code EC = getSymbolName(Symb, Result);
151 if (EC)
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000152 return errorCodeToError(EC);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000153 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000154}
155
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000156uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
157 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000158}
159
Davide Italiano6b2bba12016-11-02 17:32:19 +0000160uint32_t COFFObjectFile::getSymbolAlignment(DataRefImpl Ref) const {
161 // MSVC/link.exe seems to align symbols to the next-power-of-2
162 // up to 32 bytes.
163 COFFSymbolRef Symb = getCOFFSymbol(Ref);
164 uint32_t Value = Symb.getValue();
165 return std::min(uint64_t(32),
166 isPowerOf2_64(Value) ? Value : NextPowerOf2(Value));
167}
168
Kevin Enderby931cb652016-06-24 18:24:42 +0000169Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
Rafael Espindolaed067c42015-07-03 18:19:00 +0000170 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000171 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000172 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000173
174 if (Symb.isAnyUndefined() || Symb.isCommon() ||
175 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000176 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000177
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000178 const coff_section *Section = nullptr;
179 if (std::error_code EC = getSection(SectionNumber, Section))
Kevin Enderby931cb652016-06-24 18:24:42 +0000180 return errorCodeToError(EC);
Rafael Espindola991af662015-06-24 19:11:10 +0000181 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000182
183 // The section VirtualAddress does not include ImageBase, and we want to
184 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000185 Result += getImageBase();
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000186
Rafael Espindolaed067c42015-07-03 18:19:00 +0000187 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000188}
189
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000190Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000191 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000192 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000193
Peter Collingbournee834f422015-08-06 05:26:35 +0000194 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
195 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000196 if (Symb.isAnyUndefined())
197 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000198 if (Symb.isCommon())
199 return SymbolRef::ST_Data;
200 if (Symb.isFileRecord())
201 return SymbolRef::ST_File;
202
203 // TODO: perhaps we need a new symbol type ST_Section.
204 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
205 return SymbolRef::ST_Debug;
206
207 if (!COFF::isReservedSectionNumber(SectionNumber))
208 return SymbolRef::ST_Data;
209
210 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000211}
212
Rafael Espindola20122a42014-01-31 20:57:12 +0000213uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000214 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000215 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000216
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000217 if (Symb.isExternal() || Symb.isWeakExternal())
Lang Hames9dc0eb42016-01-25 01:21:45 +0000218 Result |= SymbolRef::SF_Global;
David Meyer1df4b842012-02-28 23:47:53 +0000219
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000220 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000221 Result |= SymbolRef::SF_Weak;
222
David Majnemer44f51e52014-09-10 12:51:52 +0000223 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000224 Result |= SymbolRef::SF_Absolute;
225
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000226 if (Symb.isFileRecord())
227 Result |= SymbolRef::SF_FormatSpecific;
228
229 if (Symb.isSectionDefinition())
230 Result |= SymbolRef::SF_FormatSpecific;
231
232 if (Symb.isCommon())
233 Result |= SymbolRef::SF_Common;
234
235 if (Symb.isAnyUndefined())
236 Result |= SymbolRef::SF_Undefined;
237
Rafael Espindola20122a42014-01-31 20:57:12 +0000238 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000239}
240
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000241uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000242 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000243 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000244}
245
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000246Expected<section_iterator>
Rafael Espindola8bab8892015-08-07 23:27:14 +0000247COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000248 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000249 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
250 return section_end();
251 const coff_section *Sec = nullptr;
252 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000253 return errorCodeToError(EC);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000254 DataRefImpl Ret;
255 Ret.p = reinterpret_cast<uintptr_t>(Sec);
256 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000257}
258
Rafael Espindola6bf32212015-06-24 19:57:32 +0000259unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
260 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
261 return Symb.getSectionNumber();
262}
263
Rafael Espindola5e812af2014-01-30 02:49:50 +0000264void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000265 const coff_section *Sec = toSec(Ref);
266 Sec += 1;
267 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000268}
269
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000270std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
271 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000272 const coff_section *Sec = toSec(Ref);
273 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000274}
275
Rafael Espindola80291272014-10-08 15:28:58 +0000276uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000277 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000278 uint64_t Result = Sec->VirtualAddress;
279
280 // The section VirtualAddress does not include ImageBase, and we want to
281 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000282 Result += getImageBase();
David Majnemer7c6a0712015-07-31 17:40:24 +0000283 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000284}
285
Rafael Espindola80291272014-10-08 15:28:58 +0000286uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000287 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000288}
289
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000290std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
291 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000292 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000293 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000294 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000295 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
296 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000297}
298
Rafael Espindola80291272014-10-08 15:28:58 +0000299uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000300 const coff_section *Sec = toSec(Ref);
David Majnemer511391f2016-03-17 16:55:18 +0000301 return Sec->getAlignment();
Michael J. Spencer79894602011-10-10 21:55:43 +0000302}
303
George Rimar401e4e52016-05-24 12:48:46 +0000304bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
305 return false;
306}
307
Rafael Espindola80291272014-10-08 15:28:58 +0000308bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000309 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000310 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000311}
312
Rafael Espindola80291272014-10-08 15:28:58 +0000313bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000314 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000315 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000316}
317
Rafael Espindola80291272014-10-08 15:28:58 +0000318bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000319 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000320 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
321 COFF::IMAGE_SCN_MEM_READ |
322 COFF::IMAGE_SCN_MEM_WRITE;
323 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000324}
325
Rafael Espindola6bf32212015-06-24 19:57:32 +0000326unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
327 uintptr_t Offset =
328 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
329 assert((Offset % sizeof(coff_section)) == 0);
330 return (Offset / sizeof(coff_section)) + 1;
331}
332
Rafael Espindola80291272014-10-08 15:28:58 +0000333bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000334 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000335 // In COFF, a virtual section won't have any in-file
336 // content, so the file pointer to the content will be zero.
337 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000338}
339
David Majnemere830c602014-11-13 08:46:37 +0000340static uint32_t getNumberOfRelocations(const coff_section *Sec,
341 MemoryBufferRef M, const uint8_t *base) {
342 // The field for the number of relocations in COFF section table is only
343 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
344 // NumberOfRelocations field, and the actual relocation count is stored in the
345 // VirtualAddress field in the first relocation entry.
346 if (Sec->hasExtendedRelocations()) {
347 const coff_relocation *FirstReloc;
348 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
349 base + Sec->PointerToRelocations)))
350 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000351 // -1 to exclude this first relocation entry.
352 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000353 }
354 return Sec->NumberOfRelocations;
355}
356
David Majnemer94751be2014-11-13 09:50:18 +0000357static const coff_relocation *
358getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
359 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
360 if (!NumRelocs)
361 return nullptr;
362 auto begin = reinterpret_cast<const coff_relocation *>(
363 Base + Sec->PointerToRelocations);
364 if (Sec->hasExtendedRelocations()) {
365 // Skip the first relocation entry repurposed to store the number of
366 // relocations.
367 begin++;
368 }
369 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
370 return nullptr;
371 return begin;
372}
373
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000374relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
375 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000376 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000377 if (begin && Sec->VirtualAddress != 0)
378 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000379 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000380 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000381 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000382}
383
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000384relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
385 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000386 const coff_relocation *I = getFirstReloc(Sec, Data, base());
387 if (I)
388 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000390 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000391 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000392}
393
Rui Ueyamac2bed422013-09-27 21:04:00 +0000394// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000395std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000396 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000397 if (std::error_code EC = getObject(
398 SymbolTable16, Data, base() + getPointerToSymbolTable(),
399 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000400 return EC;
401
402 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000403 if (std::error_code EC = getObject(
404 SymbolTable32, Data, base() + getPointerToSymbolTable(),
405 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000406 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000407
408 // Find string table. The first four byte of the string table contains the
409 // total size of the string table, including the size field itself. If the
410 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000411 uint32_t StringTableOffset = getPointerToSymbolTable() +
412 getNumberOfSymbols() * getSymbolTableEntrySize();
413 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000414 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000415 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000416 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000417 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000418 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000419 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000420 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000421
Nico Rieck773a5792014-02-26 19:51:44 +0000422 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
423 // tools like cvtres write a size of 0 for an empty table instead of 4.
424 if (StringTableSize < 4)
425 StringTableSize = 4;
426
Rui Ueyamac2bed422013-09-27 21:04:00 +0000427 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000428 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000430 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000431}
432
Reid Kleckner21427ad2015-10-09 00:15:08 +0000433uint64_t COFFObjectFile::getImageBase() const {
Reid Klecknere94fef72015-10-09 00:15:01 +0000434 if (PE32Header)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000435 return PE32Header->ImageBase;
Reid Klecknere94fef72015-10-09 00:15:01 +0000436 else if (PE32PlusHeader)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000437 return PE32PlusHeader->ImageBase;
438 // This actually comes up in practice.
439 return 0;
Reid Klecknere94fef72015-10-09 00:15:01 +0000440}
441
Rui Ueyama215a5862014-02-20 06:51:07 +0000442// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000443std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner21427ad2015-10-09 00:15:08 +0000444 uint64_t ImageBase = getImageBase();
Rui Ueyamab7a40082014-02-20 19:14:56 +0000445 uint64_t Rva = Addr - ImageBase;
446 assert(Rva <= UINT32_MAX);
447 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000448}
449
Rui Ueyamac2bed422013-09-27 21:04:00 +0000450// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000451std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000452 for (const SectionRef &S : sections()) {
453 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000454 uint32_t SectionStart = Section->VirtualAddress;
455 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000456 if (SectionStart <= Addr && Addr < SectionEnd) {
457 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000459 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000460 }
461 }
462 return object_error::parse_failed;
463}
464
Reid Kleckner2da433e2016-06-02 17:10:43 +0000465std::error_code
466COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size,
467 ArrayRef<uint8_t> &Contents) const {
468 for (const SectionRef &S : sections()) {
469 const coff_section *Section = getCOFFSection(S);
470 uint32_t SectionStart = Section->VirtualAddress;
471 // Check if this RVA is within the section bounds. Be careful about integer
472 // overflow.
473 uint32_t OffsetIntoSection = RVA - SectionStart;
474 if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
475 Size <= Section->VirtualSize - OffsetIntoSection) {
476 uintptr_t Begin =
477 uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
478 Contents =
479 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
480 return std::error_code();
481 }
482 }
483 return object_error::parse_failed;
484}
485
Rui Ueyamac2bed422013-09-27 21:04:00 +0000486// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
487// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000488std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
489 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000490 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000491 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000492 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000493 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
494 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
495 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000496 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000497}
498
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000499std::error_code
500COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir,
501 const codeview::DebugInfo *&PDBInfo,
502 StringRef &PDBFileName) const {
Reid Kleckner2da433e2016-06-02 17:10:43 +0000503 ArrayRef<uint8_t> InfoBytes;
504 if (std::error_code EC = getRvaAndSizeAsBytes(
505 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
506 return EC;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000507 if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
Reid Kleckner2da433e2016-06-02 17:10:43 +0000508 return object_error::parse_failed;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000509 PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
510 InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
Reid Kleckner2da433e2016-06-02 17:10:43 +0000511 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
512 InfoBytes.size());
513 // Truncate the name at the first null byte. Ignore any padding.
514 PDBFileName = PDBFileName.split('\0').first;
515 return std::error_code();
516}
517
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000518std::error_code
519COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo,
520 StringRef &PDBFileName) const {
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000521 for (const debug_directory &D : debug_directories())
522 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW)
523 return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
524 // If we get here, there is no PDB info to return.
525 PDBInfo = nullptr;
526 PDBFileName = StringRef();
527 return std::error_code();
528}
529
Rui Ueyamac2bed422013-09-27 21:04:00 +0000530// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000531std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000532 // First, we get the RVA of the import table. If the file lacks a pointer to
533 // the import table, do nothing.
534 const data_directory *DataEntry;
535 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000536 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000537
538 // Do nothing if the pointer to import table is NULL.
539 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000540 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000541
542 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000543
544 // Find the section that contains the RVA. This is needed because the RVA is
545 // the import table's memory address which is different from its file offset.
546 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000547 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000548 return EC;
David Majnemerad7b7e72016-06-26 04:36:32 +0000549 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
550 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000551 ImportDirectory = reinterpret_cast<
David Majnemer1c0aa042016-07-31 19:25:21 +0000552 const coff_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000553 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000554}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000555
Rui Ueyama15d99352014-10-03 00:41:58 +0000556// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
557std::error_code COFFObjectFile::initDelayImportTablePtr() {
558 const data_directory *DataEntry;
559 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000560 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000561 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000562 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000563
564 uint32_t RVA = DataEntry->RelativeVirtualAddress;
565 NumberOfDelayImportDirectory = DataEntry->Size /
566 sizeof(delay_import_directory_table_entry) - 1;
567
568 uintptr_t IntPtr = 0;
569 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
570 return EC;
571 DelayImportDirectory = reinterpret_cast<
572 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000573 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000574}
575
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000576// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000577std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000578 // First, we get the RVA of the export table. If the file lacks a pointer to
579 // the export table, do nothing.
580 const data_directory *DataEntry;
581 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000582 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000583
584 // Do nothing if the pointer to export table is NULL.
585 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000586 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000587
588 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
589 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000590 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000591 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000592 ExportDirectory =
593 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000594 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000595}
596
Rui Ueyama74e85132014-11-19 00:18:07 +0000597std::error_code COFFObjectFile::initBaseRelocPtr() {
598 const data_directory *DataEntry;
599 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000600 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000601 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000602 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000603
604 uintptr_t IntPtr = 0;
605 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
606 return EC;
607 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
608 IntPtr);
609 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
610 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000611 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000612}
613
Reid Kleckner2da433e2016-06-02 17:10:43 +0000614std::error_code COFFObjectFile::initDebugDirectoryPtr() {
615 // Get the RVA of the debug directory. Do nothing if it does not exist.
616 const data_directory *DataEntry;
617 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
618 return std::error_code();
619
620 // Do nothing if the RVA is NULL.
621 if (DataEntry->RelativeVirtualAddress == 0)
622 return std::error_code();
623
624 // Check that the size is a multiple of the entry size.
625 if (DataEntry->Size % sizeof(debug_directory) != 0)
626 return object_error::parse_failed;
627
628 uintptr_t IntPtr = 0;
629 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
630 return EC;
631 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
632 if (std::error_code EC = getRvaPtr(
633 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
634 return EC;
635 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
636 return std::error_code();
637}
638
Rafael Espindola48af1c22014-08-19 18:44:46 +0000639COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
640 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000641 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
642 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
643 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
David Majnemerad7b7e72016-06-26 04:36:32 +0000644 ImportDirectory(nullptr),
Rui Ueyama15d99352014-10-03 00:41:58 +0000645 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Kleckner2da433e2016-06-02 17:10:43 +0000646 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
647 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000648 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000649 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000650 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000651
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000652 // The current location in the file where we are looking at.
653 uint64_t CurPtr = 0;
654
655 // PE header is optional and is present only in executables. If it exists,
656 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000657 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000658
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000659 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000660 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000661 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
662 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000663 const auto *DH = reinterpret_cast<const dos_header *>(base());
664 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
665 CurPtr = DH->AddressOfNewExeHeader;
666 // Check the PE magic bytes. ("PE\0\0")
667 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
668 EC = object_error::parse_failed;
669 return;
670 }
671 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
672 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000673 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000674 }
675
Rafael Espindola48af1c22014-08-19 18:44:46 +0000676 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000677 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000678
679 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
680 // import libraries share a common prefix but bigobj is more restrictive.
681 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
682 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
683 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
684 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
685 return;
686
687 // Verify that we are dealing with bigobj.
688 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
689 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
690 sizeof(COFF::BigObjMagic)) == 0) {
691 COFFHeader = nullptr;
692 CurPtr += sizeof(coff_bigobj_file_header);
693 } else {
694 // It's not a bigobj.
695 COFFBigObjHeader = nullptr;
696 }
697 }
698 if (COFFHeader) {
699 // The prior checkSize call may have failed. This isn't a hard error
700 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000701 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000702 CurPtr += sizeof(coff_file_header);
703
704 if (COFFHeader->isImportLibrary())
705 return;
706 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000707
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000708 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000709 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000710 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000711 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000712
713 const uint8_t *DataDirAddr;
714 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000715 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000716 PE32Header = Header;
717 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
718 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000719 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000720 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
721 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
722 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
723 } else {
724 // It's neither PE32 nor PE32+.
725 EC = object_error::parse_failed;
726 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000727 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000728 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000729 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000730 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000731
Rui Ueyama8950a532016-08-11 22:02:44 +0000732 if (COFFHeader)
733 CurPtr += COFFHeader->SizeOfOptionalHeader;
734
Rafael Espindola48af1c22014-08-19 18:44:46 +0000735 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000736 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000737 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000738
Rui Ueyamac2bed422013-09-27 21:04:00 +0000739 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000740 if (getPointerToSymbolTable() != 0) {
David Majnemerac8cfab2016-08-30 20:20:24 +0000741 if ((EC = initSymbolTablePtr())) {
742 SymbolTable16 = nullptr;
743 SymbolTable32 = nullptr;
744 StringTable = nullptr;
745 StringTableSize = 0;
746 }
David Majnemer236b0ca2014-11-17 11:17:17 +0000747 } else {
748 // We had better not have any symbols if we don't have a symbol table.
749 if (getNumberOfSymbols() != 0) {
750 EC = object_error::parse_failed;
751 return;
752 }
753 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000754
Rui Ueyamac2bed422013-09-27 21:04:00 +0000755 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000756 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000757 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000758 if ((EC = initDelayImportTablePtr()))
759 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000760
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000761 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000762 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000763 return;
764
Rui Ueyama74e85132014-11-19 00:18:07 +0000765 // Initialize the pointer to the base relocation table.
766 if ((EC = initBaseRelocPtr()))
767 return;
768
Reid Kleckner2da433e2016-06-02 17:10:43 +0000769 // Initialize the pointer to the export table.
770 if ((EC = initDebugDirectoryPtr()))
771 return;
772
Rui Ueyama7d099192015-06-09 15:20:42 +0000773 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000774}
775
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000776basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000777 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000778 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000779 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000780}
781
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000782basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000783 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000784 DataRefImpl Ret;
785 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000786 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000787}
788
Rui Ueyamabc654b12013-09-27 21:47:05 +0000789import_directory_iterator COFFObjectFile::import_directory_begin() const {
David Majnemerad7b7e72016-06-26 04:36:32 +0000790 if (!ImportDirectory)
791 return import_directory_end();
David Majnemer1c0aa042016-07-31 19:25:21 +0000792 if (ImportDirectory->isNull())
David Majnemerad7b7e72016-06-26 04:36:32 +0000793 return import_directory_end();
Rui Ueyamaa045b732014-01-16 03:13:19 +0000794 return import_directory_iterator(
795 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000796}
797
Rui Ueyamabc654b12013-09-27 21:47:05 +0000798import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000799 return import_directory_iterator(
David Majnemerad7b7e72016-06-26 04:36:32 +0000800 ImportDirectoryEntryRef(nullptr, -1, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000801}
David Meyerc429b802012-03-01 22:19:54 +0000802
Rui Ueyama15d99352014-10-03 00:41:58 +0000803delay_import_directory_iterator
804COFFObjectFile::delay_import_directory_begin() const {
805 return delay_import_directory_iterator(
806 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
807}
808
809delay_import_directory_iterator
810COFFObjectFile::delay_import_directory_end() const {
811 return delay_import_directory_iterator(
812 DelayImportDirectoryEntryRef(
813 DelayImportDirectory, NumberOfDelayImportDirectory, this));
814}
815
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000816export_directory_iterator COFFObjectFile::export_directory_begin() const {
817 return export_directory_iterator(
818 ExportDirectoryEntryRef(ExportDirectory, 0, this));
819}
820
821export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000822 if (!ExportDirectory)
823 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000824 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000825 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000826 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000827}
828
Rafael Espindolab5155a52014-02-10 20:24:04 +0000829section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000830 DataRefImpl Ret;
831 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
832 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000833}
834
Rafael Espindolab5155a52014-02-10 20:24:04 +0000835section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000836 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000837 int NumSections =
838 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000839 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
840 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000841}
842
Rui Ueyama74e85132014-11-19 00:18:07 +0000843base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
844 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
845}
846
847base_reloc_iterator COFFObjectFile::base_reloc_end() const {
848 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
849}
850
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000851uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000852 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000853}
854
855StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000856 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000857 case COFF::IMAGE_FILE_MACHINE_I386:
858 return "COFF-i386";
859 case COFF::IMAGE_FILE_MACHINE_AMD64:
860 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000861 case COFF::IMAGE_FILE_MACHINE_ARMNT:
862 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000863 case COFF::IMAGE_FILE_MACHINE_ARM64:
864 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000865 default:
866 return "COFF-<unknown arch>";
867 }
868}
869
870unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000871 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000872 case COFF::IMAGE_FILE_MACHINE_I386:
873 return Triple::x86;
874 case COFF::IMAGE_FILE_MACHINE_AMD64:
875 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000876 case COFF::IMAGE_FILE_MACHINE_ARMNT:
877 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000878 case COFF::IMAGE_FILE_MACHINE_ARM64:
879 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000880 default:
881 return Triple::UnknownArch;
882 }
883}
884
Rui Ueyama979fb402014-10-09 02:16:38 +0000885iterator_range<import_directory_iterator>
886COFFObjectFile::import_directories() const {
887 return make_range(import_directory_begin(), import_directory_end());
888}
889
890iterator_range<delay_import_directory_iterator>
891COFFObjectFile::delay_import_directories() const {
892 return make_range(delay_import_directory_begin(),
893 delay_import_directory_end());
894}
895
896iterator_range<export_directory_iterator>
897COFFObjectFile::export_directories() const {
898 return make_range(export_directory_begin(), export_directory_end());
899}
900
Rui Ueyama74e85132014-11-19 00:18:07 +0000901iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
902 return make_range(base_reloc_begin(), base_reloc_end());
903}
904
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000905std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000906 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000907 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000908}
909
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000910std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000911COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
912 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000913 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000914}
915
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000916std::error_code
917COFFObjectFile::getDataDirectory(uint32_t Index,
918 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000919 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000920 if (!DataDirectory) {
921 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000922 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000923 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000924 assert(PE32Header || PE32PlusHeader);
925 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
926 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000927 if (Index >= NumEnt) {
928 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000929 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000930 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000931 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000932 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000933}
934
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000935std::error_code COFFObjectFile::getSection(int32_t Index,
936 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000937 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000938 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000939 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000940 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000941 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000942 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000943 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000944 }
945 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000946}
947
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000948std::error_code COFFObjectFile::getString(uint32_t Offset,
949 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000950 if (StringTableSize <= 4)
951 // Tried to get a string from an empty string table.
952 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000953 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000954 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000955 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000956 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000957}
958
David Majnemer44f51e52014-09-10 12:51:52 +0000959std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000960 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000961 return getSymbolName(Symbol.getGeneric(), Res);
962}
963
964std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
965 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000966 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000967 if (Symbol->Name.Offset.Zeroes == 0) {
968 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000969 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000970 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000971 }
972
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000973 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000974 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000975 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000976 else
977 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000978 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000979 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000980}
981
David Majnemer44f51e52014-09-10 12:51:52 +0000982ArrayRef<uint8_t>
983COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000984 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000985
David Majnemer44f51e52014-09-10 12:51:52 +0000986 size_t SymbolSize = getSymbolTableEntrySize();
987 if (Symbol.getNumberOfAuxSymbols() > 0) {
988 // AUX data comes immediately after the symbol in COFF
989 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000990# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000991 // Verify that the Aux symbol points to a valid entry in the symbol table.
992 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000993 if (Offset < getPointerToSymbolTable() ||
994 Offset >=
995 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000996 report_fatal_error("Aux Symbol data was outside of symbol table.");
997
David Majnemer44f51e52014-09-10 12:51:52 +0000998 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
999 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +00001000# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +00001001 }
David Majnemer44f51e52014-09-10 12:51:52 +00001002 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +00001003}
1004
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001005std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
1006 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001007 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +00001008 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001009 // Null terminated, let ::strlen figure out the length.
1010 Name = Sec->Name;
1011 else
1012 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +00001013 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001014
1015 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +00001016 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001017 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +00001018 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +00001019 if (decodeBase64StringEntry(Name.substr(2), Offset))
1020 return object_error::parse_failed;
1021 } else {
1022 if (Name.substr(1).getAsInteger(10, Offset))
1023 return object_error::parse_failed;
1024 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001025 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001026 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001027 }
1028
1029 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +00001030 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001031}
1032
David Majnemera9ee5c02014-10-09 08:42:31 +00001033uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1034 // SizeOfRawData and VirtualSize change what they represent depending on
1035 // whether or not we have an executable image.
1036 //
1037 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001038 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +00001039 //
1040 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1041 // actual section size is in VirtualSize. It is possible for VirtualSize to
1042 // be greater than SizeOfRawData; the contents past that point should be
1043 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001044 if (getDOSHeader())
1045 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1046 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001047}
1048
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001049std::error_code
1050COFFObjectFile::getSectionContents(const coff_section *Sec,
1051 ArrayRef<uint8_t> &Res) const {
David Majnemere2129662016-05-28 19:45:51 +00001052 // In COFF, a virtual section won't have any in-file
1053 // content, so the file pointer to the content will be zero.
1054 if (Sec->PointerToRawData == 0)
1055 return object_error::parse_failed;
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001056 // The only thing that we need to verify is that the contents is contained
1057 // within the file bounds. We don't need to make sure it doesn't cover other
1058 // data, as there's nothing that says that is not allowed.
1059 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001060 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001061 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001062 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001063 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001064 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001065}
1066
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001067const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001068 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001069}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001070
Rafael Espindola5e812af2014-01-30 02:49:50 +00001071void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001072 Rel.p = reinterpret_cast<uintptr_t>(
1073 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001074}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001075
Rafael Espindola96d071c2015-06-29 23:29:12 +00001076uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +00001077 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001078 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001079}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001080
Rafael Espindola806f0062013-06-05 01:33:53 +00001081symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001082 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001083 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001084 if (R->SymbolTableIndex >= getNumberOfSymbols())
1085 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001086 if (SymbolTable16)
1087 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1088 else if (SymbolTable32)
1089 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1090 else
David Majnemerc7353b52014-11-25 07:43:14 +00001091 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001092 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001093}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001094
Rafael Espindola99c041b2015-06-30 01:53:01 +00001095uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001096 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001097 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001098}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001099
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001100const coff_section *
1101COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1102 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001103}
1104
David Majnemer44f51e52014-09-10 12:51:52 +00001105COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1106 if (SymbolTable16)
1107 return toSymb<coff_symbol16>(Ref);
1108 if (SymbolTable32)
1109 return toSymb<coff_symbol32>(Ref);
1110 llvm_unreachable("no symbol table pointer!");
1111}
1112
1113COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1114 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001115}
1116
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001117const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001118COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1119 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001120}
1121
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001122iterator_range<const coff_relocation *>
1123COFFObjectFile::getRelocations(const coff_section *Sec) const {
1124 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1125 const coff_relocation *E = I;
1126 if (I)
1127 E += getNumberOfRelocations(Sec, Data, base());
1128 return make_range(I, E);
1129}
1130
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001131#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1132 case COFF::reloc_type: \
1133 Res = #reloc_type; \
1134 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001135
Rafael Espindola41bb4322015-06-30 04:08:37 +00001136void COFFObjectFile::getRelocationTypeName(
1137 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001138 const coff_relocation *Reloc = toRel(Rel);
1139 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001140 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001141 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001142 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001143 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1144 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1145 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1146 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1147 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1148 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1149 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1150 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1151 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1152 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1153 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1154 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1155 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1156 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1157 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1158 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1159 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1160 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001161 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001162 }
1163 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001164 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1165 switch (Reloc->Type) {
1166 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1167 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1168 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1169 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1170 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1171 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1172 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1173 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1174 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1175 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1176 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1177 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1178 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1179 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1180 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1181 default:
1182 Res = "Unknown";
1183 }
1184 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001185 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001186 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001187 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1188 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1189 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1190 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1191 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1192 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1193 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1194 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1195 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1196 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1197 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1198 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001199 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001200 }
1201 break;
1202 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001203 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001204 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001205 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001206}
1207
1208#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1209
Rafael Espindolac66d7612014-08-17 19:09:37 +00001210bool COFFObjectFile::isRelocatableObject() const {
1211 return !DataDirectory;
1212}
1213
Rui Ueyamac2bed422013-09-27 21:04:00 +00001214bool ImportDirectoryEntryRef::
1215operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001216 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001217}
1218
Rafael Espindola5e812af2014-01-30 02:49:50 +00001219void ImportDirectoryEntryRef::moveNext() {
1220 ++Index;
David Majnemer1c0aa042016-07-31 19:25:21 +00001221 if (ImportTable[Index].isNull()) {
David Majnemerad7b7e72016-06-26 04:36:32 +00001222 Index = -1;
1223 ImportTable = nullptr;
1224 }
Rui Ueyamac2bed422013-09-27 21:04:00 +00001225}
1226
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001227std::error_code ImportDirectoryEntryRef::getImportTableEntry(
David Majnemer1c0aa042016-07-31 19:25:21 +00001228 const coff_import_directory_table_entry *&Result) const {
David Majnemerad7b7e72016-06-26 04:36:32 +00001229 return getObject(Result, OwningObject->Data, ImportTable + Index);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001230}
1231
Rui Ueyama861021f2014-10-02 22:05:29 +00001232static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001233makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001234 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001235 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001236 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001237 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001238 }
1239 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001240 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001241}
1242
Rui Ueyama15d99352014-10-03 00:41:58 +00001243static imported_symbol_iterator
1244importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001245 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001246 Object->getRvaPtr(RVA, IntPtr);
1247 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001248}
1249
Rui Ueyama15d99352014-10-03 00:41:58 +00001250static imported_symbol_iterator
1251importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001252 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001253 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001254 // Forward the pointer to the last entry which is null.
1255 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001256 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001257 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1258 while (*Entry++)
1259 ++Index;
1260 } else {
1261 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1262 while (*Entry++)
1263 ++Index;
1264 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001265 return makeImportedSymbolIterator(Object, IntPtr, Index);
1266}
1267
1268imported_symbol_iterator
1269ImportDirectoryEntryRef::imported_symbol_begin() const {
David Majnemer60049522016-07-31 19:40:02 +00001270 return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001271 OwningObject);
1272}
1273
1274imported_symbol_iterator
1275ImportDirectoryEntryRef::imported_symbol_end() const {
David Majnemer60049522016-07-31 19:40:02 +00001276 return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001277 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001278}
1279
Rui Ueyama979fb402014-10-09 02:16:38 +00001280iterator_range<imported_symbol_iterator>
1281ImportDirectoryEntryRef::imported_symbols() const {
1282 return make_range(imported_symbol_begin(), imported_symbol_end());
1283}
1284
David Majnemer60049522016-07-31 19:40:02 +00001285imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const {
1286 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1287 OwningObject);
1288}
1289
1290imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const {
1291 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1292 OwningObject);
1293}
1294
1295iterator_range<imported_symbol_iterator>
1296ImportDirectoryEntryRef::lookup_table_symbols() const {
1297 return make_range(lookup_table_begin(), lookup_table_end());
1298}
1299
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001300std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001301 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001302 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001303 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001304 return EC;
1305 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001306 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001307}
1308
Rui Ueyama1e152d52014-10-02 17:02:18 +00001309std::error_code
1310ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1311 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001312 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001313}
1314
1315std::error_code
1316ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1317 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001318 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001319}
1320
Rui Ueyama15d99352014-10-03 00:41:58 +00001321bool DelayImportDirectoryEntryRef::
1322operator==(const DelayImportDirectoryEntryRef &Other) const {
1323 return Table == Other.Table && Index == Other.Index;
1324}
1325
1326void DelayImportDirectoryEntryRef::moveNext() {
1327 ++Index;
1328}
1329
1330imported_symbol_iterator
1331DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1332 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1333 OwningObject);
1334}
1335
1336imported_symbol_iterator
1337DelayImportDirectoryEntryRef::imported_symbol_end() const {
1338 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1339 OwningObject);
1340}
1341
Rui Ueyama979fb402014-10-09 02:16:38 +00001342iterator_range<imported_symbol_iterator>
1343DelayImportDirectoryEntryRef::imported_symbols() const {
1344 return make_range(imported_symbol_begin(), imported_symbol_end());
1345}
1346
Rui Ueyama15d99352014-10-03 00:41:58 +00001347std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1348 uintptr_t IntPtr = 0;
1349 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1350 return EC;
1351 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001352 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001353}
1354
Rui Ueyama1af08652014-10-03 18:07:18 +00001355std::error_code DelayImportDirectoryEntryRef::
1356getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1357 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001358 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001359}
1360
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001361std::error_code DelayImportDirectoryEntryRef::
1362getImportAddress(int AddrIndex, uint64_t &Result) const {
1363 uint32_t RVA = Table[Index].DelayImportAddressTable +
1364 AddrIndex * (OwningObject->is64() ? 8 : 4);
1365 uintptr_t IntPtr = 0;
1366 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1367 return EC;
1368 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001369 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001370 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001371 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001372 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001373}
1374
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001375bool ExportDirectoryEntryRef::
1376operator==(const ExportDirectoryEntryRef &Other) const {
1377 return ExportTable == Other.ExportTable && Index == Other.Index;
1378}
1379
Rafael Espindola5e812af2014-01-30 02:49:50 +00001380void ExportDirectoryEntryRef::moveNext() {
1381 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001382}
1383
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001384// Returns the name of the current export symbol. If the symbol is exported only
1385// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001386std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001387 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001388 if (std::error_code EC =
1389 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001390 return EC;
1391 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001392 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001393}
1394
Rui Ueyamae5df6092014-01-17 22:02:24 +00001395// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001396std::error_code
1397ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001398 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001399 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001400}
1401
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001402// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001403std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001404 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001405 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001406}
1407
1408// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001409std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001410 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001411 if (std::error_code EC =
1412 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001413 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001414 const export_address_table_entry *entry =
1415 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001416 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001417 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001418}
1419
1420// Returns the name of the current export symbol. If the symbol is exported only
1421// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001422std::error_code
1423ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001424 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001425 if (std::error_code EC =
1426 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001427 return EC;
1428 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1429
1430 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1431 int Offset = 0;
1432 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1433 I < E; ++I, ++Offset) {
1434 if (*I != Index)
1435 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001436 if (std::error_code EC =
1437 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001438 return EC;
1439 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001440 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001441 return EC;
1442 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001443 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001444 }
1445 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001446 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001447}
1448
Rui Ueyama6161b382016-01-12 23:28:42 +00001449std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1450 const data_directory *DataEntry;
1451 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1452 return EC;
1453 uint32_t RVA;
1454 if (auto EC = getExportRVA(RVA))
1455 return EC;
1456 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1457 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1458 Result = (Begin <= RVA && RVA < End);
1459 return std::error_code();
1460}
1461
1462std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1463 uint32_t RVA;
1464 if (auto EC = getExportRVA(RVA))
1465 return EC;
1466 uintptr_t IntPtr = 0;
1467 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1468 return EC;
1469 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1470 return std::error_code();
1471}
1472
Rui Ueyama861021f2014-10-02 22:05:29 +00001473bool ImportedSymbolRef::
1474operator==(const ImportedSymbolRef &Other) const {
1475 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1476 && Index == Other.Index;
1477}
1478
1479void ImportedSymbolRef::moveNext() {
1480 ++Index;
1481}
1482
1483std::error_code
1484ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1485 uint32_t RVA;
1486 if (Entry32) {
1487 // If a symbol is imported only by ordinal, it has no name.
1488 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001489 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001490 RVA = Entry32[Index].getHintNameRVA();
1491 } else {
1492 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001493 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001494 RVA = Entry64[Index].getHintNameRVA();
1495 }
1496 uintptr_t IntPtr = 0;
1497 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1498 return EC;
1499 // +2 because the first two bytes is hint.
1500 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001501 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001502}
1503
David Majnemerad7b7e72016-06-26 04:36:32 +00001504std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1505 if (Entry32)
1506 Result = Entry32[Index].isOrdinal();
1507 else
1508 Result = Entry64[Index].isOrdinal();
1509 return std::error_code();
1510}
1511
1512std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1513 if (Entry32)
1514 Result = Entry32[Index].getHintNameRVA();
1515 else
1516 Result = Entry64[Index].getHintNameRVA();
1517 return std::error_code();
1518}
1519
Rui Ueyama861021f2014-10-02 22:05:29 +00001520std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1521 uint32_t RVA;
1522 if (Entry32) {
1523 if (Entry32[Index].isOrdinal()) {
1524 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001525 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001526 }
1527 RVA = Entry32[Index].getHintNameRVA();
1528 } else {
1529 if (Entry64[Index].isOrdinal()) {
1530 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001531 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001532 }
1533 RVA = Entry64[Index].getHintNameRVA();
1534 }
1535 uintptr_t IntPtr = 0;
1536 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1537 return EC;
1538 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001539 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001540}
1541
Rafael Espindola437b0d52014-07-31 03:12:45 +00001542ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001543ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001544 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001545 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001546 if (EC)
1547 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001548 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001549}
Rui Ueyama74e85132014-11-19 00:18:07 +00001550
1551bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1552 return Header == Other.Header && Index == Other.Index;
1553}
1554
1555void BaseRelocRef::moveNext() {
1556 // Header->BlockSize is the size of the current block, including the
1557 // size of the header itself.
1558 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001559 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001560 if (Size == Header->BlockSize) {
1561 // .reloc contains a list of base relocation blocks. Each block
1562 // consists of the header followed by entries. The header contains
1563 // how many entories will follow. When we reach the end of the
1564 // current block, proceed to the next block.
1565 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1566 reinterpret_cast<const uint8_t *>(Header) + Size);
1567 Index = 0;
1568 } else {
1569 ++Index;
1570 }
1571}
1572
1573std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1574 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1575 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001576 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001577}
1578
1579std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1580 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1581 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001582 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001583}