blob: c497b13ac01212630cf49d7d2fccc8bcfed042bc [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
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000157 if (Symb.isAnyUndefined() || Symb.isCommon()) {
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 }
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000161
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000162 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000163 if (COFF::isReservedSectionNumber(SectionNumber)) {
164 Result = Symb.getValue();
Rui Ueyama7d099192015-06-09 15:20:42 +0000165 return std::error_code();
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000166 }
167
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000168 const coff_section *Section = nullptr;
169 if (std::error_code EC = getSection(SectionNumber, Section))
170 return EC;
171 Result = Section->VirtualAddress + Symb.getValue();
Rui Ueyama7d099192015-06-09 15:20:42 +0000172 return std::error_code();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000173}
174
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000175std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
176 SymbolRef::Type &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000177 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000178 int32_t SectionNumber = Symb.getSectionNumber();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000179 Result = SymbolRef::ST_Other;
David Majnemer44f51e52014-09-10 12:51:52 +0000180
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000181 if (Symb.isAnyUndefined()) {
David Meyer7e4b9762012-02-29 02:11:55 +0000182 Result = SymbolRef::ST_Unknown;
David Majnemer44f51e52014-09-10 12:51:52 +0000183 } else if (Symb.isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000184 Result = SymbolRef::ST_Function;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000185 } else if (Symb.isCommon()) {
186 Result = SymbolRef::ST_Data;
187 } else if (Symb.isFileRecord()) {
188 Result = SymbolRef::ST_File;
David Majnemer1a666e02015-03-07 20:21:27 +0000189 } else if (SectionNumber == COFF::IMAGE_SYM_DEBUG ||
190 Symb.isSectionDefinition()) {
191 // TODO: perhaps we need a new symbol type ST_Section.
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000192 Result = SymbolRef::ST_Debug;
193 } else if (!COFF::isReservedSectionNumber(SectionNumber)) {
194 const coff_section *Section = nullptr;
195 if (std::error_code EC = getSection(SectionNumber, Section))
196 return EC;
197 uint32_t Characteristics = Section->Characteristics;
198 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
199 Result = SymbolRef::ST_Function;
200 else if (Characteristics & (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
201 COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA))
Rui Ueyama5efa6652014-01-16 20:22:55 +0000202 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000203 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000204 return std::error_code();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000205}
206
Rafael Espindola20122a42014-01-31 20:57:12 +0000207uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000208 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000209 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000210
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000211 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000212 Result |= SymbolRef::SF_Global;
213
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000214 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000215 Result |= SymbolRef::SF_Weak;
216
David Majnemer44f51e52014-09-10 12:51:52 +0000217 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000218 Result |= SymbolRef::SF_Absolute;
219
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000220 if (Symb.isFileRecord())
221 Result |= SymbolRef::SF_FormatSpecific;
222
223 if (Symb.isSectionDefinition())
224 Result |= SymbolRef::SF_FormatSpecific;
225
226 if (Symb.isCommon())
227 Result |= SymbolRef::SF_Common;
228
229 if (Symb.isAnyUndefined())
230 Result |= SymbolRef::SF_Undefined;
231
Rafael Espindola20122a42014-01-31 20:57:12 +0000232 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000233}
234
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000235uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000236 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000237 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000238}
239
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000240std::error_code
241COFFObjectFile::getSymbolSection(DataRefImpl Ref,
242 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000243 COFFSymbolRef Symb = getCOFFSymbol(Ref);
244 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000245 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000246 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000247 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000248 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000249 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000250 DataRefImpl Ref;
251 Ref.p = reinterpret_cast<uintptr_t>(Sec);
252 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000253 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000254 return std::error_code();
Michael J. Spencer3217315392011-10-17 23:54:46 +0000255}
256
Rafael Espindola5e812af2014-01-30 02:49:50 +0000257void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000258 const coff_section *Sec = toSec(Ref);
259 Sec += 1;
260 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000261}
262
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000263std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
264 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000265 const coff_section *Sec = toSec(Ref);
266 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000267}
268
Rafael Espindola80291272014-10-08 15:28:58 +0000269uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000270 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000271 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000272}
273
Rafael Espindola80291272014-10-08 15:28:58 +0000274uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000275 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000276}
277
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000278std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
279 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000280 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000281 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000282 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000283 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
284 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000285}
286
Rafael Espindola80291272014-10-08 15:28:58 +0000287uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000288 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000289 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000290}
291
Rafael Espindola80291272014-10-08 15:28:58 +0000292bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000293 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000294 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000295}
296
Rafael Espindola80291272014-10-08 15:28:58 +0000297bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000298 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000299 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000300}
301
Rafael Espindola80291272014-10-08 15:28:58 +0000302bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000303 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000304 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
305 COFF::IMAGE_SCN_MEM_READ |
306 COFF::IMAGE_SCN_MEM_WRITE;
307 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000308}
309
Rafael Espindola80291272014-10-08 15:28:58 +0000310bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000311 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000312 // In COFF, a virtual section won't have any in-file
313 // content, so the file pointer to the content will be zero.
314 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000315}
316
Rafael Espindola80291272014-10-08 15:28:58 +0000317bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
318 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000319 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000320 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000321 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000322 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000323}
324
David Majnemere830c602014-11-13 08:46:37 +0000325static uint32_t getNumberOfRelocations(const coff_section *Sec,
326 MemoryBufferRef M, const uint8_t *base) {
327 // The field for the number of relocations in COFF section table is only
328 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
329 // NumberOfRelocations field, and the actual relocation count is stored in the
330 // VirtualAddress field in the first relocation entry.
331 if (Sec->hasExtendedRelocations()) {
332 const coff_relocation *FirstReloc;
333 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
334 base + Sec->PointerToRelocations)))
335 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000336 // -1 to exclude this first relocation entry.
337 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000338 }
339 return Sec->NumberOfRelocations;
340}
341
David Majnemer94751be2014-11-13 09:50:18 +0000342static const coff_relocation *
343getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
344 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
345 if (!NumRelocs)
346 return nullptr;
347 auto begin = reinterpret_cast<const coff_relocation *>(
348 Base + Sec->PointerToRelocations);
349 if (Sec->hasExtendedRelocations()) {
350 // Skip the first relocation entry repurposed to store the number of
351 // relocations.
352 begin++;
353 }
354 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
355 return nullptr;
356 return begin;
357}
358
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000359relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
360 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000361 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000362 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000363 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000364 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000365}
366
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000367relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
368 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000369 const coff_relocation *I = getFirstReloc(Sec, Data, base());
370 if (I)
371 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000372 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000373 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000374 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000375}
376
Rui Ueyamac2bed422013-09-27 21:04:00 +0000377// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000378std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000379 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000380 if (std::error_code EC = getObject(
381 SymbolTable16, Data, base() + getPointerToSymbolTable(),
382 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000383 return EC;
384
385 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000386 if (std::error_code EC = getObject(
387 SymbolTable32, Data, base() + getPointerToSymbolTable(),
388 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000389 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000390
391 // Find string table. The first four byte of the string table contains the
392 // total size of the string table, including the size field itself. If the
393 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000394 uint32_t StringTableOffset = getPointerToSymbolTable() +
395 getNumberOfSymbols() * getSymbolTableEntrySize();
396 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000397 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000398 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000399 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000400 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000401 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000402 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000404
Nico Rieck773a5792014-02-26 19:51:44 +0000405 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
406 // tools like cvtres write a size of 0 for an empty table instead of 4.
407 if (StringTableSize < 4)
408 StringTableSize = 4;
409
Rui Ueyamac2bed422013-09-27 21:04:00 +0000410 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000411 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000412 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000413 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000414}
415
Rui Ueyama215a5862014-02-20 06:51:07 +0000416// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000417std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000418 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
419 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000420 uint64_t Rva = Addr - ImageBase;
421 assert(Rva <= UINT32_MAX);
422 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000423}
424
Rui Ueyamac2bed422013-09-27 21:04:00 +0000425// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000426std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000427 for (const SectionRef &S : sections()) {
428 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429 uint32_t SectionStart = Section->VirtualAddress;
430 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000431 if (SectionStart <= Addr && Addr < SectionEnd) {
432 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000433 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000434 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000435 }
436 }
437 return object_error::parse_failed;
438}
439
440// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
441// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000442std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
443 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000444 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000445 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000446 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000447 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
448 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
449 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000450 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000451}
452
453// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000454std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000455 // First, we get the RVA of the import table. If the file lacks a pointer to
456 // the import table, do nothing.
457 const data_directory *DataEntry;
458 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000459 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000460
461 // Do nothing if the pointer to import table is NULL.
462 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000463 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000464
465 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000466 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000467 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000468 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000469
470 // Find the section that contains the RVA. This is needed because the RVA is
471 // the import table's memory address which is different from its file offset.
472 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000473 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000474 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000475 ImportDirectory = reinterpret_cast<
476 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000477 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000478}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000479
Rui Ueyama15d99352014-10-03 00:41:58 +0000480// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
481std::error_code COFFObjectFile::initDelayImportTablePtr() {
482 const data_directory *DataEntry;
483 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000484 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000485 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000486 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000487
488 uint32_t RVA = DataEntry->RelativeVirtualAddress;
489 NumberOfDelayImportDirectory = DataEntry->Size /
490 sizeof(delay_import_directory_table_entry) - 1;
491
492 uintptr_t IntPtr = 0;
493 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
494 return EC;
495 DelayImportDirectory = reinterpret_cast<
496 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000497 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000498}
499
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000500// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000501std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000502 // First, we get the RVA of the export table. If the file lacks a pointer to
503 // the export table, do nothing.
504 const data_directory *DataEntry;
505 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000506 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000507
508 // Do nothing if the pointer to export table is NULL.
509 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000510 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000511
512 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
513 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000514 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000515 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000516 ExportDirectory =
517 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000518 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000519}
520
Rui Ueyama74e85132014-11-19 00:18:07 +0000521std::error_code COFFObjectFile::initBaseRelocPtr() {
522 const data_directory *DataEntry;
523 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000524 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000525 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000526 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000527
528 uintptr_t IntPtr = 0;
529 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
530 return EC;
531 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
532 IntPtr);
533 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
534 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000535 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000536}
537
Rafael Espindola48af1c22014-08-19 18:44:46 +0000538COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
539 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000540 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
541 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
542 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
543 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000544 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000545 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
546 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000547 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000548 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000549 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000550
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000551 // The current location in the file where we are looking at.
552 uint64_t CurPtr = 0;
553
554 // PE header is optional and is present only in executables. If it exists,
555 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000556 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000557
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000558 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000559 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000560 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
561 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000562 const auto *DH = reinterpret_cast<const dos_header *>(base());
563 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
564 CurPtr = DH->AddressOfNewExeHeader;
565 // Check the PE magic bytes. ("PE\0\0")
566 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
567 EC = object_error::parse_failed;
568 return;
569 }
570 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
571 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000572 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000573 }
574
Rafael Espindola48af1c22014-08-19 18:44:46 +0000575 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000576 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000577
578 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
579 // import libraries share a common prefix but bigobj is more restrictive.
580 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
581 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
582 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
583 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
584 return;
585
586 // Verify that we are dealing with bigobj.
587 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
588 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
589 sizeof(COFF::BigObjMagic)) == 0) {
590 COFFHeader = nullptr;
591 CurPtr += sizeof(coff_bigobj_file_header);
592 } else {
593 // It's not a bigobj.
594 COFFBigObjHeader = nullptr;
595 }
596 }
597 if (COFFHeader) {
598 // The prior checkSize call may have failed. This isn't a hard error
599 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000600 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000601 CurPtr += sizeof(coff_file_header);
602
603 if (COFFHeader->isImportLibrary())
604 return;
605 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000606
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000607 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000608 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000609 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000610 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000611
612 const uint8_t *DataDirAddr;
613 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000614 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000615 PE32Header = Header;
616 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
617 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000618 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000619 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
620 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
621 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
622 } else {
623 // It's neither PE32 nor PE32+.
624 EC = object_error::parse_failed;
625 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000626 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000627 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000628 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000629 CurPtr += COFFHeader->SizeOfOptionalHeader;
630 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000631
Rafael Espindola48af1c22014-08-19 18:44:46 +0000632 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000633 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000634 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000635
Rui Ueyamac2bed422013-09-27 21:04:00 +0000636 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000637 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000638 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000639 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000640 } else {
641 // We had better not have any symbols if we don't have a symbol table.
642 if (getNumberOfSymbols() != 0) {
643 EC = object_error::parse_failed;
644 return;
645 }
646 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000647
Rui Ueyamac2bed422013-09-27 21:04:00 +0000648 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000649 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000650 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000651 if ((EC = initDelayImportTablePtr()))
652 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000653
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000654 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000655 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000656 return;
657
Rui Ueyama74e85132014-11-19 00:18:07 +0000658 // Initialize the pointer to the base relocation table.
659 if ((EC = initBaseRelocPtr()))
660 return;
661
Rui Ueyama7d099192015-06-09 15:20:42 +0000662 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000663}
664
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000665basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000666 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000667 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000668 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000669}
670
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000671basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000672 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000673 DataRefImpl Ret;
674 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000675 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000676}
677
Rui Ueyamabc654b12013-09-27 21:47:05 +0000678import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000679 return import_directory_iterator(
680 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000681}
682
Rui Ueyamabc654b12013-09-27 21:47:05 +0000683import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000684 return import_directory_iterator(
685 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000686}
David Meyerc429b802012-03-01 22:19:54 +0000687
Rui Ueyama15d99352014-10-03 00:41:58 +0000688delay_import_directory_iterator
689COFFObjectFile::delay_import_directory_begin() const {
690 return delay_import_directory_iterator(
691 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
692}
693
694delay_import_directory_iterator
695COFFObjectFile::delay_import_directory_end() const {
696 return delay_import_directory_iterator(
697 DelayImportDirectoryEntryRef(
698 DelayImportDirectory, NumberOfDelayImportDirectory, this));
699}
700
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000701export_directory_iterator COFFObjectFile::export_directory_begin() const {
702 return export_directory_iterator(
703 ExportDirectoryEntryRef(ExportDirectory, 0, this));
704}
705
706export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000707 if (!ExportDirectory)
708 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000709 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000710 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000711 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000712}
713
Rafael Espindolab5155a52014-02-10 20:24:04 +0000714section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000715 DataRefImpl Ret;
716 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
717 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000718}
719
Rafael Espindolab5155a52014-02-10 20:24:04 +0000720section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000721 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000722 int NumSections =
723 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000724 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
725 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000726}
727
Rui Ueyama74e85132014-11-19 00:18:07 +0000728base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
729 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
730}
731
732base_reloc_iterator COFFObjectFile::base_reloc_end() const {
733 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
734}
735
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000736uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000737 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000738}
739
740StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000741 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000742 case COFF::IMAGE_FILE_MACHINE_I386:
743 return "COFF-i386";
744 case COFF::IMAGE_FILE_MACHINE_AMD64:
745 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000746 case COFF::IMAGE_FILE_MACHINE_ARMNT:
747 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000748 default:
749 return "COFF-<unknown arch>";
750 }
751}
752
753unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000754 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000755 case COFF::IMAGE_FILE_MACHINE_I386:
756 return Triple::x86;
757 case COFF::IMAGE_FILE_MACHINE_AMD64:
758 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000759 case COFF::IMAGE_FILE_MACHINE_ARMNT:
760 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000761 default:
762 return Triple::UnknownArch;
763 }
764}
765
Rui Ueyama979fb402014-10-09 02:16:38 +0000766iterator_range<import_directory_iterator>
767COFFObjectFile::import_directories() const {
768 return make_range(import_directory_begin(), import_directory_end());
769}
770
771iterator_range<delay_import_directory_iterator>
772COFFObjectFile::delay_import_directories() const {
773 return make_range(delay_import_directory_begin(),
774 delay_import_directory_end());
775}
776
777iterator_range<export_directory_iterator>
778COFFObjectFile::export_directories() const {
779 return make_range(export_directory_begin(), export_directory_end());
780}
781
Rui Ueyama74e85132014-11-19 00:18:07 +0000782iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
783 return make_range(base_reloc_begin(), base_reloc_end());
784}
785
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000786std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000787 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000788 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000789}
790
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000791std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000792COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
793 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000794 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000795}
796
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000797std::error_code
798COFFObjectFile::getDataDirectory(uint32_t Index,
799 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000800 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000801 if (!DataDirectory) {
802 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000803 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000804 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000805 assert(PE32Header || PE32PlusHeader);
806 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
807 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000808 if (Index >= NumEnt) {
809 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000810 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000811 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000812 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000813 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000814}
815
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000816std::error_code COFFObjectFile::getSection(int32_t Index,
817 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000818 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000819 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000820 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000821 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000822 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000823 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000824 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000825 }
826 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000827}
828
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000829std::error_code COFFObjectFile::getString(uint32_t Offset,
830 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000831 if (StringTableSize <= 4)
832 // Tried to get a string from an empty string table.
833 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000834 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000835 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000836 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000837 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000838}
839
David Majnemer44f51e52014-09-10 12:51:52 +0000840std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000841 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000842 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000843 if (Symbol.getStringTableOffset().Zeroes == 0) {
844 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000845 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000846 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000847 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000848 }
849
David Majnemer44f51e52014-09-10 12:51:52 +0000850 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000851 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000852 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000853 else
854 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000855 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000856 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000857}
858
David Majnemer44f51e52014-09-10 12:51:52 +0000859ArrayRef<uint8_t>
860COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000861 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000862
David Majnemer44f51e52014-09-10 12:51:52 +0000863 size_t SymbolSize = getSymbolTableEntrySize();
864 if (Symbol.getNumberOfAuxSymbols() > 0) {
865 // AUX data comes immediately after the symbol in COFF
866 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000867# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000868 // Verify that the Aux symbol points to a valid entry in the symbol table.
869 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000870 if (Offset < getPointerToSymbolTable() ||
871 Offset >=
872 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000873 report_fatal_error("Aux Symbol data was outside of symbol table.");
874
David Majnemer44f51e52014-09-10 12:51:52 +0000875 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
876 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000877# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000878 }
David Majnemer44f51e52014-09-10 12:51:52 +0000879 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000880}
881
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000882std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
883 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000884 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000885 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000886 // Null terminated, let ::strlen figure out the length.
887 Name = Sec->Name;
888 else
889 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000890 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000891
892 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000893 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000894 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000895 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000896 if (decodeBase64StringEntry(Name.substr(2), Offset))
897 return object_error::parse_failed;
898 } else {
899 if (Name.substr(1).getAsInteger(10, Offset))
900 return object_error::parse_failed;
901 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000902 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000903 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000904 }
905
906 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000907 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000908}
909
David Majnemera9ee5c02014-10-09 08:42:31 +0000910uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
911 // SizeOfRawData and VirtualSize change what they represent depending on
912 // whether or not we have an executable image.
913 //
914 // For object files, SizeOfRawData contains the size of section's data;
915 // VirtualSize is always zero.
916 //
917 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
918 // actual section size is in VirtualSize. It is possible for VirtualSize to
919 // be greater than SizeOfRawData; the contents past that point should be
920 // considered to be zero.
921 uint32_t SectionSize;
922 if (Sec->VirtualSize)
923 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
924 else
925 SectionSize = Sec->SizeOfRawData;
926
927 return SectionSize;
928}
929
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000930std::error_code
931COFFObjectFile::getSectionContents(const coff_section *Sec,
932 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000933 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
934 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000935 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
936 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000937 // The only thing that we need to verify is that the contents is contained
938 // within the file bounds. We don't need to make sure it doesn't cover other
939 // data, as there's nothing that says that is not allowed.
940 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000941 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000942 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000943 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000944 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000945 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000946}
947
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000948const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000949 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000950}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000951
Rafael Espindola5e812af2014-01-30 02:49:50 +0000952void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000953 Rel.p = reinterpret_cast<uintptr_t>(
954 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000955}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000956
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000957std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
958 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000959 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000960}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000961
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000962std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
963 uint64_t &Res) const {
David Majnemer58323a92014-11-13 07:42:07 +0000964 const coff_relocation *R = toRel(Rel);
965 const support::ulittle32_t *VirtualAddressPtr;
966 if (std::error_code EC =
967 getObject(VirtualAddressPtr, Data, &R->VirtualAddress))
968 return EC;
969 Res = *VirtualAddressPtr;
Rui Ueyama7d099192015-06-09 15:20:42 +0000970 return std::error_code();
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000971}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000972
Rafael Espindola806f0062013-06-05 01:33:53 +0000973symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000974 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000975 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000976 if (R->SymbolTableIndex >= getNumberOfSymbols())
977 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000978 if (SymbolTable16)
979 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
980 else if (SymbolTable32)
981 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
982 else
David Majnemerc7353b52014-11-25 07:43:14 +0000983 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000984 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000985}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000986
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000987std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
988 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000989 const coff_relocation* R = toRel(Rel);
990 Res = R->Type;
Rui Ueyama7d099192015-06-09 15:20:42 +0000991 return std::error_code();
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000992}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000993
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000994const coff_section *
995COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
996 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000997}
998
David Majnemer44f51e52014-09-10 12:51:52 +0000999COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1000 if (SymbolTable16)
1001 return toSymb<coff_symbol16>(Ref);
1002 if (SymbolTable32)
1003 return toSymb<coff_symbol32>(Ref);
1004 llvm_unreachable("no symbol table pointer!");
1005}
1006
1007COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1008 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001009}
1010
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001011const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001012COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1013 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001014}
1015
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001016#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1017 case COFF::reloc_type: \
1018 Res = #reloc_type; \
1019 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001020
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001021std::error_code
1022COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
1023 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001024 const coff_relocation *Reloc = toRel(Rel);
1025 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001026 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001027 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001028 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001029 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1030 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1031 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1032 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1033 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1034 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1035 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1036 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1037 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1038 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1039 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1040 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1041 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1042 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1046 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001047 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001048 }
1049 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001050 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1051 switch (Reloc->Type) {
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1060 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1061 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1062 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1063 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1064 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1065 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1067 default:
1068 Res = "Unknown";
1069 }
1070 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001071 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001072 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1081 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1082 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1083 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1084 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001085 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001086 }
1087 break;
1088 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001089 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001090 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001091 Result.append(Res.begin(), Res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +00001092 return std::error_code();
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001093}
1094
1095#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1096
Rafael Espindolac66d7612014-08-17 19:09:37 +00001097bool COFFObjectFile::isRelocatableObject() const {
1098 return !DataDirectory;
1099}
1100
Rui Ueyamac2bed422013-09-27 21:04:00 +00001101bool ImportDirectoryEntryRef::
1102operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001103 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001104}
1105
Rafael Espindola5e812af2014-01-30 02:49:50 +00001106void ImportDirectoryEntryRef::moveNext() {
1107 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001108}
1109
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001110std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1111 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001112 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001113 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001114}
1115
Rui Ueyama861021f2014-10-02 22:05:29 +00001116static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001117makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001118 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001119 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001120 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001121 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001122 }
1123 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001124 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001125}
1126
Rui Ueyama15d99352014-10-03 00:41:58 +00001127static imported_symbol_iterator
1128importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001129 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001130 Object->getRvaPtr(RVA, IntPtr);
1131 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001132}
1133
Rui Ueyama15d99352014-10-03 00:41:58 +00001134static imported_symbol_iterator
1135importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001136 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001137 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001138 // Forward the pointer to the last entry which is null.
1139 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001140 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001141 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1142 while (*Entry++)
1143 ++Index;
1144 } else {
1145 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1146 while (*Entry++)
1147 ++Index;
1148 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001149 return makeImportedSymbolIterator(Object, IntPtr, Index);
1150}
1151
1152imported_symbol_iterator
1153ImportDirectoryEntryRef::imported_symbol_begin() const {
1154 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1155 OwningObject);
1156}
1157
1158imported_symbol_iterator
1159ImportDirectoryEntryRef::imported_symbol_end() const {
1160 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1161 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001162}
1163
Rui Ueyama979fb402014-10-09 02:16:38 +00001164iterator_range<imported_symbol_iterator>
1165ImportDirectoryEntryRef::imported_symbols() const {
1166 return make_range(imported_symbol_begin(), imported_symbol_end());
1167}
1168
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001169std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001170 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001171 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001172 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001173 return EC;
1174 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001175 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001176}
1177
Rui Ueyama1e152d52014-10-02 17:02:18 +00001178std::error_code
1179ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1180 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001181 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001182}
1183
1184std::error_code
1185ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1186 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001187 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001188}
1189
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001190std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001191 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001192 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001193 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1194 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001195 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001196 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001197 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001198}
1199
Rui Ueyama15d99352014-10-03 00:41:58 +00001200bool DelayImportDirectoryEntryRef::
1201operator==(const DelayImportDirectoryEntryRef &Other) const {
1202 return Table == Other.Table && Index == Other.Index;
1203}
1204
1205void DelayImportDirectoryEntryRef::moveNext() {
1206 ++Index;
1207}
1208
1209imported_symbol_iterator
1210DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1211 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1212 OwningObject);
1213}
1214
1215imported_symbol_iterator
1216DelayImportDirectoryEntryRef::imported_symbol_end() const {
1217 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1218 OwningObject);
1219}
1220
Rui Ueyama979fb402014-10-09 02:16:38 +00001221iterator_range<imported_symbol_iterator>
1222DelayImportDirectoryEntryRef::imported_symbols() const {
1223 return make_range(imported_symbol_begin(), imported_symbol_end());
1224}
1225
Rui Ueyama15d99352014-10-03 00:41:58 +00001226std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1227 uintptr_t IntPtr = 0;
1228 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1229 return EC;
1230 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001231 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001232}
1233
Rui Ueyama1af08652014-10-03 18:07:18 +00001234std::error_code DelayImportDirectoryEntryRef::
1235getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1236 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001237 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001238}
1239
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001240std::error_code DelayImportDirectoryEntryRef::
1241getImportAddress(int AddrIndex, uint64_t &Result) const {
1242 uint32_t RVA = Table[Index].DelayImportAddressTable +
1243 AddrIndex * (OwningObject->is64() ? 8 : 4);
1244 uintptr_t IntPtr = 0;
1245 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1246 return EC;
1247 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001248 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001249 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001250 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001251 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001252}
1253
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001254bool ExportDirectoryEntryRef::
1255operator==(const ExportDirectoryEntryRef &Other) const {
1256 return ExportTable == Other.ExportTable && Index == Other.Index;
1257}
1258
Rafael Espindola5e812af2014-01-30 02:49:50 +00001259void ExportDirectoryEntryRef::moveNext() {
1260 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001261}
1262
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001263// Returns the name of the current export symbol. If the symbol is exported only
1264// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001265std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001266 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001267 if (std::error_code EC =
1268 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001269 return EC;
1270 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001271 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001272}
1273
Rui Ueyamae5df6092014-01-17 22:02:24 +00001274// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001275std::error_code
1276ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001277 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001278 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001279}
1280
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001281// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001282std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001283 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001284 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001285}
1286
1287// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001288std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001289 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001290 if (std::error_code EC =
1291 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001292 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001293 const export_address_table_entry *entry =
1294 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001295 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001296 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001297}
1298
1299// Returns the name of the current export symbol. If the symbol is exported only
1300// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001301std::error_code
1302ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001303 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001304 if (std::error_code EC =
1305 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001306 return EC;
1307 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1308
1309 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1310 int Offset = 0;
1311 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1312 I < E; ++I, ++Offset) {
1313 if (*I != Index)
1314 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001315 if (std::error_code EC =
1316 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001317 return EC;
1318 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001319 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001320 return EC;
1321 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001322 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001323 }
1324 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001325 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001326}
1327
Rui Ueyama861021f2014-10-02 22:05:29 +00001328bool ImportedSymbolRef::
1329operator==(const ImportedSymbolRef &Other) const {
1330 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1331 && Index == Other.Index;
1332}
1333
1334void ImportedSymbolRef::moveNext() {
1335 ++Index;
1336}
1337
1338std::error_code
1339ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1340 uint32_t RVA;
1341 if (Entry32) {
1342 // If a symbol is imported only by ordinal, it has no name.
1343 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001344 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001345 RVA = Entry32[Index].getHintNameRVA();
1346 } else {
1347 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001348 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001349 RVA = Entry64[Index].getHintNameRVA();
1350 }
1351 uintptr_t IntPtr = 0;
1352 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1353 return EC;
1354 // +2 because the first two bytes is hint.
1355 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001356 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001357}
1358
1359std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1360 uint32_t RVA;
1361 if (Entry32) {
1362 if (Entry32[Index].isOrdinal()) {
1363 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001364 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001365 }
1366 RVA = Entry32[Index].getHintNameRVA();
1367 } else {
1368 if (Entry64[Index].isOrdinal()) {
1369 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001370 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001371 }
1372 RVA = Entry64[Index].getHintNameRVA();
1373 }
1374 uintptr_t IntPtr = 0;
1375 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1376 return EC;
1377 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001378 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001379}
1380
Rafael Espindola437b0d52014-07-31 03:12:45 +00001381ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001382ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001383 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001384 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001385 if (EC)
1386 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001387 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001388}
Rui Ueyama74e85132014-11-19 00:18:07 +00001389
1390bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1391 return Header == Other.Header && Index == Other.Index;
1392}
1393
1394void BaseRelocRef::moveNext() {
1395 // Header->BlockSize is the size of the current block, including the
1396 // size of the header itself.
1397 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001398 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001399 if (Size == Header->BlockSize) {
1400 // .reloc contains a list of base relocation blocks. Each block
1401 // consists of the header followed by entries. The header contains
1402 // how many entories will follow. When we reach the end of the
1403 // current block, proceed to the next block.
1404 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1405 reinterpret_cast<const uint8_t *>(Header) + Size);
1406 Index = 0;
1407 } else {
1408 ++Index;
1409 }
1410}
1411
1412std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1413 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1414 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001415 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001416}
1417
1418std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1419 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1420 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001421 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001422}