blob: 42066c372b43b5077a5de4c1099735ac3770f453 [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 Ueyamac2bed422013-09-27 21:04:00 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000021#include <cctype>
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000022
23using namespace llvm;
24using namespace object;
25
26namespace {
27using support::ulittle8_t;
28using support::ulittle16_t;
29using support::ulittle32_t;
30using support::little16_t;
31}
32
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000033namespace {
34// Returns false if size is greater than the buffer size. And sets ec.
35bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
36 if (m->getBufferSize() < size) {
37 ec = object_error::unexpected_eof;
38 return false;
39 }
40 return true;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +000041}
42
Rui Ueyamaed64342b2013-07-19 23:23:29 +000043// Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
44// Returns unexpected_eof if error.
45template<typename T>
46error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
47 const size_t Size = sizeof(T)) {
48 uintptr_t Addr = uintptr_t(Ptr);
49 if (Addr + Size < Addr ||
50 Addr + Size < Size ||
51 Addr + Size > uintptr_t(M->getBufferEnd())) {
52 return object_error::unexpected_eof;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000053 }
Rui Ueyamaed64342b2013-07-19 23:23:29 +000054 Obj = reinterpret_cast<const T *>(Addr);
55 return object_error::success;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000056}
57}
58
59const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
60 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
61
62# ifndef NDEBUG
63 // Verify that the symbol points to a valid entry in the symbol table.
64 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
Rui Ueyama82ebd8e2013-06-12 19:10:33 +000065 if (offset < COFFHeader->PointerToSymbolTable
66 || offset >= COFFHeader->PointerToSymbolTable
67 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000068 report_fatal_error("Symbol was outside of symbol table.");
69
Rui Ueyama82ebd8e2013-06-12 19:10:33 +000070 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000071 == 0 && "Symbol did not point to the beginning of a symbol");
72# endif
73
74 return addr;
75}
76
77const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
78 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
79
80# ifndef NDEBUG
81 // Verify that the section points to a valid entry in the section table.
82 if (addr < SectionTable
Rui Ueyama82ebd8e2013-06-12 19:10:33 +000083 || addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +000084 report_fatal_error("Section was outside of section table.");
85
86 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
87 assert(offset % sizeof(coff_section) == 0 &&
88 "Section did not point to the beginning of a section");
89# endif
90
91 return addr;
92}
93
94error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
95 SymbolRef &Result) const {
96 const coff_symbol *symb = toSymb(Symb);
97 symb += 1 + symb->NumberOfAuxSymbols;
98 Symb.p = reinterpret_cast<uintptr_t>(symb);
99 Result = SymbolRef(Symb, this);
100 return object_error::success;
101}
102
103 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
104 StringRef &Result) const {
105 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000106 return getSymbolName(symb, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000107}
108
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000109error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000110 uint64_t &Result) const {
111 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000112 const coff_section *Section = NULL;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000113 if (error_code ec = getSection(symb->SectionNumber, Section))
114 return ec;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000115
116 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000117 Result = UnknownAddressOrSize;
118 else if (Section)
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000119 Result = Section->PointerToRawData + symb->Value;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000120 else
121 Result = symb->Value;
122 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000123}
124
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000125error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
126 uint64_t &Result) const {
127 const coff_symbol *symb = toSymb(Symb);
128 const coff_section *Section = NULL;
129 if (error_code ec = getSection(symb->SectionNumber, Section))
130 return ec;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000131
132 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000133 Result = UnknownAddressOrSize;
134 else if (Section)
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000135 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000136 else
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000137 Result = symb->Value;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000138 return object_error::success;
139}
140
141error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencerd3946672011-10-17 20:19:29 +0000142 SymbolRef::Type &Result) const {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000143 const coff_symbol *symb = toSymb(Symb);
144 Result = SymbolRef::ST_Other;
145 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
146 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer7e4b9762012-02-29 02:11:55 +0000147 Result = SymbolRef::ST_Unknown;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000148 } else {
Michael J. Spencer097be9f2011-10-18 19:31:59 +0000149 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000150 Result = SymbolRef::ST_Function;
151 } else {
Rafael Espindola06adfac2013-11-02 20:10:07 +0000152 uint32_t Characteristics = 0;
153 if (symb->SectionNumber > 0) {
154 const coff_section *Section = NULL;
155 if (error_code ec = getSection(symb->SectionNumber, Section))
156 return ec;
157 Characteristics = Section->Characteristics;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000158 }
Rafael Espindola06adfac2013-11-02 20:10:07 +0000159 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
160 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
161 Result = SymbolRef::ST_Data;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000162 }
163 }
164 return object_error::success;
165}
166
David Meyer1df4b842012-02-28 23:47:53 +0000167error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
168 uint32_t &Result) const {
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000169 const coff_symbol *symb = toSymb(Symb);
David Meyer1df4b842012-02-28 23:47:53 +0000170 Result = SymbolRef::SF_None;
Benjamin Kramer75d1cf332011-09-14 01:22:52 +0000171
David Meyer7e4b9762012-02-29 02:11:55 +0000172 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
173
174 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
175 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
176 Result |= SymbolRef::SF_Undefined;
David Meyer1df4b842012-02-28 23:47:53 +0000177
178 // TODO: This are certainly too restrictive.
179 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
180 Result |= SymbolRef::SF_Global;
181
182 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
183 Result |= SymbolRef::SF_Weak;
184
185 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
186 Result |= SymbolRef::SF_Absolute;
187
Michael J. Spencer01759752011-10-17 23:54:22 +0000188 return object_error::success;
189}
190
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000191error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
192 uint64_t &Result) const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000193 // FIXME: Return the correct size. This requires looking at all the symbols
194 // in the same section as this symbol, and looking for either the next
195 // symbol, or the end of the section.
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000196 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer5ebaed22011-07-05 14:48:59 +0000197 const coff_section *Section = NULL;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000198 if (error_code ec = getSection(symb->SectionNumber, Section))
199 return ec;
Rafael Espindolae62ab112013-11-02 18:07:48 +0000200
201 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000202 Result = UnknownAddressOrSize;
203 else if (Section)
204 Result = Section->SizeOfRawData - symb->Value;
205 else
206 Result = 0;
207 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000208}
209
Michael J. Spencer3217315392011-10-17 23:54:46 +0000210error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
211 section_iterator &Result) const {
212 const coff_symbol *symb = toSymb(Symb);
213 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
214 Result = end_sections();
215 else {
Daniel Dunbar9339d452011-11-28 22:19:32 +0000216 const coff_section *sec = 0;
Michael J. Spencer3217315392011-10-17 23:54:46 +0000217 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
218 DataRefImpl Sec;
219 Sec.p = reinterpret_cast<uintptr_t>(sec);
220 Result = section_iterator(SectionRef(Sec, this));
221 }
222 return object_error::success;
223}
224
Tim Northover4f223bf72012-10-29 10:47:00 +0000225error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
226 uint64_t &Val) const {
227 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
228}
229
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000230error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
231 SectionRef &Result) const {
232 const coff_section *sec = toSec(Sec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000233 sec += 1;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000234 Sec.p = reinterpret_cast<uintptr_t>(sec);
235 Result = SectionRef(Sec, this);
236 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000237}
238
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000239error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
240 StringRef &Result) const {
241 const coff_section *sec = toSec(Sec);
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000242 return getSectionName(sec, Result);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000243}
244
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000245error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
246 uint64_t &Result) const {
247 const coff_section *sec = toSec(Sec);
248 Result = sec->VirtualAddress;
249 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000250}
251
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000252error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
253 uint64_t &Result) const {
254 const coff_section *sec = toSec(Sec);
255 Result = sec->SizeOfRawData;
256 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000257}
258
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000259error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
260 StringRef &Result) const {
261 const coff_section *sec = toSec(Sec);
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000262 ArrayRef<uint8_t> Res;
263 error_code EC = getSectionContents(sec, Res);
264 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
265 return EC;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000266}
267
Michael J. Spencer79894602011-10-10 21:55:43 +0000268error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
269 uint64_t &Res) const {
270 const coff_section *sec = toSec(Sec);
271 if (!sec)
272 return object_error::parse_failed;
273 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
274 return object_error::success;
275}
276
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000277error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
278 bool &Result) const {
279 const coff_section *sec = toSec(Sec);
280 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
281 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000282}
283
Michael J. Spencer800619f2011-09-28 20:57:30 +0000284error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
285 bool &Result) const {
286 const coff_section *sec = toSec(Sec);
287 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
288 return object_error::success;
289}
290
291error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
292 bool &Result) const {
293 const coff_section *sec = toSec(Sec);
294 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
295 return object_error::success;
296}
297
Preston Gurd2138ef62012-04-12 20:13:57 +0000298error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
299 bool &Result) const {
300 // FIXME: Unimplemented
301 Result = true;
302 return object_error::success;
303}
304
305error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
306 bool &Result) const {
307 const coff_section *sec = toSec(Sec);
308 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
309 return object_error::success;
310}
311
312error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
313 bool &Result) const {
Andrew Kaylorb96a3202012-10-10 01:45:52 +0000314 // FIXME: Unimplemented.
Preston Gurd2138ef62012-04-12 20:13:57 +0000315 Result = false;
316 return object_error::success;
317}
318
Andrew Kaylor3f31fa02012-10-10 01:41:33 +0000319error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
320 bool &Result) const {
321 // FIXME: Unimplemented.
322 Result = false;
323 return object_error::success;
324}
325
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000326error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
327 DataRefImpl Symb,
328 bool &Result) const {
Michael J. Spencer9a288512011-10-13 20:36:54 +0000329 const coff_section *sec = toSec(Sec);
330 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbar9339d452011-11-28 22:19:32 +0000331 const coff_section *symb_sec = 0;
Michael J. Spencer9a288512011-10-13 20:36:54 +0000332 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
333 if (symb_sec == sec)
334 Result = true;
335 else
336 Result = false;
Benjamin Kramerf6f3e812011-07-15 18:39:21 +0000337 return object_error::success;
338}
339
Rui Ueyamabc654b12013-09-27 21:47:05 +0000340relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000341 const coff_section *sec = toSec(Sec);
342 DataRefImpl ret;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000343 if (sec->NumberOfRelocations == 0)
344 ret.p = 0;
345 else
346 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
347
348 return relocation_iterator(RelocationRef(ret, this));
349}
350
Rui Ueyamabc654b12013-09-27 21:47:05 +0000351relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Sec) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000352 const coff_section *sec = toSec(Sec);
353 DataRefImpl ret;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000354 if (sec->NumberOfRelocations == 0)
355 ret.p = 0;
356 else
357 ret.p = reinterpret_cast<uintptr_t>(
358 reinterpret_cast<const coff_relocation*>(
359 base() + sec->PointerToRelocations)
360 + sec->NumberOfRelocations);
361
362 return relocation_iterator(RelocationRef(ret, this));
363}
364
Rui Ueyamac2bed422013-09-27 21:04:00 +0000365// Initialize the pointer to the symbol table.
366error_code COFFObjectFile::initSymbolTablePtr() {
367 if (error_code ec = getObject(
368 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
369 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
370 return ec;
371
372 // Find string table. The first four byte of the string table contains the
373 // total size of the string table, including the size field itself. If the
374 // string table is empty, the value of the first four byte would be 4.
375 const uint8_t *StringTableAddr =
376 base() + COFFHeader->PointerToSymbolTable +
377 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
378 const ulittle32_t *StringTableSizePtr;
379 if (error_code ec = getObject(StringTableSizePtr, Data, StringTableAddr))
380 return ec;
381 StringTableSize = *StringTableSizePtr;
382 if (error_code ec =
383 getObject(StringTable, Data, StringTableAddr, StringTableSize))
384 return ec;
385
386 // Check that the string table is null terminated if has any in it.
387 if (StringTableSize < 4 ||
388 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
389 return object_error::parse_failed;
390 return object_error::success;
391}
392
393// Returns the file offset for the given RVA.
394error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
395 error_code ec;
396 for (section_iterator i = begin_sections(), e = end_sections(); i != e;
397 i.increment(ec)) {
398 if (ec)
399 return ec;
400 const coff_section *Section = getCOFFSection(i);
401 uint32_t SectionStart = Section->VirtualAddress;
402 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
403 if (SectionStart <= Rva && Rva < SectionEnd) {
404 uint32_t Offset = Rva - SectionStart;
405 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
406 return object_error::success;
407 }
408 }
409 return object_error::parse_failed;
410}
411
412// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
413// table entry.
414error_code COFFObjectFile::
415getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
416 uintptr_t IntPtr = 0;
417 if (error_code ec = getRvaPtr(Rva, IntPtr))
418 return ec;
419 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
420 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
421 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
422 return object_error::success;
423}
424
425// Find the import table.
426error_code COFFObjectFile::initImportTablePtr() {
427 // First, we get the RVA of the import table. If the file lacks a pointer to
428 // the import table, do nothing.
429 const data_directory *DataEntry;
430 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
431 return object_error::success;
432
433 // Do nothing if the pointer to import table is NULL.
434 if (DataEntry->RelativeVirtualAddress == 0)
435 return object_error::success;
436
437 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
438 NumberOfImportDirectory = DataEntry->Size /
439 sizeof(import_directory_table_entry);
440
441 // Find the section that contains the RVA. This is needed because the RVA is
442 // the import table's memory address which is different from its file offset.
443 uintptr_t IntPtr = 0;
444 if (error_code ec = getRvaPtr(ImportTableRva, IntPtr))
445 return ec;
446 ImportDirectory = reinterpret_cast<
447 const import_directory_table_entry *>(IntPtr);
448
449 // It's an error if there's no section containing the Import Table RVA.
450 return object_error::parse_failed;
451}
452
Michael J. Spencerec29b122011-06-25 17:54:50 +0000453COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
Rafael Espindola717c4d42013-04-07 16:40:00 +0000454 : ObjectFile(Binary::ID_COFF, Object)
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000455 , COFFHeader(0)
456 , PE32Header(0)
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000457 , DataDirectory(0)
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000458 , SectionTable(0)
459 , SymbolTable(0)
460 , StringTable(0)
Rui Ueyamac2bed422013-09-27 21:04:00 +0000461 , StringTableSize(0)
462 , ImportDirectory(0)
463 , NumberOfImportDirectory(0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000464 // Check that we at least have enough room for a header.
465 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopheree066fc2011-04-03 22:53:19 +0000466
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000467 // The current location in the file where we are looking at.
468 uint64_t CurPtr = 0;
469
470 // PE header is optional and is present only in executables. If it exists,
471 // it is placed right after COFF header.
472 bool hasPEHeader = false;
Eric Christopheree066fc2011-04-03 22:53:19 +0000473
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000474 // Check if this is a PE/COFF file.
Michael J. Spencerec29b122011-06-25 17:54:50 +0000475 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopheree066fc2011-04-03 22:53:19 +0000476 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
477 // PE signature to find 'normal' COFF header.
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000478 if (!checkSize(Data, ec, 0x3c + 8)) return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000479 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
480 // Check the PE magic bytes. ("PE\0\0")
481 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000482 ec = object_error::parse_failed;
483 return;
484 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000485 CurPtr += 4; // Skip the PE magic bytes.
486 hasPEHeader = true;
Eric Christopheree066fc2011-04-03 22:53:19 +0000487 }
488
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000489 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000490 return;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000491 CurPtr += sizeof(coff_file_header);
492
493 if (hasPEHeader) {
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000494 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000495 return;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000496 if (PE32Header->Magic != 0x10b) {
497 // We only support PE32. If this is PE32 (not PE32+), the magic byte
498 // should be 0x10b. If this is not PE32, continue as if there's no PE
499 // header in this file.
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000500 PE32Header = 0;
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000501 } else if (PE32Header->NumberOfRvaAndSize > 0) {
502 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
503 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
504 if ((ec = getObject(DataDirectory, Data, addr, size)))
505 return;
506 }
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000507 CurPtr += COFFHeader->SizeOfOptionalHeader;
508 }
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000509
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000510 if (!COFFHeader->isImportLibrary())
511 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
512 COFFHeader->NumberOfSections * sizeof(coff_section))))
513 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000514
Rui Ueyamac2bed422013-09-27 21:04:00 +0000515 // Initialize the pointer to the symbol table.
516 if (COFFHeader->PointerToSymbolTable != 0)
517 if ((ec = initSymbolTablePtr()))
Michael J. Spencerd5930ca2011-11-08 23:34:07 +0000518 return;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000519
Rui Ueyamac2bed422013-09-27 21:04:00 +0000520 // Initialize the pointer to the beginning of the import table.
521 if ((ec = initImportTablePtr()))
522 return;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000523
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000524 ec = object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000525}
526
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000527symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000528 DataRefImpl ret;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000529 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000530 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000531}
532
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000533symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000534 // The symbol table ends where the string table begins.
Michael J. Spencer0324b672011-01-21 02:27:02 +0000535 DataRefImpl ret;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000536 ret.p = reinterpret_cast<uintptr_t>(StringTable);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000537 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000538}
539
Michael J. Spencer8c4729f2012-02-28 00:40:37 +0000540symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
541 // TODO: implement
542 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
543}
544
545symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
546 // TODO: implement
547 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
548}
549
David Meyer2fc34c52012-03-01 01:36:50 +0000550library_iterator COFFObjectFile::begin_libraries_needed() const {
551 // TODO: implement
552 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
553}
554
555library_iterator COFFObjectFile::end_libraries_needed() const {
556 // TODO: implement
557 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
558}
559
David Meyerc429b802012-03-01 22:19:54 +0000560StringRef COFFObjectFile::getLoadName() const {
561 // COFF does not have this field.
562 return "";
563}
564
Rui Ueyamabc654b12013-09-27 21:47:05 +0000565import_directory_iterator COFFObjectFile::import_directory_begin() const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000566 DataRefImpl Imp;
567 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
568 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
569}
570
Rui Ueyamabc654b12013-09-27 21:47:05 +0000571import_directory_iterator COFFObjectFile::import_directory_end() const {
Rui Ueyamac2bed422013-09-27 21:04:00 +0000572 DataRefImpl Imp;
573 if (ImportDirectory) {
574 Imp.p = reinterpret_cast<uintptr_t>(
575 ImportDirectory + (NumberOfImportDirectory - 1));
576 } else {
577 Imp.p = 0;
578 }
579 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
580}
David Meyerc429b802012-03-01 22:19:54 +0000581
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000582section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000583 DataRefImpl ret;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000584 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000585 return section_iterator(SectionRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000586}
587
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000588section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000589 DataRefImpl ret;
Rui Ueyama15ba1e22013-11-15 20:23:25 +0000590 int numSections = COFFHeader->isImportLibrary()
591 ? 0 : COFFHeader->NumberOfSections;
592 ret.p = reinterpret_cast<uintptr_t>(SectionTable + numSections);
Michael J. Spencer0324b672011-01-21 02:27:02 +0000593 return section_iterator(SectionRef(ret, this));
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000594}
595
596uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer0324b672011-01-21 02:27:02 +0000597 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000598}
599
600StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000601 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000602 case COFF::IMAGE_FILE_MACHINE_I386:
603 return "COFF-i386";
604 case COFF::IMAGE_FILE_MACHINE_AMD64:
605 return "COFF-x86-64";
606 default:
607 return "COFF-<unknown arch>";
608 }
609}
610
611unsigned COFFObjectFile::getArch() const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000612 switch(COFFHeader->Machine) {
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000613 case COFF::IMAGE_FILE_MACHINE_I386:
614 return Triple::x86;
615 case COFF::IMAGE_FILE_MACHINE_AMD64:
616 return Triple::x86_64;
617 default:
618 return Triple::UnknownArch;
619 }
620}
621
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000622// This method is kept here because lld uses this. As soon as we make
623// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000624error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000625 return getCOFFHeader(Res);
626}
627
628error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
629 Res = COFFHeader;
630 return object_error::success;
631}
632
633error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
634 Res = PE32Header;
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000635 return object_error::success;
636}
637
Rui Ueyamaed64342b2013-07-19 23:23:29 +0000638error_code COFFObjectFile::getDataDirectory(uint32_t index,
639 const data_directory *&Res) const {
640 // Error if if there's no data directory or the index is out of range.
641 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
642 return object_error::parse_failed;
643 Res = &DataDirectory[index];
644 return object_error::success;
645}
646
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000647error_code COFFObjectFile::getSection(int32_t index,
648 const coff_section *&Result) const {
649 // Check for special index values.
650 if (index == COFF::IMAGE_SYM_UNDEFINED ||
651 index == COFF::IMAGE_SYM_ABSOLUTE ||
652 index == COFF::IMAGE_SYM_DEBUG)
653 Result = NULL;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000654 else if (index > 0 && index <= COFFHeader->NumberOfSections)
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000655 // We already verified the section table data, so no need to check again.
656 Result = SectionTable + (index - 1);
657 else
658 return object_error::parse_failed;
659 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000660}
661
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000662error_code COFFObjectFile::getString(uint32_t offset,
663 StringRef &Result) const {
664 if (StringTableSize <= 4)
665 // Tried to get a string from an empty string table.
666 return object_error::parse_failed;
667 if (offset >= StringTableSize)
668 return object_error::unexpected_eof;
669 Result = StringRef(StringTable + offset);
670 return object_error::success;
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000671}
672
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000673error_code COFFObjectFile::getSymbol(uint32_t index,
674 const coff_symbol *&Result) const {
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000675 if (index < COFFHeader->NumberOfSymbols)
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000676 Result = SymbolTable + index;
677 else
678 return object_error::parse_failed;
679 return object_error::success;
680}
681
Michael J. Spencer89a7a5e2011-10-17 23:53:56 +0000682error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
683 StringRef &Res) const {
684 // Check for string table entry. First 4 bytes are 0.
685 if (symbol->Name.Offset.Zeroes == 0) {
686 uint32_t Offset = symbol->Name.Offset.Offset;
687 if (error_code ec = getString(Offset, Res))
688 return ec;
689 return object_error::success;
690 }
691
692 if (symbol->Name.ShortName[7] == 0)
693 // Null terminated, let ::strlen figure out the length.
694 Res = StringRef(symbol->Name.ShortName);
695 else
696 // Not null terminated, use all 8 bytes.
697 Res = StringRef(symbol->Name.ShortName, 8);
698 return object_error::success;
699}
700
Marshall Clow71757ef2012-06-15 01:08:25 +0000701ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
702 const coff_symbol *symbol) const {
703 const uint8_t *aux = NULL;
Rui Ueyamac2bed422013-09-27 21:04:00 +0000704
Marshall Clow71757ef2012-06-15 01:08:25 +0000705 if ( symbol->NumberOfAuxSymbols > 0 ) {
706 // AUX data comes immediately after the symbol in COFF
707 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
708# ifndef NDEBUG
709 // Verify that the aux symbol points to a valid entry in the symbol table.
710 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000711 if (offset < COFFHeader->PointerToSymbolTable
712 || offset >= COFFHeader->PointerToSymbolTable
713 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clow71757ef2012-06-15 01:08:25 +0000714 report_fatal_error("Aux Symbol data was outside of symbol table.");
715
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000716 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clow71757ef2012-06-15 01:08:25 +0000717 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clow71757ef2012-06-15 01:08:25 +0000718# endif
Marshall Clowbfb85e62012-06-15 01:15:47 +0000719 }
Marshall Clow71757ef2012-06-15 01:08:25 +0000720 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
721}
722
Michael J. Spencer53c2d542012-03-19 20:27:15 +0000723error_code COFFObjectFile::getSectionName(const coff_section *Sec,
724 StringRef &Res) const {
725 StringRef Name;
726 if (Sec->Name[7] == 0)
727 // Null terminated, let ::strlen figure out the length.
728 Name = Sec->Name;
729 else
730 // Not null terminated, use all 8 bytes.
731 Name = StringRef(Sec->Name, 8);
732
733 // Check for string table entry. First byte is '/'.
734 if (Name[0] == '/') {
735 uint32_t Offset;
736 if (Name.substr(1).getAsInteger(10, Offset))
737 return object_error::parse_failed;
738 if (error_code ec = getString(Offset, Name))
739 return ec;
740 }
741
742 Res = Name;
743 return object_error::success;
744}
745
Michael J. Spencer9da9e692012-03-19 20:27:37 +0000746error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
747 ArrayRef<uint8_t> &Res) const {
748 // The only thing that we need to verify is that the contents is contained
749 // within the file bounds. We don't need to make sure it doesn't cover other
750 // data, as there's nothing that says that is not allowed.
751 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
752 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
753 if (ConEnd > uintptr_t(Data->getBufferEnd()))
754 return object_error::parse_failed;
755 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
756 Sec->SizeOfRawData);
757 return object_error::success;
758}
759
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000760const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000761 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000762}
763error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
764 RelocationRef &Res) const {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000765 Rel.p = reinterpret_cast<uintptr_t>(
766 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000767 Res = RelocationRef(Rel, this);
768 return object_error::success;
769}
770error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
771 uint64_t &Res) const {
Rafael Espindola1e483872013-04-25 12:28:45 +0000772 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000773}
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000774error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
775 uint64_t &Res) const {
776 Res = toRel(Rel)->VirtualAddress;
777 return object_error::success;
778}
Rafael Espindola806f0062013-06-05 01:33:53 +0000779symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000780 const coff_relocation* R = toRel(Rel);
781 DataRefImpl Symb;
782 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
Rafael Espindola806f0062013-06-05 01:33:53 +0000783 return symbol_iterator(SymbolRef(Symb, this));
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000784}
785error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson7be76592011-10-26 17:08:49 +0000786 uint64_t &Res) const {
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000787 const coff_relocation* R = toRel(Rel);
788 Res = R->Type;
789 return object_error::success;
790}
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000791
Marshall Clow71757ef2012-06-15 01:08:25 +0000792const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
793 return toSec(It->getRawDataRefImpl());
794}
795
796const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
797 return toSymb(It->getRawDataRefImpl());
798}
799
Marshall Clowd3e2a762012-06-18 19:47:16 +0000800const coff_relocation *COFFObjectFile::getCOFFRelocation(
801 relocation_iterator &It) const {
802 return toRel(It->getRawDataRefImpl());
803}
804
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000805#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
806 case COFF::enum: res = #enum; break;
807
808error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
809 SmallVectorImpl<char> &Result) const {
810 const coff_relocation *reloc = toRel(Rel);
811 StringRef res;
Rui Ueyama82ebd8e2013-06-12 19:10:33 +0000812 switch (COFFHeader->Machine) {
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000813 case COFF::IMAGE_FILE_MACHINE_AMD64:
814 switch (reloc->Type) {
815 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
816 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
817 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
818 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
819 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
820 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
821 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
822 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
823 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
824 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
825 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
826 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
827 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
828 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
829 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
830 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
831 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
832 default:
833 res = "Unknown";
834 }
835 break;
836 case COFF::IMAGE_FILE_MACHINE_I386:
837 switch (reloc->Type) {
838 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
839 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
840 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
841 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
842 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
843 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
844 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
845 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
846 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
847 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
848 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
849 default:
850 res = "Unknown";
851 }
852 break;
853 default:
854 res = "Unknown";
855 }
856 Result.append(res.begin(), res.end());
857 return object_error::success;
858}
859
860#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
861
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000862error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
863 SmallVectorImpl<char> &Result) const {
864 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi648b2fa2011-10-08 11:22:53 +0000865 const coff_symbol *symb = 0;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000866 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
867 DataRefImpl sym;
Michael J. Spencere5fd0042011-10-07 19:25:32 +0000868 sym.p = reinterpret_cast<uintptr_t>(symb);
869 StringRef symname;
870 if (error_code ec = getSymbolName(sym, symname)) return ec;
871 Result.append(symname.begin(), symname.end());
872 return object_error::success;
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000873}
Benjamin Kramer022ecdf2011-09-08 20:52:17 +0000874
David Meyer2fc34c52012-03-01 01:36:50 +0000875error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
876 LibraryRef &Result) const {
877 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
878}
879
880error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
881 StringRef &Result) const {
882 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
883}
884
Rui Ueyamac2bed422013-09-27 21:04:00 +0000885bool ImportDirectoryEntryRef::
886operator==(const ImportDirectoryEntryRef &Other) const {
887 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
888}
889
890static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
891 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
892}
893
894error_code
895ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
896 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
897 Dir += 1;
898 DataRefImpl Next;
899 Next.p = reinterpret_cast<uintptr_t>(Dir);
900 Result = ImportDirectoryEntryRef(Next, OwningObject);
901 return object_error::success;
902}
903
904error_code ImportDirectoryEntryRef::
905getImportTableEntry(const import_directory_table_entry *&Result) const {
906 Result = toImportEntry(ImportDirectoryPimpl);
907 return object_error::success;
908}
909
910error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
911 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
912 uintptr_t IntPtr = 0;
913 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
914 return ec;
915 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
916 Result = StringRef(Ptr);
917 return object_error::success;
918}
919
920error_code ImportDirectoryEntryRef::getImportLookupEntry(
921 const import_lookup_table_entry32 *&Result) const {
922 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
923 uintptr_t IntPtr = 0;
924 if (error_code ec = OwningObject->getRvaPtr(
925 Dir->ImportLookupTableRVA, IntPtr))
926 return ec;
927 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
928 return object_error::success;
929}
930
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000931namespace llvm {
932
933 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencerec29b122011-06-25 17:54:50 +0000934 error_code ec;
935 return new COFFObjectFile(Object, ec);
Michael J. Spencer8e90ada2011-01-20 06:38:34 +0000936 }
937
938} // end namespace llvm