blob: 0595d38f0863af4e4faeeb30befc166b5b595c87 [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 }
Rui Ueyama7d099192015-06-09 15:20:42 +000049 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000050}
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);
Rui Ueyama7d099192015-06-09 15:20:42 +000062 return std::error_code();
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()) {
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000158 Result = UnknownAddress;
Rui Ueyama7d099192015-06-09 15:20:42 +0000159 return std::error_code();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000160 }
161 if (Symb.isCommon()) {
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000162 Result = UnknownAddress;
Rui Ueyama7d099192015-06-09 15:20:42 +0000163 return std::error_code();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000164 }
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();
Rui Ueyama7d099192015-06-09 15:20:42 +0000172 return std::error_code();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000173 }
174
175 Result = Symb.getValue();
Rui Ueyama7d099192015-06-09 15:20:42 +0000176 return std::error_code();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000177}
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 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000208 return std::error_code();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000209}
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 Espindolad7a32ea2015-06-24 10:20:30 +0000239uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000240 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000241 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000242}
243
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000244std::error_code
245COFFObjectFile::getSymbolSection(DataRefImpl Ref,
246 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000247 COFFSymbolRef Symb = getCOFFSymbol(Ref);
248 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000249 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000250 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000251 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000252 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000253 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000254 DataRefImpl Ref;
255 Ref.p = reinterpret_cast<uintptr_t>(Sec);
256 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000257 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000258 return std::error_code();
Michael J. Spencer3217315392011-10-17 23:54:46 +0000259}
260
Rafael Espindola5e812af2014-01-30 02:49:50 +0000261void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000262 const coff_section *Sec = toSec(Ref);
263 Sec += 1;
264 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000265}
266
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000267std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
268 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000269 const coff_section *Sec = toSec(Ref);
270 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000271}
272
Rafael Espindola80291272014-10-08 15:28:58 +0000273uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000274 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000275 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000276}
277
Rafael Espindola80291272014-10-08 15:28:58 +0000278uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000279 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000280}
281
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000282std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
283 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000284 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000285 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000286 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000287 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
288 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000289}
290
Rafael Espindola80291272014-10-08 15:28:58 +0000291uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000292 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000293 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000294}
295
Rafael Espindola80291272014-10-08 15:28:58 +0000296bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000297 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000298 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000299}
300
Rafael Espindola80291272014-10-08 15:28:58 +0000301bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000302 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000303 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000304}
305
Rafael Espindola80291272014-10-08 15:28:58 +0000306bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000307 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000308 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
309 COFF::IMAGE_SCN_MEM_READ |
310 COFF::IMAGE_SCN_MEM_WRITE;
311 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000312}
313
Rafael Espindola80291272014-10-08 15:28:58 +0000314bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000315 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000316 // In COFF, a virtual section won't have any in-file
317 // content, so the file pointer to the content will be zero.
318 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000319}
320
Rafael Espindola80291272014-10-08 15:28:58 +0000321bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
322 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000323 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000324 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000325 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000326 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000327}
328
David Majnemere830c602014-11-13 08:46:37 +0000329static uint32_t getNumberOfRelocations(const coff_section *Sec,
330 MemoryBufferRef M, const uint8_t *base) {
331 // The field for the number of relocations in COFF section table is only
332 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
333 // NumberOfRelocations field, and the actual relocation count is stored in the
334 // VirtualAddress field in the first relocation entry.
335 if (Sec->hasExtendedRelocations()) {
336 const coff_relocation *FirstReloc;
337 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
338 base + Sec->PointerToRelocations)))
339 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000340 // -1 to exclude this first relocation entry.
341 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000342 }
343 return Sec->NumberOfRelocations;
344}
345
David Majnemer94751be2014-11-13 09:50:18 +0000346static const coff_relocation *
347getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
348 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
349 if (!NumRelocs)
350 return nullptr;
351 auto begin = reinterpret_cast<const coff_relocation *>(
352 Base + Sec->PointerToRelocations);
353 if (Sec->hasExtendedRelocations()) {
354 // Skip the first relocation entry repurposed to store the number of
355 // relocations.
356 begin++;
357 }
358 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
359 return nullptr;
360 return begin;
361}
362
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000363relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
364 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000365 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000366 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000367 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000368 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000369}
370
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000371relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
372 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000373 const coff_relocation *I = getFirstReloc(Sec, Data, base());
374 if (I)
375 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000376 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000377 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000378 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000379}
380
Rui Ueyamac2bed422013-09-27 21:04:00 +0000381// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000382std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000383 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000384 if (std::error_code EC = getObject(
385 SymbolTable16, Data, base() + getPointerToSymbolTable(),
386 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000387 return EC;
388
389 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000390 if (std::error_code EC = getObject(
391 SymbolTable32, Data, base() + getPointerToSymbolTable(),
392 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000393 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000394
395 // Find string table. The first four byte of the string table contains the
396 // total size of the string table, including the size field itself. If the
397 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000398 uint32_t StringTableOffset = getPointerToSymbolTable() +
399 getNumberOfSymbols() * getSymbolTableEntrySize();
400 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000401 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000402 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000404 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000405 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000406 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000407 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408
Nico Rieck773a5792014-02-26 19:51:44 +0000409 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
410 // tools like cvtres write a size of 0 for an empty table instead of 4.
411 if (StringTableSize < 4)
412 StringTableSize = 4;
413
Rui Ueyamac2bed422013-09-27 21:04:00 +0000414 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000415 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000416 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000417 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000418}
419
Rui Ueyama215a5862014-02-20 06:51:07 +0000420// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000421std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000422 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
423 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000424 uint64_t Rva = Addr - ImageBase;
425 assert(Rva <= UINT32_MAX);
426 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000427}
428
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000430std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000431 for (const SectionRef &S : sections()) {
432 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000433 uint32_t SectionStart = Section->VirtualAddress;
434 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000435 if (SectionStart <= Addr && Addr < SectionEnd) {
436 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000438 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000439 }
440 }
441 return object_error::parse_failed;
442}
443
444// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
445// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000446std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
447 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000448 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000449 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000450 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000451 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
452 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
453 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000454 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000455}
456
457// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000458std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000459 // First, we get the RVA of the import table. If the file lacks a pointer to
460 // the import table, do nothing.
461 const data_directory *DataEntry;
462 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000463 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000464
465 // Do nothing if the pointer to import table is NULL.
466 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000467 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468
469 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000470 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000471 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000472 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000473
474 // Find the section that contains the RVA. This is needed because the RVA is
475 // the import table's memory address which is different from its file offset.
476 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000477 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000478 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000479 ImportDirectory = reinterpret_cast<
480 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000481 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000482}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000483
Rui Ueyama15d99352014-10-03 00:41:58 +0000484// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
485std::error_code COFFObjectFile::initDelayImportTablePtr() {
486 const data_directory *DataEntry;
487 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000488 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000489 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000490 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000491
492 uint32_t RVA = DataEntry->RelativeVirtualAddress;
493 NumberOfDelayImportDirectory = DataEntry->Size /
494 sizeof(delay_import_directory_table_entry) - 1;
495
496 uintptr_t IntPtr = 0;
497 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
498 return EC;
499 DelayImportDirectory = reinterpret_cast<
500 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000501 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000502}
503
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000504// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000505std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000506 // First, we get the RVA of the export table. If the file lacks a pointer to
507 // the export table, do nothing.
508 const data_directory *DataEntry;
509 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000510 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000511
512 // Do nothing if the pointer to export table is NULL.
513 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000514 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000515
516 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
517 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000518 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000519 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000520 ExportDirectory =
521 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000522 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000523}
524
Rui Ueyama74e85132014-11-19 00:18:07 +0000525std::error_code COFFObjectFile::initBaseRelocPtr() {
526 const data_directory *DataEntry;
527 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000528 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000529 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000530 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000531
532 uintptr_t IntPtr = 0;
533 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
534 return EC;
535 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
536 IntPtr);
537 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
538 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000539 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000540}
541
Rafael Espindola48af1c22014-08-19 18:44:46 +0000542COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
543 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000544 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
545 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
546 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
547 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000548 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000549 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
550 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000551 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000552 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000553 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000554
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000555 // The current location in the file where we are looking at.
556 uint64_t CurPtr = 0;
557
558 // PE header is optional and is present only in executables. If it exists,
559 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000560 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000561
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000562 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000563 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000564 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
565 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000566 const auto *DH = reinterpret_cast<const dos_header *>(base());
567 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
568 CurPtr = DH->AddressOfNewExeHeader;
569 // Check the PE magic bytes. ("PE\0\0")
570 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
571 EC = object_error::parse_failed;
572 return;
573 }
574 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
575 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000576 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000577 }
578
Rafael Espindola48af1c22014-08-19 18:44:46 +0000579 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000580 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000581
582 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
583 // import libraries share a common prefix but bigobj is more restrictive.
584 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
585 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
586 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
587 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
588 return;
589
590 // Verify that we are dealing with bigobj.
591 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
592 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
593 sizeof(COFF::BigObjMagic)) == 0) {
594 COFFHeader = nullptr;
595 CurPtr += sizeof(coff_bigobj_file_header);
596 } else {
597 // It's not a bigobj.
598 COFFBigObjHeader = nullptr;
599 }
600 }
601 if (COFFHeader) {
602 // The prior checkSize call may have failed. This isn't a hard error
603 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000604 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000605 CurPtr += sizeof(coff_file_header);
606
607 if (COFFHeader->isImportLibrary())
608 return;
609 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000610
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000611 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000612 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000613 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000614 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000615
616 const uint8_t *DataDirAddr;
617 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000618 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000619 PE32Header = Header;
620 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
621 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000622 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000623 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
624 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
625 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
626 } else {
627 // It's neither PE32 nor PE32+.
628 EC = object_error::parse_failed;
629 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000630 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000631 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000632 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000633 CurPtr += COFFHeader->SizeOfOptionalHeader;
634 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000635
Rafael Espindola48af1c22014-08-19 18:44:46 +0000636 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000637 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000638 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000639
Rui Ueyamac2bed422013-09-27 21:04:00 +0000640 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000641 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000642 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000643 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000644 } else {
645 // We had better not have any symbols if we don't have a symbol table.
646 if (getNumberOfSymbols() != 0) {
647 EC = object_error::parse_failed;
648 return;
649 }
650 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000651
Rui Ueyamac2bed422013-09-27 21:04:00 +0000652 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000653 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000654 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000655 if ((EC = initDelayImportTablePtr()))
656 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000657
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000658 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000659 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000660 return;
661
Rui Ueyama74e85132014-11-19 00:18:07 +0000662 // Initialize the pointer to the base relocation table.
663 if ((EC = initBaseRelocPtr()))
664 return;
665
Rui Ueyama7d099192015-06-09 15:20:42 +0000666 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000667}
668
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000669basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000670 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000671 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000672 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000673}
674
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000675basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000676 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000677 DataRefImpl Ret;
678 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000679 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000680}
681
Rui Ueyamabc654b12013-09-27 21:47:05 +0000682import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000683 return import_directory_iterator(
684 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000685}
686
Rui Ueyamabc654b12013-09-27 21:47:05 +0000687import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000688 return import_directory_iterator(
689 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000690}
David Meyerc429b802012-03-01 22:19:54 +0000691
Rui Ueyama15d99352014-10-03 00:41:58 +0000692delay_import_directory_iterator
693COFFObjectFile::delay_import_directory_begin() const {
694 return delay_import_directory_iterator(
695 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
696}
697
698delay_import_directory_iterator
699COFFObjectFile::delay_import_directory_end() const {
700 return delay_import_directory_iterator(
701 DelayImportDirectoryEntryRef(
702 DelayImportDirectory, NumberOfDelayImportDirectory, this));
703}
704
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000705export_directory_iterator COFFObjectFile::export_directory_begin() const {
706 return export_directory_iterator(
707 ExportDirectoryEntryRef(ExportDirectory, 0, this));
708}
709
710export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000711 if (!ExportDirectory)
712 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000713 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000714 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000715 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000716}
717
Rafael Espindolab5155a52014-02-10 20:24:04 +0000718section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000719 DataRefImpl Ret;
720 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
721 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000722}
723
Rafael Espindolab5155a52014-02-10 20:24:04 +0000724section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000725 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000726 int NumSections =
727 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000728 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
729 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000730}
731
Rui Ueyama74e85132014-11-19 00:18:07 +0000732base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
733 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
734}
735
736base_reloc_iterator COFFObjectFile::base_reloc_end() const {
737 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
738}
739
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000740uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000741 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000742}
743
744StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000745 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000746 case COFF::IMAGE_FILE_MACHINE_I386:
747 return "COFF-i386";
748 case COFF::IMAGE_FILE_MACHINE_AMD64:
749 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000750 case COFF::IMAGE_FILE_MACHINE_ARMNT:
751 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000752 default:
753 return "COFF-<unknown arch>";
754 }
755}
756
757unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000758 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000759 case COFF::IMAGE_FILE_MACHINE_I386:
760 return Triple::x86;
761 case COFF::IMAGE_FILE_MACHINE_AMD64:
762 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000763 case COFF::IMAGE_FILE_MACHINE_ARMNT:
764 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000765 default:
766 return Triple::UnknownArch;
767 }
768}
769
Rui Ueyama979fb402014-10-09 02:16:38 +0000770iterator_range<import_directory_iterator>
771COFFObjectFile::import_directories() const {
772 return make_range(import_directory_begin(), import_directory_end());
773}
774
775iterator_range<delay_import_directory_iterator>
776COFFObjectFile::delay_import_directories() const {
777 return make_range(delay_import_directory_begin(),
778 delay_import_directory_end());
779}
780
781iterator_range<export_directory_iterator>
782COFFObjectFile::export_directories() const {
783 return make_range(export_directory_begin(), export_directory_end());
784}
785
Rui Ueyama74e85132014-11-19 00:18:07 +0000786iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
787 return make_range(base_reloc_begin(), base_reloc_end());
788}
789
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000790std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000791 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000792 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000793}
794
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000795std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000796COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
797 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000798 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000799}
800
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000801std::error_code
802COFFObjectFile::getDataDirectory(uint32_t Index,
803 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000804 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000805 if (!DataDirectory) {
806 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000807 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000808 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000809 assert(PE32Header || PE32PlusHeader);
810 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
811 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000812 if (Index >= NumEnt) {
813 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000814 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000815 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000816 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000817 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000818}
819
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000820std::error_code COFFObjectFile::getSection(int32_t Index,
821 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000822 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000823 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000824 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000825 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000826 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000827 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000828 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000829 }
830 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000831}
832
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000833std::error_code COFFObjectFile::getString(uint32_t Offset,
834 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000835 if (StringTableSize <= 4)
836 // Tried to get a string from an empty string table.
837 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000838 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000839 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000840 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000841 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000842}
843
David Majnemer44f51e52014-09-10 12:51:52 +0000844std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000845 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000846 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000847 if (Symbol.getStringTableOffset().Zeroes == 0) {
848 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000849 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000850 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000851 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000852 }
853
David Majnemer44f51e52014-09-10 12:51:52 +0000854 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000855 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000856 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000857 else
858 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000859 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000860 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000861}
862
David Majnemer44f51e52014-09-10 12:51:52 +0000863ArrayRef<uint8_t>
864COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000865 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000866
David Majnemer44f51e52014-09-10 12:51:52 +0000867 size_t SymbolSize = getSymbolTableEntrySize();
868 if (Symbol.getNumberOfAuxSymbols() > 0) {
869 // AUX data comes immediately after the symbol in COFF
870 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000871# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000872 // Verify that the Aux symbol points to a valid entry in the symbol table.
873 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000874 if (Offset < getPointerToSymbolTable() ||
875 Offset >=
876 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000877 report_fatal_error("Aux Symbol data was outside of symbol table.");
878
David Majnemer44f51e52014-09-10 12:51:52 +0000879 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
880 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000881# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000882 }
David Majnemer44f51e52014-09-10 12:51:52 +0000883 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000884}
885
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000886std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
887 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000888 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000889 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000890 // Null terminated, let ::strlen figure out the length.
891 Name = Sec->Name;
892 else
893 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000894 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000895
896 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000897 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000898 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000899 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000900 if (decodeBase64StringEntry(Name.substr(2), Offset))
901 return object_error::parse_failed;
902 } else {
903 if (Name.substr(1).getAsInteger(10, Offset))
904 return object_error::parse_failed;
905 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000906 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000907 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000908 }
909
910 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000911 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000912}
913
David Majnemera9ee5c02014-10-09 08:42:31 +0000914uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
915 // SizeOfRawData and VirtualSize change what they represent depending on
916 // whether or not we have an executable image.
917 //
918 // For object files, SizeOfRawData contains the size of section's data;
919 // VirtualSize is always zero.
920 //
921 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
922 // actual section size is in VirtualSize. It is possible for VirtualSize to
923 // be greater than SizeOfRawData; the contents past that point should be
924 // considered to be zero.
925 uint32_t SectionSize;
926 if (Sec->VirtualSize)
927 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
928 else
929 SectionSize = Sec->SizeOfRawData;
930
931 return SectionSize;
932}
933
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000934std::error_code
935COFFObjectFile::getSectionContents(const coff_section *Sec,
936 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000937 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
938 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000939 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
940 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000941 // The only thing that we need to verify is that the contents is contained
942 // within the file bounds. We don't need to make sure it doesn't cover other
943 // data, as there's nothing that says that is not allowed.
944 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000945 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000946 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000947 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000948 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000949 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000950}
951
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000952const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000953 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000954}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000955
Rafael Espindola5e812af2014-01-30 02:49:50 +0000956void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000957 Rel.p = reinterpret_cast<uintptr_t>(
958 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000959}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000960
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000961std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
962 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000963 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000964}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000965
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000966std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
967 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000968 const coff_relocation *R = toRel(Rel);
969 const support::ulittle32_t *VirtualAddressPtr;
970 if (std::error_code EC =
971 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
972 return EC;
973 Res = *VirtualAddressPtr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000974 return std::error_code();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000975}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000976
Rafael Espindola806f0062013-06-05 01:33:53 +0000977symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000978 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000979 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000980 if (R->SymbolTableIndex >= getNumberOfSymbols())
981 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000982 if (SymbolTable16)
983 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
984 else if (SymbolTable32)
985 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
986 else
David Majnemerc7353b52014-11-25 07:43:14 +0000987 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000988 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000989}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000990
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000991std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
992 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000993 const coff_relocation* R = toRel(Rel);
994 Res = R->Type;
Rui Ueyama7d099192015-06-09 15:20:42 +0000995 return std::error_code();
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000996}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000997
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000998const coff_section *
999COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1000 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001001}
1002
David Majnemer44f51e52014-09-10 12:51:52 +00001003COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1004 if (SymbolTable16)
1005 return toSymb<coff_symbol16>(Ref);
1006 if (SymbolTable32)
1007 return toSymb<coff_symbol32>(Ref);
1008 llvm_unreachable("no symbol table pointer!");
1009}
1010
1011COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1012 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001013}
1014
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001015const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001016COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1017 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001018}
1019
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001020#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1021 case COFF::reloc_type: \
1022 Res = #reloc_type; \
1023 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001024
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001025std::error_code
1026COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1027 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001028 const coff_relocation *Reloc = toRel(Rel);
1029 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001030 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001031 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001032 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001033 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1034 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1035 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1036 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1037 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1038 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1039 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1040 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1050 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001051 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001052 }
1053 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001054 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1055 switch (Reloc->Type) {
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1071 default:
1072 Res = "Unknown";
1073 }
1074 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001075 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001076 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1084 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1085 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1086 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1088 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001089 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001090 }
1091 break;
1092 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001093 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001094 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001095 Result.append(Res.begin(), Res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +00001096 return std::error_code();
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001097}
1098
1099#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1100
Rafael Espindolac66d7612014-08-17 19:09:37 +00001101bool COFFObjectFile::isRelocatableObject() const {
1102 return !DataDirectory;
1103}
1104
Rui Ueyamac2bed422013-09-27 21:04:00 +00001105bool ImportDirectoryEntryRef::
1106operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001107 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001108}
1109
Rafael Espindola5e812af2014-01-30 02:49:50 +00001110void ImportDirectoryEntryRef::moveNext() {
1111 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001112}
1113
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001114std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1115 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001116 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001117 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001118}
1119
Rui Ueyama861021f2014-10-02 22:05:29 +00001120static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001121makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001122 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001123 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001124 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001125 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001126 }
1127 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001128 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001129}
1130
Rui Ueyama15d99352014-10-03 00:41:58 +00001131static imported_symbol_iterator
1132importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001133 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001134 Object->getRvaPtr(RVA, IntPtr);
1135 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001136}
1137
Rui Ueyama15d99352014-10-03 00:41:58 +00001138static imported_symbol_iterator
1139importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001140 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001141 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001142 // Forward the pointer to the last entry which is null.
1143 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001144 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001145 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1146 while (*Entry++)
1147 ++Index;
1148 } else {
1149 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1150 while (*Entry++)
1151 ++Index;
1152 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001153 return makeImportedSymbolIterator(Object, IntPtr, Index);
1154}
1155
1156imported_symbol_iterator
1157ImportDirectoryEntryRef::imported_symbol_begin() const {
1158 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1159 OwningObject);
1160}
1161
1162imported_symbol_iterator
1163ImportDirectoryEntryRef::imported_symbol_end() const {
1164 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1165 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001166}
1167
Rui Ueyama979fb402014-10-09 02:16:38 +00001168iterator_range<imported_symbol_iterator>
1169ImportDirectoryEntryRef::imported_symbols() const {
1170 return make_range(imported_symbol_begin(), imported_symbol_end());
1171}
1172
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001173std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001174 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001175 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001176 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001177 return EC;
1178 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001179 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001180}
1181
Rui Ueyama1e152d52014-10-02 17:02:18 +00001182std::error_code
1183ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1184 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001185 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001186}
1187
1188std::error_code
1189ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1190 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001191 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001192}
1193
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001194std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001195 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001196 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001197 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1198 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001199 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001200 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001201 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001202}
1203
Rui Ueyama15d99352014-10-03 00:41:58 +00001204bool DelayImportDirectoryEntryRef::
1205operator==(const DelayImportDirectoryEntryRef &Other) const {
1206 return Table == Other.Table && Index == Other.Index;
1207}
1208
1209void DelayImportDirectoryEntryRef::moveNext() {
1210 ++Index;
1211}
1212
1213imported_symbol_iterator
1214DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1215 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1216 OwningObject);
1217}
1218
1219imported_symbol_iterator
1220DelayImportDirectoryEntryRef::imported_symbol_end() const {
1221 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1222 OwningObject);
1223}
1224
Rui Ueyama979fb402014-10-09 02:16:38 +00001225iterator_range<imported_symbol_iterator>
1226DelayImportDirectoryEntryRef::imported_symbols() const {
1227 return make_range(imported_symbol_begin(), imported_symbol_end());
1228}
1229
Rui Ueyama15d99352014-10-03 00:41:58 +00001230std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1231 uintptr_t IntPtr = 0;
1232 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1233 return EC;
1234 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001235 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001236}
1237
Rui Ueyama1af08652014-10-03 18:07:18 +00001238std::error_code DelayImportDirectoryEntryRef::
1239getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1240 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001241 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001242}
1243
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001244std::error_code DelayImportDirectoryEntryRef::
1245getImportAddress(int AddrIndex, uint64_t &Result) const {
1246 uint32_t RVA = Table[Index].DelayImportAddressTable +
1247 AddrIndex * (OwningObject->is64() ? 8 : 4);
1248 uintptr_t IntPtr = 0;
1249 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1250 return EC;
1251 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001252 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001253 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001254 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001255 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001256}
1257
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001258bool ExportDirectoryEntryRef::
1259operator==(const ExportDirectoryEntryRef &Other) const {
1260 return ExportTable == Other.ExportTable && Index == Other.Index;
1261}
1262
Rafael Espindola5e812af2014-01-30 02:49:50 +00001263void ExportDirectoryEntryRef::moveNext() {
1264 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001265}
1266
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001267// Returns the name of the current export symbol. If the symbol is exported only
1268// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001269std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001270 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001271 if (std::error_code EC =
1272 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001273 return EC;
1274 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001275 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001276}
1277
Rui Ueyamae5df6092014-01-17 22:02:24 +00001278// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001279std::error_code
1280ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001281 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001282 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001283}
1284
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001285// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001286std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001287 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001288 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001289}
1290
1291// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001292std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001293 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001294 if (std::error_code EC =
1295 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001296 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001297 const export_address_table_entry *entry =
1298 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001299 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001300 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001301}
1302
1303// Returns the name of the current export symbol. If the symbol is exported only
1304// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001305std::error_code
1306ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001307 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001308 if (std::error_code EC =
1309 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001310 return EC;
1311 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1312
1313 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1314 int Offset = 0;
1315 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1316 I < E; ++I, ++Offset) {
1317 if (*I != Index)
1318 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001319 if (std::error_code EC =
1320 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001321 return EC;
1322 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001323 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001324 return EC;
1325 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001326 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001327 }
1328 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001329 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001330}
1331
Rui Ueyama861021f2014-10-02 22:05:29 +00001332bool ImportedSymbolRef::
1333operator==(const ImportedSymbolRef &Other) const {
1334 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1335 && Index == Other.Index;
1336}
1337
1338void ImportedSymbolRef::moveNext() {
1339 ++Index;
1340}
1341
1342std::error_code
1343ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1344 uint32_t RVA;
1345 if (Entry32) {
1346 // If a symbol is imported only by ordinal, it has no name.
1347 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001348 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001349 RVA = Entry32[Index].getHintNameRVA();
1350 } else {
1351 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001352 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001353 RVA = Entry64[Index].getHintNameRVA();
1354 }
1355 uintptr_t IntPtr = 0;
1356 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1357 return EC;
1358 // +2 because the first two bytes is hint.
1359 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001360 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001361}
1362
1363std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1364 uint32_t RVA;
1365 if (Entry32) {
1366 if (Entry32[Index].isOrdinal()) {
1367 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001368 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001369 }
1370 RVA = Entry32[Index].getHintNameRVA();
1371 } else {
1372 if (Entry64[Index].isOrdinal()) {
1373 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001374 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001375 }
1376 RVA = Entry64[Index].getHintNameRVA();
1377 }
1378 uintptr_t IntPtr = 0;
1379 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1380 return EC;
1381 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001382 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001383}
1384
Rafael Espindola437b0d52014-07-31 03:12:45 +00001385ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001386ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001387 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001388 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001389 if (EC)
1390 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001391 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001392}
Rui Ueyama74e85132014-11-19 00:18:07 +00001393
1394bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1395 return Header == Other.Header && Index == Other.Index;
1396}
1397
1398void BaseRelocRef::moveNext() {
1399 // Header->BlockSize is the size of the current block, including the
1400 // size of the header itself.
1401 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001402 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001403 if (Size == Header->BlockSize) {
1404 // .reloc contains a list of base relocation blocks. Each block
1405 // consists of the header followed by entries. The header contains
1406 // how many entories will follow. When we reach the end of the
1407 // current block, proceed to the next block.
1408 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1409 reinterpret_cast<const uint8_t *>(Header) + Size);
1410 Index = 0;
1411 } else {
1412 ++Index;
1413 }
1414}
1415
1416std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1417 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1418 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001419 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001420}
1421
1422std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1423 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1424 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001425 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001426}