blob: 463121c39f8a356eeee97595c1edc94f0bff3d8a [file] [log] [blame]
Michael J. Spencera1ef8ef2011-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. Spencer001c9202011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Rui Ueyamaa6610ee2013-09-27 21:04:00 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000021
22using namespace llvm;
23using namespace object;
24
25namespace {
26using support::ulittle8_t;
27using support::ulittle16_t;
28using support::ulittle32_t;
29using support::little16_t;
30}
31
Michael J. Spencer25b15772011-06-25 17:55:23 +000032namespace {
33// Returns false if size is greater than the buffer size. And sets ec.
34bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
35 if (m->getBufferSize() < size) {
36 ec = object_error::unexpected_eof;
37 return false;
38 }
39 return true;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000040}
41
Rui Ueyama2f6c0482013-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>
45error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
46 const size_t Size = sizeof(T)) {
47 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. Spencer25b15772011-06-25 17:55:23 +000052 }
Rui Ueyama2f6c0482013-07-19 23:23:29 +000053 Obj = reinterpret_cast<const T *>(Addr);
54 return object_error::success;
Michael J. Spencer25b15772011-06-25 17:55:23 +000055}
56}
57
58const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
59 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
60
61# ifndef NDEBUG
62 // Verify that the symbol points to a valid entry in the symbol table.
63 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
Rui Ueyama4bf771b2013-06-12 19:10:33 +000064 if (offset < COFFHeader->PointerToSymbolTable
65 || offset >= COFFHeader->PointerToSymbolTable
66 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer25b15772011-06-25 17:55:23 +000067 report_fatal_error("Symbol was outside of symbol table.");
68
Rui Ueyama4bf771b2013-06-12 19:10:33 +000069 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer25b15772011-06-25 17:55:23 +000070 == 0 && "Symbol did not point to the beginning of a symbol");
71# endif
72
73 return addr;
74}
75
76const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
77 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
78
79# ifndef NDEBUG
80 // Verify that the section points to a valid entry in the section table.
81 if (addr < SectionTable
Rui Ueyama4bf771b2013-06-12 19:10:33 +000082 || addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer25b15772011-06-25 17:55:23 +000083 report_fatal_error("Section was outside of section table.");
84
85 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
86 assert(offset % sizeof(coff_section) == 0 &&
87 "Section did not point to the beginning of a section");
88# endif
89
90 return addr;
91}
92
93error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
94 SymbolRef &Result) const {
95 const coff_symbol *symb = toSymb(Symb);
96 symb += 1 + symb->NumberOfAuxSymbols;
97 Symb.p = reinterpret_cast<uintptr_t>(symb);
98 Result = SymbolRef(Symb, this);
99 return object_error::success;
100}
101
102 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
103 StringRef &Result) const {
104 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000105 return getSymbolName(symb, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000106}
107
Danil Malyshevb0436a72011-11-29 17:40:10 +0000108error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000109 uint64_t &Result) const {
110 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000111 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000112 if (error_code ec = getSection(symb->SectionNumber, Section))
113 return ec;
114 char Type;
115 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
116 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000117 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000118 Result = UnknownAddressOrSize;
119 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000120 Result = Section->PointerToRawData + symb->Value;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000121 else
122 Result = symb->Value;
123 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000124}
125
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000126error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
127 uint64_t &Result) const {
128 const coff_symbol *symb = toSymb(Symb);
129 const coff_section *Section = NULL;
130 if (error_code ec = getSection(symb->SectionNumber, Section))
131 return ec;
132 char Type;
133 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
134 return ec;
135 if (Type == 'U' || Type == 'w')
136 Result = UnknownAddressOrSize;
137 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000138 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000139 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000140 Result = symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000141 return object_error::success;
142}
143
144error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000145 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000146 const coff_symbol *symb = toSymb(Symb);
147 Result = SymbolRef::ST_Other;
148 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
149 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer2c677272012-02-29 02:11:55 +0000150 Result = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000151 } else {
Michael J. Spencer5e3a0822011-10-18 19:31:59 +0000152 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000153 Result = SymbolRef::ST_Function;
154 } else {
155 char Type;
156 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
157 return ec;
158 if (Type == 'r' || Type == 'R') {
159 Result = SymbolRef::ST_Data;
160 }
161 }
162 }
163 return object_error::success;
164}
165
David Meyerc46255a2012-02-28 23:47:53 +0000166error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
167 uint32_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000168 const coff_symbol *symb = toSymb(Symb);
David Meyerc46255a2012-02-28 23:47:53 +0000169 Result = SymbolRef::SF_None;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000170
David Meyer2c677272012-02-29 02:11:55 +0000171 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
172
173 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
174 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
175 Result |= SymbolRef::SF_Undefined;
David Meyerc46255a2012-02-28 23:47:53 +0000176
177 // TODO: This are certainly too restrictive.
178 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
179 Result |= SymbolRef::SF_Global;
180
181 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
182 Result |= SymbolRef::SF_Weak;
183
184 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
185 Result |= SymbolRef::SF_Absolute;
186
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000187 return object_error::success;
188}
189
Michael J. Spencer25b15772011-06-25 17:55:23 +0000190error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
191 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000192 // FIXME: Return the correct size. This requires looking at all the symbols
193 // in the same section as this symbol, and looking for either the next
194 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000195 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000196 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000197 if (error_code ec = getSection(symb->SectionNumber, Section))
198 return ec;
199 char Type;
200 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
201 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000202 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000203 Result = UnknownAddressOrSize;
204 else if (Section)
205 Result = Section->SizeOfRawData - symb->Value;
206 else
207 Result = 0;
208 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000209}
210
Michael J. Spencer25b15772011-06-25 17:55:23 +0000211error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
212 char &Result) const {
213 const coff_symbol *symb = toSymb(Symb);
214 StringRef name;
215 if (error_code ec = getSymbolName(Symb, name))
216 return ec;
217 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000218 .StartsWith(".debug", 'N')
219 .StartsWith(".sxdata", 'N')
220 .Default('?');
221
Michael J. Spencer25b15772011-06-25 17:55:23 +0000222 if (ret != '?') {
223 Result = ret;
224 return object_error::success;
225 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000226
227 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000228 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000229 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000230 if (error_code ec = getSection(symb->SectionNumber, Section))
231 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000232 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000233 }
234
235 switch (symb->SectionNumber) {
236 case COFF::IMAGE_SYM_UNDEFINED:
237 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000238 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
239 Result = 'w';
240 return object_error::success; // Don't do ::toupper.
Michael J. Spencer11ba26d2011-11-16 23:36:12 +0000241 } else if (symb->Value != 0) // Check for common symbols.
242 ret = 'c';
243 else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000244 ret = 'u';
245 break;
246 case COFF::IMAGE_SYM_ABSOLUTE:
247 ret = 'a';
248 break;
249 case COFF::IMAGE_SYM_DEBUG:
250 ret = 'n';
251 break;
252 default:
253 // Check section type.
254 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
255 ret = 't';
256 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
257 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
258 ret = 'r';
259 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
260 ret = 'd';
261 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
262 ret = 'b';
263 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
264 ret = 'i';
265
266 // Check for section symbol.
267 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
268 && symb->Value == 0)
269 ret = 's';
270 }
271
272 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000273 ret = ::toupper(static_cast<unsigned char>(ret));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000274
Michael J. Spencer25b15772011-06-25 17:55:23 +0000275 Result = ret;
276 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000277}
278
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000279error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
280 section_iterator &Result) const {
281 const coff_symbol *symb = toSymb(Symb);
282 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
283 Result = end_sections();
284 else {
Daniel Dunbara483fc82011-11-28 22:19:32 +0000285 const coff_section *sec = 0;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000286 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
287 DataRefImpl Sec;
288 Sec.p = reinterpret_cast<uintptr_t>(sec);
289 Result = section_iterator(SectionRef(Sec, this));
290 }
291 return object_error::success;
292}
293
Tim Northovera41dce32012-10-29 10:47:00 +0000294error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
295 uint64_t &Val) const {
296 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
297}
298
Michael J. Spencer25b15772011-06-25 17:55:23 +0000299error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
300 SectionRef &Result) const {
301 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000302 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000303 Sec.p = reinterpret_cast<uintptr_t>(sec);
304 Result = SectionRef(Sec, this);
305 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000306}
307
Michael J. Spencer25b15772011-06-25 17:55:23 +0000308error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
309 StringRef &Result) const {
310 const coff_section *sec = toSec(Sec);
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000311 return getSectionName(sec, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000312}
313
Michael J. Spencer25b15772011-06-25 17:55:23 +0000314error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
315 uint64_t &Result) const {
316 const coff_section *sec = toSec(Sec);
317 Result = sec->VirtualAddress;
318 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000319}
320
Michael J. Spencer25b15772011-06-25 17:55:23 +0000321error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
322 uint64_t &Result) const {
323 const coff_section *sec = toSec(Sec);
324 Result = sec->SizeOfRawData;
325 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000326}
327
Michael J. Spencer25b15772011-06-25 17:55:23 +0000328error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
329 StringRef &Result) const {
330 const coff_section *sec = toSec(Sec);
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000331 ArrayRef<uint8_t> Res;
332 error_code EC = getSectionContents(sec, Res);
333 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
334 return EC;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000335}
336
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000337error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
338 uint64_t &Res) const {
339 const coff_section *sec = toSec(Sec);
340 if (!sec)
341 return object_error::parse_failed;
342 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
343 return object_error::success;
344}
345
Michael J. Spencer25b15772011-06-25 17:55:23 +0000346error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
347 bool &Result) const {
348 const coff_section *sec = toSec(Sec);
349 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
350 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000351}
352
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000353error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
354 bool &Result) const {
355 const coff_section *sec = toSec(Sec);
356 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
357 return object_error::success;
358}
359
360error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
361 bool &Result) const {
362 const coff_section *sec = toSec(Sec);
363 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
364 return object_error::success;
365}
366
Preston Gurdc68dda82012-04-12 20:13:57 +0000367error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
368 bool &Result) const {
369 // FIXME: Unimplemented
370 Result = true;
371 return object_error::success;
372}
373
374error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
375 bool &Result) const {
376 const coff_section *sec = toSec(Sec);
377 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
378 return object_error::success;
379}
380
381error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
382 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000383 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000384 Result = false;
385 return object_error::success;
386}
387
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000388error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
389 bool &Result) const {
390 // FIXME: Unimplemented.
391 Result = false;
392 return object_error::success;
393}
394
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000395error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
396 DataRefImpl Symb,
397 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000398 const coff_section *sec = toSec(Sec);
399 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbara483fc82011-11-28 22:19:32 +0000400 const coff_section *symb_sec = 0;
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000401 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
402 if (symb_sec == sec)
403 Result = true;
404 else
405 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000406 return object_error::success;
407}
408
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000409relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
410 const coff_section *sec = toSec(Sec);
411 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000412 if (sec->NumberOfRelocations == 0)
413 ret.p = 0;
414 else
415 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
416
417 return relocation_iterator(RelocationRef(ret, this));
418}
419
420relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
421 const coff_section *sec = toSec(Sec);
422 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000423 if (sec->NumberOfRelocations == 0)
424 ret.p = 0;
425 else
426 ret.p = reinterpret_cast<uintptr_t>(
427 reinterpret_cast<const coff_relocation*>(
428 base() + sec->PointerToRelocations)
429 + sec->NumberOfRelocations);
430
431 return relocation_iterator(RelocationRef(ret, this));
432}
433
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000434// Initialize the pointer to the symbol table.
435error_code COFFObjectFile::initSymbolTablePtr() {
436 if (error_code ec = getObject(
437 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
438 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
439 return ec;
440
441 // Find string table. The first four byte of the string table contains the
442 // total size of the string table, including the size field itself. If the
443 // string table is empty, the value of the first four byte would be 4.
444 const uint8_t *StringTableAddr =
445 base() + COFFHeader->PointerToSymbolTable +
446 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
447 const ulittle32_t *StringTableSizePtr;
448 if (error_code ec = getObject(StringTableSizePtr, Data, StringTableAddr))
449 return ec;
450 StringTableSize = *StringTableSizePtr;
451 if (error_code ec =
452 getObject(StringTable, Data, StringTableAddr, StringTableSize))
453 return ec;
454
455 // Check that the string table is null terminated if has any in it.
456 if (StringTableSize < 4 ||
457 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
458 return object_error::parse_failed;
459 return object_error::success;
460}
461
462// Returns the file offset for the given RVA.
463error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
464 error_code ec;
465 for (section_iterator i = begin_sections(), e = end_sections(); i != e;
466 i.increment(ec)) {
467 if (ec)
468 return ec;
469 const coff_section *Section = getCOFFSection(i);
470 uint32_t SectionStart = Section->VirtualAddress;
471 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
472 if (SectionStart <= Rva && Rva < SectionEnd) {
473 uint32_t Offset = Rva - SectionStart;
474 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
475 return object_error::success;
476 }
477 }
478 return object_error::parse_failed;
479}
480
481// Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
482// table entry.
483error_code COFFObjectFile::
484getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
485 uintptr_t IntPtr = 0;
486 if (error_code ec = getRvaPtr(Rva, IntPtr))
487 return ec;
488 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
489 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
490 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
491 return object_error::success;
492}
493
494// Find the import table.
495error_code COFFObjectFile::initImportTablePtr() {
496 // First, we get the RVA of the import table. If the file lacks a pointer to
497 // the import table, do nothing.
498 const data_directory *DataEntry;
499 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
500 return object_error::success;
501
502 // Do nothing if the pointer to import table is NULL.
503 if (DataEntry->RelativeVirtualAddress == 0)
504 return object_error::success;
505
506 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
507 NumberOfImportDirectory = DataEntry->Size /
508 sizeof(import_directory_table_entry);
509
510 // Find the section that contains the RVA. This is needed because the RVA is
511 // the import table's memory address which is different from its file offset.
512 uintptr_t IntPtr = 0;
513 if (error_code ec = getRvaPtr(ImportTableRva, IntPtr))
514 return ec;
515 ImportDirectory = reinterpret_cast<
516 const import_directory_table_entry *>(IntPtr);
517
518 // It's an error if there's no section containing the Import Table RVA.
519 return object_error::parse_failed;
520}
521
Michael J. Spencer001c9202011-06-25 17:54:50 +0000522COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
Rafael Espindola2c6f9972013-04-07 16:40:00 +0000523 : ObjectFile(Binary::ID_COFF, Object)
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000524 , COFFHeader(0)
525 , PE32Header(0)
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000526 , DataDirectory(0)
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000527 , SectionTable(0)
528 , SymbolTable(0)
529 , StringTable(0)
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000530 , StringTableSize(0)
531 , ImportDirectory(0)
532 , NumberOfImportDirectory(0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000533 // Check that we at least have enough room for a header.
534 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000535
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000536 // The current location in the file where we are looking at.
537 uint64_t CurPtr = 0;
538
539 // PE header is optional and is present only in executables. If it exists,
540 // it is placed right after COFF header.
541 bool hasPEHeader = false;
Eric Christopher539d8d82011-04-03 22:53:19 +0000542
Michael J. Spencer25b15772011-06-25 17:55:23 +0000543 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000544 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000545 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
546 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000547 if (!checkSize(Data, ec, 0x3c + 8)) return;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000548 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
549 // Check the PE magic bytes. ("PE\0\0")
550 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000551 ec = object_error::parse_failed;
552 return;
553 }
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000554 CurPtr += 4; // Skip the PE magic bytes.
555 hasPEHeader = true;
Eric Christopher539d8d82011-04-03 22:53:19 +0000556 }
557
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000558 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000559 return;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000560 CurPtr += sizeof(coff_file_header);
561
562 if (hasPEHeader) {
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000563 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000564 return;
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000565 if (PE32Header->Magic != 0x10b) {
566 // We only support PE32. If this is PE32 (not PE32+), the magic byte
567 // should be 0x10b. If this is not PE32, continue as if there's no PE
568 // header in this file.
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000569 PE32Header = 0;
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000570 } else if (PE32Header->NumberOfRvaAndSize > 0) {
571 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
572 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
573 if ((ec = getObject(DataDirectory, Data, addr, size)))
574 return;
575 }
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000576 CurPtr += COFFHeader->SizeOfOptionalHeader;
577 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000578
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000579 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
580 COFFHeader->NumberOfSections * sizeof(coff_section))))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000581 return;
582
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000583 // Initialize the pointer to the symbol table.
584 if (COFFHeader->PointerToSymbolTable != 0)
585 if ((ec = initSymbolTablePtr()))
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000586 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000587
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000588 // Initialize the pointer to the beginning of the import table.
589 if ((ec = initImportTablePtr()))
590 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000591
Michael J. Spencer25b15772011-06-25 17:55:23 +0000592 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000593}
594
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000595symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000596 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000597 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000598 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000599}
600
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000601symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000602 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000603 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000604 ret.p = reinterpret_cast<uintptr_t>(StringTable);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000605 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000606}
607
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000608symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
609 // TODO: implement
610 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
611}
612
613symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
614 // TODO: implement
615 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
616}
617
David Meyer5c2b4ea2012-03-01 01:36:50 +0000618library_iterator COFFObjectFile::begin_libraries_needed() const {
619 // TODO: implement
620 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
621}
622
623library_iterator COFFObjectFile::end_libraries_needed() const {
624 // TODO: implement
625 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
626}
627
David Meyer97f77872012-03-01 22:19:54 +0000628StringRef COFFObjectFile::getLoadName() const {
629 // COFF does not have this field.
630 return "";
631}
632
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000633import_directory_iterator COFFObjectFile::getImportDirectoryBegin() const {
634 DataRefImpl Imp;
635 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
636 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
637}
638
639import_directory_iterator COFFObjectFile::getImportDirectoryEnd() const {
640 DataRefImpl Imp;
641 if (ImportDirectory) {
642 Imp.p = reinterpret_cast<uintptr_t>(
643 ImportDirectory + (NumberOfImportDirectory - 1));
644 } else {
645 Imp.p = 0;
646 }
647 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
648}
David Meyer97f77872012-03-01 22:19:54 +0000649
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000650section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000651 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000652 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000653 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000654}
655
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000656section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000657 DataRefImpl ret;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000658 ret.p = reinterpret_cast<uintptr_t>(SectionTable + COFFHeader->NumberOfSections);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000659 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000660}
661
662uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000663 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000664}
665
666StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000667 switch(COFFHeader->Machine) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000668 case COFF::IMAGE_FILE_MACHINE_I386:
669 return "COFF-i386";
670 case COFF::IMAGE_FILE_MACHINE_AMD64:
671 return "COFF-x86-64";
672 default:
673 return "COFF-<unknown arch>";
674 }
675}
676
677unsigned COFFObjectFile::getArch() const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000678 switch(COFFHeader->Machine) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000679 case COFF::IMAGE_FILE_MACHINE_I386:
680 return Triple::x86;
681 case COFF::IMAGE_FILE_MACHINE_AMD64:
682 return Triple::x86_64;
683 default:
684 return Triple::UnknownArch;
685 }
686}
687
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000688// This method is kept here because lld uses this. As soon as we make
689// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000690error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000691 return getCOFFHeader(Res);
692}
693
694error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
695 Res = COFFHeader;
696 return object_error::success;
697}
698
699error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
700 Res = PE32Header;
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000701 return object_error::success;
702}
703
Rui Ueyama2f6c0482013-07-19 23:23:29 +0000704error_code COFFObjectFile::getDataDirectory(uint32_t index,
705 const data_directory *&Res) const {
706 // Error if if there's no data directory or the index is out of range.
707 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
708 return object_error::parse_failed;
709 Res = &DataDirectory[index];
710 return object_error::success;
711}
712
Michael J. Spencer25b15772011-06-25 17:55:23 +0000713error_code COFFObjectFile::getSection(int32_t index,
714 const coff_section *&Result) const {
715 // Check for special index values.
716 if (index == COFF::IMAGE_SYM_UNDEFINED ||
717 index == COFF::IMAGE_SYM_ABSOLUTE ||
718 index == COFF::IMAGE_SYM_DEBUG)
719 Result = NULL;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000720 else if (index > 0 && index <= COFFHeader->NumberOfSections)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000721 // We already verified the section table data, so no need to check again.
722 Result = SectionTable + (index - 1);
723 else
724 return object_error::parse_failed;
725 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000726}
727
Michael J. Spencer25b15772011-06-25 17:55:23 +0000728error_code COFFObjectFile::getString(uint32_t offset,
729 StringRef &Result) const {
730 if (StringTableSize <= 4)
731 // Tried to get a string from an empty string table.
732 return object_error::parse_failed;
733 if (offset >= StringTableSize)
734 return object_error::unexpected_eof;
735 Result = StringRef(StringTable + offset);
736 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000737}
738
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000739error_code COFFObjectFile::getSymbol(uint32_t index,
740 const coff_symbol *&Result) const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000741 if (index < COFFHeader->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000742 Result = SymbolTable + index;
743 else
744 return object_error::parse_failed;
745 return object_error::success;
746}
747
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000748error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
749 StringRef &Res) const {
750 // Check for string table entry. First 4 bytes are 0.
751 if (symbol->Name.Offset.Zeroes == 0) {
752 uint32_t Offset = symbol->Name.Offset.Offset;
753 if (error_code ec = getString(Offset, Res))
754 return ec;
755 return object_error::success;
756 }
757
758 if (symbol->Name.ShortName[7] == 0)
759 // Null terminated, let ::strlen figure out the length.
760 Res = StringRef(symbol->Name.ShortName);
761 else
762 // Not null terminated, use all 8 bytes.
763 Res = StringRef(symbol->Name.ShortName, 8);
764 return object_error::success;
765}
766
Marshall Clowd4d03e02012-06-15 01:08:25 +0000767ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
768 const coff_symbol *symbol) const {
769 const uint8_t *aux = NULL;
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000770
Marshall Clowd4d03e02012-06-15 01:08:25 +0000771 if ( symbol->NumberOfAuxSymbols > 0 ) {
772 // AUX data comes immediately after the symbol in COFF
773 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
774# ifndef NDEBUG
775 // Verify that the aux symbol points to a valid entry in the symbol table.
776 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000777 if (offset < COFFHeader->PointerToSymbolTable
778 || offset >= COFFHeader->PointerToSymbolTable
779 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clowd4d03e02012-06-15 01:08:25 +0000780 report_fatal_error("Aux Symbol data was outside of symbol table.");
781
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000782 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clowd4d03e02012-06-15 01:08:25 +0000783 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clowd4d03e02012-06-15 01:08:25 +0000784# endif
Marshall Clow45aad162012-06-15 01:15:47 +0000785 }
Marshall Clowd4d03e02012-06-15 01:08:25 +0000786 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
787}
788
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000789error_code COFFObjectFile::getSectionName(const coff_section *Sec,
790 StringRef &Res) const {
791 StringRef Name;
792 if (Sec->Name[7] == 0)
793 // Null terminated, let ::strlen figure out the length.
794 Name = Sec->Name;
795 else
796 // Not null terminated, use all 8 bytes.
797 Name = StringRef(Sec->Name, 8);
798
799 // Check for string table entry. First byte is '/'.
800 if (Name[0] == '/') {
801 uint32_t Offset;
802 if (Name.substr(1).getAsInteger(10, Offset))
803 return object_error::parse_failed;
804 if (error_code ec = getString(Offset, Name))
805 return ec;
806 }
807
808 Res = Name;
809 return object_error::success;
810}
811
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000812error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
813 ArrayRef<uint8_t> &Res) const {
814 // The only thing that we need to verify is that the contents is contained
815 // within the file bounds. We don't need to make sure it doesn't cover other
816 // data, as there's nothing that says that is not allowed.
817 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
818 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
819 if (ConEnd > uintptr_t(Data->getBufferEnd()))
820 return object_error::parse_failed;
821 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
822 Sec->SizeOfRawData);
823 return object_error::success;
824}
825
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000826const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000827 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000828}
829error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
830 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000831 Rel.p = reinterpret_cast<uintptr_t>(
832 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000833 Res = RelocationRef(Rel, this);
834 return object_error::success;
835}
836error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
837 uint64_t &Res) const {
Rafael Espindola956ca722013-04-25 12:28:45 +0000838 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000839}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000840error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
841 uint64_t &Res) const {
842 Res = toRel(Rel)->VirtualAddress;
843 return object_error::success;
844}
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000845symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000846 const coff_relocation* R = toRel(Rel);
847 DataRefImpl Symb;
848 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000849 return symbol_iterator(SymbolRef(Symb, this));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000850}
851error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000852 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000853 const coff_relocation* R = toRel(Rel);
854 Res = R->Type;
855 return object_error::success;
856}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000857
Marshall Clowd4d03e02012-06-15 01:08:25 +0000858const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
859 return toSec(It->getRawDataRefImpl());
860}
861
862const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
863 return toSymb(It->getRawDataRefImpl());
864}
865
Marshall Clow9ac0f1d2012-06-18 19:47:16 +0000866const coff_relocation *COFFObjectFile::getCOFFRelocation(
867 relocation_iterator &It) const {
868 return toRel(It->getRawDataRefImpl());
869}
870
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000871#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
872 case COFF::enum: res = #enum; break;
873
874error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
875 SmallVectorImpl<char> &Result) const {
876 const coff_relocation *reloc = toRel(Rel);
877 StringRef res;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000878 switch (COFFHeader->Machine) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000879 case COFF::IMAGE_FILE_MACHINE_AMD64:
880 switch (reloc->Type) {
881 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
882 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
883 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
884 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
885 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
886 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
887 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
888 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
889 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
890 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
891 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
892 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
893 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
894 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
895 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
896 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
897 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
898 default:
899 res = "Unknown";
900 }
901 break;
902 case COFF::IMAGE_FILE_MACHINE_I386:
903 switch (reloc->Type) {
904 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
905 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
906 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
907 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
908 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
909 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
910 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
911 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
912 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
913 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
914 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
915 default:
916 res = "Unknown";
917 }
918 break;
919 default:
920 res = "Unknown";
921 }
922 Result.append(res.begin(), res.end());
923 return object_error::success;
924}
925
926#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
927
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000928error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
929 SmallVectorImpl<char> &Result) const {
930 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000931 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000932 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
933 DataRefImpl sym;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000934 sym.p = reinterpret_cast<uintptr_t>(symb);
935 StringRef symname;
936 if (error_code ec = getSymbolName(sym, symname)) return ec;
937 Result.append(symname.begin(), symname.end());
938 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000939}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000940
David Meyer5c2b4ea2012-03-01 01:36:50 +0000941error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
942 LibraryRef &Result) const {
943 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
944}
945
946error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
947 StringRef &Result) const {
948 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
949}
950
Rui Ueyamaa6610ee2013-09-27 21:04:00 +0000951bool ImportDirectoryEntryRef::
952operator==(const ImportDirectoryEntryRef &Other) const {
953 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
954}
955
956static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
957 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
958}
959
960error_code
961ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
962 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
963 Dir += 1;
964 DataRefImpl Next;
965 Next.p = reinterpret_cast<uintptr_t>(Dir);
966 Result = ImportDirectoryEntryRef(Next, OwningObject);
967 return object_error::success;
968}
969
970error_code ImportDirectoryEntryRef::
971getImportTableEntry(const import_directory_table_entry *&Result) const {
972 Result = toImportEntry(ImportDirectoryPimpl);
973 return object_error::success;
974}
975
976error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
977 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
978 uintptr_t IntPtr = 0;
979 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
980 return ec;
981 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
982 Result = StringRef(Ptr);
983 return object_error::success;
984}
985
986error_code ImportDirectoryEntryRef::getImportLookupEntry(
987 const import_lookup_table_entry32 *&Result) const {
988 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
989 uintptr_t IntPtr = 0;
990 if (error_code ec = OwningObject->getRvaPtr(
991 Dir->ImportLookupTableRVA, IntPtr))
992 return ec;
993 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
994 return object_error::success;
995}
996
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000997namespace llvm {
998
999 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001000 error_code ec;
1001 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +00001002 }
1003
1004} // end namespace llvm