blob: f98ca4c90e37f6dca35e405953327a42fb3f2b3c [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 Ueyamaf078eff2014-03-18 23:37:53 +000019#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000022#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000023#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000024
25using namespace llvm;
26using namespace object;
27
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000028using support::ulittle16_t;
29using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000030using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000031using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000033// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000034static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000035 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000036 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000037 return false;
38 }
39 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000040}
41
David Majnemere830c602014-11-13 08:46:37 +000042static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
David Majnemer94751be2014-11-13 09:50:18 +000043 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000044 if (Addr + Size < Addr || Addr + Size < Size ||
45 Addr + Size > uintptr_t(M.getBufferEnd()) ||
46 Addr < uintptr_t(M.getBufferStart())) {
47 return object_error::unexpected_eof;
48 }
49 return object_error::success;
50}
51
Rui Ueyamaed64342b2013-07-19 23:23:29 +000052// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
53// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000054template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000055static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000056 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000057 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000058 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000059 if (std::error_code EC = checkOffset(M, Addr, Size))
60 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000061 Obj = reinterpret_cast<const T *>(Addr);
62 return object_error::success;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000063}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064
Nico Rieck9d2c15e2014-02-22 16:12:20 +000065// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
66// prefixed slashes.
67static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
68 assert(Str.size() <= 6 && "String too long, possible overflow.");
69 if (Str.size() > 6)
70 return true;
71
72 uint64_t Value = 0;
73 while (!Str.empty()) {
74 unsigned CharVal;
75 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
76 CharVal = Str[0] - 'A';
77 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
78 CharVal = Str[0] - 'a' + 26;
79 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
80 CharVal = Str[0] - '0' + 52;
81 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000082 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000083 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000084 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000085 else
86 return true;
87
88 Value = (Value * 64) + CharVal;
89 Str = Str.substr(1);
90 }
91
92 if (Value > std::numeric_limits<uint32_t>::max())
93 return true;
94
95 Result = static_cast<uint32_t>(Value);
96 return false;
97}
98
David Majnemer44f51e52014-09-10 12:51:52 +000099template <typename coff_symbol_type>
100const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
101 const coff_symbol_type *Addr =
102 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103
David Majnemer236b0ca2014-11-17 11:17:17 +0000104 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000105#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000106 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000107 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000108
David Majnemer44f51e52014-09-10 12:51:52 +0000109 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
110 "Symbol did not point to the beginning of a symbol");
111#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000112
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000113 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000114}
115
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000116const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
117 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000118
119# ifndef NDEBUG
120 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000121 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000122 report_fatal_error("Section was outside of section table.");
123
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000124 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
125 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000126 "Section did not point to the beginning of a section");
127# endif
128
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000129 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000130}
131
Rafael Espindola5e812af2014-01-30 02:49:50 +0000132void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000133 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000134 if (SymbolTable16) {
135 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
136 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000137 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000138 } else if (SymbolTable32) {
139 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
140 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000141 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000142 } else {
143 llvm_unreachable("no symbol table pointer!");
144 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000145}
146
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000147std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
148 StringRef &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000150 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000151}
152
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000153std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
154 uint64_t &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000155 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolae62ab112013-11-02 18:07:48 +0000156
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000157 if (Symb.isAnyUndefined()) {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000158 Result = UnknownAddressOrSize;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000159 return object_error::success;
160 }
161 if (Symb.isCommon()) {
162 Result = UnknownAddressOrSize;
163 return object_error::success;
164 }
165 int32_t SectionNumber = Symb.getSectionNumber();
166 if (!COFF::isReservedSectionNumber(SectionNumber)) {
167 const coff_section *Section = nullptr;
168 if (std::error_code EC = getSection(SectionNumber, Section))
169 return EC;
170
David Majnemer44f51e52014-09-10 12:51:52 +0000171 Result = Section->VirtualAddress + Symb.getValue();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000172 return object_error::success;
173 }
174
175 Result = Symb.getValue();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000176 return object_error::success;
177}
178
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000179std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
180 SymbolRef::Type &Result) 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();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000183 Result = SymbolRef::ST_Other;
David Majnemer44f51e52014-09-10 12:51:52 +0000184
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000185 if (Symb.isAnyUndefined()) {
David Meyer7e4b9762012-02-29 02:11:55 +0000186 Result = SymbolRef::ST_Unknown;
David Majnemer44f51e52014-09-10 12:51:52 +0000187 } else if (Symb.isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000188 Result = SymbolRef::ST_Function;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000189 } else if (Symb.isCommon()) {
190 Result = SymbolRef::ST_Data;
191 } else if (Symb.isFileRecord()) {
192 Result = SymbolRef::ST_File;
David Majnemer1a666e02015-03-07 20:21:27 +0000193 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG ||
194 Symb.isSectionDefinition()) {
195 // TODO: perhaps we need a new symbol type ST_Section.
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000196 Result = SymbolRef::ST_Debug;
197 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
198 const coff_section *Section = nullptr;
199 if (std::error_code EC = getSection(SectionNumber, Section))
200 return EC;
201 uint32_t Characteristics = Section->Characteristics;
202 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
203 Result = SymbolRef::ST_Function;
204 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
205 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
Rui Ueyama5efa6652014-01-16 20:22:55 +0000206 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000207 }
208 return object_error::success;
209}
210
Rafael Espindola20122a42014-01-31 20:57:12 +0000211uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000212 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000213 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000214
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000215 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000216 Result |= SymbolRef::SF_Global;
217
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000218 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000219 Result |= SymbolRef::SF_Weak;
220
David Majnemer44f51e52014-09-10 12:51:52 +0000221 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000222 Result |= SymbolRef::SF_Absolute;
223
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000224 if (Symb.isFileRecord())
225 Result |= SymbolRef::SF_FormatSpecific;
226
227 if (Symb.isSectionDefinition())
228 Result |= SymbolRef::SF_FormatSpecific;
229
230 if (Symb.isCommon())
231 Result |= SymbolRef::SF_Common;
232
233 if (Symb.isAnyUndefined())
234 Result |= SymbolRef::SF_Undefined;
235
Rafael Espindola20122a42014-01-31 20:57:12 +0000236 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000237}
238
Rafael Espindola5eb02e42015-06-01 00:27:26 +0000239uint64_t COFFObjectFile::getSymbolSize(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000240 COFFSymbolRef Symb = getCOFFSymbol(Ref);
241
Rafael Espindola62a07cb2015-05-22 15:43:00 +0000242 if (Symb.isCommon())
Rafael Espindola5eb02e42015-06-01 00:27:26 +0000243 return Symb.getValue();
244 return UnknownAddressOrSize;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000245}
246
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000247std::error_code
248COFFObjectFile::getSymbolSection(DataRefImpl Ref,
249 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000250 COFFSymbolRef Symb = getCOFFSymbol(Ref);
251 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000252 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000253 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000254 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000255 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000256 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000257 DataRefImpl Ref;
258 Ref.p = reinterpret_cast<uintptr_t>(Sec);
259 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000260 }
261 return object_error::success;
262}
263
Rafael Espindola5e812af2014-01-30 02:49:50 +0000264void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000265 const coff_section *Sec = toSec(Ref);
266 Sec += 1;
267 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000268}
269
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000270std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
271 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000272 const coff_section *Sec = toSec(Ref);
273 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000274}
275
Rafael Espindola80291272014-10-08 15:28:58 +0000276uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000277 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000278 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000279}
280
Rafael Espindola80291272014-10-08 15:28:58 +0000281uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000282 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000283}
284
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000285std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
286 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000287 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000288 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000289 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000290 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
291 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000292}
293
Rafael Espindola80291272014-10-08 15:28:58 +0000294uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000295 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000296 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000297}
298
Rafael Espindola80291272014-10-08 15:28:58 +0000299bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000300 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000301 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000302}
303
Rafael Espindola80291272014-10-08 15:28:58 +0000304bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000305 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000306 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000307}
308
Rafael Espindola80291272014-10-08 15:28:58 +0000309bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000310 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000311 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
312 COFF::IMAGE_SCN_MEM_READ |
313 COFF::IMAGE_SCN_MEM_WRITE;
314 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000315}
316
Rafael Espindola80291272014-10-08 15:28:58 +0000317bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000318 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000319 // In COFF, a virtual section won't have any in-file
320 // content, so the file pointer to the content will be zero.
321 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000322}
323
Rafael Espindola80291272014-10-08 15:28:58 +0000324bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
325 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000326 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000327 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000328 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000329 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000330}
331
David Majnemere830c602014-11-13 08:46:37 +0000332static uint32_t getNumberOfRelocations(const coff_section *Sec,
333 MemoryBufferRef M, const uint8_t *base) {
334 // The field for the number of relocations in COFF section table is only
335 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
336 // NumberOfRelocations field, and the actual relocation count is stored in the
337 // VirtualAddress field in the first relocation entry.
338 if (Sec->hasExtendedRelocations()) {
339 const coff_relocation *FirstReloc;
340 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
341 base + Sec->PointerToRelocations)))
342 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000343 // -1 to exclude this first relocation entry.
344 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000345 }
346 return Sec->NumberOfRelocations;
347}
348
David Majnemer94751be2014-11-13 09:50:18 +0000349static const coff_relocation *
350getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
351 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
352 if (!NumRelocs)
353 return nullptr;
354 auto begin = reinterpret_cast<const coff_relocation *>(
355 Base + Sec->PointerToRelocations);
356 if (Sec->hasExtendedRelocations()) {
357 // Skip the first relocation entry repurposed to store the number of
358 // relocations.
359 begin++;
360 }
361 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
362 return nullptr;
363 return begin;
364}
365
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000366relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
367 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000368 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000369 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000370 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000371 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000372}
373
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000374relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
375 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000376 const coff_relocation *I = getFirstReloc(Sec, Data, base());
377 if (I)
378 I += getNumberOfRelocations(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>(I);
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 Ueyamac2bed422013-09-27 21:04:00 +0000384// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000385std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000386 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000387 if (std::error_code EC = getObject(
388 SymbolTable16, Data, base() + getPointerToSymbolTable(),
389 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000390 return EC;
391
392 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000393 if (std::error_code EC = getObject(
394 SymbolTable32, Data, base() + getPointerToSymbolTable(),
395 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000396 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000397
398 // Find string table. The first four byte of the string table contains the
399 // total size of the string table, including the size field itself. If the
400 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000401 uint32_t StringTableOffset = getPointerToSymbolTable() +
402 getNumberOfSymbols() * getSymbolTableEntrySize();
403 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000404 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000405 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000406 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000407 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000408 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000409 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000410 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000411
Nico Rieck773a5792014-02-26 19:51:44 +0000412 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
413 // tools like cvtres write a size of 0 for an empty table instead of 4.
414 if (StringTableSize < 4)
415 StringTableSize = 4;
416
Rui Ueyamac2bed422013-09-27 21:04:00 +0000417 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000418 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000419 return object_error::parse_failed;
420 return object_error::success;
421}
422
Rui Ueyama215a5862014-02-20 06:51:07 +0000423// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000424std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000425 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
426 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000427 uint64_t Rva = Addr - ImageBase;
428 assert(Rva <= UINT32_MAX);
429 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000430}
431
Rui Ueyamac2bed422013-09-27 21:04:00 +0000432// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000433std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000434 for (const SectionRef &S : sections()) {
435 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000436 uint32_t SectionStart = Section->VirtualAddress;
437 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000438 if (SectionStart <= Addr && Addr < SectionEnd) {
439 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000440 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
441 return object_error::success;
442 }
443 }
444 return object_error::parse_failed;
445}
446
447// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
448// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000449std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
450 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000451 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000452 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000453 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000454 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
455 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
456 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
457 return object_error::success;
458}
459
460// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000461std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462 // First, we get the RVA of the import table. If the file lacks a pointer to
463 // the import table, do nothing.
464 const data_directory *DataEntry;
465 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
466 return object_error::success;
467
468 // Do nothing if the pointer to import table is NULL.
469 if (DataEntry->RelativeVirtualAddress == 0)
470 return object_error::success;
471
472 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000473 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000474 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000475 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000476
477 // Find the section that contains the RVA. This is needed because the RVA is
478 // the import table's memory address which is different from its file offset.
479 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000480 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000481 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000482 ImportDirectory = reinterpret_cast<
483 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000484 return object_error::success;
485}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000486
Rui Ueyama15d99352014-10-03 00:41:58 +0000487// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
488std::error_code COFFObjectFile::initDelayImportTablePtr() {
489 const data_directory *DataEntry;
490 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
491 return object_error::success;
492 if (DataEntry->RelativeVirtualAddress == 0)
493 return object_error::success;
494
495 uint32_t RVA = DataEntry->RelativeVirtualAddress;
496 NumberOfDelayImportDirectory = DataEntry->Size /
497 sizeof(delay_import_directory_table_entry) - 1;
498
499 uintptr_t IntPtr = 0;
500 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
501 return EC;
502 DelayImportDirectory = reinterpret_cast<
503 const delay_import_directory_table_entry *>(IntPtr);
504 return object_error::success;
505}
506
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000507// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000508std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000509 // First, we get the RVA of the export table. If the file lacks a pointer to
510 // the export table, do nothing.
511 const data_directory *DataEntry;
512 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
513 return object_error::success;
514
515 // Do nothing if the pointer to export table is NULL.
516 if (DataEntry->RelativeVirtualAddress == 0)
517 return object_error::success;
518
519 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
520 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000521 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000522 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000523 ExportDirectory =
524 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000525 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000526}
527
Rui Ueyama74e85132014-11-19 00:18:07 +0000528std::error_code COFFObjectFile::initBaseRelocPtr() {
529 const data_directory *DataEntry;
530 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
531 return object_error::success;
532 if (DataEntry->RelativeVirtualAddress == 0)
533 return object_error::success;
534
535 uintptr_t IntPtr = 0;
536 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
537 return EC;
538 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
539 IntPtr);
540 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
541 IntPtr + DataEntry->Size);
542 return object_error::success;
543}
544
Rafael Espindola48af1c22014-08-19 18:44:46 +0000545COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
546 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000547 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
548 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
549 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
550 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000551 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000552 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
553 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000554 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000555 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000556 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000557
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000558 // The current location in the file where we are looking at.
559 uint64_t CurPtr = 0;
560
561 // PE header is optional and is present only in executables. If it exists,
562 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000563 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000564
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000565 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000566 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000567 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
568 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000569 const auto *DH = reinterpret_cast<const dos_header *>(base());
570 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
571 CurPtr = DH->AddressOfNewExeHeader;
572 // Check the PE magic bytes. ("PE\0\0")
573 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
574 EC = object_error::parse_failed;
575 return;
576 }
577 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
578 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000579 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000580 }
581
Rafael Espindola48af1c22014-08-19 18:44:46 +0000582 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000583 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000584
585 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
586 // import libraries share a common prefix but bigobj is more restrictive.
587 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
588 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
589 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
590 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
591 return;
592
593 // Verify that we are dealing with bigobj.
594 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
595 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
596 sizeof(COFF::BigObjMagic)) == 0) {
597 COFFHeader = nullptr;
598 CurPtr += sizeof(coff_bigobj_file_header);
599 } else {
600 // It's not a bigobj.
601 COFFBigObjHeader = nullptr;
602 }
603 }
604 if (COFFHeader) {
605 // The prior checkSize call may have failed. This isn't a hard error
606 // because we were just trying to sniff out bigobj.
607 EC = object_error::success;
608 CurPtr += sizeof(coff_file_header);
609
610 if (COFFHeader->isImportLibrary())
611 return;
612 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000613
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000614 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000615 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000616 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000617 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000618
619 const uint8_t *DataDirAddr;
620 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000621 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000622 PE32Header = Header;
623 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
624 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000625 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000626 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
627 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
628 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
629 } else {
630 // It's neither PE32 nor PE32+.
631 EC = object_error::parse_failed;
632 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000633 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000634 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000635 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000636 CurPtr += COFFHeader->SizeOfOptionalHeader;
637 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000638
Rafael Espindola48af1c22014-08-19 18:44:46 +0000639 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000640 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000641 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000642
Rui Ueyamac2bed422013-09-27 21:04:00 +0000643 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000644 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000645 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000646 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000647 } else {
648 // We had better not have any symbols if we don't have a symbol table.
649 if (getNumberOfSymbols() != 0) {
650 EC = object_error::parse_failed;
651 return;
652 }
653 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000654
Rui Ueyamac2bed422013-09-27 21:04:00 +0000655 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000656 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000657 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000658 if ((EC = initDelayImportTablePtr()))
659 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000660
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000661 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000662 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000663 return;
664
Rui Ueyama74e85132014-11-19 00:18:07 +0000665 // Initialize the pointer to the base relocation table.
666 if ((EC = initBaseRelocPtr()))
667 return;
668
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000669 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000670}
671
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000672basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000673 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000674 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000675 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000676}
677
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000678basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000679 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000680 DataRefImpl Ret;
681 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000682 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000683}
684
Rui Ueyamabc654b12013-09-27 21:47:05 +0000685import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000686 return import_directory_iterator(
687 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000688}
689
Rui Ueyamabc654b12013-09-27 21:47:05 +0000690import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000691 return import_directory_iterator(
692 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000693}
David Meyerc429b802012-03-01 22:19:54 +0000694
Rui Ueyama15d99352014-10-03 00:41:58 +0000695delay_import_directory_iterator
696COFFObjectFile::delay_import_directory_begin() const {
697 return delay_import_directory_iterator(
698 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
699}
700
701delay_import_directory_iterator
702COFFObjectFile::delay_import_directory_end() const {
703 return delay_import_directory_iterator(
704 DelayImportDirectoryEntryRef(
705 DelayImportDirectory, NumberOfDelayImportDirectory, this));
706}
707
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000708export_directory_iterator COFFObjectFile::export_directory_begin() const {
709 return export_directory_iterator(
710 ExportDirectoryEntryRef(ExportDirectory, 0, this));
711}
712
713export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000714 if (!ExportDirectory)
715 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000716 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000717 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000718 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000719}
720
Rafael Espindolab5155a52014-02-10 20:24:04 +0000721section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000722 DataRefImpl Ret;
723 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
724 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000725}
726
Rafael Espindolab5155a52014-02-10 20:24:04 +0000727section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000728 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000729 int NumSections =
730 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000731 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
732 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000733}
734
Rui Ueyama74e85132014-11-19 00:18:07 +0000735base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
736 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
737}
738
739base_reloc_iterator COFFObjectFile::base_reloc_end() const {
740 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
741}
742
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000743uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000744 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000745}
746
747StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000748 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000749 case COFF::IMAGE_FILE_MACHINE_I386:
750 return "COFF-i386";
751 case COFF::IMAGE_FILE_MACHINE_AMD64:
752 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000753 case COFF::IMAGE_FILE_MACHINE_ARMNT:
754 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000755 default:
756 return "COFF-<unknown arch>";
757 }
758}
759
760unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000761 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000762 case COFF::IMAGE_FILE_MACHINE_I386:
763 return Triple::x86;
764 case COFF::IMAGE_FILE_MACHINE_AMD64:
765 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000766 case COFF::IMAGE_FILE_MACHINE_ARMNT:
767 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000768 default:
769 return Triple::UnknownArch;
770 }
771}
772
Rui Ueyama979fb402014-10-09 02:16:38 +0000773iterator_range<import_directory_iterator>
774COFFObjectFile::import_directories() const {
775 return make_range(import_directory_begin(), import_directory_end());
776}
777
778iterator_range<delay_import_directory_iterator>
779COFFObjectFile::delay_import_directories() const {
780 return make_range(delay_import_directory_begin(),
781 delay_import_directory_end());
782}
783
784iterator_range<export_directory_iterator>
785COFFObjectFile::export_directories() const {
786 return make_range(export_directory_begin(), export_directory_end());
787}
788
Rui Ueyama74e85132014-11-19 00:18:07 +0000789iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
790 return make_range(base_reloc_begin(), base_reloc_end());
791}
792
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000793std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000794 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000795 return object_error::success;
796}
797
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000798std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000799COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
800 Res = PE32PlusHeader;
801 return object_error::success;
802}
803
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000804std::error_code
805COFFObjectFile::getDataDirectory(uint32_t Index,
806 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000807 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000808 if (!DataDirectory) {
809 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000810 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000811 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000812 assert(PE32Header || PE32PlusHeader);
813 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
814 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000815 if (Index >= NumEnt) {
816 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000817 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000818 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000819 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000820 return object_error::success;
821}
822
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000823std::error_code COFFObjectFile::getSection(int32_t Index,
824 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000825 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000826 if (COFF::isReservedSectionNumber(Index))
David Majnemer236b0ca2014-11-17 11:17:17 +0000827 return object_error::success;
828 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000829 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000830 Result = SectionTable + (Index - 1);
David Majnemer236b0ca2014-11-17 11:17:17 +0000831 return object_error::success;
832 }
833 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000834}
835
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000836std::error_code COFFObjectFile::getString(uint32_t Offset,
837 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000838 if (StringTableSize <= 4)
839 // Tried to get a string from an empty string table.
840 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000841 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000842 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000843 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000844 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000845}
846
David Majnemer44f51e52014-09-10 12:51:52 +0000847std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000848 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000849 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000850 if (Symbol.getStringTableOffset().Zeroes == 0) {
851 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000852 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000853 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000854 return object_error::success;
855 }
856
David Majnemer44f51e52014-09-10 12:51:52 +0000857 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000858 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000859 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000860 else
861 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000862 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000863 return object_error::success;
864}
865
David Majnemer44f51e52014-09-10 12:51:52 +0000866ArrayRef<uint8_t>
867COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000868 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000869
David Majnemer44f51e52014-09-10 12:51:52 +0000870 size_t SymbolSize = getSymbolTableEntrySize();
871 if (Symbol.getNumberOfAuxSymbols() > 0) {
872 // AUX data comes immediately after the symbol in COFF
873 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000874# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000875 // Verify that the Aux symbol points to a valid entry in the symbol table.
876 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000877 if (Offset < getPointerToSymbolTable() ||
878 Offset >=
879 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000880 report_fatal_error("Aux Symbol data was outside of symbol table.");
881
David Majnemer44f51e52014-09-10 12:51:52 +0000882 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
883 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000884# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000885 }
David Majnemer44f51e52014-09-10 12:51:52 +0000886 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000887}
888
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000889std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
890 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000891 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000892 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000893 // Null terminated, let ::strlen figure out the length.
894 Name = Sec->Name;
895 else
896 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000897 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000898
899 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000900 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000901 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000902 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000903 if (decodeBase64StringEntry(Name.substr(2), Offset))
904 return object_error::parse_failed;
905 } else {
906 if (Name.substr(1).getAsInteger(10, Offset))
907 return object_error::parse_failed;
908 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000909 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000910 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000911 }
912
913 Res = Name;
914 return object_error::success;
915}
916
David Majnemera9ee5c02014-10-09 08:42:31 +0000917uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
918 // SizeOfRawData and VirtualSize change what they represent depending on
919 // whether or not we have an executable image.
920 //
921 // For object files, SizeOfRawData contains the size of section's data;
922 // VirtualSize is always zero.
923 //
924 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
925 // actual section size is in VirtualSize. It is possible for VirtualSize to
926 // be greater than SizeOfRawData; the contents past that point should be
927 // considered to be zero.
928 uint32_t SectionSize;
929 if (Sec->VirtualSize)
930 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
931 else
932 SectionSize = Sec->SizeOfRawData;
933
934 return SectionSize;
935}
936
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000937std::error_code
938COFFObjectFile::getSectionContents(const coff_section *Sec,
939 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000940 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
941 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000942 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
943 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000944 // The only thing that we need to verify is that the contents is contained
945 // within the file bounds. We don't need to make sure it doesn't cover other
946 // data, as there's nothing that says that is not allowed.
947 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000948 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000949 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000950 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000951 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000952 return object_error::success;
953}
954
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000955const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000956 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000957}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000958
Rafael Espindola5e812af2014-01-30 02:49:50 +0000959void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000960 Rel.p = reinterpret_cast<uintptr_t>(
961 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000962}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000963
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000964std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
965 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000966 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000967}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000968
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000969std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
970 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000971 const coff_relocation *R = toRel(Rel);
972 const support::ulittle32_t *VirtualAddressPtr;
973 if (std::error_code EC =
974 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
975 return EC;
976 Res = *VirtualAddressPtr;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000977 return object_error::success;
978}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000979
Rafael Espindola806f0062013-06-05 01:33:53 +0000980symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000981 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000982 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000983 if (R->SymbolTableIndex >= getNumberOfSymbols())
984 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000985 if (SymbolTable16)
986 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
987 else if (SymbolTable32)
988 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
989 else
David Majnemerc7353b52014-11-25 07:43:14 +0000990 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000991 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000992}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000993
Keno Fischerc780e8e2015-05-21 21:24:32 +0000994section_iterator COFFObjectFile::getRelocationSection(DataRefImpl Rel) const {
995 symbol_iterator Sym = getRelocationSymbol(Rel);
996 if (Sym == symbol_end())
997 return section_end();
998 COFFSymbolRef Symb = getCOFFSymbol(*Sym);
999 if (!Symb.isSection())
1000 return section_end();
1001 section_iterator Res(section_end());
1002 if (getSymbolSection(Sym->getRawDataRefImpl(),Res))
1003 return section_end();
1004 return Res;
1005}
1006
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001007std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1008 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001009 const coff_relocation* R = toRel(Rel);
1010 Res = R->Type;
1011 return object_error::success;
1012}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001013
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001014const coff_section *
1015COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1016 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001017}
1018
David Majnemer44f51e52014-09-10 12:51:52 +00001019COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1020 if (SymbolTable16)
1021 return toSymb<coff_symbol16>(Ref);
1022 if (SymbolTable32)
1023 return toSymb<coff_symbol32>(Ref);
1024 llvm_unreachable("no symbol table pointer!");
1025}
1026
1027COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1028 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001029}
1030
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001031const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001032COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1033 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001034}
1035
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001036#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1037 case COFF::reloc_type: \
1038 Res = #reloc_type; \
1039 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001040
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001041std::error_code
1042COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1043 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001044 const coff_relocation *Reloc = toRel(Rel);
1045 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001046 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001047 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001048 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1066 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001067 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001068 }
1069 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001070 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1071 switch (Reloc->Type) {
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1087 default:
1088 Res = "Unknown";
1089 }
1090 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001091 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001092 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1096 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1098 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1099 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1100 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1101 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1102 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1103 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1104 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001105 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001106 }
1107 break;
1108 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001109 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001110 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001111 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001112 return object_error::success;
1113}
1114
1115#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1116
Rafael Espindolac66d7612014-08-17 19:09:37 +00001117bool COFFObjectFile::isRelocatableObject() const {
1118 return !DataDirectory;
1119}
1120
Rui Ueyamac2bed422013-09-27 21:04:00 +00001121bool ImportDirectoryEntryRef::
1122operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001123 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001124}
1125
Rafael Espindola5e812af2014-01-30 02:49:50 +00001126void ImportDirectoryEntryRef::moveNext() {
1127 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001128}
1129
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001130std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1131 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001132 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001133 return object_error::success;
1134}
1135
Rui Ueyama861021f2014-10-02 22:05:29 +00001136static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001137makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001138 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001139 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001140 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(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 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001144 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001145}
1146
Rui Ueyama15d99352014-10-03 00:41:58 +00001147static imported_symbol_iterator
1148importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001149 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001150 Object->getRvaPtr(RVA, IntPtr);
1151 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001152}
1153
Rui Ueyama15d99352014-10-03 00:41:58 +00001154static imported_symbol_iterator
1155importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001156 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001157 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001158 // Forward the pointer to the last entry which is null.
1159 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001160 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001161 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1162 while (*Entry++)
1163 ++Index;
1164 } else {
1165 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1166 while (*Entry++)
1167 ++Index;
1168 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001169 return makeImportedSymbolIterator(Object, IntPtr, Index);
1170}
1171
1172imported_symbol_iterator
1173ImportDirectoryEntryRef::imported_symbol_begin() const {
1174 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1175 OwningObject);
1176}
1177
1178imported_symbol_iterator
1179ImportDirectoryEntryRef::imported_symbol_end() const {
1180 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1181 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001182}
1183
Rui Ueyama979fb402014-10-09 02:16:38 +00001184iterator_range<imported_symbol_iterator>
1185ImportDirectoryEntryRef::imported_symbols() const {
1186 return make_range(imported_symbol_begin(), imported_symbol_end());
1187}
1188
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001189std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001190 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001191 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001192 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001193 return EC;
1194 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001195 return object_error::success;
1196}
1197
Rui Ueyama1e152d52014-10-02 17:02:18 +00001198std::error_code
1199ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1200 Result = ImportTable[Index].ImportLookupTableRVA;
1201 return object_error::success;
1202}
1203
1204std::error_code
1205ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1206 Result = ImportTable[Index].ImportAddressTableRVA;
1207 return object_error::success;
1208}
1209
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001210std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001211 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001212 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001213 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1214 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001215 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001216 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1217 return object_error::success;
1218}
1219
Rui Ueyama15d99352014-10-03 00:41:58 +00001220bool DelayImportDirectoryEntryRef::
1221operator==(const DelayImportDirectoryEntryRef &Other) const {
1222 return Table == Other.Table && Index == Other.Index;
1223}
1224
1225void DelayImportDirectoryEntryRef::moveNext() {
1226 ++Index;
1227}
1228
1229imported_symbol_iterator
1230DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1231 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1232 OwningObject);
1233}
1234
1235imported_symbol_iterator
1236DelayImportDirectoryEntryRef::imported_symbol_end() const {
1237 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1238 OwningObject);
1239}
1240
Rui Ueyama979fb402014-10-09 02:16:38 +00001241iterator_range<imported_symbol_iterator>
1242DelayImportDirectoryEntryRef::imported_symbols() const {
1243 return make_range(imported_symbol_begin(), imported_symbol_end());
1244}
1245
Rui Ueyama15d99352014-10-03 00:41:58 +00001246std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1247 uintptr_t IntPtr = 0;
1248 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1249 return EC;
1250 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1251 return object_error::success;
1252}
1253
Rui Ueyama1af08652014-10-03 18:07:18 +00001254std::error_code DelayImportDirectoryEntryRef::
1255getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1256 Result = Table;
1257 return object_error::success;
1258}
1259
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001260std::error_code DelayImportDirectoryEntryRef::
1261getImportAddress(int AddrIndex, uint64_t &Result) const {
1262 uint32_t RVA = Table[Index].DelayImportAddressTable +
1263 AddrIndex * (OwningObject->is64() ? 8 : 4);
1264 uintptr_t IntPtr = 0;
1265 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1266 return EC;
1267 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001268 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001269 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001270 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001271 return object_error::success;
1272}
1273
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001274bool ExportDirectoryEntryRef::
1275operator==(const ExportDirectoryEntryRef &Other) const {
1276 return ExportTable == Other.ExportTable && Index == Other.Index;
1277}
1278
Rafael Espindola5e812af2014-01-30 02:49:50 +00001279void ExportDirectoryEntryRef::moveNext() {
1280 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001281}
1282
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001283// Returns the name of the current export symbol. If the symbol is exported only
1284// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001285std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001286 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001287 if (std::error_code EC =
1288 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001289 return EC;
1290 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1291 return object_error::success;
1292}
1293
Rui Ueyamae5df6092014-01-17 22:02:24 +00001294// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001295std::error_code
1296ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001297 Result = ExportTable->OrdinalBase;
1298 return object_error::success;
1299}
1300
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001301// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001302std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001303 Result = ExportTable->OrdinalBase + Index;
1304 return object_error::success;
1305}
1306
1307// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001308std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001309 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001310 if (std::error_code EC =
1311 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001312 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001313 const export_address_table_entry *entry =
1314 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001315 Result = entry[Index].ExportRVA;
1316 return object_error::success;
1317}
1318
1319// Returns the name of the current export symbol. If the symbol is exported only
1320// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001321std::error_code
1322ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001323 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001324 if (std::error_code EC =
1325 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001326 return EC;
1327 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1328
1329 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1330 int Offset = 0;
1331 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1332 I < E; ++I, ++Offset) {
1333 if (*I != Index)
1334 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001335 if (std::error_code EC =
1336 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001337 return EC;
1338 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001339 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001340 return EC;
1341 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1342 return object_error::success;
1343 }
1344 Result = "";
1345 return object_error::success;
1346}
1347
Rui Ueyama861021f2014-10-02 22:05:29 +00001348bool ImportedSymbolRef::
1349operator==(const ImportedSymbolRef &Other) const {
1350 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1351 && Index == Other.Index;
1352}
1353
1354void ImportedSymbolRef::moveNext() {
1355 ++Index;
1356}
1357
1358std::error_code
1359ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1360 uint32_t RVA;
1361 if (Entry32) {
1362 // If a symbol is imported only by ordinal, it has no name.
1363 if (Entry32[Index].isOrdinal())
1364 return object_error::success;
1365 RVA = Entry32[Index].getHintNameRVA();
1366 } else {
1367 if (Entry64[Index].isOrdinal())
1368 return object_error::success;
1369 RVA = Entry64[Index].getHintNameRVA();
1370 }
1371 uintptr_t IntPtr = 0;
1372 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1373 return EC;
1374 // +2 because the first two bytes is hint.
1375 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1376 return object_error::success;
1377}
1378
1379std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1380 uint32_t RVA;
1381 if (Entry32) {
1382 if (Entry32[Index].isOrdinal()) {
1383 Result = Entry32[Index].getOrdinal();
1384 return object_error::success;
1385 }
1386 RVA = Entry32[Index].getHintNameRVA();
1387 } else {
1388 if (Entry64[Index].isOrdinal()) {
1389 Result = Entry64[Index].getOrdinal();
1390 return object_error::success;
1391 }
1392 RVA = Entry64[Index].getHintNameRVA();
1393 }
1394 uintptr_t IntPtr = 0;
1395 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1396 return EC;
1397 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1398 return object_error::success;
1399}
1400
Rafael Espindola437b0d52014-07-31 03:12:45 +00001401ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001402ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001403 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001404 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001405 if (EC)
1406 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001407 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001408}
Rui Ueyama74e85132014-11-19 00:18:07 +00001409
1410bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1411 return Header == Other.Header && Index == Other.Index;
1412}
1413
1414void BaseRelocRef::moveNext() {
1415 // Header->BlockSize is the size of the current block, including the
1416 // size of the header itself.
1417 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001418 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001419 if (Size == Header->BlockSize) {
1420 // .reloc contains a list of base relocation blocks. Each block
1421 // consists of the header followed by entries. The header contains
1422 // how many entories will follow. When we reach the end of the
1423 // current block, proceed to the next block.
1424 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1425 reinterpret_cast<const uint8_t *>(Header) + Size);
1426 Index = 0;
1427 } else {
1428 ++Index;
1429 }
1430}
1431
1432std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1433 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1434 Type = Entry[Index].getType();
1435 return object_error::success;
1436}
1437
1438std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1439 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1440 Result = Header->PageRVA + Entry[Index].getOffset();
1441 return object_error::success;
1442}