blob: 601fd8c613eef14e23c3b3662be25baea57eafdf [file] [log] [blame]
Michael J. Spencer8e90ada2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencerec29b122011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer9da9e692012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencere5fd0042011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyamaf078eff2014-03-18 23:37:53 +000019#include "llvm/Support/COFF.h"
Rui Ueyamac2bed422013-09-27 21:04:00 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000022#include <cctype>
Nico Rieck9d2c15e2014-02-22 16:12:20 +000023#include <limits>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000024
25using namespace llvm;
26using namespace object;
27
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000028using support::ulittle8_t;
29using support::ulittle16_t;
30using support::ulittle32_t;
31using support::little16_t;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000032
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000033// Returns false if size is greater than the buffer size. And sets ec.
Rui Ueyama686738e2014-01-16 20:30:36 +000034static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +000035 if (M->getBufferSize() < Size) {
36 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.
44template<typename T>
Rui Ueyama686738e2014-01-16 20:30:36 +000045static error_code getObject(const T *&Obj, const MemoryBuffer *M,
46 const uint8_t *Ptr, const size_t Size = sizeof(T)) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +000047 uintptr_t Addr = uintptr_t(Ptr);
48 if (Addr + Size < Addr ||
49 Addr + Size < Size ||
50 Addr + Size > uintptr_t(M->getBufferEnd())) {
51 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
Rui Ueyama8ff24d22014-01-16 20:11:48 +000091const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
92 const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000093
94# ifndef NDEBUG
95 // Verify that the symbol points to a valid entry in the symbol table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +000096 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
97 if (Offset < COFFHeader->PointerToSymbolTable
98 || Offset >= COFFHeader->PointerToSymbolTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +000099 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000100 report_fatal_error("Symbol was outside of symbol table.");
101
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000102 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000103 == 0 && "Symbol did not point to the beginning of a symbol");
104# endif
105
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000106 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000107}
108
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000109const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
110 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000111
112# ifndef NDEBUG
113 // Verify that the section points to a valid entry in the section table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000114 if (Addr < SectionTable
115 || Addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000116 report_fatal_error("Section was outside of section table.");
117
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000118 uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
119 assert(Offset % sizeof(coff_section) == 0 &&
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000120 "Section did not point to the beginning of a section");
121# endif
122
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000123 return Addr;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000124}
125
Rafael Espindola5e812af2014-01-30 02:49:50 +0000126void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000127 const coff_symbol *Symb = toSymb(Ref);
128 Symb += 1 + Symb->NumberOfAuxSymbols;
129 Ref.p = reinterpret_cast<uintptr_t>(Symb);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000130}
131
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000132error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
133 StringRef &Result) const {
134 const coff_symbol *Symb = toSymb(Ref);
135 return getSymbolName(Symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000136}
137
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000138error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000139 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000140 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000141 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000142 if (error_code EC = getSection(Symb->SectionNumber, Section))
143 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000144
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000145 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000146 Result = UnknownAddressOrSize;
147 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000148 Result = Section->VirtualAddress + Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000149 else
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000150 Result = Symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000151 return object_error::success;
152}
153
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000154error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
Michael J. Spencerd3946672011-10-17 20:19:29 +0000155 SymbolRef::Type &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000156 const coff_symbol *Symb = toSymb(Ref);
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000157 Result = SymbolRef::ST_Other;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000158 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
159 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer7e4b9762012-02-29 02:11:55 +0000160 Result = SymbolRef::ST_Unknown;
David Majnemerddf28f22014-03-19 04:47:47 +0000161 } else if (Symb->isFunctionDefinition()) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000162 Result = SymbolRef::ST_Function;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000163 } else {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000164 uint32_t Characteristics = 0;
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000165 if (!COFF::isReservedSectionNumber(Symb->SectionNumber)) {
Rui Ueyama5efa6652014-01-16 20:22:55 +0000166 const coff_section *Section = NULL;
167 if (error_code EC = getSection(Symb->SectionNumber, Section))
168 return EC;
169 Characteristics = Section->Characteristics;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000170 }
Rui Ueyama5efa6652014-01-16 20:22:55 +0000171 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
172 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
173 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000174 }
175 return object_error::success;
176}
177
Rafael Espindola20122a42014-01-31 20:57:12 +0000178uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000179 const coff_symbol *Symb = toSymb(Ref);
Rafael Espindola20122a42014-01-31 20:57:12 +0000180 uint32_t Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000181
Rafael Espindola975e1152014-02-04 22:50:47 +0000182 // TODO: Correctly set SF_FormatSpecific, SF_Common
David Meyer7e4b9762012-02-29 02:11:55 +0000183
Rafael Espindola22fe9c12014-02-05 05:19:19 +0000184 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
185 if (Symb->Value == 0)
186 Result |= SymbolRef::SF_Undefined;
187 else
188 Result |= SymbolRef::SF_Common;
189 }
190
David Meyer1df4b842012-02-28 23:47:53 +0000191
192 // TODO: This are certainly too restrictive.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000193 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000194 Result |= SymbolRef::SF_Global;
195
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000196 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
David Meyer1df4b842012-02-28 23:47:53 +0000197 Result |= SymbolRef::SF_Weak;
198
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000199 if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
David Meyer1df4b842012-02-28 23:47:53 +0000200 Result |= SymbolRef::SF_Absolute;
201
Rafael Espindola20122a42014-01-31 20:57:12 +0000202 return Result;
Michael J. Spencer01759752011-10-17 23:54:22 +0000203}
204
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000205error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000206 uint64_t &Result) const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000207 // FIXME: Return the correct size. This requires looking at all the symbols
208 // in the same section as this symbol, and looking for either the next
209 // symbol, or the end of the section.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000210 const coff_symbol *Symb = toSymb(Ref);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000211 const coff_section *Section = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000212 if (error_code EC = getSection(Symb->SectionNumber, Section))
213 return EC;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000214
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000215 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000216 Result = UnknownAddressOrSize;
217 else if (Section)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000218 Result = Section->SizeOfRawData - Symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000219 else
220 Result = 0;
221 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000222}
223
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000224error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
Michael J. Spencer3217315392011-10-17 23:54:46 +0000225 section_iterator &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000226 const coff_symbol *Symb = toSymb(Ref);
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000227 if (COFF::isReservedSectionNumber(Symb->SectionNumber)) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000228 Result = section_end();
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000229 } else {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000230 const coff_section *Sec = 0;
231 if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
232 DataRefImpl Ref;
233 Ref.p = reinterpret_cast<uintptr_t>(Sec);
234 Result = section_iterator(SectionRef(Ref, this));
Michael J. Spencer3217315392011-10-17 23:54:46 +0000235 }
236 return object_error::success;
237}
238
Rafael Espindola5e812af2014-01-30 02:49:50 +0000239void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000240 const coff_section *Sec = toSec(Ref);
241 Sec += 1;
242 Ref.p = reinterpret_cast<uintptr_t>(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000243}
244
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000245error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000246 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000247 const coff_section *Sec = toSec(Ref);
248 return getSectionName(Sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000249}
250
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000251error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000252 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000253 const coff_section *Sec = toSec(Ref);
254 Result = Sec->VirtualAddress;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000255 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000256}
257
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000258error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000259 uint64_t &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000260 const coff_section *Sec = toSec(Ref);
261 Result = Sec->SizeOfRawData;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000262 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000263}
264
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000265error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000266 StringRef &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000267 const coff_section *Sec = toSec(Ref);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000268 ArrayRef<uint8_t> Res;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000269 error_code EC = getSectionContents(Sec, Res);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000270 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
271 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000272}
273
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000274error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
Michael J. Spencer79894602011-10-10 21:55:43 +0000275 uint64_t &Res) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000276 const coff_section *Sec = toSec(Ref);
277 if (!Sec)
Michael J. Spencer79894602011-10-10 21:55:43 +0000278 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000279 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
Michael J. Spencer79894602011-10-10 21:55:43 +0000280 return object_error::success;
281}
282
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000283error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000284 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000285 const coff_section *Sec = toSec(Ref);
286 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000287 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000288}
289
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000290error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000291 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000292 const coff_section *Sec = toSec(Ref);
293 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000294 return object_error::success;
295}
296
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000297error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
Michael J. Spencer800619f2011-09-28 20:57:30 +0000298 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000299 const coff_section *Sec = toSec(Ref);
300 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Michael J. Spencer800619f2011-09-28 20:57:30 +0000301 return object_error::success;
302}
303
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000304error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000305 bool &Result) const {
306 // FIXME: Unimplemented
307 Result = true;
308 return object_error::success;
309}
310
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000311error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000312 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000313 const coff_section *Sec = toSec(Ref);
314 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
Preston Gurd2138ef62012-04-12 20:13:57 +0000315 return object_error::success;
316}
317
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000318error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
Preston Gurd2138ef62012-04-12 20:13:57 +0000319 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000320 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000321 Result = false;
322 return object_error::success;
323}
324
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000325error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000326 bool &Result) const {
327 // FIXME: Unimplemented.
328 Result = false;
329 return object_error::success;
330}
331
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000332error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
333 DataRefImpl SymbRef,
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000334 bool &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000335 const coff_section *Sec = toSec(SecRef);
336 const coff_symbol *Symb = toSymb(SymbRef);
337 const coff_section *SymbSec = 0;
338 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
339 if (SymbSec == Sec)
Michael J. Spencer9a288512011-10-13 20:36:54 +0000340 Result = true;
341 else
342 Result = false;
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000343 return object_error::success;
344}
345
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000346relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
347 const coff_section *Sec = toSec(Ref);
348 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000349 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000350 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000351 } else {
352 auto begin = reinterpret_cast<const coff_relocation*>(
353 base() + Sec->PointerToRelocations);
354 if (Sec->hasExtendedRelocations()) {
355 // Skip the first relocation entry repurposed to store the number of
356 // relocations.
357 begin++;
358 }
359 Ret.p = reinterpret_cast<uintptr_t>(begin);
360 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000361 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000362}
363
Rui Ueyama827c8a22014-03-21 00:44:19 +0000364static uint32_t getNumberOfRelocations(const coff_section *Sec,
365 const uint8_t *base) {
366 // The field for the number of relocations in COFF section table is only
367 // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
368 // NumberOfRelocations field, and the actual relocation count is stored in the
369 // VirtualAddress field in the first relocation entry.
370 if (Sec->hasExtendedRelocations()) {
371 auto *FirstReloc = reinterpret_cast<const coff_relocation*>(
372 base + Sec->PointerToRelocations);
373 return FirstReloc->VirtualAddress;
374 }
375 return Sec->NumberOfRelocations;
376}
377
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000378relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
379 const coff_section *Sec = toSec(Ref);
380 DataRefImpl Ret;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000381 if (Sec->NumberOfRelocations == 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000382 Ret.p = 0;
Rui Ueyama827c8a22014-03-21 00:44:19 +0000383 } else {
384 auto begin = reinterpret_cast<const coff_relocation*>(
385 base() + Sec->PointerToRelocations);
386 uint32_t NumReloc = getNumberOfRelocations(Sec, base());
387 Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc);
388 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000389 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000390}
391
Rui Ueyamac2bed422013-09-27 21:04:00 +0000392// Initialize the pointer to the symbol table.
393error_code COFFObjectFile::initSymbolTablePtr() {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000394 if (error_code EC = getObject(
Rui Ueyamac2bed422013-09-27 21:04:00 +0000395 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
396 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000397 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000398
399 // Find string table. The first four byte of the string table contains the
400 // total size of the string table, including the size field itself. If the
401 // string table is empty, the value of the first four byte would be 4.
402 const uint8_t *StringTableAddr =
403 base() + COFFHeader->PointerToSymbolTable +
404 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
405 const ulittle32_t *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000406 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
407 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000408 StringTableSize = *StringTableSizePtr;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000409 if (error_code EC =
Rui Ueyamac2bed422013-09-27 21:04:00 +0000410 getObject(StringTable, Data, StringTableAddr, StringTableSize))
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000411 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000412
Nico Rieck773a5792014-02-26 19:51:44 +0000413 // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
414 // tools like cvtres write a size of 0 for an empty table instead of 4.
415 if (StringTableSize < 4)
416 StringTableSize = 4;
417
Rui Ueyamac2bed422013-09-27 21:04:00 +0000418 // Check that the string table is null terminated if has any in it.
Nico Rieck773a5792014-02-26 19:51:44 +0000419 if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000420 return object_error::parse_failed;
421 return object_error::success;
422}
423
Rui Ueyama215a5862014-02-20 06:51:07 +0000424// Returns the file offset for the given VA.
Rui Ueyamab7a40082014-02-20 19:14:56 +0000425error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
Rui Ueyamab6eb2642014-02-20 19:32:00 +0000426 uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase
427 : (uint64_t)PE32PlusHeader->ImageBase;
Rui Ueyamab7a40082014-02-20 19:14:56 +0000428 uint64_t Rva = Addr - ImageBase;
429 assert(Rva <= UINT32_MAX);
430 return getRvaPtr((uint32_t)Rva, Res);
Rui Ueyama215a5862014-02-20 06:51:07 +0000431}
432
Rui Ueyamac2bed422013-09-27 21:04:00 +0000433// Returns the file offset for the given RVA.
Rui Ueyama215a5862014-02-20 06:51:07 +0000434error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000435 for (const SectionRef &S : sections()) {
436 const coff_section *Section = getCOFFSection(S);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000437 uint32_t SectionStart = Section->VirtualAddress;
438 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
Rui Ueyama215a5862014-02-20 06:51:07 +0000439 if (SectionStart <= Addr && Addr < SectionEnd) {
440 uint32_t Offset = Addr - SectionStart;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000441 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
442 return object_error::success;
443 }
444 }
445 return object_error::parse_failed;
446}
447
448// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
449// table entry.
450error_code COFFObjectFile::
451getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
452 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000453 if (error_code EC = getRvaPtr(Rva, IntPtr))
454 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000455 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
456 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
457 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
458 return object_error::success;
459}
460
461// Find the import table.
462error_code COFFObjectFile::initImportTablePtr() {
463 // First, we get the RVA of the import table. If the file lacks a pointer to
464 // the import table, do nothing.
465 const data_directory *DataEntry;
466 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
467 return object_error::success;
468
469 // Do nothing if the pointer to import table is NULL.
470 if (DataEntry->RelativeVirtualAddress == 0)
471 return object_error::success;
472
473 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
474 NumberOfImportDirectory = DataEntry->Size /
475 sizeof(import_directory_table_entry);
476
477 // Find the section that contains the RVA. This is needed because the RVA is
478 // the import table's memory address which is different from its file offset.
479 uintptr_t IntPtr = 0;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000480 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
481 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000482 ImportDirectory = reinterpret_cast<
483 const import_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000484 return object_error::success;
485}
Rui Ueyamac2bed422013-09-27 21:04:00 +0000486
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000487// Find the export table.
488error_code COFFObjectFile::initExportTablePtr() {
489 // First, we get the RVA of the export table. If the file lacks a pointer to
490 // the export table, do nothing.
491 const data_directory *DataEntry;
492 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
493 return object_error::success;
494
495 // Do nothing if the pointer to export table is NULL.
496 if (DataEntry->RelativeVirtualAddress == 0)
497 return object_error::success;
498
499 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
500 uintptr_t IntPtr = 0;
501 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
502 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000503 ExportDirectory =
504 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000505 return object_error::success;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000506}
507
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000508COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
509 bool BufferOwned)
510 : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0),
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000511 PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0),
512 SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0),
Rafael Espindolaafcc3df2014-01-24 21:32:21 +0000513 NumberOfImportDirectory(0), ExportDirectory(0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000514 // Check that we at least have enough room for a header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000515 if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000516
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000517 // The current location in the file where we are looking at.
518 uint64_t CurPtr = 0;
519
520 // PE header is optional and is present only in executables. If it exists,
521 // it is placed right after COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000522 bool HasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000523
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000524 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000525 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000526 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
527 // PE signature to find 'normal' COFF header.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000528 if (!checkSize(Data, EC, 0x3c + 8)) return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000529 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
530 // Check the PE magic bytes. ("PE\0\0")
531 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000532 EC = object_error::parse_failed;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000533 return;
534 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000535 CurPtr += 4; // Skip the PE magic bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000536 HasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000537 }
538
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000539 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000540 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000541 CurPtr += sizeof(coff_file_header);
542
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000543 if (HasPEHeader) {
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000544 const pe32_header *Header;
545 if ((EC = getObject(Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000546 return;
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000547
548 const uint8_t *DataDirAddr;
549 uint64_t DataDirSize;
550 if (Header->Magic == 0x10b) {
551 PE32Header = Header;
552 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
553 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
554 } else if (Header->Magic == 0x20b) {
555 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
556 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
557 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
558 } else {
559 // It's neither PE32 nor PE32+.
560 EC = object_error::parse_failed;
561 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000562 }
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000563 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
564 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000565 CurPtr += COFFHeader->SizeOfOptionalHeader;
566 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000567
Rafael Espindola692410e2014-01-21 23:06:54 +0000568 if (COFFHeader->isImportLibrary())
569 return;
570
571 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
572 COFFHeader->NumberOfSections * sizeof(coff_section))))
573 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000574
Rui Ueyamac2bed422013-09-27 21:04:00 +0000575 // Initialize the pointer to the symbol table.
576 if (COFFHeader->PointerToSymbolTable != 0)
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000577 if ((EC = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000578 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000579
Rui Ueyamac2bed422013-09-27 21:04:00 +0000580 // Initialize the pointer to the beginning of the import table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000581 if ((EC = initImportTablePtr()))
Rui Ueyamac2bed422013-09-27 21:04:00 +0000582 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000583
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000584 // Initialize the pointer to the export table.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000585 if ((EC = initExportTablePtr()))
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000586 return;
587
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000588 EC = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000589}
590
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000591basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000592 DataRefImpl Ret;
593 Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000594 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000595}
596
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000597basic_symbol_iterator COFFObjectFile::symbol_end_impl() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000598 // The symbol table ends where the string table begins.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000599 DataRefImpl Ret;
600 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000601 return basic_symbol_iterator(SymbolRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000602}
603
Rafael Espindolab5155a52014-02-10 20:24:04 +0000604library_iterator COFFObjectFile::needed_library_begin() const {
David Meyer2fc34c52012-03-01 01:36:50 +0000605 // TODO: implement
606 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
607}
608
Rafael Espindolab5155a52014-02-10 20:24:04 +0000609library_iterator COFFObjectFile::needed_library_end() const {
David Meyer2fc34c52012-03-01 01:36:50 +0000610 // TODO: implement
611 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
612}
613
David Meyerc429b802012-03-01 22:19:54 +0000614StringRef COFFObjectFile::getLoadName() const {
615 // COFF does not have this field.
616 return "";
617}
618
Rui Ueyamabc654b12013-09-27 21:47:05 +0000619import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000620 return import_directory_iterator(
621 ImportDirectoryEntryRef(ImportDirectory, 0, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000622}
623
Rui Ueyamabc654b12013-09-27 21:47:05 +0000624import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000625 return import_directory_iterator(
626 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
Rui Ueyamac2bed422013-09-27 21:04:00 +0000627}
David Meyerc429b802012-03-01 22:19:54 +0000628
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000629export_directory_iterator COFFObjectFile::export_directory_begin() const {
630 return export_directory_iterator(
631 ExportDirectoryEntryRef(ExportDirectory, 0, this));
632}
633
634export_directory_iterator COFFObjectFile::export_directory_end() const {
635 if (ExportDirectory == 0)
636 return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000637 ExportDirectoryEntryRef Ref(ExportDirectory,
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000638 ExportDirectory->AddressTableEntries, this);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000639 return export_directory_iterator(Ref);
Rui Ueyamaad882ba2014-01-16 07:05:49 +0000640}
641
Rafael Espindolab5155a52014-02-10 20:24:04 +0000642section_iterator COFFObjectFile::section_begin() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000643 DataRefImpl Ret;
644 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
645 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000646}
647
Rafael Espindolab5155a52014-02-10 20:24:04 +0000648section_iterator COFFObjectFile::section_end() const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000649 DataRefImpl Ret;
650 int NumSections = COFFHeader->isImportLibrary()
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000651 ? 0 : COFFHeader->NumberOfSections;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000652 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
653 return section_iterator(SectionRef(Ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000654}
655
656uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000657 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000658}
659
660StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000661 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000662 case COFF::IMAGE_FILE_MACHINE_I386:
663 return "COFF-i386";
664 case COFF::IMAGE_FILE_MACHINE_AMD64:
665 return "COFF-x86-64";
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000666 case COFF::IMAGE_FILE_MACHINE_ARMNT:
667 return "COFF-ARM";
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000668 default:
669 return "COFF-<unknown arch>";
670 }
671}
672
673unsigned COFFObjectFile::getArch() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000674 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000675 case COFF::IMAGE_FILE_MACHINE_I386:
676 return Triple::x86;
677 case COFF::IMAGE_FILE_MACHINE_AMD64:
678 return Triple::x86_64;
Saleem Abdulrasool9b7c0af2014-03-13 07:02:35 +0000679 case COFF::IMAGE_FILE_MACHINE_ARMNT:
680 return Triple::thumb;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000681 default:
682 return Triple::UnknownArch;
683 }
684}
685
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000686// This method is kept here because lld uses this. As soon as we make
687// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000688error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000689 return getCOFFHeader(Res);
690}
691
692error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
693 Res = COFFHeader;
694 return object_error::success;
695}
696
697error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
698 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000699 return object_error::success;
700}
701
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000702error_code
703COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
704 Res = PE32PlusHeader;
705 return object_error::success;
706}
707
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000708error_code COFFObjectFile::getDataDirectory(uint32_t Index,
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000709 const data_directory *&Res) const {
710 // Error if if there's no data directory or the index is out of range.
Rui Ueyama10ed9dd2014-01-26 04:15:52 +0000711 if (!DataDirectory)
712 return object_error::parse_failed;
713 assert(PE32Header || PE32PlusHeader);
714 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
715 : PE32PlusHeader->NumberOfRvaAndSize;
716 if (Index > NumEnt)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000717 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000718 Res = &DataDirectory[Index];
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000719 return object_error::success;
720}
721
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000722error_code COFFObjectFile::getSection(int32_t Index,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000723 const coff_section *&Result) const {
724 // Check for special index values.
Rui Ueyamaf078eff2014-03-18 23:37:53 +0000725 if (COFF::isReservedSectionNumber(Index))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000726 Result = NULL;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000727 else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000728 // We already verified the section table data, so no need to check again.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000729 Result = SectionTable + (Index - 1);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000730 else
731 return object_error::parse_failed;
732 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000733}
734
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000735error_code COFFObjectFile::getString(uint32_t Offset,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000736 StringRef &Result) const {
737 if (StringTableSize <= 4)
738 // Tried to get a string from an empty string table.
739 return object_error::parse_failed;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000740 if (Offset >= StringTableSize)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000741 return object_error::unexpected_eof;
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000742 Result = StringRef(StringTable + Offset);
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000743 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000744}
745
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000746error_code COFFObjectFile::getSymbol(uint32_t Index,
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000747 const coff_symbol *&Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000748 if (Index < COFFHeader->NumberOfSymbols)
749 Result = SymbolTable + Index;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000750 else
751 return object_error::parse_failed;
752 return object_error::success;
753}
754
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000755error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000756 StringRef &Res) const {
757 // Check for string table entry. First 4 bytes are 0.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000758 if (Symbol->Name.Offset.Zeroes == 0) {
759 uint32_t Offset = Symbol->Name.Offset.Offset;
760 if (error_code EC = getString(Offset, Res))
761 return EC;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000762 return object_error::success;
763 }
764
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000765 if (Symbol->Name.ShortName[7] == 0)
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000766 // Null terminated, let ::strlen figure out the length.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000767 Res = StringRef(Symbol->Name.ShortName);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000768 else
769 // Not null terminated, use all 8 bytes.
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000770 Res = StringRef(Symbol->Name.ShortName, 8);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000771 return object_error::success;
772}
773
Marshall Clow71757ef2012-06-15 01:08:25 +0000774ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000775 const coff_symbol *Symbol) const {
776 const uint8_t *Aux = NULL;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000777
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000778 if (Symbol->NumberOfAuxSymbols > 0) {
Marshall Clow71757ef2012-06-15 01:08:25 +0000779 // AUX data comes immediately after the symbol in COFF
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000780 Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
Marshall Clow71757ef2012-06-15 01:08:25 +0000781# ifndef NDEBUG
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000782 // Verify that the Aux symbol points to a valid entry in the symbol table.
783 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
784 if (Offset < COFFHeader->PointerToSymbolTable
785 || Offset >= COFFHeader->PointerToSymbolTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000786 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clow71757ef2012-06-15 01:08:25 +0000787 report_fatal_error("Aux Symbol data was outside of symbol table.");
788
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000789 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clow71757ef2012-06-15 01:08:25 +0000790 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000791# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000792 }
Rui Ueyama24fc2d62014-01-17 22:11:27 +0000793 return ArrayRef<uint8_t>(Aux,
794 Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
Marshall Clow71757ef2012-06-15 01:08:25 +0000795}
796
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000797error_code COFFObjectFile::getSectionName(const coff_section *Sec,
798 StringRef &Res) const {
799 StringRef Name;
800 if (Sec->Name[7] == 0)
801 // Null terminated, let ::strlen figure out the length.
802 Name = Sec->Name;
803 else
804 // Not null terminated, use all 8 bytes.
805 Name = StringRef(Sec->Name, 8);
806
807 // Check for string table entry. First byte is '/'.
808 if (Name[0] == '/') {
809 uint32_t Offset;
Nico Rieck9d2c15e2014-02-22 16:12:20 +0000810 if (Name[1] == '/') {
811 if (decodeBase64StringEntry(Name.substr(2), Offset))
812 return object_error::parse_failed;
813 } else {
814 if (Name.substr(1).getAsInteger(10, Offset))
815 return object_error::parse_failed;
816 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000817 if (error_code EC = getString(Offset, Name))
818 return EC;
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000819 }
820
821 Res = Name;
822 return object_error::success;
823}
824
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000825error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
826 ArrayRef<uint8_t> &Res) const {
827 // The only thing that we need to verify is that the contents is contained
828 // within the file bounds. We don't need to make sure it doesn't cover other
829 // data, as there's nothing that says that is not allowed.
830 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
831 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
832 if (ConEnd > uintptr_t(Data->getBufferEnd()))
833 return object_error::parse_failed;
834 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
835 Sec->SizeOfRawData);
836 return object_error::success;
837}
838
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000839const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000840 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000841}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000842
Rafael Espindola5e812af2014-01-30 02:49:50 +0000843void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000844 Rel.p = reinterpret_cast<uintptr_t>(
845 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000846}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000847
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000848error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
849 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000850 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000851}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000852
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000853error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
854 uint64_t &Res) const {
855 Res = toRel(Rel)->VirtualAddress;
856 return object_error::success;
857}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000858
Rafael Espindola806f0062013-06-05 01:33:53 +0000859symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000860 const coff_relocation* R = toRel(Rel);
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000861 DataRefImpl Ref;
862 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
863 return symbol_iterator(SymbolRef(Ref, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000864}
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000865
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000866error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson7be76592011-10-26 17:08:49 +0000867 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000868 const coff_relocation* R = toRel(Rel);
869 Res = R->Type;
870 return object_error::success;
871}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000872
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000873const coff_section *
874COFFObjectFile::getCOFFSection(const SectionRef &Section) const {
875 return toSec(Section.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000876}
877
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000878const coff_symbol *
879COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
880 return toSymb(Symbol.getRawDataRefImpl());
Marshall Clow71757ef2012-06-15 01:08:25 +0000881}
882
Rafael Espindolaf12b8282014-02-21 20:10:59 +0000883const coff_relocation *
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000884COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const {
885 return toRel(Reloc.getRawDataRefImpl());
Marshall Clowd3e2a762012-06-18 19:47:16 +0000886}
887
Alexey Samsonov27dc8392014-03-18 06:53:02 +0000888#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
889 case COFF::reloc_type: \
890 Res = #reloc_type; \
891 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000892
893error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
894 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000895 const coff_relocation *Reloc = toRel(Rel);
896 StringRef Res;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000897 switch (COFFHeader->Machine) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000898 case COFF::IMAGE_FILE_MACHINE_AMD64:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000899 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000900 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
901 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
902 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
903 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
904 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
905 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
906 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
907 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
908 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
909 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
910 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
911 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
912 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
913 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
914 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
915 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
916 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
917 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000918 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000919 }
920 break;
Saleem Abdulrasool5c503bf2014-04-09 06:18:28 +0000921 case COFF::IMAGE_FILE_MACHINE_ARMNT:
922 switch (Reloc->Type) {
923 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE);
924 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32);
925 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB);
926 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24);
927 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11);
928 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN);
929 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24);
930 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11);
931 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION);
932 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL);
933 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A);
934 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T);
935 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T);
936 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T);
937 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T);
938 default:
939 Res = "Unknown";
940 }
941 break;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000942 case COFF::IMAGE_FILE_MACHINE_I386:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000943 switch (Reloc->Type) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000944 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
945 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
946 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
947 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
948 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
949 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
950 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
951 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
952 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
953 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
954 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
955 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000956 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000957 }
958 break;
959 default:
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000960 Res = "Unknown";
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000961 }
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000962 Result.append(Res.begin(), Res.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000963 return object_error::success;
964}
965
966#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
967
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000968error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
969 SmallVectorImpl<char> &Result) const {
Rui Ueyama8ff24d22014-01-16 20:11:48 +0000970 const coff_relocation *Reloc = toRel(Rel);
971 const coff_symbol *Symb = 0;
972 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
973 DataRefImpl Sym;
974 Sym.p = reinterpret_cast<uintptr_t>(Symb);
975 StringRef SymName;
976 if (error_code EC = getSymbolName(Sym, SymName)) return EC;
977 Result.append(SymName.begin(), SymName.end());
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000978 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000979}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000980
David Meyer2fc34c52012-03-01 01:36:50 +0000981error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
982 LibraryRef &Result) const {
983 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
984}
985
986error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
987 StringRef &Result) const {
988 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
989}
990
Rui Ueyamac2bed422013-09-27 21:04:00 +0000991bool ImportDirectoryEntryRef::
992operator==(const ImportDirectoryEntryRef &Other) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +0000993 return ImportTable == Other.ImportTable && Index == Other.Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000994}
995
Rafael Espindola5e812af2014-01-30 02:49:50 +0000996void ImportDirectoryEntryRef::moveNext() {
997 ++Index;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000998}
999
1000error_code ImportDirectoryEntryRef::
1001getImportTableEntry(const import_directory_table_entry *&Result) const {
Rui Ueyamaa045b732014-01-16 03:13:19 +00001002 Result = ImportTable;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001003 return object_error::success;
1004}
1005
1006error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001007 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +00001008 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
1009 return EC;
1010 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
Rui Ueyamac2bed422013-09-27 21:04:00 +00001011 return object_error::success;
1012}
1013
1014error_code ImportDirectoryEntryRef::getImportLookupEntry(
1015 const import_lookup_table_entry32 *&Result) const {
Rui Ueyamac2bed422013-09-27 21:04:00 +00001016 uintptr_t IntPtr = 0;
Rui Ueyamaa045b732014-01-16 03:13:19 +00001017 if (error_code EC =
1018 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
1019 return EC;
Rui Ueyamac2bed422013-09-27 21:04:00 +00001020 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
1021 return object_error::success;
1022}
1023
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001024bool ExportDirectoryEntryRef::
1025operator==(const ExportDirectoryEntryRef &Other) const {
1026 return ExportTable == Other.ExportTable && Index == Other.Index;
1027}
1028
Rafael Espindola5e812af2014-01-30 02:49:50 +00001029void ExportDirectoryEntryRef::moveNext() {
1030 ++Index;
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001031}
1032
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001033// Returns the name of the current export symbol. If the symbol is exported only
1034// by ordinal, the empty string is set as a result.
1035error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1036 uintptr_t IntPtr = 0;
1037 if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1038 return EC;
1039 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1040 return object_error::success;
1041}
1042
Rui Ueyamae5df6092014-01-17 22:02:24 +00001043// Returns the starting ordinal number.
1044error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
1045 Result = ExportTable->OrdinalBase;
1046 return object_error::success;
1047}
1048
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001049// Returns the export ordinal of the current export symbol.
1050error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1051 Result = ExportTable->OrdinalBase + Index;
1052 return object_error::success;
1053}
1054
1055// Returns the address of the current export symbol.
1056error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1057 uintptr_t IntPtr = 0;
1058 if (error_code EC = OwningObject->getRvaPtr(
1059 ExportTable->ExportAddressTableRVA, IntPtr))
1060 return EC;
Rui Ueyama24fc2d62014-01-17 22:11:27 +00001061 const export_address_table_entry *entry =
1062 reinterpret_cast<const export_address_table_entry *>(IntPtr);
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001063 Result = entry[Index].ExportRVA;
1064 return object_error::success;
1065}
1066
1067// Returns the name of the current export symbol. If the symbol is exported only
1068// by ordinal, the empty string is set as a result.
Rui Ueyamada49d0d2014-01-16 20:50:34 +00001069error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
Rui Ueyamaad882ba2014-01-16 07:05:49 +00001070 uintptr_t IntPtr = 0;
1071 if (error_code EC = OwningObject->getRvaPtr(
1072 ExportTable->OrdinalTableRVA, IntPtr))
1073 return EC;
1074 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1075
1076 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1077 int Offset = 0;
1078 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1079 I < E; ++I, ++Offset) {
1080 if (*I != Index)
1081 continue;
1082 if (error_code EC = OwningObject->getRvaPtr(
1083 ExportTable->NamePointerRVA, IntPtr))
1084 return EC;
1085 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1086 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1087 return EC;
1088 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1089 return object_error::success;
1090 }
1091 Result = "";
1092 return object_error::success;
1093}
1094
Rafael Espindolaafcc3df2014-01-24 21:32:21 +00001095ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
1096 bool BufferOwned) {
Rui Ueyama8ff24d22014-01-16 20:11:48 +00001097 error_code EC;
Ahmed Charles56440fd2014-03-06 05:51:42 +00001098 std::unique_ptr<COFFObjectFile> Ret(
1099 new COFFObjectFile(Object, EC, BufferOwned));
Rafael Espindola692410e2014-01-21 23:06:54 +00001100 if (EC)
1101 return EC;
Ahmed Charles96c9d952014-03-05 10:19:29 +00001102 return Ret.release();
Rui Ueyama686738e2014-01-16 20:30:36 +00001103}