blob: fa6aa4ced392572ae740cf4cfe18b22f3c195798 [file] [log] [blame]
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencere5fd0042011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyama6a75acb2015-06-25 00:07:39 +000019#include "llvm/ADT/iterator_range.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000020#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000023#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000024#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000025
26using namespace llvm;
27using namespace object;
28
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000029using support::ulittle16_t;
30using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000031using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000033
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000034// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000035static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000036 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000037 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000038 return false;
39 }
40 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000041}
42
David Majnemere830c602014-11-13 08:46:37 +000043static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
David Majnemer94751be2014-11-13 09:50:18 +000044 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000045 if (Addr + Size < Addr || Addr + Size < Size ||
46 Addr + Size > uintptr_t(M.getBufferEnd()) ||
47 Addr < uintptr_t(M.getBufferStart())) {
48 return object_error::unexpected_eof;
49 }
Rui Ueyama7d099192015-06-09 15:20:42 +000050 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000051}
52
Rui Ueyamaed64342b2013-07-19 23:23:29 +000053// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
54// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000055template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000056static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000057 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000058 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000059 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000060 if (std::error_code EC = checkOffset(M, Addr, Size))
61 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000062 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyama7d099192015-06-09 15:20:42 +000063 return std::error_code();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000064}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000065
Nico Rieck9d2c15e2014-02-22 16:12:20 +000066// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
67// prefixed slashes.
68static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
69 assert(Str.size() <= 6 && "String too long, possible overflow.");
70 if (Str.size() > 6)
71 return true;
72
73 uint64_t Value = 0;
74 while (!Str.empty()) {
75 unsigned CharVal;
76 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
77 CharVal = Str[0] - 'A';
78 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
79 CharVal = Str[0] - 'a' + 26;
80 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
81 CharVal = Str[0] - '0' + 52;
82 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000083 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000084 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000085 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000086 else
87 return true;
88
89 Value = (Value * 64) + CharVal;
90 Str = Str.substr(1);
91 }
92
93 if (Value > std::numeric_limits<uint32_t>::max())
94 return true;
95
96 Result = static_cast<uint32_t>(Value);
97 return false;
98}
99
David Majnemer44f51e52014-09-10 12:51:52 +0000100template <typename coff_symbol_type>
101const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
102 const coff_symbol_type *Addr =
103 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000104
David Majnemer236b0ca2014-11-17 11:17:17 +0000105 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000106#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000107 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000108 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000109
David Majnemer44f51e52014-09-10 12:51:52 +0000110 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
111 "Symbol did not point to the beginning of a symbol");
112#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000114 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000115}
116
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000117const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
118 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000119
120# ifndef NDEBUG
121 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000122 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000123 report_fatal_error("Section was outside of section table.");
124
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000125 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
126 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000127 "Section did not point to the beginning of a section");
128# endif
129
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000130 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000131}
132
Rafael Espindola5e812af2014-01-30 02:49:50 +0000133void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000134 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000135 if (SymbolTable16) {
136 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
137 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000138 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000139 } else if (SymbolTable32) {
140 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
141 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000142 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000143 } else {
144 llvm_unreachable("no symbol table pointer!");
145 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000146}
147
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000148ErrorOr<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000150 StringRef Result;
151 std::error_code EC = getSymbolName(Symb, Result);
152 if (EC)
153 return EC;
154 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000155}
156
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000157uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
158 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000159}
160
Rafael Espindolaed067c42015-07-03 18:19:00 +0000161ErrorOr<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
162 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000163 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000164 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000165
166 if (Symb.isAnyUndefined() || Symb.isCommon() ||
167 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000168 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000169
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000170 const coff_section *Section = nullptr;
171 if (std::error_code EC = getSection(SectionNumber, Section))
172 return EC;
Rafael Espindola991af662015-06-24 19:11:10 +0000173 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000174
175 // The section VirtualAddress does not include ImageBase, and we want to
176 // return virtual addresses.
177 if (PE32Header)
178 Result += PE32Header->ImageBase;
179 else if (PE32PlusHeader)
David Majnemer7c6a0712015-07-31 17:40:24 +0000180 Result += PE32PlusHeader->ImageBase;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000181
Rafael Espindolaed067c42015-07-03 18:19:00 +0000182 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000183}
184
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000185SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000186 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000187 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000188
Peter Collingbournee834f422015-08-06 05:26:35 +0000189 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
190 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000191 if (Symb.isAnyUndefined())
192 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000193 if (Symb.isCommon())
194 return SymbolRef::ST_Data;
195 if (Symb.isFileRecord())
196 return SymbolRef::ST_File;
197
198 // TODO: perhaps we need a new symbol type ST_Section.
199 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
200 return SymbolRef::ST_Debug;
201
202 if (!COFF::isReservedSectionNumber(SectionNumber))
203 return SymbolRef::ST_Data;
204
205 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000206}
207
Rafael Espindola20122a42014-01-31 20:57:12 +0000208uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000209 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000210 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000211
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000212 if (Symb.isExternal() || Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000213 Result |= SymbolRef::SF_Global;
214
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000215 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000216 Result |= SymbolRef::SF_Weak;
217
David Majnemer44f51e52014-09-10 12:51:52 +0000218 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000219 Result |= SymbolRef::SF_Absolute;
220
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000221 if (Symb.isFileRecord())
222 Result |= SymbolRef::SF_FormatSpecific;
223
224 if (Symb.isSectionDefinition())
225 Result |= SymbolRef::SF_FormatSpecific;
226
227 if (Symb.isCommon())
228 Result |= SymbolRef::SF_Common;
229
230 if (Symb.isAnyUndefined())
231 Result |= SymbolRef::SF_Undefined;
232
Rafael Espindola20122a42014-01-31 20:57:12 +0000233 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000234}
235
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000236uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000237 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000238 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000239}
240
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000241std::error_code
242COFFObjectFile::getSymbolSection(DataRefImpl Ref,
243 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000244 COFFSymbolRef Symb = getCOFFSymbol(Ref);
245 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000246 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000247 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000248 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000249 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000250 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000251 DataRefImpl Ref;
252 Ref.p = reinterpret_cast<uintptr_t>(Sec);
253 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000254 }
Rui Ueyama7d099192015-06-09 15:20:42 +0000255 return std::error_code();
Michael J. Spencer3217315392011-10-17 23:54:46 +0000256}
257
Rafael Espindola6bf32212015-06-24 19:57:32 +0000258unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
259 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
260 return Symb.getSectionNumber();
261}
262
Rafael Espindola5e812af2014-01-30 02:49:50 +0000263void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000264 const coff_section *Sec = toSec(Ref);
265 Sec += 1;
266 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000267}
268
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000269std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
270 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000271 const coff_section *Sec = toSec(Ref);
272 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000273}
274
Rafael Espindola80291272014-10-08 15:28:58 +0000275uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000276 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000277 uint64_t Result = Sec->VirtualAddress;
278
279 // The section VirtualAddress does not include ImageBase, and we want to
280 // return virtual addresses.
281 if (PE32Header)
282 Result += PE32Header->ImageBase;
283 else if (PE32PlusHeader)
284 Result += PE32PlusHeader->ImageBase;
285 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000286}
287
Rafael Espindola80291272014-10-08 15:28:58 +0000288uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000289 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000290}
291
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000292std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
293 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000294 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000295 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000296 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000297 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
298 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000299}
300
Rafael Espindola80291272014-10-08 15:28:58 +0000301uint64_t COFFObjectFile::getSectionAlignment(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 uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000304}
305
Rafael Espindola80291272014-10-08 15:28:58 +0000306bool COFFObjectFile::isSectionText(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_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000309}
310
Rafael Espindola80291272014-10-08 15:28:58 +0000311bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000312 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000313 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000314}
315
Rafael Espindola80291272014-10-08 15:28:58 +0000316bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000317 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000318 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
319 COFF::IMAGE_SCN_MEM_READ |
320 COFF::IMAGE_SCN_MEM_WRITE;
321 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000322}
323
Rafael Espindola6bf32212015-06-24 19:57:32 +0000324unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
325 uintptr_t Offset =
326 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
327 assert((Offset % sizeof(coff_section)) == 0);
328 return (Offset / sizeof(coff_section)) + 1;
329}
330
Rafael Espindola80291272014-10-08 15:28:58 +0000331bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000332 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000333 // In COFF, a virtual section won't have any in-file
334 // content, so the file pointer to the content will be zero.
335 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000336}
337
David Majnemere830c602014-11-13 08:46:37 +0000338static uint32_t getNumberOfRelocations(const coff_section *Sec,
339 MemoryBufferRef M, const uint8_t *base) {
340 // The field for the number of relocations in COFF section table is only
341 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
342 // NumberOfRelocations field, and the actual relocation count is stored in the
343 // VirtualAddress field in the first relocation entry.
344 if (Sec->hasExtendedRelocations()) {
345 const coff_relocation *FirstReloc;
346 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
347 base + Sec->PointerToRelocations)))
348 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000349 // -1 to exclude this first relocation entry.
350 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000351 }
352 return Sec->NumberOfRelocations;
353}
354
David Majnemer94751be2014-11-13 09:50:18 +0000355static const coff_relocation *
356getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
357 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
358 if (!NumRelocs)
359 return nullptr;
360 auto begin = reinterpret_cast<const coff_relocation *>(
361 Base + Sec->PointerToRelocations);
362 if (Sec->hasExtendedRelocations()) {
363 // Skip the first relocation entry repurposed to store the number of
364 // relocations.
365 begin++;
366 }
367 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
368 return nullptr;
369 return begin;
370}
371
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000372relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
373 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000374 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000375 if (begin && Sec->VirtualAddress != 0)
376 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000377 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000378 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000379 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000380}
381
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000382relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
383 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000384 const coff_relocation *I = getFirstReloc(Sec, Data, base());
385 if (I)
386 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000387 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000388 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000390}
391
Rui Ueyamac2bed422013-09-27 21:04:00 +0000392// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000393std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000394 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000395 if (std::error_code EC = getObject(
396 SymbolTable16, Data, base() + getPointerToSymbolTable(),
397 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000398 return EC;
399
400 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000401 if (std::error_code EC = getObject(
402 SymbolTable32, Data, base() + getPointerToSymbolTable(),
403 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000404 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000405
406 // Find string table. The first four byte of the string table contains the
407 // total size of the string table, including the size field itself. If the
408 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000409 uint32_t StringTableOffset = getPointerToSymbolTable() +
410 getNumberOfSymbols() * getSymbolTableEntrySize();
411 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000412 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000413 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000414 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000415 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000416 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000417 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000418 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000419
Nico Rieck773a5792014-02-26 19:51:44 +0000420 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
421 // tools like cvtres write a size of 0 for an empty table instead of 4.
422 if (StringTableSize < 4)
423 StringTableSize = 4;
424
Rui Ueyamac2bed422013-09-27 21:04:00 +0000425 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000426 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000427 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000428 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429}
430
Rui Ueyama215a5862014-02-20 06:51:07 +0000431// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000432std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000433 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
434 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000435 uint64_t Rva = Addr - ImageBase;
436 assert(Rva <= UINT32_MAX);
437 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000438}
439
Rui Ueyamac2bed422013-09-27 21:04:00 +0000440// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000441std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000442 for (const SectionRef &S : sections()) {
443 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000444 uint32_t SectionStart = Section->VirtualAddress;
445 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000446 if (SectionStart <= Addr && Addr < SectionEnd) {
447 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000448 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000449 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000450 }
451 }
452 return object_error::parse_failed;
453}
454
455// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
456// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000457std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
458 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000459 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000460 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000461 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
463 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
464 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000465 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466}
467
468// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000469std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000470 // First, we get the RVA of the import table. If the file lacks a pointer to
471 // the import table, do nothing.
472 const data_directory *DataEntry;
473 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000474 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000475
476 // Do nothing if the pointer to import table is NULL.
477 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000478 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000479
480 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000481 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000482 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000483 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000484
485 // Find the section that contains the RVA. This is needed because the RVA is
486 // the import table's memory address which is different from its file offset.
487 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000488 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000489 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000490 ImportDirectory = reinterpret_cast<
491 const import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000492 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000493}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000494
Rui Ueyama15d99352014-10-03 00:41:58 +0000495// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
496std::error_code COFFObjectFile::initDelayImportTablePtr() {
497 const data_directory *DataEntry;
498 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000499 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000500 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000501 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000502
503 uint32_t RVA = DataEntry->RelativeVirtualAddress;
504 NumberOfDelayImportDirectory = DataEntry->Size /
505 sizeof(delay_import_directory_table_entry) - 1;
506
507 uintptr_t IntPtr = 0;
508 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
509 return EC;
510 DelayImportDirectory = reinterpret_cast<
511 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000512 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000513}
514
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000515// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000516std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000517 // First, we get the RVA of the export table. If the file lacks a pointer to
518 // the export table, do nothing.
519 const data_directory *DataEntry;
520 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000521 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000522
523 // Do nothing if the pointer to export table is NULL.
524 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000525 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000526
527 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
528 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000529 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000530 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000531 ExportDirectory =
532 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000533 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000534}
535
Rui Ueyama74e85132014-11-19 00:18:07 +0000536std::error_code COFFObjectFile::initBaseRelocPtr() {
537 const data_directory *DataEntry;
538 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000539 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000540 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000541 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000542
543 uintptr_t IntPtr = 0;
544 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
545 return EC;
546 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
547 IntPtr);
548 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
549 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000550 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000551}
552
Rafael Espindola48af1c22014-08-19 18:44:46 +0000553COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
554 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000555 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
556 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
557 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
558 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000559 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Rui Ueyama74e85132014-11-19 00:18:07 +0000560 ExportDirectory(nullptr), BaseRelocHeader(nullptr),
561 BaseRelocEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000562 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000563 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000564 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000565
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000566 // The current location in the file where we are looking at.
567 uint64_t CurPtr = 0;
568
569 // PE header is optional and is present only in executables. If it exists,
570 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000571 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000572
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000573 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000574 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000575 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
576 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000577 const auto *DH = reinterpret_cast<const dos_header *>(base());
578 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
579 CurPtr = DH->AddressOfNewExeHeader;
580 // Check the PE magic bytes. ("PE\0\0")
581 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
582 EC = object_error::parse_failed;
583 return;
584 }
585 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
586 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000587 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000588 }
589
Rafael Espindola48af1c22014-08-19 18:44:46 +0000590 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000591 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000592
593 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
594 // import libraries share a common prefix but bigobj is more restrictive.
595 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
596 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
597 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
598 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
599 return;
600
601 // Verify that we are dealing with bigobj.
602 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
603 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
604 sizeof(COFF::BigObjMagic)) == 0) {
605 COFFHeader = nullptr;
606 CurPtr += sizeof(coff_bigobj_file_header);
607 } else {
608 // It's not a bigobj.
609 COFFBigObjHeader = nullptr;
610 }
611 }
612 if (COFFHeader) {
613 // The prior checkSize call may have failed. This isn't a hard error
614 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000615 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000616 CurPtr += sizeof(coff_file_header);
617
618 if (COFFHeader->isImportLibrary())
619 return;
620 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000621
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000622 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000623 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000624 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000625 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000626
627 const uint8_t *DataDirAddr;
628 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000629 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000630 PE32Header = Header;
631 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
632 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000633 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000634 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
635 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
636 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
637 } else {
638 // It's neither PE32 nor PE32+.
639 EC = object_error::parse_failed;
640 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000641 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000642 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000643 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000644 CurPtr += COFFHeader->SizeOfOptionalHeader;
645 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000646
Rafael Espindola48af1c22014-08-19 18:44:46 +0000647 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000648 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000649 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000650
Rui Ueyamac2bed422013-09-27 21:04:00 +0000651 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000652 if (getPointerToSymbolTable() != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000653 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000654 return;
David Majnemer236b0ca2014-11-17 11:17:17 +0000655 } else {
656 // We had better not have any symbols if we don't have a symbol table.
657 if (getNumberOfSymbols() != 0) {
658 EC = object_error::parse_failed;
659 return;
660 }
661 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000662
Rui Ueyamac2bed422013-09-27 21:04:00 +0000663 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000664 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000665 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000666 if ((EC = initDelayImportTablePtr()))
667 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000668
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000669 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000670 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000671 return;
672
Rui Ueyama74e85132014-11-19 00:18:07 +0000673 // Initialize the pointer to the base relocation table.
674 if ((EC = initBaseRelocPtr()))
675 return;
676
Rui Ueyama7d099192015-06-09 15:20:42 +0000677 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000678}
679
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000680basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000681 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000682 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000683 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000684}
685
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000686basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000687 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000688 DataRefImpl Ret;
689 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000690 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000691}
692
Rui Ueyamabc654b12013-09-27 21:47:05 +0000693import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000694 return import_directory_iterator(
695 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000696}
697
Rui Ueyamabc654b12013-09-27 21:47:05 +0000698import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000699 return import_directory_iterator(
700 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000701}
David Meyerc429b802012-03-01 22:19:54 +0000702
Rui Ueyama15d99352014-10-03 00:41:58 +0000703delay_import_directory_iterator
704COFFObjectFile::delay_import_directory_begin() const {
705 return delay_import_directory_iterator(
706 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
707}
708
709delay_import_directory_iterator
710COFFObjectFile::delay_import_directory_end() const {
711 return delay_import_directory_iterator(
712 DelayImportDirectoryEntryRef(
713 DelayImportDirectory, NumberOfDelayImportDirectory, this));
714}
715
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000716export_directory_iterator COFFObjectFile::export_directory_begin() const {
717 return export_directory_iterator(
718 ExportDirectoryEntryRef(ExportDirectory, 0, this));
719}
720
721export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000722 if (!ExportDirectory)
723 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000724 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000725 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000726 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000727}
728
Rafael Espindolab5155a52014-02-10 20:24:04 +0000729section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000730 DataRefImpl Ret;
731 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
732 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000733}
734
Rafael Espindolab5155a52014-02-10 20:24:04 +0000735section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000736 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000737 int NumSections =
738 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000739 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
740 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000741}
742
Rui Ueyama74e85132014-11-19 00:18:07 +0000743base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
744 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
745}
746
747base_reloc_iterator COFFObjectFile::base_reloc_end() const {
748 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
749}
750
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000751uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000752 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000753}
754
755StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000756 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000757 case COFF::IMAGE_FILE_MACHINE_I386:
758 return "COFF-i386";
759 case COFF::IMAGE_FILE_MACHINE_AMD64:
760 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000761 case COFF::IMAGE_FILE_MACHINE_ARMNT:
762 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000763 case COFF::IMAGE_FILE_MACHINE_ARM64:
764 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000765 default:
766 return "COFF-<unknown arch>";
767 }
768}
769
770unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000771 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000772 case COFF::IMAGE_FILE_MACHINE_I386:
773 return Triple::x86;
774 case COFF::IMAGE_FILE_MACHINE_AMD64:
775 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000776 case COFF::IMAGE_FILE_MACHINE_ARMNT:
777 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000778 case COFF::IMAGE_FILE_MACHINE_ARM64:
779 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000780 default:
781 return Triple::UnknownArch;
782 }
783}
784
Rui Ueyama979fb402014-10-09 02:16:38 +0000785iterator_range<import_directory_iterator>
786COFFObjectFile::import_directories() const {
787 return make_range(import_directory_begin(), import_directory_end());
788}
789
790iterator_range<delay_import_directory_iterator>
791COFFObjectFile::delay_import_directories() const {
792 return make_range(delay_import_directory_begin(),
793 delay_import_directory_end());
794}
795
796iterator_range<export_directory_iterator>
797COFFObjectFile::export_directories() const {
798 return make_range(export_directory_begin(), export_directory_end());
799}
800
Rui Ueyama74e85132014-11-19 00:18:07 +0000801iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
802 return make_range(base_reloc_begin(), base_reloc_end());
803}
804
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000805std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000806 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000807 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000808}
809
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000810std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000811COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
812 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000813 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000814}
815
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000816std::error_code
817COFFObjectFile::getDataDirectory(uint32_t Index,
818 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000819 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000820 if (!DataDirectory) {
821 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000822 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000823 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000824 assert(PE32Header || PE32PlusHeader);
825 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
826 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000827 if (Index >= NumEnt) {
828 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000829 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000830 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000831 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000832 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000833}
834
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000835std::error_code COFFObjectFile::getSection(int32_t Index,
836 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000837 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000838 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000839 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000840 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000841 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000842 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000843 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000844 }
845 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000846}
847
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000848std::error_code COFFObjectFile::getString(uint32_t Offset,
849 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000850 if (StringTableSize <= 4)
851 // Tried to get a string from an empty string table.
852 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000853 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000854 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000855 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000856 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000857}
858
David Majnemer44f51e52014-09-10 12:51:52 +0000859std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000860 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000861 return getSymbolName(Symbol.getGeneric(), Res);
862}
863
864std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
865 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000866 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000867 if (Symbol->Name.Offset.Zeroes == 0) {
868 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000869 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000870 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000871 }
872
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000873 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000874 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000875 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000876 else
877 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000878 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000879 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000880}
881
David Majnemer44f51e52014-09-10 12:51:52 +0000882ArrayRef<uint8_t>
883COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000884 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000885
David Majnemer44f51e52014-09-10 12:51:52 +0000886 size_t SymbolSize = getSymbolTableEntrySize();
887 if (Symbol.getNumberOfAuxSymbols() > 0) {
888 // AUX data comes immediately after the symbol in COFF
889 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000890# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000891 // Verify that the Aux symbol points to a valid entry in the symbol table.
892 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000893 if (Offset < getPointerToSymbolTable() ||
894 Offset >=
895 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000896 report_fatal_error("Aux Symbol data was outside of symbol table.");
897
David Majnemer44f51e52014-09-10 12:51:52 +0000898 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
899 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000900# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000901 }
David Majnemer44f51e52014-09-10 12:51:52 +0000902 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000903}
904
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000905std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
906 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000907 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000908 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000909 // Null terminated, let ::strlen figure out the length.
910 Name = Sec->Name;
911 else
912 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000913 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000914
915 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +0000916 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000917 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +0000918 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000919 if (decodeBase64StringEntry(Name.substr(2), Offset))
920 return object_error::parse_failed;
921 } else {
922 if (Name.substr(1).getAsInteger(10, Offset))
923 return object_error::parse_failed;
924 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000925 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000926 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000927 }
928
929 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +0000930 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000931}
932
David Majnemera9ee5c02014-10-09 08:42:31 +0000933uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
934 // SizeOfRawData and VirtualSize change what they represent depending on
935 // whether or not we have an executable image.
936 //
937 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000938 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +0000939 //
940 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
941 // actual section size is in VirtualSize. It is possible for VirtualSize to
942 // be greater than SizeOfRawData; the contents past that point should be
943 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +0000944 if (getDOSHeader())
945 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
946 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000947}
948
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000949std::error_code
950COFFObjectFile::getSectionContents(const coff_section *Sec,
951 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000952 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
953 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000954 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
955 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000956 // The only thing that we need to verify is that the contents is contained
957 // within the file bounds. We don't need to make sure it doesn't cover other
958 // data, as there's nothing that says that is not allowed.
959 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000960 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +0000961 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000962 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000963 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000964 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000965}
966
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000967const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000968 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000969}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000970
Rafael Espindola5e812af2014-01-30 02:49:50 +0000971void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000972 Rel.p = reinterpret_cast<uintptr_t>(
973 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000974}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000975
Rafael Espindola96d071c2015-06-29 23:29:12 +0000976uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +0000977 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +0000978 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000979}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000980
Rafael Espindola806f0062013-06-05 01:33:53 +0000981symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000982 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000983 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +0000984 if (R->SymbolTableIndex >= getNumberOfSymbols())
985 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +0000986 if (SymbolTable16)
987 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
988 else if (SymbolTable32)
989 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
990 else
David Majnemerc7353b52014-11-25 07:43:14 +0000991 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000992 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000993}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000994
Rafael Espindola99c041b2015-06-30 01:53:01 +0000995uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000996 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +0000997 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000998}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000999
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001000const coff_section *
1001COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1002 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001003}
1004
David Majnemer44f51e52014-09-10 12:51:52 +00001005COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1006 if (SymbolTable16)
1007 return toSymb<coff_symbol16>(Ref);
1008 if (SymbolTable32)
1009 return toSymb<coff_symbol32>(Ref);
1010 llvm_unreachable("no symbol table pointer!");
1011}
1012
1013COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1014 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001015}
1016
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001017const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001018COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1019 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001020}
1021
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001022iterator_range<const coff_relocation *>
1023COFFObjectFile::getRelocations(const coff_section *Sec) const {
1024 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1025 const coff_relocation *E = I;
1026 if (I)
1027 E += getNumberOfRelocations(Sec, Data, base());
1028 return make_range(I, E);
1029}
1030
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001031#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1032 case COFF::reloc_type: \
1033 Res = #reloc_type; \
1034 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001035
Rafael Espindola41bb4322015-06-30 04:08:37 +00001036void COFFObjectFile::getRelocationTypeName(
1037 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001038 const coff_relocation *Reloc = toRel(Rel);
1039 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001040 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001041 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001042 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001043 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1044 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1045 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1046 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1047 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1048 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1049 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1050 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1051 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1052 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1053 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1054 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1055 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1056 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1057 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1058 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1059 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1060 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001061 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001062 }
1063 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001064 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1065 switch (Reloc->Type) {
1066 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1067 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1068 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1069 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1070 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1071 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1072 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1073 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1074 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1075 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1076 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1077 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1078 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1079 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1080 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1081 default:
1082 Res = "Unknown";
1083 }
1084 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001085 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001086 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001087 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1088 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1089 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1090 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1091 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1092 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1093 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1094 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1095 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1096 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1097 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1098 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001099 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001100 }
1101 break;
1102 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001103 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001104 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001105 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001106}
1107
1108#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1109
Rafael Espindolac66d7612014-08-17 19:09:37 +00001110bool COFFObjectFile::isRelocatableObject() const {
1111 return !DataDirectory;
1112}
1113
Rui Ueyamac2bed422013-09-27 21:04:00 +00001114bool ImportDirectoryEntryRef::
1115operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001116 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001117}
1118
Rafael Espindola5e812af2014-01-30 02:49:50 +00001119void ImportDirectoryEntryRef::moveNext() {
1120 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001121}
1122
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001123std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1124 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001125 Result = ImportTable + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001126 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001127}
1128
Rui Ueyama861021f2014-10-02 22:05:29 +00001129static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001130makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001131 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001132 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001133 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001134 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001135 }
1136 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(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
Rui Ueyama15d99352014-10-03 00:41:58 +00001140static imported_symbol_iterator
1141importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001142 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001143 Object->getRvaPtr(RVA, IntPtr);
1144 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001145}
1146
Rui Ueyama15d99352014-10-03 00:41:58 +00001147static imported_symbol_iterator
1148importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001149 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001150 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001151 // Forward the pointer to the last entry which is null.
1152 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001153 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001154 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1155 while (*Entry++)
1156 ++Index;
1157 } else {
1158 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1159 while (*Entry++)
1160 ++Index;
1161 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001162 return makeImportedSymbolIterator(Object, IntPtr, Index);
1163}
1164
1165imported_symbol_iterator
1166ImportDirectoryEntryRef::imported_symbol_begin() const {
1167 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1168 OwningObject);
1169}
1170
1171imported_symbol_iterator
1172ImportDirectoryEntryRef::imported_symbol_end() const {
1173 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1174 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001175}
1176
Rui Ueyama979fb402014-10-09 02:16:38 +00001177iterator_range<imported_symbol_iterator>
1178ImportDirectoryEntryRef::imported_symbols() const {
1179 return make_range(imported_symbol_begin(), imported_symbol_end());
1180}
1181
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001182std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001183 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001184 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001185 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001186 return EC;
1187 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001188 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001189}
1190
Rui Ueyama1e152d52014-10-02 17:02:18 +00001191std::error_code
1192ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1193 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001194 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001195}
1196
1197std::error_code
1198ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1199 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001200 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001201}
1202
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001203std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001204 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001205 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001206 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1207 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001208 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001209 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001210 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001211}
1212
Rui Ueyama15d99352014-10-03 00:41:58 +00001213bool DelayImportDirectoryEntryRef::
1214operator==(const DelayImportDirectoryEntryRef &Other) const {
1215 return Table == Other.Table && Index == Other.Index;
1216}
1217
1218void DelayImportDirectoryEntryRef::moveNext() {
1219 ++Index;
1220}
1221
1222imported_symbol_iterator
1223DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1224 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1225 OwningObject);
1226}
1227
1228imported_symbol_iterator
1229DelayImportDirectoryEntryRef::imported_symbol_end() const {
1230 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1231 OwningObject);
1232}
1233
Rui Ueyama979fb402014-10-09 02:16:38 +00001234iterator_range<imported_symbol_iterator>
1235DelayImportDirectoryEntryRef::imported_symbols() const {
1236 return make_range(imported_symbol_begin(), imported_symbol_end());
1237}
1238
Rui Ueyama15d99352014-10-03 00:41:58 +00001239std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1240 uintptr_t IntPtr = 0;
1241 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1242 return EC;
1243 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001244 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001245}
1246
Rui Ueyama1af08652014-10-03 18:07:18 +00001247std::error_code DelayImportDirectoryEntryRef::
1248getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1249 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001250 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001251}
1252
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001253std::error_code DelayImportDirectoryEntryRef::
1254getImportAddress(int AddrIndex, uint64_t &Result) const {
1255 uint32_t RVA = Table[Index].DelayImportAddressTable +
1256 AddrIndex * (OwningObject->is64() ? 8 : 4);
1257 uintptr_t IntPtr = 0;
1258 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1259 return EC;
1260 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001261 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001262 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001263 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001264 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001265}
1266
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001267bool ExportDirectoryEntryRef::
1268operator==(const ExportDirectoryEntryRef &Other) const {
1269 return ExportTable == Other.ExportTable && Index == Other.Index;
1270}
1271
Rafael Espindola5e812af2014-01-30 02:49:50 +00001272void ExportDirectoryEntryRef::moveNext() {
1273 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001274}
1275
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001276// Returns the name of the current export symbol. If the symbol is exported only
1277// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001278std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001279 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001280 if (std::error_code EC =
1281 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001282 return EC;
1283 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001284 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001285}
1286
Rui Ueyamae5df6092014-01-17 22:02:24 +00001287// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001288std::error_code
1289ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001290 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001291 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001292}
1293
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001294// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001295std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001296 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001297 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001298}
1299
1300// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001301std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001302 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001303 if (std::error_code EC =
1304 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001305 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001306 const export_address_table_entry *entry =
1307 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001308 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001309 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001310}
1311
1312// Returns the name of the current export symbol. If the symbol is exported only
1313// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001314std::error_code
1315ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001316 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001317 if (std::error_code EC =
1318 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001319 return EC;
1320 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1321
1322 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1323 int Offset = 0;
1324 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1325 I < E; ++I, ++Offset) {
1326 if (*I != Index)
1327 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001328 if (std::error_code EC =
1329 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001330 return EC;
1331 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001332 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001333 return EC;
1334 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001335 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001336 }
1337 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001338 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001339}
1340
Rui Ueyama861021f2014-10-02 22:05:29 +00001341bool ImportedSymbolRef::
1342operator==(const ImportedSymbolRef &Other) const {
1343 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1344 && Index == Other.Index;
1345}
1346
1347void ImportedSymbolRef::moveNext() {
1348 ++Index;
1349}
1350
1351std::error_code
1352ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1353 uint32_t RVA;
1354 if (Entry32) {
1355 // If a symbol is imported only by ordinal, it has no name.
1356 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001357 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001358 RVA = Entry32[Index].getHintNameRVA();
1359 } else {
1360 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001361 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001362 RVA = Entry64[Index].getHintNameRVA();
1363 }
1364 uintptr_t IntPtr = 0;
1365 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1366 return EC;
1367 // +2 because the first two bytes is hint.
1368 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001369 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001370}
1371
1372std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1373 uint32_t RVA;
1374 if (Entry32) {
1375 if (Entry32[Index].isOrdinal()) {
1376 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001377 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001378 }
1379 RVA = Entry32[Index].getHintNameRVA();
1380 } else {
1381 if (Entry64[Index].isOrdinal()) {
1382 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001383 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001384 }
1385 RVA = Entry64[Index].getHintNameRVA();
1386 }
1387 uintptr_t IntPtr = 0;
1388 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1389 return EC;
1390 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001391 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001392}
1393
Rafael Espindola437b0d52014-07-31 03:12:45 +00001394ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001395ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001396 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001397 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001398 if (EC)
1399 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001400 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001401}
Rui Ueyama74e85132014-11-19 00:18:07 +00001402
1403bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1404 return Header == Other.Header && Index == Other.Index;
1405}
1406
1407void BaseRelocRef::moveNext() {
1408 // Header->BlockSize is the size of the current block, including the
1409 // size of the header itself.
1410 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001411 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001412 if (Size == Header->BlockSize) {
1413 // .reloc contains a list of base relocation blocks. Each block
1414 // consists of the header followed by entries. The header contains
1415 // how many entories will follow. When we reach the end of the
1416 // current block, proceed to the next block.
1417 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1418 reinterpret_cast<const uint8_t *>(Header) + Size);
1419 Index = 0;
1420 } else {
1421 ++Index;
1422 }
1423}
1424
1425std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1426 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1427 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001428 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001429}
1430
1431std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1432 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1433 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001434 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001435}