blob: 28531feccfe1a893f59c0af333393881ca593937 [file] [log] [blame]
Eugene Zelenkod341c932017-04-19 23:02:10 +00001//===- COFFObjectFile.cpp - COFF object file implementation ---------------===//
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00002//
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. Spencer9da9e692012-03-19 20:27:37 +000014#include "llvm/ADT/ArrayRef.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000015#include "llvm/ADT/StringRef.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000016#include "llvm/ADT/Triple.h"
Rui Ueyama6a75acb2015-06-25 00:07:39 +000017#include "llvm/ADT/iterator_range.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000018#include "llvm/Object/Binary.h"
19#include "llvm/Object/COFF.h"
20#include "llvm/Object/Error.h"
21#include "llvm/Object/ObjectFile.h"
Eric Beckmannefef15a2017-05-08 02:47:07 +000022#include "llvm/Support/BinaryStreamReader.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000023#include "llvm/Support/COFF.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000024#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include <algorithm>
30#include <cassert>
31#include <cstddef>
32#include <cstdint>
33#include <cstring>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000034#include <limits>
Eugene Zelenkod341c932017-04-19 23:02:10 +000035#include <memory>
36#include <system_error>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000037
38using namespace llvm;
39using namespace object;
40
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000041using support::ulittle16_t;
42using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000043using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000044using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000045
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000046// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000047static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000048 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000049 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000050 return false;
51 }
52 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000053}
54
David Majnemere830c602014-11-13 08:46:37 +000055static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
David Majnemer94751be2014-11-13 09:50:18 +000056 const uint64_t Size) {
David Majnemere830c602014-11-13 08:46:37 +000057 if (Addr + Size < Addr || Addr + Size < Size ||
58 Addr + Size > uintptr_t(M.getBufferEnd()) ||
59 Addr < uintptr_t(M.getBufferStart())) {
60 return object_error::unexpected_eof;
61 }
Rui Ueyama7d099192015-06-09 15:20:42 +000062 return std::error_code();
David Majnemere830c602014-11-13 08:46:37 +000063}
64
Rui Ueyamaed64342b2013-07-19 23:23:29 +000065// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
66// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000067template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000068static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
David Majnemer58323a92014-11-13 07:42:07 +000069 const void *Ptr,
David Majnemer236b0ca2014-11-17 11:17:17 +000070 const uint64_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000071 uintptr_t Addr = uintptr_t(Ptr);
David Majnemere830c602014-11-13 08:46:37 +000072 if (std::error_code EC = checkOffset(M, Addr, Size))
73 return EC;
Rui Ueyamaed64342b2013-07-19 23:23:29 +000074 Obj = reinterpret_cast<const T *>(Addr);
Rui Ueyama7d099192015-06-09 15:20:42 +000075 return std::error_code();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000076}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000077
Nico Rieck9d2c15e2014-02-22 16:12:20 +000078// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
79// prefixed slashes.
80static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
81 assert(Str.size() <= 6 && "String too long, possible overflow.");
82 if (Str.size() > 6)
83 return true;
84
85 uint64_t Value = 0;
86 while (!Str.empty()) {
87 unsigned CharVal;
88 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
89 CharVal = Str[0] - 'A';
90 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
91 CharVal = Str[0] - 'a' + 26;
92 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
93 CharVal = Str[0] - '0' + 52;
94 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000095 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000096 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000097 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000098 else
99 return true;
100
101 Value = (Value * 64) + CharVal;
102 Str = Str.substr(1);
103 }
104
105 if (Value > std::numeric_limits<uint32_t>::max())
106 return true;
107
108 Result = static_cast<uint32_t>(Value);
109 return false;
110}
111
David Majnemer44f51e52014-09-10 12:51:52 +0000112template <typename coff_symbol_type>
113const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
114 const coff_symbol_type *Addr =
115 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000116
David Majnemer236b0ca2014-11-17 11:17:17 +0000117 assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
David Majnemer44f51e52014-09-10 12:51:52 +0000118#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000119 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000120 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000121
David Majnemer44f51e52014-09-10 12:51:52 +0000122 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
123 "Symbol did not point to the beginning of a symbol");
124#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000125
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000126 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000127}
128
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000129const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
130 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000131
Eugene Zelenkod341c932017-04-19 23:02:10 +0000132#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000133 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000134 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000135 report_fatal_error("Section was outside of section table.");
136
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000137 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
138 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000139 "Section did not point to the beginning of a section");
Eugene Zelenkod341c932017-04-19 23:02:10 +0000140#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000141
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000142 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000143}
144
Rafael Espindola5e812af2014-01-30 02:49:50 +0000145void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000146 auto End = reinterpret_cast<uintptr_t>(StringTable);
David Majnemer44f51e52014-09-10 12:51:52 +0000147 if (SymbolTable16) {
148 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
149 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000150 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000151 } else if (SymbolTable32) {
152 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
153 Symb += 1 + Symb->NumberOfAuxSymbols;
David Majnemer236b0ca2014-11-17 11:17:17 +0000154 Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
David Majnemer44f51e52014-09-10 12:51:52 +0000155 } else {
156 llvm_unreachable("no symbol table pointer!");
157 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000158}
159
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000160Expected<StringRef> COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000161 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000162 StringRef Result;
Eric Beckmannefef15a2017-05-08 02:47:07 +0000163 if (std::error_code EC = getSymbolName(Symb, Result))
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000164 return errorCodeToError(EC);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000165 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000166}
167
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000168uint64_t COFFObjectFile::getSymbolValueImpl(DataRefImpl Ref) const {
169 return getCOFFSymbol(Ref).getValue();
Rafael Espindola991af662015-06-24 19:11:10 +0000170}
171
Davide Italiano6b2bba12016-11-02 17:32:19 +0000172uint32_t COFFObjectFile::getSymbolAlignment(DataRefImpl Ref) const {
173 // MSVC/link.exe seems to align symbols to the next-power-of-2
174 // up to 32 bytes.
175 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Davide Italiano03a85682016-11-11 03:07:45 +0000176 return std::min(uint64_t(32), PowerOf2Ceil(Symb.getValue()));
Davide Italiano6b2bba12016-11-02 17:32:19 +0000177}
178
Kevin Enderby931cb652016-06-24 18:24:42 +0000179Expected<uint64_t> COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
Rafael Espindolaed067c42015-07-03 18:19:00 +0000180 uint64_t Result = getSymbolValue(Ref);
David Majnemer44f51e52014-09-10 12:51:52 +0000181 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000182 int32_t SectionNumber = Symb.getSectionNumber();
Rafael Espindola991af662015-06-24 19:11:10 +0000183
184 if (Symb.isAnyUndefined() || Symb.isCommon() ||
185 COFF::isReservedSectionNumber(SectionNumber))
Rafael Espindolaed067c42015-07-03 18:19:00 +0000186 return Result;
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000187
Rafael Espindola54c9f3d2015-06-24 17:08:44 +0000188 const coff_section *Section = nullptr;
189 if (std::error_code EC = getSection(SectionNumber, Section))
Kevin Enderby931cb652016-06-24 18:24:42 +0000190 return errorCodeToError(EC);
Rafael Espindola991af662015-06-24 19:11:10 +0000191 Result += Section->VirtualAddress;
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000192
193 // The section VirtualAddress does not include ImageBase, and we want to
194 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000195 Result += getImageBase();
Reid Kleckner47ea9ec2015-07-31 16:14:22 +0000196
Rafael Espindolaed067c42015-07-03 18:19:00 +0000197 return Result;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000198}
199
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000200Expected<SymbolRef::Type> COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000201 COFFSymbolRef Symb = getCOFFSymbol(Ref);
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000202 int32_t SectionNumber = Symb.getSectionNumber();
David Majnemer44f51e52014-09-10 12:51:52 +0000203
Peter Collingbournee834f422015-08-06 05:26:35 +0000204 if (Symb.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION)
205 return SymbolRef::ST_Function;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000206 if (Symb.isAnyUndefined())
207 return SymbolRef::ST_Unknown;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000208 if (Symb.isCommon())
209 return SymbolRef::ST_Data;
210 if (Symb.isFileRecord())
211 return SymbolRef::ST_File;
212
213 // TODO: perhaps we need a new symbol type ST_Section.
214 if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
215 return SymbolRef::ST_Debug;
216
217 if (!COFF::isReservedSectionNumber(SectionNumber))
218 return SymbolRef::ST_Data;
219
220 return SymbolRef::ST_Other;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000221}
222
Rafael Espindola20122a42014-01-31 20:57:12 +0000223uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000224 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000225 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000226
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000227 if (Symb.isExternal() || Symb.isWeakExternal())
Lang Hames9dc0eb42016-01-25 01:21:45 +0000228 Result |= SymbolRef::SF_Global;
David Meyer1df4b842012-02-28 23:47:53 +0000229
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000230 if (Symb.isWeakExternal())
David Meyer1df4b842012-02-28 23:47:53 +0000231 Result |= SymbolRef::SF_Weak;
232
David Majnemer44f51e52014-09-10 12:51:52 +0000233 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000234 Result |= SymbolRef::SF_Absolute;
235
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000236 if (Symb.isFileRecord())
237 Result |= SymbolRef::SF_FormatSpecific;
238
239 if (Symb.isSectionDefinition())
240 Result |= SymbolRef::SF_FormatSpecific;
241
242 if (Symb.isCommon())
243 Result |= SymbolRef::SF_Common;
244
245 if (Symb.isAnyUndefined())
246 Result |= SymbolRef::SF_Undefined;
247
Rafael Espindola20122a42014-01-31 20:57:12 +0000248 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000249}
250
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000251uint64_t COFFObjectFile::getCommonSymbolSizeImpl(DataRefImpl Ref) const {
David Majnemerc7d7c6f2014-10-31 05:07:00 +0000252 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000253 return Symb.getValue();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000254}
255
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000256Expected<section_iterator>
Rafael Espindola8bab8892015-08-07 23:27:14 +0000257COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000258 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000259 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
260 return section_end();
261 const coff_section *Sec = nullptr;
262 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000263 return errorCodeToError(EC);
Rafael Espindola8bab8892015-08-07 23:27:14 +0000264 DataRefImpl Ret;
265 Ret.p = reinterpret_cast<uintptr_t>(Sec);
266 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000267}
268
Rafael Espindola6bf32212015-06-24 19:57:32 +0000269unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
270 COFFSymbolRef Symb = getCOFFSymbol(Sym.getRawDataRefImpl());
271 return Symb.getSectionNumber();
272}
273
Rafael Espindola5e812af2014-01-30 02:49:50 +0000274void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000275 const coff_section *Sec = toSec(Ref);
276 Sec += 1;
277 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000278}
279
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000280std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
281 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000282 const coff_section *Sec = toSec(Ref);
283 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000284}
285
Rafael Espindola80291272014-10-08 15:28:58 +0000286uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000287 const coff_section *Sec = toSec(Ref);
David Majnemer7c6a0712015-07-31 17:40:24 +0000288 uint64_t Result = Sec->VirtualAddress;
289
290 // The section VirtualAddress does not include ImageBase, and we want to
291 // return virtual addresses.
Reid Kleckner21427ad2015-10-09 00:15:08 +0000292 Result += getImageBase();
David Majnemer7c6a0712015-07-31 17:40:24 +0000293 return Result;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000294}
295
Rafael Espindola80291272014-10-08 15:28:58 +0000296uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000297 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000298}
299
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000300std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
301 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000302 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000303 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000304 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000305 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
306 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000307}
308
Rafael Espindola80291272014-10-08 15:28:58 +0000309uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000310 const coff_section *Sec = toSec(Ref);
David Majnemer511391f2016-03-17 16:55:18 +0000311 return Sec->getAlignment();
Michael J. Spencer79894602011-10-10 21:55:43 +0000312}
313
George Rimar401e4e52016-05-24 12:48:46 +0000314bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
315 return false;
316}
317
Rafael Espindola80291272014-10-08 15:28:58 +0000318bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000319 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000320 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000321}
322
Rafael Espindola80291272014-10-08 15:28:58 +0000323bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000324 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000325 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000326}
327
Rafael Espindola80291272014-10-08 15:28:58 +0000328bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000329 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000330 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
331 COFF::IMAGE_SCN_MEM_READ |
332 COFF::IMAGE_SCN_MEM_WRITE;
333 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000334}
335
Rafael Espindola6bf32212015-06-24 19:57:32 +0000336unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
337 uintptr_t Offset =
338 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
339 assert((Offset % sizeof(coff_section)) == 0);
340 return (Offset / sizeof(coff_section)) + 1;
341}
342
Rafael Espindola80291272014-10-08 15:28:58 +0000343bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000344 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000345 // In COFF, a virtual section won't have any in-file
346 // content, so the file pointer to the content will be zero.
347 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000348}
349
David Majnemere830c602014-11-13 08:46:37 +0000350static uint32_t getNumberOfRelocations(const coff_section *Sec,
351 MemoryBufferRef M, const uint8_t *base) {
352 // The field for the number of relocations in COFF section table is only
353 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
354 // NumberOfRelocations field, and the actual relocation count is stored in the
355 // VirtualAddress field in the first relocation entry.
356 if (Sec->hasExtendedRelocations()) {
357 const coff_relocation *FirstReloc;
358 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
359 base + Sec->PointerToRelocations)))
360 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000361 // -1 to exclude this first relocation entry.
362 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000363 }
364 return Sec->NumberOfRelocations;
365}
366
David Majnemer94751be2014-11-13 09:50:18 +0000367static const coff_relocation *
368getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
369 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
370 if (!NumRelocs)
371 return nullptr;
372 auto begin = reinterpret_cast<const coff_relocation *>(
373 Base + Sec->PointerToRelocations);
374 if (Sec->hasExtendedRelocations()) {
375 // Skip the first relocation entry repurposed to store the number of
376 // relocations.
377 begin++;
378 }
379 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
380 return nullptr;
381 return begin;
382}
383
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000384relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
385 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000386 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000387 if (begin && Sec->VirtualAddress != 0)
388 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000390 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000391 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000392}
393
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000394relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
395 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000396 const coff_relocation *I = getFirstReloc(Sec, Data, base());
397 if (I)
398 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000399 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000400 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000401 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000402}
403
Rui Ueyamac2bed422013-09-27 21:04:00 +0000404// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000405std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000406 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000407 if (std::error_code EC = getObject(
408 SymbolTable16, Data, base() + getPointerToSymbolTable(),
409 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000410 return EC;
411
412 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000413 if (std::error_code EC = getObject(
414 SymbolTable32, Data, base() + getPointerToSymbolTable(),
415 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000416 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000417
418 // Find string table. The first four byte of the string table contains the
419 // total size of the string table, including the size field itself. If the
420 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000421 uint32_t StringTableOffset = getPointerToSymbolTable() +
422 getNumberOfSymbols() * getSymbolTableEntrySize();
423 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000424 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000425 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000426 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000427 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000428 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000429 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000430 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000431
Nico Rieck773a5792014-02-26 19:51:44 +0000432 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
433 // tools like cvtres write a size of 0 for an empty table instead of 4.
434 if (StringTableSize < 4)
435 StringTableSize = 4;
436
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000438 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000439 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000440 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441}
442
Reid Kleckner21427ad2015-10-09 00:15:08 +0000443uint64_t COFFObjectFile::getImageBase() const {
Reid Klecknere94fef72015-10-09 00:15:01 +0000444 if (PE32Header)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000445 return PE32Header->ImageBase;
Reid Klecknere94fef72015-10-09 00:15:01 +0000446 else if (PE32PlusHeader)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000447 return PE32PlusHeader->ImageBase;
448 // This actually comes up in practice.
449 return 0;
Reid Klecknere94fef72015-10-09 00:15:01 +0000450}
451
Rui Ueyama215a5862014-02-20 06:51:07 +0000452// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000453std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner21427ad2015-10-09 00:15:08 +0000454 uint64_t ImageBase = getImageBase();
Rui Ueyamab7a40082014-02-20 19:14:56 +0000455 uint64_t Rva = Addr - ImageBase;
456 assert(Rva <= UINT32_MAX);
457 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000458}
459
Rui Ueyamac2bed422013-09-27 21:04:00 +0000460// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000461std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000462 for (const SectionRef &S : sections()) {
463 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000464 uint32_t SectionStart = Section->VirtualAddress;
465 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000466 if (SectionStart <= Addr && Addr < SectionEnd) {
467 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000469 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000470 }
471 }
472 return object_error::parse_failed;
473}
474
Reid Kleckner2da433e2016-06-02 17:10:43 +0000475std::error_code
476COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size,
477 ArrayRef<uint8_t> &Contents) const {
478 for (const SectionRef &S : sections()) {
479 const coff_section *Section = getCOFFSection(S);
480 uint32_t SectionStart = Section->VirtualAddress;
481 // Check if this RVA is within the section bounds. Be careful about integer
482 // overflow.
483 uint32_t OffsetIntoSection = RVA - SectionStart;
484 if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
485 Size <= Section->VirtualSize - OffsetIntoSection) {
486 uintptr_t Begin =
487 uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
488 Contents =
489 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
490 return std::error_code();
491 }
492 }
493 return object_error::parse_failed;
494}
495
Rui Ueyamac2bed422013-09-27 21:04:00 +0000496// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
497// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000498std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
499 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000500 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000501 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000502 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000503 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
504 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
505 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000506 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000507}
508
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000509std::error_code
510COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir,
511 const codeview::DebugInfo *&PDBInfo,
512 StringRef &PDBFileName) const {
Reid Kleckner2da433e2016-06-02 17:10:43 +0000513 ArrayRef<uint8_t> InfoBytes;
514 if (std::error_code EC = getRvaAndSizeAsBytes(
515 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
516 return EC;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000517 if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
Reid Kleckner2da433e2016-06-02 17:10:43 +0000518 return object_error::parse_failed;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000519 PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
520 InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
Reid Kleckner2da433e2016-06-02 17:10:43 +0000521 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
522 InfoBytes.size());
523 // Truncate the name at the first null byte. Ignore any padding.
524 PDBFileName = PDBFileName.split('\0').first;
525 return std::error_code();
526}
527
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000528std::error_code
529COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo,
530 StringRef &PDBFileName) const {
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000531 for (const debug_directory &D : debug_directories())
532 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW)
533 return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
534 // If we get here, there is no PDB info to return.
535 PDBInfo = nullptr;
536 PDBFileName = StringRef();
537 return std::error_code();
538}
539
Rui Ueyamac2bed422013-09-27 21:04:00 +0000540// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000541std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000542 // First, we get the RVA of the import table. If the file lacks a pointer to
543 // the import table, do nothing.
544 const data_directory *DataEntry;
545 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000546 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000547
548 // Do nothing if the pointer to import table is NULL.
549 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000550 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000551
552 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000553
554 // Find the section that contains the RVA. This is needed because the RVA is
555 // the import table's memory address which is different from its file offset.
556 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000557 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000558 return EC;
David Majnemerad7b7e72016-06-26 04:36:32 +0000559 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
560 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000561 ImportDirectory = reinterpret_cast<
David Majnemer1c0aa042016-07-31 19:25:21 +0000562 const coff_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000563 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000564}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000565
Rui Ueyama15d99352014-10-03 00:41:58 +0000566// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
567std::error_code COFFObjectFile::initDelayImportTablePtr() {
568 const data_directory *DataEntry;
569 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000570 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000571 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000572 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000573
574 uint32_t RVA = DataEntry->RelativeVirtualAddress;
575 NumberOfDelayImportDirectory = DataEntry->Size /
576 sizeof(delay_import_directory_table_entry) - 1;
577
578 uintptr_t IntPtr = 0;
579 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
580 return EC;
581 DelayImportDirectory = reinterpret_cast<
582 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000583 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000584}
585
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000586// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000587std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000588 // First, we get the RVA of the export table. If the file lacks a pointer to
589 // the export table, do nothing.
590 const data_directory *DataEntry;
591 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000592 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000593
594 // Do nothing if the pointer to export table is NULL.
595 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000596 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000597
598 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
599 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000600 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000601 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000602 ExportDirectory =
603 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000604 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000605}
606
Rui Ueyama74e85132014-11-19 00:18:07 +0000607std::error_code COFFObjectFile::initBaseRelocPtr() {
608 const data_directory *DataEntry;
609 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000610 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000611 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000612 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000613
614 uintptr_t IntPtr = 0;
615 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
616 return EC;
617 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
618 IntPtr);
619 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
620 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000621 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000622}
623
Reid Kleckner2da433e2016-06-02 17:10:43 +0000624std::error_code COFFObjectFile::initDebugDirectoryPtr() {
625 // Get the RVA of the debug directory. Do nothing if it does not exist.
626 const data_directory *DataEntry;
627 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
628 return std::error_code();
629
630 // Do nothing if the RVA is NULL.
631 if (DataEntry->RelativeVirtualAddress == 0)
632 return std::error_code();
633
634 // Check that the size is a multiple of the entry size.
635 if (DataEntry->Size % sizeof(debug_directory) != 0)
636 return object_error::parse_failed;
637
638 uintptr_t IntPtr = 0;
639 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
640 return EC;
641 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
642 if (std::error_code EC = getRvaPtr(
643 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
644 return EC;
645 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
646 return std::error_code();
647}
648
Rafael Espindola48af1c22014-08-19 18:44:46 +0000649COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
650 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000651 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
652 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
653 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
David Majnemerad7b7e72016-06-26 04:36:32 +0000654 ImportDirectory(nullptr),
Rui Ueyama15d99352014-10-03 00:41:58 +0000655 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Kleckner2da433e2016-06-02 17:10:43 +0000656 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
657 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000658 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000659 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000660 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000661
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000662 // The current location in the file where we are looking at.
663 uint64_t CurPtr = 0;
664
665 // PE header is optional and is present only in executables. If it exists,
666 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000667 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000668
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000669 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000670 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000671 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
672 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000673 const auto *DH = reinterpret_cast<const dos_header *>(base());
674 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
675 CurPtr = DH->AddressOfNewExeHeader;
676 // Check the PE magic bytes. ("PE\0\0")
677 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
678 EC = object_error::parse_failed;
679 return;
680 }
681 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
682 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000683 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000684 }
685
Rafael Espindola48af1c22014-08-19 18:44:46 +0000686 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000687 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000688
689 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
690 // import libraries share a common prefix but bigobj is more restrictive.
691 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
692 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
693 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
694 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
695 return;
696
697 // Verify that we are dealing with bigobj.
698 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
699 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
700 sizeof(COFF::BigObjMagic)) == 0) {
701 COFFHeader = nullptr;
702 CurPtr += sizeof(coff_bigobj_file_header);
703 } else {
704 // It's not a bigobj.
705 COFFBigObjHeader = nullptr;
706 }
707 }
708 if (COFFHeader) {
709 // The prior checkSize call may have failed. This isn't a hard error
710 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000711 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000712 CurPtr += sizeof(coff_file_header);
713
714 if (COFFHeader->isImportLibrary())
715 return;
716 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000717
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000718 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000719 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000720 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000721 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000722
723 const uint8_t *DataDirAddr;
724 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000725 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000726 PE32Header = Header;
727 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
728 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000729 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000730 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
731 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
732 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
733 } else {
734 // It's neither PE32 nor PE32+.
735 EC = object_error::parse_failed;
736 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000737 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000738 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000739 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000740 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000741
Rui Ueyama8950a532016-08-11 22:02:44 +0000742 if (COFFHeader)
743 CurPtr += COFFHeader->SizeOfOptionalHeader;
744
Rafael Espindola48af1c22014-08-19 18:44:46 +0000745 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000746 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000747 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000748
Rui Ueyamac2bed422013-09-27 21:04:00 +0000749 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000750 if (getPointerToSymbolTable() != 0) {
David Majnemerac8cfab2016-08-30 20:20:24 +0000751 if ((EC = initSymbolTablePtr())) {
752 SymbolTable16 = nullptr;
753 SymbolTable32 = nullptr;
754 StringTable = nullptr;
755 StringTableSize = 0;
756 }
David Majnemer236b0ca2014-11-17 11:17:17 +0000757 } else {
758 // We had better not have any symbols if we don't have a symbol table.
759 if (getNumberOfSymbols() != 0) {
760 EC = object_error::parse_failed;
761 return;
762 }
763 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000764
Rui Ueyamac2bed422013-09-27 21:04:00 +0000765 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000766 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000767 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000768 if ((EC = initDelayImportTablePtr()))
769 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000770
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000771 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000772 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000773 return;
774
Rui Ueyama74e85132014-11-19 00:18:07 +0000775 // Initialize the pointer to the base relocation table.
776 if ((EC = initBaseRelocPtr()))
777 return;
778
Reid Kleckner2da433e2016-06-02 17:10:43 +0000779 // Initialize the pointer to the export table.
780 if ((EC = initDebugDirectoryPtr()))
781 return;
782
Rui Ueyama7d099192015-06-09 15:20:42 +0000783 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000784}
785
Peter Collingbourne435890a2016-11-22 03:38:40 +0000786basic_symbol_iterator COFFObjectFile::symbol_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000787 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000788 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000789 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000790}
791
Peter Collingbourne435890a2016-11-22 03:38:40 +0000792basic_symbol_iterator COFFObjectFile::symbol_end() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000793 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000794 DataRefImpl Ret;
795 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000796 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000797}
798
Rui Ueyamabc654b12013-09-27 21:47:05 +0000799import_directory_iterator COFFObjectFile::import_directory_begin() const {
David Majnemerad7b7e72016-06-26 04:36:32 +0000800 if (!ImportDirectory)
801 return import_directory_end();
David Majnemer1c0aa042016-07-31 19:25:21 +0000802 if (ImportDirectory->isNull())
David Majnemerad7b7e72016-06-26 04:36:32 +0000803 return import_directory_end();
Rui Ueyamaa045b732014-01-16 03:13:19 +0000804 return import_directory_iterator(
805 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000806}
807
Rui Ueyamabc654b12013-09-27 21:47:05 +0000808import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000809 return import_directory_iterator(
David Majnemerad7b7e72016-06-26 04:36:32 +0000810 ImportDirectoryEntryRef(nullptr, -1, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000811}
David Meyerc429b802012-03-01 22:19:54 +0000812
Rui Ueyama15d99352014-10-03 00:41:58 +0000813delay_import_directory_iterator
814COFFObjectFile::delay_import_directory_begin() const {
815 return delay_import_directory_iterator(
816 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
817}
818
819delay_import_directory_iterator
820COFFObjectFile::delay_import_directory_end() const {
821 return delay_import_directory_iterator(
822 DelayImportDirectoryEntryRef(
823 DelayImportDirectory, NumberOfDelayImportDirectory, this));
824}
825
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000826export_directory_iterator COFFObjectFile::export_directory_begin() const {
827 return export_directory_iterator(
828 ExportDirectoryEntryRef(ExportDirectory, 0, this));
829}
830
831export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000832 if (!ExportDirectory)
833 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000834 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000835 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000836 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000837}
838
Rafael Espindolab5155a52014-02-10 20:24:04 +0000839section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000840 DataRefImpl Ret;
841 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
842 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000843}
844
Rafael Espindolab5155a52014-02-10 20:24:04 +0000845section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000846 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000847 int NumSections =
848 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000849 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
850 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000851}
852
Rui Ueyama74e85132014-11-19 00:18:07 +0000853base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
854 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
855}
856
857base_reloc_iterator COFFObjectFile::base_reloc_end() const {
858 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
859}
860
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000861uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000862 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000863}
864
865StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000866 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000867 case COFF::IMAGE_FILE_MACHINE_I386:
868 return "COFF-i386";
869 case COFF::IMAGE_FILE_MACHINE_AMD64:
870 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000871 case COFF::IMAGE_FILE_MACHINE_ARMNT:
872 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000873 case COFF::IMAGE_FILE_MACHINE_ARM64:
874 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000875 default:
876 return "COFF-<unknown arch>";
877 }
878}
879
880unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000881 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000882 case COFF::IMAGE_FILE_MACHINE_I386:
883 return Triple::x86;
884 case COFF::IMAGE_FILE_MACHINE_AMD64:
885 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000886 case COFF::IMAGE_FILE_MACHINE_ARMNT:
887 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000888 case COFF::IMAGE_FILE_MACHINE_ARM64:
889 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000890 default:
891 return Triple::UnknownArch;
892 }
893}
894
Rui Ueyama979fb402014-10-09 02:16:38 +0000895iterator_range<import_directory_iterator>
896COFFObjectFile::import_directories() const {
897 return make_range(import_directory_begin(), import_directory_end());
898}
899
900iterator_range<delay_import_directory_iterator>
901COFFObjectFile::delay_import_directories() const {
902 return make_range(delay_import_directory_begin(),
903 delay_import_directory_end());
904}
905
906iterator_range<export_directory_iterator>
907COFFObjectFile::export_directories() const {
908 return make_range(export_directory_begin(), export_directory_end());
909}
910
Rui Ueyama74e85132014-11-19 00:18:07 +0000911iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
912 return make_range(base_reloc_begin(), base_reloc_end());
913}
914
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000915std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000916 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000917 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000918}
919
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000920std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000921COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
922 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000923 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000924}
925
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000926std::error_code
927COFFObjectFile::getDataDirectory(uint32_t Index,
928 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000929 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000930 if (!DataDirectory) {
931 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000932 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000933 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000934 assert(PE32Header || PE32PlusHeader);
935 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
936 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000937 if (Index >= NumEnt) {
938 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000939 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000940 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000941 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000942 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000943}
944
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000945std::error_code COFFObjectFile::getSection(int32_t Index,
946 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000947 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000948 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000949 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000950 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000951 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000952 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000953 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000954 }
955 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000956}
957
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000958std::error_code COFFObjectFile::getString(uint32_t Offset,
959 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000960 if (StringTableSize <= 4)
961 // Tried to get a string from an empty string table.
962 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000963 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000964 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000965 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000966 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000967}
968
David Majnemer44f51e52014-09-10 12:51:52 +0000969std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000970 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000971 return getSymbolName(Symbol.getGeneric(), Res);
972}
973
974std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
975 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000976 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000977 if (Symbol->Name.Offset.Zeroes == 0) {
978 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000979 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +0000980 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000981 }
982
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000983 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000984 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000985 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000986 else
987 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000988 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +0000989 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000990}
991
David Majnemer44f51e52014-09-10 12:51:52 +0000992ArrayRef<uint8_t>
993COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000994 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000995
David Majnemer44f51e52014-09-10 12:51:52 +0000996 size_t SymbolSize = getSymbolTableEntrySize();
997 if (Symbol.getNumberOfAuxSymbols() > 0) {
998 // AUX data comes immediately after the symbol in COFF
999 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Eugene Zelenkod341c932017-04-19 23:02:10 +00001000#ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001001 // Verify that the Aux symbol points to a valid entry in the symbol table.
1002 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +00001003 if (Offset < getPointerToSymbolTable() ||
1004 Offset >=
1005 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +00001006 report_fatal_error("Aux Symbol data was outside of symbol table.");
1007
David Majnemer44f51e52014-09-10 12:51:52 +00001008 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
1009 "Aux Symbol data did not point to the beginning of a symbol");
Eugene Zelenkod341c932017-04-19 23:02:10 +00001010#endif
Marshall Clowbfb85e62012-06-15 01:15:47 +00001011 }
David Majnemer44f51e52014-09-10 12:51:52 +00001012 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +00001013}
1014
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001015std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
1016 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001017 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +00001018 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001019 // Null terminated, let ::strlen figure out the length.
1020 Name = Sec->Name;
1021 else
1022 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +00001023 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001024
1025 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +00001026 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001027 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +00001028 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +00001029 if (decodeBase64StringEntry(Name.substr(2), Offset))
1030 return object_error::parse_failed;
1031 } else {
1032 if (Name.substr(1).getAsInteger(10, Offset))
1033 return object_error::parse_failed;
1034 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001035 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001036 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001037 }
1038
1039 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +00001040 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001041}
1042
David Majnemera9ee5c02014-10-09 08:42:31 +00001043uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1044 // SizeOfRawData and VirtualSize change what they represent depending on
1045 // whether or not we have an executable image.
1046 //
1047 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001048 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +00001049 //
1050 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1051 // actual section size is in VirtualSize. It is possible for VirtualSize to
1052 // be greater than SizeOfRawData; the contents past that point should be
1053 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001054 if (getDOSHeader())
1055 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1056 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001057}
1058
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001059std::error_code
1060COFFObjectFile::getSectionContents(const coff_section *Sec,
1061 ArrayRef<uint8_t> &Res) const {
David Majnemere2129662016-05-28 19:45:51 +00001062 // In COFF, a virtual section won't have any in-file
1063 // content, so the file pointer to the content will be zero.
1064 if (Sec->PointerToRawData == 0)
Shoaib Meenaiee97c5f2017-05-14 18:34:56 +00001065 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001066 // The only thing that we need to verify is that the contents is contained
1067 // within the file bounds. We don't need to make sure it doesn't cover other
1068 // data, as there's nothing that says that is not allowed.
1069 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001070 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001071 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001072 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001073 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001074 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001075}
1076
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001077const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001078 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001079}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001080
Rafael Espindola5e812af2014-01-30 02:49:50 +00001081void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001082 Rel.p = reinterpret_cast<uintptr_t>(
1083 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001084}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001085
Rafael Espindola96d071c2015-06-29 23:29:12 +00001086uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +00001087 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001088 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001089}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001090
Rafael Espindola806f0062013-06-05 01:33:53 +00001091symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001092 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001093 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001094 if (R->SymbolTableIndex >= getNumberOfSymbols())
1095 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001096 if (SymbolTable16)
1097 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1098 else if (SymbolTable32)
1099 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1100 else
David Majnemerc7353b52014-11-25 07:43:14 +00001101 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001102 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001103}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001104
Rafael Espindola99c041b2015-06-30 01:53:01 +00001105uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001106 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001107 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001108}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001109
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001110const coff_section *
1111COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1112 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001113}
1114
David Majnemer44f51e52014-09-10 12:51:52 +00001115COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1116 if (SymbolTable16)
1117 return toSymb<coff_symbol16>(Ref);
1118 if (SymbolTable32)
1119 return toSymb<coff_symbol32>(Ref);
1120 llvm_unreachable("no symbol table pointer!");
1121}
1122
1123COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1124 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001125}
1126
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001127const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001128COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1129 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001130}
1131
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001132iterator_range<const coff_relocation *>
1133COFFObjectFile::getRelocations(const coff_section *Sec) const {
1134 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1135 const coff_relocation *E = I;
1136 if (I)
1137 E += getNumberOfRelocations(Sec, Data, base());
1138 return make_range(I, E);
1139}
1140
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001141#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1142 case COFF::reloc_type: \
1143 Res = #reloc_type; \
1144 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001145
Rafael Espindola41bb4322015-06-30 04:08:37 +00001146void COFFObjectFile::getRelocationTypeName(
1147 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001148 const coff_relocation *Reloc = toRel(Rel);
1149 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001150 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001151 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001152 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001153 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1154 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1155 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1156 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1157 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1158 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1159 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1160 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1161 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1162 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1163 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1164 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1165 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1166 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1167 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1168 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1169 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1170 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001171 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001172 }
1173 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001174 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1175 switch (Reloc->Type) {
1176 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1177 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1178 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1179 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1180 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1181 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1182 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1183 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1184 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1185 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1186 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1187 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1188 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1189 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1190 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1191 default:
1192 Res = "Unknown";
1193 }
1194 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001195 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001196 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001197 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1198 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1199 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1200 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1201 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1202 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1203 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1204 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1205 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1206 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1207 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1208 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001209 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001210 }
1211 break;
1212 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001213 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001214 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001215 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001216}
1217
1218#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1219
Rafael Espindolac66d7612014-08-17 19:09:37 +00001220bool COFFObjectFile::isRelocatableObject() const {
1221 return !DataDirectory;
1222}
1223
Rui Ueyamac2bed422013-09-27 21:04:00 +00001224bool ImportDirectoryEntryRef::
1225operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001226 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001227}
1228
Rafael Espindola5e812af2014-01-30 02:49:50 +00001229void ImportDirectoryEntryRef::moveNext() {
1230 ++Index;
David Majnemer1c0aa042016-07-31 19:25:21 +00001231 if (ImportTable[Index].isNull()) {
David Majnemerad7b7e72016-06-26 04:36:32 +00001232 Index = -1;
1233 ImportTable = nullptr;
1234 }
Rui Ueyamac2bed422013-09-27 21:04:00 +00001235}
1236
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001237std::error_code ImportDirectoryEntryRef::getImportTableEntry(
David Majnemer1c0aa042016-07-31 19:25:21 +00001238 const coff_import_directory_table_entry *&Result) const {
David Majnemerad7b7e72016-06-26 04:36:32 +00001239 return getObject(Result, OwningObject->Data, ImportTable + Index);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001240}
1241
Rui Ueyama861021f2014-10-02 22:05:29 +00001242static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001243makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001244 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001245 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001246 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001247 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001248 }
1249 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001250 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001251}
1252
Rui Ueyama15d99352014-10-03 00:41:58 +00001253static imported_symbol_iterator
1254importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001255 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001256 Object->getRvaPtr(RVA, IntPtr);
1257 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001258}
1259
Rui Ueyama15d99352014-10-03 00:41:58 +00001260static imported_symbol_iterator
1261importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001262 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001263 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001264 // Forward the pointer to the last entry which is null.
1265 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001266 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001267 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1268 while (*Entry++)
1269 ++Index;
1270 } else {
1271 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1272 while (*Entry++)
1273 ++Index;
1274 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001275 return makeImportedSymbolIterator(Object, IntPtr, Index);
1276}
1277
1278imported_symbol_iterator
1279ImportDirectoryEntryRef::imported_symbol_begin() const {
David Majnemer60049522016-07-31 19:40:02 +00001280 return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001281 OwningObject);
1282}
1283
1284imported_symbol_iterator
1285ImportDirectoryEntryRef::imported_symbol_end() const {
David Majnemer60049522016-07-31 19:40:02 +00001286 return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001287 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001288}
1289
Rui Ueyama979fb402014-10-09 02:16:38 +00001290iterator_range<imported_symbol_iterator>
1291ImportDirectoryEntryRef::imported_symbols() const {
1292 return make_range(imported_symbol_begin(), imported_symbol_end());
1293}
1294
David Majnemer60049522016-07-31 19:40:02 +00001295imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const {
1296 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1297 OwningObject);
1298}
1299
1300imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const {
1301 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1302 OwningObject);
1303}
1304
1305iterator_range<imported_symbol_iterator>
1306ImportDirectoryEntryRef::lookup_table_symbols() const {
1307 return make_range(lookup_table_begin(), lookup_table_end());
1308}
1309
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001310std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001311 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001312 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001313 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001314 return EC;
1315 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001316 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001317}
1318
Rui Ueyama1e152d52014-10-02 17:02:18 +00001319std::error_code
1320ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1321 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001322 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001323}
1324
1325std::error_code
1326ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1327 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001328 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001329}
1330
Rui Ueyama15d99352014-10-03 00:41:58 +00001331bool DelayImportDirectoryEntryRef::
1332operator==(const DelayImportDirectoryEntryRef &Other) const {
1333 return Table == Other.Table && Index == Other.Index;
1334}
1335
1336void DelayImportDirectoryEntryRef::moveNext() {
1337 ++Index;
1338}
1339
1340imported_symbol_iterator
1341DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1342 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1343 OwningObject);
1344}
1345
1346imported_symbol_iterator
1347DelayImportDirectoryEntryRef::imported_symbol_end() const {
1348 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1349 OwningObject);
1350}
1351
Rui Ueyama979fb402014-10-09 02:16:38 +00001352iterator_range<imported_symbol_iterator>
1353DelayImportDirectoryEntryRef::imported_symbols() const {
1354 return make_range(imported_symbol_begin(), imported_symbol_end());
1355}
1356
Rui Ueyama15d99352014-10-03 00:41:58 +00001357std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1358 uintptr_t IntPtr = 0;
1359 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1360 return EC;
1361 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001362 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001363}
1364
Rui Ueyama1af08652014-10-03 18:07:18 +00001365std::error_code DelayImportDirectoryEntryRef::
1366getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1367 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001368 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001369}
1370
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001371std::error_code DelayImportDirectoryEntryRef::
1372getImportAddress(int AddrIndex, uint64_t &Result) const {
1373 uint32_t RVA = Table[Index].DelayImportAddressTable +
1374 AddrIndex * (OwningObject->is64() ? 8 : 4);
1375 uintptr_t IntPtr = 0;
1376 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1377 return EC;
1378 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001379 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001380 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001381 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001382 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001383}
1384
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001385bool ExportDirectoryEntryRef::
1386operator==(const ExportDirectoryEntryRef &Other) const {
1387 return ExportTable == Other.ExportTable && Index == Other.Index;
1388}
1389
Rafael Espindola5e812af2014-01-30 02:49:50 +00001390void ExportDirectoryEntryRef::moveNext() {
1391 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001392}
1393
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001394// Returns the name of the current export symbol. If the symbol is exported only
1395// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001396std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001397 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001398 if (std::error_code EC =
1399 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001400 return EC;
1401 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001402 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001403}
1404
Rui Ueyamae5df6092014-01-17 22:02:24 +00001405// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001406std::error_code
1407ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001408 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001409 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001410}
1411
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001412// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001413std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001414 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001415 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001416}
1417
1418// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001419std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001420 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001421 if (std::error_code EC =
1422 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001423 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001424 const export_address_table_entry *entry =
1425 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001426 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001427 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001428}
1429
1430// Returns the name of the current export symbol. If the symbol is exported only
1431// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001432std::error_code
1433ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001434 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001435 if (std::error_code EC =
1436 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001437 return EC;
1438 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1439
1440 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1441 int Offset = 0;
1442 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1443 I < E; ++I, ++Offset) {
1444 if (*I != Index)
1445 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001446 if (std::error_code EC =
1447 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001448 return EC;
1449 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001450 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001451 return EC;
1452 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001453 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001454 }
1455 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001456 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001457}
1458
Rui Ueyama6161b382016-01-12 23:28:42 +00001459std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1460 const data_directory *DataEntry;
1461 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1462 return EC;
1463 uint32_t RVA;
1464 if (auto EC = getExportRVA(RVA))
1465 return EC;
1466 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1467 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1468 Result = (Begin <= RVA && RVA < End);
1469 return std::error_code();
1470}
1471
1472std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1473 uint32_t RVA;
1474 if (auto EC = getExportRVA(RVA))
1475 return EC;
1476 uintptr_t IntPtr = 0;
1477 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1478 return EC;
1479 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1480 return std::error_code();
1481}
1482
Rui Ueyama861021f2014-10-02 22:05:29 +00001483bool ImportedSymbolRef::
1484operator==(const ImportedSymbolRef &Other) const {
1485 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1486 && Index == Other.Index;
1487}
1488
1489void ImportedSymbolRef::moveNext() {
1490 ++Index;
1491}
1492
1493std::error_code
1494ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1495 uint32_t RVA;
1496 if (Entry32) {
1497 // If a symbol is imported only by ordinal, it has no name.
1498 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001499 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001500 RVA = Entry32[Index].getHintNameRVA();
1501 } else {
1502 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001503 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001504 RVA = Entry64[Index].getHintNameRVA();
1505 }
1506 uintptr_t IntPtr = 0;
1507 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1508 return EC;
1509 // +2 because the first two bytes is hint.
1510 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001511 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001512}
1513
David Majnemerad7b7e72016-06-26 04:36:32 +00001514std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1515 if (Entry32)
1516 Result = Entry32[Index].isOrdinal();
1517 else
1518 Result = Entry64[Index].isOrdinal();
1519 return std::error_code();
1520}
1521
1522std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1523 if (Entry32)
1524 Result = Entry32[Index].getHintNameRVA();
1525 else
1526 Result = Entry64[Index].getHintNameRVA();
1527 return std::error_code();
1528}
1529
Rui Ueyama861021f2014-10-02 22:05:29 +00001530std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1531 uint32_t RVA;
1532 if (Entry32) {
1533 if (Entry32[Index].isOrdinal()) {
1534 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001535 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001536 }
1537 RVA = Entry32[Index].getHintNameRVA();
1538 } else {
1539 if (Entry64[Index].isOrdinal()) {
1540 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001541 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001542 }
1543 RVA = Entry64[Index].getHintNameRVA();
1544 }
1545 uintptr_t IntPtr = 0;
1546 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1547 return EC;
1548 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001549 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001550}
1551
Rafael Espindola437b0d52014-07-31 03:12:45 +00001552ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001553ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001554 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001555 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001556 if (EC)
1557 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001558 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001559}
Rui Ueyama74e85132014-11-19 00:18:07 +00001560
1561bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1562 return Header == Other.Header && Index == Other.Index;
1563}
1564
1565void BaseRelocRef::moveNext() {
1566 // Header->BlockSize is the size of the current block, including the
1567 // size of the header itself.
1568 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001569 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001570 if (Size == Header->BlockSize) {
1571 // .reloc contains a list of base relocation blocks. Each block
1572 // consists of the header followed by entries. The header contains
1573 // how many entories will follow. When we reach the end of the
1574 // current block, proceed to the next block.
1575 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1576 reinterpret_cast<const uint8_t *>(Header) + Size);
1577 Index = 0;
1578 } else {
1579 ++Index;
1580 }
1581}
1582
1583std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1584 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1585 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001586 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001587}
1588
1589std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1590 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1591 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001592 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001593}
Eric Beckmannefef15a2017-05-08 02:47:07 +00001594
1595#define RETURN_IF_ERROR(X) \
1596 if (auto EC = errorToErrorCode(X)) \
1597 return EC;
1598
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001599ErrorOr<ArrayRef<UTF16>> ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) {
Eric Beckmannefef15a2017-05-08 02:47:07 +00001600 BinaryStreamReader Reader = BinaryStreamReader(BBS);
1601 Reader.setOffset(Offset);
1602 uint16_t Length;
1603 RETURN_IF_ERROR(Reader.readInteger(Length));
1604 ArrayRef<UTF16> RawDirString;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001605 RETURN_IF_ERROR(Reader.readArray(RawDirString, Length));
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001606 return RawDirString;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001607}
1608
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001609ErrorOr<ArrayRef<UTF16>>
Eric Beckmannefef15a2017-05-08 02:47:07 +00001610ResourceSectionRef::getEntryNameString(const coff_resource_dir_entry &Entry) {
1611 return getDirStringAtOffset(Entry.Identifier.getNameOffset());
1612}
1613
1614ErrorOr<const coff_resource_dir_table &>
1615ResourceSectionRef::getTableAtOffset(uint32_t Offset) {
1616 const coff_resource_dir_table *Table = nullptr;
1617
1618 BinaryStreamReader Reader(BBS);
1619 Reader.setOffset(Offset);
1620 RETURN_IF_ERROR(Reader.readObject(Table));
1621 assert(Table != nullptr);
1622 return *Table;
1623}
1624
1625ErrorOr<const coff_resource_dir_table &>
1626ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) {
1627 return getTableAtOffset(Entry.Offset.value());
1628}
1629
1630ErrorOr<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() {
1631 return getTableAtOffset(0);
1632}