blob: 507d690f0964232120e9baa42a7b9f52adbde662 [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 Espindoladb4ed0b2014-06-13 02:24:39 +0000148std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
149 StringRef &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000150 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000151 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000152}
153
Rafael Espindola991af662015-06-24 19:11:10 +0000154uint64_t COFFObjectFile::getSymbolValue(DataRefImpl Ref) const {
155 COFFSymbolRef Sym = getCOFFSymbol(Ref);
156
157 if (Sym.isAnyUndefined() || Sym.isCommon())
158 return UnknownAddress;
159
160 return Sym.getValue();
161}
162
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000163std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
164 uint64_t &Result) const {
Rafael Espindola991af662015-06-24 19:11:10 +0000165 Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000166 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000167 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000168
169 if (Symb.isAnyUndefined() || Symb.isCommon() ||
170 COFF::isReservedSectionNumber(SectionNumber))
Rui Ueyama7d099192015-06-09 15:20:42 +0000171 return std::error_code();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000172
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000173 const coff_section *Section = nullptr;
174 if (std::error_code EC = getSection(SectionNumber, Section))
175 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000176 Result += Section->VirtualAddress;
Rui Ueyama7d099192015-06-09 15:20:42 +0000177 return std::error_code();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000178}
179
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000180std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
181 SymbolRef::Type &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000182 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000183 int32_t SectionNumber = Symb.getSectionNumber();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000184 Result = SymbolRef::ST_Other;
David Majnemer44f51e52014-09-10 12:51:52 +0000185
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000186 if (Symb.isAnyUndefined()) {
David Meyer7e4b9762012-02-29 02:11:55 +0000187 Result = SymbolRef::ST_Unknown;
David Majnemer44f51e52014-09-10 12:51:52 +0000188 } else if (Symb.isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000189 Result = SymbolRef::ST_Function;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000190 } else if (Symb.isCommon()) {
191 Result = SymbolRef::ST_Data;
192 } else if (Symb.isFileRecord()) {
193 Result = SymbolRef::ST_File;
David Majnemer1a666e02015-03-07 20:21:27 +0000194 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG ||
195 Symb.isSectionDefinition()) {
196 // TODO: perhaps we need a new symbol type ST_Section.
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000197 Result = SymbolRef::ST_Debug;
198 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
199 const coff_section *Section = nullptr;
200 if (std::error_code EC = getSection(SectionNumber, Section))
201 return EC;
202 uint32_t Characteristics = Section->Characteristics;
203 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
204 Result = SymbolRef::ST_Function;
205 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
206 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
Rui Ueyama5efa6652014-01-16 20:22:55 +0000207 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000208 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000209 return std::error_code();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000210}
211
Rafael Espindola20122a42014-01-31 20:57:12 +0000212uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000213 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000214 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000215
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000216 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000217 Result |= SymbolRef::SF_Global;
218
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000219 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000220 Result |= SymbolRef::SF_Weak;
221
David Majnemer44f51e52014-09-10 12:51:52 +0000222 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000223 Result |= SymbolRef::SF_Absolute;
224
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000225 if (Symb.isFileRecord())
226 Result |= SymbolRef::SF_FormatSpecific;
227
228 if (Symb.isSectionDefinition())
229 Result |= SymbolRef::SF_FormatSpecific;
230
231 if (Symb.isCommon())
232 Result |= SymbolRef::SF_Common;
233
234 if (Symb.isAnyUndefined())
235 Result |= SymbolRef::SF_Undefined;
236
Rafael Espindola20122a42014-01-31 20:57:12 +0000237 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000238}
239
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000240uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000241 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000242 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000243}
244
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000245std::error_code
246COFFObjectFile::getSymbolSection(DataRefImpl Ref,
247 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000248 COFFSymbolRef Symb = getCOFFSymbol(Ref);
249 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000250 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000251 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000252 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000253 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000254 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000255 DataRefImpl Ref;
256 Ref.p = reinterpret_cast<uintptr_t>(Sec);
257 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000258 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000259 return std::error_code();
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);
Rafael Espindola80291272014-10-08 15:28:58 +0000281 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000282}
283
Rafael Espindola80291272014-10-08 15:28:58 +0000284uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000285 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000286}
287
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000288std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
289 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000290 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000291 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000292 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000293 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
294 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000295}
296
Rafael Espindola80291272014-10-08 15:28:58 +0000297uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000298 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000299 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000300}
301
Rafael Espindola80291272014-10-08 15:28:58 +0000302bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000303 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000304 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000305}
306
Rafael Espindola80291272014-10-08 15:28:58 +0000307bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000308 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000309 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000310}
311
Rafael Espindola80291272014-10-08 15:28:58 +0000312bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000313 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000314 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
315 COFF::IMAGE_SCN_MEM_READ |
316 COFF::IMAGE_SCN_MEM_WRITE;
317 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000318}
319
Rafael Espindola6bf32212015-06-24 19:57:32 +0000320unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
321 uintptr_t Offset =
322 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
323 assert((Offset % sizeof(coff_section)) == 0);
324 return (Offset / sizeof(coff_section)) + 1;
325}
326
Rafael Espindola80291272014-10-08 15:28:58 +0000327bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000328 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000329 // In COFF, a virtual section won't have any in-file
330 // content, so the file pointer to the content will be zero.
331 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000332}
333
Rafael Espindola80291272014-10-08 15:28:58 +0000334bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
335 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000336 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000337 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000338 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000339 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000340}
341
David Majnemere830c602014-11-13 08:46:37 +0000342static uint32_t getNumberOfRelocations(const coff_section *Sec,
343 MemoryBufferRef M, const uint8_t *base) {
344 // The field for the number of relocations in COFF section table is only
345 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
346 // NumberOfRelocations field, and the actual relocation count is stored in the
347 // VirtualAddress field in the first relocation entry.
348 if (Sec->hasExtendedRelocations()) {
349 const coff_relocation *FirstReloc;
350 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
351 base + Sec->PointerToRelocations)))
352 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000353 // -1 to exclude this first relocation entry.
354 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000355 }
356 return Sec->NumberOfRelocations;
357}
358
David Majnemer94751be2014-11-13 09:50:18 +0000359static const coff_relocation *
360getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
361 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
362 if (!NumRelocs)
363 return nullptr;
364 auto begin = reinterpret_cast<const coff_relocation *>(
365 Base + Sec->PointerToRelocations);
366 if (Sec->hasExtendedRelocations()) {
367 // Skip the first relocation entry repurposed to store the number of
368 // relocations.
369 begin++;
370 }
371 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
372 return nullptr;
373 return begin;
374}
375
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000376relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
377 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000378 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000379 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000380 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000381 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000382}
383
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000384relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
385 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000386 const coff_relocation *I = getFirstReloc(Sec, Data, base());
387 if (I)
388 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000390 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000391 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000392}
393
Rui Ueyamac2bed422013-09-27 21:04:00 +0000394// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000395std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000396 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000397 if (std::error_code EC = getObject(
398 SymbolTable16, Data, base() + getPointerToSymbolTable(),
399 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000400 return EC;
401
402 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000403 if (std::error_code EC = getObject(
404 SymbolTable32, Data, base() + getPointerToSymbolTable(),
405 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000406 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000407
408 // Find string table. The first four byte of the string table contains the
409 // total size of the string table, including the size field itself. If the
410 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000411 uint32_t StringTableOffset = getPointerToSymbolTable() +
412 getNumberOfSymbols() * getSymbolTableEntrySize();
413 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000414 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000415 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000416 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000417 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000418 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000419 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000420 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000421
Nico Rieck773a5792014-02-26 19:51:44 +0000422 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
423 // tools like cvtres write a size of 0 for an empty table instead of 4.
424 if (StringTableSize < 4)
425 StringTableSize = 4;
426
Rui Ueyamac2bed422013-09-27 21:04:00 +0000427 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000428 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000430 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000431}
432
Rui Ueyama215a5862014-02-20 06:51:07 +0000433// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000434std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000435 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
436 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000437 uint64_t Rva = Addr - ImageBase;
438 assert(Rva <= UINT32_MAX);
439 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000440}
441
Rui Ueyamac2bed422013-09-27 21:04:00 +0000442// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000443std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000444 for (const SectionRef &S : sections()) {
445 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000446 uint32_t SectionStart = Section->VirtualAddress;
447 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000448 if (SectionStart <= Addr && Addr < SectionEnd) {
449 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000450 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000451 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000452 }
453 }
454 return object_error::parse_failed;
455}
456
457// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
458// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000459std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
460 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000461 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000462 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000463 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000464 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
465 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
466 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000467 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468}
469
470// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000471std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000472 // First, we get the RVA of the import table. If the file lacks a pointer to
473 // the import table, do nothing.
474 const data_directory *DataEntry;
475 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000476 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000477
478 // Do nothing if the pointer to import table is NULL.
479 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000480 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000481
482 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000483 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000484 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000485 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000486
487 // Find the section that contains the RVA. This is needed because the RVA is
488 // the import table's memory address which is different from its file offset.
489 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000490 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000491 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000492 ImportDirectory = reinterpret_cast<
493 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000494 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000495}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000496
Rui Ueyama15d99352014-10-03 00:41:58 +0000497// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
498std::error_code COFFObjectFile::initDelayImportTablePtr() {
499 const data_directory *DataEntry;
500 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000501 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000502 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000503 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000504
505 uint32_t RVA = DataEntry->RelativeVirtualAddress;
506 NumberOfDelayImportDirectory = DataEntry->Size /
507 sizeof(delay_import_directory_table_entry) - 1;
508
509 uintptr_t IntPtr = 0;
510 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
511 return EC;
512 DelayImportDirectory = reinterpret_cast<
513 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000514 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000515}
516
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000517// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000518std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000519 // First, we get the RVA of the export table. If the file lacks a pointer to
520 // the export table, do nothing.
521 const data_directory *DataEntry;
522 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000523 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000524
525 // Do nothing if the pointer to export table is NULL.
526 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000527 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000528
529 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
530 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000531 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000532 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000533 ExportDirectory =
534 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000535 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000536}
537
Rui Ueyama74e85132014-11-19 00:18:07 +0000538std::error_code COFFObjectFile::initBaseRelocPtr() {
539 const data_directory *DataEntry;
540 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000541 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000542 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000543 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000544
545 uintptr_t IntPtr = 0;
546 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
547 return EC;
548 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
549 IntPtr);
550 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
551 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000552 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000553}
554
Rafael Espindola48af1c22014-08-19 18:44:46 +0000555COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
556 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000557 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
558 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
559 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
560 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000561 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000562 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
563 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000564 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000565 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000566 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000567
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000568 // The current location in the file where we are looking at.
569 uint64_t CurPtr = 0;
570
571 // PE header is optional and is present only in executables. If it exists,
572 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000573 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000574
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000575 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000576 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000577 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
578 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000579 const auto *DH = reinterpret_cast<const dos_header *>(base());
580 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
581 CurPtr = DH->AddressOfNewExeHeader;
582 // Check the PE magic bytes. ("PE\0\0")
583 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
584 EC = object_error::parse_failed;
585 return;
586 }
587 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
588 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000589 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000590 }
591
Rafael Espindola48af1c22014-08-19 18:44:46 +0000592 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000593 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000594
595 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
596 // import libraries share a common prefix but bigobj is more restrictive.
597 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
598 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
599 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
600 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
601 return;
602
603 // Verify that we are dealing with bigobj.
604 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
605 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
606 sizeof(COFF::BigObjMagic)) == 0) {
607 COFFHeader = nullptr;
608 CurPtr += sizeof(coff_bigobj_file_header);
609 } else {
610 // It's not a bigobj.
611 COFFBigObjHeader = nullptr;
612 }
613 }
614 if (COFFHeader) {
615 // The prior checkSize call may have failed. This isn't a hard error
616 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000617 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000618 CurPtr += sizeof(coff_file_header);
619
620 if (COFFHeader->isImportLibrary())
621 return;
622 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000623
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000624 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000625 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000626 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000627 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000628
629 const uint8_t *DataDirAddr;
630 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000631 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000632 PE32Header = Header;
633 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
634 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000635 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000636 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
637 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
638 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
639 } else {
640 // It's neither PE32 nor PE32+.
641 EC = object_error::parse_failed;
642 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000643 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000644 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000645 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000646 CurPtr += COFFHeader->SizeOfOptionalHeader;
647 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000648
Rafael Espindola48af1c22014-08-19 18:44:46 +0000649 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000650 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000651 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000652
Rui Ueyamac2bed422013-09-27 21:04:00 +0000653 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000654 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000655 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000656 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000657 } else {
658 // We had better not have any symbols if we don't have a symbol table.
659 if (getNumberOfSymbols() != 0) {
660 EC = object_error::parse_failed;
661 return;
662 }
663 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000664
Rui Ueyamac2bed422013-09-27 21:04:00 +0000665 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000666 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000667 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000668 if ((EC = initDelayImportTablePtr()))
669 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000670
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000671 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000672 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000673 return;
674
Rui Ueyama74e85132014-11-19 00:18:07 +0000675 // Initialize the pointer to the base relocation table.
676 if ((EC = initBaseRelocPtr()))
677 return;
678
Rui Ueyama7d099192015-06-09 15:20:42 +0000679 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000680}
681
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000682basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000683 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000684 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000685 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000686}
687
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000688basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000689 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000690 DataRefImpl Ret;
691 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000692 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000693}
694
Rui Ueyamabc654b12013-09-27 21:47:05 +0000695import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000696 return import_directory_iterator(
697 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000698}
699
Rui Ueyamabc654b12013-09-27 21:47:05 +0000700import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000701 return import_directory_iterator(
702 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000703}
David Meyerc429b802012-03-01 22:19:54 +0000704
Rui Ueyama15d99352014-10-03 00:41:58 +0000705delay_import_directory_iterator
706COFFObjectFile::delay_import_directory_begin() const {
707 return delay_import_directory_iterator(
708 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
709}
710
711delay_import_directory_iterator
712COFFObjectFile::delay_import_directory_end() const {
713 return delay_import_directory_iterator(
714 DelayImportDirectoryEntryRef(
715 DelayImportDirectory, NumberOfDelayImportDirectory, this));
716}
717
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000718export_directory_iterator COFFObjectFile::export_directory_begin() const {
719 return export_directory_iterator(
720 ExportDirectoryEntryRef(ExportDirectory, 0, this));
721}
722
723export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000724 if (!ExportDirectory)
725 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000726 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000727 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000728 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000729}
730
Rafael Espindolab5155a52014-02-10 20:24:04 +0000731section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000732 DataRefImpl Ret;
733 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
734 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000735}
736
Rafael Espindolab5155a52014-02-10 20:24:04 +0000737section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000738 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000739 int NumSections =
740 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000741 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
742 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000743}
744
Rui Ueyama74e85132014-11-19 00:18:07 +0000745base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
746 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
747}
748
749base_reloc_iterator COFFObjectFile::base_reloc_end() const {
750 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
751}
752
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000753uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000754 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000755}
756
757StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000758 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000759 case COFF::IMAGE_FILE_MACHINE_I386:
760 return "COFF-i386";
761 case COFF::IMAGE_FILE_MACHINE_AMD64:
762 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000763 case COFF::IMAGE_FILE_MACHINE_ARMNT:
764 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000765 default:
766 return "COFF-<unknown arch>";
767 }
768}
769
770unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000771 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000772 case COFF::IMAGE_FILE_MACHINE_I386:
773 return Triple::x86;
774 case COFF::IMAGE_FILE_MACHINE_AMD64:
775 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000776 case COFF::IMAGE_FILE_MACHINE_ARMNT:
777 return Triple::thumb;
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 {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000859 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000860 if (Symbol.getStringTableOffset().Zeroes == 0) {
861 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000862 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000863 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000864 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000865 }
866
David Majnemer44f51e52014-09-10 12:51:52 +0000867 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000868 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000869 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000870 else
871 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000872 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000873 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000874}
875
David Majnemer44f51e52014-09-10 12:51:52 +0000876ArrayRef<uint8_t>
877COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000878 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000879
David Majnemer44f51e52014-09-10 12:51:52 +0000880 size_t SymbolSize = getSymbolTableEntrySize();
881 if (Symbol.getNumberOfAuxSymbols() > 0) {
882 // AUX data comes immediately after the symbol in COFF
883 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000884# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000885 // Verify that the Aux symbol points to a valid entry in the symbol table.
886 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000887 if (Offset < getPointerToSymbolTable() ||
888 Offset >=
889 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000890 report_fatal_error("Aux Symbol data was outside of symbol table.");
891
David Majnemer44f51e52014-09-10 12:51:52 +0000892 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
893 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000894# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000895 }
David Majnemer44f51e52014-09-10 12:51:52 +0000896 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000897}
898
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000899std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
900 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000901 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000902 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000903 // Null terminated, let ::strlen figure out the length.
904 Name = Sec->Name;
905 else
906 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000907 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000908
909 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000910 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000911 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000912 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000913 if (decodeBase64StringEntry(Name.substr(2), Offset))
914 return object_error::parse_failed;
915 } else {
916 if (Name.substr(1).getAsInteger(10, Offset))
917 return object_error::parse_failed;
918 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000919 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000920 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000921 }
922
923 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000924 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000925}
926
David Majnemera9ee5c02014-10-09 08:42:31 +0000927uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
928 // SizeOfRawData and VirtualSize change what they represent depending on
929 // whether or not we have an executable image.
930 //
931 // For object files, SizeOfRawData contains the size of section's data;
932 // VirtualSize is always zero.
933 //
934 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
935 // actual section size is in VirtualSize. It is possible for VirtualSize to
936 // be greater than SizeOfRawData; the contents past that point should be
937 // considered to be zero.
938 uint32_t SectionSize;
939 if (Sec->VirtualSize)
940 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
941 else
942 SectionSize = Sec->SizeOfRawData;
943
944 return SectionSize;
945}
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 Espindoladb4ed0b2014-06-13 02:24:39 +0000974std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
975 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000976 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000977}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000978
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000979std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
980 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000981 const coff_relocation *R = toRel(Rel);
982 const support::ulittle32_t *VirtualAddressPtr;
983 if (std::error_code EC =
984 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
985 return EC;
986 Res = *VirtualAddressPtr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000987 return std::error_code();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000988}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000989
Rafael Espindola806f0062013-06-05 01:33:53 +0000990symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000991 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000992 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000993 if (R->SymbolTableIndex >= getNumberOfSymbols())
994 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000995 if (SymbolTable16)
996 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
997 else if (SymbolTable32)
998 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
999 else
David Majnemerc7353b52014-11-25 07:43:14 +00001000 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001001 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001002}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001003
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001004std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1005 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001006 const coff_relocation* R = toRel(Rel);
1007 Res = R->Type;
Rui Ueyama7d099192015-06-09 15:20:42 +00001008 return std::error_code();
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001009}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001010
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001011const coff_section *
1012COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1013 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001014}
1015
David Majnemer44f51e52014-09-10 12:51:52 +00001016COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1017 if (SymbolTable16)
1018 return toSymb<coff_symbol16>(Ref);
1019 if (SymbolTable32)
1020 return toSymb<coff_symbol32>(Ref);
1021 llvm_unreachable("no symbol table pointer!");
1022}
1023
1024COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1025 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001026}
1027
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001028const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001029COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1030 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001031}
1032
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001033iterator_range<const coff_relocation *>
1034COFFObjectFile::getRelocations(const coff_section *Sec) const {
1035 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1036 const coff_relocation *E = I;
1037 if (I)
1038 E += getNumberOfRelocations(Sec, Data, base());
1039 return make_range(I, E);
1040}
1041
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001042#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1043 case COFF::reloc_type: \
1044 Res = #reloc_type; \
1045 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001046
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001047std::error_code
1048COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1049 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001050 const coff_relocation *Reloc = toRel(Rel);
1051 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001052 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001053 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001054 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1072 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001073 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001074 }
1075 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001076 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1077 switch (Reloc->Type) {
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1093 default:
1094 Res = "Unknown";
1095 }
1096 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001097 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001098 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001099 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1100 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1101 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1102 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1103 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1104 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1105 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1106 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1107 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1108 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1109 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1110 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001111 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001112 }
1113 break;
1114 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001115 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001116 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001117 Result.append(Res.begin(), Res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +00001118 return std::error_code();
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001119}
1120
1121#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1122
Rafael Espindolac66d7612014-08-17 19:09:37 +00001123bool COFFObjectFile::isRelocatableObject() const {
1124 return !DataDirectory;
1125}
1126
Rui Ueyamac2bed422013-09-27 21:04:00 +00001127bool ImportDirectoryEntryRef::
1128operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001129 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001130}
1131
Rafael Espindola5e812af2014-01-30 02:49:50 +00001132void ImportDirectoryEntryRef::moveNext() {
1133 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001134}
1135
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001136std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1137 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001138 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001139 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001140}
1141
Rui Ueyama861021f2014-10-02 22:05:29 +00001142static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001143makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001144 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001145 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001146 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001147 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001148 }
1149 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001150 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001151}
1152
Rui Ueyama15d99352014-10-03 00:41:58 +00001153static imported_symbol_iterator
1154importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001155 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001156 Object->getRvaPtr(RVA, IntPtr);
1157 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001158}
1159
Rui Ueyama15d99352014-10-03 00:41:58 +00001160static imported_symbol_iterator
1161importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001162 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001163 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001164 // Forward the pointer to the last entry which is null.
1165 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001166 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001167 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1168 while (*Entry++)
1169 ++Index;
1170 } else {
1171 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1172 while (*Entry++)
1173 ++Index;
1174 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001175 return makeImportedSymbolIterator(Object, IntPtr, Index);
1176}
1177
1178imported_symbol_iterator
1179ImportDirectoryEntryRef::imported_symbol_begin() const {
1180 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1181 OwningObject);
1182}
1183
1184imported_symbol_iterator
1185ImportDirectoryEntryRef::imported_symbol_end() const {
1186 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1187 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001188}
1189
Rui Ueyama979fb402014-10-09 02:16:38 +00001190iterator_range<imported_symbol_iterator>
1191ImportDirectoryEntryRef::imported_symbols() const {
1192 return make_range(imported_symbol_begin(), imported_symbol_end());
1193}
1194
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001195std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001196 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001197 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001198 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001199 return EC;
1200 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001201 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001202}
1203
Rui Ueyama1e152d52014-10-02 17:02:18 +00001204std::error_code
1205ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1206 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001207 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001208}
1209
1210std::error_code
1211ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1212 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001213 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001214}
1215
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001216std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001217 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001218 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001219 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1220 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001221 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001222 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001223 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001224}
1225
Rui Ueyama15d99352014-10-03 00:41:58 +00001226bool DelayImportDirectoryEntryRef::
1227operator==(const DelayImportDirectoryEntryRef &Other) const {
1228 return Table == Other.Table && Index == Other.Index;
1229}
1230
1231void DelayImportDirectoryEntryRef::moveNext() {
1232 ++Index;
1233}
1234
1235imported_symbol_iterator
1236DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1237 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1238 OwningObject);
1239}
1240
1241imported_symbol_iterator
1242DelayImportDirectoryEntryRef::imported_symbol_end() const {
1243 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1244 OwningObject);
1245}
1246
Rui Ueyama979fb402014-10-09 02:16:38 +00001247iterator_range<imported_symbol_iterator>
1248DelayImportDirectoryEntryRef::imported_symbols() const {
1249 return make_range(imported_symbol_begin(), imported_symbol_end());
1250}
1251
Rui Ueyama15d99352014-10-03 00:41:58 +00001252std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1253 uintptr_t IntPtr = 0;
1254 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1255 return EC;
1256 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001257 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001258}
1259
Rui Ueyama1af08652014-10-03 18:07:18 +00001260std::error_code DelayImportDirectoryEntryRef::
1261getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1262 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001263 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001264}
1265
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001266std::error_code DelayImportDirectoryEntryRef::
1267getImportAddress(int AddrIndex, uint64_t &Result) const {
1268 uint32_t RVA = Table[Index].DelayImportAddressTable +
1269 AddrIndex * (OwningObject->is64() ? 8 : 4);
1270 uintptr_t IntPtr = 0;
1271 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1272 return EC;
1273 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001274 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001275 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001276 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001277 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001278}
1279
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001280bool ExportDirectoryEntryRef::
1281operator==(const ExportDirectoryEntryRef &Other) const {
1282 return ExportTable == Other.ExportTable && Index == Other.Index;
1283}
1284
Rafael Espindola5e812af2014-01-30 02:49:50 +00001285void ExportDirectoryEntryRef::moveNext() {
1286 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001287}
1288
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001289// Returns the name of the current export symbol. If the symbol is exported only
1290// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001291std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001292 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001293 if (std::error_code EC =
1294 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001295 return EC;
1296 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001297 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001298}
1299
Rui Ueyamae5df6092014-01-17 22:02:24 +00001300// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001301std::error_code
1302ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001303 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001304 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001305}
1306
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001307// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001308std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001309 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001310 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001311}
1312
1313// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001314std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001315 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001316 if (std::error_code EC =
1317 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001318 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001319 const export_address_table_entry *entry =
1320 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001321 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001322 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001323}
1324
1325// Returns the name of the current export symbol. If the symbol is exported only
1326// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001327std::error_code
1328ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001329 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001330 if (std::error_code EC =
1331 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001332 return EC;
1333 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1334
1335 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1336 int Offset = 0;
1337 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1338 I < E; ++I, ++Offset) {
1339 if (*I != Index)
1340 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001341 if (std::error_code EC =
1342 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001343 return EC;
1344 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001345 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001346 return EC;
1347 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001348 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001349 }
1350 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001351 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001352}
1353
Rui Ueyama861021f2014-10-02 22:05:29 +00001354bool ImportedSymbolRef::
1355operator==(const ImportedSymbolRef &Other) const {
1356 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1357 && Index == Other.Index;
1358}
1359
1360void ImportedSymbolRef::moveNext() {
1361 ++Index;
1362}
1363
1364std::error_code
1365ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1366 uint32_t RVA;
1367 if (Entry32) {
1368 // If a symbol is imported only by ordinal, it has no name.
1369 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001370 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001371 RVA = Entry32[Index].getHintNameRVA();
1372 } else {
1373 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001374 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001375 RVA = Entry64[Index].getHintNameRVA();
1376 }
1377 uintptr_t IntPtr = 0;
1378 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1379 return EC;
1380 // +2 because the first two bytes is hint.
1381 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001382 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001383}
1384
1385std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1386 uint32_t RVA;
1387 if (Entry32) {
1388 if (Entry32[Index].isOrdinal()) {
1389 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001390 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001391 }
1392 RVA = Entry32[Index].getHintNameRVA();
1393 } else {
1394 if (Entry64[Index].isOrdinal()) {
1395 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001396 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001397 }
1398 RVA = Entry64[Index].getHintNameRVA();
1399 }
1400 uintptr_t IntPtr = 0;
1401 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1402 return EC;
1403 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001404 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001405}
1406
Rafael Espindola437b0d52014-07-31 03:12:45 +00001407ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001408ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001409 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001410 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001411 if (EC)
1412 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001413 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001414}
Rui Ueyama74e85132014-11-19 00:18:07 +00001415
1416bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1417 return Header == Other.Header && Index == Other.Index;
1418}
1419
1420void BaseRelocRef::moveNext() {
1421 // Header->BlockSize is the size of the current block, including the
1422 // size of the header itself.
1423 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001424 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001425 if (Size == Header->BlockSize) {
1426 // .reloc contains a list of base relocation blocks. Each block
1427 // consists of the header followed by entries. The header contains
1428 // how many entories will follow. When we reach the end of the
1429 // current block, proceed to the next block.
1430 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1431 reinterpret_cast<const uint8_t *>(Header) + Size);
1432 Index = 0;
1433 } else {
1434 ++Index;
1435 }
1436}
1437
1438std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1439 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1440 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001441 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001442}
1443
1444std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1445 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1446 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001447 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001448}