blob: 3e668552d9c65d6680bd8f9af539c0561a550ef7 [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 Espindola80291272014-10-08 15:28:58 +0000263uint64_t COFFObjectFile::getSectionAddress(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000264 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000265 return Sec->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000266}
267
Rafael Espindola80291272014-10-08 15:28:58 +0000268uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000269 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000270 return Sec->SizeOfRawData;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000271}
272
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000273std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
274 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000275 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000276 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000277 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000278 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
279 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000280}
281
Rafael Espindola80291272014-10-08 15:28:58 +0000282uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000283 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000284 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000285}
286
Rafael Espindola80291272014-10-08 15:28:58 +0000287bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000288 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000289 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000290}
291
Rafael Espindola80291272014-10-08 15:28:58 +0000292bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000293 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000294 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000295}
296
Rafael Espindola80291272014-10-08 15:28:58 +0000297bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000298 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000299 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000300}
301
Rafael Espindola80291272014-10-08 15:28:58 +0000302bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
Preston Gurd2138ef62012-04-12 20:13:57 +0000303 // FIXME: Unimplemented
Rafael Espindola80291272014-10-08 15:28:58 +0000304 return true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000305}
306
Rafael Espindola80291272014-10-08 15:28:58 +0000307bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000308 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000309 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000310}
311
Rafael Espindola80291272014-10-08 15:28:58 +0000312bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000313 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000314 return false;
Preston Gurd2138ef62012-04-12 20:13:57 +0000315}
316
Rafael Espindola80291272014-10-08 15:28:58 +0000317bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000318 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000319 return false;
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000320}
321
Rafael Espindola80291272014-10-08 15:28:58 +0000322bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
323 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000324 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000325 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000326 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000327 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000328}
329
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000330relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
331 const coff_section *Sec = toSec(Ref);
332 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000333 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000334 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000335 } else {
336 auto begin = reinterpret_cast<const coff_relocation*>(
337 base() + Sec->PointerToRelocations);
338 if (Sec->hasExtendedRelocations()) {
339 // Skip the first relocation entry repurposed to store the number of
340 // relocations.
341 begin++;
342 }
343 Ret.p = reinterpret_cast<uintptr_t>(begin);
344 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000345 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000346}
347
Rui Ueyama827c8a22014-03-21 00:44:19 +0000348static uint32_t getNumberOfRelocations(const coff_section *Sec,
349 const uint8_t *base) {
350 // The field for the number of relocations in COFF section table is only
351 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
352 // NumberOfRelocations field, and the actual relocation count is stored in the
353 // VirtualAddress field in the first relocation entry.
354 if (Sec->hasExtendedRelocations()) {
355 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
356 base + Sec->PointerToRelocations);
357 return FirstReloc->VirtualAddress;
358 }
359 return Sec->NumberOfRelocations;
360}
361
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000362relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
363 const coff_section *Sec = toSec(Ref);
364 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000365 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000366 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000367 } else {
368 auto begin = reinterpret_cast<const coff_relocation*>(
369 base() + Sec->PointerToRelocations);
370 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
371 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
372 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000373 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000374}
375
Rui Ueyamac2bed422013-09-27 21:04:00 +0000376// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000377std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000378 if (COFFHeader)
379 if (std::error_code EC =
380 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
381 getNumberOfSymbols() * getSymbolTableEntrySize()))
382 return EC;
383
384 if (COFFBigObjHeader)
385 if (std::error_code EC =
386 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
387 getNumberOfSymbols() * getSymbolTableEntrySize()))
388 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000389
390 // Find string table. The first four byte of the string table contains the
391 // total size of the string table, including the size field itself. If the
392 // string table is empty, the value of the first four byte would be 4.
393 const uint8_t *StringTableAddr =
David Majnemer44f51e52014-09-10 12:51:52 +0000394 base() + getPointerToSymbolTable() +
395 getNumberOfSymbols() * getSymbolTableEntrySize();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000396 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000397 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000398 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000399 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000400 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000401 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000402 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000403
Nico Rieck773a5792014-02-26 19:51:44 +0000404 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
405 // tools like cvtres write a size of 0 for an empty table instead of 4.
406 if (StringTableSize < 4)
407 StringTableSize = 4;
408
Rui Ueyamac2bed422013-09-27 21:04:00 +0000409 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000410 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000411 return object_error::parse_failed;
412 return object_error::success;
413}
414
Rui Ueyama215a5862014-02-20 06:51:07 +0000415// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000416std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000417 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
418 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000419 uint64_t Rva = Addr - ImageBase;
420 assert(Rva <= UINT32_MAX);
421 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000422}
423
Rui Ueyamac2bed422013-09-27 21:04:00 +0000424// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000425std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000426 for (const SectionRef &S : sections()) {
427 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000428 uint32_t SectionStart = Section->VirtualAddress;
429 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000430 if (SectionStart <= Addr && Addr < SectionEnd) {
431 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000432 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
433 return object_error::success;
434 }
435 }
436 return object_error::parse_failed;
437}
438
439// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
440// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000441std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
442 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000443 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000444 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000445 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000446 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
447 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
448 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
449 return object_error::success;
450}
451
452// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000453std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000454 // First, we get the RVA of the import table. If the file lacks a pointer to
455 // the import table, do nothing.
456 const data_directory *DataEntry;
457 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
458 return object_error::success;
459
460 // Do nothing if the pointer to import table is NULL.
461 if (DataEntry->RelativeVirtualAddress == 0)
462 return object_error::success;
463
464 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000465 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000466 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000467 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000468
469 // Find the section that contains the RVA. This is needed because the RVA is
470 // the import table's memory address which is different from its file offset.
471 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000472 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000473 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000474 ImportDirectory = reinterpret_cast<
475 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000476 return object_error::success;
477}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000478
Rui Ueyama15d99352014-10-03 00:41:58 +0000479// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
480std::error_code COFFObjectFile::initDelayImportTablePtr() {
481 const data_directory *DataEntry;
482 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
483 return object_error::success;
484 if (DataEntry->RelativeVirtualAddress == 0)
485 return object_error::success;
486
487 uint32_t RVA = DataEntry->RelativeVirtualAddress;
488 NumberOfDelayImportDirectory = DataEntry->Size /
489 sizeof(delay_import_directory_table_entry) - 1;
490
491 uintptr_t IntPtr = 0;
492 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
493 return EC;
494 DelayImportDirectory = reinterpret_cast<
495 const delay_import_directory_table_entry *>(IntPtr);
496 return object_error::success;
497}
498
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000499// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000500std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000501 // First, we get the RVA of the export table. If the file lacks a pointer to
502 // the export table, do nothing.
503 const data_directory *DataEntry;
504 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
505 return object_error::success;
506
507 // Do nothing if the pointer to export table is NULL.
508 if (DataEntry->RelativeVirtualAddress == 0)
509 return object_error::success;
510
511 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
512 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000513 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000514 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000515 ExportDirectory =
516 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000517 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000518}
519
Rafael Espindola48af1c22014-08-19 18:44:46 +0000520COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
521 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000522 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
523 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
524 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
525 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000526 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000527 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000528 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000529 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000530 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000531
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000532 // The current location in the file where we are looking at.
533 uint64_t CurPtr = 0;
534
535 // PE header is optional and is present only in executables. If it exists,
536 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000537 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000538
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000539 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000540 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000541 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
542 // PE signature to find 'normal' COFF header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000543 if (!checkSize(Data, EC, 0x3c + 8))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000544 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000545 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
546 // Check the PE magic bytes. ("PE\0\0")
David Majnemer44f51e52014-09-10 12:51:52 +0000547 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
548 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000549 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000550 return;
551 }
David Majnemer44f51e52014-09-10 12:51:52 +0000552 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000553 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000554 }
555
Rafael Espindola48af1c22014-08-19 18:44:46 +0000556 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000557 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000558
559 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
560 // import libraries share a common prefix but bigobj is more restrictive.
561 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
562 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
563 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
564 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
565 return;
566
567 // Verify that we are dealing with bigobj.
568 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
569 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
570 sizeof(COFF::BigObjMagic)) == 0) {
571 COFFHeader = nullptr;
572 CurPtr += sizeof(coff_bigobj_file_header);
573 } else {
574 // It's not a bigobj.
575 COFFBigObjHeader = nullptr;
576 }
577 }
578 if (COFFHeader) {
579 // The prior checkSize call may have failed. This isn't a hard error
580 // because we were just trying to sniff out bigobj.
581 EC = object_error::success;
582 CurPtr += sizeof(coff_file_header);
583
584 if (COFFHeader->isImportLibrary())
585 return;
586 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000587
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000588 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000589 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000590 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000591 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000592
593 const uint8_t *DataDirAddr;
594 uint64_t DataDirSize;
595 if (Header->Magic == 0x10b) {
596 PE32Header = Header;
597 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
598 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
599 } else if (Header->Magic == 0x20b) {
600 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
601 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
602 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
603 } else {
604 // It's neither PE32 nor PE32+.
605 EC = object_error::parse_failed;
606 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000607 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000608 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000609 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000610 CurPtr += COFFHeader->SizeOfOptionalHeader;
611 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000612
Rafael Espindola48af1c22014-08-19 18:44:46 +0000613 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000614 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000615 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000616
Rui Ueyamac2bed422013-09-27 21:04:00 +0000617 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000618 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000619 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000620 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000621
Rui Ueyamac2bed422013-09-27 21:04:00 +0000622 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000623 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000624 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000625 if ((EC = initDelayImportTablePtr()))
626 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000627
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000628 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000629 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000630 return;
631
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000632 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000633}
634
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000635basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000636 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000637 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000638 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000639}
640
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000641basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000642 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000643 DataRefImpl Ret;
644 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000645 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000646}
647
Rui Ueyamabc654b12013-09-27 21:47:05 +0000648import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000649 return import_directory_iterator(
650 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000651}
652
Rui Ueyamabc654b12013-09-27 21:47:05 +0000653import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000654 return import_directory_iterator(
655 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000656}
David Meyerc429b802012-03-01 22:19:54 +0000657
Rui Ueyama15d99352014-10-03 00:41:58 +0000658delay_import_directory_iterator
659COFFObjectFile::delay_import_directory_begin() const {
660 return delay_import_directory_iterator(
661 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
662}
663
664delay_import_directory_iterator
665COFFObjectFile::delay_import_directory_end() const {
666 return delay_import_directory_iterator(
667 DelayImportDirectoryEntryRef(
668 DelayImportDirectory, NumberOfDelayImportDirectory, this));
669}
670
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000671export_directory_iterator COFFObjectFile::export_directory_begin() const {
672 return export_directory_iterator(
673 ExportDirectoryEntryRef(ExportDirectory, 0, this));
674}
675
676export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000677 if (!ExportDirectory)
678 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000679 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000680 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000681 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000682}
683
Rafael Espindolab5155a52014-02-10 20:24:04 +0000684section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000685 DataRefImpl Ret;
686 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
687 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000688}
689
Rafael Espindolab5155a52014-02-10 20:24:04 +0000690section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000691 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000692 int NumSections =
693 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000694 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
695 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000696}
697
698uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000699 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000700}
701
702StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000703 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000704 case COFF::IMAGE_FILE_MACHINE_I386:
705 return "COFF-i386";
706 case COFF::IMAGE_FILE_MACHINE_AMD64:
707 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000708 case COFF::IMAGE_FILE_MACHINE_ARMNT:
709 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000710 default:
711 return "COFF-<unknown arch>";
712 }
713}
714
715unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000716 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000717 case COFF::IMAGE_FILE_MACHINE_I386:
718 return Triple::x86;
719 case COFF::IMAGE_FILE_MACHINE_AMD64:
720 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000721 case COFF::IMAGE_FILE_MACHINE_ARMNT:
722 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000723 default:
724 return Triple::UnknownArch;
725 }
726}
727
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000728std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000729 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000730 return object_error::success;
731}
732
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000733std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000734COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
735 Res = PE32PlusHeader;
736 return object_error::success;
737}
738
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000739std::error_code
740COFFObjectFile::getDataDirectory(uint32_t Index,
741 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000742 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000743 if (!DataDirectory)
744 return object_error::parse_failed;
745 assert(PE32Header || PE32PlusHeader);
746 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
747 : PE32PlusHeader->NumberOfRvaAndSize;
748 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000749 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000750 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000751 return object_error::success;
752}
753
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000754std::error_code COFFObjectFile::getSection(int32_t Index,
755 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000756 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000757 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000758 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000759 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000760 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000761 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000762 else
763 return object_error::parse_failed;
764 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000765}
766
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000767std::error_code COFFObjectFile::getString(uint32_t Offset,
768 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000769 if (StringTableSize <= 4)
770 // Tried to get a string from an empty string table.
771 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000772 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000773 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000774 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000775 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000776}
777
David Majnemer44f51e52014-09-10 12:51:52 +0000778std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000779 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000780 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000781 if (Symbol.getStringTableOffset().Zeroes == 0) {
782 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000783 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000784 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000785 return object_error::success;
786 }
787
David Majnemer44f51e52014-09-10 12:51:52 +0000788 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000789 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000790 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000791 else
792 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000793 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000794 return object_error::success;
795}
796
David Majnemer44f51e52014-09-10 12:51:52 +0000797ArrayRef<uint8_t>
798COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000799 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000800
David Majnemer44f51e52014-09-10 12:51:52 +0000801 size_t SymbolSize = getSymbolTableEntrySize();
802 if (Symbol.getNumberOfAuxSymbols() > 0) {
803 // AUX data comes immediately after the symbol in COFF
804 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000805# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000806 // Verify that the Aux symbol points to a valid entry in the symbol table.
807 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000808 if (Offset < getPointerToSymbolTable() ||
809 Offset >=
810 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000811 report_fatal_error("Aux Symbol data was outside of symbol table.");
812
David Majnemer44f51e52014-09-10 12:51:52 +0000813 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
814 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000815# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000816 }
David Majnemer44f51e52014-09-10 12:51:52 +0000817 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000818}
819
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000820std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
821 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000822 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000823 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000824 // Null terminated, let ::strlen figure out the length.
825 Name = Sec->Name;
826 else
827 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000828 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000829
830 // Check for string table entry. First byte is '/'.
831 if (Name[0] == '/') {
832 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000833 if (Name[1] == '/') {
834 if (decodeBase64StringEntry(Name.substr(2), Offset))
835 return object_error::parse_failed;
836 } else {
837 if (Name.substr(1).getAsInteger(10, Offset))
838 return object_error::parse_failed;
839 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000840 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000841 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000842 }
843
844 Res = Name;
845 return object_error::success;
846}
847
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000848std::error_code
849COFFObjectFile::getSectionContents(const coff_section *Sec,
850 ArrayRef<uint8_t> &Res) const {
David Majnemerdac39852014-09-26 22:32:16 +0000851 // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
852 // do anything interesting for them.
853 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
854 "BSS sections don't have contents!");
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 Ueyama1e152d52014-10-02 17:02:18 +00001040 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001041 return object_error::success;
1042}
1043
Rui Ueyama861021f2014-10-02 22:05:29 +00001044static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001045makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001046 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001047 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001048 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001049 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001050 }
1051 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001052 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001053}
1054
Rui Ueyama15d99352014-10-03 00:41:58 +00001055static imported_symbol_iterator
1056importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001057 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001058 Object->getRvaPtr(RVA, IntPtr);
1059 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001060}
1061
Rui Ueyama15d99352014-10-03 00:41:58 +00001062static imported_symbol_iterator
1063importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001064 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001065 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001066 // Forward the pointer to the last entry which is null.
1067 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001068 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001069 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1070 while (*Entry++)
1071 ++Index;
1072 } else {
1073 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1074 while (*Entry++)
1075 ++Index;
1076 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001077 return makeImportedSymbolIterator(Object, IntPtr, Index);
1078}
1079
1080imported_symbol_iterator
1081ImportDirectoryEntryRef::imported_symbol_begin() const {
1082 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1083 OwningObject);
1084}
1085
1086imported_symbol_iterator
1087ImportDirectoryEntryRef::imported_symbol_end() const {
1088 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1089 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001090}
1091
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001092std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001093 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001094 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001095 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001096 return EC;
1097 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001098 return object_error::success;
1099}
1100
Rui Ueyama1e152d52014-10-02 17:02:18 +00001101std::error_code
1102ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1103 Result = ImportTable[Index].ImportLookupTableRVA;
1104 return object_error::success;
1105}
1106
1107std::error_code
1108ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1109 Result = ImportTable[Index].ImportAddressTableRVA;
1110 return object_error::success;
1111}
1112
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001113std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001114 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001115 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001116 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1117 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001118 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001119 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1120 return object_error::success;
1121}
1122
Rui Ueyama15d99352014-10-03 00:41:58 +00001123bool DelayImportDirectoryEntryRef::
1124operator==(const DelayImportDirectoryEntryRef &Other) const {
1125 return Table == Other.Table && Index == Other.Index;
1126}
1127
1128void DelayImportDirectoryEntryRef::moveNext() {
1129 ++Index;
1130}
1131
1132imported_symbol_iterator
1133DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1134 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1135 OwningObject);
1136}
1137
1138imported_symbol_iterator
1139DelayImportDirectoryEntryRef::imported_symbol_end() const {
1140 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1141 OwningObject);
1142}
1143
1144std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1145 uintptr_t IntPtr = 0;
1146 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1147 return EC;
1148 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1149 return object_error::success;
1150}
1151
Rui Ueyama1af08652014-10-03 18:07:18 +00001152std::error_code DelayImportDirectoryEntryRef::
1153getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1154 Result = Table;
1155 return object_error::success;
1156}
1157
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001158bool ExportDirectoryEntryRef::
1159operator==(const ExportDirectoryEntryRef &Other) const {
1160 return ExportTable == Other.ExportTable && Index == Other.Index;
1161}
1162
Rafael Espindola5e812af2014-01-30 02:49:50 +00001163void ExportDirectoryEntryRef::moveNext() {
1164 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001165}
1166
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001167// Returns the name of the current export symbol. If the symbol is exported only
1168// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001169std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001170 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001171 if (std::error_code EC =
1172 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001173 return EC;
1174 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1175 return object_error::success;
1176}
1177
Rui Ueyamae5df6092014-01-17 22:02:24 +00001178// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001179std::error_code
1180ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001181 Result = ExportTable->OrdinalBase;
1182 return object_error::success;
1183}
1184
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001185// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001186std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001187 Result = ExportTable->OrdinalBase + Index;
1188 return object_error::success;
1189}
1190
1191// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001192std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001193 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001194 if (std::error_code EC =
1195 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001196 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001197 const export_address_table_entry *entry =
1198 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001199 Result = entry[Index].ExportRVA;
1200 return object_error::success;
1201}
1202
1203// Returns the name of the current export symbol. If the symbol is exported only
1204// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001205std::error_code
1206ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001207 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001208 if (std::error_code EC =
1209 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001210 return EC;
1211 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1212
1213 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1214 int Offset = 0;
1215 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1216 I < E; ++I, ++Offset) {
1217 if (*I != Index)
1218 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001219 if (std::error_code EC =
1220 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001221 return EC;
1222 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001223 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001224 return EC;
1225 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1226 return object_error::success;
1227 }
1228 Result = "";
1229 return object_error::success;
1230}
1231
Rui Ueyama861021f2014-10-02 22:05:29 +00001232bool ImportedSymbolRef::
1233operator==(const ImportedSymbolRef &Other) const {
1234 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1235 && Index == Other.Index;
1236}
1237
1238void ImportedSymbolRef::moveNext() {
1239 ++Index;
1240}
1241
1242std::error_code
1243ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1244 uint32_t RVA;
1245 if (Entry32) {
1246 // If a symbol is imported only by ordinal, it has no name.
1247 if (Entry32[Index].isOrdinal())
1248 return object_error::success;
1249 RVA = Entry32[Index].getHintNameRVA();
1250 } else {
1251 if (Entry64[Index].isOrdinal())
1252 return object_error::success;
1253 RVA = Entry64[Index].getHintNameRVA();
1254 }
1255 uintptr_t IntPtr = 0;
1256 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1257 return EC;
1258 // +2 because the first two bytes is hint.
1259 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1260 return object_error::success;
1261}
1262
1263std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1264 uint32_t RVA;
1265 if (Entry32) {
1266 if (Entry32[Index].isOrdinal()) {
1267 Result = Entry32[Index].getOrdinal();
1268 return object_error::success;
1269 }
1270 RVA = Entry32[Index].getHintNameRVA();
1271 } else {
1272 if (Entry64[Index].isOrdinal()) {
1273 Result = Entry64[Index].getOrdinal();
1274 return object_error::success;
1275 }
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 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1282 return object_error::success;
1283}
1284
Rafael Espindola437b0d52014-07-31 03:12:45 +00001285ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001286ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001287 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001288 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001289 if (EC)
1290 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001291 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001292}