blob: 797ee1a7cdafe9f9a84a5ba42672b2d888e768b7 [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 Espindola991af662015-06-24 19:11:10 +0000153uint64_t COFFObjectFile::getSymbolValue(DataRefImpl Ref) const {
154 COFFSymbolRef Sym = getCOFFSymbol(Ref);
155
156 if (Sym.isAnyUndefined() || Sym.isCommon())
157 return UnknownAddress;
158
159 return Sym.getValue();
160}
161
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000162std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
163 uint64_t &Result) const {
Rafael Espindola991af662015-06-24 19:11:10 +0000164 Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000165 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000166 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000167
168 if (Symb.isAnyUndefined() || Symb.isCommon() ||
169 COFF::isReservedSectionNumber(SectionNumber))
Rui Ueyama7d099192015-06-09 15:20:42 +0000170 return std::error_code();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000171
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000172 const coff_section *Section = nullptr;
173 if (std::error_code EC = getSection(SectionNumber, Section))
174 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000175 Result += Section->VirtualAddress;
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 Espindola6bf32212015-06-24 19:57:32 +0000261unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
262 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
263 return Symb.getSectionNumber();
264}
265
Rafael Espindola5e812af2014-01-30 02:49:50 +0000266void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000267 const coff_section *Sec = toSec(Ref);
268 Sec += 1;
269 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000270}
271
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000272std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
273 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000274 const coff_section *Sec = toSec(Ref);
275 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000276}
277
Rafael Espindola80291272014-10-08 15:28:58 +0000278uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000279 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000280 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000281}
282
Rafael Espindola80291272014-10-08 15:28:58 +0000283uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000284 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000285}
286
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000287std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
288 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000289 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000290 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000291 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000292 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
293 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000294}
295
Rafael Espindola80291272014-10-08 15:28:58 +0000296uint64_t COFFObjectFile::getSectionAlignment(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 uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000299}
300
Rafael Espindola80291272014-10-08 15:28:58 +0000301bool COFFObjectFile::isSectionText(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_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000304}
305
Rafael Espindola80291272014-10-08 15:28:58 +0000306bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000307 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000308 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000309}
310
Rafael Espindola80291272014-10-08 15:28:58 +0000311bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000312 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000313 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
314 COFF::IMAGE_SCN_MEM_READ |
315 COFF::IMAGE_SCN_MEM_WRITE;
316 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000317}
318
Rafael Espindola6bf32212015-06-24 19:57:32 +0000319unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
320 uintptr_t Offset =
321 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
322 assert((Offset % sizeof(coff_section)) == 0);
323 return (Offset / sizeof(coff_section)) + 1;
324}
325
Rafael Espindola80291272014-10-08 15:28:58 +0000326bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000327 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000328 // In COFF, a virtual section won't have any in-file
329 // content, so the file pointer to the content will be zero.
330 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000331}
332
Rafael Espindola80291272014-10-08 15:28:58 +0000333bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
334 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000335 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000336 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000337 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000338 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000339}
340
David Majnemere830c602014-11-13 08:46:37 +0000341static uint32_t getNumberOfRelocations(const coff_section *Sec,
342 MemoryBufferRef M, const uint8_t *base) {
343 // The field for the number of relocations in COFF section table is only
344 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
345 // NumberOfRelocations field, and the actual relocation count is stored in the
346 // VirtualAddress field in the first relocation entry.
347 if (Sec->hasExtendedRelocations()) {
348 const coff_relocation *FirstReloc;
349 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
350 base + Sec->PointerToRelocations)))
351 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000352 // -1 to exclude this first relocation entry.
353 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000354 }
355 return Sec->NumberOfRelocations;
356}
357
David Majnemer94751be2014-11-13 09:50:18 +0000358static const coff_relocation *
359getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
360 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
361 if (!NumRelocs)
362 return nullptr;
363 auto begin = reinterpret_cast<const coff_relocation *>(
364 Base + Sec->PointerToRelocations);
365 if (Sec->hasExtendedRelocations()) {
366 // Skip the first relocation entry repurposed to store the number of
367 // relocations.
368 begin++;
369 }
370 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
371 return nullptr;
372 return begin;
373}
374
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
376 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000377 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000378 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000379 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000380 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000381}
382
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000383relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
384 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000385 const coff_relocation *I = getFirstReloc(Sec, Data, base());
386 if (I)
387 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000388 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000389 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000390 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000391}
392
Rui Ueyamac2bed422013-09-27 21:04:00 +0000393// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000394std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000395 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000396 if (std::error_code EC = getObject(
397 SymbolTable16, Data, base() + getPointerToSymbolTable(),
398 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000399 return EC;
400
401 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000402 if (std::error_code EC = getObject(
403 SymbolTable32, Data, base() + getPointerToSymbolTable(),
404 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000405 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000406
407 // Find string table. The first four byte of the string table contains the
408 // total size of the string table, including the size field itself. If the
409 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000410 uint32_t StringTableOffset = getPointerToSymbolTable() +
411 getNumberOfSymbols() * getSymbolTableEntrySize();
412 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000413 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000414 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000415 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000416 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000417 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000418 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000419 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000420
Nico Rieck773a5792014-02-26 19:51:44 +0000421 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
422 // tools like cvtres write a size of 0 for an empty table instead of 4.
423 if (StringTableSize < 4)
424 StringTableSize = 4;
425
Rui Ueyamac2bed422013-09-27 21:04:00 +0000426 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000427 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000428 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000429 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000430}
431
Rui Ueyama215a5862014-02-20 06:51:07 +0000432// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000433std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000434 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
435 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000436 uint64_t Rva = Addr - ImageBase;
437 assert(Rva <= UINT32_MAX);
438 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000439}
440
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000442std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000443 for (const SectionRef &S : sections()) {
444 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000445 uint32_t SectionStart = Section->VirtualAddress;
446 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000447 if (SectionStart <= Addr && Addr < SectionEnd) {
448 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000449 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000450 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000451 }
452 }
453 return object_error::parse_failed;
454}
455
456// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
457// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000458std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
459 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000460 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000461 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000462 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000463 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
464 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
465 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000466 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000467}
468
469// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000470std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000471 // First, we get the RVA of the import table. If the file lacks a pointer to
472 // the import table, do nothing.
473 const data_directory *DataEntry;
474 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000475 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000476
477 // Do nothing if the pointer to import table is NULL.
478 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000479 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000480
481 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000482 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000483 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000484 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000485
486 // Find the section that contains the RVA. This is needed because the RVA is
487 // the import table's memory address which is different from its file offset.
488 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000489 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000490 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000491 ImportDirectory = reinterpret_cast<
492 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000493 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000494}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000495
Rui Ueyama15d99352014-10-03 00:41:58 +0000496// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
497std::error_code COFFObjectFile::initDelayImportTablePtr() {
498 const data_directory *DataEntry;
499 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000500 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000501 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000502 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000503
504 uint32_t RVA = DataEntry->RelativeVirtualAddress;
505 NumberOfDelayImportDirectory = DataEntry->Size /
506 sizeof(delay_import_directory_table_entry) - 1;
507
508 uintptr_t IntPtr = 0;
509 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
510 return EC;
511 DelayImportDirectory = reinterpret_cast<
512 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000513 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000514}
515
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000516// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000517std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000518 // First, we get the RVA of the export table. If the file lacks a pointer to
519 // the export table, do nothing.
520 const data_directory *DataEntry;
521 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000522 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000523
524 // Do nothing if the pointer to export table is NULL.
525 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000526 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000527
528 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
529 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000530 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000531 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000532 ExportDirectory =
533 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000534 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000535}
536
Rui Ueyama74e85132014-11-19 00:18:07 +0000537std::error_code COFFObjectFile::initBaseRelocPtr() {
538 const data_directory *DataEntry;
539 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000540 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000541 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000542 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000543
544 uintptr_t IntPtr = 0;
545 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
546 return EC;
547 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
548 IntPtr);
549 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
550 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000551 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000552}
553
Rafael Espindola48af1c22014-08-19 18:44:46 +0000554COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
555 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000556 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
557 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
558 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
559 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000560 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000561 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
562 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000563 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000564 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000565 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000566
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000567 // The current location in the file where we are looking at.
568 uint64_t CurPtr = 0;
569
570 // PE header is optional and is present only in executables. If it exists,
571 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000572 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000573
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000574 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000575 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000576 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
577 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000578 const auto *DH = reinterpret_cast<const dos_header *>(base());
579 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
580 CurPtr = DH->AddressOfNewExeHeader;
581 // Check the PE magic bytes. ("PE\0\0")
582 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
583 EC = object_error::parse_failed;
584 return;
585 }
586 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
587 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000588 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000589 }
590
Rafael Espindola48af1c22014-08-19 18:44:46 +0000591 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000592 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000593
594 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
595 // import libraries share a common prefix but bigobj is more restrictive.
596 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
597 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
598 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
599 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
600 return;
601
602 // Verify that we are dealing with bigobj.
603 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
604 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
605 sizeof(COFF::BigObjMagic)) == 0) {
606 COFFHeader = nullptr;
607 CurPtr += sizeof(coff_bigobj_file_header);
608 } else {
609 // It's not a bigobj.
610 COFFBigObjHeader = nullptr;
611 }
612 }
613 if (COFFHeader) {
614 // The prior checkSize call may have failed. This isn't a hard error
615 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000616 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000617 CurPtr += sizeof(coff_file_header);
618
619 if (COFFHeader->isImportLibrary())
620 return;
621 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000622
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000623 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000624 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000625 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000626 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000627
628 const uint8_t *DataDirAddr;
629 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000630 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000631 PE32Header = Header;
632 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
633 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000634 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000635 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
636 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
637 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
638 } else {
639 // It's neither PE32 nor PE32+.
640 EC = object_error::parse_failed;
641 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000642 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000643 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000644 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000645 CurPtr += COFFHeader->SizeOfOptionalHeader;
646 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000647
Rafael Espindola48af1c22014-08-19 18:44:46 +0000648 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000649 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000650 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000651
Rui Ueyamac2bed422013-09-27 21:04:00 +0000652 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000653 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000654 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000655 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000656 } else {
657 // We had better not have any symbols if we don't have a symbol table.
658 if (getNumberOfSymbols() != 0) {
659 EC = object_error::parse_failed;
660 return;
661 }
662 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000663
Rui Ueyamac2bed422013-09-27 21:04:00 +0000664 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000665 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000666 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000667 if ((EC = initDelayImportTablePtr()))
668 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000669
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000670 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000671 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000672 return;
673
Rui Ueyama74e85132014-11-19 00:18:07 +0000674 // Initialize the pointer to the base relocation table.
675 if ((EC = initBaseRelocPtr()))
676 return;
677
Rui Ueyama7d099192015-06-09 15:20:42 +0000678 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000679}
680
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000681basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000682 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000683 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000684 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000685}
686
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000687basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000688 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000689 DataRefImpl Ret;
690 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000691 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000692}
693
Rui Ueyamabc654b12013-09-27 21:47:05 +0000694import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000695 return import_directory_iterator(
696 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000697}
698
Rui Ueyamabc654b12013-09-27 21:47:05 +0000699import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000700 return import_directory_iterator(
701 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000702}
David Meyerc429b802012-03-01 22:19:54 +0000703
Rui Ueyama15d99352014-10-03 00:41:58 +0000704delay_import_directory_iterator
705COFFObjectFile::delay_import_directory_begin() const {
706 return delay_import_directory_iterator(
707 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
708}
709
710delay_import_directory_iterator
711COFFObjectFile::delay_import_directory_end() const {
712 return delay_import_directory_iterator(
713 DelayImportDirectoryEntryRef(
714 DelayImportDirectory, NumberOfDelayImportDirectory, this));
715}
716
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000717export_directory_iterator COFFObjectFile::export_directory_begin() const {
718 return export_directory_iterator(
719 ExportDirectoryEntryRef(ExportDirectory, 0, this));
720}
721
722export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000723 if (!ExportDirectory)
724 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000725 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000726 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000727 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000728}
729
Rafael Espindolab5155a52014-02-10 20:24:04 +0000730section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000731 DataRefImpl Ret;
732 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
733 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000734}
735
Rafael Espindolab5155a52014-02-10 20:24:04 +0000736section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000737 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000738 int NumSections =
739 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000740 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
741 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000742}
743
Rui Ueyama74e85132014-11-19 00:18:07 +0000744base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
745 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
746}
747
748base_reloc_iterator COFFObjectFile::base_reloc_end() const {
749 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
750}
751
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000752uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000753 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000754}
755
756StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000757 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000758 case COFF::IMAGE_FILE_MACHINE_I386:
759 return "COFF-i386";
760 case COFF::IMAGE_FILE_MACHINE_AMD64:
761 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000762 case COFF::IMAGE_FILE_MACHINE_ARMNT:
763 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000764 default:
765 return "COFF-<unknown arch>";
766 }
767}
768
769unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000770 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000771 case COFF::IMAGE_FILE_MACHINE_I386:
772 return Triple::x86;
773 case COFF::IMAGE_FILE_MACHINE_AMD64:
774 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000775 case COFF::IMAGE_FILE_MACHINE_ARMNT:
776 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000777 default:
778 return Triple::UnknownArch;
779 }
780}
781
Rui Ueyama979fb402014-10-09 02:16:38 +0000782iterator_range<import_directory_iterator>
783COFFObjectFile::import_directories() const {
784 return make_range(import_directory_begin(), import_directory_end());
785}
786
787iterator_range<delay_import_directory_iterator>
788COFFObjectFile::delay_import_directories() const {
789 return make_range(delay_import_directory_begin(),
790 delay_import_directory_end());
791}
792
793iterator_range<export_directory_iterator>
794COFFObjectFile::export_directories() const {
795 return make_range(export_directory_begin(), export_directory_end());
796}
797
Rui Ueyama74e85132014-11-19 00:18:07 +0000798iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
799 return make_range(base_reloc_begin(), base_reloc_end());
800}
801
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000802std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000803 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000804 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000805}
806
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000807std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000808COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
809 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000810 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000811}
812
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000813std::error_code
814COFFObjectFile::getDataDirectory(uint32_t Index,
815 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000816 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000817 if (!DataDirectory) {
818 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000819 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000820 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000821 assert(PE32Header || PE32PlusHeader);
822 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
823 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000824 if (Index >= NumEnt) {
825 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000826 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000827 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000828 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000829 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000830}
831
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000832std::error_code COFFObjectFile::getSection(int32_t Index,
833 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000834 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000835 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000836 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000837 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000838 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000839 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000840 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000841 }
842 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000843}
844
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000845std::error_code COFFObjectFile::getString(uint32_t Offset,
846 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000847 if (StringTableSize <= 4)
848 // Tried to get a string from an empty string table.
849 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000850 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000851 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000852 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000853 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000854}
855
David Majnemer44f51e52014-09-10 12:51:52 +0000856std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000857 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000858 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000859 if (Symbol.getStringTableOffset().Zeroes == 0) {
860 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000861 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000862 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000863 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000864 }
865
David Majnemer44f51e52014-09-10 12:51:52 +0000866 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000867 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000868 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000869 else
870 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000871 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000872 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000873}
874
David Majnemer44f51e52014-09-10 12:51:52 +0000875ArrayRef<uint8_t>
876COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000877 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000878
David Majnemer44f51e52014-09-10 12:51:52 +0000879 size_t SymbolSize = getSymbolTableEntrySize();
880 if (Symbol.getNumberOfAuxSymbols() > 0) {
881 // AUX data comes immediately after the symbol in COFF
882 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000883# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000884 // Verify that the Aux symbol points to a valid entry in the symbol table.
885 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000886 if (Offset < getPointerToSymbolTable() ||
887 Offset >=
888 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000889 report_fatal_error("Aux Symbol data was outside of symbol table.");
890
David Majnemer44f51e52014-09-10 12:51:52 +0000891 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
892 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000893# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000894 }
David Majnemer44f51e52014-09-10 12:51:52 +0000895 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000896}
897
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000898std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
899 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000900 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000901 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000902 // Null terminated, let ::strlen figure out the length.
903 Name = Sec->Name;
904 else
905 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000906 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000907
908 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000909 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000910 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000911 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000912 if (decodeBase64StringEntry(Name.substr(2), Offset))
913 return object_error::parse_failed;
914 } else {
915 if (Name.substr(1).getAsInteger(10, Offset))
916 return object_error::parse_failed;
917 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000918 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000919 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000920 }
921
922 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000923 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000924}
925
David Majnemera9ee5c02014-10-09 08:42:31 +0000926uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
927 // SizeOfRawData and VirtualSize change what they represent depending on
928 // whether or not we have an executable image.
929 //
930 // For object files, SizeOfRawData contains the size of section's data;
931 // VirtualSize is always zero.
932 //
933 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
934 // actual section size is in VirtualSize. It is possible for VirtualSize to
935 // be greater than SizeOfRawData; the contents past that point should be
936 // considered to be zero.
937 uint32_t SectionSize;
938 if (Sec->VirtualSize)
939 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
940 else
941 SectionSize = Sec->SizeOfRawData;
942
943 return SectionSize;
944}
945
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000946std::error_code
947COFFObjectFile::getSectionContents(const coff_section *Sec,
948 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000949 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
950 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000951 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
952 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000953 // The only thing that we need to verify is that the contents is contained
954 // within the file bounds. We don't need to make sure it doesn't cover other
955 // data, as there's nothing that says that is not allowed.
956 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000957 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000958 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000959 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000960 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000961 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000962}
963
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000964const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000965 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000966}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000967
Rafael Espindola5e812af2014-01-30 02:49:50 +0000968void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000969 Rel.p = reinterpret_cast<uintptr_t>(
970 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000971}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000972
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000973std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
974 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000975 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000976}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000977
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000978std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
979 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000980 const coff_relocation *R = toRel(Rel);
981 const support::ulittle32_t *VirtualAddressPtr;
982 if (std::error_code EC =
983 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
984 return EC;
985 Res = *VirtualAddressPtr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000986 return std::error_code();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000987}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000988
Rafael Espindola806f0062013-06-05 01:33:53 +0000989symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000990 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000991 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000992 if (R->SymbolTableIndex >= getNumberOfSymbols())
993 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000994 if (SymbolTable16)
995 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
996 else if (SymbolTable32)
997 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
998 else
David Majnemerc7353b52014-11-25 07:43:14 +0000999 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001000 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001001}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001002
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001003std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
1004 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001005 const coff_relocation* R = toRel(Rel);
1006 Res = R->Type;
Rui Ueyama7d099192015-06-09 15:20:42 +00001007 return std::error_code();
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001008}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001009
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001010const coff_section *
1011COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1012 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001013}
1014
David Majnemer44f51e52014-09-10 12:51:52 +00001015COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1016 if (SymbolTable16)
1017 return toSymb<coff_symbol16>(Ref);
1018 if (SymbolTable32)
1019 return toSymb<coff_symbol32>(Ref);
1020 llvm_unreachable("no symbol table pointer!");
1021}
1022
1023COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1024 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001025}
1026
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001027const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001028COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1029 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001030}
1031
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001032#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1033 case COFF::reloc_type: \
1034 Res = #reloc_type; \
1035 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001036
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001037std::error_code
1038COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1039 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001040 const coff_relocation *Reloc = toRel(Rel);
1041 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001042 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001043 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001044 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1062 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001063 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001064 }
1065 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001066 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1067 switch (Reloc->Type) {
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1083 default:
1084 Res = "Unknown";
1085 }
1086 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001087 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001088 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1096 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1098 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1099 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1100 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001101 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001102 }
1103 break;
1104 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001105 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001106 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001107 Result.append(Res.begin(), Res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +00001108 return std::error_code();
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001109}
1110
1111#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1112
Rafael Espindolac66d7612014-08-17 19:09:37 +00001113bool COFFObjectFile::isRelocatableObject() const {
1114 return !DataDirectory;
1115}
1116
Rui Ueyamac2bed422013-09-27 21:04:00 +00001117bool ImportDirectoryEntryRef::
1118operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001119 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001120}
1121
Rafael Espindola5e812af2014-01-30 02:49:50 +00001122void ImportDirectoryEntryRef::moveNext() {
1123 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001124}
1125
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001126std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1127 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001128 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001129 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001130}
1131
Rui Ueyama861021f2014-10-02 22:05:29 +00001132static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001133makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001134 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001135 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001136 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001137 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001138 }
1139 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001140 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001141}
1142
Rui Ueyama15d99352014-10-03 00:41:58 +00001143static imported_symbol_iterator
1144importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001145 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001146 Object->getRvaPtr(RVA, IntPtr);
1147 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001148}
1149
Rui Ueyama15d99352014-10-03 00:41:58 +00001150static imported_symbol_iterator
1151importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001152 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001153 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001154 // Forward the pointer to the last entry which is null.
1155 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001156 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001157 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1158 while (*Entry++)
1159 ++Index;
1160 } else {
1161 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1162 while (*Entry++)
1163 ++Index;
1164 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001165 return makeImportedSymbolIterator(Object, IntPtr, Index);
1166}
1167
1168imported_symbol_iterator
1169ImportDirectoryEntryRef::imported_symbol_begin() const {
1170 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1171 OwningObject);
1172}
1173
1174imported_symbol_iterator
1175ImportDirectoryEntryRef::imported_symbol_end() const {
1176 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1177 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001178}
1179
Rui Ueyama979fb402014-10-09 02:16:38 +00001180iterator_range<imported_symbol_iterator>
1181ImportDirectoryEntryRef::imported_symbols() const {
1182 return make_range(imported_symbol_begin(), imported_symbol_end());
1183}
1184
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001185std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001186 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001187 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001188 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001189 return EC;
1190 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001191 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001192}
1193
Rui Ueyama1e152d52014-10-02 17:02:18 +00001194std::error_code
1195ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1196 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001197 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001198}
1199
1200std::error_code
1201ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1202 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001203 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001204}
1205
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001206std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001207 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001208 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001209 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1210 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001211 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001212 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001213 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001214}
1215
Rui Ueyama15d99352014-10-03 00:41:58 +00001216bool DelayImportDirectoryEntryRef::
1217operator==(const DelayImportDirectoryEntryRef &Other) const {
1218 return Table == Other.Table && Index == Other.Index;
1219}
1220
1221void DelayImportDirectoryEntryRef::moveNext() {
1222 ++Index;
1223}
1224
1225imported_symbol_iterator
1226DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1227 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1228 OwningObject);
1229}
1230
1231imported_symbol_iterator
1232DelayImportDirectoryEntryRef::imported_symbol_end() const {
1233 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1234 OwningObject);
1235}
1236
Rui Ueyama979fb402014-10-09 02:16:38 +00001237iterator_range<imported_symbol_iterator>
1238DelayImportDirectoryEntryRef::imported_symbols() const {
1239 return make_range(imported_symbol_begin(), imported_symbol_end());
1240}
1241
Rui Ueyama15d99352014-10-03 00:41:58 +00001242std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1243 uintptr_t IntPtr = 0;
1244 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1245 return EC;
1246 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001247 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001248}
1249
Rui Ueyama1af08652014-10-03 18:07:18 +00001250std::error_code DelayImportDirectoryEntryRef::
1251getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1252 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001253 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001254}
1255
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001256std::error_code DelayImportDirectoryEntryRef::
1257getImportAddress(int AddrIndex, uint64_t &Result) const {
1258 uint32_t RVA = Table[Index].DelayImportAddressTable +
1259 AddrIndex * (OwningObject->is64() ? 8 : 4);
1260 uintptr_t IntPtr = 0;
1261 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1262 return EC;
1263 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001264 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001265 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001266 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001267 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001268}
1269
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001270bool ExportDirectoryEntryRef::
1271operator==(const ExportDirectoryEntryRef &Other) const {
1272 return ExportTable == Other.ExportTable && Index == Other.Index;
1273}
1274
Rafael Espindola5e812af2014-01-30 02:49:50 +00001275void ExportDirectoryEntryRef::moveNext() {
1276 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001277}
1278
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001279// Returns the name of the current export symbol. If the symbol is exported only
1280// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001281std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001282 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001283 if (std::error_code EC =
1284 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001285 return EC;
1286 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001287 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001288}
1289
Rui Ueyamae5df6092014-01-17 22:02:24 +00001290// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001291std::error_code
1292ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001293 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001294 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001295}
1296
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001297// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001298std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001299 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001300 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001301}
1302
1303// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001304std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001305 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001306 if (std::error_code EC =
1307 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001308 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001309 const export_address_table_entry *entry =
1310 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001311 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001312 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001313}
1314
1315// Returns the name of the current export symbol. If the symbol is exported only
1316// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001317std::error_code
1318ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001319 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001320 if (std::error_code EC =
1321 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001322 return EC;
1323 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1324
1325 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1326 int Offset = 0;
1327 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1328 I < E; ++I, ++Offset) {
1329 if (*I != Index)
1330 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001331 if (std::error_code EC =
1332 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001333 return EC;
1334 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001335 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001336 return EC;
1337 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001338 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001339 }
1340 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001341 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001342}
1343
Rui Ueyama861021f2014-10-02 22:05:29 +00001344bool ImportedSymbolRef::
1345operator==(const ImportedSymbolRef &Other) const {
1346 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1347 && Index == Other.Index;
1348}
1349
1350void ImportedSymbolRef::moveNext() {
1351 ++Index;
1352}
1353
1354std::error_code
1355ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1356 uint32_t RVA;
1357 if (Entry32) {
1358 // If a symbol is imported only by ordinal, it has no name.
1359 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001360 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001361 RVA = Entry32[Index].getHintNameRVA();
1362 } else {
1363 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001364 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001365 RVA = Entry64[Index].getHintNameRVA();
1366 }
1367 uintptr_t IntPtr = 0;
1368 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1369 return EC;
1370 // +2 because the first two bytes is hint.
1371 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001372 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001373}
1374
1375std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1376 uint32_t RVA;
1377 if (Entry32) {
1378 if (Entry32[Index].isOrdinal()) {
1379 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001380 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001381 }
1382 RVA = Entry32[Index].getHintNameRVA();
1383 } else {
1384 if (Entry64[Index].isOrdinal()) {
1385 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001386 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001387 }
1388 RVA = Entry64[Index].getHintNameRVA();
1389 }
1390 uintptr_t IntPtr = 0;
1391 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1392 return EC;
1393 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001394 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001395}
1396
Rafael Espindola437b0d52014-07-31 03:12:45 +00001397ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001398ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001399 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001400 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001401 if (EC)
1402 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001403 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001404}
Rui Ueyama74e85132014-11-19 00:18:07 +00001405
1406bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1407 return Header == Other.Header && Index == Other.Index;
1408}
1409
1410void BaseRelocRef::moveNext() {
1411 // Header->BlockSize is the size of the current block, including the
1412 // size of the header itself.
1413 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001414 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001415 if (Size == Header->BlockSize) {
1416 // .reloc contains a list of base relocation blocks. Each block
1417 // consists of the header followed by entries. The header contains
1418 // how many entories will follow. When we reach the end of the
1419 // current block, proceed to the next block.
1420 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1421 reinterpret_cast<const uint8_t *>(Header) + Size);
1422 Index = 0;
1423 } else {
1424 ++Index;
1425 }
1426}
1427
1428std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1429 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1430 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001431 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001432}
1433
1434std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1435 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1436 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001437 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001438}