blob: 3d144089a20993ff4c8a38baacf0cbef43b57c66 [file] [log] [blame]
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencere5fd0042011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyama6a75acb2015-06-25 00:07:39 +000019#include "llvm/ADT/iterator_range.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000020#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000023#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000024#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000025
26using namespace llvm;
27using namespace object;
28
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000029using support::ulittle16_t;
30using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000031using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000033
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000034// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000035static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000036 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000037 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000038 return false;
39 }
40 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000041}
42
David Majnemere830c602014-11-13 08:46:37 +000043static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
David Majnemer94751be2014-11-13 09:50:18 +000044 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000045 if (Addr + Size < Addr || Addr + Size < Size ||
46 Addr + Size > uintptr_t(M.getBufferEnd()) ||
47 Addr < uintptr_t(M.getBufferStart())) {
48 return object_error::unexpected_eof;
49 }
Rui Ueyama7d099192015-06-09 15:20:42 +000050 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000051}
52
Rui Ueyamaed64342b2013-07-19 23:23:29 +000053// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
54// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000055template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000056static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000057 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000058 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000059 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000060 if (std::error_code EC = checkOffset(M, Addr, Size))
61 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000062 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyama7d099192015-06-09 15:20:42 +000063 return std::error_code();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000065
Nico Rieck9d2c15e2014-02-22 16:12:20 +000066// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
67// prefixed slashes.
68static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
69 assert(Str.size() <= 6 && "String too long, possible overflow.");
70 if (Str.size() > 6)
71 return true;
72
73 uint64_t Value = 0;
74 while (!Str.empty()) {
75 unsigned CharVal;
76 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
77 CharVal = Str[0] - 'A';
78 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
79 CharVal = Str[0] - 'a' + 26;
80 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
81 CharVal = Str[0] - '0' + 52;
82 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000083 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000084 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000085 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000086 else
87 return true;
88
89 Value = (Value * 64) + CharVal;
90 Str = Str.substr(1);
91 }
92
93 if (Value > std::numeric_limits<uint32_t>::max())
94 return true;
95
96 Result = static_cast<uint32_t>(Value);
97 return false;
98}
99
David Majnemer44f51e52014-09-10 12:51:52 +0000100template <typename coff_symbol_type>
101const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
102 const coff_symbol_type *Addr =
103 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000104
David Majnemer236b0ca2014-11-17 11:17:17 +0000105 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000106#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000107 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000108 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000109
David Majnemer44f51e52014-09-10 12:51:52 +0000110 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
111 "Symbol did not point to the beginning of a symbol");
112#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000114 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000115}
116
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000117const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
118 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000119
120# ifndef NDEBUG
121 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000122 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000123 report_fatal_error("Section was outside of section table.");
124
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000125 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
126 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000127 "Section did not point to the beginning of a section");
128# endif
129
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000130 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000131}
132
Rafael Espindola5e812af2014-01-30 02:49:50 +0000133void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000134 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000135 if (SymbolTable16) {
136 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
137 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000138 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000139 } else if (SymbolTable32) {
140 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
141 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000142 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000143 } else {
144 llvm_unreachable("no symbol table pointer!");
145 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000146}
147
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000148ErrorOr<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000150 StringRef Result;
151 std::error_code EC = getSymbolName(Symb, Result);
152 if (EC)
153 return EC;
154 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000155}
156
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000157uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
158 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000159}
160
Rafael Espindolaed067c42015-07-03 18:19:00 +0000161ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
162 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000163 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000164 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000165
166 if (Symb.isAnyUndefined() || Symb.isCommon() ||
167 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000168 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000169
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000170 const coff_section *Section = nullptr;
171 if (std::error_code EC = getSection(SectionNumber, Section))
172 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000173 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000174
175 // The section VirtualAddress does not include ImageBase, and we want to
176 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000177 Result += getImageBase();
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000178
Rafael Espindolaed067c42015-07-03 18:19:00 +0000179 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000180}
181
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000182ErrorOr<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000183 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000184 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000185
Peter Collingbournee834f422015-08-06 05:26:35 +0000186 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
187 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000188 if (Symb.isAnyUndefined())
189 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000190 if (Symb.isCommon())
191 return SymbolRef::ST_Data;
192 if (Symb.isFileRecord())
193 return SymbolRef::ST_File;
194
195 // TODO: perhaps we need a new symbol type ST_Section.
196 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
197 return SymbolRef::ST_Debug;
198
199 if (!COFF::isReservedSectionNumber(SectionNumber))
200 return SymbolRef::ST_Data;
201
202 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000203}
204
Rafael Espindola20122a42014-01-31 20:57:12 +0000205uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000206 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000207 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000208
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000209 if (Symb.isExternal() || Symb.isWeakExternal())
Lang Hames9dc0eb42016-01-25 01:21:45 +0000210 Result |= SymbolRef::SF_Global;
David Meyer1df4b842012-02-28 23:47:53 +0000211
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000212 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000213 Result |= SymbolRef::SF_Weak;
214
David Majnemer44f51e52014-09-10 12:51:52 +0000215 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000216 Result |= SymbolRef::SF_Absolute;
217
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000218 if (Symb.isFileRecord())
219 Result |= SymbolRef::SF_FormatSpecific;
220
221 if (Symb.isSectionDefinition())
222 Result |= SymbolRef::SF_FormatSpecific;
223
224 if (Symb.isCommon())
225 Result |= SymbolRef::SF_Common;
226
227 if (Symb.isAnyUndefined())
228 Result |= SymbolRef::SF_Undefined;
229
Rafael Espindola20122a42014-01-31 20:57:12 +0000230 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000231}
232
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000233uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000234 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000235 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000236}
237
Rafael Espindola8bab8892015-08-07 23:27:14 +0000238ErrorOr<section_iterator>
239COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000240 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000241 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
242 return section_end();
243 const coff_section *Sec = nullptr;
244 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
245 return EC;
246 DataRefImpl Ret;
247 Ret.p = reinterpret_cast<uintptr_t>(Sec);
248 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000249}
250
Rafael Espindola6bf32212015-06-24 19:57:32 +0000251unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
252 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
253 return Symb.getSectionNumber();
254}
255
Rafael Espindola5e812af2014-01-30 02:49:50 +0000256void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000257 const coff_section *Sec = toSec(Ref);
258 Sec += 1;
259 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000260}
261
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000262std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
263 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000264 const coff_section *Sec = toSec(Ref);
265 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000266}
267
Rafael Espindola80291272014-10-08 15:28:58 +0000268uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000269 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000270 uint64_t Result = Sec->VirtualAddress;
271
272 // The section VirtualAddress does not include ImageBase, and we want to
273 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000274 Result += getImageBase();
David Majnemer7c6a0712015-07-31 17:40:24 +0000275 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000276}
277
Rafael Espindola80291272014-10-08 15:28:58 +0000278uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000279 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000280}
281
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000282std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
283 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000284 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000285 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000286 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000287 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
288 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000289}
290
Rafael Espindola80291272014-10-08 15:28:58 +0000291uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000292 const coff_section *Sec = toSec(Ref);
David Majnemer511391f2016-03-17 16:55:18 +0000293 return Sec->getAlignment();
Michael J. Spencer79894602011-10-10 21:55:43 +0000294}
295
Rafael Espindola80291272014-10-08 15:28:58 +0000296bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000297 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000298 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000299}
300
Rafael Espindola80291272014-10-08 15:28:58 +0000301bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000302 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000303 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000304}
305
Rafael Espindola80291272014-10-08 15:28:58 +0000306bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000307 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000308 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
309 COFF::IMAGE_SCN_MEM_READ |
310 COFF::IMAGE_SCN_MEM_WRITE;
311 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000312}
313
Rafael Espindola6bf32212015-06-24 19:57:32 +0000314unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
315 uintptr_t Offset =
316 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
317 assert((Offset % sizeof(coff_section)) == 0);
318 return (Offset / sizeof(coff_section)) + 1;
319}
320
Rafael Espindola80291272014-10-08 15:28:58 +0000321bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000322 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000323 // In COFF, a virtual section won't have any in-file
324 // content, so the file pointer to the content will be zero.
325 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000326}
327
David Majnemere830c602014-11-13 08:46:37 +0000328static uint32_t getNumberOfRelocations(const coff_section *Sec,
329 MemoryBufferRef M, const uint8_t *base) {
330 // The field for the number of relocations in COFF section table is only
331 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
332 // NumberOfRelocations field, and the actual relocation count is stored in the
333 // VirtualAddress field in the first relocation entry.
334 if (Sec->hasExtendedRelocations()) {
335 const coff_relocation *FirstReloc;
336 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
337 base + Sec->PointerToRelocations)))
338 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000339 // -1 to exclude this first relocation entry.
340 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000341 }
342 return Sec->NumberOfRelocations;
343}
344
David Majnemer94751be2014-11-13 09:50:18 +0000345static const coff_relocation *
346getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
347 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
348 if (!NumRelocs)
349 return nullptr;
350 auto begin = reinterpret_cast<const coff_relocation *>(
351 Base + Sec->PointerToRelocations);
352 if (Sec->hasExtendedRelocations()) {
353 // Skip the first relocation entry repurposed to store the number of
354 // relocations.
355 begin++;
356 }
357 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
358 return nullptr;
359 return begin;
360}
361
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000362relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
363 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000364 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000365 if (begin && Sec->VirtualAddress != 0)
366 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000367 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000368 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000369 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000370}
371
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000372relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
373 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000374 const coff_relocation *I = getFirstReloc(Sec, Data, base());
375 if (I)
376 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000377 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000378 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000379 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000380}
381
Rui Ueyamac2bed422013-09-27 21:04:00 +0000382// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000383std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000384 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000385 if (std::error_code EC = getObject(
386 SymbolTable16, Data, base() + getPointerToSymbolTable(),
387 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000388 return EC;
389
390 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000391 if (std::error_code EC = getObject(
392 SymbolTable32, Data, base() + getPointerToSymbolTable(),
393 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000394 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000395
396 // Find string table. The first four byte of the string table contains the
397 // total size of the string table, including the size field itself. If the
398 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000399 uint32_t StringTableOffset = getPointerToSymbolTable() +
400 getNumberOfSymbols() * getSymbolTableEntrySize();
401 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000402 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000403 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000404 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000405 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000406 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000407 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000408 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000409
Nico Rieck773a5792014-02-26 19:51:44 +0000410 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
411 // tools like cvtres write a size of 0 for an empty table instead of 4.
412 if (StringTableSize < 4)
413 StringTableSize = 4;
414
Rui Ueyamac2bed422013-09-27 21:04:00 +0000415 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000416 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000417 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000418 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000419}
420
Reid Kleckner21427ad2015-10-09 00:15:08 +0000421uint64_t COFFObjectFile::getImageBase() const {
Reid Klecknere94fef72015-10-09 00:15:01 +0000422 if (PE32Header)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000423 return PE32Header->ImageBase;
Reid Klecknere94fef72015-10-09 00:15:01 +0000424 else if (PE32PlusHeader)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000425 return PE32PlusHeader->ImageBase;
426 // This actually comes up in practice.
427 return 0;
Reid Klecknere94fef72015-10-09 00:15:01 +0000428}
429
Rui Ueyama215a5862014-02-20 06:51:07 +0000430// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000431std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner21427ad2015-10-09 00:15:08 +0000432 uint64_t ImageBase = getImageBase();
Rui Ueyamab7a40082014-02-20 19:14:56 +0000433 uint64_t Rva = Addr - ImageBase;
434 assert(Rva <= UINT32_MAX);
435 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000436}
437
Rui Ueyamac2bed422013-09-27 21:04:00 +0000438// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000439std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000440 for (const SectionRef &S : sections()) {
441 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000442 uint32_t SectionStart = Section->VirtualAddress;
443 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000444 if (SectionStart <= Addr && Addr < SectionEnd) {
445 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000446 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000447 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000448 }
449 }
450 return object_error::parse_failed;
451}
452
453// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
454// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000455std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
456 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000457 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000458 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000459 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000460 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
461 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
462 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000463 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000464}
465
466// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000467std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468 // First, we get the RVA of the import table. If the file lacks a pointer to
469 // the import table, do nothing.
470 const data_directory *DataEntry;
471 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000472 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000473
474 // Do nothing if the pointer to import table is NULL.
475 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000476 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000477
478 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000479 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000480 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000481 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000482
483 // Find the section that contains the RVA. This is needed because the RVA is
484 // the import table's memory address which is different from its file offset.
485 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000486 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000487 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000488 ImportDirectory = reinterpret_cast<
489 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000490 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000491}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000492
Rui Ueyama15d99352014-10-03 00:41:58 +0000493// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
494std::error_code COFFObjectFile::initDelayImportTablePtr() {
495 const data_directory *DataEntry;
496 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000497 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000498 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000499 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000500
501 uint32_t RVA = DataEntry->RelativeVirtualAddress;
502 NumberOfDelayImportDirectory = DataEntry->Size /
503 sizeof(delay_import_directory_table_entry) - 1;
504
505 uintptr_t IntPtr = 0;
506 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
507 return EC;
508 DelayImportDirectory = reinterpret_cast<
509 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000510 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000511}
512
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000513// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000514std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000515 // First, we get the RVA of the export table. If the file lacks a pointer to
516 // the export table, do nothing.
517 const data_directory *DataEntry;
518 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000519 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000520
521 // Do nothing if the pointer to export table is NULL.
522 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000523 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000524
525 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
526 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000527 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000528 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000529 ExportDirectory =
530 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000531 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000532}
533
Rui Ueyama74e85132014-11-19 00:18:07 +0000534std::error_code COFFObjectFile::initBaseRelocPtr() {
535 const data_directory *DataEntry;
536 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000537 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000538 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000539 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000540
541 uintptr_t IntPtr = 0;
542 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
543 return EC;
544 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
545 IntPtr);
546 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
547 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000548 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000549}
550
Rafael Espindola48af1c22014-08-19 18:44:46 +0000551COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
552 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000553 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
554 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
555 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
556 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000557 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000558 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
559 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000560 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000561 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000562 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000563
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000564 // The current location in the file where we are looking at.
565 uint64_t CurPtr = 0;
566
567 // PE header is optional and is present only in executables. If it exists,
568 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000569 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000570
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000571 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000572 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000573 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
574 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000575 const auto *DH = reinterpret_cast<const dos_header *>(base());
576 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
577 CurPtr = DH->AddressOfNewExeHeader;
578 // Check the PE magic bytes. ("PE\0\0")
579 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
580 EC = object_error::parse_failed;
581 return;
582 }
583 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
584 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000585 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000586 }
587
Rafael Espindola48af1c22014-08-19 18:44:46 +0000588 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000589 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000590
591 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
592 // import libraries share a common prefix but bigobj is more restrictive.
593 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
594 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
595 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
596 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
597 return;
598
599 // Verify that we are dealing with bigobj.
600 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
601 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
602 sizeof(COFF::BigObjMagic)) == 0) {
603 COFFHeader = nullptr;
604 CurPtr += sizeof(coff_bigobj_file_header);
605 } else {
606 // It's not a bigobj.
607 COFFBigObjHeader = nullptr;
608 }
609 }
610 if (COFFHeader) {
611 // The prior checkSize call may have failed. This isn't a hard error
612 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000613 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000614 CurPtr += sizeof(coff_file_header);
615
616 if (COFFHeader->isImportLibrary())
617 return;
618 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000619
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000620 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000621 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000622 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000623 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000624
625 const uint8_t *DataDirAddr;
626 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000627 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000628 PE32Header = Header;
629 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
630 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000631 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000632 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
633 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
634 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
635 } else {
636 // It's neither PE32 nor PE32+.
637 EC = object_error::parse_failed;
638 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000639 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000640 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000641 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000642 CurPtr += COFFHeader->SizeOfOptionalHeader;
643 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000644
Rafael Espindola48af1c22014-08-19 18:44:46 +0000645 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000646 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000647 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000648
Rui Ueyamac2bed422013-09-27 21:04:00 +0000649 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000650 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000651 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000652 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000653 } else {
654 // We had better not have any symbols if we don't have a symbol table.
655 if (getNumberOfSymbols() != 0) {
656 EC = object_error::parse_failed;
657 return;
658 }
659 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000660
Rui Ueyamac2bed422013-09-27 21:04:00 +0000661 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000662 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000663 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000664 if ((EC = initDelayImportTablePtr()))
665 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000666
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000667 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000668 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000669 return;
670
Rui Ueyama74e85132014-11-19 00:18:07 +0000671 // Initialize the pointer to the base relocation table.
672 if ((EC = initBaseRelocPtr()))
673 return;
674
Rui Ueyama7d099192015-06-09 15:20:42 +0000675 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000676}
677
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000678basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000679 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000680 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000681 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000682}
683
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000684basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000685 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000686 DataRefImpl Ret;
687 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000688 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000689}
690
Rui Ueyamabc654b12013-09-27 21:47:05 +0000691import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000692 return import_directory_iterator(
693 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000694}
695
Rui Ueyamabc654b12013-09-27 21:47:05 +0000696import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000697 return import_directory_iterator(
698 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000699}
David Meyerc429b802012-03-01 22:19:54 +0000700
Rui Ueyama15d99352014-10-03 00:41:58 +0000701delay_import_directory_iterator
702COFFObjectFile::delay_import_directory_begin() const {
703 return delay_import_directory_iterator(
704 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
705}
706
707delay_import_directory_iterator
708COFFObjectFile::delay_import_directory_end() const {
709 return delay_import_directory_iterator(
710 DelayImportDirectoryEntryRef(
711 DelayImportDirectory, NumberOfDelayImportDirectory, this));
712}
713
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000714export_directory_iterator COFFObjectFile::export_directory_begin() const {
715 return export_directory_iterator(
716 ExportDirectoryEntryRef(ExportDirectory, 0, this));
717}
718
719export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000720 if (!ExportDirectory)
721 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000722 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000723 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000724 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000725}
726
Rafael Espindolab5155a52014-02-10 20:24:04 +0000727section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000728 DataRefImpl Ret;
729 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
730 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000731}
732
Rafael Espindolab5155a52014-02-10 20:24:04 +0000733section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000734 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000735 int NumSections =
736 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000737 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
738 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000739}
740
Rui Ueyama74e85132014-11-19 00:18:07 +0000741base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
742 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
743}
744
745base_reloc_iterator COFFObjectFile::base_reloc_end() const {
746 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
747}
748
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000749uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000750 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000751}
752
753StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000754 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000755 case COFF::IMAGE_FILE_MACHINE_I386:
756 return "COFF-i386";
757 case COFF::IMAGE_FILE_MACHINE_AMD64:
758 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000759 case COFF::IMAGE_FILE_MACHINE_ARMNT:
760 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000761 case COFF::IMAGE_FILE_MACHINE_ARM64:
762 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000763 default:
764 return "COFF-<unknown arch>";
765 }
766}
767
768unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000769 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000770 case COFF::IMAGE_FILE_MACHINE_I386:
771 return Triple::x86;
772 case COFF::IMAGE_FILE_MACHINE_AMD64:
773 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000774 case COFF::IMAGE_FILE_MACHINE_ARMNT:
775 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000776 case COFF::IMAGE_FILE_MACHINE_ARM64:
777 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000778 default:
779 return Triple::UnknownArch;
780 }
781}
782
Rui Ueyama979fb402014-10-09 02:16:38 +0000783iterator_range<import_directory_iterator>
784COFFObjectFile::import_directories() const {
785 return make_range(import_directory_begin(), import_directory_end());
786}
787
788iterator_range<delay_import_directory_iterator>
789COFFObjectFile::delay_import_directories() const {
790 return make_range(delay_import_directory_begin(),
791 delay_import_directory_end());
792}
793
794iterator_range<export_directory_iterator>
795COFFObjectFile::export_directories() const {
796 return make_range(export_directory_begin(), export_directory_end());
797}
798
Rui Ueyama74e85132014-11-19 00:18:07 +0000799iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
800 return make_range(base_reloc_begin(), base_reloc_end());
801}
802
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000803std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000804 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000805 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000806}
807
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000808std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000809COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
810 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000811 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000812}
813
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000814std::error_code
815COFFObjectFile::getDataDirectory(uint32_t Index,
816 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000817 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000818 if (!DataDirectory) {
819 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000820 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000821 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000822 assert(PE32Header || PE32PlusHeader);
823 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
824 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000825 if (Index >= NumEnt) {
826 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000827 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000828 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000829 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000830 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000831}
832
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000833std::error_code COFFObjectFile::getSection(int32_t Index,
834 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000835 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000836 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000837 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000838 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000839 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000840 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000841 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000842 }
843 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000844}
845
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000846std::error_code COFFObjectFile::getString(uint32_t Offset,
847 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000848 if (StringTableSize <= 4)
849 // Tried to get a string from an empty string table.
850 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000851 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000852 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000853 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000854 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000855}
856
David Majnemer44f51e52014-09-10 12:51:52 +0000857std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000858 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000859 return getSymbolName(Symbol.getGeneric(), Res);
860}
861
862std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
863 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000864 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000865 if (Symbol->Name.Offset.Zeroes == 0) {
866 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000867 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000868 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000869 }
870
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000871 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000872 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000873 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000874 else
875 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000876 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000877 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000878}
879
David Majnemer44f51e52014-09-10 12:51:52 +0000880ArrayRef<uint8_t>
881COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000882 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000883
David Majnemer44f51e52014-09-10 12:51:52 +0000884 size_t SymbolSize = getSymbolTableEntrySize();
885 if (Symbol.getNumberOfAuxSymbols() > 0) {
886 // AUX data comes immediately after the symbol in COFF
887 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000888# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000889 // Verify that the Aux symbol points to a valid entry in the symbol table.
890 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000891 if (Offset < getPointerToSymbolTable() ||
892 Offset >=
893 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000894 report_fatal_error("Aux Symbol data was outside of symbol table.");
895
David Majnemer44f51e52014-09-10 12:51:52 +0000896 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
897 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000898# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000899 }
David Majnemer44f51e52014-09-10 12:51:52 +0000900 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000901}
902
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000903std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
904 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000905 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000906 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000907 // Null terminated, let ::strlen figure out the length.
908 Name = Sec->Name;
909 else
910 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000911 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000912
913 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000914 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000915 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000916 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000917 if (decodeBase64StringEntry(Name.substr(2), Offset))
918 return object_error::parse_failed;
919 } else {
920 if (Name.substr(1).getAsInteger(10, Offset))
921 return object_error::parse_failed;
922 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000923 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000924 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000925 }
926
927 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000928 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000929}
930
David Majnemera9ee5c02014-10-09 08:42:31 +0000931uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
932 // SizeOfRawData and VirtualSize change what they represent depending on
933 // whether or not we have an executable image.
934 //
935 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000936 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +0000937 //
938 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
939 // actual section size is in VirtualSize. It is possible for VirtualSize to
940 // be greater than SizeOfRawData; the contents past that point should be
941 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000942 if (getDOSHeader())
943 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
944 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000945}
946
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000947std::error_code
948COFFObjectFile::getSectionContents(const coff_section *Sec,
949 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000950 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
951 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000952 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
953 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000954 // The only thing that we need to verify is that the contents is contained
955 // within the file bounds. We don't need to make sure it doesn't cover other
956 // data, as there's nothing that says that is not allowed.
957 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000958 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000959 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000960 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000961 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000962 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000963}
964
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000965const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000966 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000967}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000968
Rafael Espindola5e812af2014-01-30 02:49:50 +0000969void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000970 Rel.p = reinterpret_cast<uintptr_t>(
971 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000972}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000973
Rafael Espindola96d071c2015-06-29 23:29:12 +0000974uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +0000975 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000976 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000977}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000978
Rafael Espindola806f0062013-06-05 01:33:53 +0000979symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000980 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000981 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000982 if (R->SymbolTableIndex >= getNumberOfSymbols())
983 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000984 if (SymbolTable16)
985 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
986 else if (SymbolTable32)
987 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
988 else
David Majnemerc7353b52014-11-25 07:43:14 +0000989 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000990 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000991}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000992
Rafael Espindola99c041b2015-06-30 01:53:01 +0000993uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000994 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +0000995 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000996}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000997
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000998const coff_section *
999COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1000 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001001}
1002
David Majnemer44f51e52014-09-10 12:51:52 +00001003COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1004 if (SymbolTable16)
1005 return toSymb<coff_symbol16>(Ref);
1006 if (SymbolTable32)
1007 return toSymb<coff_symbol32>(Ref);
1008 llvm_unreachable("no symbol table pointer!");
1009}
1010
1011COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1012 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001013}
1014
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001015const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001016COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1017 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001018}
1019
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001020iterator_range<const coff_relocation *>
1021COFFObjectFile::getRelocations(const coff_section *Sec) const {
1022 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1023 const coff_relocation *E = I;
1024 if (I)
1025 E += getNumberOfRelocations(Sec, Data, base());
1026 return make_range(I, E);
1027}
1028
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001029#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1030 case COFF::reloc_type: \
1031 Res = #reloc_type; \
1032 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001033
Rafael Espindola41bb4322015-06-30 04:08:37 +00001034void COFFObjectFile::getRelocationTypeName(
1035 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001036 const coff_relocation *Reloc = toRel(Rel);
1037 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001038 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001039 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001040 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1058 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001059 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001060 }
1061 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001062 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1063 switch (Reloc->Type) {
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1079 default:
1080 Res = "Unknown";
1081 }
1082 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001083 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001084 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1096 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001097 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001098 }
1099 break;
1100 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001101 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001102 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001103 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001104}
1105
1106#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1107
Rafael Espindolac66d7612014-08-17 19:09:37 +00001108bool COFFObjectFile::isRelocatableObject() const {
1109 return !DataDirectory;
1110}
1111
Rui Ueyamac2bed422013-09-27 21:04:00 +00001112bool ImportDirectoryEntryRef::
1113operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001114 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001115}
1116
Rafael Espindola5e812af2014-01-30 02:49:50 +00001117void ImportDirectoryEntryRef::moveNext() {
1118 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001119}
1120
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001121std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1122 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001123 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001124 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001125}
1126
Rui Ueyama861021f2014-10-02 22:05:29 +00001127static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001128makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001129 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001130 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001131 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001132 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001133 }
1134 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001135 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001136}
1137
Rui Ueyama15d99352014-10-03 00:41:58 +00001138static imported_symbol_iterator
1139importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001140 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001141 Object->getRvaPtr(RVA, IntPtr);
1142 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001143}
1144
Rui Ueyama15d99352014-10-03 00:41:58 +00001145static imported_symbol_iterator
1146importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001147 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001148 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001149 // Forward the pointer to the last entry which is null.
1150 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001151 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001152 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1153 while (*Entry++)
1154 ++Index;
1155 } else {
1156 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1157 while (*Entry++)
1158 ++Index;
1159 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001160 return makeImportedSymbolIterator(Object, IntPtr, Index);
1161}
1162
1163imported_symbol_iterator
1164ImportDirectoryEntryRef::imported_symbol_begin() const {
1165 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1166 OwningObject);
1167}
1168
1169imported_symbol_iterator
1170ImportDirectoryEntryRef::imported_symbol_end() const {
1171 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1172 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001173}
1174
Rui Ueyama979fb402014-10-09 02:16:38 +00001175iterator_range<imported_symbol_iterator>
1176ImportDirectoryEntryRef::imported_symbols() const {
1177 return make_range(imported_symbol_begin(), imported_symbol_end());
1178}
1179
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001180std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001181 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001182 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001183 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001184 return EC;
1185 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001186 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001187}
1188
Rui Ueyama1e152d52014-10-02 17:02:18 +00001189std::error_code
1190ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1191 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001192 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001193}
1194
1195std::error_code
1196ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1197 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001198 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001199}
1200
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001201std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001202 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001203 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001204 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1205 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001206 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001207 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001208 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001209}
1210
Rui Ueyama15d99352014-10-03 00:41:58 +00001211bool DelayImportDirectoryEntryRef::
1212operator==(const DelayImportDirectoryEntryRef &Other) const {
1213 return Table == Other.Table && Index == Other.Index;
1214}
1215
1216void DelayImportDirectoryEntryRef::moveNext() {
1217 ++Index;
1218}
1219
1220imported_symbol_iterator
1221DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1222 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1223 OwningObject);
1224}
1225
1226imported_symbol_iterator
1227DelayImportDirectoryEntryRef::imported_symbol_end() const {
1228 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1229 OwningObject);
1230}
1231
Rui Ueyama979fb402014-10-09 02:16:38 +00001232iterator_range<imported_symbol_iterator>
1233DelayImportDirectoryEntryRef::imported_symbols() const {
1234 return make_range(imported_symbol_begin(), imported_symbol_end());
1235}
1236
Rui Ueyama15d99352014-10-03 00:41:58 +00001237std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1238 uintptr_t IntPtr = 0;
1239 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1240 return EC;
1241 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001242 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001243}
1244
Rui Ueyama1af08652014-10-03 18:07:18 +00001245std::error_code DelayImportDirectoryEntryRef::
1246getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1247 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001248 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001249}
1250
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001251std::error_code DelayImportDirectoryEntryRef::
1252getImportAddress(int AddrIndex, uint64_t &Result) const {
1253 uint32_t RVA = Table[Index].DelayImportAddressTable +
1254 AddrIndex * (OwningObject->is64() ? 8 : 4);
1255 uintptr_t IntPtr = 0;
1256 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1257 return EC;
1258 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001259 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001260 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001261 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001262 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001263}
1264
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001265bool ExportDirectoryEntryRef::
1266operator==(const ExportDirectoryEntryRef &Other) const {
1267 return ExportTable == Other.ExportTable && Index == Other.Index;
1268}
1269
Rafael Espindola5e812af2014-01-30 02:49:50 +00001270void ExportDirectoryEntryRef::moveNext() {
1271 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001272}
1273
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001274// Returns the name of the current export symbol. If the symbol is exported only
1275// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001276std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001277 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001278 if (std::error_code EC =
1279 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001280 return EC;
1281 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001282 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001283}
1284
Rui Ueyamae5df6092014-01-17 22:02:24 +00001285// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001286std::error_code
1287ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001288 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001289 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001290}
1291
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001292// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001293std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001294 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001295 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001296}
1297
1298// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001299std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001300 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001301 if (std::error_code EC =
1302 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001303 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001304 const export_address_table_entry *entry =
1305 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001306 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001307 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001308}
1309
1310// Returns the name of the current export symbol. If the symbol is exported only
1311// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001312std::error_code
1313ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001314 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001315 if (std::error_code EC =
1316 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001317 return EC;
1318 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1319
1320 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1321 int Offset = 0;
1322 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1323 I < E; ++I, ++Offset) {
1324 if (*I != Index)
1325 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001326 if (std::error_code EC =
1327 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001328 return EC;
1329 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001330 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001331 return EC;
1332 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001333 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001334 }
1335 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001336 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001337}
1338
Rui Ueyama6161b382016-01-12 23:28:42 +00001339std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1340 const data_directory *DataEntry;
1341 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1342 return EC;
1343 uint32_t RVA;
1344 if (auto EC = getExportRVA(RVA))
1345 return EC;
1346 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1347 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1348 Result = (Begin <= RVA && RVA < End);
1349 return std::error_code();
1350}
1351
1352std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1353 uint32_t RVA;
1354 if (auto EC = getExportRVA(RVA))
1355 return EC;
1356 uintptr_t IntPtr = 0;
1357 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1358 return EC;
1359 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1360 return std::error_code();
1361}
1362
Rui Ueyama861021f2014-10-02 22:05:29 +00001363bool ImportedSymbolRef::
1364operator==(const ImportedSymbolRef &Other) const {
1365 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1366 && Index == Other.Index;
1367}
1368
1369void ImportedSymbolRef::moveNext() {
1370 ++Index;
1371}
1372
1373std::error_code
1374ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1375 uint32_t RVA;
1376 if (Entry32) {
1377 // If a symbol is imported only by ordinal, it has no name.
1378 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001379 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001380 RVA = Entry32[Index].getHintNameRVA();
1381 } else {
1382 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001383 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001384 RVA = Entry64[Index].getHintNameRVA();
1385 }
1386 uintptr_t IntPtr = 0;
1387 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1388 return EC;
1389 // +2 because the first two bytes is hint.
1390 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001391 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001392}
1393
1394std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1395 uint32_t RVA;
1396 if (Entry32) {
1397 if (Entry32[Index].isOrdinal()) {
1398 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001399 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001400 }
1401 RVA = Entry32[Index].getHintNameRVA();
1402 } else {
1403 if (Entry64[Index].isOrdinal()) {
1404 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001405 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001406 }
1407 RVA = Entry64[Index].getHintNameRVA();
1408 }
1409 uintptr_t IntPtr = 0;
1410 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1411 return EC;
1412 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001413 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001414}
1415
Rafael Espindola437b0d52014-07-31 03:12:45 +00001416ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001417ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001418 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001419 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001420 if (EC)
1421 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001422 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001423}
Rui Ueyama74e85132014-11-19 00:18:07 +00001424
1425bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1426 return Header == Other.Header && Index == Other.Index;
1427}
1428
1429void BaseRelocRef::moveNext() {
1430 // Header->BlockSize is the size of the current block, including the
1431 // size of the header itself.
1432 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001433 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001434 if (Size == Header->BlockSize) {
1435 // .reloc contains a list of base relocation blocks. Each block
1436 // consists of the header followed by entries. The header contains
1437 // how many entories will follow. When we reach the end of the
1438 // current block, proceed to the next block.
1439 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1440 reinterpret_cast<const uint8_t *>(Header) + Size);
1441 Index = 0;
1442 } else {
1443 ++Index;
1444 }
1445}
1446
1447std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1448 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1449 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001450 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001451}
1452
1453std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1454 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1455 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001456 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001457}