blob: 9a760d86e7e2224aba074d889c2975dc37274e34 [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"
Zachary Turner264b5d92017-06-07 03:48:56 +000018#include "llvm/BinaryFormat/COFF.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000019#include "llvm/Object/Binary.h"
20#include "llvm/Object/COFF.h"
21#include "llvm/Object/Error.h"
22#include "llvm/Object/ObjectFile.h"
Eric Beckmannefef15a2017-05-08 02:47:07 +000023#include "llvm/Support/BinaryStreamReader.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
George Rimara25d3292017-05-27 18:10:23 +0000296uint64_t COFFObjectFile::getSectionIndex(DataRefImpl Sec) const {
297 return toSec(Sec) - SectionTable;
298}
299
Rafael Espindola80291272014-10-08 15:28:58 +0000300uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
David Majnemera9ee5c02014-10-09 08:42:31 +0000301 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000302}
303
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000304std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
305 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000306 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000307 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000308 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000309 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
310 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000311}
312
Rafael Espindola80291272014-10-08 15:28:58 +0000313uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000314 const coff_section *Sec = toSec(Ref);
David Majnemer511391f2016-03-17 16:55:18 +0000315 return Sec->getAlignment();
Michael J. Spencer79894602011-10-10 21:55:43 +0000316}
317
George Rimar401e4e52016-05-24 12:48:46 +0000318bool COFFObjectFile::isSectionCompressed(DataRefImpl Sec) const {
319 return false;
320}
321
Rafael Espindola80291272014-10-08 15:28:58 +0000322bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000323 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000324 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000325}
326
Rafael Espindola80291272014-10-08 15:28:58 +0000327bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000328 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000329 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000330}
331
Rafael Espindola80291272014-10-08 15:28:58 +0000332bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000333 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000334 const uint32_t BssFlags = COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
335 COFF::IMAGE_SCN_MEM_READ |
336 COFF::IMAGE_SCN_MEM_WRITE;
337 return (Sec->Characteristics & BssFlags) == BssFlags;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000338}
339
Rafael Espindola6bf32212015-06-24 19:57:32 +0000340unsigned COFFObjectFile::getSectionID(SectionRef Sec) const {
341 uintptr_t Offset =
342 uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
343 assert((Offset % sizeof(coff_section)) == 0);
344 return (Offset / sizeof(coff_section)) + 1;
345}
346
Rafael Espindola80291272014-10-08 15:28:58 +0000347bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000348 const coff_section *Sec = toSec(Ref);
David Majnemer1a666e02015-03-07 20:21:27 +0000349 // In COFF, a virtual section won't have any in-file
350 // content, so the file pointer to the content will be zero.
351 return Sec->PointerToRawData == 0;
Preston Gurd2138ef62012-04-12 20:13:57 +0000352}
353
David Majnemere830c602014-11-13 08:46:37 +0000354static uint32_t getNumberOfRelocations(const coff_section *Sec,
355 MemoryBufferRef M, const uint8_t *base) {
356 // The field for the number of relocations in COFF section table is only
357 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
358 // NumberOfRelocations field, and the actual relocation count is stored in the
359 // VirtualAddress field in the first relocation entry.
360 if (Sec->hasExtendedRelocations()) {
361 const coff_relocation *FirstReloc;
362 if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
363 base + Sec->PointerToRelocations)))
364 return 0;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000365 // -1 to exclude this first relocation entry.
366 return FirstReloc->VirtualAddress - 1;
David Majnemere830c602014-11-13 08:46:37 +0000367 }
368 return Sec->NumberOfRelocations;
369}
370
David Majnemer94751be2014-11-13 09:50:18 +0000371static const coff_relocation *
372getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
373 uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
374 if (!NumRelocs)
375 return nullptr;
376 auto begin = reinterpret_cast<const coff_relocation *>(
377 Base + Sec->PointerToRelocations);
378 if (Sec->hasExtendedRelocations()) {
379 // Skip the first relocation entry repurposed to store the number of
380 // relocations.
381 begin++;
382 }
383 if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
384 return nullptr;
385 return begin;
386}
387
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000388relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
389 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000390 const coff_relocation *begin = getFirstReloc(Sec, Data, base());
Rafael Espindola76d650e2015-07-06 14:26:07 +0000391 if (begin && Sec->VirtualAddress != 0)
392 report_fatal_error("Sections with relocations should have an address of 0");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000393 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000394 Ret.p = reinterpret_cast<uintptr_t>(begin);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000395 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000396}
397
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000398relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
399 const coff_section *Sec = toSec(Ref);
David Majnemer94751be2014-11-13 09:50:18 +0000400 const coff_relocation *I = getFirstReloc(Sec, Data, base());
401 if (I)
402 I += getNumberOfRelocations(Sec, Data, base());
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 DataRefImpl Ret;
David Majnemer94751be2014-11-13 09:50:18 +0000404 Ret.p = reinterpret_cast<uintptr_t>(I);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000405 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000406}
407
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000409std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000410 if (COFFHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000411 if (std::error_code EC = getObject(
412 SymbolTable16, Data, base() + getPointerToSymbolTable(),
413 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000414 return EC;
415
416 if (COFFBigObjHeader)
David Majnemer236b0ca2014-11-17 11:17:17 +0000417 if (std::error_code EC = getObject(
418 SymbolTable32, Data, base() + getPointerToSymbolTable(),
419 (uint64_t)getNumberOfSymbols() * getSymbolTableEntrySize()))
David Majnemer44f51e52014-09-10 12:51:52 +0000420 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000421
422 // Find string table. The first four byte of the string table contains the
423 // total size of the string table, including the size field itself. If the
424 // string table is empty, the value of the first four byte would be 4.
David Majnemerf69b05852014-11-14 08:15:42 +0000425 uint32_t StringTableOffset = getPointerToSymbolTable() +
426 getNumberOfSymbols() * getSymbolTableEntrySize();
427 const uint8_t *StringTableAddr = base() + StringTableOffset;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000428 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000429 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000430 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000431 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000432 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000433 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000434 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000435
Nico Rieck773a5792014-02-26 19:51:44 +0000436 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
437 // tools like cvtres write a size of 0 for an empty table instead of 4.
438 if (StringTableSize < 4)
439 StringTableSize = 4;
440
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000442 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000443 return object_error::parse_failed;
Rui Ueyama7d099192015-06-09 15:20:42 +0000444 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000445}
446
Reid Kleckner21427ad2015-10-09 00:15:08 +0000447uint64_t COFFObjectFile::getImageBase() const {
Reid Klecknere94fef72015-10-09 00:15:01 +0000448 if (PE32Header)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000449 return PE32Header->ImageBase;
Reid Klecknere94fef72015-10-09 00:15:01 +0000450 else if (PE32PlusHeader)
Reid Kleckner21427ad2015-10-09 00:15:08 +0000451 return PE32PlusHeader->ImageBase;
452 // This actually comes up in practice.
453 return 0;
Reid Klecknere94fef72015-10-09 00:15:01 +0000454}
455
Rui Ueyama215a5862014-02-20 06:51:07 +0000456// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000457std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Reid Kleckner21427ad2015-10-09 00:15:08 +0000458 uint64_t ImageBase = getImageBase();
Rui Ueyamab7a40082014-02-20 19:14:56 +0000459 uint64_t Rva = Addr - ImageBase;
460 assert(Rva <= UINT32_MAX);
461 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000462}
463
Rui Ueyamac2bed422013-09-27 21:04:00 +0000464// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000465std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000466 for (const SectionRef &S : sections()) {
467 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468 uint32_t SectionStart = Section->VirtualAddress;
469 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000470 if (SectionStart <= Addr && Addr < SectionEnd) {
471 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000472 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
Rui Ueyama7d099192015-06-09 15:20:42 +0000473 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000474 }
475 }
476 return object_error::parse_failed;
477}
478
Reid Kleckner2da433e2016-06-02 17:10:43 +0000479std::error_code
480COFFObjectFile::getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size,
481 ArrayRef<uint8_t> &Contents) const {
482 for (const SectionRef &S : sections()) {
483 const coff_section *Section = getCOFFSection(S);
484 uint32_t SectionStart = Section->VirtualAddress;
485 // Check if this RVA is within the section bounds. Be careful about integer
486 // overflow.
487 uint32_t OffsetIntoSection = RVA - SectionStart;
488 if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
489 Size <= Section->VirtualSize - OffsetIntoSection) {
490 uintptr_t Begin =
491 uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
492 Contents =
493 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
494 return std::error_code();
495 }
496 }
497 return object_error::parse_failed;
498}
499
Rui Ueyamac2bed422013-09-27 21:04:00 +0000500// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
501// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000502std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
503 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000504 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000505 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000506 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000507 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
508 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
509 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +0000510 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000511}
512
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000513std::error_code
514COFFObjectFile::getDebugPDBInfo(const debug_directory *DebugDir,
515 const codeview::DebugInfo *&PDBInfo,
516 StringRef &PDBFileName) const {
Reid Kleckner2da433e2016-06-02 17:10:43 +0000517 ArrayRef<uint8_t> InfoBytes;
518 if (std::error_code EC = getRvaAndSizeAsBytes(
519 DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
520 return EC;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000521 if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
Reid Kleckner2da433e2016-06-02 17:10:43 +0000522 return object_error::parse_failed;
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000523 PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
524 InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
Reid Kleckner2da433e2016-06-02 17:10:43 +0000525 PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
526 InfoBytes.size());
527 // Truncate the name at the first null byte. Ignore any padding.
528 PDBFileName = PDBFileName.split('\0').first;
529 return std::error_code();
530}
531
Saleem Abdulrasool01528022016-08-09 00:25:12 +0000532std::error_code
533COFFObjectFile::getDebugPDBInfo(const codeview::DebugInfo *&PDBInfo,
534 StringRef &PDBFileName) const {
Reid Klecknerf27f3f82016-06-03 20:25:09 +0000535 for (const debug_directory &D : debug_directories())
536 if (D.Type == COFF::IMAGE_DEBUG_TYPE_CODEVIEW)
537 return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
538 // If we get here, there is no PDB info to return.
539 PDBInfo = nullptr;
540 PDBFileName = StringRef();
541 return std::error_code();
542}
543
Rui Ueyamac2bed422013-09-27 21:04:00 +0000544// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000545std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000546 // First, we get the RVA of the import table. If the file lacks a pointer to
547 // the import table, do nothing.
548 const data_directory *DataEntry;
549 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000550 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000551
552 // Do nothing if the pointer to import table is NULL.
553 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000554 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000555
556 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000557
558 // Find the section that contains the RVA. This is needed because the RVA is
559 // the import table's memory address which is different from its file offset.
560 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000561 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000562 return EC;
David Majnemerad7b7e72016-06-26 04:36:32 +0000563 if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
564 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000565 ImportDirectory = reinterpret_cast<
David Majnemer1c0aa042016-07-31 19:25:21 +0000566 const coff_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000567 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000568}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000569
Rui Ueyama15d99352014-10-03 00:41:58 +0000570// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
571std::error_code COFFObjectFile::initDelayImportTablePtr() {
572 const data_directory *DataEntry;
573 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000574 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000575 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000576 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000577
578 uint32_t RVA = DataEntry->RelativeVirtualAddress;
579 NumberOfDelayImportDirectory = DataEntry->Size /
580 sizeof(delay_import_directory_table_entry) - 1;
581
582 uintptr_t IntPtr = 0;
583 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
584 return EC;
585 DelayImportDirectory = reinterpret_cast<
586 const delay_import_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000587 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +0000588}
589
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000590// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000591std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000592 // First, we get the RVA of the export table. If the file lacks a pointer to
593 // the export table, do nothing.
594 const data_directory *DataEntry;
595 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000596 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000597
598 // Do nothing if the pointer to export table is NULL.
599 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000600 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000601
602 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
603 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000604 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000605 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000606 ExportDirectory =
607 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +0000608 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000609}
610
Rui Ueyama74e85132014-11-19 00:18:07 +0000611std::error_code COFFObjectFile::initBaseRelocPtr() {
612 const data_directory *DataEntry;
613 if (getDataDirectory(COFF::BASE_RELOCATION_TABLE, DataEntry))
Rui Ueyama7d099192015-06-09 15:20:42 +0000614 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000615 if (DataEntry->RelativeVirtualAddress == 0)
Rui Ueyama7d099192015-06-09 15:20:42 +0000616 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000617
618 uintptr_t IntPtr = 0;
619 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
620 return EC;
621 BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
622 IntPtr);
623 BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
624 IntPtr + DataEntry->Size);
Rui Ueyama7d099192015-06-09 15:20:42 +0000625 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +0000626}
627
Reid Kleckner2da433e2016-06-02 17:10:43 +0000628std::error_code COFFObjectFile::initDebugDirectoryPtr() {
629 // Get the RVA of the debug directory. Do nothing if it does not exist.
630 const data_directory *DataEntry;
631 if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
632 return std::error_code();
633
634 // Do nothing if the RVA is NULL.
635 if (DataEntry->RelativeVirtualAddress == 0)
636 return std::error_code();
637
638 // Check that the size is a multiple of the entry size.
639 if (DataEntry->Size % sizeof(debug_directory) != 0)
640 return object_error::parse_failed;
641
642 uintptr_t IntPtr = 0;
643 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
644 return EC;
645 DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
646 if (std::error_code EC = getRvaPtr(
647 DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
648 return EC;
649 DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
650 return std::error_code();
651}
652
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000653std::error_code COFFObjectFile::initLoadConfigPtr() {
654 // Get the RVA of the debug directory. Do nothing if it does not exist.
655 const data_directory *DataEntry;
656 if (getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataEntry))
657 return std::error_code();
658
659 // Do nothing if the RVA is NULL.
660 if (DataEntry->RelativeVirtualAddress == 0)
661 return std::error_code();
662 uintptr_t IntPtr = 0;
663 if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
664 return EC;
665
666 LoadConfig = (const void *)IntPtr;
667 return std::error_code();
668}
669
Rafael Espindola48af1c22014-08-19 18:44:46 +0000670COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
671 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000672 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
673 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
674 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
David Majnemerad7b7e72016-06-26 04:36:32 +0000675 ImportDirectory(nullptr),
Rui Ueyama15d99352014-10-03 00:41:58 +0000676 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Reid Kleckner2da433e2016-06-02 17:10:43 +0000677 ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
678 DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000679 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000680 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000681 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000682
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000683 // The current location in the file where we are looking at.
684 uint64_t CurPtr = 0;
685
686 // PE header is optional and is present only in executables. If it exists,
687 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000688 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000689
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000690 // Check if this is a PE/COFF file.
David Majnemer50267222014-11-05 06:24:35 +0000691 if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000692 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
693 // PE signature to find 'normal' COFF header.
David Majnemer50267222014-11-05 06:24:35 +0000694 const auto *DH = reinterpret_cast<const dos_header *>(base());
695 if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
696 CurPtr = DH->AddressOfNewExeHeader;
697 // Check the PE magic bytes. ("PE\0\0")
698 if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
699 EC = object_error::parse_failed;
700 return;
701 }
702 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
703 HasPEHeader = true;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000704 }
Eric Christopheree066fc2011-04-03 22:53:19 +0000705 }
706
Rafael Espindola48af1c22014-08-19 18:44:46 +0000707 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000708 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000709
710 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
711 // import libraries share a common prefix but bigobj is more restrictive.
712 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
713 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
714 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
715 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
716 return;
717
718 // Verify that we are dealing with bigobj.
719 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
720 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
721 sizeof(COFF::BigObjMagic)) == 0) {
722 COFFHeader = nullptr;
723 CurPtr += sizeof(coff_bigobj_file_header);
724 } else {
725 // It's not a bigobj.
726 COFFBigObjHeader = nullptr;
727 }
728 }
729 if (COFFHeader) {
730 // The prior checkSize call may have failed. This isn't a hard error
731 // because we were just trying to sniff out bigobj.
Rui Ueyama7d099192015-06-09 15:20:42 +0000732 EC = std::error_code();
David Majnemer44f51e52014-09-10 12:51:52 +0000733 CurPtr += sizeof(coff_file_header);
734
735 if (COFFHeader->isImportLibrary())
736 return;
737 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000738
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000739 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000740 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000741 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000742 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000743
744 const uint8_t *DataDirAddr;
745 uint64_t DataDirSize;
David Majnemer50267222014-11-05 06:24:35 +0000746 if (Header->Magic == COFF::PE32Header::PE32) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000747 PE32Header = Header;
748 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
749 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
David Majnemer50267222014-11-05 06:24:35 +0000750 } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000751 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
752 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
753 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
754 } else {
755 // It's neither PE32 nor PE32+.
756 EC = object_error::parse_failed;
757 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000758 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000759 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000760 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000761 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000762
Rui Ueyama8950a532016-08-11 22:02:44 +0000763 if (COFFHeader)
764 CurPtr += COFFHeader->SizeOfOptionalHeader;
765
Rafael Espindola48af1c22014-08-19 18:44:46 +0000766 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer236b0ca2014-11-17 11:17:17 +0000767 (uint64_t)getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000768 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000769
Rui Ueyamac2bed422013-09-27 21:04:00 +0000770 // Initialize the pointer to the symbol table.
David Majnemer236b0ca2014-11-17 11:17:17 +0000771 if (getPointerToSymbolTable() != 0) {
David Majnemerac8cfab2016-08-30 20:20:24 +0000772 if ((EC = initSymbolTablePtr())) {
773 SymbolTable16 = nullptr;
774 SymbolTable32 = nullptr;
775 StringTable = nullptr;
776 StringTableSize = 0;
777 }
David Majnemer236b0ca2014-11-17 11:17:17 +0000778 } else {
779 // We had better not have any symbols if we don't have a symbol table.
780 if (getNumberOfSymbols() != 0) {
781 EC = object_error::parse_failed;
782 return;
783 }
784 }
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000785
Rui Ueyamac2bed422013-09-27 21:04:00 +0000786 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000787 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000788 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000789 if ((EC = initDelayImportTablePtr()))
790 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000791
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000792 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000793 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000794 return;
795
Rui Ueyama74e85132014-11-19 00:18:07 +0000796 // Initialize the pointer to the base relocation table.
797 if ((EC = initBaseRelocPtr()))
798 return;
799
Reid Kleckner2da433e2016-06-02 17:10:43 +0000800 // Initialize the pointer to the export table.
801 if ((EC = initDebugDirectoryPtr()))
802 return;
803
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000804 if ((EC = initLoadConfigPtr()))
805 return;
806
Rui Ueyama7d099192015-06-09 15:20:42 +0000807 EC = std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000808}
809
Peter Collingbourne435890a2016-11-22 03:38:40 +0000810basic_symbol_iterator COFFObjectFile::symbol_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000811 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000812 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000813 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000814}
815
Peter Collingbourne435890a2016-11-22 03:38:40 +0000816basic_symbol_iterator COFFObjectFile::symbol_end() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000817 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000818 DataRefImpl Ret;
819 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000820 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000821}
822
Rui Ueyamabc654b12013-09-27 21:47:05 +0000823import_directory_iterator COFFObjectFile::import_directory_begin() const {
David Majnemerad7b7e72016-06-26 04:36:32 +0000824 if (!ImportDirectory)
825 return import_directory_end();
David Majnemer1c0aa042016-07-31 19:25:21 +0000826 if (ImportDirectory->isNull())
David Majnemerad7b7e72016-06-26 04:36:32 +0000827 return import_directory_end();
Rui Ueyamaa045b732014-01-16 03:13:19 +0000828 return import_directory_iterator(
829 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000830}
831
Rui Ueyamabc654b12013-09-27 21:47:05 +0000832import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000833 return import_directory_iterator(
David Majnemerad7b7e72016-06-26 04:36:32 +0000834 ImportDirectoryEntryRef(nullptr, -1, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000835}
David Meyerc429b802012-03-01 22:19:54 +0000836
Rui Ueyama15d99352014-10-03 00:41:58 +0000837delay_import_directory_iterator
838COFFObjectFile::delay_import_directory_begin() const {
839 return delay_import_directory_iterator(
840 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
841}
842
843delay_import_directory_iterator
844COFFObjectFile::delay_import_directory_end() const {
845 return delay_import_directory_iterator(
846 DelayImportDirectoryEntryRef(
847 DelayImportDirectory, NumberOfDelayImportDirectory, this));
848}
849
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000850export_directory_iterator COFFObjectFile::export_directory_begin() const {
851 return export_directory_iterator(
852 ExportDirectoryEntryRef(ExportDirectory, 0, this));
853}
854
855export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000856 if (!ExportDirectory)
857 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000858 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000859 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000860 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000861}
862
Rafael Espindolab5155a52014-02-10 20:24:04 +0000863section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000864 DataRefImpl Ret;
865 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
866 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000867}
868
Rafael Espindolab5155a52014-02-10 20:24:04 +0000869section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000870 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000871 int NumSections =
872 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000873 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
874 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000875}
876
Rui Ueyama74e85132014-11-19 00:18:07 +0000877base_reloc_iterator COFFObjectFile::base_reloc_begin() const {
878 return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
879}
880
881base_reloc_iterator COFFObjectFile::base_reloc_end() const {
882 return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
883}
884
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000885uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000886 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000887}
888
889StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000890 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000891 case COFF::IMAGE_FILE_MACHINE_I386:
892 return "COFF-i386";
893 case COFF::IMAGE_FILE_MACHINE_AMD64:
894 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000895 case COFF::IMAGE_FILE_MACHINE_ARMNT:
896 return "COFF-ARM";
Martell Malone1eff5c92015-07-28 16:18:17 +0000897 case COFF::IMAGE_FILE_MACHINE_ARM64:
898 return "COFF-ARM64";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000899 default:
900 return "COFF-<unknown arch>";
901 }
902}
903
904unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000905 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000906 case COFF::IMAGE_FILE_MACHINE_I386:
907 return Triple::x86;
908 case COFF::IMAGE_FILE_MACHINE_AMD64:
909 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000910 case COFF::IMAGE_FILE_MACHINE_ARMNT:
911 return Triple::thumb;
Martell Malone1eff5c92015-07-28 16:18:17 +0000912 case COFF::IMAGE_FILE_MACHINE_ARM64:
913 return Triple::aarch64;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000914 default:
915 return Triple::UnknownArch;
916 }
917}
918
Rui Ueyama979fb402014-10-09 02:16:38 +0000919iterator_range<import_directory_iterator>
920COFFObjectFile::import_directories() const {
921 return make_range(import_directory_begin(), import_directory_end());
922}
923
924iterator_range<delay_import_directory_iterator>
925COFFObjectFile::delay_import_directories() const {
926 return make_range(delay_import_directory_begin(),
927 delay_import_directory_end());
928}
929
930iterator_range<export_directory_iterator>
931COFFObjectFile::export_directories() const {
932 return make_range(export_directory_begin(), export_directory_end());
933}
934
Rui Ueyama74e85132014-11-19 00:18:07 +0000935iterator_range<base_reloc_iterator> COFFObjectFile::base_relocs() const {
936 return make_range(base_reloc_begin(), base_reloc_end());
937}
938
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000939std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000940 Res = PE32Header;
Rui Ueyama7d099192015-06-09 15:20:42 +0000941 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000942}
943
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000944std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000945COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
946 Res = PE32PlusHeader;
Rui Ueyama7d099192015-06-09 15:20:42 +0000947 return std::error_code();
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000948}
949
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000950std::error_code
951COFFObjectFile::getDataDirectory(uint32_t Index,
952 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000953 // Error if if there's no data directory or the index is out of range.
David Majnemerf69b05852014-11-14 08:15:42 +0000954 if (!DataDirectory) {
955 Res = nullptr;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000956 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000957 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000958 assert(PE32Header || PE32PlusHeader);
959 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
960 : PE32PlusHeader->NumberOfRvaAndSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000961 if (Index >= NumEnt) {
962 Res = nullptr;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000963 return object_error::parse_failed;
David Majnemerf69b05852014-11-14 08:15:42 +0000964 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000965 Res = &DataDirectory[Index];
Rui Ueyama7d099192015-06-09 15:20:42 +0000966 return std::error_code();
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000967}
968
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000969std::error_code COFFObjectFile::getSection(int32_t Index,
970 const coff_section *&Result) const {
David Majnemer236b0ca2014-11-17 11:17:17 +0000971 Result = nullptr;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000972 if (COFF::isReservedSectionNumber(Index))
Rui Ueyama7d099192015-06-09 15:20:42 +0000973 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000974 if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000975 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000976 Result = SectionTable + (Index - 1);
Rui Ueyama7d099192015-06-09 15:20:42 +0000977 return std::error_code();
David Majnemer236b0ca2014-11-17 11:17:17 +0000978 }
979 return object_error::parse_failed;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000980}
981
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000982std::error_code COFFObjectFile::getString(uint32_t Offset,
983 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000984 if (StringTableSize <= 4)
985 // Tried to get a string from an empty string table.
986 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000987 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000988 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000989 Result = StringRef(StringTable + Offset);
Rui Ueyama7d099192015-06-09 15:20:42 +0000990 return std::error_code();
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000991}
992
David Majnemer44f51e52014-09-10 12:51:52 +0000993std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000994 StringRef &Res) const {
Rui Ueyamae40d30f2015-06-30 00:03:56 +0000995 return getSymbolName(Symbol.getGeneric(), Res);
996}
997
998std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
999 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001000 // Check for string table entry. First 4 bytes are 0.
Rui Ueyamae40d30f2015-06-30 00:03:56 +00001001 if (Symbol->Name.Offset.Zeroes == 0) {
1002 if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001003 return EC;
Rui Ueyama7d099192015-06-09 15:20:42 +00001004 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001005 }
1006
Rui Ueyamae40d30f2015-06-30 00:03:56 +00001007 if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001008 // Null terminated, let ::strlen figure out the length.
Rui Ueyamae40d30f2015-06-30 00:03:56 +00001009 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001010 else
1011 // Not null terminated, use all 8 bytes.
Rui Ueyamae40d30f2015-06-30 00:03:56 +00001012 Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001013 return std::error_code();
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +00001014}
1015
David Majnemer44f51e52014-09-10 12:51:52 +00001016ArrayRef<uint8_t>
1017COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +00001018 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001019
David Majnemer44f51e52014-09-10 12:51:52 +00001020 size_t SymbolSize = getSymbolTableEntrySize();
1021 if (Symbol.getNumberOfAuxSymbols() > 0) {
1022 // AUX data comes immediately after the symbol in COFF
1023 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Eugene Zelenkod341c932017-04-19 23:02:10 +00001024#ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001025 // Verify that the Aux symbol points to a valid entry in the symbol table.
1026 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +00001027 if (Offset < getPointerToSymbolTable() ||
1028 Offset >=
1029 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +00001030 report_fatal_error("Aux Symbol data was outside of symbol table.");
1031
David Majnemer44f51e52014-09-10 12:51:52 +00001032 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
1033 "Aux Symbol data did not point to the beginning of a symbol");
Eugene Zelenkod341c932017-04-19 23:02:10 +00001034#endif
Marshall Clowbfb85e62012-06-15 01:15:47 +00001035 }
David Majnemer44f51e52014-09-10 12:51:52 +00001036 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +00001037}
1038
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001039std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
1040 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001041 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +00001042 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001043 // Null terminated, let ::strlen figure out the length.
1044 Name = Sec->Name;
1045 else
1046 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +00001047 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001048
1049 // Check for string table entry. First byte is '/'.
David Majnemer2314b3d2014-11-13 07:42:09 +00001050 if (Name.startswith("/")) {
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001051 uint32_t Offset;
David Majnemer2314b3d2014-11-13 07:42:09 +00001052 if (Name.startswith("//")) {
Nico Rieck9d2c15e2014-02-22 16:12:20 +00001053 if (decodeBase64StringEntry(Name.substr(2), Offset))
1054 return object_error::parse_failed;
1055 } else {
1056 if (Name.substr(1).getAsInteger(10, Offset))
1057 return object_error::parse_failed;
1058 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001059 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001060 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001061 }
1062
1063 Res = Name;
Rui Ueyama7d099192015-06-09 15:20:42 +00001064 return std::error_code();
Michael J. Spencer53c2d542012-03-19 20:27:15 +00001065}
1066
David Majnemera9ee5c02014-10-09 08:42:31 +00001067uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1068 // SizeOfRawData and VirtualSize change what they represent depending on
1069 // whether or not we have an executable image.
1070 //
1071 // For object files, SizeOfRawData contains the size of section's data;
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001072 // VirtualSize should be zero but isn't due to buggy COFF writers.
David Majnemera9ee5c02014-10-09 08:42:31 +00001073 //
1074 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1075 // actual section size is in VirtualSize. It is possible for VirtualSize to
1076 // be greater than SizeOfRawData; the contents past that point should be
1077 // considered to be zero.
Rui Ueyamad5297ee2015-07-04 03:25:51 +00001078 if (getDOSHeader())
1079 return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1080 return Sec->SizeOfRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001081}
1082
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001083std::error_code
1084COFFObjectFile::getSectionContents(const coff_section *Sec,
1085 ArrayRef<uint8_t> &Res) const {
David Majnemere2129662016-05-28 19:45:51 +00001086 // In COFF, a virtual section won't have any in-file
1087 // content, so the file pointer to the content will be zero.
1088 if (Sec->PointerToRawData == 0)
Shoaib Meenaiee97c5f2017-05-14 18:34:56 +00001089 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001090 // The only thing that we need to verify is that the contents is contained
1091 // within the file bounds. We don't need to make sure it doesn't cover other
1092 // data, as there's nothing that says that is not allowed.
1093 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +00001094 uint32_t SectionSize = getSectionSize(Sec);
David Majnemere830c602014-11-13 08:46:37 +00001095 if (checkOffset(Data, ConStart, SectionSize))
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001096 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +00001097 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Rui Ueyama7d099192015-06-09 15:20:42 +00001098 return std::error_code();
Michael J. Spencer9da9e692012-03-19 20:27:37 +00001099}
1100
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001101const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001102 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001103}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001104
Rafael Espindola5e812af2014-01-30 02:49:50 +00001105void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001106 Rel.p = reinterpret_cast<uintptr_t>(
1107 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001108}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001109
Rafael Espindola96d071c2015-06-29 23:29:12 +00001110uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
David Majnemer58323a92014-11-13 07:42:07 +00001111 const coff_relocation *R = toRel(Rel);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001112 return R->VirtualAddress;
Danil Malyshevcbe72fc2011-11-29 17:40:10 +00001113}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001114
Rafael Espindola806f0062013-06-05 01:33:53 +00001115symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +00001116 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001117 DataRefImpl Ref;
David Majnemer236b0ca2014-11-17 11:17:17 +00001118 if (R->SymbolTableIndex >= getNumberOfSymbols())
1119 return symbol_end();
David Majnemer44f51e52014-09-10 12:51:52 +00001120 if (SymbolTable16)
1121 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1122 else if (SymbolTable32)
1123 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1124 else
David Majnemerc7353b52014-11-25 07:43:14 +00001125 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001126 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001127}
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001128
Rafael Espindola99c041b2015-06-30 01:53:01 +00001129uint64_t COFFObjectFile::getRelocationType(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001130 const coff_relocation* R = toRel(Rel);
Rafael Espindola99c041b2015-06-30 01:53:01 +00001131 return R->Type;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001132}
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001133
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001134const coff_section *
1135COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
1136 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001137}
1138
David Majnemer44f51e52014-09-10 12:51:52 +00001139COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
1140 if (SymbolTable16)
1141 return toSymb<coff_symbol16>(Ref);
1142 if (SymbolTable32)
1143 return toSymb<coff_symbol32>(Ref);
1144 llvm_unreachable("no symbol table pointer!");
1145}
1146
1147COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1148 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +00001149}
1150
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001151const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001152COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
1153 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +00001154}
1155
Rui Ueyama6a75acb2015-06-25 00:07:39 +00001156iterator_range<const coff_relocation *>
1157COFFObjectFile::getRelocations(const coff_section *Sec) const {
1158 const coff_relocation *I = getFirstReloc(Sec, Data, base());
1159 const coff_relocation *E = I;
1160 if (I)
1161 E += getNumberOfRelocations(Sec, Data, base());
1162 return make_range(I, E);
1163}
1164
Alexey Samsonov27dc8392014-03-18 06:53:02 +00001165#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1166 case COFF::reloc_type: \
1167 Res = #reloc_type; \
1168 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001169
Rafael Espindola41bb4322015-06-30 04:08:37 +00001170void COFFObjectFile::getRelocationTypeName(
1171 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001172 const coff_relocation *Reloc = toRel(Rel);
1173 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +00001174 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001175 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001176 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001177 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
1178 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
1179 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
1180 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
1181 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
1182 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
1183 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
1184 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
1185 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
1186 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
1187 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
1188 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
1189 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
1190 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
1191 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
1192 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
1193 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
1194 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001195 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001196 }
1197 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001198 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1199 switch (Reloc->Type) {
1200 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1201 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1202 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1203 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1204 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1205 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1206 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1207 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1208 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1209 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1210 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1211 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1212 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1213 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1214 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1215 default:
1216 Res = "Unknown";
1217 }
1218 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001219 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001220 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001221 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1222 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1223 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1224 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1225 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1226 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1227 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1228 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1229 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1230 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1231 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1232 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001233 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001234 }
1235 break;
1236 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001237 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001238 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001239 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001240}
1241
1242#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1243
Rafael Espindolac66d7612014-08-17 19:09:37 +00001244bool COFFObjectFile::isRelocatableObject() const {
1245 return !DataDirectory;
1246}
1247
Rui Ueyamac2bed422013-09-27 21:04:00 +00001248bool ImportDirectoryEntryRef::
1249operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001250 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001251}
1252
Rafael Espindola5e812af2014-01-30 02:49:50 +00001253void ImportDirectoryEntryRef::moveNext() {
1254 ++Index;
David Majnemer1c0aa042016-07-31 19:25:21 +00001255 if (ImportTable[Index].isNull()) {
David Majnemerad7b7e72016-06-26 04:36:32 +00001256 Index = -1;
1257 ImportTable = nullptr;
1258 }
Rui Ueyamac2bed422013-09-27 21:04:00 +00001259}
1260
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001261std::error_code ImportDirectoryEntryRef::getImportTableEntry(
David Majnemer1c0aa042016-07-31 19:25:21 +00001262 const coff_import_directory_table_entry *&Result) const {
David Majnemerad7b7e72016-06-26 04:36:32 +00001263 return getObject(Result, OwningObject->Data, ImportTable + Index);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001264}
1265
Rui Ueyama861021f2014-10-02 22:05:29 +00001266static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001267makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001268 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001269 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001270 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001271 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001272 }
1273 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001274 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001275}
1276
Rui Ueyama15d99352014-10-03 00:41:58 +00001277static imported_symbol_iterator
1278importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001279 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001280 Object->getRvaPtr(RVA, IntPtr);
1281 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001282}
1283
Rui Ueyama15d99352014-10-03 00:41:58 +00001284static imported_symbol_iterator
1285importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001286 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001287 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001288 // Forward the pointer to the last entry which is null.
1289 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001290 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001291 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1292 while (*Entry++)
1293 ++Index;
1294 } else {
1295 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1296 while (*Entry++)
1297 ++Index;
1298 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001299 return makeImportedSymbolIterator(Object, IntPtr, Index);
1300}
1301
1302imported_symbol_iterator
1303ImportDirectoryEntryRef::imported_symbol_begin() const {
David Majnemer60049522016-07-31 19:40:02 +00001304 return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001305 OwningObject);
1306}
1307
1308imported_symbol_iterator
1309ImportDirectoryEntryRef::imported_symbol_end() const {
David Majnemer60049522016-07-31 19:40:02 +00001310 return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
Rui Ueyama15d99352014-10-03 00:41:58 +00001311 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001312}
1313
Rui Ueyama979fb402014-10-09 02:16:38 +00001314iterator_range<imported_symbol_iterator>
1315ImportDirectoryEntryRef::imported_symbols() const {
1316 return make_range(imported_symbol_begin(), imported_symbol_end());
1317}
1318
David Majnemer60049522016-07-31 19:40:02 +00001319imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_begin() const {
1320 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1321 OwningObject);
1322}
1323
1324imported_symbol_iterator ImportDirectoryEntryRef::lookup_table_end() const {
1325 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1326 OwningObject);
1327}
1328
1329iterator_range<imported_symbol_iterator>
1330ImportDirectoryEntryRef::lookup_table_symbols() const {
1331 return make_range(lookup_table_begin(), lookup_table_end());
1332}
1333
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001334std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001335 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001336 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001337 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001338 return EC;
1339 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001340 return std::error_code();
Rui Ueyamac2bed422013-09-27 21:04:00 +00001341}
1342
Rui Ueyama1e152d52014-10-02 17:02:18 +00001343std::error_code
1344ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1345 Result = ImportTable[Index].ImportLookupTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001346 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001347}
1348
1349std::error_code
1350ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1351 Result = ImportTable[Index].ImportAddressTableRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001352 return std::error_code();
Rui Ueyama1e152d52014-10-02 17:02:18 +00001353}
1354
Rui Ueyama15d99352014-10-03 00:41:58 +00001355bool DelayImportDirectoryEntryRef::
1356operator==(const DelayImportDirectoryEntryRef &Other) const {
1357 return Table == Other.Table && Index == Other.Index;
1358}
1359
1360void DelayImportDirectoryEntryRef::moveNext() {
1361 ++Index;
1362}
1363
1364imported_symbol_iterator
1365DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1366 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1367 OwningObject);
1368}
1369
1370imported_symbol_iterator
1371DelayImportDirectoryEntryRef::imported_symbol_end() const {
1372 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1373 OwningObject);
1374}
1375
Rui Ueyama979fb402014-10-09 02:16:38 +00001376iterator_range<imported_symbol_iterator>
1377DelayImportDirectoryEntryRef::imported_symbols() const {
1378 return make_range(imported_symbol_begin(), imported_symbol_end());
1379}
1380
Rui Ueyama15d99352014-10-03 00:41:58 +00001381std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1382 uintptr_t IntPtr = 0;
1383 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1384 return EC;
1385 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001386 return std::error_code();
Rui Ueyama15d99352014-10-03 00:41:58 +00001387}
1388
Rui Ueyama1af08652014-10-03 18:07:18 +00001389std::error_code DelayImportDirectoryEntryRef::
1390getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1391 Result = Table;
Rui Ueyama7d099192015-06-09 15:20:42 +00001392 return std::error_code();
Rui Ueyama1af08652014-10-03 18:07:18 +00001393}
1394
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001395std::error_code DelayImportDirectoryEntryRef::
1396getImportAddress(int AddrIndex, uint64_t &Result) const {
1397 uint32_t RVA = Table[Index].DelayImportAddressTable +
1398 AddrIndex * (OwningObject->is64() ? 8 : 4);
1399 uintptr_t IntPtr = 0;
1400 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1401 return EC;
1402 if (OwningObject->is64())
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001403 Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001404 else
Rui Ueyama5dcf11d2014-11-13 20:07:06 +00001405 Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001406 return std::error_code();
Rui Ueyamaffa4ceb2014-11-13 03:22:54 +00001407}
1408
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001409bool ExportDirectoryEntryRef::
1410operator==(const ExportDirectoryEntryRef &Other) const {
1411 return ExportTable == Other.ExportTable && Index == Other.Index;
1412}
1413
Rafael Espindola5e812af2014-01-30 02:49:50 +00001414void ExportDirectoryEntryRef::moveNext() {
1415 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001416}
1417
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001418// Returns the name of the current export symbol. If the symbol is exported only
1419// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001420std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001421 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001422 if (std::error_code EC =
1423 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001424 return EC;
1425 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001426 return std::error_code();
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001427}
1428
Rui Ueyamae5df6092014-01-17 22:02:24 +00001429// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001430std::error_code
1431ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001432 Result = ExportTable->OrdinalBase;
Rui Ueyama7d099192015-06-09 15:20:42 +00001433 return std::error_code();
Rui Ueyamae5df6092014-01-17 22:02:24 +00001434}
1435
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001436// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001437std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001438 Result = ExportTable->OrdinalBase + Index;
Rui Ueyama7d099192015-06-09 15:20:42 +00001439 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001440}
1441
1442// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001443std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001444 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001445 if (std::error_code EC =
1446 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001447 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001448 const export_address_table_entry *entry =
1449 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001450 Result = entry[Index].ExportRVA;
Rui Ueyama7d099192015-06-09 15:20:42 +00001451 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001452}
1453
1454// Returns the name of the current export symbol. If the symbol is exported only
1455// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001456std::error_code
1457ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001458 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001459 if (std::error_code EC =
1460 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001461 return EC;
1462 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1463
1464 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1465 int Offset = 0;
1466 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1467 I < E; ++I, ++Offset) {
1468 if (*I != Index)
1469 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001470 if (std::error_code EC =
1471 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001472 return EC;
1473 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001474 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001475 return EC;
1476 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyama7d099192015-06-09 15:20:42 +00001477 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001478 }
1479 Result = "";
Rui Ueyama7d099192015-06-09 15:20:42 +00001480 return std::error_code();
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001481}
1482
Rui Ueyama6161b382016-01-12 23:28:42 +00001483std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1484 const data_directory *DataEntry;
1485 if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1486 return EC;
1487 uint32_t RVA;
1488 if (auto EC = getExportRVA(RVA))
1489 return EC;
1490 uint32_t Begin = DataEntry->RelativeVirtualAddress;
1491 uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1492 Result = (Begin <= RVA && RVA < End);
1493 return std::error_code();
1494}
1495
1496std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1497 uint32_t RVA;
1498 if (auto EC = getExportRVA(RVA))
1499 return EC;
1500 uintptr_t IntPtr = 0;
1501 if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1502 return EC;
1503 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1504 return std::error_code();
1505}
1506
Rui Ueyama861021f2014-10-02 22:05:29 +00001507bool ImportedSymbolRef::
1508operator==(const ImportedSymbolRef &Other) const {
1509 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1510 && Index == Other.Index;
1511}
1512
1513void ImportedSymbolRef::moveNext() {
1514 ++Index;
1515}
1516
1517std::error_code
1518ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1519 uint32_t RVA;
1520 if (Entry32) {
1521 // If a symbol is imported only by ordinal, it has no name.
1522 if (Entry32[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001523 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001524 RVA = Entry32[Index].getHintNameRVA();
1525 } else {
1526 if (Entry64[Index].isOrdinal())
Rui Ueyama7d099192015-06-09 15:20:42 +00001527 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001528 RVA = Entry64[Index].getHintNameRVA();
1529 }
1530 uintptr_t IntPtr = 0;
1531 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1532 return EC;
1533 // +2 because the first two bytes is hint.
1534 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
Rui Ueyama7d099192015-06-09 15:20:42 +00001535 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001536}
1537
David Majnemerad7b7e72016-06-26 04:36:32 +00001538std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1539 if (Entry32)
1540 Result = Entry32[Index].isOrdinal();
1541 else
1542 Result = Entry64[Index].isOrdinal();
1543 return std::error_code();
1544}
1545
1546std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1547 if (Entry32)
1548 Result = Entry32[Index].getHintNameRVA();
1549 else
1550 Result = Entry64[Index].getHintNameRVA();
1551 return std::error_code();
1552}
1553
Rui Ueyama861021f2014-10-02 22:05:29 +00001554std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1555 uint32_t RVA;
1556 if (Entry32) {
1557 if (Entry32[Index].isOrdinal()) {
1558 Result = Entry32[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001559 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001560 }
1561 RVA = Entry32[Index].getHintNameRVA();
1562 } else {
1563 if (Entry64[Index].isOrdinal()) {
1564 Result = Entry64[Index].getOrdinal();
Rui Ueyama7d099192015-06-09 15:20:42 +00001565 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001566 }
1567 RVA = Entry64[Index].getHintNameRVA();
1568 }
1569 uintptr_t IntPtr = 0;
1570 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1571 return EC;
1572 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
Rui Ueyama7d099192015-06-09 15:20:42 +00001573 return std::error_code();
Rui Ueyama861021f2014-10-02 22:05:29 +00001574}
1575
Rafael Espindola437b0d52014-07-31 03:12:45 +00001576ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001577ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001578 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001579 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001580 if (EC)
1581 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001582 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001583}
Rui Ueyama74e85132014-11-19 00:18:07 +00001584
1585bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1586 return Header == Other.Header && Index == Other.Index;
1587}
1588
1589void BaseRelocRef::moveNext() {
1590 // Header->BlockSize is the size of the current block, including the
1591 // size of the header itself.
1592 uint32_t Size = sizeof(*Header) +
Rui Ueyama970dda22014-11-19 02:07:10 +00001593 sizeof(coff_base_reloc_block_entry) * (Index + 1);
Rui Ueyama74e85132014-11-19 00:18:07 +00001594 if (Size == Header->BlockSize) {
1595 // .reloc contains a list of base relocation blocks. Each block
1596 // consists of the header followed by entries. The header contains
1597 // how many entories will follow. When we reach the end of the
1598 // current block, proceed to the next block.
1599 Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1600 reinterpret_cast<const uint8_t *>(Header) + Size);
1601 Index = 0;
1602 } else {
1603 ++Index;
1604 }
1605}
1606
1607std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1608 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1609 Type = Entry[Index].getType();
Rui Ueyama7d099192015-06-09 15:20:42 +00001610 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001611}
1612
1613std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1614 auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1615 Result = Header->PageRVA + Entry[Index].getOffset();
Rui Ueyama7d099192015-06-09 15:20:42 +00001616 return std::error_code();
Rui Ueyama74e85132014-11-19 00:18:07 +00001617}
Eric Beckmannefef15a2017-05-08 02:47:07 +00001618
1619#define RETURN_IF_ERROR(X) \
1620 if (auto EC = errorToErrorCode(X)) \
1621 return EC;
1622
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001623ErrorOr<ArrayRef<UTF16>> ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) {
Eric Beckmannefef15a2017-05-08 02:47:07 +00001624 BinaryStreamReader Reader = BinaryStreamReader(BBS);
1625 Reader.setOffset(Offset);
1626 uint16_t Length;
1627 RETURN_IF_ERROR(Reader.readInteger(Length));
1628 ArrayRef<UTF16> RawDirString;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001629 RETURN_IF_ERROR(Reader.readArray(RawDirString, Length));
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001630 return RawDirString;
Eric Beckmannefef15a2017-05-08 02:47:07 +00001631}
1632
Eric Beckmanncd704cb2017-05-08 02:47:42 +00001633ErrorOr<ArrayRef<UTF16>>
Eric Beckmannefef15a2017-05-08 02:47:07 +00001634ResourceSectionRef::getEntryNameString(const coff_resource_dir_entry &Entry) {
1635 return getDirStringAtOffset(Entry.Identifier.getNameOffset());
1636}
1637
1638ErrorOr<const coff_resource_dir_table &>
1639ResourceSectionRef::getTableAtOffset(uint32_t Offset) {
1640 const coff_resource_dir_table *Table = nullptr;
1641
1642 BinaryStreamReader Reader(BBS);
1643 Reader.setOffset(Offset);
1644 RETURN_IF_ERROR(Reader.readObject(Table));
1645 assert(Table != nullptr);
1646 return *Table;
1647}
1648
1649ErrorOr<const coff_resource_dir_table &>
1650ResourceSectionRef::getEntrySubDir(const coff_resource_dir_entry &Entry) {
1651 return getTableAtOffset(Entry.Offset.value());
1652}
1653
1654ErrorOr<const coff_resource_dir_table &> ResourceSectionRef::getBaseTable() {
1655 return getTableAtOffset(0);
1656}