blob: ea134b2499e2921fc7ee8ac1bed3c0a1dcc75579 [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::ulittle8_t;
29using support::ulittle16_t;
30using support::ulittle32_t;
31using 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;
495 NumberOfImportDirectory = DataEntry->Size /
496 sizeof(import_directory_table_entry);
497
498 // Find the section that contains the RVA. This is needed because the RVA is
499 // the import table's memory address which is different from its file offset.
500 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000501 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000502 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000503 ImportDirectory = reinterpret_cast<
504 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000505 return object_error::success;
506}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000507
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000508// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000509std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000510 // First, we get the RVA of the export table. If the file lacks a pointer to
511 // the export table, do nothing.
512 const data_directory *DataEntry;
513 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
514 return object_error::success;
515
516 // Do nothing if the pointer to export table is NULL.
517 if (DataEntry->RelativeVirtualAddress == 0)
518 return object_error::success;
519
520 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
521 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000522 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000523 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000524 ExportDirectory =
525 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000526 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000527}
528
Rafael Espindola48af1c22014-08-19 18:44:46 +0000529COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
530 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000531 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
532 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
533 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
534 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000535 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000536 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000537 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000538 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000539
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000540 // The current location in the file where we are looking at.
541 uint64_t CurPtr = 0;
542
543 // PE header is optional and is present only in executables. If it exists,
544 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000545 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000546
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000547 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000548 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000549 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
550 // PE signature to find 'normal' COFF header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000551 if (!checkSize(Data, EC, 0x3c + 8))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000552 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000553 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
554 // Check the PE magic bytes. ("PE\0\0")
David Majnemer44f51e52014-09-10 12:51:52 +0000555 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
556 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000557 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000558 return;
559 }
David Majnemer44f51e52014-09-10 12:51:52 +0000560 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000561 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000562 }
563
Rafael Espindola48af1c22014-08-19 18:44:46 +0000564 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000565 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000566
567 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
568 // import libraries share a common prefix but bigobj is more restrictive.
569 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
570 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
571 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
572 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
573 return;
574
575 // Verify that we are dealing with bigobj.
576 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
577 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
578 sizeof(COFF::BigObjMagic)) == 0) {
579 COFFHeader = nullptr;
580 CurPtr += sizeof(coff_bigobj_file_header);
581 } else {
582 // It's not a bigobj.
583 COFFBigObjHeader = nullptr;
584 }
585 }
586 if (COFFHeader) {
587 // The prior checkSize call may have failed. This isn't a hard error
588 // because we were just trying to sniff out bigobj.
589 EC = object_error::success;
590 CurPtr += sizeof(coff_file_header);
591
592 if (COFFHeader->isImportLibrary())
593 return;
594 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000595
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000596 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000597 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000598 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000599 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000600
601 const uint8_t *DataDirAddr;
602 uint64_t DataDirSize;
603 if (Header->Magic == 0x10b) {
604 PE32Header = Header;
605 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
606 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
607 } else if (Header->Magic == 0x20b) {
608 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
609 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
610 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
611 } else {
612 // It's neither PE32 nor PE32+.
613 EC = object_error::parse_failed;
614 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000615 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000616 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000617 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000618 CurPtr += COFFHeader->SizeOfOptionalHeader;
619 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000620
Rafael Espindola48af1c22014-08-19 18:44:46 +0000621 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000622 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000623 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000624
Rui Ueyamac2bed422013-09-27 21:04:00 +0000625 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000626 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000627 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000628 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000629
Rui Ueyamac2bed422013-09-27 21:04:00 +0000630 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000631 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000632 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000633
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000634 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000635 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000636 return;
637
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000638 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000639}
640
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000641basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000642 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000643 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000644 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000645}
646
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000647basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000648 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000649 DataRefImpl Ret;
650 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000651 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000652}
653
Rui Ueyamabc654b12013-09-27 21:47:05 +0000654import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000655 return import_directory_iterator(
656 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000657}
658
Rui Ueyamabc654b12013-09-27 21:47:05 +0000659import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000660 return import_directory_iterator(
661 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000662}
David Meyerc429b802012-03-01 22:19:54 +0000663
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000664export_directory_iterator COFFObjectFile::export_directory_begin() const {
665 return export_directory_iterator(
666 ExportDirectoryEntryRef(ExportDirectory, 0, this));
667}
668
669export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000670 if (!ExportDirectory)
671 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000672 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000673 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000674 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000675}
676
Rafael Espindolab5155a52014-02-10 20:24:04 +0000677section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000678 DataRefImpl Ret;
679 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
680 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000681}
682
Rafael Espindolab5155a52014-02-10 20:24:04 +0000683section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000684 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000685 int NumSections =
686 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000687 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
688 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000689}
690
691uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000692 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000693}
694
695StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000696 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000697 case COFF::IMAGE_FILE_MACHINE_I386:
698 return "COFF-i386";
699 case COFF::IMAGE_FILE_MACHINE_AMD64:
700 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000701 case COFF::IMAGE_FILE_MACHINE_ARMNT:
702 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000703 default:
704 return "COFF-<unknown arch>";
705 }
706}
707
708unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000709 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000710 case COFF::IMAGE_FILE_MACHINE_I386:
711 return Triple::x86;
712 case COFF::IMAGE_FILE_MACHINE_AMD64:
713 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000714 case COFF::IMAGE_FILE_MACHINE_ARMNT:
715 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000716 default:
717 return Triple::UnknownArch;
718 }
719}
720
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000721std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000722 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000723 return object_error::success;
724}
725
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000726std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000727COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
728 Res = PE32PlusHeader;
729 return object_error::success;
730}
731
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000732std::error_code
733COFFObjectFile::getDataDirectory(uint32_t Index,
734 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000735 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000736 if (!DataDirectory)
737 return object_error::parse_failed;
738 assert(PE32Header || PE32PlusHeader);
739 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
740 : PE32PlusHeader->NumberOfRvaAndSize;
741 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000742 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000743 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000744 return object_error::success;
745}
746
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000747std::error_code COFFObjectFile::getSection(int32_t Index,
748 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000749 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000750 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000751 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000752 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000753 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000754 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000755 else
756 return object_error::parse_failed;
757 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000758}
759
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000760std::error_code COFFObjectFile::getString(uint32_t Offset,
761 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000762 if (StringTableSize <= 4)
763 // Tried to get a string from an empty string table.
764 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000765 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000766 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000767 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000768 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000769}
770
David Majnemer44f51e52014-09-10 12:51:52 +0000771template <typename coff_symbol_type>
772std::error_code
773COFFObjectFile::getSymbol(uint32_t Index,
774 const coff_symbol_type *&Result) const {
775 if (Index < getNumberOfSymbols())
776 Result = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000777 else
778 return object_error::parse_failed;
779 return object_error::success;
780}
781
David Majnemer44f51e52014-09-10 12:51:52 +0000782std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000783 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000784 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000785 if (Symbol.getStringTableOffset().Zeroes == 0) {
786 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000787 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000788 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000789 return object_error::success;
790 }
791
David Majnemer44f51e52014-09-10 12:51:52 +0000792 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000793 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000794 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000795 else
796 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000797 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000798 return object_error::success;
799}
800
David Majnemer44f51e52014-09-10 12:51:52 +0000801ArrayRef<uint8_t>
802COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000803 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000804
David Majnemer44f51e52014-09-10 12:51:52 +0000805 size_t SymbolSize = getSymbolTableEntrySize();
806 if (Symbol.getNumberOfAuxSymbols() > 0) {
807 // AUX data comes immediately after the symbol in COFF
808 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000809# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000810 // Verify that the Aux symbol points to a valid entry in the symbol table.
811 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000812 if (Offset < getPointerToSymbolTable() ||
813 Offset >=
814 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000815 report_fatal_error("Aux Symbol data was outside of symbol table.");
816
David Majnemer44f51e52014-09-10 12:51:52 +0000817 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
818 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000819# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000820 }
David Majnemer44f51e52014-09-10 12:51:52 +0000821 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000822}
823
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000824std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
825 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000826 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000827 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000828 // Null terminated, let ::strlen figure out the length.
829 Name = Sec->Name;
830 else
831 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000832 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000833
834 // Check for string table entry. First byte is '/'.
835 if (Name[0] == '/') {
836 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000837 if (Name[1] == '/') {
838 if (decodeBase64StringEntry(Name.substr(2), Offset))
839 return object_error::parse_failed;
840 } else {
841 if (Name.substr(1).getAsInteger(10, Offset))
842 return object_error::parse_failed;
843 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000844 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000845 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000846 }
847
848 Res = Name;
849 return object_error::success;
850}
851
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000852std::error_code
853COFFObjectFile::getSectionContents(const coff_section *Sec,
854 ArrayRef<uint8_t> &Res) const {
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000855 // The only thing that we need to verify is that the contents is contained
856 // within the file bounds. We don't need to make sure it doesn't cover other
857 // data, as there's nothing that says that is not allowed.
858 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
859 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000860 if (ConEnd > uintptr_t(Data.getBufferEnd()))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000861 return object_error::parse_failed;
Craig Toppere1d12942014-08-27 05:25:25 +0000862 Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart),
863 Sec->SizeOfRawData);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000864 return object_error::success;
865}
866
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000867const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000868 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000869}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000870
Rafael Espindola5e812af2014-01-30 02:49:50 +0000871void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000872 Rel.p = reinterpret_cast<uintptr_t>(
873 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000874}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000875
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000876std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
877 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000878 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000879}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000880
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000881std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
882 uint64_t &Res) const {
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000883 Res = toRel(Rel)->VirtualAddress;
884 return object_error::success;
885}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000886
Rafael Espindola806f0062013-06-05 01:33:53 +0000887symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000888 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000889 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +0000890 if (SymbolTable16)
891 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
892 else if (SymbolTable32)
893 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
894 else
895 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000896 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000897}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000898
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000899std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
900 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000901 const coff_relocation* R = toRel(Rel);
902 Res = R->Type;
903 return object_error::success;
904}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000905
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000906const coff_section *
907COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
908 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000909}
910
David Majnemer44f51e52014-09-10 12:51:52 +0000911COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
912 if (SymbolTable16)
913 return toSymb<coff_symbol16>(Ref);
914 if (SymbolTable32)
915 return toSymb<coff_symbol32>(Ref);
916 llvm_unreachable("no symbol table pointer!");
917}
918
919COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
920 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000921}
922
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000923const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000924COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
925 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +0000926}
927
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000928#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
929 case COFF::reloc_type: \
930 Res = #reloc_type; \
931 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000932
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000933std::error_code
934COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
935 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000936 const coff_relocation *Reloc = toRel(Rel);
937 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +0000938 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000939 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000940 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000941 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
942 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
943 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
944 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
945 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
946 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
947 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
948 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
949 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
950 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
951 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
952 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
953 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
954 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
955 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
956 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
957 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
958 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000959 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000960 }
961 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +0000962 case COFF::IMAGE_FILE_MACHINE_ARMNT:
963 switch (Reloc->Type) {
964 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
965 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
966 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
967 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
968 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
969 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
970 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
971 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
972 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
973 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
974 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
975 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
976 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
977 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
978 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
979 default:
980 Res = "Unknown";
981 }
982 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000983 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000984 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000985 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
986 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
988 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
989 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
990 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
991 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
992 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
993 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
996 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000997 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000998 }
999 break;
1000 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001001 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001002 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001003 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001004 return object_error::success;
1005}
1006
1007#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1008
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001009std::error_code
1010COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1011 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001012 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001013 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001014 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1015 if (std::error_code EC = Symb.getError())
1016 return EC;
1017 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001018 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001019 if (std::error_code EC = getSymbolName(Sym, SymName))
1020 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001021 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001022 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001023}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001024
Rafael Espindolac66d7612014-08-17 19:09:37 +00001025bool COFFObjectFile::isRelocatableObject() const {
1026 return !DataDirectory;
1027}
1028
Rui Ueyamac2bed422013-09-27 21:04:00 +00001029bool ImportDirectoryEntryRef::
1030operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001031 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001032}
1033
Rafael Espindola5e812af2014-01-30 02:49:50 +00001034void ImportDirectoryEntryRef::moveNext() {
1035 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001036}
1037
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001038std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1039 const import_directory_table_entry *&Result) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001040 Result = ImportTable;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001041 return object_error::success;
1042}
1043
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001044std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001045 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001046 if (std::error_code EC =
1047 OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001048 return EC;
1049 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001050 return object_error::success;
1051}
1052
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001053std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001054 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001055 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001056 if (std::error_code EC =
Rui Ueyamaa045b732014-01-16 03:13:19 +00001057 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
1058 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001059 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1060 return object_error::success;
1061}
1062
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001063bool ExportDirectoryEntryRef::
1064operator==(const ExportDirectoryEntryRef &Other) const {
1065 return ExportTable == Other.ExportTable && Index == Other.Index;
1066}
1067
Rafael Espindola5e812af2014-01-30 02:49:50 +00001068void ExportDirectoryEntryRef::moveNext() {
1069 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001070}
1071
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001072// Returns the name of the current export symbol. If the symbol is exported only
1073// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001074std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001075 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001076 if (std::error_code EC =
1077 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001078 return EC;
1079 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1080 return object_error::success;
1081}
1082
Rui Ueyamae5df6092014-01-17 22:02:24 +00001083// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001084std::error_code
1085ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001086 Result = ExportTable->OrdinalBase;
1087 return object_error::success;
1088}
1089
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001090// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001091std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001092 Result = ExportTable->OrdinalBase + Index;
1093 return object_error::success;
1094}
1095
1096// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001097std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001098 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001099 if (std::error_code EC =
1100 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001101 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001102 const export_address_table_entry *entry =
1103 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001104 Result = entry[Index].ExportRVA;
1105 return object_error::success;
1106}
1107
1108// Returns the name of the current export symbol. If the symbol is exported only
1109// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001110std::error_code
1111ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001112 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001113 if (std::error_code EC =
1114 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001115 return EC;
1116 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1117
1118 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1119 int Offset = 0;
1120 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1121 I < E; ++I, ++Offset) {
1122 if (*I != Index)
1123 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001124 if (std::error_code EC =
1125 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001126 return EC;
1127 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001128 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001129 return EC;
1130 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1131 return object_error::success;
1132 }
1133 Result = "";
1134 return object_error::success;
1135}
1136
Rafael Espindola437b0d52014-07-31 03:12:45 +00001137ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001138ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001139 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001140 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001141 if (EC)
1142 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001143 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001144}