blob: 72a6db4aab9a57979e92cd994027d258319c1b04 [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
Rafael Espindola8280fbb2014-10-08 17:37:19 +0000225 if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) {
226 if (Symb.getValue() == 0)
227 Result = UnknownAddressOrSize;
228 else
229 Result = Symb.getValue();
230 } else if (Section) {
David Majnemer44f51e52014-09-10 12:51:52 +0000231 Result = Section->SizeOfRawData - Symb.getValue();
Rafael Espindola8280fbb2014-10-08 17:37:19 +0000232 } else {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000233 Result = 0;
Rafael Espindola8280fbb2014-10-08 17:37:19 +0000234 }
235
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000236 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000237}
238
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000239std::error_code
240COFFObjectFile::getSymbolSection(DataRefImpl Ref,
241 section_iterator &Result) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000242 COFFSymbolRef Symb = getCOFFSymbol(Ref);
243 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000244 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000245 } else {
Craig Topper2617dcc2014-04-15 06:32:26 +0000246 const coff_section *Sec = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000247 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000248 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000249 DataRefImpl Ref;
250 Ref.p = reinterpret_cast<uintptr_t>(Sec);
251 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000252 }
253 return object_error::success;
254}
255
Rafael Espindola5e812af2014-01-30 02:49:50 +0000256void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000257 const coff_section *Sec = toSec(Ref);
258 Sec += 1;
259 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000260}
261
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000262std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
263 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000264 const coff_section *Sec = toSec(Ref);
265 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000266}
267
Rafael Espindola80291272014-10-08 15:28:58 +0000268uint64_t COFFObjectFile::getSectionAddress(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->VirtualAddress;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000271}
272
Rafael Espindola80291272014-10-08 15:28:58 +0000273uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000274 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000275 return Sec->SizeOfRawData;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000276}
277
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000278std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
279 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000280 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000281 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000282 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000283 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
284 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000285}
286
Rafael Espindola80291272014-10-08 15:28:58 +0000287uint64_t COFFObjectFile::getSectionAlignment(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 uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000290}
291
Rafael Espindola80291272014-10-08 15:28:58 +0000292bool COFFObjectFile::isSectionText(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_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000295}
296
Rafael Espindola80291272014-10-08 15:28:58 +0000297bool COFFObjectFile::isSectionData(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_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000300}
301
Rafael Espindola80291272014-10-08 15:28:58 +0000302bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000303 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000304 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000305}
306
Rafael Espindola80291272014-10-08 15:28:58 +0000307bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
Preston Gurd2138ef62012-04-12 20:13:57 +0000308 // FIXME: Unimplemented
Rafael Espindola80291272014-10-08 15:28:58 +0000309 return true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000310}
311
Rafael Espindola80291272014-10-08 15:28:58 +0000312bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000313 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000314 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000315}
316
Rafael Espindola80291272014-10-08 15:28:58 +0000317bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000318 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000319 return false;
Preston Gurd2138ef62012-04-12 20:13:57 +0000320}
321
Rafael Espindola80291272014-10-08 15:28:58 +0000322bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000323 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000324 return false;
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000325}
326
Rafael Espindola80291272014-10-08 15:28:58 +0000327bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
328 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000329 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000330 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000331 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000332 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000333}
334
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000335relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
336 const coff_section *Sec = toSec(Ref);
337 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000338 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000339 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000340 } else {
341 auto begin = reinterpret_cast<const coff_relocation*>(
342 base() + Sec->PointerToRelocations);
343 if (Sec->hasExtendedRelocations()) {
344 // Skip the first relocation entry repurposed to store the number of
345 // relocations.
346 begin++;
347 }
348 Ret.p = reinterpret_cast<uintptr_t>(begin);
349 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000350 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000351}
352
Rui Ueyama827c8a22014-03-21 00:44:19 +0000353static uint32_t getNumberOfRelocations(const coff_section *Sec,
354 const uint8_t *base) {
355 // The field for the number of relocations in COFF section table is only
356 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
357 // NumberOfRelocations field, and the actual relocation count is stored in the
358 // VirtualAddress field in the first relocation entry.
359 if (Sec->hasExtendedRelocations()) {
360 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
361 base + Sec->PointerToRelocations);
362 return FirstReloc->VirtualAddress;
363 }
364 return Sec->NumberOfRelocations;
365}
366
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000367relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
368 const coff_section *Sec = toSec(Ref);
369 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000370 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000371 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000372 } else {
373 auto begin = reinterpret_cast<const coff_relocation*>(
374 base() + Sec->PointerToRelocations);
375 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
376 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
377 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000378 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000379}
380
Rui Ueyamac2bed422013-09-27 21:04:00 +0000381// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000382std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000383 if (COFFHeader)
384 if (std::error_code EC =
385 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
386 getNumberOfSymbols() * getSymbolTableEntrySize()))
387 return EC;
388
389 if (COFFBigObjHeader)
390 if (std::error_code EC =
391 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
392 getNumberOfSymbols() * getSymbolTableEntrySize()))
393 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000394
395 // Find string table. The first four byte of the string table contains the
396 // total size of the string table, including the size field itself. If the
397 // string table is empty, the value of the first four byte would be 4.
398 const uint8_t *StringTableAddr =
David Majnemer44f51e52014-09-10 12:51:52 +0000399 base() + getPointerToSymbolTable() +
400 getNumberOfSymbols() * getSymbolTableEntrySize();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000401 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000402 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000403 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000404 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000405 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000406 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000407 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408
Nico Rieck773a5792014-02-26 19:51:44 +0000409 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
410 // tools like cvtres write a size of 0 for an empty table instead of 4.
411 if (StringTableSize < 4)
412 StringTableSize = 4;
413
Rui Ueyamac2bed422013-09-27 21:04:00 +0000414 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000415 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000416 return object_error::parse_failed;
417 return object_error::success;
418}
419
Rui Ueyama215a5862014-02-20 06:51:07 +0000420// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000421std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000422 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
423 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000424 uint64_t Rva = Addr - ImageBase;
425 assert(Rva <= UINT32_MAX);
426 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000427}
428
Rui Ueyamac2bed422013-09-27 21:04:00 +0000429// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000430std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000431 for (const SectionRef &S : sections()) {
432 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000433 uint32_t SectionStart = Section->VirtualAddress;
434 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000435 if (SectionStart <= Addr && Addr < SectionEnd) {
436 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
438 return object_error::success;
439 }
440 }
441 return object_error::parse_failed;
442}
443
444// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
445// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000446std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
447 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000448 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000449 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000450 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000451 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
452 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
453 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
454 return object_error::success;
455}
456
457// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000458std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000459 // First, we get the RVA of the import table. If the file lacks a pointer to
460 // the import table, do nothing.
461 const data_directory *DataEntry;
462 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
463 return object_error::success;
464
465 // Do nothing if the pointer to import table is NULL.
466 if (DataEntry->RelativeVirtualAddress == 0)
467 return object_error::success;
468
469 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000470 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000471 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000472 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000473
474 // Find the section that contains the RVA. This is needed because the RVA is
475 // the import table's memory address which is different from its file offset.
476 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000477 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000478 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000479 ImportDirectory = reinterpret_cast<
480 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000481 return object_error::success;
482}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000483
Rui Ueyama15d99352014-10-03 00:41:58 +0000484// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
485std::error_code COFFObjectFile::initDelayImportTablePtr() {
486 const data_directory *DataEntry;
487 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
488 return object_error::success;
489 if (DataEntry->RelativeVirtualAddress == 0)
490 return object_error::success;
491
492 uint32_t RVA = DataEntry->RelativeVirtualAddress;
493 NumberOfDelayImportDirectory = DataEntry->Size /
494 sizeof(delay_import_directory_table_entry) - 1;
495
496 uintptr_t IntPtr = 0;
497 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
498 return EC;
499 DelayImportDirectory = reinterpret_cast<
500 const delay_import_directory_table_entry *>(IntPtr);
501 return object_error::success;
502}
503
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000504// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000505std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000506 // First, we get the RVA of the export table. If the file lacks a pointer to
507 // the export table, do nothing.
508 const data_directory *DataEntry;
509 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
510 return object_error::success;
511
512 // Do nothing if the pointer to export table is NULL.
513 if (DataEntry->RelativeVirtualAddress == 0)
514 return object_error::success;
515
516 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
517 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000518 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000519 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000520 ExportDirectory =
521 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000522 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000523}
524
Rafael Espindola48af1c22014-08-19 18:44:46 +0000525COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
526 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000527 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
528 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
529 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
530 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000531 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000532 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000533 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000534 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000535 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000536
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000537 // The current location in the file where we are looking at.
538 uint64_t CurPtr = 0;
539
540 // PE header is optional and is present only in executables. If it exists,
541 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000542 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000543
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000544 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000545 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000546 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
547 // PE signature to find 'normal' COFF header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000548 if (!checkSize(Data, EC, 0x3c + 8))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000549 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000550 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
551 // Check the PE magic bytes. ("PE\0\0")
David Majnemer44f51e52014-09-10 12:51:52 +0000552 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
553 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000554 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000555 return;
556 }
David Majnemer44f51e52014-09-10 12:51:52 +0000557 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000558 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000559 }
560
Rafael Espindola48af1c22014-08-19 18:44:46 +0000561 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000562 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000563
564 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
565 // import libraries share a common prefix but bigobj is more restrictive.
566 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
567 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
568 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
569 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
570 return;
571
572 // Verify that we are dealing with bigobj.
573 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
574 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
575 sizeof(COFF::BigObjMagic)) == 0) {
576 COFFHeader = nullptr;
577 CurPtr += sizeof(coff_bigobj_file_header);
578 } else {
579 // It's not a bigobj.
580 COFFBigObjHeader = nullptr;
581 }
582 }
583 if (COFFHeader) {
584 // The prior checkSize call may have failed. This isn't a hard error
585 // because we were just trying to sniff out bigobj.
586 EC = object_error::success;
587 CurPtr += sizeof(coff_file_header);
588
589 if (COFFHeader->isImportLibrary())
590 return;
591 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000592
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000593 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000594 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000595 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000596 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000597
598 const uint8_t *DataDirAddr;
599 uint64_t DataDirSize;
600 if (Header->Magic == 0x10b) {
601 PE32Header = Header;
602 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
603 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
604 } else if (Header->Magic == 0x20b) {
605 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
606 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
607 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
608 } else {
609 // It's neither PE32 nor PE32+.
610 EC = object_error::parse_failed;
611 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000612 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000613 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000614 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000615 CurPtr += COFFHeader->SizeOfOptionalHeader;
616 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000617
Rafael Espindola48af1c22014-08-19 18:44:46 +0000618 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000619 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000620 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000621
Rui Ueyamac2bed422013-09-27 21:04:00 +0000622 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000623 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000624 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000625 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000626
Rui Ueyamac2bed422013-09-27 21:04:00 +0000627 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000628 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000629 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000630 if ((EC = initDelayImportTablePtr()))
631 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000632
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000633 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000634 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000635 return;
636
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000637 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000638}
639
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000640basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000641 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000642 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000643 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000644}
645
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000646basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000647 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000648 DataRefImpl Ret;
649 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000650 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000651}
652
Rui Ueyamabc654b12013-09-27 21:47:05 +0000653import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000654 return import_directory_iterator(
655 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000656}
657
Rui Ueyamabc654b12013-09-27 21:47:05 +0000658import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000659 return import_directory_iterator(
660 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000661}
David Meyerc429b802012-03-01 22:19:54 +0000662
Rui Ueyama15d99352014-10-03 00:41:58 +0000663delay_import_directory_iterator
664COFFObjectFile::delay_import_directory_begin() const {
665 return delay_import_directory_iterator(
666 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
667}
668
669delay_import_directory_iterator
670COFFObjectFile::delay_import_directory_end() const {
671 return delay_import_directory_iterator(
672 DelayImportDirectoryEntryRef(
673 DelayImportDirectory, NumberOfDelayImportDirectory, this));
674}
675
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000676export_directory_iterator COFFObjectFile::export_directory_begin() const {
677 return export_directory_iterator(
678 ExportDirectoryEntryRef(ExportDirectory, 0, this));
679}
680
681export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000682 if (!ExportDirectory)
683 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000684 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000685 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000686 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000687}
688
Rafael Espindolab5155a52014-02-10 20:24:04 +0000689section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000690 DataRefImpl Ret;
691 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
692 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000693}
694
Rafael Espindolab5155a52014-02-10 20:24:04 +0000695section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000696 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000697 int NumSections =
698 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000699 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
700 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000701}
702
703uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000704 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000705}
706
707StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000708 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000709 case COFF::IMAGE_FILE_MACHINE_I386:
710 return "COFF-i386";
711 case COFF::IMAGE_FILE_MACHINE_AMD64:
712 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000713 case COFF::IMAGE_FILE_MACHINE_ARMNT:
714 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000715 default:
716 return "COFF-<unknown arch>";
717 }
718}
719
720unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000721 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000722 case COFF::IMAGE_FILE_MACHINE_I386:
723 return Triple::x86;
724 case COFF::IMAGE_FILE_MACHINE_AMD64:
725 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000726 case COFF::IMAGE_FILE_MACHINE_ARMNT:
727 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000728 default:
729 return Triple::UnknownArch;
730 }
731}
732
Rui Ueyama979fb402014-10-09 02:16:38 +0000733iterator_range<import_directory_iterator>
734COFFObjectFile::import_directories() const {
735 return make_range(import_directory_begin(), import_directory_end());
736}
737
738iterator_range<delay_import_directory_iterator>
739COFFObjectFile::delay_import_directories() const {
740 return make_range(delay_import_directory_begin(),
741 delay_import_directory_end());
742}
743
744iterator_range<export_directory_iterator>
745COFFObjectFile::export_directories() const {
746 return make_range(export_directory_begin(), export_directory_end());
747}
748
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000749std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000750 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000751 return object_error::success;
752}
753
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000754std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000755COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
756 Res = PE32PlusHeader;
757 return object_error::success;
758}
759
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000760std::error_code
761COFFObjectFile::getDataDirectory(uint32_t Index,
762 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000763 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000764 if (!DataDirectory)
765 return object_error::parse_failed;
766 assert(PE32Header || PE32PlusHeader);
767 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
768 : PE32PlusHeader->NumberOfRvaAndSize;
769 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000770 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000771 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000772 return object_error::success;
773}
774
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000775std::error_code COFFObjectFile::getSection(int32_t Index,
776 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000777 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000778 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000779 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000780 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000781 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000782 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000783 else
784 return object_error::parse_failed;
785 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000786}
787
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000788std::error_code COFFObjectFile::getString(uint32_t Offset,
789 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000790 if (StringTableSize <= 4)
791 // Tried to get a string from an empty string table.
792 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000793 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000794 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000795 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000796 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000797}
798
David Majnemer44f51e52014-09-10 12:51:52 +0000799std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000800 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000801 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000802 if (Symbol.getStringTableOffset().Zeroes == 0) {
803 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000804 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000805 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000806 return object_error::success;
807 }
808
David Majnemer44f51e52014-09-10 12:51:52 +0000809 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000810 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000811 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000812 else
813 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000814 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000815 return object_error::success;
816}
817
David Majnemer44f51e52014-09-10 12:51:52 +0000818ArrayRef<uint8_t>
819COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000820 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000821
David Majnemer44f51e52014-09-10 12:51:52 +0000822 size_t SymbolSize = getSymbolTableEntrySize();
823 if (Symbol.getNumberOfAuxSymbols() > 0) {
824 // AUX data comes immediately after the symbol in COFF
825 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000826# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000827 // Verify that the Aux symbol points to a valid entry in the symbol table.
828 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000829 if (Offset < getPointerToSymbolTable() ||
830 Offset >=
831 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000832 report_fatal_error("Aux Symbol data was outside of symbol table.");
833
David Majnemer44f51e52014-09-10 12:51:52 +0000834 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
835 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000836# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000837 }
David Majnemer44f51e52014-09-10 12:51:52 +0000838 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000839}
840
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000841std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
842 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000843 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000844 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000845 // Null terminated, let ::strlen figure out the length.
846 Name = Sec->Name;
847 else
848 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000849 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000850
851 // Check for string table entry. First byte is '/'.
852 if (Name[0] == '/') {
853 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000854 if (Name[1] == '/') {
855 if (decodeBase64StringEntry(Name.substr(2), Offset))
856 return object_error::parse_failed;
857 } else {
858 if (Name.substr(1).getAsInteger(10, Offset))
859 return object_error::parse_failed;
860 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000861 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000862 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000863 }
864
865 Res = Name;
866 return object_error::success;
867}
868
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000869std::error_code
870COFFObjectFile::getSectionContents(const coff_section *Sec,
871 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000872 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
873 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000874 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
875 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000876 // The only thing that we need to verify is that the contents is contained
877 // within the file bounds. We don't need to make sure it doesn't cover other
878 // data, as there's nothing that says that is not allowed.
879 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemerdd9cff22014-10-09 07:49:28 +0000880 // SizeOfRawData and VirtualSize change what they represent depending on
881 // whether or not we have an executable image.
882 //
883 // For object files, SizeOfRawData contains the size of section's data;
884 // VirtualSize is always zero.
885 //
886 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
887 // actual section size is in VirtualSize. It is possible for VirtualSize to
888 // be greater than SizeOfRawData; the contents past that point should be
889 // considered to be zero.
890 uint32_t DataSize;
891 if (Sec->VirtualSize)
892 DataSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
893 else
894 DataSize = Sec->SizeOfRawData;
895 uintptr_t ConEnd = ConStart + DataSize;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000896 if (ConEnd > uintptr_t(Data.getBufferEnd()))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000897 return object_error::parse_failed;
David Majnemerdd9cff22014-10-09 07:49:28 +0000898 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), DataSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000899 return object_error::success;
900}
901
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000902const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000903 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000904}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000905
Rafael Espindola5e812af2014-01-30 02:49:50 +0000906void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000907 Rel.p = reinterpret_cast<uintptr_t>(
908 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000909}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000910
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000911std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
912 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000913 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000914}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000915
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000916std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
917 uint64_t &Res) const {
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000918 Res = toRel(Rel)->VirtualAddress;
919 return object_error::success;
920}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000921
Rafael Espindola806f0062013-06-05 01:33:53 +0000922symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000923 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000924 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +0000925 if (SymbolTable16)
926 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
927 else if (SymbolTable32)
928 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
929 else
930 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000931 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000932}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000933
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000934std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
935 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000936 const coff_relocation* R = toRel(Rel);
937 Res = R->Type;
938 return object_error::success;
939}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000940
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000941const coff_section *
942COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
943 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000944}
945
David Majnemer44f51e52014-09-10 12:51:52 +0000946COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
947 if (SymbolTable16)
948 return toSymb<coff_symbol16>(Ref);
949 if (SymbolTable32)
950 return toSymb<coff_symbol32>(Ref);
951 llvm_unreachable("no symbol table pointer!");
952}
953
954COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
955 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000956}
957
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000958const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000959COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
960 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +0000961}
962
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000963#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
964 case COFF::reloc_type: \
965 Res = #reloc_type; \
966 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000967
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000968std::error_code
969COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
970 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000971 const coff_relocation *Reloc = toRel(Rel);
972 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +0000973 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000974 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000975 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000976 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
977 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
978 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
979 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
980 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
983 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
984 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
985 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
986 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
988 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
989 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
990 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
991 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
992 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
993 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000994 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000995 }
996 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +0000997 case COFF::IMAGE_FILE_MACHINE_ARMNT:
998 switch (Reloc->Type) {
999 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1000 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1001 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1002 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1003 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1004 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1005 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1006 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1007 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1008 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1009 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1010 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1011 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1012 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1013 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1014 default:
1015 Res = "Unknown";
1016 }
1017 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001018 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001019 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001020 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1021 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1022 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1023 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1024 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1025 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1026 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1027 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1028 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1029 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1030 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1031 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001032 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001033 }
1034 break;
1035 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001036 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001037 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001038 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001039 return object_error::success;
1040}
1041
1042#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1043
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001044std::error_code
1045COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1046 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001047 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001048 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001049 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1050 if (std::error_code EC = Symb.getError())
1051 return EC;
1052 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001053 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001054 if (std::error_code EC = getSymbolName(Sym, SymName))
1055 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001056 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001057 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001058}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001059
Rafael Espindolac66d7612014-08-17 19:09:37 +00001060bool COFFObjectFile::isRelocatableObject() const {
1061 return !DataDirectory;
1062}
1063
Rui Ueyamac2bed422013-09-27 21:04:00 +00001064bool ImportDirectoryEntryRef::
1065operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001066 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001067}
1068
Rafael Espindola5e812af2014-01-30 02:49:50 +00001069void ImportDirectoryEntryRef::moveNext() {
1070 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001071}
1072
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001073std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1074 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001075 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001076 return object_error::success;
1077}
1078
Rui Ueyama861021f2014-10-02 22:05:29 +00001079static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001080makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001081 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001082 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001083 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001084 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001085 }
1086 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001087 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001088}
1089
Rui Ueyama15d99352014-10-03 00:41:58 +00001090static imported_symbol_iterator
1091importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001092 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001093 Object->getRvaPtr(RVA, IntPtr);
1094 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001095}
1096
Rui Ueyama15d99352014-10-03 00:41:58 +00001097static imported_symbol_iterator
1098importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001099 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001100 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001101 // Forward the pointer to the last entry which is null.
1102 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001103 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001104 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1105 while (*Entry++)
1106 ++Index;
1107 } else {
1108 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1109 while (*Entry++)
1110 ++Index;
1111 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001112 return makeImportedSymbolIterator(Object, IntPtr, Index);
1113}
1114
1115imported_symbol_iterator
1116ImportDirectoryEntryRef::imported_symbol_begin() const {
1117 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1118 OwningObject);
1119}
1120
1121imported_symbol_iterator
1122ImportDirectoryEntryRef::imported_symbol_end() const {
1123 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1124 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001125}
1126
Rui Ueyama979fb402014-10-09 02:16:38 +00001127iterator_range<imported_symbol_iterator>
1128ImportDirectoryEntryRef::imported_symbols() const {
1129 return make_range(imported_symbol_begin(), imported_symbol_end());
1130}
1131
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001132std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001133 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001134 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001135 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001136 return EC;
1137 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001138 return object_error::success;
1139}
1140
Rui Ueyama1e152d52014-10-02 17:02:18 +00001141std::error_code
1142ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1143 Result = ImportTable[Index].ImportLookupTableRVA;
1144 return object_error::success;
1145}
1146
1147std::error_code
1148ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1149 Result = ImportTable[Index].ImportAddressTableRVA;
1150 return object_error::success;
1151}
1152
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001153std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001154 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001155 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001156 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1157 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001158 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001159 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1160 return object_error::success;
1161}
1162
Rui Ueyama15d99352014-10-03 00:41:58 +00001163bool DelayImportDirectoryEntryRef::
1164operator==(const DelayImportDirectoryEntryRef &Other) const {
1165 return Table == Other.Table && Index == Other.Index;
1166}
1167
1168void DelayImportDirectoryEntryRef::moveNext() {
1169 ++Index;
1170}
1171
1172imported_symbol_iterator
1173DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1174 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1175 OwningObject);
1176}
1177
1178imported_symbol_iterator
1179DelayImportDirectoryEntryRef::imported_symbol_end() const {
1180 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1181 OwningObject);
1182}
1183
Rui Ueyama979fb402014-10-09 02:16:38 +00001184iterator_range<imported_symbol_iterator>
1185DelayImportDirectoryEntryRef::imported_symbols() const {
1186 return make_range(imported_symbol_begin(), imported_symbol_end());
1187}
1188
Rui Ueyama15d99352014-10-03 00:41:58 +00001189std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1190 uintptr_t IntPtr = 0;
1191 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1192 return EC;
1193 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1194 return object_error::success;
1195}
1196
Rui Ueyama1af08652014-10-03 18:07:18 +00001197std::error_code DelayImportDirectoryEntryRef::
1198getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1199 Result = Table;
1200 return object_error::success;
1201}
1202
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001203bool ExportDirectoryEntryRef::
1204operator==(const ExportDirectoryEntryRef &Other) const {
1205 return ExportTable == Other.ExportTable && Index == Other.Index;
1206}
1207
Rafael Espindola5e812af2014-01-30 02:49:50 +00001208void ExportDirectoryEntryRef::moveNext() {
1209 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001210}
1211
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001212// Returns the name of the current export symbol. If the symbol is exported only
1213// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001214std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001215 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001216 if (std::error_code EC =
1217 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001218 return EC;
1219 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1220 return object_error::success;
1221}
1222
Rui Ueyamae5df6092014-01-17 22:02:24 +00001223// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001224std::error_code
1225ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001226 Result = ExportTable->OrdinalBase;
1227 return object_error::success;
1228}
1229
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001230// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001231std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001232 Result = ExportTable->OrdinalBase + Index;
1233 return object_error::success;
1234}
1235
1236// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001237std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001238 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001239 if (std::error_code EC =
1240 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001241 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001242 const export_address_table_entry *entry =
1243 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001244 Result = entry[Index].ExportRVA;
1245 return object_error::success;
1246}
1247
1248// Returns the name of the current export symbol. If the symbol is exported only
1249// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001250std::error_code
1251ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001252 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001253 if (std::error_code EC =
1254 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001255 return EC;
1256 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1257
1258 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1259 int Offset = 0;
1260 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1261 I < E; ++I, ++Offset) {
1262 if (*I != Index)
1263 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001264 if (std::error_code EC =
1265 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001266 return EC;
1267 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001268 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001269 return EC;
1270 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1271 return object_error::success;
1272 }
1273 Result = "";
1274 return object_error::success;
1275}
1276
Rui Ueyama861021f2014-10-02 22:05:29 +00001277bool ImportedSymbolRef::
1278operator==(const ImportedSymbolRef &Other) const {
1279 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1280 && Index == Other.Index;
1281}
1282
1283void ImportedSymbolRef::moveNext() {
1284 ++Index;
1285}
1286
1287std::error_code
1288ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1289 uint32_t RVA;
1290 if (Entry32) {
1291 // If a symbol is imported only by ordinal, it has no name.
1292 if (Entry32[Index].isOrdinal())
1293 return object_error::success;
1294 RVA = Entry32[Index].getHintNameRVA();
1295 } else {
1296 if (Entry64[Index].isOrdinal())
1297 return object_error::success;
1298 RVA = Entry64[Index].getHintNameRVA();
1299 }
1300 uintptr_t IntPtr = 0;
1301 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1302 return EC;
1303 // +2 because the first two bytes is hint.
1304 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1305 return object_error::success;
1306}
1307
1308std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1309 uint32_t RVA;
1310 if (Entry32) {
1311 if (Entry32[Index].isOrdinal()) {
1312 Result = Entry32[Index].getOrdinal();
1313 return object_error::success;
1314 }
1315 RVA = Entry32[Index].getHintNameRVA();
1316 } else {
1317 if (Entry64[Index].isOrdinal()) {
1318 Result = Entry64[Index].getOrdinal();
1319 return object_error::success;
1320 }
1321 RVA = Entry64[Index].getHintNameRVA();
1322 }
1323 uintptr_t IntPtr = 0;
1324 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1325 return EC;
1326 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1327 return object_error::success;
1328}
1329
Rafael Espindola437b0d52014-07-31 03:12:45 +00001330ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001331ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001332 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001333 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001334 if (EC)
1335 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001336 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001337}