blob: 3beab00ed7d53af55c7b3bd0042745e315f92679 [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 {
David Majnemera9ee5c02014-10-09 08:42:31 +0000274 return getSectionSize(toSec(Ref));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000275}
276
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000277std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
278 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000279 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000280 ArrayRef<uint8_t> Res;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000281 std::error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000282 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
283 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000284}
285
Rafael Espindola80291272014-10-08 15:28:58 +0000286uint64_t COFFObjectFile::getSectionAlignment(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000287 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000288 return uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000289}
290
Rafael Espindola80291272014-10-08 15:28:58 +0000291bool COFFObjectFile::isSectionText(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000292 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000293 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000294}
295
Rafael Espindola80291272014-10-08 15:28:58 +0000296bool COFFObjectFile::isSectionData(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000297 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000298 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000299}
300
Rafael Espindola80291272014-10-08 15:28:58 +0000301bool COFFObjectFile::isSectionBSS(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000302 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000303 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000304}
305
Rafael Espindola80291272014-10-08 15:28:58 +0000306bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
Preston Gurd2138ef62012-04-12 20:13:57 +0000307 // FIXME: Unimplemented
Rafael Espindola80291272014-10-08 15:28:58 +0000308 return true;
Preston Gurd2138ef62012-04-12 20:13:57 +0000309}
310
Rafael Espindola80291272014-10-08 15:28:58 +0000311bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000312 const coff_section *Sec = toSec(Ref);
Rafael Espindola80291272014-10-08 15:28:58 +0000313 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000314}
315
Rafael Espindola80291272014-10-08 15:28:58 +0000316bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000317 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000318 return false;
Preston Gurd2138ef62012-04-12 20:13:57 +0000319}
320
Rafael Espindola80291272014-10-08 15:28:58 +0000321bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000322 // FIXME: Unimplemented.
Rafael Espindola80291272014-10-08 15:28:58 +0000323 return false;
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000324}
325
Rafael Espindola80291272014-10-08 15:28:58 +0000326bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
327 DataRefImpl SymbRef) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000328 const coff_section *Sec = toSec(SecRef);
David Majnemer44f51e52014-09-10 12:51:52 +0000329 COFFSymbolRef Symb = getCOFFSymbol(SymbRef);
Rafael Espindolaa9260862014-10-07 20:42:47 +0000330 int32_t SecNumber = (Sec - SectionTable) + 1;
Rafael Espindola80291272014-10-08 15:28:58 +0000331 return SecNumber == Symb.getSectionNumber();
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000332}
333
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000334relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
335 const coff_section *Sec = toSec(Ref);
336 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000337 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000338 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000339 } else {
340 auto begin = reinterpret_cast<const coff_relocation*>(
341 base() + Sec->PointerToRelocations);
342 if (Sec->hasExtendedRelocations()) {
343 // Skip the first relocation entry repurposed to store the number of
344 // relocations.
345 begin++;
346 }
347 Ret.p = reinterpret_cast<uintptr_t>(begin);
348 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000349 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000350}
351
Rui Ueyama827c8a22014-03-21 00:44:19 +0000352static uint32_t getNumberOfRelocations(const coff_section *Sec,
353 const uint8_t *base) {
354 // The field for the number of relocations in COFF section table is only
355 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
356 // NumberOfRelocations field, and the actual relocation count is stored in the
357 // VirtualAddress field in the first relocation entry.
358 if (Sec->hasExtendedRelocations()) {
359 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
360 base + Sec->PointerToRelocations);
361 return FirstReloc->VirtualAddress;
362 }
363 return Sec->NumberOfRelocations;
364}
365
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000366relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
367 const coff_section *Sec = toSec(Ref);
368 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000369 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000370 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000371 } else {
372 auto begin = reinterpret_cast<const coff_relocation*>(
373 base() + Sec->PointerToRelocations);
374 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
375 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
376 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000377 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000378}
379
Rui Ueyamac2bed422013-09-27 21:04:00 +0000380// Initialize the pointer to the symbol table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000381std::error_code COFFObjectFile::initSymbolTablePtr() {
David Majnemer44f51e52014-09-10 12:51:52 +0000382 if (COFFHeader)
383 if (std::error_code EC =
384 getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(),
385 getNumberOfSymbols() * getSymbolTableEntrySize()))
386 return EC;
387
388 if (COFFBigObjHeader)
389 if (std::error_code EC =
390 getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(),
391 getNumberOfSymbols() * getSymbolTableEntrySize()))
392 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000393
394 // Find string table. The first four byte of the string table contains the
395 // total size of the string table, including the size field itself. If the
396 // string table is empty, the value of the first four byte would be 4.
397 const uint8_t *StringTableAddr =
David Majnemer44f51e52014-09-10 12:51:52 +0000398 base() + getPointerToSymbolTable() +
399 getNumberOfSymbols() * getSymbolTableEntrySize();
Rui Ueyamac2bed422013-09-27 21:04:00 +0000400 const ulittle32_t *StringTableSizePtr;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000401 if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000402 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000403 StringTableSize = *StringTableSizePtr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000404 if (std::error_code EC =
Rafael Espindola48af1c22014-08-19 18:44:46 +0000405 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000406 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000407
Nico Rieck773a5792014-02-26 19:51:44 +0000408 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
409 // tools like cvtres write a size of 0 for an empty table instead of 4.
410 if (StringTableSize < 4)
411 StringTableSize = 4;
412
Rui Ueyamac2bed422013-09-27 21:04:00 +0000413 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000414 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000415 return object_error::parse_failed;
416 return object_error::success;
417}
418
Rui Ueyama215a5862014-02-20 06:51:07 +0000419// Returns the file offset for the given VA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000420std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000421 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
422 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000423 uint64_t Rva = Addr - ImageBase;
424 assert(Rva <= UINT32_MAX);
425 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000426}
427
Rui Ueyamac2bed422013-09-27 21:04:00 +0000428// Returns the file offset for the given RVA.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000429std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000430 for (const SectionRef &S : sections()) {
431 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000432 uint32_t SectionStart = Section->VirtualAddress;
433 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000434 if (SectionStart <= Addr && Addr < SectionEnd) {
435 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000436 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
437 return object_error::success;
438 }
439 }
440 return object_error::parse_failed;
441}
442
443// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
444// table entry.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000445std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
446 StringRef &Name) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000447 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000448 if (std::error_code EC = getRvaPtr(Rva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000449 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000450 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
451 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
452 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
453 return object_error::success;
454}
455
456// Find the import table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000457std::error_code COFFObjectFile::initImportTablePtr() {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000458 // First, we get the RVA of the import table. If the file lacks a pointer to
459 // the import table, do nothing.
460 const data_directory *DataEntry;
461 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
462 return object_error::success;
463
464 // Do nothing if the pointer to import table is NULL.
465 if (DataEntry->RelativeVirtualAddress == 0)
466 return object_error::success;
467
468 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
Rui Ueyama1e152d52014-10-02 17:02:18 +0000469 // -1 because the last entry is the null entry.
Rui Ueyamac2bed422013-09-27 21:04:00 +0000470 NumberOfImportDirectory = DataEntry->Size /
Rui Ueyama1e152d52014-10-02 17:02:18 +0000471 sizeof(import_directory_table_entry) - 1;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000472
473 // Find the section that contains the RVA. This is needed because the RVA is
474 // the import table's memory address which is different from its file offset.
475 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000476 if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000477 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000478 ImportDirectory = reinterpret_cast<
479 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000480 return object_error::success;
481}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000482
Rui Ueyama15d99352014-10-03 00:41:58 +0000483// Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
484std::error_code COFFObjectFile::initDelayImportTablePtr() {
485 const data_directory *DataEntry;
486 if (getDataDirectory(COFF::DELAY_IMPORT_DESCRIPTOR, DataEntry))
487 return object_error::success;
488 if (DataEntry->RelativeVirtualAddress == 0)
489 return object_error::success;
490
491 uint32_t RVA = DataEntry->RelativeVirtualAddress;
492 NumberOfDelayImportDirectory = DataEntry->Size /
493 sizeof(delay_import_directory_table_entry) - 1;
494
495 uintptr_t IntPtr = 0;
496 if (std::error_code EC = getRvaPtr(RVA, IntPtr))
497 return EC;
498 DelayImportDirectory = reinterpret_cast<
499 const delay_import_directory_table_entry *>(IntPtr);
500 return object_error::success;
501}
502
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000503// Find the export table.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000504std::error_code COFFObjectFile::initExportTablePtr() {
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000505 // First, we get the RVA of the export table. If the file lacks a pointer to
506 // the export table, do nothing.
507 const data_directory *DataEntry;
508 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
509 return object_error::success;
510
511 // Do nothing if the pointer to export table is NULL.
512 if (DataEntry->RelativeVirtualAddress == 0)
513 return object_error::success;
514
515 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
516 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000517 if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000518 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000519 ExportDirectory =
520 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000521 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000522}
523
Rafael Espindola48af1c22014-08-19 18:44:46 +0000524COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
525 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
David Majnemer44f51e52014-09-10 12:51:52 +0000526 COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
527 DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
528 SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
529 ImportDirectory(nullptr), NumberOfImportDirectory(0),
Rui Ueyama15d99352014-10-03 00:41:58 +0000530 DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
Craig Topper2617dcc2014-04-15 06:32:26 +0000531 ExportDirectory(nullptr) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000532 // Check that we at least have enough room for a header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000533 if (!checkSize(Data, EC, sizeof(coff_file_header)))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000534 return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000535
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000536 // The current location in the file where we are looking at.
537 uint64_t CurPtr = 0;
538
539 // PE header is optional and is present only in executables. If it exists,
540 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000541 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000542
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000543 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000544 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000545 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
546 // PE signature to find 'normal' COFF header.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000547 if (!checkSize(Data, EC, 0x3c + 8))
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +0000548 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000549 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
550 // Check the PE magic bytes. ("PE\0\0")
David Majnemer44f51e52014-09-10 12:51:52 +0000551 if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) !=
552 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000553 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000554 return;
555 }
David Majnemer44f51e52014-09-10 12:51:52 +0000556 CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000557 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000558 }
559
Rafael Espindola48af1c22014-08-19 18:44:46 +0000560 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000561 return;
David Majnemer44f51e52014-09-10 12:51:52 +0000562
563 // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
564 // import libraries share a common prefix but bigobj is more restrictive.
565 if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
566 COFFHeader->NumberOfSections == uint16_t(0xffff) &&
567 checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
568 if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
569 return;
570
571 // Verify that we are dealing with bigobj.
572 if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
573 std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
574 sizeof(COFF::BigObjMagic)) == 0) {
575 COFFHeader = nullptr;
576 CurPtr += sizeof(coff_bigobj_file_header);
577 } else {
578 // It's not a bigobj.
579 COFFBigObjHeader = nullptr;
580 }
581 }
582 if (COFFHeader) {
583 // The prior checkSize call may have failed. This isn't a hard error
584 // because we were just trying to sniff out bigobj.
585 EC = object_error::success;
586 CurPtr += sizeof(coff_file_header);
587
588 if (COFFHeader->isImportLibrary())
589 return;
590 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000591
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000592 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000593 const pe32_header *Header;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000594 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000595 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000596
597 const uint8_t *DataDirAddr;
598 uint64_t DataDirSize;
599 if (Header->Magic == 0x10b) {
600 PE32Header = Header;
601 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
602 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
603 } else if (Header->Magic == 0x20b) {
604 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
605 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
606 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
607 } else {
608 // It's neither PE32 nor PE32+.
609 EC = object_error::parse_failed;
610 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000611 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000612 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000613 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000614 CurPtr += COFFHeader->SizeOfOptionalHeader;
615 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000616
Rafael Espindola48af1c22014-08-19 18:44:46 +0000617 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
David Majnemer44f51e52014-09-10 12:51:52 +0000618 getNumberOfSections() * sizeof(coff_section))))
Rafael Espindola692410e2014-01-21 23:06:54 +0000619 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000620
Rui Ueyamac2bed422013-09-27 21:04:00 +0000621 // Initialize the pointer to the symbol table.
David Majnemer44f51e52014-09-10 12:51:52 +0000622 if (getPointerToSymbolTable() != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000623 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000624 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000625
Rui Ueyamac2bed422013-09-27 21:04:00 +0000626 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000627 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000628 return;
Rui Ueyama15d99352014-10-03 00:41:58 +0000629 if ((EC = initDelayImportTablePtr()))
630 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000631
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000632 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000633 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000634 return;
635
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000636 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000637}
638
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000639basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000640 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000641 Ret.p = getSymbolTable();
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000642 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000643}
644
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000645basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000646 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000647 DataRefImpl Ret;
648 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000649 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000650}
651
Rui Ueyamabc654b12013-09-27 21:47:05 +0000652import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000653 return import_directory_iterator(
654 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000655}
656
Rui Ueyamabc654b12013-09-27 21:47:05 +0000657import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000658 return import_directory_iterator(
659 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000660}
David Meyerc429b802012-03-01 22:19:54 +0000661
Rui Ueyama15d99352014-10-03 00:41:58 +0000662delay_import_directory_iterator
663COFFObjectFile::delay_import_directory_begin() const {
664 return delay_import_directory_iterator(
665 DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
666}
667
668delay_import_directory_iterator
669COFFObjectFile::delay_import_directory_end() const {
670 return delay_import_directory_iterator(
671 DelayImportDirectoryEntryRef(
672 DelayImportDirectory, NumberOfDelayImportDirectory, this));
673}
674
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000675export_directory_iterator COFFObjectFile::export_directory_begin() const {
676 return export_directory_iterator(
677 ExportDirectoryEntryRef(ExportDirectory, 0, this));
678}
679
680export_directory_iterator COFFObjectFile::export_directory_end() const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000681 if (!ExportDirectory)
682 return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000683 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000684 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000685 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000686}
687
Rafael Espindolab5155a52014-02-10 20:24:04 +0000688section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000689 DataRefImpl Ret;
690 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
691 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000692}
693
Rafael Espindolab5155a52014-02-10 20:24:04 +0000694section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000695 DataRefImpl Ret;
David Majnemer44f51e52014-09-10 12:51:52 +0000696 int NumSections =
697 COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000698 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
699 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000700}
701
702uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000703 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000704}
705
706StringRef COFFObjectFile::getFileFormatName() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000707 switch(getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000708 case COFF::IMAGE_FILE_MACHINE_I386:
709 return "COFF-i386";
710 case COFF::IMAGE_FILE_MACHINE_AMD64:
711 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000712 case COFF::IMAGE_FILE_MACHINE_ARMNT:
713 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000714 default:
715 return "COFF-<unknown arch>";
716 }
717}
718
719unsigned COFFObjectFile::getArch() const {
David Majnemer44f51e52014-09-10 12:51:52 +0000720 switch (getMachine()) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000721 case COFF::IMAGE_FILE_MACHINE_I386:
722 return Triple::x86;
723 case COFF::IMAGE_FILE_MACHINE_AMD64:
724 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000725 case COFF::IMAGE_FILE_MACHINE_ARMNT:
726 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000727 default:
728 return Triple::UnknownArch;
729 }
730}
731
Rui Ueyama979fb402014-10-09 02:16:38 +0000732iterator_range<import_directory_iterator>
733COFFObjectFile::import_directories() const {
734 return make_range(import_directory_begin(), import_directory_end());
735}
736
737iterator_range<delay_import_directory_iterator>
738COFFObjectFile::delay_import_directories() const {
739 return make_range(delay_import_directory_begin(),
740 delay_import_directory_end());
741}
742
743iterator_range<export_directory_iterator>
744COFFObjectFile::export_directories() const {
745 return make_range(export_directory_begin(), export_directory_end());
746}
747
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000748std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000749 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000750 return object_error::success;
751}
752
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000753std::error_code
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000754COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
755 Res = PE32PlusHeader;
756 return object_error::success;
757}
758
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000759std::error_code
760COFFObjectFile::getDataDirectory(uint32_t Index,
761 const data_directory *&Res) const {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000762 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000763 if (!DataDirectory)
764 return object_error::parse_failed;
765 assert(PE32Header || PE32PlusHeader);
766 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
767 : PE32PlusHeader->NumberOfRvaAndSize;
768 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000769 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000770 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000771 return object_error::success;
772}
773
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000774std::error_code COFFObjectFile::getSection(int32_t Index,
775 const coff_section *&Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000776 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000777 if (COFF::isReservedSectionNumber(Index))
Craig Topper2617dcc2014-04-15 06:32:26 +0000778 Result = nullptr;
David Majnemer44f51e52014-09-10 12:51:52 +0000779 else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections())
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000780 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000781 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000782 else
783 return object_error::parse_failed;
784 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000785}
786
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000787std::error_code COFFObjectFile::getString(uint32_t Offset,
788 StringRef &Result) const {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000789 if (StringTableSize <= 4)
790 // Tried to get a string from an empty string table.
791 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000792 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000793 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000794 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000795 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000796}
797
David Majnemer44f51e52014-09-10 12:51:52 +0000798std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000799 StringRef &Res) const {
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000800 // Check for string table entry. First 4 bytes are 0.
David Majnemer44f51e52014-09-10 12:51:52 +0000801 if (Symbol.getStringTableOffset().Zeroes == 0) {
802 uint32_t Offset = Symbol.getStringTableOffset().Offset;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000803 if (std::error_code EC = getString(Offset, Res))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000804 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000805 return object_error::success;
806 }
807
David Majnemer44f51e52014-09-10 12:51:52 +0000808 if (Symbol.getShortName()[COFF::NameSize - 1] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000809 // Null terminated, let ::strlen figure out the length.
David Majnemer44f51e52014-09-10 12:51:52 +0000810 Res = StringRef(Symbol.getShortName());
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000811 else
812 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000813 Res = StringRef(Symbol.getShortName(), COFF::NameSize);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000814 return object_error::success;
815}
816
David Majnemer44f51e52014-09-10 12:51:52 +0000817ArrayRef<uint8_t>
818COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
Craig Topper2617dcc2014-04-15 06:32:26 +0000819 const uint8_t *Aux = nullptr;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000820
David Majnemer44f51e52014-09-10 12:51:52 +0000821 size_t SymbolSize = getSymbolTableEntrySize();
822 if (Symbol.getNumberOfAuxSymbols() > 0) {
823 // AUX data comes immediately after the symbol in COFF
824 Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
Marshall Clow71757ef2012-06-15 01:08:25 +0000825# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000826 // Verify that the Aux symbol points to a valid entry in the symbol table.
827 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
David Majnemer44f51e52014-09-10 12:51:52 +0000828 if (Offset < getPointerToSymbolTable() ||
829 Offset >=
830 getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
Marshall Clow71757ef2012-06-15 01:08:25 +0000831 report_fatal_error("Aux Symbol data was outside of symbol table.");
832
David Majnemer44f51e52014-09-10 12:51:52 +0000833 assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
834 "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000835# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000836 }
David Majnemer44f51e52014-09-10 12:51:52 +0000837 return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
Marshall Clow71757ef2012-06-15 01:08:25 +0000838}
839
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000840std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
841 StringRef &Res) const {
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000842 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000843 if (Sec->Name[COFF::NameSize - 1] == 0)
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000844 // Null terminated, let ::strlen figure out the length.
845 Name = Sec->Name;
846 else
847 // Not null terminated, use all 8 bytes.
David Majnemer44f51e52014-09-10 12:51:52 +0000848 Name = StringRef(Sec->Name, COFF::NameSize);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000849
850 // Check for string table entry. First byte is '/'.
851 if (Name[0] == '/') {
852 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000853 if (Name[1] == '/') {
854 if (decodeBase64StringEntry(Name.substr(2), Offset))
855 return object_error::parse_failed;
856 } else {
857 if (Name.substr(1).getAsInteger(10, Offset))
858 return object_error::parse_failed;
859 }
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000860 if (std::error_code EC = getString(Offset, Name))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000861 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000862 }
863
864 Res = Name;
865 return object_error::success;
866}
867
David Majnemera9ee5c02014-10-09 08:42:31 +0000868uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
869 // SizeOfRawData and VirtualSize change what they represent depending on
870 // whether or not we have an executable image.
871 //
872 // For object files, SizeOfRawData contains the size of section's data;
873 // VirtualSize is always zero.
874 //
875 // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
876 // actual section size is in VirtualSize. It is possible for VirtualSize to
877 // be greater than SizeOfRawData; the contents past that point should be
878 // considered to be zero.
879 uint32_t SectionSize;
880 if (Sec->VirtualSize)
881 SectionSize = std::min(Sec->VirtualSize, Sec->SizeOfRawData);
882 else
883 SectionSize = Sec->SizeOfRawData;
884
885 return SectionSize;
886}
887
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000888std::error_code
889COFFObjectFile::getSectionContents(const coff_section *Sec,
890 ArrayRef<uint8_t> &Res) const {
David Majnemerdd9cff22014-10-09 07:49:28 +0000891 // PointerToRawData and SizeOfRawData won't make sense for BSS sections,
892 // don't do anything interesting for them.
David Majnemerdac39852014-09-26 22:32:16 +0000893 assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
894 "BSS sections don't have contents!");
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000895 // The only thing that we need to verify is that the contents is contained
896 // within the file bounds. We don't need to make sure it doesn't cover other
897 // data, as there's nothing that says that is not allowed.
898 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
David Majnemera9ee5c02014-10-09 08:42:31 +0000899 uint32_t SectionSize = getSectionSize(Sec);
900 uintptr_t ConEnd = ConStart + SectionSize;
Rafael Espindola48af1c22014-08-19 18:44:46 +0000901 if (ConEnd > uintptr_t(Data.getBufferEnd()))
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000902 return object_error::parse_failed;
David Majnemera9ee5c02014-10-09 08:42:31 +0000903 Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000904 return object_error::success;
905}
906
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000907const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000908 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000909}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000910
Rafael Espindola5e812af2014-01-30 02:49:50 +0000911void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000912 Rel.p = reinterpret_cast<uintptr_t>(
913 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
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::getRelocationAddress(DataRefImpl Rel,
917 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000918 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000919}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000920
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000921std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
922 uint64_t &Res) const {
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000923 Res = toRel(Rel)->VirtualAddress;
924 return object_error::success;
925}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000926
Rafael Espindola806f0062013-06-05 01:33:53 +0000927symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
David Majnemer44f51e52014-09-10 12:51:52 +0000928 const coff_relocation *R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000929 DataRefImpl Ref;
David Majnemer44f51e52014-09-10 12:51:52 +0000930 if (SymbolTable16)
931 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
932 else if (SymbolTable32)
933 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
934 else
935 llvm_unreachable("no symbol table pointer!");
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000936 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000937}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000938
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000939std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
940 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000941 const coff_relocation* R = toRel(Rel);
942 Res = R->Type;
943 return object_error::success;
944}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000945
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000946const coff_section *
947COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
948 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000949}
950
David Majnemer44f51e52014-09-10 12:51:52 +0000951COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const {
952 if (SymbolTable16)
953 return toSymb<coff_symbol16>(Ref);
954 if (SymbolTable32)
955 return toSymb<coff_symbol32>(Ref);
956 llvm_unreachable("no symbol table pointer!");
957}
958
959COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
960 return getCOFFSymbol(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000961}
962
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000963const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000964COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
965 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +0000966}
967
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000968#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
969 case COFF::reloc_type: \
970 Res = #reloc_type; \
971 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000972
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000973std::error_code
974COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
975 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000976 const coff_relocation *Reloc = toRel(Rel);
977 StringRef Res;
David Majnemer44f51e52014-09-10 12:51:52 +0000978 switch (getMachine()) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000979 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000980 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000981 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
982 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
983 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
984 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
985 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
986 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
987 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
988 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
989 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
990 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
991 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
992 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
993 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
994 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
995 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
996 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
997 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
998 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000999 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001000 }
1001 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +00001002 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1003 switch (Reloc->Type) {
1004 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
1005 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
1006 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
1007 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
1008 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
1009 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
1010 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
1011 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
1012 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
1013 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
1014 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
1015 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
1016 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
1017 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
1018 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
1019 default:
1020 Res = "Unknown";
1021 }
1022 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001023 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001024 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001025 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
1026 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
1027 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
1028 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
1029 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
1030 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
1031 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
1032 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
1033 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
1034 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
1035 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
1036 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001037 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001038 }
1039 break;
1040 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001041 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001042 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001043 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001044 return object_error::success;
1045}
1046
1047#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1048
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001049std::error_code
1050COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
1051 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001052 const coff_relocation *Reloc = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001053 DataRefImpl Sym;
David Majnemer44f51e52014-09-10 12:51:52 +00001054 ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex);
1055 if (std::error_code EC = Symb.getError())
1056 return EC;
1057 Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr());
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001058 StringRef SymName;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001059 if (std::error_code EC = getSymbolName(Sym, SymName))
1060 return EC;
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001061 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +00001062 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001063}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +00001064
Rafael Espindolac66d7612014-08-17 19:09:37 +00001065bool COFFObjectFile::isRelocatableObject() const {
1066 return !DataDirectory;
1067}
1068
Rui Ueyamac2bed422013-09-27 21:04:00 +00001069bool ImportDirectoryEntryRef::
1070operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001071 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001072}
1073
Rafael Espindola5e812af2014-01-30 02:49:50 +00001074void ImportDirectoryEntryRef::moveNext() {
1075 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001076}
1077
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001078std::error_code ImportDirectoryEntryRef::getImportTableEntry(
1079 const import_directory_table_entry *&Result) const {
Rui Ueyama1e152d52014-10-02 17:02:18 +00001080 Result = ImportTable + Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001081 return object_error::success;
1082}
1083
Rui Ueyama861021f2014-10-02 22:05:29 +00001084static imported_symbol_iterator
Rui Ueyama15d99352014-10-03 00:41:58 +00001085makeImportedSymbolIterator(const COFFObjectFile *Object,
Rui Ueyama861021f2014-10-02 22:05:29 +00001086 uintptr_t Ptr, int Index) {
Rui Ueyama15d99352014-10-03 00:41:58 +00001087 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001088 auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001089 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001090 }
1091 auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
Rui Ueyama15d99352014-10-03 00:41:58 +00001092 return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
Rui Ueyama861021f2014-10-02 22:05:29 +00001093}
1094
Rui Ueyama15d99352014-10-03 00:41:58 +00001095static imported_symbol_iterator
1096importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001097 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001098 Object->getRvaPtr(RVA, IntPtr);
1099 return makeImportedSymbolIterator(Object, IntPtr, 0);
Rui Ueyama861021f2014-10-02 22:05:29 +00001100}
1101
Rui Ueyama15d99352014-10-03 00:41:58 +00001102static imported_symbol_iterator
1103importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001104 uintptr_t IntPtr = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001105 Object->getRvaPtr(RVA, IntPtr);
Rui Ueyama861021f2014-10-02 22:05:29 +00001106 // Forward the pointer to the last entry which is null.
1107 int Index = 0;
Rui Ueyama15d99352014-10-03 00:41:58 +00001108 if (Object->getBytesInAddress() == 4) {
Rui Ueyama861021f2014-10-02 22:05:29 +00001109 auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1110 while (*Entry++)
1111 ++Index;
1112 } else {
1113 auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1114 while (*Entry++)
1115 ++Index;
1116 }
Rui Ueyama15d99352014-10-03 00:41:58 +00001117 return makeImportedSymbolIterator(Object, IntPtr, Index);
1118}
1119
1120imported_symbol_iterator
1121ImportDirectoryEntryRef::imported_symbol_begin() const {
1122 return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1123 OwningObject);
1124}
1125
1126imported_symbol_iterator
1127ImportDirectoryEntryRef::imported_symbol_end() const {
1128 return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1129 OwningObject);
Rui Ueyama861021f2014-10-02 22:05:29 +00001130}
1131
Rui Ueyama979fb402014-10-09 02:16:38 +00001132iterator_range<imported_symbol_iterator>
1133ImportDirectoryEntryRef::imported_symbols() const {
1134 return make_range(imported_symbol_begin(), imported_symbol_end());
1135}
1136
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001137std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001138 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001139 if (std::error_code EC =
Rui Ueyama1e152d52014-10-02 17:02:18 +00001140 OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001141 return EC;
1142 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001143 return object_error::success;
1144}
1145
Rui Ueyama1e152d52014-10-02 17:02:18 +00001146std::error_code
1147ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const {
1148 Result = ImportTable[Index].ImportLookupTableRVA;
1149 return object_error::success;
1150}
1151
1152std::error_code
1153ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const {
1154 Result = ImportTable[Index].ImportAddressTableRVA;
1155 return object_error::success;
1156}
1157
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001158std::error_code ImportDirectoryEntryRef::getImportLookupEntry(
Rui Ueyamac2bed422013-09-27 21:04:00 +00001159 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001160 uintptr_t IntPtr = 0;
Rui Ueyama1e152d52014-10-02 17:02:18 +00001161 uint32_t RVA = ImportTable[Index].ImportLookupTableRVA;
1162 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
Rui Ueyamaa045b732014-01-16 03:13:19 +00001163 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001164 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1165 return object_error::success;
1166}
1167
Rui Ueyama15d99352014-10-03 00:41:58 +00001168bool DelayImportDirectoryEntryRef::
1169operator==(const DelayImportDirectoryEntryRef &Other) const {
1170 return Table == Other.Table && Index == Other.Index;
1171}
1172
1173void DelayImportDirectoryEntryRef::moveNext() {
1174 ++Index;
1175}
1176
1177imported_symbol_iterator
1178DelayImportDirectoryEntryRef::imported_symbol_begin() const {
1179 return importedSymbolBegin(Table[Index].DelayImportNameTable,
1180 OwningObject);
1181}
1182
1183imported_symbol_iterator
1184DelayImportDirectoryEntryRef::imported_symbol_end() const {
1185 return importedSymbolEnd(Table[Index].DelayImportNameTable,
1186 OwningObject);
1187}
1188
Rui Ueyama979fb402014-10-09 02:16:38 +00001189iterator_range<imported_symbol_iterator>
1190DelayImportDirectoryEntryRef::imported_symbols() const {
1191 return make_range(imported_symbol_begin(), imported_symbol_end());
1192}
1193
Rui Ueyama15d99352014-10-03 00:41:58 +00001194std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1195 uintptr_t IntPtr = 0;
1196 if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1197 return EC;
1198 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1199 return object_error::success;
1200}
1201
Rui Ueyama1af08652014-10-03 18:07:18 +00001202std::error_code DelayImportDirectoryEntryRef::
1203getDelayImportTable(const delay_import_directory_table_entry *&Result) const {
1204 Result = Table;
1205 return object_error::success;
1206}
1207
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001208bool ExportDirectoryEntryRef::
1209operator==(const ExportDirectoryEntryRef &Other) const {
1210 return ExportTable == Other.ExportTable && Index == Other.Index;
1211}
1212
Rafael Espindola5e812af2014-01-30 02:49:50 +00001213void ExportDirectoryEntryRef::moveNext() {
1214 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001215}
1216
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001217// Returns the name of the current export symbol. If the symbol is exported only
1218// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001219std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001220 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001221 if (std::error_code EC =
1222 OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001223 return EC;
1224 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1225 return object_error::success;
1226}
1227
Rui Ueyamae5df6092014-01-17 22:02:24 +00001228// Returns the starting ordinal number.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001229std::error_code
1230ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
Rui Ueyamae5df6092014-01-17 22:02:24 +00001231 Result = ExportTable->OrdinalBase;
1232 return object_error::success;
1233}
1234
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001235// Returns the export ordinal of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001236std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001237 Result = ExportTable->OrdinalBase + Index;
1238 return object_error::success;
1239}
1240
1241// Returns the address of the current export symbol.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001242std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001243 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001244 if (std::error_code EC =
1245 OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001246 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001247 const export_address_table_entry *entry =
1248 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001249 Result = entry[Index].ExportRVA;
1250 return object_error::success;
1251}
1252
1253// Returns the name of the current export symbol. If the symbol is exported only
1254// by ordinal, the empty string is set as a result.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001255std::error_code
1256ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001257 uintptr_t IntPtr = 0;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001258 if (std::error_code EC =
1259 OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001260 return EC;
1261 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1262
1263 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1264 int Offset = 0;
1265 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1266 I < E; ++I, ++Offset) {
1267 if (*I != Index)
1268 continue;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001269 if (std::error_code EC =
1270 OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001271 return EC;
1272 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001273 if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001274 return EC;
1275 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1276 return object_error::success;
1277 }
1278 Result = "";
1279 return object_error::success;
1280}
1281
Rui Ueyama861021f2014-10-02 22:05:29 +00001282bool ImportedSymbolRef::
1283operator==(const ImportedSymbolRef &Other) const {
1284 return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1285 && Index == Other.Index;
1286}
1287
1288void ImportedSymbolRef::moveNext() {
1289 ++Index;
1290}
1291
1292std::error_code
1293ImportedSymbolRef::getSymbolName(StringRef &Result) const {
1294 uint32_t RVA;
1295 if (Entry32) {
1296 // If a symbol is imported only by ordinal, it has no name.
1297 if (Entry32[Index].isOrdinal())
1298 return object_error::success;
1299 RVA = Entry32[Index].getHintNameRVA();
1300 } else {
1301 if (Entry64[Index].isOrdinal())
1302 return object_error::success;
1303 RVA = Entry64[Index].getHintNameRVA();
1304 }
1305 uintptr_t IntPtr = 0;
1306 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1307 return EC;
1308 // +2 because the first two bytes is hint.
1309 Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1310 return object_error::success;
1311}
1312
1313std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1314 uint32_t RVA;
1315 if (Entry32) {
1316 if (Entry32[Index].isOrdinal()) {
1317 Result = Entry32[Index].getOrdinal();
1318 return object_error::success;
1319 }
1320 RVA = Entry32[Index].getHintNameRVA();
1321 } else {
1322 if (Entry64[Index].isOrdinal()) {
1323 Result = Entry64[Index].getOrdinal();
1324 return object_error::success;
1325 }
1326 RVA = Entry64[Index].getHintNameRVA();
1327 }
1328 uintptr_t IntPtr = 0;
1329 if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1330 return EC;
1331 Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1332 return object_error::success;
1333}
1334
Rafael Espindola437b0d52014-07-31 03:12:45 +00001335ErrorOr<std::unique_ptr<COFFObjectFile>>
Rafael Espindola48af1c22014-08-19 18:44:46 +00001336ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001337 std::error_code EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +00001338 std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
Rafael Espindola692410e2014-01-21 23:06:54 +00001339 if (EC)
1340 return EC;
Rafael Espindola437b0d52014-07-31 03:12:45 +00001341 return std::move(Ret);
Rui Ueyama686738e2014-01-16 20:30:36 +00001342}