blob: 5b72e48b14e5da38cd7ab2e099e46794e3749f3c [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 Espindola2fa80cc2015-06-26 12:18:49 +0000180SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000181 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000182 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000183
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000184 if (Symb.isAnyUndefined())
185 return SymbolRef::ST_Unknown;
186 if (Symb.isFunctionDefinition())
187 return SymbolRef::ST_Function;
188 if (Symb.isCommon())
189 return SymbolRef::ST_Data;
190 if (Symb.isFileRecord())
191 return SymbolRef::ST_File;
192
193 // TODO: perhaps we need a new symbol type ST_Section.
194 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
195 return SymbolRef::ST_Debug;
196
197 if (!COFF::isReservedSectionNumber(SectionNumber))
198 return SymbolRef::ST_Data;
199
200 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000201}
202
Rafael Espindola20122a42014-01-31 20:57:12 +0000203uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000204 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000205 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000206
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000207 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000208 Result |= SymbolRef::SF_Global;
209
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000210 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000211 Result |= SymbolRef::SF_Weak;
212
David Majnemer44f51e52014-09-10 12:51:52 +0000213 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000214 Result |= SymbolRef::SF_Absolute;
215
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000216 if (Symb.isFileRecord())
217 Result |= SymbolRef::SF_FormatSpecific;
218
219 if (Symb.isSectionDefinition())
220 Result |= SymbolRef::SF_FormatSpecific;
221
222 if (Symb.isCommon())
223 Result |= SymbolRef::SF_Common;
224
225 if (Symb.isAnyUndefined())
226 Result |= SymbolRef::SF_Undefined;
227
Rafael Espindola20122a42014-01-31 20:57:12 +0000228 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000229}
230
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000231uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000232 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000233 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000234}
235
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000236std::error_code
237COFFObjectFile::getSymbolSection(DataRefImpl Ref,
238 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000239 COFFSymbolRef Symb = getCOFFSymbol(Ref);
240 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000241 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000242 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000243 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000244 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000245 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000246 DataRefImpl Ref;
247 Ref.p = reinterpret_cast<uintptr_t>(Sec);
248 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000249 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000250 return std::error_code();
Michael J. Spencer3217315392011-10-17 23:54:46 +0000251}
252
Rafael Espindola6bf32212015-06-24 19:57:32 +0000253unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
254 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
255 return Symb.getSectionNumber();
256}
257
Rafael Espindola5e812af2014-01-30 02:49:50 +0000258void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000259 const coff_section *Sec = toSec(Ref);
260 Sec += 1;
261 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000262}
263
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000264std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
265 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000266 const coff_section *Sec = toSec(Ref);
267 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000268}
269
Rafael Espindola80291272014-10-08 15:28:58 +0000270uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000271 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000272 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000273}
274
Rafael Espindola80291272014-10-08 15:28:58 +0000275uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000276 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000277}
278
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000279std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
280 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000281 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000282 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000283 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000284 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
285 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000286}
287
Rafael Espindola80291272014-10-08 15:28:58 +0000288uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000289 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000290 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000291}
292
Rafael Espindola80291272014-10-08 15:28:58 +0000293bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000294 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000295 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000296}
297
Rafael Espindola80291272014-10-08 15:28:58 +0000298bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000299 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000300 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000301}
302
Rafael Espindola80291272014-10-08 15:28:58 +0000303bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000304 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000305 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
306 COFF::IMAGE_SCN_MEM_READ |
307 COFF::IMAGE_SCN_MEM_WRITE;
308 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000309}
310
Rafael Espindola6bf32212015-06-24 19:57:32 +0000311unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
312 uintptr_t Offset =
313 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
314 assert((Offset % sizeof(coff_section)) == 0);
315 return (Offset / sizeof(coff_section)) + 1;
316}
317
Rafael Espindola80291272014-10-08 15:28:58 +0000318bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000319 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000320 // In COFF, a virtual section won't have any in-file
321 // content, so the file pointer to the content will be zero.
322 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000323}
324
Rafael Espindola80291272014-10-08 15:28:58 +0000325bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
326 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000327 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000328 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000329 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000330 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000331}
332
David Majnemere830c602014-11-13 08:46:37 +0000333static uint32_t getNumberOfRelocations(const coff_section *Sec,
334 MemoryBufferRef M, const uint8_t *base) {
335 // The field for the number of relocations in COFF section table is only
336 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
337 // NumberOfRelocations field, and the actual relocation count is stored in the
338 // VirtualAddress field in the first relocation entry.
339 if (Sec->hasExtendedRelocations()) {
340 const coff_relocation *FirstReloc;
341 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
342 base + Sec->PointerToRelocations)))
343 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000344 // -1 to exclude this first relocation entry.
345 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000346 }
347 return Sec->NumberOfRelocations;
348}
349
David Majnemer94751be2014-11-13 09:50:18 +0000350static const coff_relocation *
351getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
352 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
353 if (!NumRelocs)
354 return nullptr;
355 auto begin = reinterpret_cast<const coff_relocation *>(
356 Base + Sec->PointerToRelocations);
357 if (Sec->hasExtendedRelocations()) {
358 // Skip the first relocation entry repurposed to store the number of
359 // relocations.
360 begin++;
361 }
362 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
363 return nullptr;
364 return begin;
365}
366
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000367relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
368 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000369 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000370 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000371 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000372 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000373}
374
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
376 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000377 const coff_relocation *I = getFirstReloc(Sec, Data, base());
378 if (I)
379 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000380 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000381 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000382 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000383}
384
Rui Ueyamac2bed422013-09-27 21:04:00 +0000385// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000386std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000387 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000388 if (std::error_code EC = getObject(
389 SymbolTable16, Data, base() + getPointerToSymbolTable(),
390 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000391 return EC;
392
393 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000394 if (std::error_code EC = getObject(
395 SymbolTable32, Data, base() + getPointerToSymbolTable(),
396 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000397 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000398
399 // Find string table. The first four byte of the string table contains the
400 // total size of the string table, including the size field itself. If the
401 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000402 uint32_t StringTableOffset = getPointerToSymbolTable() +
403 getNumberOfSymbols() * getSymbolTableEntrySize();
404 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000405 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000406 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000407 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000409 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000410 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000411 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000412
Nico Rieck773a5792014-02-26 19:51:44 +0000413 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
414 // tools like cvtres write a size of 0 for an empty table instead of 4.
415 if (StringTableSize < 4)
416 StringTableSize = 4;
417
Rui Ueyamac2bed422013-09-27 21:04:00 +0000418 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000419 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000420 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000421 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000422}
423
Rui Ueyama215a5862014-02-20 06:51:07 +0000424// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000425std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000426 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
427 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000428 uint64_t Rva = Addr - ImageBase;
429 assert(Rva <= UINT32_MAX);
430 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000431}
432
Rui Ueyamac2bed422013-09-27 21:04:00 +0000433// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000434std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000435 for (const SectionRef &S : sections()) {
436 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437 uint32_t SectionStart = Section->VirtualAddress;
438 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000439 if (SectionStart <= Addr && Addr < SectionEnd) {
440 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000442 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000443 }
444 }
445 return object_error::parse_failed;
446}
447
448// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
449// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000450std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
451 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000452 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000453 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000454 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000455 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
456 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
457 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000458 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000459}
460
461// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000462std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000463 // First, we get the RVA of the import table. If the file lacks a pointer to
464 // the import table, do nothing.
465 const data_directory *DataEntry;
466 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000467 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468
469 // Do nothing if the pointer to import table is NULL.
470 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000471 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000472
473 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000474 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000475 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000476 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000477
478 // Find the section that contains the RVA. This is needed because the RVA is
479 // the import table's memory address which is different from its file offset.
480 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000481 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000482 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000483 ImportDirectory = reinterpret_cast<
484 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000485 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000486}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000487
Rui Ueyama15d99352014-10-03 00:41:58 +0000488// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
489std::error_code COFFObjectFile::initDelayImportTablePtr() {
490 const data_directory *DataEntry;
491 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000492 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000493 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000494 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000495
496 uint32_t RVA = DataEntry->RelativeVirtualAddress;
497 NumberOfDelayImportDirectory = DataEntry->Size /
498 sizeof(delay_import_directory_table_entry) - 1;
499
500 uintptr_t IntPtr = 0;
501 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
502 return EC;
503 DelayImportDirectory = reinterpret_cast<
504 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000505 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000506}
507
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000508// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000509std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000510 // First, we get the RVA of the export table. If the file lacks a pointer to
511 // the export table, do nothing.
512 const data_directory *DataEntry;
513 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000514 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000515
516 // Do nothing if the pointer to export table is NULL.
517 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000518 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000519
520 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
521 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000522 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000523 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000524 ExportDirectory =
525 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000526 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000527}
528
Rui Ueyama74e85132014-11-19 00:18:07 +0000529std::error_code COFFObjectFile::initBaseRelocPtr() {
530 const data_directory *DataEntry;
531 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000532 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000533 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000534 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000535
536 uintptr_t IntPtr = 0;
537 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
538 return EC;
539 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
540 IntPtr);
541 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
542 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000543 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000544}
545
Rafael Espindola48af1c22014-08-19 18:44:46 +0000546COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
547 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000548 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
549 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
550 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
551 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000552 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000553 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
554 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000555 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000556 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000557 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000558
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000559 // The current location in the file where we are looking at.
560 uint64_t CurPtr = 0;
561
562 // PE header is optional and is present only in executables. If it exists,
563 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000564 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000565
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000566 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000567 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000568 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
569 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000570 const auto *DH = reinterpret_cast<const dos_header *>(base());
571 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
572 CurPtr = DH->AddressOfNewExeHeader;
573 // Check the PE magic bytes. ("PE\0\0")
574 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
575 EC = object_error::parse_failed;
576 return;
577 }
578 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
579 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000580 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000581 }
582
Rafael Espindola48af1c22014-08-19 18:44:46 +0000583 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000584 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000585
586 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
587 // import libraries share a common prefix but bigobj is more restrictive.
588 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
589 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
590 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
591 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
592 return;
593
594 // Verify that we are dealing with bigobj.
595 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
596 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
597 sizeof(COFF::BigObjMagic)) == 0) {
598 COFFHeader = nullptr;
599 CurPtr += sizeof(coff_bigobj_file_header);
600 } else {
601 // It's not a bigobj.
602 COFFBigObjHeader = nullptr;
603 }
604 }
605 if (COFFHeader) {
606 // The prior checkSize call may have failed. This isn't a hard error
607 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000608 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000609 CurPtr += sizeof(coff_file_header);
610
611 if (COFFHeader->isImportLibrary())
612 return;
613 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000614
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000615 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000616 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000617 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000618 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000619
620 const uint8_t *DataDirAddr;
621 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000622 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000623 PE32Header = Header;
624 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
625 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000626 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000627 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
628 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
629 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
630 } else {
631 // It's neither PE32 nor PE32+.
632 EC = object_error::parse_failed;
633 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000634 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000635 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000636 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000637 CurPtr += COFFHeader->SizeOfOptionalHeader;
638 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000639
Rafael Espindola48af1c22014-08-19 18:44:46 +0000640 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000641 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000642 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000643
Rui Ueyamac2bed422013-09-27 21:04:00 +0000644 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000645 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000646 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000647 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000648 } else {
649 // We had better not have any symbols if we don't have a symbol table.
650 if (getNumberOfSymbols() != 0) {
651 EC = object_error::parse_failed;
652 return;
653 }
654 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000655
Rui Ueyamac2bed422013-09-27 21:04:00 +0000656 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000657 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000658 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000659 if ((EC = initDelayImportTablePtr()))
660 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000661
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000662 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000663 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000664 return;
665
Rui Ueyama74e85132014-11-19 00:18:07 +0000666 // Initialize the pointer to the base relocation table.
667 if ((EC = initBaseRelocPtr()))
668 return;
669
Rui Ueyama7d099192015-06-09 15:20:42 +0000670 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000671}
672
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000673basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000674 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000675 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000676 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000677}
678
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000679basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000680 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000681 DataRefImpl Ret;
682 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000683 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000684}
685
Rui Ueyamabc654b12013-09-27 21:47:05 +0000686import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000687 return import_directory_iterator(
688 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000689}
690
Rui Ueyamabc654b12013-09-27 21:47:05 +0000691import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000692 return import_directory_iterator(
693 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000694}
David Meyerc429b802012-03-01 22:19:54 +0000695
Rui Ueyama15d99352014-10-03 00:41:58 +0000696delay_import_directory_iterator
697COFFObjectFile::delay_import_directory_begin() const {
698 return delay_import_directory_iterator(
699 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
700}
701
702delay_import_directory_iterator
703COFFObjectFile::delay_import_directory_end() const {
704 return delay_import_directory_iterator(
705 DelayImportDirectoryEntryRef(
706 DelayImportDirectory, NumberOfDelayImportDirectory, this));
707}
708
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000709export_directory_iterator COFFObjectFile::export_directory_begin() const {
710 return export_directory_iterator(
711 ExportDirectoryEntryRef(ExportDirectory, 0, this));
712}
713
714export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000715 if (!ExportDirectory)
716 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000717 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000718 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000719 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000720}
721
Rafael Espindolab5155a52014-02-10 20:24:04 +0000722section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000723 DataRefImpl Ret;
724 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
725 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000726}
727
Rafael Espindolab5155a52014-02-10 20:24:04 +0000728section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000729 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000730 int NumSections =
731 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000732 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
733 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000734}
735
Rui Ueyama74e85132014-11-19 00:18:07 +0000736base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
737 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
738}
739
740base_reloc_iterator COFFObjectFile::base_reloc_end() const {
741 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
742}
743
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000744uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000745 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000746}
747
748StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000749 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000750 case COFF::IMAGE_FILE_MACHINE_I386:
751 return "COFF-i386";
752 case COFF::IMAGE_FILE_MACHINE_AMD64:
753 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000754 case COFF::IMAGE_FILE_MACHINE_ARMNT:
755 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000756 default:
757 return "COFF-<unknown arch>";
758 }
759}
760
761unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000762 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000763 case COFF::IMAGE_FILE_MACHINE_I386:
764 return Triple::x86;
765 case COFF::IMAGE_FILE_MACHINE_AMD64:
766 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000767 case COFF::IMAGE_FILE_MACHINE_ARMNT:
768 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000769 default:
770 return Triple::UnknownArch;
771 }
772}
773
Rui Ueyama979fb402014-10-09 02:16:38 +0000774iterator_range<import_directory_iterator>
775COFFObjectFile::import_directories() const {
776 return make_range(import_directory_begin(), import_directory_end());
777}
778
779iterator_range<delay_import_directory_iterator>
780COFFObjectFile::delay_import_directories() const {
781 return make_range(delay_import_directory_begin(),
782 delay_import_directory_end());
783}
784
785iterator_range<export_directory_iterator>
786COFFObjectFile::export_directories() const {
787 return make_range(export_directory_begin(), export_directory_end());
788}
789
Rui Ueyama74e85132014-11-19 00:18:07 +0000790iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
791 return make_range(base_reloc_begin(), base_reloc_end());
792}
793
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000794std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000795 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000796 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000797}
798
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000799std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000800COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
801 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000802 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000803}
804
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000805std::error_code
806COFFObjectFile::getDataDirectory(uint32_t Index,
807 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000808 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000809 if (!DataDirectory) {
810 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000811 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000812 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000813 assert(PE32Header || PE32PlusHeader);
814 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
815 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000816 if (Index >= NumEnt) {
817 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000818 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000819 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000820 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000821 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000822}
823
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000824std::error_code COFFObjectFile::getSection(int32_t Index,
825 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000826 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000827 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000828 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000829 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000830 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000831 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000832 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000833 }
834 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000835}
836
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000837std::error_code COFFObjectFile::getString(uint32_t Offset,
838 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000839 if (StringTableSize <= 4)
840 // Tried to get a string from an empty string table.
841 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000842 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000843 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000844 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000845 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000846}
847
David Majnemer44f51e52014-09-10 12:51:52 +0000848std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000849 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000850 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000851 if (Symbol.getStringTableOffset().Zeroes == 0) {
852 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000853 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000854 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000855 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000856 }
857
David Majnemer44f51e52014-09-10 12:51:52 +0000858 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000859 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000860 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000861 else
862 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000863 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
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 +0000867ArrayRef<uint8_t>
868COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000869 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000870
David Majnemer44f51e52014-09-10 12:51:52 +0000871 size_t SymbolSize = getSymbolTableEntrySize();
872 if (Symbol.getNumberOfAuxSymbols() > 0) {
873 // AUX data comes immediately after the symbol in COFF
874 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000875# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000876 // Verify that the Aux symbol points to a valid entry in the symbol table.
877 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000878 if (Offset < getPointerToSymbolTable() ||
879 Offset >=
880 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000881 report_fatal_error("Aux Symbol data was outside of symbol table.");
882
David Majnemer44f51e52014-09-10 12:51:52 +0000883 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
884 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000885# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000886 }
David Majnemer44f51e52014-09-10 12:51:52 +0000887 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000888}
889
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000890std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
891 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000892 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000893 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000894 // Null terminated, let ::strlen figure out the length.
895 Name = Sec->Name;
896 else
897 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000898 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000899
900 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000901 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000902 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000903 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000904 if (decodeBase64StringEntry(Name.substr(2), Offset))
905 return object_error::parse_failed;
906 } else {
907 if (Name.substr(1).getAsInteger(10, Offset))
908 return object_error::parse_failed;
909 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000910 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000911 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000912 }
913
914 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000915 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000916}
917
David Majnemera9ee5c02014-10-09 08:42:31 +0000918uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
919 // SizeOfRawData and VirtualSize change what they represent depending on
920 // whether or not we have an executable image.
921 //
922 // For object files, SizeOfRawData contains the size of section's data;
923 // VirtualSize is always zero.
924 //
925 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
926 // actual section size is in VirtualSize. It is possible for VirtualSize to
927 // be greater than SizeOfRawData; the contents past that point should be
928 // considered to be zero.
929 uint32_t SectionSize;
930 if (Sec->VirtualSize)
931 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
932 else
933 SectionSize = Sec->SizeOfRawData;
934
935 return SectionSize;
936}
937
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000938std::error_code
939COFFObjectFile::getSectionContents(const coff_section *Sec,
940 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000941 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
942 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000943 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
944 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000945 // The only thing that we need to verify is that the contents is contained
946 // within the file bounds. We don't need to make sure it doesn't cover other
947 // data, as there's nothing that says that is not allowed.
948 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000949 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000950 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000951 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000952 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000953 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000954}
955
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000956const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000957 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000958}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000959
Rafael Espindola5e812af2014-01-30 02:49:50 +0000960void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000961 Rel.p = reinterpret_cast<uintptr_t>(
962 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000963}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000964
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000965std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
966 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000967 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000968}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000969
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000970std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
971 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000972 const coff_relocation *R = toRel(Rel);
973 const support::ulittle32_t *VirtualAddressPtr;
974 if (std::error_code EC =
975 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
976 return EC;
977 Res = *VirtualAddressPtr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000978 return std::error_code();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000979}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000980
Rafael Espindola806f0062013-06-05 01:33:53 +0000981symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000982 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000983 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000984 if (R->SymbolTableIndex >= getNumberOfSymbols())
985 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000986 if (SymbolTable16)
987 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
988 else if (SymbolTable32)
989 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
990 else
David Majnemerc7353b52014-11-25 07:43:14 +0000991 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000992 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000993}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000994
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000995std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
996 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000997 const coff_relocation* R = toRel(Rel);
998 Res = R->Type;
Rui Ueyama7d099192015-06-09 15:20:42 +0000999 return std::error_code();
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001000}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001001
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001002const coff_section *
1003COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1004 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001005}
1006
David Majnemer44f51e52014-09-10 12:51:52 +00001007COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1008 if (SymbolTable16)
1009 return toSymb<coff_symbol16>(Ref);
1010 if (SymbolTable32)
1011 return toSymb<coff_symbol32>(Ref);
1012 llvm_unreachable("no symbol table pointer!");
1013}
1014
1015COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1016 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001017}
1018
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001019const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001020COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1021 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001022}
1023
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001024iterator_range<const coff_relocation *>
1025COFFObjectFile::getRelocations(const coff_section *Sec) const {
1026 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1027 const coff_relocation *E = I;
1028 if (I)
1029 E += getNumberOfRelocations(Sec, Data, base());
1030 return make_range(I, E);
1031}
1032
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001033#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1034 case COFF::reloc_type: \
1035 Res = #reloc_type; \
1036 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001037
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001038std::error_code
1039COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1040 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001041 const coff_relocation *Reloc = toRel(Rel);
1042 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001043 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001044 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001045 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1063 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001064 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001065 }
1066 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001067 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1068 switch (Reloc->Type) {
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1084 default:
1085 Res = "Unknown";
1086 }
1087 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001088 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001089 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1096 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1098 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1099 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1100 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1101 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001102 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001103 }
1104 break;
1105 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001106 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001107 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001108 Result.append(Res.begin(), Res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +00001109 return std::error_code();
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001110}
1111
1112#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1113
Rafael Espindolac66d7612014-08-17 19:09:37 +00001114bool COFFObjectFile::isRelocatableObject() const {
1115 return !DataDirectory;
1116}
1117
Rui Ueyamac2bed422013-09-27 21:04:00 +00001118bool ImportDirectoryEntryRef::
1119operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001120 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001121}
1122
Rafael Espindola5e812af2014-01-30 02:49:50 +00001123void ImportDirectoryEntryRef::moveNext() {
1124 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001125}
1126
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001127std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1128 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001129 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001130 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001131}
1132
Rui Ueyama861021f2014-10-02 22:05:29 +00001133static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001134makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001135 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001136 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001137 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001138 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001139 }
1140 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001141 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001142}
1143
Rui Ueyama15d99352014-10-03 00:41:58 +00001144static imported_symbol_iterator
1145importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001146 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001147 Object->getRvaPtr(RVA, IntPtr);
1148 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001149}
1150
Rui Ueyama15d99352014-10-03 00:41:58 +00001151static imported_symbol_iterator
1152importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001153 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001154 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001155 // Forward the pointer to the last entry which is null.
1156 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001157 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001158 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1159 while (*Entry++)
1160 ++Index;
1161 } else {
1162 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1163 while (*Entry++)
1164 ++Index;
1165 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001166 return makeImportedSymbolIterator(Object, IntPtr, Index);
1167}
1168
1169imported_symbol_iterator
1170ImportDirectoryEntryRef::imported_symbol_begin() const {
1171 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1172 OwningObject);
1173}
1174
1175imported_symbol_iterator
1176ImportDirectoryEntryRef::imported_symbol_end() const {
1177 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1178 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001179}
1180
Rui Ueyama979fb402014-10-09 02:16:38 +00001181iterator_range<imported_symbol_iterator>
1182ImportDirectoryEntryRef::imported_symbols() const {
1183 return make_range(imported_symbol_begin(), imported_symbol_end());
1184}
1185
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001186std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001187 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001188 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001189 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001190 return EC;
1191 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001192 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001193}
1194
Rui Ueyama1e152d52014-10-02 17:02:18 +00001195std::error_code
1196ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1197 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001198 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001199}
1200
1201std::error_code
1202ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1203 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001204 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001205}
1206
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001207std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001208 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001209 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001210 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1211 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001212 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001213 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001214 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001215}
1216
Rui Ueyama15d99352014-10-03 00:41:58 +00001217bool DelayImportDirectoryEntryRef::
1218operator==(const DelayImportDirectoryEntryRef &Other) const {
1219 return Table == Other.Table && Index == Other.Index;
1220}
1221
1222void DelayImportDirectoryEntryRef::moveNext() {
1223 ++Index;
1224}
1225
1226imported_symbol_iterator
1227DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1228 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1229 OwningObject);
1230}
1231
1232imported_symbol_iterator
1233DelayImportDirectoryEntryRef::imported_symbol_end() const {
1234 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1235 OwningObject);
1236}
1237
Rui Ueyama979fb402014-10-09 02:16:38 +00001238iterator_range<imported_symbol_iterator>
1239DelayImportDirectoryEntryRef::imported_symbols() const {
1240 return make_range(imported_symbol_begin(), imported_symbol_end());
1241}
1242
Rui Ueyama15d99352014-10-03 00:41:58 +00001243std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1244 uintptr_t IntPtr = 0;
1245 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1246 return EC;
1247 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001248 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001249}
1250
Rui Ueyama1af08652014-10-03 18:07:18 +00001251std::error_code DelayImportDirectoryEntryRef::
1252getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1253 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001254 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001255}
1256
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001257std::error_code DelayImportDirectoryEntryRef::
1258getImportAddress(int AddrIndex, uint64_t &Result) const {
1259 uint32_t RVA = Table[Index].DelayImportAddressTable +
1260 AddrIndex * (OwningObject->is64() ? 8 : 4);
1261 uintptr_t IntPtr = 0;
1262 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1263 return EC;
1264 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001265 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001266 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001267 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001268 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001269}
1270
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001271bool ExportDirectoryEntryRef::
1272operator==(const ExportDirectoryEntryRef &Other) const {
1273 return ExportTable == Other.ExportTable && Index == Other.Index;
1274}
1275
Rafael Espindola5e812af2014-01-30 02:49:50 +00001276void ExportDirectoryEntryRef::moveNext() {
1277 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001278}
1279
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001280// Returns the name of the current export symbol. If the symbol is exported only
1281// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001282std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001283 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001284 if (std::error_code EC =
1285 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001286 return EC;
1287 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001288 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001289}
1290
Rui Ueyamae5df6092014-01-17 22:02:24 +00001291// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001292std::error_code
1293ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001294 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001295 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001296}
1297
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001298// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001299std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001300 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001301 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001302}
1303
1304// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001305std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001306 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001307 if (std::error_code EC =
1308 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001309 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001310 const export_address_table_entry *entry =
1311 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001312 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001313 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001314}
1315
1316// Returns the name of the current export symbol. If the symbol is exported only
1317// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001318std::error_code
1319ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001320 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001321 if (std::error_code EC =
1322 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001323 return EC;
1324 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1325
1326 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1327 int Offset = 0;
1328 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1329 I < E; ++I, ++Offset) {
1330 if (*I != Index)
1331 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001332 if (std::error_code EC =
1333 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001334 return EC;
1335 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001336 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001337 return EC;
1338 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001339 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001340 }
1341 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001342 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001343}
1344
Rui Ueyama861021f2014-10-02 22:05:29 +00001345bool ImportedSymbolRef::
1346operator==(const ImportedSymbolRef &Other) const {
1347 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1348 && Index == Other.Index;
1349}
1350
1351void ImportedSymbolRef::moveNext() {
1352 ++Index;
1353}
1354
1355std::error_code
1356ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1357 uint32_t RVA;
1358 if (Entry32) {
1359 // If a symbol is imported only by ordinal, it has no name.
1360 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001361 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001362 RVA = Entry32[Index].getHintNameRVA();
1363 } else {
1364 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001365 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001366 RVA = Entry64[Index].getHintNameRVA();
1367 }
1368 uintptr_t IntPtr = 0;
1369 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1370 return EC;
1371 // +2 because the first two bytes is hint.
1372 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001373 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001374}
1375
1376std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1377 uint32_t RVA;
1378 if (Entry32) {
1379 if (Entry32[Index].isOrdinal()) {
1380 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001381 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001382 }
1383 RVA = Entry32[Index].getHintNameRVA();
1384 } else {
1385 if (Entry64[Index].isOrdinal()) {
1386 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001387 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001388 }
1389 RVA = Entry64[Index].getHintNameRVA();
1390 }
1391 uintptr_t IntPtr = 0;
1392 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1393 return EC;
1394 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001395 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001396}
1397
Rafael Espindola437b0d52014-07-31 03:12:45 +00001398ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001399ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001400 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001401 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001402 if (EC)
1403 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001404 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001405}
Rui Ueyama74e85132014-11-19 00:18:07 +00001406
1407bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1408 return Header == Other.Header && Index == Other.Index;
1409}
1410
1411void BaseRelocRef::moveNext() {
1412 // Header->BlockSize is the size of the current block, including the
1413 // size of the header itself.
1414 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001415 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001416 if (Size == Header->BlockSize) {
1417 // .reloc contains a list of base relocation blocks. Each block
1418 // consists of the header followed by entries. The header contains
1419 // how many entories will follow. When we reach the end of the
1420 // current block, proceed to the next block.
1421 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1422 reinterpret_cast<const uint8_t *>(Header) + Size);
1423 Index = 0;
1424 } else {
1425 ++Index;
1426 }
1427}
1428
1429std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1430 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1431 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001432 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001433}
1434
1435std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1436 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1437 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001438 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001439}