blob: fc1deeba339a781c84672a3eb58a795216128139 [file] [log] [blame]
Eugene Zelenko86bfc782017-04-19 23:02:10 +00001//===- COFFObjectFile.cpp - COFF object file implementation ---------------===//
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +00002//
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. Spencer1f6e3f92012-03-19 20:27:37 +000014#include "llvm/ADT/ArrayRef.h"
Eugene Zelenko86bfc782017-04-19 23:02:10 +000015#include "llvm/ADT/StringRef.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000016#include "llvm/ADT/Triple.h"
Rui Ueyama7d6e44b2015-06-25 00:07:39 +000017#include "llvm/ADT/iterator_range.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000018#include "llvm/BinaryFormat/COFF.h"
Eugene Zelenko86bfc782017-04-19 23:02:10 +000019#include "llvm/Object/Binary.h"
20#include "llvm/Object/COFF.h"
21#include "llvm/Object/Error.h"
22#include "llvm/Object/ObjectFile.h"
Eric Beckmann8d0cc562017-05-08 02:47:07 +000023#include "llvm/Support/BinaryStreamReader.h"
Eugene Zelenko86bfc782017-04-19 23:02:10 +000024#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include <algorithm>
30#include <cassert>
31#include <cstddef>
32#include <cstdint>
33#include <cstring>
Nico Rieckfa3089b2014-02-22 16:12:20 +000034#include <limits>
Eugene Zelenko86bfc782017-04-19 23:02:10 +000035#include <memory>
36#include <system_error>
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000037
38using namespace llvm;
39using namespace object;
40
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000041using support::ulittle16_t;
42using support::ulittle32_t;
Rui Ueyama3d49ad02014-10-02 22:05:29 +000043using support::ulittle64_t;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000044using support::little16_t;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000045
Michael J. Spencer25b15772011-06-25 17:55:23 +000046// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola548f2b62014-08-19 18:44:46 +000047static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindola1f659322014-06-23 21:53:12 +000048 if (M.getBufferSize() < Size) {
Rui Ueyama671a9e22014-01-16 20:11:48 +000049 EC = object_error::unexpected_eof;
Michael J. Spencer25b15772011-06-25 17:55:23 +000050 return false;
51 }
52 return true;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000053}
54
Rui Ueyama2f6c0482013-07-19 23:23:29 +000055// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
56// Returns unexpected_eof if error.
Rafael Espindola4e2b9222014-06-13 02:24:39 +000057template <typename T>
Rafael Espindola548f2b62014-08-19 18:44:46 +000058static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer465237b2014-11-13 07:42:07 +000059 const void *Ptr,
David Majnemera18e46c2014-11-17 11:17:17 +000060 const uint64_t Size = sizeof(T)) {
Rui Ueyama2f6c0482013-07-19 23:23:29 +000061 uintptr_t Addr = uintptr_t(Ptr);
Benjamin Kramer1a7f03e2017-08-31 12:27:10 +000062 if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
David Majnemerb6ddb3c2014-11-13 08:46:37 +000063 return EC;
Rui Ueyama2f6c0482013-07-19 23:23:29 +000064 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyamaeae46732015-06-09 15:20:42 +000065 return std::error_code();
Michael J. Spencer25b15772011-06-25 17:55:23 +000066}
Michael J. Spencer25b15772011-06-25 17:55:23 +000067
Nico Rieckfa3089b2014-02-22 16:12:20 +000068// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
69// prefixed slashes.
70static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
71 assert(Str.size() <= 6 && "String too long, possible overflow.");
72 if (Str.size() > 6)
73 return true;
74
75 uint64_t Value = 0;
76 while (!Str.empty()) {
77 unsigned CharVal;
78 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
79 CharVal = Str[0] - 'A';
80 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
81 CharVal = Str[0] - 'a' + 26;
82 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
83 CharVal = Str[0] - '0' + 52;
84 else if (Str[0] == '+') // 62
Rui Ueyama204755b2014-02-25 23:49:11 +000085 CharVal = 62;
Nico Rieckfa3089b2014-02-22 16:12:20 +000086 else if (Str[0] == '/') // 63
Rui Ueyama204755b2014-02-25 23:49:11 +000087 CharVal = 63;
Nico Rieckfa3089b2014-02-22 16:12:20 +000088 else
89 return true;
90
91 Value = (Value * 64) + CharVal;
92 Str = Str.substr(1);
93 }
94
95 if (Value > std::numeric_limits<uint32_t>::max())
96 return true;
97
98 Result = static_cast<uint32_t>(Value);
99 return false;
100}
101
David Majnemerb3a86a02014-09-10 12:51:52 +0000102template <typename coff_symbol_type>
103const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
104 const coff_symbol_type *Addr =
105 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000106
David Majnemera18e46c2014-11-17 11:17:17 +0000107 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemerb3a86a02014-09-10 12:51:52 +0000108#ifndef NDEBUG
Michael J. Spencer25b15772011-06-25 17:55:23 +0000109 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama671a9e22014-01-16 20:11:48 +0000110 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer25b15772011-06-25 17:55:23 +0000111
David Majnemerb3a86a02014-09-10 12:51:52 +0000112 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
113 "Symbol did not point to the beginning of a symbol");
114#endif
Michael J. Spencer25b15772011-06-25 17:55:23 +0000115
Rui Ueyama671a9e22014-01-16 20:11:48 +0000116 return Addr;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000117}
118
Rui Ueyama671a9e22014-01-16 20:11:48 +0000119const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
120 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000121
Eugene Zelenko86bfc782017-04-19 23:02:10 +0000122#ifndef NDEBUG
Michael J. Spencer25b15772011-06-25 17:55:23 +0000123 // Verify that the section points to a valid entry in the section table.
David Majnemerb3a86a02014-09-10 12:51:52 +0000124 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000125 report_fatal_error("Section was outside of section table.");
126
Rui Ueyama671a9e22014-01-16 20:11:48 +0000127 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
128 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer25b15772011-06-25 17:55:23 +0000129 "Section did not point to the beginning of a section");
Eugene Zelenko86bfc782017-04-19 23:02:10 +0000130#endif
Michael J. Spencer25b15772011-06-25 17:55:23 +0000131
Rui Ueyama671a9e22014-01-16 20:11:48 +0000132 return Addr;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000133}
134
Rafael Espindolaefdbec82014-01-30 02:49:50 +0000135void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemera18e46c2014-11-17 11:17:17 +0000136 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemerb3a86a02014-09-10 12:51:52 +0000137 if (SymbolTable16) {
138 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
139 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemera18e46c2014-11-17 11:17:17 +0000140 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemerb3a86a02014-09-10 12:51:52 +0000141 } else if (SymbolTable32) {
142 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
143 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemera18e46c2014-11-17 11:17:17 +0000144 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemerb3a86a02014-09-10 12:51:52 +0000145 } else {
146 llvm_unreachable("no symbol table pointer!");
147 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000148}
149
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000150Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemerb3a86a02014-09-10 12:51:52 +0000151 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8a806412015-07-02 20:55:21 +0000152 StringRef Result;
Eric Beckmann8d0cc562017-05-08 02:47:07 +0000153 if (std::error_code EC = getSymbolName(Symb, Result))
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000154 return errorCodeToError(EC);
Rafael Espindola8a806412015-07-02 20:55:21 +0000155 return Result;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000156}
157
Rafael Espindola7b7c81c2015-07-07 17:12:59 +0000158uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
159 return getCOFFSymbol(Ref).getValue();
Rafael Espindola9fa0ab32015-06-24 19:11:10 +0000160}
161
Davide Italiano74a715d2016-11-02 17:32:19 +0000162uint32_t COFFObjectFile::getSymbolAlignment(DataRefImpl Ref) const {
163 // MSVC/link.exe seems to align symbols to the next-power-of-2
164 // up to 32 bytes.
165 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Davide Italiano88b31bf2016-11-11 03:07:45 +0000166 return std::min(uint64_t(32), PowerOf2Ceil(Symb.getValue()));
Davide Italiano74a715d2016-11-02 17:32:19 +0000167}
168
Kevin Enderby317de7c2016-06-24 18:24:42 +0000169Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
Rafael Espindola5954faa2015-07-03 18:19:00 +0000170 uint64_t Result = getSymbolValue(Ref);
David Majnemerb3a86a02014-09-10 12:51:52 +0000171 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemera2715902014-10-31 05:07:00 +0000172 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola9fa0ab32015-06-24 19:11:10 +0000173
174 if (Symb.isAnyUndefined() || Symb.isCommon() ||
175 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindola5954faa2015-07-03 18:19:00 +0000176 return Result;
David Majnemera2715902014-10-31 05:07:00 +0000177
Rafael Espindola9a5f9622015-06-24 17:08:44 +0000178 const coff_section *Section = nullptr;
179 if (std::error_code EC = getSection(SectionNumber, Section))
Kevin Enderby317de7c2016-06-24 18:24:42 +0000180 return errorCodeToError(EC);
Rafael Espindola9fa0ab32015-06-24 19:11:10 +0000181 Result += Section->VirtualAddress;
Reid Klecknerc136e552015-07-31 16:14:22 +0000182
183 // The section VirtualAddress does not include ImageBase, and we want to
184 // return virtual addresses.
Reid Kleckner00fda732015-10-09 00:15:08 +0000185 Result += getImageBase();
Reid Klecknerc136e552015-07-31 16:14:22 +0000186
Rafael Espindola5954faa2015-07-03 18:19:00 +0000187 return Result;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000188}
189
Kevin Enderbya486dca2016-05-02 20:28:12 +0000190Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemerb3a86a02014-09-10 12:51:52 +0000191 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemera2715902014-10-31 05:07:00 +0000192 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemerb3a86a02014-09-10 12:51:52 +0000193
Peter Collingbournef4fa49e2015-08-06 05:26:35 +0000194 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
195 return SymbolRef::ST_Function;
Rafael Espindola50bea402015-06-26 12:18:49 +0000196 if (Symb.isAnyUndefined())
197 return SymbolRef::ST_Unknown;
Rafael Espindola50bea402015-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 Kramerac241fe2011-09-14 01:22:52 +0000211}
212
Rafael Espindolad8324e62014-01-31 20:57:12 +0000213uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemerb3a86a02014-09-10 12:51:52 +0000214 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad8324e62014-01-31 20:57:12 +0000215 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000216
David Majnemera2715902014-10-31 05:07:00 +0000217 if (Symb.isExternal() || Symb.isWeakExternal())
Lang Hamesb4125012016-01-25 01:21:45 +0000218 Result |= SymbolRef::SF_Global;
David Meyerc46255a2012-02-28 23:47:53 +0000219
Martin Storsjo3631b662018-07-20 20:48:29 +0000220 if (const coff_aux_weak_external *AWE = Symb.getWeakExternal()) {
David Meyerc46255a2012-02-28 23:47:53 +0000221 Result |= SymbolRef::SF_Weak;
Martin Storsjo3631b662018-07-20 20:48:29 +0000222 if (AWE->Characteristics != COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS)
223 Result |= SymbolRef::SF_Undefined;
Martell Maloned8886462017-07-18 21:26:38 +0000224 }
David Meyerc46255a2012-02-28 23:47:53 +0000225
David Majnemerb3a86a02014-09-10 12:51:52 +0000226 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyerc46255a2012-02-28 23:47:53 +0000227 Result |= SymbolRef::SF_Absolute;
228
David Majnemera2715902014-10-31 05:07:00 +0000229 if (Symb.isFileRecord())
230 Result |= SymbolRef::SF_FormatSpecific;
231
232 if (Symb.isSectionDefinition())
233 Result |= SymbolRef::SF_FormatSpecific;
234
235 if (Symb.isCommon())
236 Result |= SymbolRef::SF_Common;
237
Martin Storsjo3631b662018-07-20 20:48:29 +0000238 if (Symb.isUndefined())
David Majnemera2715902014-10-31 05:07:00 +0000239 Result |= SymbolRef::SF_Undefined;
240
Rafael Espindolad8324e62014-01-31 20:57:12 +0000241 return Result;
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000242}
243
Rafael Espindola821b06f2015-06-24 10:20:30 +0000244uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemera2715902014-10-31 05:07:00 +0000245 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola821b06f2015-06-24 10:20:30 +0000246 return Symb.getValue();
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000247}
248
Kevin Enderbya486dca2016-05-02 20:28:12 +0000249Expected<section_iterator>
Rafael Espindolae84d8c12015-08-07 23:27:14 +0000250COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemerb3a86a02014-09-10 12:51:52 +0000251 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolae84d8c12015-08-07 23:27:14 +0000252 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
253 return section_end();
254 const coff_section *Sec = nullptr;
255 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Kevin Enderbya486dca2016-05-02 20:28:12 +0000256 return errorCodeToError(EC);
Rafael Espindolae84d8c12015-08-07 23:27:14 +0000257 DataRefImpl Ret;
258 Ret.p = reinterpret_cast<uintptr_t>(Sec);
259 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000260}
261
Rafael Espindolaa3af3472015-06-24 19:57:32 +0000262unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
263 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
264 return Symb.getSectionNumber();
265}
266
Rafael Espindolaefdbec82014-01-30 02:49:50 +0000267void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000268 const coff_section *Sec = toSec(Ref);
269 Sec += 1;
270 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000271}
272
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000273std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
274 StringRef &Result) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000275 const coff_section *Sec = toSec(Ref);
276 return getSectionName(Sec, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000277}
278
Rafael Espindola8175be52014-10-08 15:28:58 +0000279uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000280 const coff_section *Sec = toSec(Ref);
David Majnemer0229c3b2015-07-31 17:40:24 +0000281 uint64_t Result = Sec->VirtualAddress;
282
283 // The section VirtualAddress does not include ImageBase, and we want to
284 // return virtual addresses.
Reid Kleckner00fda732015-10-09 00:15:08 +0000285 Result += getImageBase();
David Majnemer0229c3b2015-07-31 17:40:24 +0000286 return Result;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000287}
288
George Rimarb5ac7002017-05-27 18:10:23 +0000289uint64_t COFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
290 return toSec(Sec) - SectionTable;
291}
292
Rafael Espindola8175be52014-10-08 15:28:58 +0000293uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemer06f1f352014-10-09 08:42:31 +0000294 return getSectionSize(toSec(Ref));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000295}
296
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000297std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
298 StringRef &Result) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000299 const coff_section *Sec = toSec(Ref);
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000300 ArrayRef<uint8_t> Res;
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000301 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000302 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
303 return EC;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000304}
305
Rafael Espindola8175be52014-10-08 15:28:58 +0000306uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000307 const coff_section *Sec = toSec(Ref);
David Majnemer30abf162016-03-17 16:55:18 +0000308 return Sec->getAlignment();
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000309}
310
George Rimar5ad80b22016-05-24 12:48:46 +0000311bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
312 return false;
313}
314
Rafael Espindola8175be52014-10-08 15:28:58 +0000315bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000316 const coff_section *Sec = toSec(Ref);
Rafael Espindola8175be52014-10-08 15:28:58 +0000317 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000318}
319
Rafael Espindola8175be52014-10-08 15:28:58 +0000320bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000321 const coff_section *Sec = toSec(Ref);
Rafael Espindola8175be52014-10-08 15:28:58 +0000322 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000323}
324
Rafael Espindola8175be52014-10-08 15:28:58 +0000325bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000326 const coff_section *Sec = toSec(Ref);
David Majnemer2c084392015-03-07 20:21:27 +0000327 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
328 COFF::IMAGE_SCN_MEM_READ |
329 COFF::IMAGE_SCN_MEM_WRITE;
330 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000331}
332
Rafael Espindolaa3af3472015-06-24 19:57:32 +0000333unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
334 uintptr_t Offset =
335 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
336 assert((Offset % sizeof(coff_section)) == 0);
337 return (Offset / sizeof(coff_section)) + 1;
338}
339
Rafael Espindola8175be52014-10-08 15:28:58 +0000340bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000341 const coff_section *Sec = toSec(Ref);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000342 // In COFF, a virtual section won't have any in-file
David Majnemer2c084392015-03-07 20:21:27 +0000343 // content, so the file pointer to the content will be zero.
344 return Sec->PointerToRawData == 0;
Preston Gurdc68dda82012-04-12 20:13:57 +0000345}
346
David Majnemerb6ddb3c2014-11-13 08:46:37 +0000347static uint32_t getNumberOfRelocations(const coff_section *Sec,
348 MemoryBufferRef M, const uint8_t *base) {
349 // The field for the number of relocations in COFF section table is only
350 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
351 // NumberOfRelocations field, and the actual relocation count is stored in the
352 // VirtualAddress field in the first relocation entry.
353 if (Sec->hasExtendedRelocations()) {
354 const coff_relocation *FirstReloc;
355 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
356 base + Sec->PointerToRelocations)))
357 return 0;
Rui Ueyamad2077b02014-11-26 22:17:25 +0000358 // -1 to exclude this first relocation entry.
359 return FirstReloc->VirtualAddress - 1;
David Majnemerb6ddb3c2014-11-13 08:46:37 +0000360 }
361 return Sec->NumberOfRelocations;
362}
363
David Majnemera98fcec2014-11-13 09:50:18 +0000364static const coff_relocation *
365getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
366 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
367 if (!NumRelocs)
368 return nullptr;
369 auto begin = reinterpret_cast<const coff_relocation *>(
370 Base + Sec->PointerToRelocations);
371 if (Sec->hasExtendedRelocations()) {
372 // Skip the first relocation entry repurposed to store the number of
373 // relocations.
374 begin++;
375 }
Benjamin Kramer1a7f03e2017-08-31 12:27:10 +0000376 if (Binary::checkOffset(M, uintptr_t(begin),
377 sizeof(coff_relocation) * NumRelocs))
David Majnemera98fcec2014-11-13 09:50:18 +0000378 return nullptr;
379 return begin;
380}
381
Rui Ueyama671a9e22014-01-16 20:11:48 +0000382relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
383 const coff_section *Sec = toSec(Ref);
David Majnemera98fcec2014-11-13 09:50:18 +0000384 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola15994692015-07-06 14:26:07 +0000385 if (begin && Sec->VirtualAddress != 0)
386 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama671a9e22014-01-16 20:11:48 +0000387 DataRefImpl Ret;
David Majnemera98fcec2014-11-13 09:50:18 +0000388 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama671a9e22014-01-16 20:11:48 +0000389 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000390}
391
Rui Ueyama671a9e22014-01-16 20:11:48 +0000392relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
393 const coff_section *Sec = toSec(Ref);
David Majnemera98fcec2014-11-13 09:50:18 +0000394 const coff_relocation *I = getFirstReloc(Sec, Data, base());
395 if (I)
396 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama671a9e22014-01-16 20:11:48 +0000397 DataRefImpl Ret;
David Majnemera98fcec2014-11-13 09:50:18 +0000398 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama671a9e22014-01-16 20:11:48 +0000399 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000400}
401
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000402// Initialize the pointer to the symbol table.
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000403std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemerb3a86a02014-09-10 12:51:52 +0000404 if (COFFHeader)
David Majnemera18e46c2014-11-17 11:17:17 +0000405 if (std::error_code EC = getObject(
406 SymbolTable16, Data, base() + getPointerToSymbolTable(),
407 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemerb3a86a02014-09-10 12:51:52 +0000408 return EC;
409
410 if (COFFBigObjHeader)
David Majnemera18e46c2014-11-17 11:17:17 +0000411 if (std::error_code EC = getObject(
412 SymbolTable32, Data, base() + getPointerToSymbolTable(),
413 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemerb3a86a02014-09-10 12:51:52 +0000414 return EC;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000415
416 // Find string table. The first four byte of the string table contains the
417 // total size of the string table, including the size field itself. If the
418 // string table is empty, the value of the first four byte would be 4.
David Majnemer237544b2014-11-14 08:15:42 +0000419 uint32_t StringTableOffset = getPointerToSymbolTable() +
420 getNumberOfSymbols() * getSymbolTableEntrySize();
421 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000422 const ulittle32_t *StringTableSizePtr;
Rafael Espindola548f2b62014-08-19 18:44:46 +0000423 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama671a9e22014-01-16 20:11:48 +0000424 return EC;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000425 StringTableSize = *StringTableSizePtr;
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000426 if (std::error_code EC =
Rafael Espindola548f2b62014-08-19 18:44:46 +0000427 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama671a9e22014-01-16 20:11:48 +0000428 return EC;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000429
Nico Rieck0a91f482014-02-26 19:51:44 +0000430 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
431 // tools like cvtres write a size of 0 for an empty table instead of 4.
432 if (StringTableSize < 4)
433 StringTableSize = 4;
434
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000435 // Check that the string table is null terminated if has any in it.
Nico Rieck0a91f482014-02-26 19:51:44 +0000436 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000437 return object_error::parse_failed;
Rui Ueyamaeae46732015-06-09 15:20:42 +0000438 return std::error_code();
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000439}
440
Reid Kleckner00fda732015-10-09 00:15:08 +0000441uint64_t COFFObjectFile::getImageBase() const {
Reid Kleckner27a9e1d2015-10-09 00:15:01 +0000442 if (PE32Header)
Reid Kleckner00fda732015-10-09 00:15:08 +0000443 return PE32Header->ImageBase;
Reid Kleckner27a9e1d2015-10-09 00:15:01 +0000444 else if (PE32PlusHeader)
Reid Kleckner00fda732015-10-09 00:15:08 +0000445 return PE32PlusHeader->ImageBase;
446 // This actually comes up in practice.
447 return 0;
Reid Kleckner27a9e1d2015-10-09 00:15:01 +0000448}
449
Rui Ueyama6fac32b2014-02-20 06:51:07 +0000450// Returns the file offset for the given VA.
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000451std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner00fda732015-10-09 00:15:08 +0000452 uint64_t ImageBase = getImageBase();
Rui Ueyamaf41901d2014-02-20 19:14:56 +0000453 uint64_t Rva = Addr - ImageBase;
454 assert(Rva <= UINT32_MAX);
455 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama6fac32b2014-02-20 06:51:07 +0000456}
457
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000458// Returns the file offset for the given RVA.
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000459std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov5b645792014-03-18 06:53:02 +0000460 for (const SectionRef &S : sections()) {
461 const coff_section *Section = getCOFFSection(S);
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000462 uint32_t SectionStart = Section->VirtualAddress;
463 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama6fac32b2014-02-20 06:51:07 +0000464 if (SectionStart <= Addr && Addr < SectionEnd) {
465 uint32_t Offset = Addr - SectionStart;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000466 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyamaeae46732015-06-09 15:20:42 +0000467 return std::error_code();
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000468 }
469 }
470 return object_error::parse_failed;
471}
472
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000473std::error_code
474COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size,
475 ArrayRef<uint8_t> &Contents) const {
476 for (const SectionRef &S : sections()) {
477 const coff_section *Section = getCOFFSection(S);
478 uint32_t SectionStart = Section->VirtualAddress;
479 // Check if this RVA is within the section bounds. Be careful about integer
480 // overflow.
481 uint32_t OffsetIntoSection = RVA - SectionStart;
482 if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
483 Size <= Section->VirtualSize - OffsetIntoSection) {
484 uintptr_t Begin =
485 uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
486 Contents =
487 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
488 return std::error_code();
489 }
490 }
491 return object_error::parse_failed;
492}
493
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000494// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
495// table entry.
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000496std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
497 StringRef &Name) const {
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000498 uintptr_t IntPtr = 0;
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000499 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama671a9e22014-01-16 20:11:48 +0000500 return EC;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000501 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
502 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
503 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyamaeae46732015-06-09 15:20:42 +0000504 return std::error_code();
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000505}
506
Saleem Abdulrasoolb79146f2016-08-09 00:25:12 +0000507std::error_code
508COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir,
509 const codeview::DebugInfo *&PDBInfo,
510 StringRef &PDBFileName) const {
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000511 ArrayRef<uint8_t> InfoBytes;
512 if (std::error_code EC = getRvaAndSizeAsBytes(
513 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
514 return EC;
Saleem Abdulrasoolb79146f2016-08-09 00:25:12 +0000515 if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000516 return object_error::parse_failed;
Saleem Abdulrasoolb79146f2016-08-09 00:25:12 +0000517 PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
518 InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000519 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
520 InfoBytes.size());
521 // Truncate the name at the first null byte. Ignore any padding.
522 PDBFileName = PDBFileName.split('\0').first;
523 return std::error_code();
524}
525
Saleem Abdulrasoolb79146f2016-08-09 00:25:12 +0000526std::error_code
527COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo,
528 StringRef &PDBFileName) const {
Reid Kleckner1ac3f3e2016-06-03 20:25:09 +0000529 for (const debug_directory &D : debug_directories())
530 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW)
531 return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
532 // If we get here, there is no PDB info to return.
533 PDBInfo = nullptr;
534 PDBFileName = StringRef();
535 return std::error_code();
536}
537
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000538// Find the import table.
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000539std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000540 // First, we get the RVA of the import table. If the file lacks a pointer to
541 // the import table, do nothing.
542 const data_directory *DataEntry;
543 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyamaeae46732015-06-09 15:20:42 +0000544 return std::error_code();
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000545
546 // Do nothing if the pointer to import table is NULL.
547 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyamaeae46732015-06-09 15:20:42 +0000548 return std::error_code();
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000549
550 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000551
552 // Find the section that contains the RVA. This is needed because the RVA is
553 // the import table's memory address which is different from its file offset.
554 uintptr_t IntPtr = 0;
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000555 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama671a9e22014-01-16 20:11:48 +0000556 return EC;
David Majnemer6ea11a42016-06-26 04:36:32 +0000557 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
558 return EC;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000559 ImportDirectory = reinterpret_cast<
David Majnemere2fac432016-07-31 19:25:21 +0000560 const coff_import_directory_table_entry *>(IntPtr);
Rui Ueyamaeae46732015-06-09 15:20:42 +0000561 return std::error_code();
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000562}
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000563
Rui Ueyama82066462014-10-03 00:41:58 +0000564// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
565std::error_code COFFObjectFile::initDelayImportTablePtr() {
566 const data_directory *DataEntry;
567 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyamaeae46732015-06-09 15:20:42 +0000568 return std::error_code();
Rui Ueyama82066462014-10-03 00:41:58 +0000569 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyamaeae46732015-06-09 15:20:42 +0000570 return std::error_code();
Rui Ueyama82066462014-10-03 00:41:58 +0000571
572 uint32_t RVA = DataEntry->RelativeVirtualAddress;
573 NumberOfDelayImportDirectory = DataEntry->Size /
574 sizeof(delay_import_directory_table_entry) - 1;
575
576 uintptr_t IntPtr = 0;
577 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
578 return EC;
579 DelayImportDirectory = reinterpret_cast<
580 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyamaeae46732015-06-09 15:20:42 +0000581 return std::error_code();
Rui Ueyama82066462014-10-03 00:41:58 +0000582}
583
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000584// Find the export table.
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000585std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000586 // First, we get the RVA of the export table. If the file lacks a pointer to
587 // the export table, do nothing.
588 const data_directory *DataEntry;
589 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyamaeae46732015-06-09 15:20:42 +0000590 return std::error_code();
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000591
592 // Do nothing if the pointer to export table is NULL.
593 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyamaeae46732015-06-09 15:20:42 +0000594 return std::error_code();
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000595
596 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
597 uintptr_t IntPtr = 0;
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000598 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000599 return EC;
Rui Ueyama64382602014-01-17 22:11:27 +0000600 ExportDirectory =
601 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaeae46732015-06-09 15:20:42 +0000602 return std::error_code();
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000603}
604
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000605std::error_code COFFObjectFile::initBaseRelocPtr() {
606 const data_directory *DataEntry;
607 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyamaeae46732015-06-09 15:20:42 +0000608 return std::error_code();
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000609 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyamaeae46732015-06-09 15:20:42 +0000610 return std::error_code();
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000611
612 uintptr_t IntPtr = 0;
613 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
614 return EC;
615 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
616 IntPtr);
617 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
618 IntPtr + DataEntry->Size);
Nico Weber1f9e6bb2018-09-05 18:01:04 +0000619 // FIXME: Verify the section containing BaseRelocHeader has at least
620 // DataEntry->Size bytes after DataEntry->RelativeVirtualAddress.
Rui Ueyamaeae46732015-06-09 15:20:42 +0000621 return std::error_code();
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000622}
623
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000624std::error_code COFFObjectFile::initDebugDirectoryPtr() {
625 // Get the RVA of the debug directory. Do nothing if it does not exist.
626 const data_directory *DataEntry;
627 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
628 return std::error_code();
629
630 // Do nothing if the RVA is NULL.
631 if (DataEntry->RelativeVirtualAddress == 0)
632 return std::error_code();
633
634 // Check that the size is a multiple of the entry size.
635 if (DataEntry->Size % sizeof(debug_directory) != 0)
636 return object_error::parse_failed;
637
638 uintptr_t IntPtr = 0;
639 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
640 return EC;
641 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
Nico Weber1f9e6bb2018-09-05 18:01:04 +0000642 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(
643 IntPtr + DataEntry->Size);
644 // FIXME: Verify the section containing DebugDirectoryBegin has at least
645 // DataEntry->Size bytes after DataEntry->RelativeVirtualAddress.
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000646 return std::error_code();
647}
648
Reid Kleckner367f21d2017-06-22 01:10:29 +0000649std::error_code COFFObjectFile::initLoadConfigPtr() {
650 // Get the RVA of the debug directory. Do nothing if it does not exist.
651 const data_directory *DataEntry;
652 if (getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataEntry))
653 return std::error_code();
654
655 // Do nothing if the RVA is NULL.
656 if (DataEntry->RelativeVirtualAddress == 0)
657 return std::error_code();
658 uintptr_t IntPtr = 0;
659 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
660 return EC;
661
662 LoadConfig = (const void *)IntPtr;
663 return std::error_code();
664}
665
Rafael Espindola548f2b62014-08-19 18:44:46 +0000666COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
667 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemerb3a86a02014-09-10 12:51:52 +0000668 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
669 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
670 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
David Majnemer6ea11a42016-06-26 04:36:32 +0000671 ImportDirectory(nullptr),
Rui Ueyama82066462014-10-03 00:41:58 +0000672 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000673 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
674 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000675 // Check that we at least have enough room for a header.
Rafael Espindola548f2b62014-08-19 18:44:46 +0000676 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindola1f659322014-06-23 21:53:12 +0000677 return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000678
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000679 // The current location in the file where we are looking at.
680 uint64_t CurPtr = 0;
681
682 // PE header is optional and is present only in executables. If it exists,
683 // it is placed right after COFF header.
Rui Ueyama671a9e22014-01-16 20:11:48 +0000684 bool HasPEHeader = false;
Eric Christopher539d8d82011-04-03 22:53:19 +0000685
Michael J. Spencer25b15772011-06-25 17:55:23 +0000686 // Check if this is a PE/COFF file.
David Majnemer47870592014-11-05 06:24:35 +0000687 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000688 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
689 // PE signature to find 'normal' COFF header.
David Majnemer47870592014-11-05 06:24:35 +0000690 const auto *DH = reinterpret_cast<const dos_header *>(base());
691 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
692 CurPtr = DH->AddressOfNewExeHeader;
693 // Check the PE magic bytes. ("PE\0\0")
694 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
695 EC = object_error::parse_failed;
696 return;
697 }
698 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
699 HasPEHeader = true;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000700 }
Eric Christopher539d8d82011-04-03 22:53:19 +0000701 }
702
Rafael Espindola548f2b62014-08-19 18:44:46 +0000703 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000704 return;
David Majnemerb3a86a02014-09-10 12:51:52 +0000705
706 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
707 // import libraries share a common prefix but bigobj is more restrictive.
708 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
709 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
710 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
711 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
712 return;
713
714 // Verify that we are dealing with bigobj.
715 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
716 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
717 sizeof(COFF::BigObjMagic)) == 0) {
718 COFFHeader = nullptr;
719 CurPtr += sizeof(coff_bigobj_file_header);
720 } else {
721 // It's not a bigobj.
722 COFFBigObjHeader = nullptr;
723 }
724 }
725 if (COFFHeader) {
726 // The prior checkSize call may have failed. This isn't a hard error
727 // because we were just trying to sniff out bigobj.
Rui Ueyamaeae46732015-06-09 15:20:42 +0000728 EC = std::error_code();
David Majnemerb3a86a02014-09-10 12:51:52 +0000729 CurPtr += sizeof(coff_file_header);
730
731 if (COFFHeader->isImportLibrary())
732 return;
733 }
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000734
Rui Ueyama671a9e22014-01-16 20:11:48 +0000735 if (HasPEHeader) {
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000736 const pe32_header *Header;
Rafael Espindola548f2b62014-08-19 18:44:46 +0000737 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000738 return;
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000739
740 const uint8_t *DataDirAddr;
741 uint64_t DataDirSize;
David Majnemer47870592014-11-05 06:24:35 +0000742 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000743 PE32Header = Header;
744 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
745 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer47870592014-11-05 06:24:35 +0000746 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000747 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
748 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
749 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
750 } else {
751 // It's neither PE32 nor PE32+.
752 EC = object_error::parse_failed;
753 return;
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000754 }
Rafael Espindola548f2b62014-08-19 18:44:46 +0000755 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000756 return;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000757 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000758
Rui Ueyama837032f2016-08-11 22:02:44 +0000759 if (COFFHeader)
760 CurPtr += COFFHeader->SizeOfOptionalHeader;
761
Rafael Espindola548f2b62014-08-19 18:44:46 +0000762 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemera18e46c2014-11-17 11:17:17 +0000763 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola3d218152014-01-21 23:06:54 +0000764 return;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000765
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000766 // Initialize the pointer to the symbol table.
David Majnemera18e46c2014-11-17 11:17:17 +0000767 if (getPointerToSymbolTable() != 0) {
David Majnemer99edeba2016-08-30 20:20:24 +0000768 if ((EC = initSymbolTablePtr())) {
769 SymbolTable16 = nullptr;
770 SymbolTable32 = nullptr;
771 StringTable = nullptr;
772 StringTableSize = 0;
773 }
David Majnemera18e46c2014-11-17 11:17:17 +0000774 } else {
775 // We had better not have any symbols if we don't have a symbol table.
776 if (getNumberOfSymbols() != 0) {
777 EC = object_error::parse_failed;
778 return;
779 }
780 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000781
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000782 // Initialize the pointer to the beginning of the import table.
Rui Ueyama671a9e22014-01-16 20:11:48 +0000783 if ((EC = initImportTablePtr()))
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000784 return;
Rui Ueyama82066462014-10-03 00:41:58 +0000785 if ((EC = initDelayImportTablePtr()))
786 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000787
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000788 // Initialize the pointer to the export table.
Rui Ueyama671a9e22014-01-16 20:11:48 +0000789 if ((EC = initExportTablePtr()))
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000790 return;
791
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000792 // Initialize the pointer to the base relocation table.
793 if ((EC = initBaseRelocPtr()))
794 return;
795
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000796 // Initialize the pointer to the export table.
797 if ((EC = initDebugDirectoryPtr()))
798 return;
799
Reid Kleckner367f21d2017-06-22 01:10:29 +0000800 if ((EC = initLoadConfigPtr()))
801 return;
802
Rui Ueyamaeae46732015-06-09 15:20:42 +0000803 EC = std::error_code();
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000804}
805
Peter Collingbourneb9b39082016-11-22 03:38:40 +0000806basic_symbol_iterator COFFObjectFile::symbol_begin() const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000807 DataRefImpl Ret;
David Majnemerb3a86a02014-09-10 12:51:52 +0000808 Ret.p = getSymbolTable();
Rafael Espindola91f86b72014-02-21 20:10:59 +0000809 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000810}
811
Peter Collingbourneb9b39082016-11-22 03:38:40 +0000812basic_symbol_iterator COFFObjectFile::symbol_end() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000813 // The symbol table ends where the string table begins.
Rui Ueyama671a9e22014-01-16 20:11:48 +0000814 DataRefImpl Ret;
815 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindola91f86b72014-02-21 20:10:59 +0000816 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000817}
818
Rui Ueyama29552222013-09-27 21:47:05 +0000819import_directory_iterator COFFObjectFile::import_directory_begin() const {
David Majnemer6ea11a42016-06-26 04:36:32 +0000820 if (!ImportDirectory)
821 return import_directory_end();
David Majnemere2fac432016-07-31 19:25:21 +0000822 if (ImportDirectory->isNull())
David Majnemer6ea11a42016-06-26 04:36:32 +0000823 return import_directory_end();
Rui Ueyama6a8151d2014-01-16 03:13:19 +0000824 return import_directory_iterator(
825 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000826}
827
Rui Ueyama29552222013-09-27 21:47:05 +0000828import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyama6a8151d2014-01-16 03:13:19 +0000829 return import_directory_iterator(
David Majnemer6ea11a42016-06-26 04:36:32 +0000830 ImportDirectoryEntryRef(nullptr, -1, this));
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000831}
David Meyer97f77872012-03-01 22:19:54 +0000832
Rui Ueyama82066462014-10-03 00:41:58 +0000833delay_import_directory_iterator
834COFFObjectFile::delay_import_directory_begin() const {
835 return delay_import_directory_iterator(
836 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
837}
838
839delay_import_directory_iterator
840COFFObjectFile::delay_import_directory_end() const {
841 return delay_import_directory_iterator(
842 DelayImportDirectoryEntryRef(
843 DelayImportDirectory, NumberOfDelayImportDirectory, this));
844}
845
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000846export_directory_iterator COFFObjectFile::export_directory_begin() const {
847 return export_directory_iterator(
848 ExportDirectoryEntryRef(ExportDirectory, 0, this));
849}
850
851export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper0b6cb712014-04-15 06:32:26 +0000852 if (!ExportDirectory)
853 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama671a9e22014-01-16 20:11:48 +0000854 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000855 ExportDirectory->AddressTableEntries, this);
Rui Ueyama671a9e22014-01-16 20:11:48 +0000856 return export_directory_iterator(Ref);
Rui Ueyamafb432ac2014-01-16 07:05:49 +0000857}
858
Rafael Espindolaa40b3522014-02-10 20:24:04 +0000859section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000860 DataRefImpl Ret;
861 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
862 return section_iterator(SectionRef(Ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000863}
864
Rafael Espindolaa40b3522014-02-10 20:24:04 +0000865section_iterator COFFObjectFile::section_end() const {
Rui Ueyama671a9e22014-01-16 20:11:48 +0000866 DataRefImpl Ret;
David Majnemerb3a86a02014-09-10 12:51:52 +0000867 int NumSections =
868 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama671a9e22014-01-16 20:11:48 +0000869 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
870 return section_iterator(SectionRef(Ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000871}
872
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000873base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
874 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
875}
876
877base_reloc_iterator COFFObjectFile::base_reloc_end() const {
878 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
879}
880
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000881uint8_t COFFObjectFile::getBytesInAddress() const {
Martin Storsjo9f0e6a12017-06-30 07:02:13 +0000882 return getArch() == Triple::x86_64 || getArch() == Triple::aarch64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000883}
884
885StringRef COFFObjectFile::getFileFormatName() const {
David Majnemerb3a86a02014-09-10 12:51:52 +0000886 switch(getMachine()) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000887 case COFF::IMAGE_FILE_MACHINE_I386:
888 return "COFF-i386";
889 case COFF::IMAGE_FILE_MACHINE_AMD64:
890 return "COFF-x86-64";
Saleem Abdulrasoolb0f12df2014-03-13 07:02:35 +0000891 case COFF::IMAGE_FILE_MACHINE_ARMNT:
892 return "COFF-ARM";
Martell Malone751664f2015-07-28 16:18:17 +0000893 case COFF::IMAGE_FILE_MACHINE_ARM64:
894 return "COFF-ARM64";
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000895 default:
896 return "COFF-<unknown arch>";
897 }
898}
899
Zachary Turnerece9b232017-12-14 22:07:03 +0000900Triple::ArchType COFFObjectFile::getArch() const {
David Majnemerb3a86a02014-09-10 12:51:52 +0000901 switch (getMachine()) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000902 case COFF::IMAGE_FILE_MACHINE_I386:
903 return Triple::x86;
904 case COFF::IMAGE_FILE_MACHINE_AMD64:
905 return Triple::x86_64;
Saleem Abdulrasoolb0f12df2014-03-13 07:02:35 +0000906 case COFF::IMAGE_FILE_MACHINE_ARMNT:
907 return Triple::thumb;
Martell Malone751664f2015-07-28 16:18:17 +0000908 case COFF::IMAGE_FILE_MACHINE_ARM64:
909 return Triple::aarch64;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000910 default:
911 return Triple::UnknownArch;
912 }
913}
914
Paul Semeld069a212018-07-04 15:25:03 +0000915Expected<uint64_t> COFFObjectFile::getStartAddress() const {
916 if (PE32Header)
917 return PE32Header->AddressOfEntryPoint;
918 return 0;
919}
920
Rui Ueyamaf02f03c2014-10-09 02:16:38 +0000921iterator_range<import_directory_iterator>
922COFFObjectFile::import_directories() const {
923 return make_range(import_directory_begin(), import_directory_end());
924}
925
926iterator_range<delay_import_directory_iterator>
927COFFObjectFile::delay_import_directories() const {
928 return make_range(delay_import_directory_begin(),
929 delay_import_directory_end());
930}
931
932iterator_range<export_directory_iterator>
933COFFObjectFile::export_directories() const {
934 return make_range(export_directory_begin(), export_directory_end());
935}
936
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000937iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
938 return make_range(base_reloc_begin(), base_reloc_end());
939}
940
Martin Storsjoe318fd62018-12-19 07:24:38 +0000941std::error_code
942COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
943 Res = COFFHeader;
944 return std::error_code();
945}
946
947std::error_code
948COFFObjectFile::getCOFFBigObjHeader(const coff_bigobj_file_header *&Res) const {
949 Res = COFFBigObjHeader;
950 return std::error_code();
951}
952
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000953std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000954 Res = PE32Header;
Rui Ueyamaeae46732015-06-09 15:20:42 +0000955 return std::error_code();
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000956}
957
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000958std::error_code
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000959COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
960 Res = PE32PlusHeader;
Rui Ueyamaeae46732015-06-09 15:20:42 +0000961 return std::error_code();
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000962}
963
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000964std::error_code
965COFFObjectFile::getDataDirectory(uint32_t Index,
966 const data_directory *&Res) const {
Hiroshi Inoueef1bc2d2018-04-12 05:53:20 +0000967 // Error if there's no data directory or the index is out of range.
David Majnemer237544b2014-11-14 08:15:42 +0000968 if (!DataDirectory) {
969 Res = nullptr;
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000970 return object_error::parse_failed;
David Majnemer237544b2014-11-14 08:15:42 +0000971 }
Rui Ueyama1e839eb2014-01-26 04:15:52 +0000972 assert(PE32Header || PE32PlusHeader);
973 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
974 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemer237544b2014-11-14 08:15:42 +0000975 if (Index >= NumEnt) {
976 Res = nullptr;
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000977 return object_error::parse_failed;
David Majnemer237544b2014-11-14 08:15:42 +0000978 }
Rui Ueyama671a9e22014-01-16 20:11:48 +0000979 Res = &DataDirectory[Index];
Rui Ueyamaeae46732015-06-09 15:20:42 +0000980 return std::error_code();
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000981}
982
Rafael Espindola4e2b9222014-06-13 02:24:39 +0000983std::error_code COFFObjectFile::getSection(int32_t Index,
984 const coff_section *&Result) const {
David Majnemera18e46c2014-11-17 11:17:17 +0000985 Result = nullptr;
Rui Ueyamacae25dc2014-03-18 23:37:53 +0000986 if (COFF::isReservedSectionNumber(Index))
Rui Ueyamaeae46732015-06-09 15:20:42 +0000987 return std::error_code();
David Majnemera18e46c2014-11-17 11:17:17 +0000988 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000989 // We already verified the section table data, so no need to check again.
Rui Ueyama671a9e22014-01-16 20:11:48 +0000990 Result = SectionTable + (Index - 1);
Rui Ueyamaeae46732015-06-09 15:20:42 +0000991 return std::error_code();
David Majnemera18e46c2014-11-17 11:17:17 +0000992 }
993 return object_error::parse_failed;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000994}
995
Paul Semel94b09402018-07-11 10:00:29 +0000996std::error_code COFFObjectFile::getSection(StringRef SectionName,
997 const coff_section *&Result) const {
998 Result = nullptr;
999 StringRef SecName;
1000 for (const SectionRef &Section : sections()) {
1001 if (std::error_code E = Section.getName(SecName))
1002 return E;
1003 if (SecName == SectionName) {
1004 Result = getCOFFSection(Section);
1005 return std::error_code();
1006 }
1007 }
1008 return object_error::parse_failed;
1009}
1010
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001011std::error_code COFFObjectFile::getString(uint32_t Offset,
1012 StringRef &Result) const {
Michael J. Spencer25b15772011-06-25 17:55:23 +00001013 if (StringTableSize <= 4)
1014 // Tried to get a string from an empty string table.
1015 return object_error::parse_failed;
Rui Ueyama671a9e22014-01-16 20:11:48 +00001016 if (Offset >= StringTableSize)
Michael J. Spencer25b15772011-06-25 17:55:23 +00001017 return object_error::unexpected_eof;
Rui Ueyama671a9e22014-01-16 20:11:48 +00001018 Result = StringRef(StringTable + Offset);
Rui Ueyamaeae46732015-06-09 15:20:42 +00001019 return std::error_code();
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +00001020}
1021
David Majnemerb3a86a02014-09-10 12:51:52 +00001022std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001023 StringRef &Res) const {
Rui Ueyama13b58ec2015-06-30 00:03:56 +00001024 return getSymbolName(Symbol.getGeneric(), Res);
1025}
1026
1027std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
1028 StringRef &Res) const {
Michael J. Spencer0e752cb2011-10-17 23:53:56 +00001029 // Check for string table entry. First 4 bytes are 0.
Rui Ueyama13b58ec2015-06-30 00:03:56 +00001030 if (Symbol->Name.Offset.Zeroes == 0) {
1031 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama671a9e22014-01-16 20:11:48 +00001032 return EC;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001033 return std::error_code();
Michael J. Spencer0e752cb2011-10-17 23:53:56 +00001034 }
1035
Rui Ueyama13b58ec2015-06-30 00:03:56 +00001036 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer0e752cb2011-10-17 23:53:56 +00001037 // Null terminated, let ::strlen figure out the length.
Rui Ueyama13b58ec2015-06-30 00:03:56 +00001038 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer0e752cb2011-10-17 23:53:56 +00001039 else
1040 // Not null terminated, use all 8 bytes.
Rui Ueyama13b58ec2015-06-30 00:03:56 +00001041 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyamaeae46732015-06-09 15:20:42 +00001042 return std::error_code();
Michael J. Spencer0e752cb2011-10-17 23:53:56 +00001043}
1044
David Majnemerb3a86a02014-09-10 12:51:52 +00001045ArrayRef<uint8_t>
1046COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper0b6cb712014-04-15 06:32:26 +00001047 const uint8_t *Aux = nullptr;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +00001048
David Majnemerb3a86a02014-09-10 12:51:52 +00001049 size_t SymbolSize = getSymbolTableEntrySize();
1050 if (Symbol.getNumberOfAuxSymbols() > 0) {
1051 // AUX data comes immediately after the symbol in COFF
1052 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Eugene Zelenko86bfc782017-04-19 23:02:10 +00001053#ifndef NDEBUG
Rui Ueyama671a9e22014-01-16 20:11:48 +00001054 // Verify that the Aux symbol points to a valid entry in the symbol table.
1055 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemerb3a86a02014-09-10 12:51:52 +00001056 if (Offset < getPointerToSymbolTable() ||
1057 Offset >=
1058 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clowd4d03e02012-06-15 01:08:25 +00001059 report_fatal_error("Aux Symbol data was outside of symbol table.");
1060
David Majnemerb3a86a02014-09-10 12:51:52 +00001061 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
1062 "Aux Symbol data did not point to the beginning of a symbol");
Eugene Zelenko86bfc782017-04-19 23:02:10 +00001063#endif
Marshall Clow45aad162012-06-15 01:15:47 +00001064 }
David Majnemerb3a86a02014-09-10 12:51:52 +00001065 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clowd4d03e02012-06-15 01:08:25 +00001066}
1067
Martin Storsjo0239fd32019-01-03 08:08:23 +00001068uint32_t COFFObjectFile::getSymbolIndex(COFFSymbolRef Symbol) const {
1069 uintptr_t Offset =
1070 reinterpret_cast<uintptr_t>(Symbol.getRawPtr()) - getSymbolTable();
1071 assert(Offset % getSymbolTableEntrySize() == 0 &&
1072 "Symbol did not point to the beginning of a symbol");
1073 size_t Index = Offset / getSymbolTableEntrySize();
1074 assert(Index < getNumberOfSymbols());
1075 return Index;
1076}
1077
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001078std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
1079 StringRef &Res) const {
Michael J. Spencerb35a8962012-03-19 20:27:15 +00001080 StringRef Name;
David Majnemerb3a86a02014-09-10 12:51:52 +00001081 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencerb35a8962012-03-19 20:27:15 +00001082 // Null terminated, let ::strlen figure out the length.
1083 Name = Sec->Name;
1084 else
1085 // Not null terminated, use all 8 bytes.
David Majnemerb3a86a02014-09-10 12:51:52 +00001086 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencerb35a8962012-03-19 20:27:15 +00001087
1088 // Check for string table entry. First byte is '/'.
David Majnemerf7046b72014-11-13 07:42:09 +00001089 if (Name.startswith("/")) {
Michael J. Spencerb35a8962012-03-19 20:27:15 +00001090 uint32_t Offset;
David Majnemerf7046b72014-11-13 07:42:09 +00001091 if (Name.startswith("//")) {
Nico Rieckfa3089b2014-02-22 16:12:20 +00001092 if (decodeBase64StringEntry(Name.substr(2), Offset))
1093 return object_error::parse_failed;
1094 } else {
1095 if (Name.substr(1).getAsInteger(10, Offset))
1096 return object_error::parse_failed;
1097 }
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001098 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama671a9e22014-01-16 20:11:48 +00001099 return EC;
Michael J. Spencerb35a8962012-03-19 20:27:15 +00001100 }
1101
1102 Res = Name;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001103 return std::error_code();
Michael J. Spencerb35a8962012-03-19 20:27:15 +00001104}
1105
David Majnemer06f1f352014-10-09 08:42:31 +00001106uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1107 // SizeOfRawData and VirtualSize change what they represent depending on
1108 // whether or not we have an executable image.
1109 //
1110 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyama392450e2015-07-04 03:25:51 +00001111 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemer06f1f352014-10-09 08:42:31 +00001112 //
1113 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1114 // actual section size is in VirtualSize. It is possible for VirtualSize to
1115 // be greater than SizeOfRawData; the contents past that point should be
1116 // considered to be zero.
Rui Ueyama392450e2015-07-04 03:25:51 +00001117 if (getDOSHeader())
1118 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1119 return Sec->SizeOfRawData;
David Majnemer06f1f352014-10-09 08:42:31 +00001120}
1121
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001122std::error_code
1123COFFObjectFile::getSectionContents(const coff_section *Sec,
1124 ArrayRef<uint8_t> &Res) const {
David Majnemerddd45852016-05-28 19:45:51 +00001125 // In COFF, a virtual section won't have any in-file
1126 // content, so the file pointer to the content will be zero.
1127 if (Sec->PointerToRawData == 0)
Shoaib Meenai61e19cf2017-05-14 18:34:56 +00001128 return std::error_code();
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +00001129 // The only thing that we need to verify is that the contents is contained
1130 // within the file bounds. We don't need to make sure it doesn't cover other
1131 // data, as there's nothing that says that is not allowed.
1132 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemer06f1f352014-10-09 08:42:31 +00001133 uint32_t SectionSize = getSectionSize(Sec);
David Majnemerb6ddb3c2014-11-13 08:46:37 +00001134 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +00001135 return object_error::parse_failed;
David Majnemer06f1f352014-10-09 08:42:31 +00001136 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyamaeae46732015-06-09 15:20:42 +00001137 return std::error_code();
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +00001138}
1139
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001140const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001141 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001142}
Rui Ueyama671a9e22014-01-16 20:11:48 +00001143
Rafael Espindolaefdbec82014-01-30 02:49:50 +00001144void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001145 Rel.p = reinterpret_cast<uintptr_t>(
1146 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001147}
Rui Ueyama671a9e22014-01-16 20:11:48 +00001148
Rafael Espindolaff676292015-06-29 23:29:12 +00001149uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer465237b2014-11-13 07:42:07 +00001150 const coff_relocation *R = toRel(Rel);
Rafael Espindolaff676292015-06-29 23:29:12 +00001151 return R->VirtualAddress;
Danil Malyshevb0436a72011-11-29 17:40:10 +00001152}
Rui Ueyama671a9e22014-01-16 20:11:48 +00001153
Rafael Espindola6c1202c2013-06-05 01:33:53 +00001154symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemerb3a86a02014-09-10 12:51:52 +00001155 const coff_relocation *R = toRel(Rel);
Rui Ueyama671a9e22014-01-16 20:11:48 +00001156 DataRefImpl Ref;
David Majnemera18e46c2014-11-17 11:17:17 +00001157 if (R->SymbolTableIndex >= getNumberOfSymbols())
1158 return symbol_end();
David Majnemerb3a86a02014-09-10 12:51:52 +00001159 if (SymbolTable16)
1160 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1161 else if (SymbolTable32)
1162 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1163 else
David Majnemerc891fc32014-11-25 07:43:14 +00001164 llvm_unreachable("no symbol table pointer!");
Rui Ueyama671a9e22014-01-16 20:11:48 +00001165 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001166}
Rui Ueyama671a9e22014-01-16 20:11:48 +00001167
Rafael Espindola7ede9642015-06-30 01:53:01 +00001168uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001169 const coff_relocation* R = toRel(Rel);
Rafael Espindola7ede9642015-06-30 01:53:01 +00001170 return R->Type;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001171}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001172
Alexey Samsonov5b645792014-03-18 06:53:02 +00001173const coff_section *
1174COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1175 return toSec(Section.getRawDataRefImpl());
Marshall Clowd4d03e02012-06-15 01:08:25 +00001176}
1177
David Majnemerb3a86a02014-09-10 12:51:52 +00001178COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1179 if (SymbolTable16)
1180 return toSymb<coff_symbol16>(Ref);
1181 if (SymbolTable32)
1182 return toSymb<coff_symbol32>(Ref);
1183 llvm_unreachable("no symbol table pointer!");
1184}
1185
1186COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1187 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clowd4d03e02012-06-15 01:08:25 +00001188}
1189
Rafael Espindola91f86b72014-02-21 20:10:59 +00001190const coff_relocation *
Alexey Samsonov5b645792014-03-18 06:53:02 +00001191COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1192 return toRel(Reloc.getRawDataRefImpl());
Marshall Clow9ac0f1d2012-06-18 19:47:16 +00001193}
1194
Peter Collingbournea3064562018-04-17 01:54:34 +00001195ArrayRef<coff_relocation>
Rui Ueyama7d6e44b2015-06-25 00:07:39 +00001196COFFObjectFile::getRelocations(const coff_section *Sec) const {
Peter Collingbournea3064562018-04-17 01:54:34 +00001197 return {getFirstReloc(Sec, Data, base()),
1198 getNumberOfRelocations(Sec, Data, base())};
Rui Ueyama7d6e44b2015-06-25 00:07:39 +00001199}
1200
Alexey Samsonov5b645792014-03-18 06:53:02 +00001201#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1202 case COFF::reloc_type: \
Martin Storsjo921890c2018-08-27 08:42:39 +00001203 return #reloc_type;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001204
Martin Storsjo921890c2018-08-27 08:42:39 +00001205StringRef COFFObjectFile::getRelocationTypeName(uint16_t Type) const {
David Majnemerb3a86a02014-09-10 12:51:52 +00001206 switch (getMachine()) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001207 case COFF::IMAGE_FILE_MACHINE_AMD64:
Martin Storsjo921890c2018-08-27 08:42:39 +00001208 switch (Type) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001209 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1210 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1211 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1212 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1213 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1214 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1215 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1216 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1217 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1218 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1219 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1220 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1221 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1222 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1223 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1224 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1225 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1226 default:
Martin Storsjo921890c2018-08-27 08:42:39 +00001227 return "Unknown";
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001228 }
1229 break;
Saleem Abdulrasoold640feb2014-04-09 06:18:28 +00001230 case COFF::IMAGE_FILE_MACHINE_ARMNT:
Martin Storsjo921890c2018-08-27 08:42:39 +00001231 switch (Type) {
Saleem Abdulrasoold640feb2014-04-09 06:18:28 +00001232 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1233 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1234 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1235 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1236 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1237 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1238 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1239 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1240 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1241 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1242 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1243 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1244 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1245 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1246 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1247 default:
Martin Storsjo921890c2018-08-27 08:42:39 +00001248 return "Unknown";
Saleem Abdulrasoold640feb2014-04-09 06:18:28 +00001249 }
1250 break;
Mandeep Singh Grang85e09462017-06-27 23:58:19 +00001251 case COFF::IMAGE_FILE_MACHINE_ARM64:
Martin Storsjo921890c2018-08-27 08:42:39 +00001252 switch (Type) {
Mandeep Singh Grang85e09462017-06-27 23:58:19 +00001253 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ABSOLUTE);
1254 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32);
1255 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32NB);
1256 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH26);
1257 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEBASE_REL21);
1258 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_REL21);
1259 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12A);
1260 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12L);
1261 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL);
1262 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12A);
1263 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_HIGH12A);
1264 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12L);
1265 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_TOKEN);
1266 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECTION);
1267 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR64);
1268 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH19);
1269 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH14);
1270 default:
Martin Storsjo921890c2018-08-27 08:42:39 +00001271 return "Unknown";
Mandeep Singh Grang85e09462017-06-27 23:58:19 +00001272 }
1273 break;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001274 case COFF::IMAGE_FILE_MACHINE_I386:
Martin Storsjo921890c2018-08-27 08:42:39 +00001275 switch (Type) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001276 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1277 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1278 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1279 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1280 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1281 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1282 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1283 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1284 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1285 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1286 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1287 default:
Martin Storsjo921890c2018-08-27 08:42:39 +00001288 return "Unknown";
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001289 }
1290 break;
1291 default:
Martin Storsjo921890c2018-08-27 08:42:39 +00001292 return "Unknown";
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001293 }
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001294}
1295
1296#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1297
Martin Storsjo921890c2018-08-27 08:42:39 +00001298void COFFObjectFile::getRelocationTypeName(
1299 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1300 const coff_relocation *Reloc = toRel(Rel);
1301 StringRef Res = getRelocationTypeName(Reloc->Type);
1302 Result.append(Res.begin(), Res.end());
1303}
1304
Rafael Espindola78e8d522014-08-17 19:09:37 +00001305bool COFFObjectFile::isRelocatableObject() const {
1306 return !DataDirectory;
1307}
1308
Martin Storsjo0f6ce822018-12-08 18:15:41 +00001309StringRef COFFObjectFile::mapDebugSectionName(StringRef Name) const {
1310 return StringSwitch<StringRef>(Name)
1311 .Case("eh_fram", "eh_frame")
1312 .Default(Name);
1313}
1314
Rui Ueyamaa6610ee2013-09-27 21:04:00 +00001315bool ImportDirectoryEntryRef::
1316operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyama6a8151d2014-01-16 03:13:19 +00001317 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +00001318}
1319
Rafael Espindolaefdbec82014-01-30 02:49:50 +00001320void ImportDirectoryEntryRef::moveNext() {
1321 ++Index;
David Majnemere2fac432016-07-31 19:25:21 +00001322 if (ImportTable[Index].isNull()) {
David Majnemer6ea11a42016-06-26 04:36:32 +00001323 Index = -1;
1324 ImportTable = nullptr;
1325 }
Rui Ueyamaa6610ee2013-09-27 21:04:00 +00001326}
1327
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001328std::error_code ImportDirectoryEntryRef::getImportTableEntry(
David Majnemere2fac432016-07-31 19:25:21 +00001329 const coff_import_directory_table_entry *&Result) const {
David Majnemer6ea11a42016-06-26 04:36:32 +00001330 return getObject(Result, OwningObject->Data, ImportTable + Index);
Rui Ueyamaa6610ee2013-09-27 21:04:00 +00001331}
1332
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001333static imported_symbol_iterator
Rui Ueyama82066462014-10-03 00:41:58 +00001334makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001335 uintptr_t Ptr, int Index) {
Rui Ueyama82066462014-10-03 00:41:58 +00001336 if (Object->getBytesInAddress() == 4) {
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001337 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama82066462014-10-03 00:41:58 +00001338 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001339 }
1340 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama82066462014-10-03 00:41:58 +00001341 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001342}
1343
Rui Ueyama82066462014-10-03 00:41:58 +00001344static imported_symbol_iterator
1345importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001346 uintptr_t IntPtr = 0;
Rui Ueyama82066462014-10-03 00:41:58 +00001347 Object->getRvaPtr(RVA, IntPtr);
1348 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001349}
1350
Rui Ueyama82066462014-10-03 00:41:58 +00001351static imported_symbol_iterator
1352importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001353 uintptr_t IntPtr = 0;
Rui Ueyama82066462014-10-03 00:41:58 +00001354 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001355 // Forward the pointer to the last entry which is null.
1356 int Index = 0;
Rui Ueyama82066462014-10-03 00:41:58 +00001357 if (Object->getBytesInAddress() == 4) {
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001358 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1359 while (*Entry++)
1360 ++Index;
1361 } else {
1362 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1363 while (*Entry++)
1364 ++Index;
1365 }
Rui Ueyama82066462014-10-03 00:41:58 +00001366 return makeImportedSymbolIterator(Object, IntPtr, Index);
1367}
1368
1369imported_symbol_iterator
1370ImportDirectoryEntryRef::imported_symbol_begin() const {
David Majnemer081d3f12016-07-31 19:40:02 +00001371 return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama82066462014-10-03 00:41:58 +00001372 OwningObject);
1373}
1374
1375imported_symbol_iterator
1376ImportDirectoryEntryRef::imported_symbol_end() const {
David Majnemer081d3f12016-07-31 19:40:02 +00001377 return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama82066462014-10-03 00:41:58 +00001378 OwningObject);
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001379}
1380
Rui Ueyamaf02f03c2014-10-09 02:16:38 +00001381iterator_range<imported_symbol_iterator>
1382ImportDirectoryEntryRef::imported_symbols() const {
1383 return make_range(imported_symbol_begin(), imported_symbol_end());
1384}
1385
David Majnemer081d3f12016-07-31 19:40:02 +00001386imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const {
1387 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1388 OwningObject);
1389}
1390
1391imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const {
1392 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1393 OwningObject);
1394}
1395
1396iterator_range<imported_symbol_iterator>
1397ImportDirectoryEntryRef::lookup_table_symbols() const {
1398 return make_range(lookup_table_begin(), lookup_table_end());
1399}
1400
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001401std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamaa6610ee2013-09-27 21:04:00 +00001402 uintptr_t IntPtr = 0;
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001403 if (std::error_code EC =
Rui Ueyamaf3cd10b2014-10-02 17:02:18 +00001404 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyama6a8151d2014-01-16 03:13:19 +00001405 return EC;
1406 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamaeae46732015-06-09 15:20:42 +00001407 return std::error_code();
Rui Ueyamaa6610ee2013-09-27 21:04:00 +00001408}
1409
Rui Ueyamaf3cd10b2014-10-02 17:02:18 +00001410std::error_code
1411ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1412 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001413 return std::error_code();
Rui Ueyamaf3cd10b2014-10-02 17:02:18 +00001414}
1415
1416std::error_code
1417ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1418 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001419 return std::error_code();
Rui Ueyamaf3cd10b2014-10-02 17:02:18 +00001420}
1421
Rui Ueyama82066462014-10-03 00:41:58 +00001422bool DelayImportDirectoryEntryRef::
1423operator==(const DelayImportDirectoryEntryRef &Other) const {
1424 return Table == Other.Table && Index == Other.Index;
1425}
1426
1427void DelayImportDirectoryEntryRef::moveNext() {
1428 ++Index;
1429}
1430
1431imported_symbol_iterator
1432DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1433 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1434 OwningObject);
1435}
1436
1437imported_symbol_iterator
1438DelayImportDirectoryEntryRef::imported_symbol_end() const {
1439 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1440 OwningObject);
1441}
1442
Rui Ueyamaf02f03c2014-10-09 02:16:38 +00001443iterator_range<imported_symbol_iterator>
1444DelayImportDirectoryEntryRef::imported_symbols() const {
1445 return make_range(imported_symbol_begin(), imported_symbol_end());
1446}
1447
Rui Ueyama82066462014-10-03 00:41:58 +00001448std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1449 uintptr_t IntPtr = 0;
1450 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1451 return EC;
1452 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamaeae46732015-06-09 15:20:42 +00001453 return std::error_code();
Rui Ueyama82066462014-10-03 00:41:58 +00001454}
1455
Rui Ueyamaa6abd9e2014-10-03 18:07:18 +00001456std::error_code DelayImportDirectoryEntryRef::
1457getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1458 Result = Table;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001459 return std::error_code();
Rui Ueyamaa6abd9e2014-10-03 18:07:18 +00001460}
1461
Rui Ueyama08f70b52014-11-13 03:22:54 +00001462std::error_code DelayImportDirectoryEntryRef::
1463getImportAddress(int AddrIndex, uint64_t &Result) const {
1464 uint32_t RVA = Table[Index].DelayImportAddressTable +
1465 AddrIndex * (OwningObject->is64() ? 8 : 4);
1466 uintptr_t IntPtr = 0;
1467 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1468 return EC;
1469 if (OwningObject->is64())
Rui Ueyamafb113f62014-11-13 20:07:06 +00001470 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyama08f70b52014-11-13 03:22:54 +00001471 else
Rui Ueyamafb113f62014-11-13 20:07:06 +00001472 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyamaeae46732015-06-09 15:20:42 +00001473 return std::error_code();
Rui Ueyama08f70b52014-11-13 03:22:54 +00001474}
1475
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001476bool ExportDirectoryEntryRef::
1477operator==(const ExportDirectoryEntryRef &Other) const {
1478 return ExportTable == Other.ExportTable && Index == Other.Index;
1479}
1480
Rafael Espindolaefdbec82014-01-30 02:49:50 +00001481void ExportDirectoryEntryRef::moveNext() {
1482 ++Index;
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001483}
1484
Rui Ueyama6010e5d2014-01-16 20:50:34 +00001485// Returns the name of the current export symbol. If the symbol is exported only
1486// by ordinal, the empty string is set as a result.
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001487std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyama6010e5d2014-01-16 20:50:34 +00001488 uintptr_t IntPtr = 0;
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001489 if (std::error_code EC =
1490 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyama6010e5d2014-01-16 20:50:34 +00001491 return EC;
1492 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamaeae46732015-06-09 15:20:42 +00001493 return std::error_code();
Rui Ueyama6010e5d2014-01-16 20:50:34 +00001494}
1495
Rui Ueyama9106d362014-01-17 22:02:24 +00001496// Returns the starting ordinal number.
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001497std::error_code
1498ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyama9106d362014-01-17 22:02:24 +00001499 Result = ExportTable->OrdinalBase;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001500 return std::error_code();
Rui Ueyama9106d362014-01-17 22:02:24 +00001501}
1502
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001503// Returns the export ordinal of the current export symbol.
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001504std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001505 Result = ExportTable->OrdinalBase + Index;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001506 return std::error_code();
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001507}
1508
1509// Returns the address of the current export symbol.
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001510std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001511 uintptr_t IntPtr = 0;
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001512 if (std::error_code EC =
1513 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001514 return EC;
Rui Ueyama64382602014-01-17 22:11:27 +00001515 const export_address_table_entry *entry =
1516 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001517 Result = entry[Index].ExportRVA;
Rui Ueyamaeae46732015-06-09 15:20:42 +00001518 return std::error_code();
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001519}
1520
1521// Returns the name of the current export symbol. If the symbol is exported only
1522// by ordinal, the empty string is set as a result.
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001523std::error_code
1524ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001525 uintptr_t IntPtr = 0;
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001526 if (std::error_code EC =
1527 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001528 return EC;
1529 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1530
1531 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1532 int Offset = 0;
1533 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1534 I < E; ++I, ++Offset) {
1535 if (*I != Index)
1536 continue;
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001537 if (std::error_code EC =
1538 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001539 return EC;
1540 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001541 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001542 return EC;
1543 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamaeae46732015-06-09 15:20:42 +00001544 return std::error_code();
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001545 }
1546 Result = "";
Rui Ueyamaeae46732015-06-09 15:20:42 +00001547 return std::error_code();
Rui Ueyamafb432ac2014-01-16 07:05:49 +00001548}
1549
Rui Ueyama68e634a2016-01-12 23:28:42 +00001550std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1551 const data_directory *DataEntry;
1552 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1553 return EC;
1554 uint32_t RVA;
1555 if (auto EC = getExportRVA(RVA))
1556 return EC;
1557 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1558 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1559 Result = (Begin <= RVA && RVA < End);
1560 return std::error_code();
1561}
1562
1563std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1564 uint32_t RVA;
1565 if (auto EC = getExportRVA(RVA))
1566 return EC;
1567 uintptr_t IntPtr = 0;
1568 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1569 return EC;
1570 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1571 return std::error_code();
1572}
1573
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001574bool ImportedSymbolRef::
1575operator==(const ImportedSymbolRef &Other) const {
1576 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1577 && Index == Other.Index;
1578}
1579
1580void ImportedSymbolRef::moveNext() {
1581 ++Index;
1582}
1583
1584std::error_code
1585ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1586 uint32_t RVA;
1587 if (Entry32) {
1588 // If a symbol is imported only by ordinal, it has no name.
1589 if (Entry32[Index].isOrdinal())
Rui Ueyamaeae46732015-06-09 15:20:42 +00001590 return std::error_code();
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001591 RVA = Entry32[Index].getHintNameRVA();
1592 } else {
1593 if (Entry64[Index].isOrdinal())
Rui Ueyamaeae46732015-06-09 15:20:42 +00001594 return std::error_code();
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001595 RVA = Entry64[Index].getHintNameRVA();
1596 }
1597 uintptr_t IntPtr = 0;
1598 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1599 return EC;
1600 // +2 because the first two bytes is hint.
1601 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyamaeae46732015-06-09 15:20:42 +00001602 return std::error_code();
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001603}
1604
David Majnemer6ea11a42016-06-26 04:36:32 +00001605std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1606 if (Entry32)
1607 Result = Entry32[Index].isOrdinal();
1608 else
1609 Result = Entry64[Index].isOrdinal();
1610 return std::error_code();
1611}
1612
1613std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1614 if (Entry32)
1615 Result = Entry32[Index].getHintNameRVA();
1616 else
1617 Result = Entry64[Index].getHintNameRVA();
1618 return std::error_code();
1619}
1620
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001621std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1622 uint32_t RVA;
1623 if (Entry32) {
1624 if (Entry32[Index].isOrdinal()) {
1625 Result = Entry32[Index].getOrdinal();
Rui Ueyamaeae46732015-06-09 15:20:42 +00001626 return std::error_code();
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001627 }
1628 RVA = Entry32[Index].getHintNameRVA();
1629 } else {
1630 if (Entry64[Index].isOrdinal()) {
1631 Result = Entry64[Index].getOrdinal();
Rui Ueyamaeae46732015-06-09 15:20:42 +00001632 return std::error_code();
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001633 }
1634 RVA = Entry64[Index].getHintNameRVA();
1635 }
1636 uintptr_t IntPtr = 0;
1637 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1638 return EC;
1639 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyamaeae46732015-06-09 15:20:42 +00001640 return std::error_code();
Rui Ueyama3d49ad02014-10-02 22:05:29 +00001641}
1642
Rafael Espindolae2d20cb2017-10-10 20:00:07 +00001643Expected<std::unique_ptr<COFFObjectFile>>
Rafael Espindola548f2b62014-08-19 18:44:46 +00001644ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindola4e2b9222014-06-13 02:24:39 +00001645 std::error_code EC;
Rafael Espindola548f2b62014-08-19 18:44:46 +00001646 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola3d218152014-01-21 23:06:54 +00001647 if (EC)
Rafael Espindolae2d20cb2017-10-10 20:00:07 +00001648 return errorCodeToError(EC);
Rafael Espindola79002da2014-07-31 03:12:45 +00001649 return std::move(Ret);
Rui Ueyamaa560d152014-01-16 20:30:36 +00001650}
Rui Ueyama6272b8c2014-11-19 00:18:07 +00001651
1652bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1653 return Header == Other.Header && Index == Other.Index;
1654}
1655
1656void BaseRelocRef::moveNext() {
1657 // Header->BlockSize is the size of the current block, including the
1658 // size of the header itself.
1659 uint32_t Size = sizeof(*Header) +
Rui Ueyama3977e2f2014-11-19 02:07:10 +00001660 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama6272b8c2014-11-19 00:18:07 +00001661 if (Size == Header->BlockSize) {
1662 // .reloc contains a list of base relocation blocks. Each block
1663 // consists of the header followed by entries. The header contains
1664 // how many entories will follow. When we reach the end of the
1665 // current block, proceed to the next block.
1666 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1667 reinterpret_cast<const uint8_t *>(Header) + Size);
1668 Index = 0;
1669 } else {
1670 ++Index;
1671 }
1672}
1673
1674std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1675 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1676 Type = Entry[Index].getType();
Rui Ueyamaeae46732015-06-09 15:20:42 +00001677 return std::error_code();
Rui Ueyama6272b8c2014-11-19 00:18:07 +00001678}
1679
1680std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1681 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1682 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyamaeae46732015-06-09 15:20:42 +00001683 return std::error_code();
Rui Ueyama6272b8c2014-11-19 00:18:07 +00001684}
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001685
Rafael Espindola0aed7ae2017-10-11 17:05:24 +00001686#define RETURN_IF_ERROR(E) \
1687 if (E) \
1688 return E;
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001689
Rafael Espindola0aed7ae2017-10-11 17:05:24 +00001690Expected<ArrayRef<UTF16>>
1691ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) {
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001692 BinaryStreamReader Reader = BinaryStreamReader(BBS);
1693 Reader.setOffset(Offset);
1694 uint16_t Length;
1695 RETURN_IF_ERROR(Reader.readInteger(Length));
1696 ArrayRef<UTF16> RawDirString;
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001697 RETURN_IF_ERROR(Reader.readArray(RawDirString, Length));
Eric Beckmann20c59f12017-05-08 02:47:42 +00001698 return RawDirString;
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001699}
1700
Rafael Espindola7848ae32017-10-11 17:33:11 +00001701Expected<ArrayRef<UTF16>>
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001702ResourceSectionRef::getEntryNameString(const coff_resource_dir_entry &Entry) {
Rafael Espindola7848ae32017-10-11 17:33:11 +00001703 return getDirStringAtOffset(Entry.Identifier.getNameOffset());
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001704}
1705
Rafael Espindola0aed7ae2017-10-11 17:05:24 +00001706Expected<const coff_resource_dir_table &>
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001707ResourceSectionRef::getTableAtOffset(uint32_t Offset) {
1708 const coff_resource_dir_table *Table = nullptr;
1709
1710 BinaryStreamReader Reader(BBS);
1711 Reader.setOffset(Offset);
1712 RETURN_IF_ERROR(Reader.readObject(Table));
1713 assert(Table != nullptr);
1714 return *Table;
1715}
1716
Rafael Espindola7848ae32017-10-11 17:33:11 +00001717Expected<const coff_resource_dir_table &>
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001718ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) {
Rafael Espindola7848ae32017-10-11 17:33:11 +00001719 return getTableAtOffset(Entry.Offset.value());
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001720}
1721
Rafael Espindola7848ae32017-10-11 17:33:11 +00001722Expected<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() {
1723 return getTableAtOffset(0);
Eric Beckmann8d0cc562017-05-08 02:47:07 +00001724}