blob: 878b93fcda0d17a41d1fa3570ffd2dbfa37e12ba [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 Ueyama1e152d52014-10-02 17:02:18 +0000532 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000533 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000534 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000535
536 // Find the section that contains the RVA. This is needed because the RVA is
537 // the import table's memory address which is different from its file offset.
538 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000539 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000540 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000541 ImportDirectory = reinterpret_cast<
542 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000543 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000544}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000545
Rui Ueyama15d99352014-10-03 00:41:58 +0000546// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
547std::error_code COFFObjectFile::initDelayImportTablePtr() {
548 const data_directory *DataEntry;
549 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000550 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000551 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000552 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000553
554 uint32_t RVA = DataEntry->RelativeVirtualAddress;
555 NumberOfDelayImportDirectory = DataEntry->Size /
556 sizeof(delay_import_directory_table_entry) - 1;
557
558 uintptr_t IntPtr = 0;
559 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
560 return EC;
561 DelayImportDirectory = reinterpret_cast<
562 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000563 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000564}
565
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000566// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000567std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000568 // First, we get the RVA of the export table. If the file lacks a pointer to
569 // the export table, do nothing.
570 const data_directory *DataEntry;
571 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000572 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000573
574 // Do nothing if the pointer to export table is NULL.
575 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000576 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000577
578 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
579 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000580 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000581 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000582 ExportDirectory =
583 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000584 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000585}
586
Rui Ueyama74e85132014-11-19 00:18:07 +0000587std::error_code COFFObjectFile::initBaseRelocPtr() {
588 const data_directory *DataEntry;
589 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000590 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000591 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000592 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000593
594 uintptr_t IntPtr = 0;
595 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
596 return EC;
597 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
598 IntPtr);
599 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
600 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000601 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000602}
603
Reid Kleckner2da433e2016-06-02 17:10:43 +0000604std::error_code COFFObjectFile::initDebugDirectoryPtr() {
605 // Get the RVA of the debug directory. Do nothing if it does not exist.
606 const data_directory *DataEntry;
607 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
608 return std::error_code();
609
610 // Do nothing if the RVA is NULL.
611 if (DataEntry->RelativeVirtualAddress == 0)
612 return std::error_code();
613
614 // Check that the size is a multiple of the entry size.
615 if (DataEntry->Size % sizeof(debug_directory) != 0)
616 return object_error::parse_failed;
617
618 uintptr_t IntPtr = 0;
619 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
620 return EC;
621 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
622 if (std::error_code EC = getRvaPtr(
623 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
624 return EC;
625 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
626 return std::error_code();
627}
628
Rafael Espindola48af1c22014-08-19 18:44:46 +0000629COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
630 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000631 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
632 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
633 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
634 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000635 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Kleckner2da433e2016-06-02 17:10:43 +0000636 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
637 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000638 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000639 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000640 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000641
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000642 // The current location in the file where we are looking at.
643 uint64_t CurPtr = 0;
644
645 // PE header is optional and is present only in executables. If it exists,
646 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000647 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000648
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000649 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000650 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000651 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
652 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000653 const auto *DH = reinterpret_cast<const dos_header *>(base());
654 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
655 CurPtr = DH->AddressOfNewExeHeader;
656 // Check the PE magic bytes. ("PE\0\0")
657 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
658 EC = object_error::parse_failed;
659 return;
660 }
661 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
662 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000663 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000664 }
665
Rafael Espindola48af1c22014-08-19 18:44:46 +0000666 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000667 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000668
669 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
670 // import libraries share a common prefix but bigobj is more restrictive.
671 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
672 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
673 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
674 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
675 return;
676
677 // Verify that we are dealing with bigobj.
678 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
679 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
680 sizeof(COFF::BigObjMagic)) == 0) {
681 COFFHeader = nullptr;
682 CurPtr += sizeof(coff_bigobj_file_header);
683 } else {
684 // It's not a bigobj.
685 COFFBigObjHeader = nullptr;
686 }
687 }
688 if (COFFHeader) {
689 // The prior checkSize call may have failed. This isn't a hard error
690 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000691 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000692 CurPtr += sizeof(coff_file_header);
693
694 if (COFFHeader->isImportLibrary())
695 return;
696 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000697
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000698 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000699 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000700 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000701 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000702
703 const uint8_t *DataDirAddr;
704 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000705 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000706 PE32Header = Header;
707 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
708 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000709 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000710 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
711 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
712 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
713 } else {
714 // It's neither PE32 nor PE32+.
715 EC = object_error::parse_failed;
716 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000717 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000718 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000719 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000720 CurPtr += COFFHeader->SizeOfOptionalHeader;
721 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000722
Rafael Espindola48af1c22014-08-19 18:44:46 +0000723 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000724 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000725 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000726
Rui Ueyamac2bed422013-09-27 21:04:00 +0000727 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000728 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000729 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000730 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000731 } else {
732 // We had better not have any symbols if we don't have a symbol table.
733 if (getNumberOfSymbols() != 0) {
734 EC = object_error::parse_failed;
735 return;
736 }
737 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000738
Rui Ueyamac2bed422013-09-27 21:04:00 +0000739 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000740 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000741 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000742 if ((EC = initDelayImportTablePtr()))
743 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000744
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000745 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000746 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000747 return;
748
Rui Ueyama74e85132014-11-19 00:18:07 +0000749 // Initialize the pointer to the base relocation table.
750 if ((EC = initBaseRelocPtr()))
751 return;
752
Reid Kleckner2da433e2016-06-02 17:10:43 +0000753 // Initialize the pointer to the export table.
754 if ((EC = initDebugDirectoryPtr()))
755 return;
756
Rui Ueyama7d099192015-06-09 15:20:42 +0000757 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000758}
759
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000760basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000761 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000762 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000763 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000764}
765
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000766basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000767 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000768 DataRefImpl Ret;
769 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000770 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000771}
772
Rui Ueyamabc654b12013-09-27 21:47:05 +0000773import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000774 return import_directory_iterator(
775 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000776}
777
Rui Ueyamabc654b12013-09-27 21:47:05 +0000778import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000779 return import_directory_iterator(
780 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000781}
David Meyerc429b802012-03-01 22:19:54 +0000782
Rui Ueyama15d99352014-10-03 00:41:58 +0000783delay_import_directory_iterator
784COFFObjectFile::delay_import_directory_begin() const {
785 return delay_import_directory_iterator(
786 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
787}
788
789delay_import_directory_iterator
790COFFObjectFile::delay_import_directory_end() const {
791 return delay_import_directory_iterator(
792 DelayImportDirectoryEntryRef(
793 DelayImportDirectory, NumberOfDelayImportDirectory, this));
794}
795
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000796export_directory_iterator COFFObjectFile::export_directory_begin() const {
797 return export_directory_iterator(
798 ExportDirectoryEntryRef(ExportDirectory, 0, this));
799}
800
801export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000802 if (!ExportDirectory)
803 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000804 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000805 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000806 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000807}
808
Rafael Espindolab5155a52014-02-10 20:24:04 +0000809section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000810 DataRefImpl Ret;
811 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
812 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000813}
814
Rafael Espindolab5155a52014-02-10 20:24:04 +0000815section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000816 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000817 int NumSections =
818 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000819 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
820 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000821}
822
Rui Ueyama74e85132014-11-19 00:18:07 +0000823base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
824 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
825}
826
827base_reloc_iterator COFFObjectFile::base_reloc_end() const {
828 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
829}
830
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000831uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000832 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000833}
834
835StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000836 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000837 case COFF::IMAGE_FILE_MACHINE_I386:
838 return "COFF-i386";
839 case COFF::IMAGE_FILE_MACHINE_AMD64:
840 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000841 case COFF::IMAGE_FILE_MACHINE_ARMNT:
842 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000843 case COFF::IMAGE_FILE_MACHINE_ARM64:
844 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000845 default:
846 return "COFF-<unknown arch>";
847 }
848}
849
850unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000851 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000852 case COFF::IMAGE_FILE_MACHINE_I386:
853 return Triple::x86;
854 case COFF::IMAGE_FILE_MACHINE_AMD64:
855 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000856 case COFF::IMAGE_FILE_MACHINE_ARMNT:
857 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000858 case COFF::IMAGE_FILE_MACHINE_ARM64:
859 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000860 default:
861 return Triple::UnknownArch;
862 }
863}
864
Rui Ueyama979fb402014-10-09 02:16:38 +0000865iterator_range<import_directory_iterator>
866COFFObjectFile::import_directories() const {
867 return make_range(import_directory_begin(), import_directory_end());
868}
869
870iterator_range<delay_import_directory_iterator>
871COFFObjectFile::delay_import_directories() const {
872 return make_range(delay_import_directory_begin(),
873 delay_import_directory_end());
874}
875
876iterator_range<export_directory_iterator>
877COFFObjectFile::export_directories() const {
878 return make_range(export_directory_begin(), export_directory_end());
879}
880
Rui Ueyama74e85132014-11-19 00:18:07 +0000881iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
882 return make_range(base_reloc_begin(), base_reloc_end());
883}
884
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000885std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000886 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000887 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000888}
889
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000890std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000891COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
892 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000893 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000894}
895
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000896std::error_code
897COFFObjectFile::getDataDirectory(uint32_t Index,
898 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000899 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000900 if (!DataDirectory) {
901 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000902 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000903 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000904 assert(PE32Header || PE32PlusHeader);
905 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
906 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000907 if (Index >= NumEnt) {
908 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000909 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000910 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000911 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000912 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000913}
914
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000915std::error_code COFFObjectFile::getSection(int32_t Index,
916 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000917 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000918 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000919 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000920 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000921 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000922 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000923 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000924 }
925 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000926}
927
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000928std::error_code COFFObjectFile::getString(uint32_t Offset,
929 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000930 if (StringTableSize <= 4)
931 // Tried to get a string from an empty string table.
932 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000933 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000934 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000935 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000936 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000937}
938
David Majnemer44f51e52014-09-10 12:51:52 +0000939std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000940 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000941 return getSymbolName(Symbol.getGeneric(), Res);
942}
943
944std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
945 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000946 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000947 if (Symbol->Name.Offset.Zeroes == 0) {
948 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000949 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000950 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000951 }
952
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000953 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000954 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000955 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000956 else
957 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000958 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000959 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000960}
961
David Majnemer44f51e52014-09-10 12:51:52 +0000962ArrayRef<uint8_t>
963COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000964 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000965
David Majnemer44f51e52014-09-10 12:51:52 +0000966 size_t SymbolSize = getSymbolTableEntrySize();
967 if (Symbol.getNumberOfAuxSymbols() > 0) {
968 // AUX data comes immediately after the symbol in COFF
969 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000970# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000971 // Verify that the Aux symbol points to a valid entry in the symbol table.
972 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000973 if (Offset < getPointerToSymbolTable() ||
974 Offset >=
975 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000976 report_fatal_error("Aux Symbol data was outside of symbol table.");
977
David Majnemer44f51e52014-09-10 12:51:52 +0000978 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
979 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000980# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000981 }
David Majnemer44f51e52014-09-10 12:51:52 +0000982 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000983}
984
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000985std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
986 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000987 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000988 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000989 // Null terminated, let ::strlen figure out the length.
990 Name = Sec->Name;
991 else
992 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000993 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000994
995 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000996 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000997 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000998 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000999 if (decodeBase64StringEntry(Name.substr(2), Offset))
1000 return object_error::parse_failed;
1001 } else {
1002 if (Name.substr(1).getAsInteger(10, Offset))
1003 return object_error::parse_failed;
1004 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001005 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001006 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001007 }
1008
1009 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +00001010 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001011}
1012
David Majnemera9ee5c02014-10-09 08:42:31 +00001013uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1014 // SizeOfRawData and VirtualSize change what they represent depending on
1015 // whether or not we have an executable image.
1016 //
1017 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001018 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +00001019 //
1020 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1021 // actual section size is in VirtualSize. It is possible for VirtualSize to
1022 // be greater than SizeOfRawData; the contents past that point should be
1023 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001024 if (getDOSHeader())
1025 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1026 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001027}
1028
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001029std::error_code
1030COFFObjectFile::getSectionContents(const coff_section *Sec,
1031 ArrayRef<uint8_t> &Res) const {
David Majnemere2129662016-05-28 19:45:51 +00001032 // In COFF, a virtual section won't have any in-file
1033 // content, so the file pointer to the content will be zero.
1034 if (Sec->PointerToRawData == 0)
1035 return object_error::parse_failed;
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001036 // The only thing that we need to verify is that the contents is contained
1037 // within the file bounds. We don't need to make sure it doesn't cover other
1038 // data, as there's nothing that says that is not allowed.
1039 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001040 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001041 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001042 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001043 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001044 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001045}
1046
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001047const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001048 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001049}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001050
Rafael Espindola5e812af2014-01-30 02:49:50 +00001051void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001052 Rel.p = reinterpret_cast<uintptr_t>(
1053 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001054}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001055
Rafael Espindola96d071c2015-06-29 23:29:12 +00001056uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +00001057 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001058 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001059}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001060
Rafael Espindola806f0062013-06-05 01:33:53 +00001061symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001062 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001063 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001064 if (R->SymbolTableIndex >= getNumberOfSymbols())
1065 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001066 if (SymbolTable16)
1067 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1068 else if (SymbolTable32)
1069 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1070 else
David Majnemerc7353b52014-11-25 07:43:14 +00001071 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001072 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001073}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001074
Rafael Espindola99c041b2015-06-30 01:53:01 +00001075uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001076 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001077 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001078}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001079
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001080const coff_section *
1081COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1082 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001083}
1084
David Majnemer44f51e52014-09-10 12:51:52 +00001085COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1086 if (SymbolTable16)
1087 return toSymb<coff_symbol16>(Ref);
1088 if (SymbolTable32)
1089 return toSymb<coff_symbol32>(Ref);
1090 llvm_unreachable("no symbol table pointer!");
1091}
1092
1093COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1094 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001095}
1096
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001097const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001098COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1099 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001100}
1101
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001102iterator_range<const coff_relocation *>
1103COFFObjectFile::getRelocations(const coff_section *Sec) const {
1104 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1105 const coff_relocation *E = I;
1106 if (I)
1107 E += getNumberOfRelocations(Sec, Data, base());
1108 return make_range(I, E);
1109}
1110
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001111#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1112 case COFF::reloc_type: \
1113 Res = #reloc_type; \
1114 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001115
Rafael Espindola41bb4322015-06-30 04:08:37 +00001116void COFFObjectFile::getRelocationTypeName(
1117 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001118 const coff_relocation *Reloc = toRel(Rel);
1119 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001120 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001121 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001122 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001123 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1124 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1125 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1126 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1127 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1128 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1129 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1130 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1131 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1132 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1133 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1134 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1135 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1136 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1137 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1138 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1139 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1140 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001141 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001142 }
1143 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001144 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1145 switch (Reloc->Type) {
1146 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1147 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1148 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1149 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1150 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1151 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1152 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1153 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1154 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1155 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1156 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1157 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1158 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1159 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1160 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1161 default:
1162 Res = "Unknown";
1163 }
1164 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001165 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001166 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001167 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1168 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1169 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1170 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1171 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1172 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1173 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1174 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1175 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1176 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1177 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1178 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001179 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001180 }
1181 break;
1182 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001183 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001184 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001185 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001186}
1187
1188#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1189
Rafael Espindolac66d7612014-08-17 19:09:37 +00001190bool COFFObjectFile::isRelocatableObject() const {
1191 return !DataDirectory;
1192}
1193
Rui Ueyamac2bed422013-09-27 21:04:00 +00001194bool ImportDirectoryEntryRef::
1195operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001196 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001197}
1198
Rafael Espindola5e812af2014-01-30 02:49:50 +00001199void ImportDirectoryEntryRef::moveNext() {
1200 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001201}
1202
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001203std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1204 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001205 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001206 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001207}
1208
Rui Ueyama861021f2014-10-02 22:05:29 +00001209static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001210makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001211 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001212 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001213 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001214 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001215 }
1216 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001217 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001218}
1219
Rui Ueyama15d99352014-10-03 00:41:58 +00001220static imported_symbol_iterator
1221importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001222 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001223 Object->getRvaPtr(RVA, IntPtr);
1224 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001225}
1226
Rui Ueyama15d99352014-10-03 00:41:58 +00001227static imported_symbol_iterator
1228importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001229 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001230 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001231 // Forward the pointer to the last entry which is null.
1232 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001233 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001234 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1235 while (*Entry++)
1236 ++Index;
1237 } else {
1238 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1239 while (*Entry++)
1240 ++Index;
1241 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001242 return makeImportedSymbolIterator(Object, IntPtr, Index);
1243}
1244
1245imported_symbol_iterator
1246ImportDirectoryEntryRef::imported_symbol_begin() const {
1247 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1248 OwningObject);
1249}
1250
1251imported_symbol_iterator
1252ImportDirectoryEntryRef::imported_symbol_end() const {
1253 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1254 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001255}
1256
Rui Ueyama979fb402014-10-09 02:16:38 +00001257iterator_range<imported_symbol_iterator>
1258ImportDirectoryEntryRef::imported_symbols() const {
1259 return make_range(imported_symbol_begin(), imported_symbol_end());
1260}
1261
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001262std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001263 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001264 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001265 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001266 return EC;
1267 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001268 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001269}
1270
Rui Ueyama1e152d52014-10-02 17:02:18 +00001271std::error_code
1272ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1273 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001274 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001275}
1276
1277std::error_code
1278ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1279 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001280 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001281}
1282
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001283std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001284 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001285 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001286 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1287 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001288 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001289 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001290 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001291}
1292
Rui Ueyama15d99352014-10-03 00:41:58 +00001293bool DelayImportDirectoryEntryRef::
1294operator==(const DelayImportDirectoryEntryRef &Other) const {
1295 return Table == Other.Table && Index == Other.Index;
1296}
1297
1298void DelayImportDirectoryEntryRef::moveNext() {
1299 ++Index;
1300}
1301
1302imported_symbol_iterator
1303DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1304 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1305 OwningObject);
1306}
1307
1308imported_symbol_iterator
1309DelayImportDirectoryEntryRef::imported_symbol_end() const {
1310 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1311 OwningObject);
1312}
1313
Rui Ueyama979fb402014-10-09 02:16:38 +00001314iterator_range<imported_symbol_iterator>
1315DelayImportDirectoryEntryRef::imported_symbols() const {
1316 return make_range(imported_symbol_begin(), imported_symbol_end());
1317}
1318
Rui Ueyama15d99352014-10-03 00:41:58 +00001319std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1320 uintptr_t IntPtr = 0;
1321 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1322 return EC;
1323 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001324 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001325}
1326
Rui Ueyama1af08652014-10-03 18:07:18 +00001327std::error_code DelayImportDirectoryEntryRef::
1328getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1329 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001330 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001331}
1332
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001333std::error_code DelayImportDirectoryEntryRef::
1334getImportAddress(int AddrIndex, uint64_t &Result) const {
1335 uint32_t RVA = Table[Index].DelayImportAddressTable +
1336 AddrIndex * (OwningObject->is64() ? 8 : 4);
1337 uintptr_t IntPtr = 0;
1338 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1339 return EC;
1340 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001341 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001342 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001343 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001344 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001345}
1346
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001347bool ExportDirectoryEntryRef::
1348operator==(const ExportDirectoryEntryRef &Other) const {
1349 return ExportTable == Other.ExportTable && Index == Other.Index;
1350}
1351
Rafael Espindola5e812af2014-01-30 02:49:50 +00001352void ExportDirectoryEntryRef::moveNext() {
1353 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001354}
1355
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001356// Returns the name of the current export symbol. If the symbol is exported only
1357// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001358std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001359 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001360 if (std::error_code EC =
1361 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001362 return EC;
1363 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001364 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001365}
1366
Rui Ueyamae5df6092014-01-17 22:02:24 +00001367// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001368std::error_code
1369ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001370 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001371 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001372}
1373
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001374// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001375std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001376 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001377 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001378}
1379
1380// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001381std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001382 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001383 if (std::error_code EC =
1384 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001385 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001386 const export_address_table_entry *entry =
1387 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001388 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001389 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001390}
1391
1392// Returns the name of the current export symbol. If the symbol is exported only
1393// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001394std::error_code
1395ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001396 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001397 if (std::error_code EC =
1398 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001399 return EC;
1400 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1401
1402 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1403 int Offset = 0;
1404 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1405 I < E; ++I, ++Offset) {
1406 if (*I != Index)
1407 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001408 if (std::error_code EC =
1409 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001410 return EC;
1411 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001412 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001413 return EC;
1414 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001415 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001416 }
1417 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001418 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001419}
1420
Rui Ueyama6161b382016-01-12 23:28:42 +00001421std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1422 const data_directory *DataEntry;
1423 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1424 return EC;
1425 uint32_t RVA;
1426 if (auto EC = getExportRVA(RVA))
1427 return EC;
1428 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1429 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1430 Result = (Begin <= RVA && RVA < End);
1431 return std::error_code();
1432}
1433
1434std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1435 uint32_t RVA;
1436 if (auto EC = getExportRVA(RVA))
1437 return EC;
1438 uintptr_t IntPtr = 0;
1439 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1440 return EC;
1441 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1442 return std::error_code();
1443}
1444
Rui Ueyama861021f2014-10-02 22:05:29 +00001445bool ImportedSymbolRef::
1446operator==(const ImportedSymbolRef &Other) const {
1447 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1448 && Index == Other.Index;
1449}
1450
1451void ImportedSymbolRef::moveNext() {
1452 ++Index;
1453}
1454
1455std::error_code
1456ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1457 uint32_t RVA;
1458 if (Entry32) {
1459 // If a symbol is imported only by ordinal, it has no name.
1460 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001461 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001462 RVA = Entry32[Index].getHintNameRVA();
1463 } else {
1464 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001465 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001466 RVA = Entry64[Index].getHintNameRVA();
1467 }
1468 uintptr_t IntPtr = 0;
1469 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1470 return EC;
1471 // +2 because the first two bytes is hint.
1472 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001473 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001474}
1475
1476std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1477 uint32_t RVA;
1478 if (Entry32) {
1479 if (Entry32[Index].isOrdinal()) {
1480 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001481 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001482 }
1483 RVA = Entry32[Index].getHintNameRVA();
1484 } else {
1485 if (Entry64[Index].isOrdinal()) {
1486 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001487 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001488 }
1489 RVA = Entry64[Index].getHintNameRVA();
1490 }
1491 uintptr_t IntPtr = 0;
1492 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1493 return EC;
1494 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001495 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001496}
1497
Rafael Espindola437b0d52014-07-31 03:12:45 +00001498ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001499ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001500 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001501 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001502 if (EC)
1503 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001504 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001505}
Rui Ueyama74e85132014-11-19 00:18:07 +00001506
1507bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1508 return Header == Other.Header && Index == Other.Index;
1509}
1510
1511void BaseRelocRef::moveNext() {
1512 // Header->BlockSize is the size of the current block, including the
1513 // size of the header itself.
1514 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001515 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001516 if (Size == Header->BlockSize) {
1517 // .reloc contains a list of base relocation blocks. Each block
1518 // consists of the header followed by entries. The header contains
1519 // how many entories will follow. When we reach the end of the
1520 // current block, proceed to the next block.
1521 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1522 reinterpret_cast<const uint8_t *>(Header) + Size);
1523 Index = 0;
1524 } else {
1525 ++Index;
1526 }
1527}
1528
1529std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1530 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1531 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001532 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001533}
1534
1535std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1536 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1537 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001538 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001539}