blob: 0f790086cfc58f685f56e2438aa63ded283af50e [file] [log] [blame]
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000016#include "llvm/ADT/StringSwitch.h"
17#include "llvm/ADT/Triple.h"
Rui Ueyama6a75acb2015-06-25 00:07:39 +000018#include "llvm/ADT/iterator_range.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000019#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000022#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000023#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000024
25using namespace llvm;
26using namespace object;
27
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000028using support::ulittle16_t;
29using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000030using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000031using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000033// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000034static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000035 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000036 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000037 return false;
38 }
39 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000040}
41
David Majnemere830c602014-11-13 08:46:37 +000042static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
David Majnemer94751be2014-11-13 09:50:18 +000043 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000044 if (Addr + Size < Addr || Addr + Size < Size ||
45 Addr + Size > uintptr_t(M.getBufferEnd()) ||
46 Addr < uintptr_t(M.getBufferStart())) {
47 return object_error::unexpected_eof;
48 }
Rui Ueyama7d099192015-06-09 15:20:42 +000049 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000050}
51
Rui Ueyamaed64342b2013-07-19 23:23:29 +000052// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
53// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000054template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000055static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000056 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000057 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000058 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000059 if (std::error_code EC = checkOffset(M, Addr, Size))
60 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000061 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyama7d099192015-06-09 15:20:42 +000062 return std::error_code();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000063}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064
Nico Rieck9d2c15e2014-02-22 16:12:20 +000065// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
66// prefixed slashes.
67static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
68 assert(Str.size() <= 6 && "String too long, possible overflow.");
69 if (Str.size() > 6)
70 return true;
71
72 uint64_t Value = 0;
73 while (!Str.empty()) {
74 unsigned CharVal;
75 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
76 CharVal = Str[0] - 'A';
77 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
78 CharVal = Str[0] - 'a' + 26;
79 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
80 CharVal = Str[0] - '0' + 52;
81 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000082 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000083 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000084 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000085 else
86 return true;
87
88 Value = (Value * 64) + CharVal;
89 Str = Str.substr(1);
90 }
91
92 if (Value > std::numeric_limits<uint32_t>::max())
93 return true;
94
95 Result = static_cast<uint32_t>(Value);
96 return false;
97}
98
David Majnemer44f51e52014-09-10 12:51:52 +000099template <typename coff_symbol_type>
100const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
101 const coff_symbol_type *Addr =
102 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103
David Majnemer236b0ca2014-11-17 11:17:17 +0000104 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000105#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000106 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000107 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000108
David Majnemer44f51e52014-09-10 12:51:52 +0000109 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
110 "Symbol did not point to the beginning of a symbol");
111#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000112
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000113 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000114}
115
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000116const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
117 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000118
119# ifndef NDEBUG
120 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000121 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000122 report_fatal_error("Section was outside of section table.");
123
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000124 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
125 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000126 "Section did not point to the beginning of a section");
127# endif
128
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000129 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000130}
131
Rafael Espindola5e812af2014-01-30 02:49:50 +0000132void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000133 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000134 if (SymbolTable16) {
135 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
136 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000137 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000138 } else if (SymbolTable32) {
139 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
140 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000141 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000142 } else {
143 llvm_unreachable("no symbol table pointer!");
144 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000145}
146
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000147Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000148 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000149 StringRef Result;
150 std::error_code EC = getSymbolName(Symb, Result);
151 if (EC)
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000152 return errorCodeToError(EC);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000153 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000154}
155
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000156uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
157 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000158}
159
Kevin Enderby931cb652016-06-24 18:24:42 +0000160Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
Rafael Espindolaed067c42015-07-03 18:19:00 +0000161 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000162 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000163 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000164
165 if (Symb.isAnyUndefined() || Symb.isCommon() ||
166 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000167 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000168
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000169 const coff_section *Section = nullptr;
170 if (std::error_code EC = getSection(SectionNumber, Section))
Kevin Enderby931cb652016-06-24 18:24:42 +0000171 return errorCodeToError(EC);
Rafael Espindola991af662015-06-24 19:11:10 +0000172 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000173
174 // The section VirtualAddress does not include ImageBase, and we want to
175 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000176 Result += getImageBase();
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000177
Rafael Espindolaed067c42015-07-03 18:19:00 +0000178 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000179}
180
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000181Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000182 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000183 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000184
Peter Collingbournee834f422015-08-06 05:26:35 +0000185 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
186 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000187 if (Symb.isAnyUndefined())
188 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000189 if (Symb.isCommon())
190 return SymbolRef::ST_Data;
191 if (Symb.isFileRecord())
192 return SymbolRef::ST_File;
193
194 // TODO: perhaps we need a new symbol type ST_Section.
195 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
196 return SymbolRef::ST_Debug;
197
198 if (!COFF::isReservedSectionNumber(SectionNumber))
199 return SymbolRef::ST_Data;
200
201 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000202}
203
Rafael Espindola20122a42014-01-31 20:57:12 +0000204uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000205 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000206 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000207
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000208 if (Symb.isExternal() || Symb.isWeakExternal())
Lang Hames9dc0eb42016-01-25 01:21:45 +0000209 Result |= SymbolRef::SF_Global;
David Meyer1df4b842012-02-28 23:47:53 +0000210
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000211 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000212 Result |= SymbolRef::SF_Weak;
213
David Majnemer44f51e52014-09-10 12:51:52 +0000214 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000215 Result |= SymbolRef::SF_Absolute;
216
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000217 if (Symb.isFileRecord())
218 Result |= SymbolRef::SF_FormatSpecific;
219
220 if (Symb.isSectionDefinition())
221 Result |= SymbolRef::SF_FormatSpecific;
222
223 if (Symb.isCommon())
224 Result |= SymbolRef::SF_Common;
225
226 if (Symb.isAnyUndefined())
227 Result |= SymbolRef::SF_Undefined;
228
Rafael Espindola20122a42014-01-31 20:57:12 +0000229 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000230}
231
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000232uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000233 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000234 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000235}
236
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000237Expected<section_iterator>
Rafael Espindola8bab8892015-08-07 23:27:14 +0000238COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000239 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000240 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
241 return section_end();
242 const coff_section *Sec = nullptr;
243 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000244 return errorCodeToError(EC);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000245 DataRefImpl Ret;
246 Ret.p = reinterpret_cast<uintptr_t>(Sec);
247 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000248}
249
Rafael Espindola6bf32212015-06-24 19:57:32 +0000250unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
251 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
252 return Symb.getSectionNumber();
253}
254
Rafael Espindola5e812af2014-01-30 02:49:50 +0000255void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000256 const coff_section *Sec = toSec(Ref);
257 Sec += 1;
258 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000259}
260
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000261std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
262 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000263 const coff_section *Sec = toSec(Ref);
264 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000265}
266
Rafael Espindola80291272014-10-08 15:28:58 +0000267uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000268 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000269 uint64_t Result = Sec->VirtualAddress;
270
271 // The section VirtualAddress does not include ImageBase, and we want to
272 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000273 Result += getImageBase();
David Majnemer7c6a0712015-07-31 17:40:24 +0000274 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000275}
276
Rafael Espindola80291272014-10-08 15:28:58 +0000277uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000278 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000279}
280
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000281std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
282 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000283 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000284 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000285 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000286 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
287 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000288}
289
Rafael Espindola80291272014-10-08 15:28:58 +0000290uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000291 const coff_section *Sec = toSec(Ref);
David Majnemer511391f2016-03-17 16:55:18 +0000292 return Sec->getAlignment();
Michael J. Spencer79894602011-10-10 21:55:43 +0000293}
294
George Rimar401e4e52016-05-24 12:48:46 +0000295bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
296 return false;
297}
298
Rafael Espindola80291272014-10-08 15:28:58 +0000299bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000300 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000301 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000302}
303
Rafael Espindola80291272014-10-08 15:28:58 +0000304bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000305 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000306 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000307}
308
Rafael Espindola80291272014-10-08 15:28:58 +0000309bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000310 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000311 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
312 COFF::IMAGE_SCN_MEM_READ |
313 COFF::IMAGE_SCN_MEM_WRITE;
314 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000315}
316
Rafael Espindola6bf32212015-06-24 19:57:32 +0000317unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
318 uintptr_t Offset =
319 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
320 assert((Offset % sizeof(coff_section)) == 0);
321 return (Offset / sizeof(coff_section)) + 1;
322}
323
Rafael Espindola80291272014-10-08 15:28:58 +0000324bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000325 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000326 // In COFF, a virtual section won't have any in-file
327 // content, so the file pointer to the content will be zero.
328 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000329}
330
David Majnemere830c602014-11-13 08:46:37 +0000331static uint32_t getNumberOfRelocations(const coff_section *Sec,
332 MemoryBufferRef M, const uint8_t *base) {
333 // The field for the number of relocations in COFF section table is only
334 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
335 // NumberOfRelocations field, and the actual relocation count is stored in the
336 // VirtualAddress field in the first relocation entry.
337 if (Sec->hasExtendedRelocations()) {
338 const coff_relocation *FirstReloc;
339 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
340 base + Sec->PointerToRelocations)))
341 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000342 // -1 to exclude this first relocation entry.
343 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000344 }
345 return Sec->NumberOfRelocations;
346}
347
David Majnemer94751be2014-11-13 09:50:18 +0000348static const coff_relocation *
349getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
350 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
351 if (!NumRelocs)
352 return nullptr;
353 auto begin = reinterpret_cast<const coff_relocation *>(
354 Base + Sec->PointerToRelocations);
355 if (Sec->hasExtendedRelocations()) {
356 // Skip the first relocation entry repurposed to store the number of
357 // relocations.
358 begin++;
359 }
360 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
361 return nullptr;
362 return begin;
363}
364
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000365relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
366 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000367 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000368 if (begin && Sec->VirtualAddress != 0)
369 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000370 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000371 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000372 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000373}
374
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
376 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000377 const coff_relocation *I = getFirstReloc(Sec, Data, base());
378 if (I)
379 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000380 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000381 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000382 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000383}
384
Rui Ueyamac2bed422013-09-27 21:04:00 +0000385// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000386std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000387 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000388 if (std::error_code EC = getObject(
389 SymbolTable16, Data, base() + getPointerToSymbolTable(),
390 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000391 return EC;
392
393 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000394 if (std::error_code EC = getObject(
395 SymbolTable32, Data, base() + getPointerToSymbolTable(),
396 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000397 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000398
399 // Find string table. The first four byte of the string table contains the
400 // total size of the string table, including the size field itself. If the
401 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000402 uint32_t StringTableOffset = getPointerToSymbolTable() +
403 getNumberOfSymbols() * getSymbolTableEntrySize();
404 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000405 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000406 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000407 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000409 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000410 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000411 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000412
Nico Rieck773a5792014-02-26 19:51:44 +0000413 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
414 // tools like cvtres write a size of 0 for an empty table instead of 4.
415 if (StringTableSize < 4)
416 StringTableSize = 4;
417
Rui Ueyamac2bed422013-09-27 21:04:00 +0000418 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000419 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000420 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000421 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000422}
423
Reid Kleckner21427ad2015-10-09 00:15:08 +0000424uint64_t COFFObjectFile::getImageBase() const {
Reid Klecknere94fef72015-10-09 00:15:01 +0000425 if (PE32Header)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000426 return PE32Header->ImageBase;
Reid Klecknere94fef72015-10-09 00:15:01 +0000427 else if (PE32PlusHeader)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000428 return PE32PlusHeader->ImageBase;
429 // This actually comes up in practice.
430 return 0;
Reid Klecknere94fef72015-10-09 00:15:01 +0000431}
432
Rui Ueyama215a5862014-02-20 06:51:07 +0000433// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000434std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner21427ad2015-10-09 00:15:08 +0000435 uint64_t ImageBase = getImageBase();
Rui Ueyamab7a40082014-02-20 19:14:56 +0000436 uint64_t Rva = Addr - ImageBase;
437 assert(Rva <= UINT32_MAX);
438 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000439}
440
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000442std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000443 for (const SectionRef &S : sections()) {
444 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000445 uint32_t SectionStart = Section->VirtualAddress;
446 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000447 if (SectionStart <= Addr && Addr < SectionEnd) {
448 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000449 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000450 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000451 }
452 }
453 return object_error::parse_failed;
454}
455
Reid Kleckner2da433e2016-06-02 17:10:43 +0000456std::error_code
457COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size,
458 ArrayRef<uint8_t> &Contents) const {
459 for (const SectionRef &S : sections()) {
460 const coff_section *Section = getCOFFSection(S);
461 uint32_t SectionStart = Section->VirtualAddress;
462 // Check if this RVA is within the section bounds. Be careful about integer
463 // overflow.
464 uint32_t OffsetIntoSection = RVA - SectionStart;
465 if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
466 Size <= Section->VirtualSize - OffsetIntoSection) {
467 uintptr_t Begin =
468 uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
469 Contents =
470 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
471 return std::error_code();
472 }
473 }
474 return object_error::parse_failed;
475}
476
Rui Ueyamac2bed422013-09-27 21:04:00 +0000477// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
478// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000479std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
480 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000481 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000482 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000483 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000484 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
485 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
486 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000487 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000488}
489
Reid Kleckner2da433e2016-06-02 17:10:43 +0000490std::error_code COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir,
491 const debug_pdb_info *&PDBInfo,
492 StringRef &PDBFileName) const {
493 ArrayRef<uint8_t> InfoBytes;
494 if (std::error_code EC = getRvaAndSizeAsBytes(
495 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
496 return EC;
497 if (InfoBytes.size() < sizeof(debug_pdb_info) + 1)
498 return object_error::parse_failed;
499 PDBInfo = reinterpret_cast<const debug_pdb_info *>(InfoBytes.data());
500 InfoBytes = InfoBytes.drop_front(sizeof(debug_pdb_info));
501 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
502 InfoBytes.size());
503 // Truncate the name at the first null byte. Ignore any padding.
504 PDBFileName = PDBFileName.split('\0').first;
505 return std::error_code();
506}
507
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000508std::error_code COFFObjectFile::getDebugPDBInfo(const debug_pdb_info *&PDBInfo,
509 StringRef &PDBFileName) const {
510 for (const debug_directory &D : debug_directories())
511 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW)
512 return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
513 // If we get here, there is no PDB info to return.
514 PDBInfo = nullptr;
515 PDBFileName = StringRef();
516 return std::error_code();
517}
518
Rui Ueyamac2bed422013-09-27 21:04:00 +0000519// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000520std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000521 // First, we get the RVA of the import table. If the file lacks a pointer to
522 // the import table, do nothing.
523 const data_directory *DataEntry;
524 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000525 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000526
527 // Do nothing if the pointer to import table is NULL.
528 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000529 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000530
531 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000532
533 // Find the section that contains the RVA. This is needed because the RVA is
534 // the import table's memory address which is different from its file offset.
535 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000536 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000537 return EC;
David Majnemerad7b7e72016-06-26 04:36:32 +0000538 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
539 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000540 ImportDirectory = reinterpret_cast<
541 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000542 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000543}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000544
Rui Ueyama15d99352014-10-03 00:41:58 +0000545// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
546std::error_code COFFObjectFile::initDelayImportTablePtr() {
547 const data_directory *DataEntry;
548 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000549 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000550 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000551 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000552
553 uint32_t RVA = DataEntry->RelativeVirtualAddress;
554 NumberOfDelayImportDirectory = DataEntry->Size /
555 sizeof(delay_import_directory_table_entry) - 1;
556
557 uintptr_t IntPtr = 0;
558 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
559 return EC;
560 DelayImportDirectory = reinterpret_cast<
561 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000562 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000563}
564
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000565// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000566std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000567 // First, we get the RVA of the export table. If the file lacks a pointer to
568 // the export table, do nothing.
569 const data_directory *DataEntry;
570 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000571 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000572
573 // Do nothing if the pointer to export table is NULL.
574 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000575 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000576
577 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
578 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000579 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000580 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000581 ExportDirectory =
582 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000583 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000584}
585
Rui Ueyama74e85132014-11-19 00:18:07 +0000586std::error_code COFFObjectFile::initBaseRelocPtr() {
587 const data_directory *DataEntry;
588 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000589 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000590 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000591 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000592
593 uintptr_t IntPtr = 0;
594 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
595 return EC;
596 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
597 IntPtr);
598 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
599 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000600 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000601}
602
Reid Kleckner2da433e2016-06-02 17:10:43 +0000603std::error_code COFFObjectFile::initDebugDirectoryPtr() {
604 // Get the RVA of the debug directory. Do nothing if it does not exist.
605 const data_directory *DataEntry;
606 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
607 return std::error_code();
608
609 // Do nothing if the RVA is NULL.
610 if (DataEntry->RelativeVirtualAddress == 0)
611 return std::error_code();
612
613 // Check that the size is a multiple of the entry size.
614 if (DataEntry->Size % sizeof(debug_directory) != 0)
615 return object_error::parse_failed;
616
617 uintptr_t IntPtr = 0;
618 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
619 return EC;
620 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
621 if (std::error_code EC = getRvaPtr(
622 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
623 return EC;
624 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
625 return std::error_code();
626}
627
Rafael Espindola48af1c22014-08-19 18:44:46 +0000628COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
629 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000630 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
631 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
632 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
David Majnemerad7b7e72016-06-26 04:36:32 +0000633 ImportDirectory(nullptr),
Rui Ueyama15d99352014-10-03 00:41:58 +0000634 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Kleckner2da433e2016-06-02 17:10:43 +0000635 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
636 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000637 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000638 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000639 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000640
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000641 // The current location in the file where we are looking at.
642 uint64_t CurPtr = 0;
643
644 // PE header is optional and is present only in executables. If it exists,
645 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000646 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000647
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000648 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000649 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000650 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
651 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000652 const auto *DH = reinterpret_cast<const dos_header *>(base());
653 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
654 CurPtr = DH->AddressOfNewExeHeader;
655 // Check the PE magic bytes. ("PE\0\0")
656 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
657 EC = object_error::parse_failed;
658 return;
659 }
660 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
661 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000662 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000663 }
664
Rafael Espindola48af1c22014-08-19 18:44:46 +0000665 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000666 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000667
668 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
669 // import libraries share a common prefix but bigobj is more restrictive.
670 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
671 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
672 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
673 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
674 return;
675
676 // Verify that we are dealing with bigobj.
677 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
678 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
679 sizeof(COFF::BigObjMagic)) == 0) {
680 COFFHeader = nullptr;
681 CurPtr += sizeof(coff_bigobj_file_header);
682 } else {
683 // It's not a bigobj.
684 COFFBigObjHeader = nullptr;
685 }
686 }
687 if (COFFHeader) {
688 // The prior checkSize call may have failed. This isn't a hard error
689 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000690 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000691 CurPtr += sizeof(coff_file_header);
692
693 if (COFFHeader->isImportLibrary())
694 return;
695 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000696
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000697 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000698 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000699 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000700 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000701
702 const uint8_t *DataDirAddr;
703 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000704 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000705 PE32Header = Header;
706 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
707 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000708 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000709 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
710 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
711 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
712 } else {
713 // It's neither PE32 nor PE32+.
714 EC = object_error::parse_failed;
715 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000716 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000717 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000718 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000719 CurPtr += COFFHeader->SizeOfOptionalHeader;
720 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000721
Rafael Espindola48af1c22014-08-19 18:44:46 +0000722 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000723 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000724 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000725
Rui Ueyamac2bed422013-09-27 21:04:00 +0000726 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000727 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000728 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000729 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000730 } else {
731 // We had better not have any symbols if we don't have a symbol table.
732 if (getNumberOfSymbols() != 0) {
733 EC = object_error::parse_failed;
734 return;
735 }
736 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000737
Rui Ueyamac2bed422013-09-27 21:04:00 +0000738 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000739 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000740 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000741 if ((EC = initDelayImportTablePtr()))
742 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000743
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000744 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000745 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000746 return;
747
Rui Ueyama74e85132014-11-19 00:18:07 +0000748 // Initialize the pointer to the base relocation table.
749 if ((EC = initBaseRelocPtr()))
750 return;
751
Reid Kleckner2da433e2016-06-02 17:10:43 +0000752 // Initialize the pointer to the export table.
753 if ((EC = initDebugDirectoryPtr()))
754 return;
755
Rui Ueyama7d099192015-06-09 15:20:42 +0000756 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000757}
758
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000759basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000760 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000761 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000762 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000763}
764
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000765basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000766 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000767 DataRefImpl Ret;
768 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000769 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000770}
771
Rui Ueyamabc654b12013-09-27 21:47:05 +0000772import_directory_iterator COFFObjectFile::import_directory_begin() const {
David Majnemerad7b7e72016-06-26 04:36:32 +0000773 if (!ImportDirectory)
774 return import_directory_end();
775 if (ImportDirectory[0].ImportLookupTableRVA == 0)
776 return import_directory_end();
Rui Ueyamaa045b732014-01-16 03:13:19 +0000777 return import_directory_iterator(
778 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000779}
780
Rui Ueyamabc654b12013-09-27 21:47:05 +0000781import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000782 return import_directory_iterator(
David Majnemerad7b7e72016-06-26 04:36:32 +0000783 ImportDirectoryEntryRef(nullptr, -1, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000784}
David Meyerc429b802012-03-01 22:19:54 +0000785
Rui Ueyama15d99352014-10-03 00:41:58 +0000786delay_import_directory_iterator
787COFFObjectFile::delay_import_directory_begin() const {
788 return delay_import_directory_iterator(
789 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
790}
791
792delay_import_directory_iterator
793COFFObjectFile::delay_import_directory_end() const {
794 return delay_import_directory_iterator(
795 DelayImportDirectoryEntryRef(
796 DelayImportDirectory, NumberOfDelayImportDirectory, this));
797}
798
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000799export_directory_iterator COFFObjectFile::export_directory_begin() const {
800 return export_directory_iterator(
801 ExportDirectoryEntryRef(ExportDirectory, 0, this));
802}
803
804export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000805 if (!ExportDirectory)
806 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000807 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000808 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000809 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000810}
811
Rafael Espindolab5155a52014-02-10 20:24:04 +0000812section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000813 DataRefImpl Ret;
814 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
815 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000816}
817
Rafael Espindolab5155a52014-02-10 20:24:04 +0000818section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000819 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000820 int NumSections =
821 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000822 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
823 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000824}
825
Rui Ueyama74e85132014-11-19 00:18:07 +0000826base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
827 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
828}
829
830base_reloc_iterator COFFObjectFile::base_reloc_end() const {
831 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
832}
833
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000834uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000835 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000836}
837
838StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000839 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000840 case COFF::IMAGE_FILE_MACHINE_I386:
841 return "COFF-i386";
842 case COFF::IMAGE_FILE_MACHINE_AMD64:
843 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000844 case COFF::IMAGE_FILE_MACHINE_ARMNT:
845 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000846 case COFF::IMAGE_FILE_MACHINE_ARM64:
847 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000848 default:
849 return "COFF-<unknown arch>";
850 }
851}
852
853unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000854 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000855 case COFF::IMAGE_FILE_MACHINE_I386:
856 return Triple::x86;
857 case COFF::IMAGE_FILE_MACHINE_AMD64:
858 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000859 case COFF::IMAGE_FILE_MACHINE_ARMNT:
860 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000861 case COFF::IMAGE_FILE_MACHINE_ARM64:
862 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000863 default:
864 return Triple::UnknownArch;
865 }
866}
867
Rui Ueyama979fb402014-10-09 02:16:38 +0000868iterator_range<import_directory_iterator>
869COFFObjectFile::import_directories() const {
870 return make_range(import_directory_begin(), import_directory_end());
871}
872
873iterator_range<delay_import_directory_iterator>
874COFFObjectFile::delay_import_directories() const {
875 return make_range(delay_import_directory_begin(),
876 delay_import_directory_end());
877}
878
879iterator_range<export_directory_iterator>
880COFFObjectFile::export_directories() const {
881 return make_range(export_directory_begin(), export_directory_end());
882}
883
Rui Ueyama74e85132014-11-19 00:18:07 +0000884iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
885 return make_range(base_reloc_begin(), base_reloc_end());
886}
887
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000888std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000889 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000890 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000891}
892
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000893std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000894COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
895 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000896 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000897}
898
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000899std::error_code
900COFFObjectFile::getDataDirectory(uint32_t Index,
901 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000902 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000903 if (!DataDirectory) {
904 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000905 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000906 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000907 assert(PE32Header || PE32PlusHeader);
908 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
909 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000910 if (Index >= NumEnt) {
911 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000912 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000913 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000914 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000915 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000916}
917
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000918std::error_code COFFObjectFile::getSection(int32_t Index,
919 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000920 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000921 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000922 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000923 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000924 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000925 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000926 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000927 }
928 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000929}
930
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000931std::error_code COFFObjectFile::getString(uint32_t Offset,
932 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000933 if (StringTableSize <= 4)
934 // Tried to get a string from an empty string table.
935 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000936 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000937 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000938 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000939 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000940}
941
David Majnemer44f51e52014-09-10 12:51:52 +0000942std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000943 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000944 return getSymbolName(Symbol.getGeneric(), Res);
945}
946
947std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
948 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000949 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000950 if (Symbol->Name.Offset.Zeroes == 0) {
951 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000952 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000953 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000954 }
955
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000956 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000957 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000958 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000959 else
960 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000961 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000962 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000963}
964
David Majnemer44f51e52014-09-10 12:51:52 +0000965ArrayRef<uint8_t>
966COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000967 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000968
David Majnemer44f51e52014-09-10 12:51:52 +0000969 size_t SymbolSize = getSymbolTableEntrySize();
970 if (Symbol.getNumberOfAuxSymbols() > 0) {
971 // AUX data comes immediately after the symbol in COFF
972 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000973# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000974 // Verify that the Aux symbol points to a valid entry in the symbol table.
975 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000976 if (Offset < getPointerToSymbolTable() ||
977 Offset >=
978 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000979 report_fatal_error("Aux Symbol data was outside of symbol table.");
980
David Majnemer44f51e52014-09-10 12:51:52 +0000981 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
982 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000983# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000984 }
David Majnemer44f51e52014-09-10 12:51:52 +0000985 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000986}
987
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000988std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
989 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000990 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000991 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000992 // Null terminated, let ::strlen figure out the length.
993 Name = Sec->Name;
994 else
995 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000996 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000997
998 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000999 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001000 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +00001001 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +00001002 if (decodeBase64StringEntry(Name.substr(2), Offset))
1003 return object_error::parse_failed;
1004 } else {
1005 if (Name.substr(1).getAsInteger(10, Offset))
1006 return object_error::parse_failed;
1007 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001008 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001009 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001010 }
1011
1012 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +00001013 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001014}
1015
David Majnemera9ee5c02014-10-09 08:42:31 +00001016uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1017 // SizeOfRawData and VirtualSize change what they represent depending on
1018 // whether or not we have an executable image.
1019 //
1020 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001021 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +00001022 //
1023 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1024 // actual section size is in VirtualSize. It is possible for VirtualSize to
1025 // be greater than SizeOfRawData; the contents past that point should be
1026 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001027 if (getDOSHeader())
1028 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1029 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001030}
1031
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001032std::error_code
1033COFFObjectFile::getSectionContents(const coff_section *Sec,
1034 ArrayRef<uint8_t> &Res) const {
David Majnemere2129662016-05-28 19:45:51 +00001035 // In COFF, a virtual section won't have any in-file
1036 // content, so the file pointer to the content will be zero.
1037 if (Sec->PointerToRawData == 0)
1038 return object_error::parse_failed;
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001039 // The only thing that we need to verify is that the contents is contained
1040 // within the file bounds. We don't need to make sure it doesn't cover other
1041 // data, as there's nothing that says that is not allowed.
1042 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001043 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001044 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001045 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001046 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001047 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001048}
1049
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001050const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001051 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001052}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001053
Rafael Espindola5e812af2014-01-30 02:49:50 +00001054void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001055 Rel.p = reinterpret_cast<uintptr_t>(
1056 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001057}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001058
Rafael Espindola96d071c2015-06-29 23:29:12 +00001059uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +00001060 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001061 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001062}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001063
Rafael Espindola806f0062013-06-05 01:33:53 +00001064symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001065 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001066 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001067 if (R->SymbolTableIndex >= getNumberOfSymbols())
1068 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001069 if (SymbolTable16)
1070 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1071 else if (SymbolTable32)
1072 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1073 else
David Majnemerc7353b52014-11-25 07:43:14 +00001074 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001075 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001076}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001077
Rafael Espindola99c041b2015-06-30 01:53:01 +00001078uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001079 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001080 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001081}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001082
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001083const coff_section *
1084COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1085 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001086}
1087
David Majnemer44f51e52014-09-10 12:51:52 +00001088COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1089 if (SymbolTable16)
1090 return toSymb<coff_symbol16>(Ref);
1091 if (SymbolTable32)
1092 return toSymb<coff_symbol32>(Ref);
1093 llvm_unreachable("no symbol table pointer!");
1094}
1095
1096COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1097 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001098}
1099
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001100const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001101COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1102 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001103}
1104
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001105iterator_range<const coff_relocation *>
1106COFFObjectFile::getRelocations(const coff_section *Sec) const {
1107 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1108 const coff_relocation *E = I;
1109 if (I)
1110 E += getNumberOfRelocations(Sec, Data, base());
1111 return make_range(I, E);
1112}
1113
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001114#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1115 case COFF::reloc_type: \
1116 Res = #reloc_type; \
1117 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001118
Rafael Espindola41bb4322015-06-30 04:08:37 +00001119void COFFObjectFile::getRelocationTypeName(
1120 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001121 const coff_relocation *Reloc = toRel(Rel);
1122 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001123 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001124 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001125 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001126 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1127 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1128 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1129 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1130 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1131 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1132 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1133 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1134 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1135 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1136 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1137 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1138 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1139 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1140 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1141 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1142 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1143 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001144 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001145 }
1146 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001147 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1148 switch (Reloc->Type) {
1149 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1150 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1151 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1152 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1153 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1154 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1155 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1156 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1157 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1158 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1159 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1160 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1161 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1162 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1163 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1164 default:
1165 Res = "Unknown";
1166 }
1167 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001168 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001169 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001170 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1171 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1172 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1173 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1174 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1175 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1176 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1177 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1178 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1179 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1180 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1181 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001182 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001183 }
1184 break;
1185 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001186 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001187 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001188 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001189}
1190
1191#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1192
Rafael Espindolac66d7612014-08-17 19:09:37 +00001193bool COFFObjectFile::isRelocatableObject() const {
1194 return !DataDirectory;
1195}
1196
Rui Ueyamac2bed422013-09-27 21:04:00 +00001197bool ImportDirectoryEntryRef::
1198operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001199 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001200}
1201
Rafael Espindola5e812af2014-01-30 02:49:50 +00001202void ImportDirectoryEntryRef::moveNext() {
1203 ++Index;
David Majnemerad7b7e72016-06-26 04:36:32 +00001204 if (ImportTable[Index].ImportLookupTableRVA == 0) {
1205 Index = -1;
1206 ImportTable = nullptr;
1207 }
Rui Ueyamac2bed422013-09-27 21:04:00 +00001208}
1209
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001210std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1211 const import_directory_table_entry *&Result) const {
David Majnemerad7b7e72016-06-26 04:36:32 +00001212 return getObject(Result, OwningObject->Data, ImportTable + Index);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001213}
1214
Rui Ueyama861021f2014-10-02 22:05:29 +00001215static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001216makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001217 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001218 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001219 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001220 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001221 }
1222 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001223 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001224}
1225
Rui Ueyama15d99352014-10-03 00:41:58 +00001226static imported_symbol_iterator
1227importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001228 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001229 Object->getRvaPtr(RVA, IntPtr);
1230 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001231}
1232
Rui Ueyama15d99352014-10-03 00:41:58 +00001233static imported_symbol_iterator
1234importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001235 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001236 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001237 // Forward the pointer to the last entry which is null.
1238 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001239 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001240 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1241 while (*Entry++)
1242 ++Index;
1243 } else {
1244 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1245 while (*Entry++)
1246 ++Index;
1247 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001248 return makeImportedSymbolIterator(Object, IntPtr, Index);
1249}
1250
1251imported_symbol_iterator
1252ImportDirectoryEntryRef::imported_symbol_begin() const {
1253 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1254 OwningObject);
1255}
1256
1257imported_symbol_iterator
1258ImportDirectoryEntryRef::imported_symbol_end() const {
1259 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1260 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001261}
1262
Rui Ueyama979fb402014-10-09 02:16:38 +00001263iterator_range<imported_symbol_iterator>
1264ImportDirectoryEntryRef::imported_symbols() const {
1265 return make_range(imported_symbol_begin(), imported_symbol_end());
1266}
1267
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001268std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001269 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001270 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001271 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001272 return EC;
1273 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001274 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001275}
1276
Rui Ueyama1e152d52014-10-02 17:02:18 +00001277std::error_code
1278ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1279 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001280 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001281}
1282
1283std::error_code
1284ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1285 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001286 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001287}
1288
Rui Ueyama15d99352014-10-03 00:41:58 +00001289bool DelayImportDirectoryEntryRef::
1290operator==(const DelayImportDirectoryEntryRef &Other) const {
1291 return Table == Other.Table && Index == Other.Index;
1292}
1293
1294void DelayImportDirectoryEntryRef::moveNext() {
1295 ++Index;
1296}
1297
1298imported_symbol_iterator
1299DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1300 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1301 OwningObject);
1302}
1303
1304imported_symbol_iterator
1305DelayImportDirectoryEntryRef::imported_symbol_end() const {
1306 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1307 OwningObject);
1308}
1309
Rui Ueyama979fb402014-10-09 02:16:38 +00001310iterator_range<imported_symbol_iterator>
1311DelayImportDirectoryEntryRef::imported_symbols() const {
1312 return make_range(imported_symbol_begin(), imported_symbol_end());
1313}
1314
Rui Ueyama15d99352014-10-03 00:41:58 +00001315std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1316 uintptr_t IntPtr = 0;
1317 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1318 return EC;
1319 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001320 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001321}
1322
Rui Ueyama1af08652014-10-03 18:07:18 +00001323std::error_code DelayImportDirectoryEntryRef::
1324getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1325 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001326 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001327}
1328
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001329std::error_code DelayImportDirectoryEntryRef::
1330getImportAddress(int AddrIndex, uint64_t &Result) const {
1331 uint32_t RVA = Table[Index].DelayImportAddressTable +
1332 AddrIndex * (OwningObject->is64() ? 8 : 4);
1333 uintptr_t IntPtr = 0;
1334 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1335 return EC;
1336 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001337 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001338 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001339 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001340 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001341}
1342
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001343bool ExportDirectoryEntryRef::
1344operator==(const ExportDirectoryEntryRef &Other) const {
1345 return ExportTable == Other.ExportTable && Index == Other.Index;
1346}
1347
Rafael Espindola5e812af2014-01-30 02:49:50 +00001348void ExportDirectoryEntryRef::moveNext() {
1349 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001350}
1351
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001352// Returns the name of the current export symbol. If the symbol is exported only
1353// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001354std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001355 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001356 if (std::error_code EC =
1357 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001358 return EC;
1359 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001360 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001361}
1362
Rui Ueyamae5df6092014-01-17 22:02:24 +00001363// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001364std::error_code
1365ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001366 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001367 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001368}
1369
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001370// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001371std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001372 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001373 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001374}
1375
1376// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001377std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001378 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001379 if (std::error_code EC =
1380 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001381 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001382 const export_address_table_entry *entry =
1383 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001384 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001385 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001386}
1387
1388// Returns the name of the current export symbol. If the symbol is exported only
1389// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001390std::error_code
1391ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001392 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001393 if (std::error_code EC =
1394 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001395 return EC;
1396 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1397
1398 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1399 int Offset = 0;
1400 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1401 I < E; ++I, ++Offset) {
1402 if (*I != Index)
1403 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001404 if (std::error_code EC =
1405 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001406 return EC;
1407 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001408 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001409 return EC;
1410 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001411 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001412 }
1413 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001414 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001415}
1416
Rui Ueyama6161b382016-01-12 23:28:42 +00001417std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1418 const data_directory *DataEntry;
1419 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1420 return EC;
1421 uint32_t RVA;
1422 if (auto EC = getExportRVA(RVA))
1423 return EC;
1424 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1425 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1426 Result = (Begin <= RVA && RVA < End);
1427 return std::error_code();
1428}
1429
1430std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1431 uint32_t RVA;
1432 if (auto EC = getExportRVA(RVA))
1433 return EC;
1434 uintptr_t IntPtr = 0;
1435 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1436 return EC;
1437 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1438 return std::error_code();
1439}
1440
Rui Ueyama861021f2014-10-02 22:05:29 +00001441bool ImportedSymbolRef::
1442operator==(const ImportedSymbolRef &Other) const {
1443 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1444 && Index == Other.Index;
1445}
1446
1447void ImportedSymbolRef::moveNext() {
1448 ++Index;
1449}
1450
1451std::error_code
1452ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1453 uint32_t RVA;
1454 if (Entry32) {
1455 // If a symbol is imported only by ordinal, it has no name.
1456 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001457 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001458 RVA = Entry32[Index].getHintNameRVA();
1459 } else {
1460 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001461 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001462 RVA = Entry64[Index].getHintNameRVA();
1463 }
1464 uintptr_t IntPtr = 0;
1465 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1466 return EC;
1467 // +2 because the first two bytes is hint.
1468 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001469 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001470}
1471
David Majnemerad7b7e72016-06-26 04:36:32 +00001472std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1473 if (Entry32)
1474 Result = Entry32[Index].isOrdinal();
1475 else
1476 Result = Entry64[Index].isOrdinal();
1477 return std::error_code();
1478}
1479
1480std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1481 if (Entry32)
1482 Result = Entry32[Index].getHintNameRVA();
1483 else
1484 Result = Entry64[Index].getHintNameRVA();
1485 return std::error_code();
1486}
1487
Rui Ueyama861021f2014-10-02 22:05:29 +00001488std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1489 uint32_t RVA;
1490 if (Entry32) {
1491 if (Entry32[Index].isOrdinal()) {
1492 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001493 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001494 }
1495 RVA = Entry32[Index].getHintNameRVA();
1496 } else {
1497 if (Entry64[Index].isOrdinal()) {
1498 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001499 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001500 }
1501 RVA = Entry64[Index].getHintNameRVA();
1502 }
1503 uintptr_t IntPtr = 0;
1504 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1505 return EC;
1506 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001507 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001508}
1509
Rafael Espindola437b0d52014-07-31 03:12:45 +00001510ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001511ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001512 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001513 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001514 if (EC)
1515 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001516 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001517}
Rui Ueyama74e85132014-11-19 00:18:07 +00001518
1519bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1520 return Header == Other.Header && Index == Other.Index;
1521}
1522
1523void BaseRelocRef::moveNext() {
1524 // Header->BlockSize is the size of the current block, including the
1525 // size of the header itself.
1526 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001527 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001528 if (Size == Header->BlockSize) {
1529 // .reloc contains a list of base relocation blocks. Each block
1530 // consists of the header followed by entries. The header contains
1531 // how many entories will follow. When we reach the end of the
1532 // current block, proceed to the next block.
1533 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1534 reinterpret_cast<const uint8_t *>(Header) + Size);
1535 Index = 0;
1536 } else {
1537 ++Index;
1538 }
1539}
1540
1541std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1542 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1543 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001544 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001545}
1546
1547std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1548 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1549 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001550 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001551}