blob: f533d5e21e64101b2a94bf4b49060b4bbf13b033 [file] [log] [blame]
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencere5fd0042011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000019#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000022#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000023#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000024
25using namespace llvm;
26using namespace object;
27
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000028using support::ulittle16_t;
29using support::ulittle32_t;
Rui Ueyama861021f2014-10-02 22:05:29 +000030using support::ulittle64_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000031using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000033// Returns false if size is greater than the buffer size. And sets ec.
Rafael Espindola48af1c22014-08-19 18:44:46 +000034static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000035 if (M.getBufferSize() < Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000036 EC = object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000037 return false;
38 }
39 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000040}
41
Rui Ueyamaed64342b2013-07-19 23:23:29 +000042// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
43// Returns unexpected_eof if error.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000044template <typename T>
Rafael Espindola48af1c22014-08-19 18:44:46 +000045static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000046 const uint8_t *Ptr,
47 const size_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000048 uintptr_t Addr = uintptr_t(Ptr);
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000049 if (Addr + Size < Addr || Addr + Size < Size ||
50 Addr + Size > uintptr_t(M.getBufferEnd())) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000051 return object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000052 }
Rui Ueyamaed64342b2013-07-19 23:23:29 +000053 Obj = reinterpret_cast<const T *>(Addr);
54 return object_error::success;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000055}
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000056
Nico Rieck9d2c15e2014-02-22 16:12:20 +000057// Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
58// prefixed slashes.
59static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
60 assert(Str.size() <= 6 && "String too long, possible overflow.");
61 if (Str.size() > 6)
62 return true;
63
64 uint64_t Value = 0;
65 while (!Str.empty()) {
66 unsigned CharVal;
67 if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
68 CharVal = Str[0] - 'A';
69 else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
70 CharVal = Str[0] - 'a' + 26;
71 else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
72 CharVal = Str[0] - '0' + 52;
73 else if (Str[0] == '+') // 62
Rui Ueyama5500b072014-02-25 23:49:11 +000074 CharVal = 62;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000075 else if (Str[0] == '/') // 63
Rui Ueyama5500b072014-02-25 23:49:11 +000076 CharVal = 63;
Nico Rieck9d2c15e2014-02-22 16:12:20 +000077 else
78 return true;
79
80 Value = (Value * 64) + CharVal;
81 Str = Str.substr(1);
82 }
83
84 if (Value > std::numeric_limits<uint32_t>::max())
85 return true;
86
87 Result = static_cast<uint32_t>(Value);
88 return false;
89}
90
David Majnemer44f51e52014-09-10 12:51:52 +000091template <typename coff_symbol_type>
92const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
93 const coff_symbol_type *Addr =
94 reinterpret_cast<const coff_symbol_type *>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000095
David Majnemer44f51e52014-09-10 12:51:52 +000096#ifndef NDEBUG
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000097 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +000098 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +000099 if (Offset < getPointerToSymbolTable() ||
100 Offset >= getPointerToSymbolTable() +
101 (getNumberOfSymbols() * sizeof(coff_symbol_type)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000102 report_fatal_error("Symbol was outside of symbol table.");
103
David Majnemer44f51e52014-09-10 12:51:52 +0000104 assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
105 "Symbol did not point to the beginning of a symbol");
106#endif
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000107
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000108 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000109}
110
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000111const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
112 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113
114# ifndef NDEBUG
115 // Verify that the section points to a valid entry in the section table.
David Majnemer44f51e52014-09-10 12:51:52 +0000116 if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000117 report_fatal_error("Section was outside of section table.");
118
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000119 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
120 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000121 "Section did not point to the beginning of a section");
122# endif
123
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000124 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000125}
126
Rafael Espindola5e812af2014-01-30 02:49:50 +0000127void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000128 if (SymbolTable16) {
129 const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
130 Symb += 1 + Symb->NumberOfAuxSymbols;
131 Ref.p = reinterpret_cast<uintptr_t>(Symb);
132 } else if (SymbolTable32) {
133 const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
134 Symb += 1 + Symb->NumberOfAuxSymbols;
135 Ref.p = reinterpret_cast<uintptr_t>(Symb);
136 } else {
137 llvm_unreachable("no symbol table pointer!");
138 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000139}
140
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000141std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
142 StringRef &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000143 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000144 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000145}
146
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000147std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
148 uint64_t &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000149 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Craig Topper2617dcc2014-04-15 06:32:26 +0000150 const coff_section *Section = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000151 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000152 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000153
David Majnemer44f51e52014-09-10 12:51:52 +0000154 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED)
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000155 Result = UnknownAddressOrSize;
156 else if (Section)
David Majnemer44f51e52014-09-10 12:51:52 +0000157 Result = Section->VirtualAddress + Symb.getValue();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000158 else
David Majnemer44f51e52014-09-10 12:51:52 +0000159 Result = Symb.getValue();
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000160 return object_error::success;
161}
162
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000163std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
164 SymbolRef::Type &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000165 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000166 Result = SymbolRef::ST_Other;
David Majnemer44f51e52014-09-10 12:51:52 +0000167
168 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
169 Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer7e4b9762012-02-29 02:11:55 +0000170 Result = SymbolRef::ST_Unknown;
David Majnemer44f51e52014-09-10 12:51:52 +0000171 } else if (Symb.isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000172 Result = SymbolRef::ST_Function;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000173 } else {
David Majnemer44f51e52014-09-10 12:51:52 +0000174 uint32_t Characteristics = 0;
175 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
176 const coff_section *Section = nullptr;
177 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
178 return EC;
179 Characteristics = Section->Characteristics;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000180 }
Rui Ueyama5efa6652014-01-16 20:22:55 +0000181 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
182 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
183 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000184 }
185 return object_error::success;
186}
187
Rafael Espindola20122a42014-01-31 20:57:12 +0000188uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000189 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000190 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000191
Rafael Espindola975e1152014-02-04 22:50:47 +0000192 // TODO: Correctly set SF_FormatSpecific, SF_Common
David Meyer7e4b9762012-02-29 02:11:55 +0000193
David Majnemer44f51e52014-09-10 12:51:52 +0000194 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
195 if (Symb.getValue() == 0)
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000196 Result |= SymbolRef::SF_Undefined;
197 else
198 Result |= SymbolRef::SF_Common;
199 }
200
David Meyer1df4b842012-02-28 23:47:53 +0000201
202 // TODO: This are certainly too restrictive.
David Majnemer44f51e52014-09-10 12:51:52 +0000203 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000204 Result |= SymbolRef::SF_Global;
205
David Majnemer44f51e52014-09-10 12:51:52 +0000206 if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000207 Result |= SymbolRef::SF_Weak;
208
David Majnemer44f51e52014-09-10 12:51:52 +0000209 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000210 Result |= SymbolRef::SF_Absolute;
211
Rafael Espindola20122a42014-01-31 20:57:12 +0000212 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000213}
214
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000215std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
216 uint64_t &Result) const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000217 // FIXME: Return the correct size. This requires looking at all the symbols
218 // in the same section as this symbol, and looking for either the next
219 // symbol, or the end of the section.
David Majnemer44f51e52014-09-10 12:51:52 +0000220 COFFSymbolRef Symb = getCOFFSymbol(Ref);
Craig Topper2617dcc2014-04-15 06:32:26 +0000221 const coff_section *Section = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000222 if (std::error_code EC = getSection(Symb.getSectionNumber(), Section))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000223 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000224
David Majnemer44f51e52014-09-10 12:51:52 +0000225 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000226 Result = UnknownAddressOrSize;
227 else if (Section)
David Majnemer44f51e52014-09-10 12:51:52 +0000228 Result = Section->SizeOfRawData - Symb.getValue();
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000229 else
230 Result = 0;
231 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000232}
233
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000234std::error_code
235COFFObjectFile::getSymbolSection(DataRefImpl Ref,
236 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000237 COFFSymbolRef Symb = getCOFFSymbol(Ref);
238 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000239 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000240 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000241 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000242 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000243 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000244 DataRefImpl Ref;
245 Ref.p = reinterpret_cast<uintptr_t>(Sec);
246 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000247 }
248 return object_error::success;
249}
250
Rafael Espindola5e812af2014-01-30 02:49:50 +0000251void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000252 const coff_section *Sec = toSec(Ref);
253 Sec += 1;
254 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000255}
256
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000257std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
258 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000259 const coff_section *Sec = toSec(Ref);
260 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000261}
262
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000263std::error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
264 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000265 const coff_section *Sec = toSec(Ref);
266 Result = Sec->VirtualAddress;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000267 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000268}
269
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000270std::error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
271 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000272 const coff_section *Sec = toSec(Ref);
273 Result = Sec->SizeOfRawData;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000274 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000275}
276
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000277std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
278 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000279 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000280 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000281 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000282 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
283 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000284}
285
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000286std::error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
287 uint64_t &Res) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000288 const coff_section *Sec = toSec(Ref);
289 if (!Sec)
Michael J. Spencer79894602011-10-10 21:55:43 +0000290 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000291 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000292 return object_error::success;
293}
294
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000295std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
296 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000297 const coff_section *Sec = toSec(Ref);
298 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000299 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000300}
301
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000302std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
303 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000304 const coff_section *Sec = toSec(Ref);
305 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000306 return object_error::success;
307}
308
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000309std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
310 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000311 const coff_section *Sec = toSec(Ref);
312 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000313 return object_error::success;
314}
315
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000316std::error_code
317COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
318 bool &Result) const {
Preston Gurd2138ef62012-04-12 20:13:57 +0000319 // FIXME: Unimplemented
320 Result = true;
321 return object_error::success;
322}
323
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000324std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
325 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000326 const coff_section *Sec = toSec(Ref);
327 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000328 return object_error::success;
329}
330
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000331std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
332 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000333 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000334 Result = false;
335 return object_error::success;
336}
337
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000338std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
339 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000340 // FIXME: Unimplemented.
341 Result = false;
342 return object_error::success;
343}
344
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000345std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
346 DataRefImpl SymbRef,
347 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000348 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000349 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Craig Topper2617dcc2014-04-15 06:32:26 +0000350 const coff_section *SymbSec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000351 if (std::error_code EC = getSection(Symb.getSectionNumber(), SymbSec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000352 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000353 if (SymbSec == Sec)
Michael J. Spencer9a288512011-10-13 20:36:54 +0000354 Result = true;
355 else
356 Result = false;
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000357 return object_error::success;
358}
359
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000360relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
361 const coff_section *Sec = toSec(Ref);
362 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000363 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000364 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000365 } else {
366 auto begin = reinterpret_cast<const coff_relocation*>(
367 base() + Sec->PointerToRelocations);
368 if (Sec->hasExtendedRelocations()) {
369 // Skip the first relocation entry repurposed to store the number of
370 // relocations.
371 begin++;
372 }
373 Ret.p = reinterpret_cast<uintptr_t>(begin);
374 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000375 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000376}
377
Rui Ueyama827c8a22014-03-21 00:44:19 +0000378static uint32_t getNumberOfRelocations(const coff_section *Sec,
379 const uint8_t *base) {
380 // The field for the number of relocations in COFF section table is only
381 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
382 // NumberOfRelocations field, and the actual relocation count is stored in the
383 // VirtualAddress field in the first relocation entry.
384 if (Sec->hasExtendedRelocations()) {
385 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
386 base + Sec->PointerToRelocations);
387 return FirstReloc->VirtualAddress;
388 }
389 return Sec->NumberOfRelocations;
390}
391
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000392relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
393 const coff_section *Sec = toSec(Ref);
394 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000395 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000396 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000397 } else {
398 auto begin = reinterpret_cast<const coff_relocation*>(
399 base() + Sec->PointerToRelocations);
400 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
401 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
402 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000404}
405
Rui Ueyamac2bed422013-09-27 21:04:00 +0000406// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000407std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000408 if (COFFHeader)
409 if (std::error_code EC =
410 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
411 getNumberOfSymbols() * getSymbolTableEntrySize()))
412 return EC;
413
414 if (COFFBigObjHeader)
415 if (std::error_code EC =
416 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
417 getNumberOfSymbols() * getSymbolTableEntrySize()))
418 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000419
420 // Find string table. The first four byte of the string table contains the
421 // total size of the string table, including the size field itself. If the
422 // string table is empty, the value of the first four byte would be 4.
423 const uint8_t *StringTableAddr =
David Majnemer44f51e52014-09-10 12:51:52 +0000424 base() + getPointerToSymbolTable() +
425 getNumberOfSymbols() * getSymbolTableEntrySize();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000426 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000427 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000428 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000430 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000431 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000432 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000433
Nico Rieck773a5792014-02-26 19:51:44 +0000434 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
435 // tools like cvtres write a size of 0 for an empty table instead of 4.
436 if (StringTableSize < 4)
437 StringTableSize = 4;
438
Rui Ueyamac2bed422013-09-27 21:04:00 +0000439 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000440 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441 return object_error::parse_failed;
442 return object_error::success;
443}
444
Rui Ueyama215a5862014-02-20 06:51:07 +0000445// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000446std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000447 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
448 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000449 uint64_t Rva = Addr - ImageBase;
450 assert(Rva <= UINT32_MAX);
451 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000452}
453
Rui Ueyamac2bed422013-09-27 21:04:00 +0000454// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000455std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000456 for (const SectionRef &S : sections()) {
457 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458 uint32_t SectionStart = Section->VirtualAddress;
459 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000460 if (SectionStart <= Addr && Addr < SectionEnd) {
461 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000462 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
463 return object_error::success;
464 }
465 }
466 return object_error::parse_failed;
467}
468
469// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
470// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000471std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
472 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000473 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000474 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000475 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000476 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
477 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
478 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
479 return object_error::success;
480}
481
482// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000483std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000484 // First, we get the RVA of the import table. If the file lacks a pointer to
485 // the import table, do nothing.
486 const data_directory *DataEntry;
487 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
488 return object_error::success;
489
490 // Do nothing if the pointer to import table is NULL.
491 if (DataEntry->RelativeVirtualAddress == 0)
492 return object_error::success;
493
494 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000495 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000496 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000497 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000498
499 // Find the section that contains the RVA. This is needed because the RVA is
500 // the import table's memory address which is different from its file offset.
501 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000502 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000503 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000504 ImportDirectory = reinterpret_cast<
505 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000506 return object_error::success;
507}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000508
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000509// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000510std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000511 // First, we get the RVA of the export table. If the file lacks a pointer to
512 // the export table, do nothing.
513 const data_directory *DataEntry;
514 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
515 return object_error::success;
516
517 // Do nothing if the pointer to export table is NULL.
518 if (DataEntry->RelativeVirtualAddress == 0)
519 return object_error::success;
520
521 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
522 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000523 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000524 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000525 ExportDirectory =
526 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000527 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000528}
529
Rafael Espindola48af1c22014-08-19 18:44:46 +0000530COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
531 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000532 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
533 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
534 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
535 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000536 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000537 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000538 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000539 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000540
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000541 // The current location in the file where we are looking at.
542 uint64_t CurPtr = 0;
543
544 // PE header is optional and is present only in executables. If it exists,
545 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000546 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000547
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000548 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000549 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000550 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
551 // PE signature to find 'normal' COFF header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000552 if (!checkSize(Data, EC, 0x3c + 8))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000553 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000554 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
555 // Check the PE magic bytes. ("PE\0\0")
David Majnemer44f51e52014-09-10 12:51:52 +0000556 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
557 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000558 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000559 return;
560 }
David Majnemer44f51e52014-09-10 12:51:52 +0000561 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000562 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000563 }
564
Rafael Espindola48af1c22014-08-19 18:44:46 +0000565 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000566 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000567
568 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
569 // import libraries share a common prefix but bigobj is more restrictive.
570 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
571 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
572 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
573 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
574 return;
575
576 // Verify that we are dealing with bigobj.
577 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
578 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
579 sizeof(COFF::BigObjMagic)) == 0) {
580 COFFHeader = nullptr;
581 CurPtr += sizeof(coff_bigobj_file_header);
582 } else {
583 // It's not a bigobj.
584 COFFBigObjHeader = nullptr;
585 }
586 }
587 if (COFFHeader) {
588 // The prior checkSize call may have failed. This isn't a hard error
589 // because we were just trying to sniff out bigobj.
590 EC = object_error::success;
591 CurPtr += sizeof(coff_file_header);
592
593 if (COFFHeader->isImportLibrary())
594 return;
595 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000596
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000597 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000598 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000599 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000600 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000601
602 const uint8_t *DataDirAddr;
603 uint64_t DataDirSize;
604 if (Header->Magic == 0x10b) {
605 PE32Header = Header;
606 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
607 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
608 } else if (Header->Magic == 0x20b) {
609 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
610 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
611 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
612 } else {
613 // It's neither PE32 nor PE32+.
614 EC = object_error::parse_failed;
615 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000616 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000617 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000618 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000619 CurPtr += COFFHeader->SizeOfOptionalHeader;
620 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000621
Rafael Espindola48af1c22014-08-19 18:44:46 +0000622 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000623 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000624 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000625
Rui Ueyamac2bed422013-09-27 21:04:00 +0000626 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000627 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000628 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000629 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000630
Rui Ueyamac2bed422013-09-27 21:04:00 +0000631 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000632 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000633 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000634
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000635 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000636 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000637 return;
638
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000639 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000640}
641
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000642basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000643 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000644 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000645 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000646}
647
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000648basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000649 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000650 DataRefImpl Ret;
651 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000652 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000653}
654
Rui Ueyamabc654b12013-09-27 21:47:05 +0000655import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000656 return import_directory_iterator(
657 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000658}
659
Rui Ueyamabc654b12013-09-27 21:47:05 +0000660import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000661 return import_directory_iterator(
662 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000663}
David Meyerc429b802012-03-01 22:19:54 +0000664
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000665export_directory_iterator COFFObjectFile::export_directory_begin() const {
666 return export_directory_iterator(
667 ExportDirectoryEntryRef(ExportDirectory, 0, this));
668}
669
670export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000671 if (!ExportDirectory)
672 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000673 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000674 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000675 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000676}
677
Rafael Espindolab5155a52014-02-10 20:24:04 +0000678section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000679 DataRefImpl Ret;
680 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
681 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000682}
683
Rafael Espindolab5155a52014-02-10 20:24:04 +0000684section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000685 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000686 int NumSections =
687 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000688 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
689 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000690}
691
692uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000693 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000694}
695
696StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000697 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000698 case COFF::IMAGE_FILE_MACHINE_I386:
699 return "COFF-i386";
700 case COFF::IMAGE_FILE_MACHINE_AMD64:
701 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000702 case COFF::IMAGE_FILE_MACHINE_ARMNT:
703 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000704 default:
705 return "COFF-<unknown arch>";
706 }
707}
708
709unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000710 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000711 case COFF::IMAGE_FILE_MACHINE_I386:
712 return Triple::x86;
713 case COFF::IMAGE_FILE_MACHINE_AMD64:
714 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000715 case COFF::IMAGE_FILE_MACHINE_ARMNT:
716 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000717 default:
718 return Triple::UnknownArch;
719 }
720}
721
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000722std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000723 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000724 return object_error::success;
725}
726
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000727std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000728COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
729 Res = PE32PlusHeader;
730 return object_error::success;
731}
732
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000733std::error_code
734COFFObjectFile::getDataDirectory(uint32_t Index,
735 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000736 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000737 if (!DataDirectory)
738 return object_error::parse_failed;
739 assert(PE32Header || PE32PlusHeader);
740 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
741 : PE32PlusHeader->NumberOfRvaAndSize;
742 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000743 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000744 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000745 return object_error::success;
746}
747
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000748std::error_code COFFObjectFile::getSection(int32_t Index,
749 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000750 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000751 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000752 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000753 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000754 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000755 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000756 else
757 return object_error::parse_failed;
758 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000759}
760
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000761std::error_code COFFObjectFile::getString(uint32_t Offset,
762 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000763 if (StringTableSize <= 4)
764 // Tried to get a string from an empty string table.
765 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000766 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000767 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000768 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000769 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000770}
771
David Majnemer44f51e52014-09-10 12:51:52 +0000772std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000773 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000774 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000775 if (Symbol.getStringTableOffset().Zeroes == 0) {
776 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000777 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000778 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000779 return object_error::success;
780 }
781
David Majnemer44f51e52014-09-10 12:51:52 +0000782 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000783 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000784 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000785 else
786 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000787 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000788 return object_error::success;
789}
790
David Majnemer44f51e52014-09-10 12:51:52 +0000791ArrayRef<uint8_t>
792COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000793 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000794
David Majnemer44f51e52014-09-10 12:51:52 +0000795 size_t SymbolSize = getSymbolTableEntrySize();
796 if (Symbol.getNumberOfAuxSymbols() > 0) {
797 // AUX data comes immediately after the symbol in COFF
798 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000799# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000800 // Verify that the Aux symbol points to a valid entry in the symbol table.
801 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000802 if (Offset < getPointerToSymbolTable() ||
803 Offset >=
804 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000805 report_fatal_error("Aux Symbol data was outside of symbol table.");
806
David Majnemer44f51e52014-09-10 12:51:52 +0000807 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
808 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000809# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000810 }
David Majnemer44f51e52014-09-10 12:51:52 +0000811 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000812}
813
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000814std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
815 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000816 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000817 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000818 // Null terminated, let ::strlen figure out the length.
819 Name = Sec->Name;
820 else
821 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000822 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000823
824 // Check for string table entry. First byte is '/'.
825 if (Name[0] == '/') {
826 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000827 if (Name[1] == '/') {
828 if (decodeBase64StringEntry(Name.substr(2), Offset))
829 return object_error::parse_failed;
830 } else {
831 if (Name.substr(1).getAsInteger(10, Offset))
832 return object_error::parse_failed;
833 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000834 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000835 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000836 }
837
838 Res = Name;
839 return object_error::success;
840}
841
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000842std::error_code
843COFFObjectFile::getSectionContents(const coff_section *Sec,
844 ArrayRef<uint8_t> &Res) const {
David Majnemerdac39852014-09-26 22:32:16 +0000845 // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
846 // do anything interesting for them.
847 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
848 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000849 // The only thing that we need to verify is that the contents is contained
850 // within the file bounds. We don't need to make sure it doesn't cover other
851 // data, as there's nothing that says that is not allowed.
852 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
853 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000854 if (ConEnd > uintptr_t(Data.getBufferEnd()))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000855 return object_error::parse_failed;
Craig Toppere1d12942014-08-27 05:25:25 +0000856 Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart),
857 Sec->SizeOfRawData);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000858 return object_error::success;
859}
860
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000861const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000862 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000863}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000864
Rafael Espindola5e812af2014-01-30 02:49:50 +0000865void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000866 Rel.p = reinterpret_cast<uintptr_t>(
867 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000868}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000869
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000870std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
871 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000872 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000873}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000874
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000875std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
876 uint64_t &Res) const {
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000877 Res = toRel(Rel)->VirtualAddress;
878 return object_error::success;
879}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000880
Rafael Espindola806f0062013-06-05 01:33:53 +0000881symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000882 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000883 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +0000884 if (SymbolTable16)
885 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
886 else if (SymbolTable32)
887 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
888 else
889 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000890 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000891}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000892
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000893std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
894 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000895 const coff_relocation* R = toRel(Rel);
896 Res = R->Type;
897 return object_error::success;
898}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000899
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000900const coff_section *
901COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
902 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000903}
904
David Majnemer44f51e52014-09-10 12:51:52 +0000905COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
906 if (SymbolTable16)
907 return toSymb<coff_symbol16>(Ref);
908 if (SymbolTable32)
909 return toSymb<coff_symbol32>(Ref);
910 llvm_unreachable("no symbol table pointer!");
911}
912
913COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
914 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000915}
916
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000917const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000918COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
919 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +0000920}
921
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000922#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
923 case COFF::reloc_type: \
924 Res = #reloc_type; \
925 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000926
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000927std::error_code
928COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
929 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000930 const coff_relocation *Reloc = toRel(Rel);
931 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +0000932 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000933 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000934 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000935 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
936 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
937 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
938 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
939 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
940 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
941 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
942 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
943 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
944 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
945 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
946 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
947 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
948 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
949 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
950 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
951 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
952 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000953 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000954 }
955 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +0000956 case COFF::IMAGE_FILE_MACHINE_ARMNT:
957 switch (Reloc->Type) {
958 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
959 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
960 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
961 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
962 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
963 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
964 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
965 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
966 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
967 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
968 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
969 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
970 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
971 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
972 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
973 default:
974 Res = "Unknown";
975 }
976 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000977 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000978 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000979 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
980 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
983 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
984 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
985 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
986 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
988 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
989 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
990 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000991 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000992 }
993 break;
994 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000995 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000996 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000997 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000998 return object_error::success;
999}
1000
1001#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1002
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001003std::error_code
1004COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1005 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001006 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001007 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001008 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1009 if (std::error_code EC = Symb.getError())
1010 return EC;
1011 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001012 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001013 if (std::error_code EC = getSymbolName(Sym, SymName))
1014 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001015 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001016 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001017}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001018
Rafael Espindolac66d7612014-08-17 19:09:37 +00001019bool COFFObjectFile::isRelocatableObject() const {
1020 return !DataDirectory;
1021}
1022
Rui Ueyamac2bed422013-09-27 21:04:00 +00001023bool ImportDirectoryEntryRef::
1024operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001025 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001026}
1027
Rafael Espindola5e812af2014-01-30 02:49:50 +00001028void ImportDirectoryEntryRef::moveNext() {
1029 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001030}
1031
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001032std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1033 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001034 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001035 return object_error::success;
1036}
1037
Rui Ueyama861021f2014-10-02 22:05:29 +00001038static imported_symbol_iterator
1039makeImportedSymbolIterator(const COFFObjectFile *OwningObject,
1040 uintptr_t Ptr, int Index) {
1041 if (OwningObject->getBytesInAddress() == 4) {
1042 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1043 return imported_symbol_iterator(ImportedSymbolRef(P, Index, OwningObject));
1044 }
1045 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1046 return imported_symbol_iterator(ImportedSymbolRef(P, Index, OwningObject));
1047}
1048
1049imported_symbol_iterator
1050ImportDirectoryEntryRef::imported_symbol_begin() const {
1051 uintptr_t IntPtr = 0;
1052 OwningObject->getRvaPtr(ImportTable[Index].ImportLookupTableRVA, IntPtr);
1053 return makeImportedSymbolIterator(OwningObject, IntPtr, 0);
1054}
1055
1056imported_symbol_iterator
1057ImportDirectoryEntryRef::imported_symbol_end() const {
1058 uintptr_t IntPtr = 0;
1059 OwningObject->getRvaPtr(ImportTable[Index].ImportLookupTableRVA, IntPtr);
1060 // Forward the pointer to the last entry which is null.
1061 int Index = 0;
1062 if (OwningObject->getBytesInAddress() == 4) {
1063 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1064 while (*Entry++)
1065 ++Index;
1066 } else {
1067 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1068 while (*Entry++)
1069 ++Index;
1070 }
1071 return makeImportedSymbolIterator(OwningObject, IntPtr, Index);
1072}
1073
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001074std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001075 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001076 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001077 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001078 return EC;
1079 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001080 return object_error::success;
1081}
1082
Rui Ueyama1e152d52014-10-02 17:02:18 +00001083std::error_code
1084ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1085 Result = ImportTable[Index].ImportLookupTableRVA;
1086 return object_error::success;
1087}
1088
1089std::error_code
1090ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1091 Result = ImportTable[Index].ImportAddressTableRVA;
1092 return object_error::success;
1093}
1094
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001095std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001096 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001097 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001098 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1099 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001100 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001101 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1102 return object_error::success;
1103}
1104
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001105bool ExportDirectoryEntryRef::
1106operator==(const ExportDirectoryEntryRef &Other) const {
1107 return ExportTable == Other.ExportTable && Index == Other.Index;
1108}
1109
Rafael Espindola5e812af2014-01-30 02:49:50 +00001110void ExportDirectoryEntryRef::moveNext() {
1111 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001112}
1113
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001114// Returns the name of the current export symbol. If the symbol is exported only
1115// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001116std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001117 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001118 if (std::error_code EC =
1119 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001120 return EC;
1121 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1122 return object_error::success;
1123}
1124
Rui Ueyamae5df6092014-01-17 22:02:24 +00001125// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001126std::error_code
1127ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001128 Result = ExportTable->OrdinalBase;
1129 return object_error::success;
1130}
1131
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001132// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001133std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001134 Result = ExportTable->OrdinalBase + Index;
1135 return object_error::success;
1136}
1137
1138// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001139std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001140 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001141 if (std::error_code EC =
1142 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001143 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001144 const export_address_table_entry *entry =
1145 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001146 Result = entry[Index].ExportRVA;
1147 return object_error::success;
1148}
1149
1150// Returns the name of the current export symbol. If the symbol is exported only
1151// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001152std::error_code
1153ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001154 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001155 if (std::error_code EC =
1156 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001157 return EC;
1158 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1159
1160 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1161 int Offset = 0;
1162 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1163 I < E; ++I, ++Offset) {
1164 if (*I != Index)
1165 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001166 if (std::error_code EC =
1167 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001168 return EC;
1169 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001170 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001171 return EC;
1172 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1173 return object_error::success;
1174 }
1175 Result = "";
1176 return object_error::success;
1177}
1178
Rui Ueyama861021f2014-10-02 22:05:29 +00001179bool ImportedSymbolRef::
1180operator==(const ImportedSymbolRef &Other) const {
1181 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1182 && Index == Other.Index;
1183}
1184
1185void ImportedSymbolRef::moveNext() {
1186 ++Index;
1187}
1188
1189std::error_code
1190ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1191 uint32_t RVA;
1192 if (Entry32) {
1193 // If a symbol is imported only by ordinal, it has no name.
1194 if (Entry32[Index].isOrdinal())
1195 return object_error::success;
1196 RVA = Entry32[Index].getHintNameRVA();
1197 } else {
1198 if (Entry64[Index].isOrdinal())
1199 return object_error::success;
1200 RVA = Entry64[Index].getHintNameRVA();
1201 }
1202 uintptr_t IntPtr = 0;
1203 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1204 return EC;
1205 // +2 because the first two bytes is hint.
1206 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1207 return object_error::success;
1208}
1209
1210std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1211 uint32_t RVA;
1212 if (Entry32) {
1213 if (Entry32[Index].isOrdinal()) {
1214 Result = Entry32[Index].getOrdinal();
1215 return object_error::success;
1216 }
1217 RVA = Entry32[Index].getHintNameRVA();
1218 } else {
1219 if (Entry64[Index].isOrdinal()) {
1220 Result = Entry64[Index].getOrdinal();
1221 return object_error::success;
1222 }
1223 RVA = Entry64[Index].getHintNameRVA();
1224 }
1225 uintptr_t IntPtr = 0;
1226 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1227 return EC;
1228 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1229 return object_error::success;
1230}
1231
Rafael Espindola437b0d52014-07-31 03:12:45 +00001232ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001233ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001234 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001235 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001236 if (EC)
1237 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001238 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001239}