blob: be19847f6ac1453035bc483f60f2d88087c1d6c1 [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);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000289 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000290 return object_error::success;
291}
292
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000293std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
294 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000295 const coff_section *Sec = toSec(Ref);
296 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000297 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000298}
299
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000300std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
301 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000302 const coff_section *Sec = toSec(Ref);
303 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000304 return object_error::success;
305}
306
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000307std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
308 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000309 const coff_section *Sec = toSec(Ref);
310 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000311 return object_error::success;
312}
313
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000314std::error_code
315COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
316 bool &Result) const {
Preston Gurd2138ef62012-04-12 20:13:57 +0000317 // FIXME: Unimplemented
318 Result = true;
319 return object_error::success;
320}
321
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000322std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
323 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000324 const coff_section *Sec = toSec(Ref);
325 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000326 return object_error::success;
327}
328
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000329std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
330 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000331 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000332 Result = false;
333 return object_error::success;
334}
335
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000336std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
337 bool &Result) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000338 // FIXME: Unimplemented.
339 Result = false;
340 return object_error::success;
341}
342
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000343std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
344 DataRefImpl SymbRef,
345 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000346 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000347 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000348 int32_t SecNumber = (Sec - SectionTable) + 1;
349 Result = SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000350 return object_error::success;
351}
352
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000353relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
354 const coff_section *Sec = toSec(Ref);
355 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000356 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000357 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000358 } else {
359 auto begin = reinterpret_cast<const coff_relocation*>(
360 base() + Sec->PointerToRelocations);
361 if (Sec->hasExtendedRelocations()) {
362 // Skip the first relocation entry repurposed to store the number of
363 // relocations.
364 begin++;
365 }
366 Ret.p = reinterpret_cast<uintptr_t>(begin);
367 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000368 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000369}
370
Rui Ueyama827c8a22014-03-21 00:44:19 +0000371static uint32_t getNumberOfRelocations(const coff_section *Sec,
372 const uint8_t *base) {
373 // The field for the number of relocations in COFF section table is only
374 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
375 // NumberOfRelocations field, and the actual relocation count is stored in the
376 // VirtualAddress field in the first relocation entry.
377 if (Sec->hasExtendedRelocations()) {
378 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
379 base + Sec->PointerToRelocations);
380 return FirstReloc->VirtualAddress;
381 }
382 return Sec->NumberOfRelocations;
383}
384
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000385relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
386 const coff_section *Sec = toSec(Ref);
387 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000388 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000390 } else {
391 auto begin = reinterpret_cast<const coff_relocation*>(
392 base() + Sec->PointerToRelocations);
393 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
394 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
395 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000396 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000397}
398
Rui Ueyamac2bed422013-09-27 21:04:00 +0000399// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000400std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000401 if (COFFHeader)
402 if (std::error_code EC =
403 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
404 getNumberOfSymbols() * getSymbolTableEntrySize()))
405 return EC;
406
407 if (COFFBigObjHeader)
408 if (std::error_code EC =
409 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
410 getNumberOfSymbols() * getSymbolTableEntrySize()))
411 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000412
413 // Find string table. The first four byte of the string table contains the
414 // total size of the string table, including the size field itself. If the
415 // string table is empty, the value of the first four byte would be 4.
416 const uint8_t *StringTableAddr =
David Majnemer44f51e52014-09-10 12:51:52 +0000417 base() + getPointerToSymbolTable() +
418 getNumberOfSymbols() * getSymbolTableEntrySize();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000419 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000420 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000421 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000422 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000423 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000424 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000425 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000426
Nico Rieck773a5792014-02-26 19:51:44 +0000427 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
428 // tools like cvtres write a size of 0 for an empty table instead of 4.
429 if (StringTableSize < 4)
430 StringTableSize = 4;
431
Rui Ueyamac2bed422013-09-27 21:04:00 +0000432 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000433 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000434 return object_error::parse_failed;
435 return object_error::success;
436}
437
Rui Ueyama215a5862014-02-20 06:51:07 +0000438// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000439std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000440 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
441 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000442 uint64_t Rva = Addr - ImageBase;
443 assert(Rva <= UINT32_MAX);
444 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000445}
446
Rui Ueyamac2bed422013-09-27 21:04:00 +0000447// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000448std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000449 for (const SectionRef &S : sections()) {
450 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000451 uint32_t SectionStart = Section->VirtualAddress;
452 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000453 if (SectionStart <= Addr && Addr < SectionEnd) {
454 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000455 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
456 return object_error::success;
457 }
458 }
459 return object_error::parse_failed;
460}
461
462// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
463// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000464std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
465 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000467 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000468 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000469 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
470 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
471 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
472 return object_error::success;
473}
474
475// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000476std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000477 // First, we get the RVA of the import table. If the file lacks a pointer to
478 // the import table, do nothing.
479 const data_directory *DataEntry;
480 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
481 return object_error::success;
482
483 // Do nothing if the pointer to import table is NULL.
484 if (DataEntry->RelativeVirtualAddress == 0)
485 return object_error::success;
486
487 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000488 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000489 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000490 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000491
492 // Find the section that contains the RVA. This is needed because the RVA is
493 // the import table's memory address which is different from its file offset.
494 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000495 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000496 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000497 ImportDirectory = reinterpret_cast<
498 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000499 return object_error::success;
500}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000501
Rui Ueyama15d99352014-10-03 00:41:58 +0000502// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
503std::error_code COFFObjectFile::initDelayImportTablePtr() {
504 const data_directory *DataEntry;
505 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
506 return object_error::success;
507 if (DataEntry->RelativeVirtualAddress == 0)
508 return object_error::success;
509
510 uint32_t RVA = DataEntry->RelativeVirtualAddress;
511 NumberOfDelayImportDirectory = DataEntry->Size /
512 sizeof(delay_import_directory_table_entry) - 1;
513
514 uintptr_t IntPtr = 0;
515 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
516 return EC;
517 DelayImportDirectory = reinterpret_cast<
518 const delay_import_directory_table_entry *>(IntPtr);
519 return object_error::success;
520}
521
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000522// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000523std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000524 // First, we get the RVA of the export table. If the file lacks a pointer to
525 // the export table, do nothing.
526 const data_directory *DataEntry;
527 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
528 return object_error::success;
529
530 // Do nothing if the pointer to export table is NULL.
531 if (DataEntry->RelativeVirtualAddress == 0)
532 return object_error::success;
533
534 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
535 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000536 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000537 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000538 ExportDirectory =
539 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000540 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000541}
542
Rafael Espindola48af1c22014-08-19 18:44:46 +0000543COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
544 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000545 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
546 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
547 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
548 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000549 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000550 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000551 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000552 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000553 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000554
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000555 // The current location in the file where we are looking at.
556 uint64_t CurPtr = 0;
557
558 // PE header is optional and is present only in executables. If it exists,
559 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000560 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000561
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000562 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000563 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000564 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
565 // PE signature to find 'normal' COFF header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000566 if (!checkSize(Data, EC, 0x3c + 8))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000567 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000568 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
569 // Check the PE magic bytes. ("PE\0\0")
David Majnemer44f51e52014-09-10 12:51:52 +0000570 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
571 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000572 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000573 return;
574 }
David Majnemer44f51e52014-09-10 12:51:52 +0000575 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000576 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000577 }
578
Rafael Espindola48af1c22014-08-19 18:44:46 +0000579 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000580 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000581
582 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
583 // import libraries share a common prefix but bigobj is more restrictive.
584 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
585 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
586 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
587 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
588 return;
589
590 // Verify that we are dealing with bigobj.
591 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
592 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
593 sizeof(COFF::BigObjMagic)) == 0) {
594 COFFHeader = nullptr;
595 CurPtr += sizeof(coff_bigobj_file_header);
596 } else {
597 // It's not a bigobj.
598 COFFBigObjHeader = nullptr;
599 }
600 }
601 if (COFFHeader) {
602 // The prior checkSize call may have failed. This isn't a hard error
603 // because we were just trying to sniff out bigobj.
604 EC = object_error::success;
605 CurPtr += sizeof(coff_file_header);
606
607 if (COFFHeader->isImportLibrary())
608 return;
609 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000610
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000611 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000612 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000613 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000614 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000615
616 const uint8_t *DataDirAddr;
617 uint64_t DataDirSize;
618 if (Header->Magic == 0x10b) {
619 PE32Header = Header;
620 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
621 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
622 } else if (Header->Magic == 0x20b) {
623 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
624 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
625 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
626 } else {
627 // It's neither PE32 nor PE32+.
628 EC = object_error::parse_failed;
629 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000630 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000631 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000632 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000633 CurPtr += COFFHeader->SizeOfOptionalHeader;
634 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000635
Rafael Espindola48af1c22014-08-19 18:44:46 +0000636 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000637 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000638 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000639
Rui Ueyamac2bed422013-09-27 21:04:00 +0000640 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000641 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000642 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000643 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000644
Rui Ueyamac2bed422013-09-27 21:04:00 +0000645 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000646 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000647 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000648 if ((EC = initDelayImportTablePtr()))
649 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000650
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000651 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000652 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000653 return;
654
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000655 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000656}
657
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000658basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000659 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000660 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000661 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000662}
663
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000664basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000665 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000666 DataRefImpl Ret;
667 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000668 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000669}
670
Rui Ueyamabc654b12013-09-27 21:47:05 +0000671import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000672 return import_directory_iterator(
673 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000674}
675
Rui Ueyamabc654b12013-09-27 21:47:05 +0000676import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000677 return import_directory_iterator(
678 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000679}
David Meyerc429b802012-03-01 22:19:54 +0000680
Rui Ueyama15d99352014-10-03 00:41:58 +0000681delay_import_directory_iterator
682COFFObjectFile::delay_import_directory_begin() const {
683 return delay_import_directory_iterator(
684 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
685}
686
687delay_import_directory_iterator
688COFFObjectFile::delay_import_directory_end() const {
689 return delay_import_directory_iterator(
690 DelayImportDirectoryEntryRef(
691 DelayImportDirectory, NumberOfDelayImportDirectory, this));
692}
693
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000694export_directory_iterator COFFObjectFile::export_directory_begin() const {
695 return export_directory_iterator(
696 ExportDirectoryEntryRef(ExportDirectory, 0, this));
697}
698
699export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000700 if (!ExportDirectory)
701 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000702 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000703 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000704 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000705}
706
Rafael Espindolab5155a52014-02-10 20:24:04 +0000707section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000708 DataRefImpl Ret;
709 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
710 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000711}
712
Rafael Espindolab5155a52014-02-10 20:24:04 +0000713section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000714 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000715 int NumSections =
716 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000717 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
718 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000719}
720
721uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000722 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000723}
724
725StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000726 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000727 case COFF::IMAGE_FILE_MACHINE_I386:
728 return "COFF-i386";
729 case COFF::IMAGE_FILE_MACHINE_AMD64:
730 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000731 case COFF::IMAGE_FILE_MACHINE_ARMNT:
732 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000733 default:
734 return "COFF-<unknown arch>";
735 }
736}
737
738unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000739 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000740 case COFF::IMAGE_FILE_MACHINE_I386:
741 return Triple::x86;
742 case COFF::IMAGE_FILE_MACHINE_AMD64:
743 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000744 case COFF::IMAGE_FILE_MACHINE_ARMNT:
745 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000746 default:
747 return Triple::UnknownArch;
748 }
749}
750
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000751std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000752 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000753 return object_error::success;
754}
755
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000756std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000757COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
758 Res = PE32PlusHeader;
759 return object_error::success;
760}
761
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000762std::error_code
763COFFObjectFile::getDataDirectory(uint32_t Index,
764 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000765 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000766 if (!DataDirectory)
767 return object_error::parse_failed;
768 assert(PE32Header || PE32PlusHeader);
769 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
770 : PE32PlusHeader->NumberOfRvaAndSize;
771 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000772 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000773 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000774 return object_error::success;
775}
776
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000777std::error_code COFFObjectFile::getSection(int32_t Index,
778 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000779 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000780 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000781 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000782 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000783 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000784 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000785 else
786 return object_error::parse_failed;
787 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000788}
789
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000790std::error_code COFFObjectFile::getString(uint32_t Offset,
791 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000792 if (StringTableSize <= 4)
793 // Tried to get a string from an empty string table.
794 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000795 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000796 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000797 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000798 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000799}
800
David Majnemer44f51e52014-09-10 12:51:52 +0000801std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000802 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000803 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000804 if (Symbol.getStringTableOffset().Zeroes == 0) {
805 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000806 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000807 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000808 return object_error::success;
809 }
810
David Majnemer44f51e52014-09-10 12:51:52 +0000811 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000812 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000813 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000814 else
815 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000816 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000817 return object_error::success;
818}
819
David Majnemer44f51e52014-09-10 12:51:52 +0000820ArrayRef<uint8_t>
821COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000822 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000823
David Majnemer44f51e52014-09-10 12:51:52 +0000824 size_t SymbolSize = getSymbolTableEntrySize();
825 if (Symbol.getNumberOfAuxSymbols() > 0) {
826 // AUX data comes immediately after the symbol in COFF
827 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000828# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000829 // Verify that the Aux symbol points to a valid entry in the symbol table.
830 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000831 if (Offset < getPointerToSymbolTable() ||
832 Offset >=
833 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000834 report_fatal_error("Aux Symbol data was outside of symbol table.");
835
David Majnemer44f51e52014-09-10 12:51:52 +0000836 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
837 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000838# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000839 }
David Majnemer44f51e52014-09-10 12:51:52 +0000840 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000841}
842
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000843std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
844 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000845 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000846 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000847 // Null terminated, let ::strlen figure out the length.
848 Name = Sec->Name;
849 else
850 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000851 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000852
853 // Check for string table entry. First byte is '/'.
854 if (Name[0] == '/') {
855 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000856 if (Name[1] == '/') {
857 if (decodeBase64StringEntry(Name.substr(2), Offset))
858 return object_error::parse_failed;
859 } else {
860 if (Name.substr(1).getAsInteger(10, Offset))
861 return object_error::parse_failed;
862 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000863 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000864 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000865 }
866
867 Res = Name;
868 return object_error::success;
869}
870
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000871std::error_code
872COFFObjectFile::getSectionContents(const coff_section *Sec,
873 ArrayRef<uint8_t> &Res) const {
David Majnemerdac39852014-09-26 22:32:16 +0000874 // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
875 // do anything interesting for them.
876 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
877 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000878 // The only thing that we need to verify is that the contents is contained
879 // within the file bounds. We don't need to make sure it doesn't cover other
880 // data, as there's nothing that says that is not allowed.
881 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
882 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000883 if (ConEnd > uintptr_t(Data.getBufferEnd()))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000884 return object_error::parse_failed;
Craig Toppere1d12942014-08-27 05:25:25 +0000885 Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart),
886 Sec->SizeOfRawData);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000887 return object_error::success;
888}
889
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000890const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000891 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000892}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000893
Rafael Espindola5e812af2014-01-30 02:49:50 +0000894void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000895 Rel.p = reinterpret_cast<uintptr_t>(
896 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
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::getRelocationAddress(DataRefImpl Rel,
900 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000901 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000902}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000903
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000904std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
905 uint64_t &Res) const {
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000906 Res = toRel(Rel)->VirtualAddress;
907 return object_error::success;
908}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000909
Rafael Espindola806f0062013-06-05 01:33:53 +0000910symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000911 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000912 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +0000913 if (SymbolTable16)
914 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
915 else if (SymbolTable32)
916 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
917 else
918 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000919 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000920}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000921
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000922std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
923 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000924 const coff_relocation* R = toRel(Rel);
925 Res = R->Type;
926 return object_error::success;
927}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000928
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000929const coff_section *
930COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
931 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000932}
933
David Majnemer44f51e52014-09-10 12:51:52 +0000934COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
935 if (SymbolTable16)
936 return toSymb<coff_symbol16>(Ref);
937 if (SymbolTable32)
938 return toSymb<coff_symbol32>(Ref);
939 llvm_unreachable("no symbol table pointer!");
940}
941
942COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
943 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000944}
945
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000946const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000947COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
948 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +0000949}
950
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000951#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
952 case COFF::reloc_type: \
953 Res = #reloc_type; \
954 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000955
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000956std::error_code
957COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
958 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000959 const coff_relocation *Reloc = toRel(Rel);
960 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +0000961 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000962 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000963 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000964 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
965 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
966 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
967 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
968 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
969 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
970 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
971 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
972 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
973 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
974 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
975 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
976 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
977 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
978 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
979 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
980 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
981 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000982 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000983 }
984 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +0000985 case COFF::IMAGE_FILE_MACHINE_ARMNT:
986 switch (Reloc->Type) {
987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
988 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
989 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
990 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
991 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
992 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
993 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
996 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
997 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
998 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
999 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1000 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1001 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1002 default:
1003 Res = "Unknown";
1004 }
1005 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001006 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001007 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001008 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1009 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1010 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1011 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1012 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1013 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1014 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1015 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1016 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1017 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1018 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1019 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001020 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001021 }
1022 break;
1023 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001024 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001025 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001026 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001027 return object_error::success;
1028}
1029
1030#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1031
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001032std::error_code
1033COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1034 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001035 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001036 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001037 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1038 if (std::error_code EC = Symb.getError())
1039 return EC;
1040 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001041 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001042 if (std::error_code EC = getSymbolName(Sym, SymName))
1043 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001044 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001045 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001046}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001047
Rafael Espindolac66d7612014-08-17 19:09:37 +00001048bool COFFObjectFile::isRelocatableObject() const {
1049 return !DataDirectory;
1050}
1051
Rui Ueyamac2bed422013-09-27 21:04:00 +00001052bool ImportDirectoryEntryRef::
1053operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001054 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001055}
1056
Rafael Espindola5e812af2014-01-30 02:49:50 +00001057void ImportDirectoryEntryRef::moveNext() {
1058 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001059}
1060
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001061std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1062 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001063 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001064 return object_error::success;
1065}
1066
Rui Ueyama861021f2014-10-02 22:05:29 +00001067static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001068makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001069 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001070 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001071 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001072 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001073 }
1074 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001075 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001076}
1077
Rui Ueyama15d99352014-10-03 00:41:58 +00001078static imported_symbol_iterator
1079importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001080 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001081 Object->getRvaPtr(RVA, IntPtr);
1082 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001083}
1084
Rui Ueyama15d99352014-10-03 00:41:58 +00001085static imported_symbol_iterator
1086importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001087 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001088 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001089 // Forward the pointer to the last entry which is null.
1090 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001091 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001092 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1093 while (*Entry++)
1094 ++Index;
1095 } else {
1096 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1097 while (*Entry++)
1098 ++Index;
1099 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001100 return makeImportedSymbolIterator(Object, IntPtr, Index);
1101}
1102
1103imported_symbol_iterator
1104ImportDirectoryEntryRef::imported_symbol_begin() const {
1105 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1106 OwningObject);
1107}
1108
1109imported_symbol_iterator
1110ImportDirectoryEntryRef::imported_symbol_end() const {
1111 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1112 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001113}
1114
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001115std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001116 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001117 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001118 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001119 return EC;
1120 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001121 return object_error::success;
1122}
1123
Rui Ueyama1e152d52014-10-02 17:02:18 +00001124std::error_code
1125ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1126 Result = ImportTable[Index].ImportLookupTableRVA;
1127 return object_error::success;
1128}
1129
1130std::error_code
1131ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1132 Result = ImportTable[Index].ImportAddressTableRVA;
1133 return object_error::success;
1134}
1135
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001136std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001137 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001138 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001139 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1140 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001141 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001142 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1143 return object_error::success;
1144}
1145
Rui Ueyama15d99352014-10-03 00:41:58 +00001146bool DelayImportDirectoryEntryRef::
1147operator==(const DelayImportDirectoryEntryRef &Other) const {
1148 return Table == Other.Table && Index == Other.Index;
1149}
1150
1151void DelayImportDirectoryEntryRef::moveNext() {
1152 ++Index;
1153}
1154
1155imported_symbol_iterator
1156DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1157 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1158 OwningObject);
1159}
1160
1161imported_symbol_iterator
1162DelayImportDirectoryEntryRef::imported_symbol_end() const {
1163 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1164 OwningObject);
1165}
1166
1167std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1168 uintptr_t IntPtr = 0;
1169 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1170 return EC;
1171 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1172 return object_error::success;
1173}
1174
Rui Ueyama1af08652014-10-03 18:07:18 +00001175std::error_code DelayImportDirectoryEntryRef::
1176getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1177 Result = Table;
1178 return object_error::success;
1179}
1180
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001181bool ExportDirectoryEntryRef::
1182operator==(const ExportDirectoryEntryRef &Other) const {
1183 return ExportTable == Other.ExportTable && Index == Other.Index;
1184}
1185
Rafael Espindola5e812af2014-01-30 02:49:50 +00001186void ExportDirectoryEntryRef::moveNext() {
1187 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001188}
1189
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001190// Returns the name of the current export symbol. If the symbol is exported only
1191// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001192std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001193 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001194 if (std::error_code EC =
1195 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001196 return EC;
1197 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1198 return object_error::success;
1199}
1200
Rui Ueyamae5df6092014-01-17 22:02:24 +00001201// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001202std::error_code
1203ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001204 Result = ExportTable->OrdinalBase;
1205 return object_error::success;
1206}
1207
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001208// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001209std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001210 Result = ExportTable->OrdinalBase + Index;
1211 return object_error::success;
1212}
1213
1214// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001215std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001216 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001217 if (std::error_code EC =
1218 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001219 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001220 const export_address_table_entry *entry =
1221 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001222 Result = entry[Index].ExportRVA;
1223 return object_error::success;
1224}
1225
1226// Returns the name of the current export symbol. If the symbol is exported only
1227// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001228std::error_code
1229ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001230 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001231 if (std::error_code EC =
1232 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001233 return EC;
1234 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1235
1236 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1237 int Offset = 0;
1238 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1239 I < E; ++I, ++Offset) {
1240 if (*I != Index)
1241 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001242 if (std::error_code EC =
1243 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001244 return EC;
1245 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001246 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001247 return EC;
1248 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1249 return object_error::success;
1250 }
1251 Result = "";
1252 return object_error::success;
1253}
1254
Rui Ueyama861021f2014-10-02 22:05:29 +00001255bool ImportedSymbolRef::
1256operator==(const ImportedSymbolRef &Other) const {
1257 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1258 && Index == Other.Index;
1259}
1260
1261void ImportedSymbolRef::moveNext() {
1262 ++Index;
1263}
1264
1265std::error_code
1266ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1267 uint32_t RVA;
1268 if (Entry32) {
1269 // If a symbol is imported only by ordinal, it has no name.
1270 if (Entry32[Index].isOrdinal())
1271 return object_error::success;
1272 RVA = Entry32[Index].getHintNameRVA();
1273 } else {
1274 if (Entry64[Index].isOrdinal())
1275 return object_error::success;
1276 RVA = Entry64[Index].getHintNameRVA();
1277 }
1278 uintptr_t IntPtr = 0;
1279 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1280 return EC;
1281 // +2 because the first two bytes is hint.
1282 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1283 return object_error::success;
1284}
1285
1286std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1287 uint32_t RVA;
1288 if (Entry32) {
1289 if (Entry32[Index].isOrdinal()) {
1290 Result = Entry32[Index].getOrdinal();
1291 return object_error::success;
1292 }
1293 RVA = Entry32[Index].getHintNameRVA();
1294 } else {
1295 if (Entry64[Index].isOrdinal()) {
1296 Result = Entry64[Index].getOrdinal();
1297 return object_error::success;
1298 }
1299 RVA = Entry64[Index].getHintNameRVA();
1300 }
1301 uintptr_t IntPtr = 0;
1302 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1303 return EC;
1304 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1305 return object_error::success;
1306}
1307
Rafael Espindola437b0d52014-07-31 03:12:45 +00001308ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001309ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001310 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001311 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001312 if (EC)
1313 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001314 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001315}