blob: 601a7faba32a74228585ce6ee30396b5b6478c37 [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
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000490std::error_code
491COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir,
492 const codeview::DebugInfo *&PDBInfo,
493 StringRef &PDBFileName) const {
Reid Kleckner2da433e2016-06-02 17:10:43 +0000494 ArrayRef<uint8_t> InfoBytes;
495 if (std::error_code EC = getRvaAndSizeAsBytes(
496 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
497 return EC;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000498 if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
Reid Kleckner2da433e2016-06-02 17:10:43 +0000499 return object_error::parse_failed;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000500 PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
501 InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
Reid Kleckner2da433e2016-06-02 17:10:43 +0000502 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
503 InfoBytes.size());
504 // Truncate the name at the first null byte. Ignore any padding.
505 PDBFileName = PDBFileName.split('\0').first;
506 return std::error_code();
507}
508
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000509std::error_code
510COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo,
511 StringRef &PDBFileName) const {
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000512 for (const debug_directory &D : debug_directories())
513 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW)
514 return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
515 // If we get here, there is no PDB info to return.
516 PDBInfo = nullptr;
517 PDBFileName = StringRef();
518 return std::error_code();
519}
520
Rui Ueyamac2bed422013-09-27 21:04:00 +0000521// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000522std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000523 // First, we get the RVA of the import table. If the file lacks a pointer to
524 // the import table, do nothing.
525 const data_directory *DataEntry;
526 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000527 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000528
529 // Do nothing if the pointer to import table is NULL.
530 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000531 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000532
533 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000534
535 // Find the section that contains the RVA. This is needed because the RVA is
536 // the import table's memory address which is different from its file offset.
537 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000538 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000539 return EC;
David Majnemerad7b7e72016-06-26 04:36:32 +0000540 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
541 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000542 ImportDirectory = reinterpret_cast<
David Majnemer1c0aa042016-07-31 19:25:21 +0000543 const coff_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000544 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000545}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000546
Rui Ueyama15d99352014-10-03 00:41:58 +0000547// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
548std::error_code COFFObjectFile::initDelayImportTablePtr() {
549 const data_directory *DataEntry;
550 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000551 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000552 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000553 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000554
555 uint32_t RVA = DataEntry->RelativeVirtualAddress;
556 NumberOfDelayImportDirectory = DataEntry->Size /
557 sizeof(delay_import_directory_table_entry) - 1;
558
559 uintptr_t IntPtr = 0;
560 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
561 return EC;
562 DelayImportDirectory = reinterpret_cast<
563 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000564 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000565}
566
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000567// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000568std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000569 // First, we get the RVA of the export table. If the file lacks a pointer to
570 // the export table, do nothing.
571 const data_directory *DataEntry;
572 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000573 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000574
575 // Do nothing if the pointer to export table is NULL.
576 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000577 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000578
579 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
580 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000581 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000582 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000583 ExportDirectory =
584 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000585 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000586}
587
Rui Ueyama74e85132014-11-19 00:18:07 +0000588std::error_code COFFObjectFile::initBaseRelocPtr() {
589 const data_directory *DataEntry;
590 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000591 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000592 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000593 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000594
595 uintptr_t IntPtr = 0;
596 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
597 return EC;
598 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
599 IntPtr);
600 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
601 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000602 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000603}
604
Reid Kleckner2da433e2016-06-02 17:10:43 +0000605std::error_code COFFObjectFile::initDebugDirectoryPtr() {
606 // Get the RVA of the debug directory. Do nothing if it does not exist.
607 const data_directory *DataEntry;
608 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
609 return std::error_code();
610
611 // Do nothing if the RVA is NULL.
612 if (DataEntry->RelativeVirtualAddress == 0)
613 return std::error_code();
614
615 // Check that the size is a multiple of the entry size.
616 if (DataEntry->Size % sizeof(debug_directory) != 0)
617 return object_error::parse_failed;
618
619 uintptr_t IntPtr = 0;
620 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
621 return EC;
622 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
623 if (std::error_code EC = getRvaPtr(
624 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
625 return EC;
626 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
627 return std::error_code();
628}
629
Rafael Espindola48af1c22014-08-19 18:44:46 +0000630COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
631 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000632 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
633 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
634 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
David Majnemerad7b7e72016-06-26 04:36:32 +0000635 ImportDirectory(nullptr),
Rui Ueyama15d99352014-10-03 00:41:58 +0000636 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Kleckner2da433e2016-06-02 17:10:43 +0000637 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
638 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000639 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000640 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000641 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000642
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000643 // The current location in the file where we are looking at.
644 uint64_t CurPtr = 0;
645
646 // PE header is optional and is present only in executables. If it exists,
647 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000648 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000649
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000650 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000651 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000652 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
653 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000654 const auto *DH = reinterpret_cast<const dos_header *>(base());
655 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
656 CurPtr = DH->AddressOfNewExeHeader;
657 // Check the PE magic bytes. ("PE\0\0")
658 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
659 EC = object_error::parse_failed;
660 return;
661 }
662 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
663 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000664 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000665 }
666
Rafael Espindola48af1c22014-08-19 18:44:46 +0000667 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000668 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000669
670 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
671 // import libraries share a common prefix but bigobj is more restrictive.
672 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
673 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
674 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
675 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
676 return;
677
678 // Verify that we are dealing with bigobj.
679 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
680 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
681 sizeof(COFF::BigObjMagic)) == 0) {
682 COFFHeader = nullptr;
683 CurPtr += sizeof(coff_bigobj_file_header);
684 } else {
685 // It's not a bigobj.
686 COFFBigObjHeader = nullptr;
687 }
688 }
689 if (COFFHeader) {
690 // The prior checkSize call may have failed. This isn't a hard error
691 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000692 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000693 CurPtr += sizeof(coff_file_header);
694
695 if (COFFHeader->isImportLibrary())
696 return;
697 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000698
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000699 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000700 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000701 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000702 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000703
704 const uint8_t *DataDirAddr;
705 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000706 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000707 PE32Header = Header;
708 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
709 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000710 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000711 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
712 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
713 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
714 } else {
715 // It's neither PE32 nor PE32+.
716 EC = object_error::parse_failed;
717 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000718 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000719 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000720 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000721 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000722
Rui Ueyama8950a532016-08-11 22:02:44 +0000723 if (COFFHeader)
724 CurPtr += COFFHeader->SizeOfOptionalHeader;
725
Rafael Espindola48af1c22014-08-19 18:44:46 +0000726 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000727 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000728 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000729
Rui Ueyamac2bed422013-09-27 21:04:00 +0000730 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000731 if (getPointerToSymbolTable() != 0) {
David Majnemerac8cfab2016-08-30 20:20:24 +0000732 if ((EC = initSymbolTablePtr())) {
733 SymbolTable16 = nullptr;
734 SymbolTable32 = nullptr;
735 StringTable = nullptr;
736 StringTableSize = 0;
737 }
David Majnemer236b0ca2014-11-17 11:17:17 +0000738 } else {
739 // We had better not have any symbols if we don't have a symbol table.
740 if (getNumberOfSymbols() != 0) {
741 EC = object_error::parse_failed;
742 return;
743 }
744 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000745
Rui Ueyamac2bed422013-09-27 21:04:00 +0000746 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000747 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000748 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000749 if ((EC = initDelayImportTablePtr()))
750 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000751
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000752 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000753 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000754 return;
755
Rui Ueyama74e85132014-11-19 00:18:07 +0000756 // Initialize the pointer to the base relocation table.
757 if ((EC = initBaseRelocPtr()))
758 return;
759
Reid Kleckner2da433e2016-06-02 17:10:43 +0000760 // Initialize the pointer to the export table.
761 if ((EC = initDebugDirectoryPtr()))
762 return;
763
Rui Ueyama7d099192015-06-09 15:20:42 +0000764 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000765}
766
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000767basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000768 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000769 Ret.p = getSymbolTable();
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
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000773basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000774 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000775 DataRefImpl Ret;
776 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000777 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000778}
779
Rui Ueyamabc654b12013-09-27 21:47:05 +0000780import_directory_iterator COFFObjectFile::import_directory_begin() const {
David Majnemerad7b7e72016-06-26 04:36:32 +0000781 if (!ImportDirectory)
782 return import_directory_end();
David Majnemer1c0aa042016-07-31 19:25:21 +0000783 if (ImportDirectory->isNull())
David Majnemerad7b7e72016-06-26 04:36:32 +0000784 return import_directory_end();
Rui Ueyamaa045b732014-01-16 03:13:19 +0000785 return import_directory_iterator(
786 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000787}
788
Rui Ueyamabc654b12013-09-27 21:47:05 +0000789import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000790 return import_directory_iterator(
David Majnemerad7b7e72016-06-26 04:36:32 +0000791 ImportDirectoryEntryRef(nullptr, -1, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000792}
David Meyerc429b802012-03-01 22:19:54 +0000793
Rui Ueyama15d99352014-10-03 00:41:58 +0000794delay_import_directory_iterator
795COFFObjectFile::delay_import_directory_begin() const {
796 return delay_import_directory_iterator(
797 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
798}
799
800delay_import_directory_iterator
801COFFObjectFile::delay_import_directory_end() const {
802 return delay_import_directory_iterator(
803 DelayImportDirectoryEntryRef(
804 DelayImportDirectory, NumberOfDelayImportDirectory, this));
805}
806
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000807export_directory_iterator COFFObjectFile::export_directory_begin() const {
808 return export_directory_iterator(
809 ExportDirectoryEntryRef(ExportDirectory, 0, this));
810}
811
812export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000813 if (!ExportDirectory)
814 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000815 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000816 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000817 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000818}
819
Rafael Espindolab5155a52014-02-10 20:24:04 +0000820section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000821 DataRefImpl Ret;
822 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
823 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000824}
825
Rafael Espindolab5155a52014-02-10 20:24:04 +0000826section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000827 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000828 int NumSections =
829 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000830 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
831 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000832}
833
Rui Ueyama74e85132014-11-19 00:18:07 +0000834base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
835 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
836}
837
838base_reloc_iterator COFFObjectFile::base_reloc_end() const {
839 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
840}
841
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000842uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000843 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000844}
845
846StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000847 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000848 case COFF::IMAGE_FILE_MACHINE_I386:
849 return "COFF-i386";
850 case COFF::IMAGE_FILE_MACHINE_AMD64:
851 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000852 case COFF::IMAGE_FILE_MACHINE_ARMNT:
853 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000854 case COFF::IMAGE_FILE_MACHINE_ARM64:
855 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000856 default:
857 return "COFF-<unknown arch>";
858 }
859}
860
861unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000862 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000863 case COFF::IMAGE_FILE_MACHINE_I386:
864 return Triple::x86;
865 case COFF::IMAGE_FILE_MACHINE_AMD64:
866 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000867 case COFF::IMAGE_FILE_MACHINE_ARMNT:
868 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000869 case COFF::IMAGE_FILE_MACHINE_ARM64:
870 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000871 default:
872 return Triple::UnknownArch;
873 }
874}
875
Rui Ueyama979fb402014-10-09 02:16:38 +0000876iterator_range<import_directory_iterator>
877COFFObjectFile::import_directories() const {
878 return make_range(import_directory_begin(), import_directory_end());
879}
880
881iterator_range<delay_import_directory_iterator>
882COFFObjectFile::delay_import_directories() const {
883 return make_range(delay_import_directory_begin(),
884 delay_import_directory_end());
885}
886
887iterator_range<export_directory_iterator>
888COFFObjectFile::export_directories() const {
889 return make_range(export_directory_begin(), export_directory_end());
890}
891
Rui Ueyama74e85132014-11-19 00:18:07 +0000892iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
893 return make_range(base_reloc_begin(), base_reloc_end());
894}
895
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000896std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000897 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000898 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000899}
900
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000901std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000902COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
903 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000904 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000905}
906
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000907std::error_code
908COFFObjectFile::getDataDirectory(uint32_t Index,
909 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000910 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000911 if (!DataDirectory) {
912 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000913 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000914 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000915 assert(PE32Header || PE32PlusHeader);
916 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
917 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000918 if (Index >= NumEnt) {
919 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000920 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000921 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000922 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000923 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000924}
925
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000926std::error_code COFFObjectFile::getSection(int32_t Index,
927 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000928 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000929 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000930 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000931 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000932 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000933 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000934 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000935 }
936 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000937}
938
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000939std::error_code COFFObjectFile::getString(uint32_t Offset,
940 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000941 if (StringTableSize <= 4)
942 // Tried to get a string from an empty string table.
943 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000944 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000945 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000946 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000947 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000948}
949
David Majnemer44f51e52014-09-10 12:51:52 +0000950std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000951 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000952 return getSymbolName(Symbol.getGeneric(), Res);
953}
954
955std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
956 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000957 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000958 if (Symbol->Name.Offset.Zeroes == 0) {
959 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000960 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000961 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000962 }
963
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000964 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000965 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000966 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000967 else
968 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000969 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000970 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000971}
972
David Majnemer44f51e52014-09-10 12:51:52 +0000973ArrayRef<uint8_t>
974COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000975 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000976
David Majnemer44f51e52014-09-10 12:51:52 +0000977 size_t SymbolSize = getSymbolTableEntrySize();
978 if (Symbol.getNumberOfAuxSymbols() > 0) {
979 // AUX data comes immediately after the symbol in COFF
980 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000981# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000982 // Verify that the Aux symbol points to a valid entry in the symbol table.
983 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000984 if (Offset < getPointerToSymbolTable() ||
985 Offset >=
986 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000987 report_fatal_error("Aux Symbol data was outside of symbol table.");
988
David Majnemer44f51e52014-09-10 12:51:52 +0000989 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
990 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000991# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000992 }
David Majnemer44f51e52014-09-10 12:51:52 +0000993 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000994}
995
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000996std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
997 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000998 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000999 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001000 // Null terminated, let ::strlen figure out the length.
1001 Name = Sec->Name;
1002 else
1003 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +00001004 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001005
1006 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +00001007 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001008 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +00001009 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +00001010 if (decodeBase64StringEntry(Name.substr(2), Offset))
1011 return object_error::parse_failed;
1012 } else {
1013 if (Name.substr(1).getAsInteger(10, Offset))
1014 return object_error::parse_failed;
1015 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001016 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001017 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001018 }
1019
1020 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +00001021 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001022}
1023
David Majnemera9ee5c02014-10-09 08:42:31 +00001024uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1025 // SizeOfRawData and VirtualSize change what they represent depending on
1026 // whether or not we have an executable image.
1027 //
1028 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001029 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +00001030 //
1031 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1032 // actual section size is in VirtualSize. It is possible for VirtualSize to
1033 // be greater than SizeOfRawData; the contents past that point should be
1034 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001035 if (getDOSHeader())
1036 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1037 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001038}
1039
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001040std::error_code
1041COFFObjectFile::getSectionContents(const coff_section *Sec,
1042 ArrayRef<uint8_t> &Res) const {
David Majnemere2129662016-05-28 19:45:51 +00001043 // In COFF, a virtual section won't have any in-file
1044 // content, so the file pointer to the content will be zero.
1045 if (Sec->PointerToRawData == 0)
1046 return object_error::parse_failed;
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001047 // The only thing that we need to verify is that the contents is contained
1048 // within the file bounds. We don't need to make sure it doesn't cover other
1049 // data, as there's nothing that says that is not allowed.
1050 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001051 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001052 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001053 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001054 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001055 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001056}
1057
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001058const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001059 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001060}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001061
Rafael Espindola5e812af2014-01-30 02:49:50 +00001062void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001063 Rel.p = reinterpret_cast<uintptr_t>(
1064 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001065}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001066
Rafael Espindola96d071c2015-06-29 23:29:12 +00001067uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +00001068 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001069 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001070}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001071
Rafael Espindola806f0062013-06-05 01:33:53 +00001072symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001073 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001074 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001075 if (R->SymbolTableIndex >= getNumberOfSymbols())
1076 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001077 if (SymbolTable16)
1078 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1079 else if (SymbolTable32)
1080 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1081 else
David Majnemerc7353b52014-11-25 07:43:14 +00001082 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001083 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001084}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001085
Rafael Espindola99c041b2015-06-30 01:53:01 +00001086uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001087 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001088 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001089}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001090
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001091const coff_section *
1092COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1093 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001094}
1095
David Majnemer44f51e52014-09-10 12:51:52 +00001096COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1097 if (SymbolTable16)
1098 return toSymb<coff_symbol16>(Ref);
1099 if (SymbolTable32)
1100 return toSymb<coff_symbol32>(Ref);
1101 llvm_unreachable("no symbol table pointer!");
1102}
1103
1104COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1105 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001106}
1107
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001108const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001109COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1110 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001111}
1112
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001113iterator_range<const coff_relocation *>
1114COFFObjectFile::getRelocations(const coff_section *Sec) const {
1115 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1116 const coff_relocation *E = I;
1117 if (I)
1118 E += getNumberOfRelocations(Sec, Data, base());
1119 return make_range(I, E);
1120}
1121
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001122#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1123 case COFF::reloc_type: \
1124 Res = #reloc_type; \
1125 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001126
Rafael Espindola41bb4322015-06-30 04:08:37 +00001127void COFFObjectFile::getRelocationTypeName(
1128 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001129 const coff_relocation *Reloc = toRel(Rel);
1130 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001131 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001132 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001133 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001134 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1135 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1136 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1137 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1138 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1139 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1140 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1141 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1142 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1143 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1144 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1145 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1146 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1147 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1148 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1149 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1150 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1151 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001152 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001153 }
1154 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001155 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1156 switch (Reloc->Type) {
1157 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1158 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1159 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1160 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1161 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1162 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1163 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1164 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1165 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1166 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1167 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1168 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1169 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1170 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1171 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1172 default:
1173 Res = "Unknown";
1174 }
1175 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001176 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001177 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001178 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1179 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1180 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1181 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1182 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1183 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1184 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1185 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1186 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1187 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1188 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1189 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001190 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001191 }
1192 break;
1193 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001194 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001195 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001196 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001197}
1198
1199#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1200
Rafael Espindolac66d7612014-08-17 19:09:37 +00001201bool COFFObjectFile::isRelocatableObject() const {
1202 return !DataDirectory;
1203}
1204
Rui Ueyamac2bed422013-09-27 21:04:00 +00001205bool ImportDirectoryEntryRef::
1206operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001207 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001208}
1209
Rafael Espindola5e812af2014-01-30 02:49:50 +00001210void ImportDirectoryEntryRef::moveNext() {
1211 ++Index;
David Majnemer1c0aa042016-07-31 19:25:21 +00001212 if (ImportTable[Index].isNull()) {
David Majnemerad7b7e72016-06-26 04:36:32 +00001213 Index = -1;
1214 ImportTable = nullptr;
1215 }
Rui Ueyamac2bed422013-09-27 21:04:00 +00001216}
1217
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001218std::error_code ImportDirectoryEntryRef::getImportTableEntry(
David Majnemer1c0aa042016-07-31 19:25:21 +00001219 const coff_import_directory_table_entry *&Result) const {
David Majnemerad7b7e72016-06-26 04:36:32 +00001220 return getObject(Result, OwningObject->Data, ImportTable + Index);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001221}
1222
Rui Ueyama861021f2014-10-02 22:05:29 +00001223static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001224makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001225 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001226 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001227 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001228 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001229 }
1230 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001231 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001232}
1233
Rui Ueyama15d99352014-10-03 00:41:58 +00001234static imported_symbol_iterator
1235importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001236 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001237 Object->getRvaPtr(RVA, IntPtr);
1238 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001239}
1240
Rui Ueyama15d99352014-10-03 00:41:58 +00001241static imported_symbol_iterator
1242importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001243 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001244 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001245 // Forward the pointer to the last entry which is null.
1246 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001247 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001248 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1249 while (*Entry++)
1250 ++Index;
1251 } else {
1252 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1253 while (*Entry++)
1254 ++Index;
1255 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001256 return makeImportedSymbolIterator(Object, IntPtr, Index);
1257}
1258
1259imported_symbol_iterator
1260ImportDirectoryEntryRef::imported_symbol_begin() const {
David Majnemer60049522016-07-31 19:40:02 +00001261 return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001262 OwningObject);
1263}
1264
1265imported_symbol_iterator
1266ImportDirectoryEntryRef::imported_symbol_end() const {
David Majnemer60049522016-07-31 19:40:02 +00001267 return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001268 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001269}
1270
Rui Ueyama979fb402014-10-09 02:16:38 +00001271iterator_range<imported_symbol_iterator>
1272ImportDirectoryEntryRef::imported_symbols() const {
1273 return make_range(imported_symbol_begin(), imported_symbol_end());
1274}
1275
David Majnemer60049522016-07-31 19:40:02 +00001276imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const {
1277 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1278 OwningObject);
1279}
1280
1281imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const {
1282 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1283 OwningObject);
1284}
1285
1286iterator_range<imported_symbol_iterator>
1287ImportDirectoryEntryRef::lookup_table_symbols() const {
1288 return make_range(lookup_table_begin(), lookup_table_end());
1289}
1290
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001291std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001292 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001293 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001294 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001295 return EC;
1296 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001297 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001298}
1299
Rui Ueyama1e152d52014-10-02 17:02:18 +00001300std::error_code
1301ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1302 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001303 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001304}
1305
1306std::error_code
1307ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1308 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001309 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001310}
1311
Rui Ueyama15d99352014-10-03 00:41:58 +00001312bool DelayImportDirectoryEntryRef::
1313operator==(const DelayImportDirectoryEntryRef &Other) const {
1314 return Table == Other.Table && Index == Other.Index;
1315}
1316
1317void DelayImportDirectoryEntryRef::moveNext() {
1318 ++Index;
1319}
1320
1321imported_symbol_iterator
1322DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1323 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1324 OwningObject);
1325}
1326
1327imported_symbol_iterator
1328DelayImportDirectoryEntryRef::imported_symbol_end() const {
1329 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1330 OwningObject);
1331}
1332
Rui Ueyama979fb402014-10-09 02:16:38 +00001333iterator_range<imported_symbol_iterator>
1334DelayImportDirectoryEntryRef::imported_symbols() const {
1335 return make_range(imported_symbol_begin(), imported_symbol_end());
1336}
1337
Rui Ueyama15d99352014-10-03 00:41:58 +00001338std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1339 uintptr_t IntPtr = 0;
1340 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1341 return EC;
1342 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001343 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001344}
1345
Rui Ueyama1af08652014-10-03 18:07:18 +00001346std::error_code DelayImportDirectoryEntryRef::
1347getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1348 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001349 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001350}
1351
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001352std::error_code DelayImportDirectoryEntryRef::
1353getImportAddress(int AddrIndex, uint64_t &Result) const {
1354 uint32_t RVA = Table[Index].DelayImportAddressTable +
1355 AddrIndex * (OwningObject->is64() ? 8 : 4);
1356 uintptr_t IntPtr = 0;
1357 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1358 return EC;
1359 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001360 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001361 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001362 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001363 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001364}
1365
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001366bool ExportDirectoryEntryRef::
1367operator==(const ExportDirectoryEntryRef &Other) const {
1368 return ExportTable == Other.ExportTable && Index == Other.Index;
1369}
1370
Rafael Espindola5e812af2014-01-30 02:49:50 +00001371void ExportDirectoryEntryRef::moveNext() {
1372 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001373}
1374
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001375// Returns the name of the current export symbol. If the symbol is exported only
1376// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001377std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001378 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001379 if (std::error_code EC =
1380 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001381 return EC;
1382 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001383 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001384}
1385
Rui Ueyamae5df6092014-01-17 22:02:24 +00001386// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001387std::error_code
1388ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001389 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001390 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001391}
1392
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001393// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001394std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001395 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001396 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001397}
1398
1399// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001400std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001401 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001402 if (std::error_code EC =
1403 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001404 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001405 const export_address_table_entry *entry =
1406 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001407 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001408 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001409}
1410
1411// Returns the name of the current export symbol. If the symbol is exported only
1412// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001413std::error_code
1414ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001415 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001416 if (std::error_code EC =
1417 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001418 return EC;
1419 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1420
1421 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1422 int Offset = 0;
1423 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1424 I < E; ++I, ++Offset) {
1425 if (*I != Index)
1426 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001427 if (std::error_code EC =
1428 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001429 return EC;
1430 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001431 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001432 return EC;
1433 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001434 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001435 }
1436 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001437 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001438}
1439
Rui Ueyama6161b382016-01-12 23:28:42 +00001440std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1441 const data_directory *DataEntry;
1442 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1443 return EC;
1444 uint32_t RVA;
1445 if (auto EC = getExportRVA(RVA))
1446 return EC;
1447 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1448 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1449 Result = (Begin <= RVA && RVA < End);
1450 return std::error_code();
1451}
1452
1453std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1454 uint32_t RVA;
1455 if (auto EC = getExportRVA(RVA))
1456 return EC;
1457 uintptr_t IntPtr = 0;
1458 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1459 return EC;
1460 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1461 return std::error_code();
1462}
1463
Rui Ueyama861021f2014-10-02 22:05:29 +00001464bool ImportedSymbolRef::
1465operator==(const ImportedSymbolRef &Other) const {
1466 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1467 && Index == Other.Index;
1468}
1469
1470void ImportedSymbolRef::moveNext() {
1471 ++Index;
1472}
1473
1474std::error_code
1475ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1476 uint32_t RVA;
1477 if (Entry32) {
1478 // If a symbol is imported only by ordinal, it has no name.
1479 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001480 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001481 RVA = Entry32[Index].getHintNameRVA();
1482 } else {
1483 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001484 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001485 RVA = Entry64[Index].getHintNameRVA();
1486 }
1487 uintptr_t IntPtr = 0;
1488 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1489 return EC;
1490 // +2 because the first two bytes is hint.
1491 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001492 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001493}
1494
David Majnemerad7b7e72016-06-26 04:36:32 +00001495std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1496 if (Entry32)
1497 Result = Entry32[Index].isOrdinal();
1498 else
1499 Result = Entry64[Index].isOrdinal();
1500 return std::error_code();
1501}
1502
1503std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1504 if (Entry32)
1505 Result = Entry32[Index].getHintNameRVA();
1506 else
1507 Result = Entry64[Index].getHintNameRVA();
1508 return std::error_code();
1509}
1510
Rui Ueyama861021f2014-10-02 22:05:29 +00001511std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1512 uint32_t RVA;
1513 if (Entry32) {
1514 if (Entry32[Index].isOrdinal()) {
1515 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001516 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001517 }
1518 RVA = Entry32[Index].getHintNameRVA();
1519 } else {
1520 if (Entry64[Index].isOrdinal()) {
1521 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001522 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001523 }
1524 RVA = Entry64[Index].getHintNameRVA();
1525 }
1526 uintptr_t IntPtr = 0;
1527 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1528 return EC;
1529 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001530 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001531}
1532
Rafael Espindola437b0d52014-07-31 03:12:45 +00001533ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001534ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001535 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001536 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001537 if (EC)
1538 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001539 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001540}
Rui Ueyama74e85132014-11-19 00:18:07 +00001541
1542bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1543 return Header == Other.Header && Index == Other.Index;
1544}
1545
1546void BaseRelocRef::moveNext() {
1547 // Header->BlockSize is the size of the current block, including the
1548 // size of the header itself.
1549 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001550 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001551 if (Size == Header->BlockSize) {
1552 // .reloc contains a list of base relocation blocks. Each block
1553 // consists of the header followed by entries. The header contains
1554 // how many entories will follow. When we reach the end of the
1555 // current block, proceed to the next block.
1556 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1557 reinterpret_cast<const uint8_t *>(Header) + Size);
1558 Index = 0;
1559 } else {
1560 ++Index;
1561 }
1562}
1563
1564std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1565 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1566 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001567 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001568}
1569
1570std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1571 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1572 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001573 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001574}