blob: b544fa5c14702dc1efb58d9f33b5d34edb2c503e [file] [log] [blame]
Eugene Zelenkod341c932017-04-19 23:02:10 +00001//===- COFFObjectFile.cpp - COFF object file implementation ---------------===//
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer9da9e692012-03-19 20:27:37 +000014#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000015#include "llvm/ADT/StringRef.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000016#include "llvm/ADT/Triple.h"
Rui Ueyama6a75acb2015-06-25 00:07:39 +000017#include "llvm/ADT/iterator_range.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000018#include "llvm/BinaryFormat/COFF.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000019#include "llvm/Object/Binary.h"
20#include "llvm/Object/COFF.h"
21#include "llvm/Object/Error.h"
22#include "llvm/Object/ObjectFile.h"
Eric Beckmannefef15a2017-05-08 02:47:07 +000023#include "llvm/Support/BinaryStreamReader.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000024#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include <algorithm>
30#include <cassert>
31#include <cstddef>
32#include <cstdint>
33#include <cstring>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000034#include <limits>
Eugene Zelenkod341c932017-04-19 23:02:10 +000035#include <memory>
36#include <system_error>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000037
38using namespace llvm;
39using namespace object;
40
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000041using support::ulittle16_t;
42using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000043using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000044using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000045
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000046// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000047static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000048 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000049 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000050 return false;
51 }
52 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000053}
54
Rui Ueyamaed64342b2013-07-19 23:23:29 +000055// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
56// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000057template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000058static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000059 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000060 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000061 uintptr_t Addr = uintptr_t(Ptr);
Benjamin Kramercbc7ee42017-08-31 12:27:10 +000062 if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
David Majnemere830c602014-11-13 08:46:37 +000063 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000064 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyama7d099192015-06-09 15:20:42 +000065 return std::error_code();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000066}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000067
Nico Rieck9d2c15e2014-02-22 16:12:20 +000068// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
69// prefixed slashes.
70static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
71 assert(Str.size() <= 6 && "String too long, possible overflow.");
72 if (Str.size() > 6)
73 return true;
74
75 uint64_t Value = 0;
76 while (!Str.empty()) {
77 unsigned CharVal;
78 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
79 CharVal = Str[0] - 'A';
80 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
81 CharVal = Str[0] - 'a' + 26;
82 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
83 CharVal = Str[0] - '0' + 52;
84 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000085 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000086 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000087 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000088 else
89 return true;
90
91 Value = (Value * 64) + CharVal;
92 Str = Str.substr(1);
93 }
94
95 if (Value > std::numeric_limits<uint32_t>::max())
96 return true;
97
98 Result = static_cast<uint32_t>(Value);
99 return false;
100}
101
David Majnemer44f51e52014-09-10 12:51:52 +0000102template <typename coff_symbol_type>
103const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
104 const coff_symbol_type *Addr =
105 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000106
David Majnemer236b0ca2014-11-17 11:17:17 +0000107 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000108#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000109 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000110 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000111
David Majnemer44f51e52014-09-10 12:51:52 +0000112 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
113 "Symbol did not point to the beginning of a symbol");
114#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000115
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000116 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000117}
118
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000119const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
120 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000121
Eugene Zelenkod341c932017-04-19 23:02:10 +0000122#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000123 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000124 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000125 report_fatal_error("Section was outside of section table.");
126
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000127 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
128 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000129 "Section did not point to the beginning of a section");
Eugene Zelenkod341c932017-04-19 23:02:10 +0000130#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000131
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000132 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000133}
134
Rafael Espindola5e812af2014-01-30 02:49:50 +0000135void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000136 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000137 if (SymbolTable16) {
138 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
139 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000140 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000141 } else if (SymbolTable32) {
142 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
143 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000144 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000145 } else {
146 llvm_unreachable("no symbol table pointer!");
147 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000148}
149
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000150Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000151 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000152 StringRef Result;
Eric Beckmannefef15a2017-05-08 02:47:07 +0000153 if (std::error_code EC = getSymbolName(Symb, Result))
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000154 return errorCodeToError(EC);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000155 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000156}
157
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000158uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
159 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000160}
161
Davide Italiano6b2bba12016-11-02 17:32:19 +0000162uint32_t COFFObjectFile::getSymbolAlignment(DataRefImpl Ref) const {
163 // MSVC/link.exe seems to align symbols to the next-power-of-2
164 // up to 32 bytes.
165 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Davide Italiano03a85682016-11-11 03:07:45 +0000166 return std::min(uint64_t(32), PowerOf2Ceil(Symb.getValue()));
Davide Italiano6b2bba12016-11-02 17:32:19 +0000167}
168
Kevin Enderby931cb652016-06-24 18:24:42 +0000169Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
Rafael Espindolaed067c42015-07-03 18:19:00 +0000170 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000171 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000172 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000173
174 if (Symb.isAnyUndefined() || Symb.isCommon() ||
175 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000176 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000177
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000178 const coff_section *Section = nullptr;
179 if (std::error_code EC = getSection(SectionNumber, Section))
Kevin Enderby931cb652016-06-24 18:24:42 +0000180 return errorCodeToError(EC);
Rafael Espindola991af662015-06-24 19:11:10 +0000181 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000182
183 // The section VirtualAddress does not include ImageBase, and we want to
184 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000185 Result += getImageBase();
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000186
Rafael Espindolaed067c42015-07-03 18:19:00 +0000187 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000188}
189
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000190Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000191 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000192 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000193
Peter Collingbournee834f422015-08-06 05:26:35 +0000194 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
195 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000196 if (Symb.isAnyUndefined())
197 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000198 if (Symb.isCommon())
199 return SymbolRef::ST_Data;
200 if (Symb.isFileRecord())
201 return SymbolRef::ST_File;
202
203 // TODO: perhaps we need a new symbol type ST_Section.
204 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
205 return SymbolRef::ST_Debug;
206
207 if (!COFF::isReservedSectionNumber(SectionNumber))
208 return SymbolRef::ST_Data;
209
210 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000211}
212
Rafael Espindola20122a42014-01-31 20:57:12 +0000213uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000214 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000215 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000216
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000217 if (Symb.isExternal() || Symb.isWeakExternal())
Lang Hames9dc0eb42016-01-25 01:21:45 +0000218 Result |= SymbolRef::SF_Global;
David Meyer1df4b842012-02-28 23:47:53 +0000219
Martell Malone1079ef82017-07-18 21:26:38 +0000220 if (Symb.isWeakExternal()) {
David Meyer1df4b842012-02-28 23:47:53 +0000221 Result |= SymbolRef::SF_Weak;
Martell Malone1079ef82017-07-18 21:26:38 +0000222 // We use indirect to allow the archiver to write weak externs
223 Result |= SymbolRef::SF_Indirect;
224 }
David Meyer1df4b842012-02-28 23:47:53 +0000225
David Majnemer44f51e52014-09-10 12:51:52 +0000226 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000227 Result |= SymbolRef::SF_Absolute;
228
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000229 if (Symb.isFileRecord())
230 Result |= SymbolRef::SF_FormatSpecific;
231
232 if (Symb.isSectionDefinition())
233 Result |= SymbolRef::SF_FormatSpecific;
234
235 if (Symb.isCommon())
236 Result |= SymbolRef::SF_Common;
237
238 if (Symb.isAnyUndefined())
239 Result |= SymbolRef::SF_Undefined;
240
Rafael Espindola20122a42014-01-31 20:57:12 +0000241 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000242}
243
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000244uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000245 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000246 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000247}
248
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000249Expected<section_iterator>
Rafael Espindola8bab8892015-08-07 23:27:14 +0000250COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000251 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000252 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
253 return section_end();
254 const coff_section *Sec = nullptr;
255 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000256 return errorCodeToError(EC);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000257 DataRefImpl Ret;
258 Ret.p = reinterpret_cast<uintptr_t>(Sec);
259 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000260}
261
Rafael Espindola6bf32212015-06-24 19:57:32 +0000262unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
263 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
264 return Symb.getSectionNumber();
265}
266
Rafael Espindola5e812af2014-01-30 02:49:50 +0000267void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000268 const coff_section *Sec = toSec(Ref);
269 Sec += 1;
270 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000271}
272
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000273std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
274 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000275 const coff_section *Sec = toSec(Ref);
276 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000277}
278
Rafael Espindola80291272014-10-08 15:28:58 +0000279uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000280 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000281 uint64_t Result = Sec->VirtualAddress;
282
283 // The section VirtualAddress does not include ImageBase, and we want to
284 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000285 Result += getImageBase();
David Majnemer7c6a0712015-07-31 17:40:24 +0000286 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000287}
288
George Rimara25d3292017-05-27 18:10:23 +0000289uint64_t COFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
290 return toSec(Sec) - SectionTable;
291}
292
Rafael Espindola80291272014-10-08 15:28:58 +0000293uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000294 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000295}
296
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000297std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
298 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000299 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000300 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000301 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000302 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
303 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000304}
305
Rafael Espindola80291272014-10-08 15:28:58 +0000306uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000307 const coff_section *Sec = toSec(Ref);
David Majnemer511391f2016-03-17 16:55:18 +0000308 return Sec->getAlignment();
Michael J. Spencer79894602011-10-10 21:55:43 +0000309}
310
George Rimar401e4e52016-05-24 12:48:46 +0000311bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
312 return false;
313}
314
Rafael Espindola80291272014-10-08 15:28:58 +0000315bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000316 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000317 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000318}
319
Rafael Espindola80291272014-10-08 15:28:58 +0000320bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000321 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000322 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000323}
324
Rafael Espindola80291272014-10-08 15:28:58 +0000325bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000326 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000327 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
328 COFF::IMAGE_SCN_MEM_READ |
329 COFF::IMAGE_SCN_MEM_WRITE;
330 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000331}
332
Rafael Espindola6bf32212015-06-24 19:57:32 +0000333unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
334 uintptr_t Offset =
335 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
336 assert((Offset % sizeof(coff_section)) == 0);
337 return (Offset / sizeof(coff_section)) + 1;
338}
339
Rafael Espindola80291272014-10-08 15:28:58 +0000340bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000341 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000342 // In COFF, a virtual section won't have any in-file
343 // content, so the file pointer to the content will be zero.
344 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000345}
346
David Majnemere830c602014-11-13 08:46:37 +0000347static uint32_t getNumberOfRelocations(const coff_section *Sec,
348 MemoryBufferRef M, const uint8_t *base) {
349 // The field for the number of relocations in COFF section table is only
350 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
351 // NumberOfRelocations field, and the actual relocation count is stored in the
352 // VirtualAddress field in the first relocation entry.
353 if (Sec->hasExtendedRelocations()) {
354 const coff_relocation *FirstReloc;
355 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
356 base + Sec->PointerToRelocations)))
357 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000358 // -1 to exclude this first relocation entry.
359 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000360 }
361 return Sec->NumberOfRelocations;
362}
363
David Majnemer94751be2014-11-13 09:50:18 +0000364static const coff_relocation *
365getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
366 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
367 if (!NumRelocs)
368 return nullptr;
369 auto begin = reinterpret_cast<const coff_relocation *>(
370 Base + Sec->PointerToRelocations);
371 if (Sec->hasExtendedRelocations()) {
372 // Skip the first relocation entry repurposed to store the number of
373 // relocations.
374 begin++;
375 }
Benjamin Kramercbc7ee42017-08-31 12:27:10 +0000376 if (Binary::checkOffset(M, uintptr_t(begin),
377 sizeof(coff_relocation) * NumRelocs))
David Majnemer94751be2014-11-13 09:50:18 +0000378 return nullptr;
379 return begin;
380}
381
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000382relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
383 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000384 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000385 if (begin && Sec->VirtualAddress != 0)
386 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000387 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000388 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000390}
391
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000392relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
393 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000394 const coff_relocation *I = getFirstReloc(Sec, Data, base());
395 if (I)
396 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000397 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000398 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000399 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000400}
401
Rui Ueyamac2bed422013-09-27 21:04:00 +0000402// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000403std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000404 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000405 if (std::error_code EC = getObject(
406 SymbolTable16, Data, base() + getPointerToSymbolTable(),
407 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000408 return EC;
409
410 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000411 if (std::error_code EC = getObject(
412 SymbolTable32, Data, base() + getPointerToSymbolTable(),
413 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000414 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000415
416 // Find string table. The first four byte of the string table contains the
417 // total size of the string table, including the size field itself. If the
418 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000419 uint32_t StringTableOffset = getPointerToSymbolTable() +
420 getNumberOfSymbols() * getSymbolTableEntrySize();
421 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000422 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000423 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000424 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000425 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000426 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000427 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000428 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429
Nico Rieck773a5792014-02-26 19:51:44 +0000430 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
431 // tools like cvtres write a size of 0 for an empty table instead of 4.
432 if (StringTableSize < 4)
433 StringTableSize = 4;
434
Rui Ueyamac2bed422013-09-27 21:04:00 +0000435 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000436 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000438 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000439}
440
Reid Kleckner21427ad2015-10-09 00:15:08 +0000441uint64_t COFFObjectFile::getImageBase() const {
Reid Klecknere94fef72015-10-09 00:15:01 +0000442 if (PE32Header)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000443 return PE32Header->ImageBase;
Reid Klecknere94fef72015-10-09 00:15:01 +0000444 else if (PE32PlusHeader)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000445 return PE32PlusHeader->ImageBase;
446 // This actually comes up in practice.
447 return 0;
Reid Klecknere94fef72015-10-09 00:15:01 +0000448}
449
Rui Ueyama215a5862014-02-20 06:51:07 +0000450// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000451std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner21427ad2015-10-09 00:15:08 +0000452 uint64_t ImageBase = getImageBase();
Rui Ueyamab7a40082014-02-20 19:14:56 +0000453 uint64_t Rva = Addr - ImageBase;
454 assert(Rva <= UINT32_MAX);
455 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000456}
457
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000459std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000460 for (const SectionRef &S : sections()) {
461 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462 uint32_t SectionStart = Section->VirtualAddress;
463 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000464 if (SectionStart <= Addr && Addr < SectionEnd) {
465 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000467 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468 }
469 }
470 return object_error::parse_failed;
471}
472
Reid Kleckner2da433e2016-06-02 17:10:43 +0000473std::error_code
474COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size,
475 ArrayRef<uint8_t> &Contents) const {
476 for (const SectionRef &S : sections()) {
477 const coff_section *Section = getCOFFSection(S);
478 uint32_t SectionStart = Section->VirtualAddress;
479 // Check if this RVA is within the section bounds. Be careful about integer
480 // overflow.
481 uint32_t OffsetIntoSection = RVA - SectionStart;
482 if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
483 Size <= Section->VirtualSize - OffsetIntoSection) {
484 uintptr_t Begin =
485 uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
486 Contents =
487 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
488 return std::error_code();
489 }
490 }
491 return object_error::parse_failed;
492}
493
Rui Ueyamac2bed422013-09-27 21:04:00 +0000494// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
495// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000496std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
497 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000498 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000499 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000500 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000501 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
502 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
503 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000504 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000505}
506
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000507std::error_code
508COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir,
509 const codeview::DebugInfo *&PDBInfo,
510 StringRef &PDBFileName) const {
Reid Kleckner2da433e2016-06-02 17:10:43 +0000511 ArrayRef<uint8_t> InfoBytes;
512 if (std::error_code EC = getRvaAndSizeAsBytes(
513 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
514 return EC;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000515 if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
Reid Kleckner2da433e2016-06-02 17:10:43 +0000516 return object_error::parse_failed;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000517 PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
518 InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
Reid Kleckner2da433e2016-06-02 17:10:43 +0000519 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
520 InfoBytes.size());
521 // Truncate the name at the first null byte. Ignore any padding.
522 PDBFileName = PDBFileName.split('\0').first;
523 return std::error_code();
524}
525
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000526std::error_code
527COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo,
528 StringRef &PDBFileName) const {
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000529 for (const debug_directory &D : debug_directories())
530 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW)
531 return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
532 // If we get here, there is no PDB info to return.
533 PDBInfo = nullptr;
534 PDBFileName = StringRef();
535 return std::error_code();
536}
537
Rui Ueyamac2bed422013-09-27 21:04:00 +0000538// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000539std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000540 // First, we get the RVA of the import table. If the file lacks a pointer to
541 // the import table, do nothing.
542 const data_directory *DataEntry;
543 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000544 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000545
546 // Do nothing if the pointer to import table is NULL.
547 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000548 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000549
550 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000551
552 // Find the section that contains the RVA. This is needed because the RVA is
553 // the import table's memory address which is different from its file offset.
554 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000555 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000556 return EC;
David Majnemerad7b7e72016-06-26 04:36:32 +0000557 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
558 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000559 ImportDirectory = reinterpret_cast<
David Majnemer1c0aa042016-07-31 19:25:21 +0000560 const coff_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000561 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000562}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000563
Rui Ueyama15d99352014-10-03 00:41:58 +0000564// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
565std::error_code COFFObjectFile::initDelayImportTablePtr() {
566 const data_directory *DataEntry;
567 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000568 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000569 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000570 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000571
572 uint32_t RVA = DataEntry->RelativeVirtualAddress;
573 NumberOfDelayImportDirectory = DataEntry->Size /
574 sizeof(delay_import_directory_table_entry) - 1;
575
576 uintptr_t IntPtr = 0;
577 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
578 return EC;
579 DelayImportDirectory = reinterpret_cast<
580 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000581 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000582}
583
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000584// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000585std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000586 // First, we get the RVA of the export table. If the file lacks a pointer to
587 // the export table, do nothing.
588 const data_directory *DataEntry;
589 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000590 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000591
592 // Do nothing if the pointer to export table is NULL.
593 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000594 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000595
596 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
597 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000598 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000599 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000600 ExportDirectory =
601 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000602 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000603}
604
Rui Ueyama74e85132014-11-19 00:18:07 +0000605std::error_code COFFObjectFile::initBaseRelocPtr() {
606 const data_directory *DataEntry;
607 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000608 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000609 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000610 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000611
612 uintptr_t IntPtr = 0;
613 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
614 return EC;
615 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
616 IntPtr);
617 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
618 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000619 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000620}
621
Reid Kleckner2da433e2016-06-02 17:10:43 +0000622std::error_code COFFObjectFile::initDebugDirectoryPtr() {
623 // Get the RVA of the debug directory. Do nothing if it does not exist.
624 const data_directory *DataEntry;
625 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
626 return std::error_code();
627
628 // Do nothing if the RVA is NULL.
629 if (DataEntry->RelativeVirtualAddress == 0)
630 return std::error_code();
631
632 // Check that the size is a multiple of the entry size.
633 if (DataEntry->Size % sizeof(debug_directory) != 0)
634 return object_error::parse_failed;
635
636 uintptr_t IntPtr = 0;
637 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
638 return EC;
639 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
640 if (std::error_code EC = getRvaPtr(
641 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
642 return EC;
643 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
644 return std::error_code();
645}
646
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000647std::error_code COFFObjectFile::initLoadConfigPtr() {
648 // Get the RVA of the debug directory. Do nothing if it does not exist.
649 const data_directory *DataEntry;
650 if (getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataEntry))
651 return std::error_code();
652
653 // Do nothing if the RVA is NULL.
654 if (DataEntry->RelativeVirtualAddress == 0)
655 return std::error_code();
656 uintptr_t IntPtr = 0;
657 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
658 return EC;
659
660 LoadConfig = (const void *)IntPtr;
661 return std::error_code();
662}
663
Rafael Espindola48af1c22014-08-19 18:44:46 +0000664COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
665 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000666 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
667 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
668 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
David Majnemerad7b7e72016-06-26 04:36:32 +0000669 ImportDirectory(nullptr),
Rui Ueyama15d99352014-10-03 00:41:58 +0000670 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Kleckner2da433e2016-06-02 17:10:43 +0000671 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
672 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000673 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000674 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000675 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000676
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000677 // The current location in the file where we are looking at.
678 uint64_t CurPtr = 0;
679
680 // PE header is optional and is present only in executables. If it exists,
681 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000682 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000683
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000684 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000685 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000686 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
687 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000688 const auto *DH = reinterpret_cast<const dos_header *>(base());
689 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
690 CurPtr = DH->AddressOfNewExeHeader;
691 // Check the PE magic bytes. ("PE\0\0")
692 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
693 EC = object_error::parse_failed;
694 return;
695 }
696 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
697 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000698 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000699 }
700
Rafael Espindola48af1c22014-08-19 18:44:46 +0000701 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000702 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000703
704 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
705 // import libraries share a common prefix but bigobj is more restrictive.
706 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
707 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
708 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
709 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
710 return;
711
712 // Verify that we are dealing with bigobj.
713 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
714 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
715 sizeof(COFF::BigObjMagic)) == 0) {
716 COFFHeader = nullptr;
717 CurPtr += sizeof(coff_bigobj_file_header);
718 } else {
719 // It's not a bigobj.
720 COFFBigObjHeader = nullptr;
721 }
722 }
723 if (COFFHeader) {
724 // The prior checkSize call may have failed. This isn't a hard error
725 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000726 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000727 CurPtr += sizeof(coff_file_header);
728
729 if (COFFHeader->isImportLibrary())
730 return;
731 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000732
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000733 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000734 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000735 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000736 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000737
738 const uint8_t *DataDirAddr;
739 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000740 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000741 PE32Header = Header;
742 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
743 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000744 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000745 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
746 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
747 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
748 } else {
749 // It's neither PE32 nor PE32+.
750 EC = object_error::parse_failed;
751 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000752 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000753 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000754 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000755 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000756
Rui Ueyama8950a532016-08-11 22:02:44 +0000757 if (COFFHeader)
758 CurPtr += COFFHeader->SizeOfOptionalHeader;
759
Rafael Espindola48af1c22014-08-19 18:44:46 +0000760 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000761 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000762 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000763
Rui Ueyamac2bed422013-09-27 21:04:00 +0000764 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000765 if (getPointerToSymbolTable() != 0) {
David Majnemerac8cfab2016-08-30 20:20:24 +0000766 if ((EC = initSymbolTablePtr())) {
767 SymbolTable16 = nullptr;
768 SymbolTable32 = nullptr;
769 StringTable = nullptr;
770 StringTableSize = 0;
771 }
David Majnemer236b0ca2014-11-17 11:17:17 +0000772 } else {
773 // We had better not have any symbols if we don't have a symbol table.
774 if (getNumberOfSymbols() != 0) {
775 EC = object_error::parse_failed;
776 return;
777 }
778 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000779
Rui Ueyamac2bed422013-09-27 21:04:00 +0000780 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000781 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000782 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000783 if ((EC = initDelayImportTablePtr()))
784 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000785
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000786 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000787 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000788 return;
789
Rui Ueyama74e85132014-11-19 00:18:07 +0000790 // Initialize the pointer to the base relocation table.
791 if ((EC = initBaseRelocPtr()))
792 return;
793
Reid Kleckner2da433e2016-06-02 17:10:43 +0000794 // Initialize the pointer to the export table.
795 if ((EC = initDebugDirectoryPtr()))
796 return;
797
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000798 if ((EC = initLoadConfigPtr()))
799 return;
800
Rui Ueyama7d099192015-06-09 15:20:42 +0000801 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000802}
803
Peter Collingbourne435890a2016-11-22 03:38:40 +0000804basic_symbol_iterator COFFObjectFile::symbol_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000805 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000806 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000807 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000808}
809
Peter Collingbourne435890a2016-11-22 03:38:40 +0000810basic_symbol_iterator COFFObjectFile::symbol_end() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000811 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000812 DataRefImpl Ret;
813 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000814 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000815}
816
Rui Ueyamabc654b12013-09-27 21:47:05 +0000817import_directory_iterator COFFObjectFile::import_directory_begin() const {
David Majnemerad7b7e72016-06-26 04:36:32 +0000818 if (!ImportDirectory)
819 return import_directory_end();
David Majnemer1c0aa042016-07-31 19:25:21 +0000820 if (ImportDirectory->isNull())
David Majnemerad7b7e72016-06-26 04:36:32 +0000821 return import_directory_end();
Rui Ueyamaa045b732014-01-16 03:13:19 +0000822 return import_directory_iterator(
823 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000824}
825
Rui Ueyamabc654b12013-09-27 21:47:05 +0000826import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000827 return import_directory_iterator(
David Majnemerad7b7e72016-06-26 04:36:32 +0000828 ImportDirectoryEntryRef(nullptr, -1, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000829}
David Meyerc429b802012-03-01 22:19:54 +0000830
Rui Ueyama15d99352014-10-03 00:41:58 +0000831delay_import_directory_iterator
832COFFObjectFile::delay_import_directory_begin() const {
833 return delay_import_directory_iterator(
834 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
835}
836
837delay_import_directory_iterator
838COFFObjectFile::delay_import_directory_end() const {
839 return delay_import_directory_iterator(
840 DelayImportDirectoryEntryRef(
841 DelayImportDirectory, NumberOfDelayImportDirectory, this));
842}
843
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000844export_directory_iterator COFFObjectFile::export_directory_begin() const {
845 return export_directory_iterator(
846 ExportDirectoryEntryRef(ExportDirectory, 0, this));
847}
848
849export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000850 if (!ExportDirectory)
851 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000852 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000853 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000854 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000855}
856
Rafael Espindolab5155a52014-02-10 20:24:04 +0000857section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000858 DataRefImpl Ret;
859 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
860 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000861}
862
Rafael Espindolab5155a52014-02-10 20:24:04 +0000863section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000864 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000865 int NumSections =
866 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000867 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
868 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000869}
870
Rui Ueyama74e85132014-11-19 00:18:07 +0000871base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
872 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
873}
874
875base_reloc_iterator COFFObjectFile::base_reloc_end() const {
876 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
877}
878
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000879uint8_t COFFObjectFile::getBytesInAddress() const {
Martin Storsjo43c85452017-06-30 07:02:13 +0000880 return getArch() == Triple::x86_64 || getArch() == Triple::aarch64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000881}
882
883StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000884 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000885 case COFF::IMAGE_FILE_MACHINE_I386:
886 return "COFF-i386";
887 case COFF::IMAGE_FILE_MACHINE_AMD64:
888 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000889 case COFF::IMAGE_FILE_MACHINE_ARMNT:
890 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000891 case COFF::IMAGE_FILE_MACHINE_ARM64:
892 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000893 default:
894 return "COFF-<unknown arch>";
895 }
896}
897
Zachary Turner260fe3e2017-12-14 22:07:03 +0000898Triple::ArchType COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000899 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000900 case COFF::IMAGE_FILE_MACHINE_I386:
901 return Triple::x86;
902 case COFF::IMAGE_FILE_MACHINE_AMD64:
903 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000904 case COFF::IMAGE_FILE_MACHINE_ARMNT:
905 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000906 case COFF::IMAGE_FILE_MACHINE_ARM64:
907 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000908 default:
909 return Triple::UnknownArch;
910 }
911}
912
Rui Ueyama979fb402014-10-09 02:16:38 +0000913iterator_range<import_directory_iterator>
914COFFObjectFile::import_directories() const {
915 return make_range(import_directory_begin(), import_directory_end());
916}
917
918iterator_range<delay_import_directory_iterator>
919COFFObjectFile::delay_import_directories() const {
920 return make_range(delay_import_directory_begin(),
921 delay_import_directory_end());
922}
923
924iterator_range<export_directory_iterator>
925COFFObjectFile::export_directories() const {
926 return make_range(export_directory_begin(), export_directory_end());
927}
928
Rui Ueyama74e85132014-11-19 00:18:07 +0000929iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
930 return make_range(base_reloc_begin(), base_reloc_end());
931}
932
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000933std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000934 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000935 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000936}
937
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000938std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000939COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
940 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000941 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000942}
943
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000944std::error_code
945COFFObjectFile::getDataDirectory(uint32_t Index,
946 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000947 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000948 if (!DataDirectory) {
949 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000950 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000951 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000952 assert(PE32Header || PE32PlusHeader);
953 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
954 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000955 if (Index >= NumEnt) {
956 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000957 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000958 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000959 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000960 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000961}
962
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000963std::error_code COFFObjectFile::getSection(int32_t Index,
964 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000965 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000966 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000967 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000968 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000969 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000970 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000971 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000972 }
973 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000974}
975
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000976std::error_code COFFObjectFile::getString(uint32_t Offset,
977 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000978 if (StringTableSize <= 4)
979 // Tried to get a string from an empty string table.
980 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000981 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000982 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000983 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000984 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000985}
986
David Majnemer44f51e52014-09-10 12:51:52 +0000987std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000988 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000989 return getSymbolName(Symbol.getGeneric(), Res);
990}
991
992std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
993 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000994 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000995 if (Symbol->Name.Offset.Zeroes == 0) {
996 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000997 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000998 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000999 }
1000
Rui Ueyamae40d30f2015-06-30 00:03:56 +00001001 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001002 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +00001003 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001004 else
1005 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +00001006 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001007 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001008}
1009
David Majnemer44f51e52014-09-10 12:51:52 +00001010ArrayRef<uint8_t>
1011COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +00001012 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001013
David Majnemer44f51e52014-09-10 12:51:52 +00001014 size_t SymbolSize = getSymbolTableEntrySize();
1015 if (Symbol.getNumberOfAuxSymbols() > 0) {
1016 // AUX data comes immediately after the symbol in COFF
1017 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Eugene Zelenkod341c932017-04-19 23:02:10 +00001018#ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001019 // Verify that the Aux symbol points to a valid entry in the symbol table.
1020 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +00001021 if (Offset < getPointerToSymbolTable() ||
1022 Offset >=
1023 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +00001024 report_fatal_error("Aux Symbol data was outside of symbol table.");
1025
David Majnemer44f51e52014-09-10 12:51:52 +00001026 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
1027 "Aux Symbol data did not point to the beginning of a symbol");
Eugene Zelenkod341c932017-04-19 23:02:10 +00001028#endif
Marshall Clowbfb85e62012-06-15 01:15:47 +00001029 }
David Majnemer44f51e52014-09-10 12:51:52 +00001030 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +00001031}
1032
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001033std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
1034 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001035 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +00001036 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001037 // Null terminated, let ::strlen figure out the length.
1038 Name = Sec->Name;
1039 else
1040 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +00001041 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001042
1043 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +00001044 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001045 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +00001046 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +00001047 if (decodeBase64StringEntry(Name.substr(2), Offset))
1048 return object_error::parse_failed;
1049 } else {
1050 if (Name.substr(1).getAsInteger(10, Offset))
1051 return object_error::parse_failed;
1052 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001053 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001054 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001055 }
1056
1057 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +00001058 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001059}
1060
David Majnemera9ee5c02014-10-09 08:42:31 +00001061uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1062 // SizeOfRawData and VirtualSize change what they represent depending on
1063 // whether or not we have an executable image.
1064 //
1065 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001066 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +00001067 //
1068 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1069 // actual section size is in VirtualSize. It is possible for VirtualSize to
1070 // be greater than SizeOfRawData; the contents past that point should be
1071 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001072 if (getDOSHeader())
1073 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1074 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001075}
1076
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001077std::error_code
1078COFFObjectFile::getSectionContents(const coff_section *Sec,
1079 ArrayRef<uint8_t> &Res) const {
David Majnemere2129662016-05-28 19:45:51 +00001080 // In COFF, a virtual section won't have any in-file
1081 // content, so the file pointer to the content will be zero.
1082 if (Sec->PointerToRawData == 0)
Shoaib Meenaiee97c5f2017-05-14 18:34:56 +00001083 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001084 // The only thing that we need to verify is that the contents is contained
1085 // within the file bounds. We don't need to make sure it doesn't cover other
1086 // data, as there's nothing that says that is not allowed.
1087 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001088 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001089 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001090 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001091 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001092 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001093}
1094
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001095const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001096 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001097}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001098
Rafael Espindola5e812af2014-01-30 02:49:50 +00001099void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001100 Rel.p = reinterpret_cast<uintptr_t>(
1101 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001102}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001103
Rafael Espindola96d071c2015-06-29 23:29:12 +00001104uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +00001105 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001106 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001107}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001108
Rafael Espindola806f0062013-06-05 01:33:53 +00001109symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001110 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001111 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001112 if (R->SymbolTableIndex >= getNumberOfSymbols())
1113 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001114 if (SymbolTable16)
1115 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1116 else if (SymbolTable32)
1117 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1118 else
David Majnemerc7353b52014-11-25 07:43:14 +00001119 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001120 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001121}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001122
Rafael Espindola99c041b2015-06-30 01:53:01 +00001123uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001124 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001125 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001126}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001127
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001128const coff_section *
1129COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1130 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001131}
1132
David Majnemer44f51e52014-09-10 12:51:52 +00001133COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1134 if (SymbolTable16)
1135 return toSymb<coff_symbol16>(Ref);
1136 if (SymbolTable32)
1137 return toSymb<coff_symbol32>(Ref);
1138 llvm_unreachable("no symbol table pointer!");
1139}
1140
1141COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1142 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001143}
1144
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001145const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001146COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1147 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001148}
1149
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001150iterator_range<const coff_relocation *>
1151COFFObjectFile::getRelocations(const coff_section *Sec) const {
1152 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1153 const coff_relocation *E = I;
1154 if (I)
1155 E += getNumberOfRelocations(Sec, Data, base());
1156 return make_range(I, E);
1157}
1158
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001159#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1160 case COFF::reloc_type: \
1161 Res = #reloc_type; \
1162 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001163
Rafael Espindola41bb4322015-06-30 04:08:37 +00001164void COFFObjectFile::getRelocationTypeName(
1165 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001166 const coff_relocation *Reloc = toRel(Rel);
1167 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001168 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001169 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001170 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001171 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1172 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1173 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1174 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1175 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1176 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1177 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1178 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1179 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1180 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1181 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1182 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1183 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1184 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1185 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1186 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1187 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1188 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001189 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001190 }
1191 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001192 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1193 switch (Reloc->Type) {
1194 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1195 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1196 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1197 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1198 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1199 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1200 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1201 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1202 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1203 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1204 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1205 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1206 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1207 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1208 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1209 default:
1210 Res = "Unknown";
1211 }
1212 break;
Mandeep Singh Grang0c721722017-06-27 23:58:19 +00001213 case COFF::IMAGE_FILE_MACHINE_ARM64:
1214 switch (Reloc->Type) {
1215 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ABSOLUTE);
1216 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32);
1217 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR32NB);
1218 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH26);
1219 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEBASE_REL21);
1220 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_REL21);
1221 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12A);
1222 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_PAGEOFFSET_12L);
1223 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL);
1224 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12A);
1225 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_HIGH12A);
1226 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECREL_LOW12L);
1227 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_TOKEN);
1228 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_SECTION);
1229 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_ADDR64);
1230 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH19);
1231 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM64_BRANCH14);
1232 default:
1233 Res = "Unknown";
1234 }
1235 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001236 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001237 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001238 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1239 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1240 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1241 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1242 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1243 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1244 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1245 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1246 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1247 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1248 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1249 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001250 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001251 }
1252 break;
1253 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001254 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001255 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001256 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001257}
1258
1259#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1260
Rafael Espindolac66d7612014-08-17 19:09:37 +00001261bool COFFObjectFile::isRelocatableObject() const {
1262 return !DataDirectory;
1263}
1264
Rui Ueyamac2bed422013-09-27 21:04:00 +00001265bool ImportDirectoryEntryRef::
1266operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001267 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001268}
1269
Rafael Espindola5e812af2014-01-30 02:49:50 +00001270void ImportDirectoryEntryRef::moveNext() {
1271 ++Index;
David Majnemer1c0aa042016-07-31 19:25:21 +00001272 if (ImportTable[Index].isNull()) {
David Majnemerad7b7e72016-06-26 04:36:32 +00001273 Index = -1;
1274 ImportTable = nullptr;
1275 }
Rui Ueyamac2bed422013-09-27 21:04:00 +00001276}
1277
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001278std::error_code ImportDirectoryEntryRef::getImportTableEntry(
David Majnemer1c0aa042016-07-31 19:25:21 +00001279 const coff_import_directory_table_entry *&Result) const {
David Majnemerad7b7e72016-06-26 04:36:32 +00001280 return getObject(Result, OwningObject->Data, ImportTable + Index);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001281}
1282
Rui Ueyama861021f2014-10-02 22:05:29 +00001283static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001284makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001285 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001286 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001287 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001288 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001289 }
1290 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001291 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001292}
1293
Rui Ueyama15d99352014-10-03 00:41:58 +00001294static imported_symbol_iterator
1295importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001296 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001297 Object->getRvaPtr(RVA, IntPtr);
1298 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001299}
1300
Rui Ueyama15d99352014-10-03 00:41:58 +00001301static imported_symbol_iterator
1302importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001303 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001304 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001305 // Forward the pointer to the last entry which is null.
1306 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001307 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001308 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1309 while (*Entry++)
1310 ++Index;
1311 } else {
1312 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1313 while (*Entry++)
1314 ++Index;
1315 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001316 return makeImportedSymbolIterator(Object, IntPtr, Index);
1317}
1318
1319imported_symbol_iterator
1320ImportDirectoryEntryRef::imported_symbol_begin() const {
David Majnemer60049522016-07-31 19:40:02 +00001321 return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001322 OwningObject);
1323}
1324
1325imported_symbol_iterator
1326ImportDirectoryEntryRef::imported_symbol_end() const {
David Majnemer60049522016-07-31 19:40:02 +00001327 return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001328 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001329}
1330
Rui Ueyama979fb402014-10-09 02:16:38 +00001331iterator_range<imported_symbol_iterator>
1332ImportDirectoryEntryRef::imported_symbols() const {
1333 return make_range(imported_symbol_begin(), imported_symbol_end());
1334}
1335
David Majnemer60049522016-07-31 19:40:02 +00001336imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const {
1337 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1338 OwningObject);
1339}
1340
1341imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const {
1342 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1343 OwningObject);
1344}
1345
1346iterator_range<imported_symbol_iterator>
1347ImportDirectoryEntryRef::lookup_table_symbols() const {
1348 return make_range(lookup_table_begin(), lookup_table_end());
1349}
1350
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001351std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001352 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001353 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001354 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001355 return EC;
1356 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001357 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001358}
1359
Rui Ueyama1e152d52014-10-02 17:02:18 +00001360std::error_code
1361ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1362 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001363 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001364}
1365
1366std::error_code
1367ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1368 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001369 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001370}
1371
Rui Ueyama15d99352014-10-03 00:41:58 +00001372bool DelayImportDirectoryEntryRef::
1373operator==(const DelayImportDirectoryEntryRef &Other) const {
1374 return Table == Other.Table && Index == Other.Index;
1375}
1376
1377void DelayImportDirectoryEntryRef::moveNext() {
1378 ++Index;
1379}
1380
1381imported_symbol_iterator
1382DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1383 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1384 OwningObject);
1385}
1386
1387imported_symbol_iterator
1388DelayImportDirectoryEntryRef::imported_symbol_end() const {
1389 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1390 OwningObject);
1391}
1392
Rui Ueyama979fb402014-10-09 02:16:38 +00001393iterator_range<imported_symbol_iterator>
1394DelayImportDirectoryEntryRef::imported_symbols() const {
1395 return make_range(imported_symbol_begin(), imported_symbol_end());
1396}
1397
Rui Ueyama15d99352014-10-03 00:41:58 +00001398std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1399 uintptr_t IntPtr = 0;
1400 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1401 return EC;
1402 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001403 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001404}
1405
Rui Ueyama1af08652014-10-03 18:07:18 +00001406std::error_code DelayImportDirectoryEntryRef::
1407getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1408 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001409 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001410}
1411
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001412std::error_code DelayImportDirectoryEntryRef::
1413getImportAddress(int AddrIndex, uint64_t &Result) const {
1414 uint32_t RVA = Table[Index].DelayImportAddressTable +
1415 AddrIndex * (OwningObject->is64() ? 8 : 4);
1416 uintptr_t IntPtr = 0;
1417 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1418 return EC;
1419 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001420 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001421 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001422 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001423 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001424}
1425
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001426bool ExportDirectoryEntryRef::
1427operator==(const ExportDirectoryEntryRef &Other) const {
1428 return ExportTable == Other.ExportTable && Index == Other.Index;
1429}
1430
Rafael Espindola5e812af2014-01-30 02:49:50 +00001431void ExportDirectoryEntryRef::moveNext() {
1432 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001433}
1434
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001435// Returns the name of the current export symbol. If the symbol is exported only
1436// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001437std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001438 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001439 if (std::error_code EC =
1440 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001441 return EC;
1442 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001443 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001444}
1445
Rui Ueyamae5df6092014-01-17 22:02:24 +00001446// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001447std::error_code
1448ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001449 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001450 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001451}
1452
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001453// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001454std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001455 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001456 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001457}
1458
1459// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001460std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001461 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001462 if (std::error_code EC =
1463 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001464 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001465 const export_address_table_entry *entry =
1466 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001467 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001468 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001469}
1470
1471// Returns the name of the current export symbol. If the symbol is exported only
1472// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001473std::error_code
1474ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001475 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001476 if (std::error_code EC =
1477 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001478 return EC;
1479 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1480
1481 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1482 int Offset = 0;
1483 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1484 I < E; ++I, ++Offset) {
1485 if (*I != Index)
1486 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001487 if (std::error_code EC =
1488 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001489 return EC;
1490 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001491 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001492 return EC;
1493 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001494 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001495 }
1496 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001497 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001498}
1499
Rui Ueyama6161b382016-01-12 23:28:42 +00001500std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1501 const data_directory *DataEntry;
1502 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1503 return EC;
1504 uint32_t RVA;
1505 if (auto EC = getExportRVA(RVA))
1506 return EC;
1507 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1508 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1509 Result = (Begin <= RVA && RVA < End);
1510 return std::error_code();
1511}
1512
1513std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1514 uint32_t RVA;
1515 if (auto EC = getExportRVA(RVA))
1516 return EC;
1517 uintptr_t IntPtr = 0;
1518 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1519 return EC;
1520 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1521 return std::error_code();
1522}
1523
Rui Ueyama861021f2014-10-02 22:05:29 +00001524bool ImportedSymbolRef::
1525operator==(const ImportedSymbolRef &Other) const {
1526 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1527 && Index == Other.Index;
1528}
1529
1530void ImportedSymbolRef::moveNext() {
1531 ++Index;
1532}
1533
1534std::error_code
1535ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1536 uint32_t RVA;
1537 if (Entry32) {
1538 // If a symbol is imported only by ordinal, it has no name.
1539 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001540 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001541 RVA = Entry32[Index].getHintNameRVA();
1542 } else {
1543 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001544 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001545 RVA = Entry64[Index].getHintNameRVA();
1546 }
1547 uintptr_t IntPtr = 0;
1548 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1549 return EC;
1550 // +2 because the first two bytes is hint.
1551 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001552 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001553}
1554
David Majnemerad7b7e72016-06-26 04:36:32 +00001555std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1556 if (Entry32)
1557 Result = Entry32[Index].isOrdinal();
1558 else
1559 Result = Entry64[Index].isOrdinal();
1560 return std::error_code();
1561}
1562
1563std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1564 if (Entry32)
1565 Result = Entry32[Index].getHintNameRVA();
1566 else
1567 Result = Entry64[Index].getHintNameRVA();
1568 return std::error_code();
1569}
1570
Rui Ueyama861021f2014-10-02 22:05:29 +00001571std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1572 uint32_t RVA;
1573 if (Entry32) {
1574 if (Entry32[Index].isOrdinal()) {
1575 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001576 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001577 }
1578 RVA = Entry32[Index].getHintNameRVA();
1579 } else {
1580 if (Entry64[Index].isOrdinal()) {
1581 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001582 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001583 }
1584 RVA = Entry64[Index].getHintNameRVA();
1585 }
1586 uintptr_t IntPtr = 0;
1587 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1588 return EC;
1589 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001590 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001591}
1592
Rafael Espindola12db3832017-10-10 20:00:07 +00001593Expected<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001594ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001595 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001596 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001597 if (EC)
Rafael Espindola12db3832017-10-10 20:00:07 +00001598 return errorCodeToError(EC);
Rafael Espindola437b0d52014-07-31 03:12:45 +00001599 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001600}
Rui Ueyama74e85132014-11-19 00:18:07 +00001601
1602bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1603 return Header == Other.Header && Index == Other.Index;
1604}
1605
1606void BaseRelocRef::moveNext() {
1607 // Header->BlockSize is the size of the current block, including the
1608 // size of the header itself.
1609 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001610 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001611 if (Size == Header->BlockSize) {
1612 // .reloc contains a list of base relocation blocks. Each block
1613 // consists of the header followed by entries. The header contains
1614 // how many entories will follow. When we reach the end of the
1615 // current block, proceed to the next block.
1616 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1617 reinterpret_cast<const uint8_t *>(Header) + Size);
1618 Index = 0;
1619 } else {
1620 ++Index;
1621 }
1622}
1623
1624std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1625 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1626 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001627 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001628}
1629
1630std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1631 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1632 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001633 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001634}
Eric Beckmannefef15a2017-05-08 02:47:07 +00001635
Rafael Espindola87867982017-10-11 17:05:24 +00001636#define RETURN_IF_ERROR(E) \
1637 if (E) \
1638 return E;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001639
Rafael Espindola87867982017-10-11 17:05:24 +00001640Expected<ArrayRef<UTF16>>
1641ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) {
Eric Beckmannefef15a2017-05-08 02:47:07 +00001642 BinaryStreamReader Reader = BinaryStreamReader(BBS);
1643 Reader.setOffset(Offset);
1644 uint16_t Length;
1645 RETURN_IF_ERROR(Reader.readInteger(Length));
1646 ArrayRef<UTF16> RawDirString;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001647 RETURN_IF_ERROR(Reader.readArray(RawDirString, Length));
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001648 return RawDirString;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001649}
1650
Rafael Espindolaf3404672017-10-11 17:33:11 +00001651Expected<ArrayRef<UTF16>>
Eric Beckmannefef15a2017-05-08 02:47:07 +00001652ResourceSectionRef::getEntryNameString(const coff_resource_dir_entry &Entry) {
Rafael Espindolaf3404672017-10-11 17:33:11 +00001653 return getDirStringAtOffset(Entry.Identifier.getNameOffset());
Eric Beckmannefef15a2017-05-08 02:47:07 +00001654}
1655
Rafael Espindola87867982017-10-11 17:05:24 +00001656Expected<const coff_resource_dir_table &>
Eric Beckmannefef15a2017-05-08 02:47:07 +00001657ResourceSectionRef::getTableAtOffset(uint32_t Offset) {
1658 const coff_resource_dir_table *Table = nullptr;
1659
1660 BinaryStreamReader Reader(BBS);
1661 Reader.setOffset(Offset);
1662 RETURN_IF_ERROR(Reader.readObject(Table));
1663 assert(Table != nullptr);
1664 return *Table;
1665}
1666
Rafael Espindolaf3404672017-10-11 17:33:11 +00001667Expected<const coff_resource_dir_table &>
Eric Beckmannefef15a2017-05-08 02:47:07 +00001668ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) {
Rafael Espindolaf3404672017-10-11 17:33:11 +00001669 return getTableAtOffset(Entry.Offset.value());
Eric Beckmannefef15a2017-05-08 02:47:07 +00001670}
1671
Rafael Espindolaf3404672017-10-11 17:33:11 +00001672Expected<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() {
1673 return getTableAtOffset(0);
Eric Beckmannefef15a2017-05-08 02:47:07 +00001674}