blob: 49317e905142d2115bfd09f5e17efd9e48c7f074 [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"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000019
20using namespace llvm;
21using namespace object;
22
23namespace {
24using support::ulittle8_t;
25using support::ulittle16_t;
26using support::ulittle32_t;
27using support::little16_t;
28}
29
Michael J. Spencer25b15772011-06-25 17:55:23 +000030namespace {
31// Returns false if size is greater than the buffer size. And sets ec.
32bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
33 if (m->getBufferSize() < size) {
34 ec = object_error::unexpected_eof;
35 return false;
36 }
37 return true;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000038}
39
Michael J. Spencer25b15772011-06-25 17:55:23 +000040// Returns false if any bytes in [addr, addr + size) fall outsize of m.
41bool checkAddr(const MemoryBuffer *m,
42 error_code &ec,
43 uintptr_t addr,
44 uint64_t size) {
45 if (addr + size < addr ||
46 addr + size < size ||
47 addr + size > uintptr_t(m->getBufferEnd())) {
48 ec = object_error::unexpected_eof;
49 return false;
50 }
51 return true;
52}
53}
54
55const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
56 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
57
58# ifndef NDEBUG
59 // Verify that the symbol points to a valid entry in the symbol table.
60 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
Rui Ueyama4bf771b2013-06-12 19:10:33 +000061 if (offset < COFFHeader->PointerToSymbolTable
62 || offset >= COFFHeader->PointerToSymbolTable
63 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer25b15772011-06-25 17:55:23 +000064 report_fatal_error("Symbol was outside of symbol table.");
65
Rui Ueyama4bf771b2013-06-12 19:10:33 +000066 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Michael J. Spencer25b15772011-06-25 17:55:23 +000067 == 0 && "Symbol did not point to the beginning of a symbol");
68# endif
69
70 return addr;
71}
72
73const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
74 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
75
76# ifndef NDEBUG
77 // Verify that the section points to a valid entry in the section table.
78 if (addr < SectionTable
Rui Ueyama4bf771b2013-06-12 19:10:33 +000079 || addr >= (SectionTable + COFFHeader->NumberOfSections))
Michael J. Spencer25b15772011-06-25 17:55:23 +000080 report_fatal_error("Section was outside of section table.");
81
82 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
83 assert(offset % sizeof(coff_section) == 0 &&
84 "Section did not point to the beginning of a section");
85# endif
86
87 return addr;
88}
89
90error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
91 SymbolRef &Result) const {
92 const coff_symbol *symb = toSymb(Symb);
93 symb += 1 + symb->NumberOfAuxSymbols;
94 Symb.p = reinterpret_cast<uintptr_t>(symb);
95 Result = SymbolRef(Symb, this);
96 return object_error::success;
97}
98
99 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
100 StringRef &Result) const {
101 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000102 return getSymbolName(symb, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000103}
104
Danil Malyshevb0436a72011-11-29 17:40:10 +0000105error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000106 uint64_t &Result) const {
107 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000108 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000109 if (error_code ec = getSection(symb->SectionNumber, Section))
110 return ec;
111 char Type;
112 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
113 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000114 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000115 Result = UnknownAddressOrSize;
116 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000117 Result = Section->PointerToRawData + symb->Value;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000118 else
119 Result = symb->Value;
120 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000121}
122
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000123error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
124 uint64_t &Result) const {
125 const coff_symbol *symb = toSymb(Symb);
126 const coff_section *Section = NULL;
127 if (error_code ec = getSection(symb->SectionNumber, Section))
128 return ec;
129 char Type;
130 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
131 return ec;
132 if (Type == 'U' || Type == 'w')
133 Result = UnknownAddressOrSize;
134 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000135 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000136 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000137 Result = symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000138 return object_error::success;
139}
140
141error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000142 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-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 Meyer2c677272012-02-29 02:11:55 +0000147 Result = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000148 } else {
Michael J. Spencer5e3a0822011-10-18 19:31:59 +0000149 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000150 Result = SymbolRef::ST_Function;
151 } else {
152 char Type;
153 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
154 return ec;
155 if (Type == 'r' || Type == 'R') {
156 Result = SymbolRef::ST_Data;
157 }
158 }
159 }
160 return object_error::success;
161}
162
David Meyerc46255a2012-02-28 23:47:53 +0000163error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
164 uint32_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000165 const coff_symbol *symb = toSymb(Symb);
David Meyerc46255a2012-02-28 23:47:53 +0000166 Result = SymbolRef::SF_None;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000167
David Meyer2c677272012-02-29 02:11:55 +0000168 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
169
170 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
171 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
172 Result |= SymbolRef::SF_Undefined;
David Meyerc46255a2012-02-28 23:47:53 +0000173
174 // TODO: This are certainly too restrictive.
175 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
176 Result |= SymbolRef::SF_Global;
177
178 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
179 Result |= SymbolRef::SF_Weak;
180
181 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
182 Result |= SymbolRef::SF_Absolute;
183
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000184 return object_error::success;
185}
186
Michael J. Spencer25b15772011-06-25 17:55:23 +0000187error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
188 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000189 // FIXME: Return the correct size. This requires looking at all the symbols
190 // in the same section as this symbol, and looking for either the next
191 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000192 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000193 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000194 if (error_code ec = getSection(symb->SectionNumber, Section))
195 return ec;
196 char Type;
197 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
198 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000199 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000200 Result = UnknownAddressOrSize;
201 else if (Section)
202 Result = Section->SizeOfRawData - symb->Value;
203 else
204 Result = 0;
205 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000206}
207
Michael J. Spencer25b15772011-06-25 17:55:23 +0000208error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
209 char &Result) const {
210 const coff_symbol *symb = toSymb(Symb);
211 StringRef name;
212 if (error_code ec = getSymbolName(Symb, name))
213 return ec;
214 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000215 .StartsWith(".debug", 'N')
216 .StartsWith(".sxdata", 'N')
217 .Default('?');
218
Michael J. Spencer25b15772011-06-25 17:55:23 +0000219 if (ret != '?') {
220 Result = ret;
221 return object_error::success;
222 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000223
224 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000225 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000226 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000227 if (error_code ec = getSection(symb->SectionNumber, Section))
228 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000229 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000230 }
231
232 switch (symb->SectionNumber) {
233 case COFF::IMAGE_SYM_UNDEFINED:
234 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000235 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
236 Result = 'w';
237 return object_error::success; // Don't do ::toupper.
Michael J. Spencer11ba26d2011-11-16 23:36:12 +0000238 } else if (symb->Value != 0) // Check for common symbols.
239 ret = 'c';
240 else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000241 ret = 'u';
242 break;
243 case COFF::IMAGE_SYM_ABSOLUTE:
244 ret = 'a';
245 break;
246 case COFF::IMAGE_SYM_DEBUG:
247 ret = 'n';
248 break;
249 default:
250 // Check section type.
251 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
252 ret = 't';
253 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
254 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
255 ret = 'r';
256 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
257 ret = 'd';
258 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
259 ret = 'b';
260 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
261 ret = 'i';
262
263 // Check for section symbol.
264 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
265 && symb->Value == 0)
266 ret = 's';
267 }
268
269 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000270 ret = ::toupper(static_cast<unsigned char>(ret));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000271
Michael J. Spencer25b15772011-06-25 17:55:23 +0000272 Result = ret;
273 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000274}
275
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000276error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
277 section_iterator &Result) const {
278 const coff_symbol *symb = toSymb(Symb);
279 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
280 Result = end_sections();
281 else {
Daniel Dunbara483fc82011-11-28 22:19:32 +0000282 const coff_section *sec = 0;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000283 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
284 DataRefImpl Sec;
285 Sec.p = reinterpret_cast<uintptr_t>(sec);
286 Result = section_iterator(SectionRef(Sec, this));
287 }
288 return object_error::success;
289}
290
Tim Northovera41dce32012-10-29 10:47:00 +0000291error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
292 uint64_t &Val) const {
293 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
294}
295
Michael J. Spencer25b15772011-06-25 17:55:23 +0000296error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
297 SectionRef &Result) const {
298 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000299 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000300 Sec.p = reinterpret_cast<uintptr_t>(sec);
301 Result = SectionRef(Sec, this);
302 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000303}
304
Michael J. Spencer25b15772011-06-25 17:55:23 +0000305error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
306 StringRef &Result) const {
307 const coff_section *sec = toSec(Sec);
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000308 return getSectionName(sec, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000309}
310
Michael J. Spencer25b15772011-06-25 17:55:23 +0000311error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
312 uint64_t &Result) const {
313 const coff_section *sec = toSec(Sec);
314 Result = sec->VirtualAddress;
315 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000316}
317
Michael J. Spencer25b15772011-06-25 17:55:23 +0000318error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
319 uint64_t &Result) const {
320 const coff_section *sec = toSec(Sec);
321 Result = sec->SizeOfRawData;
322 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000323}
324
Michael J. Spencer25b15772011-06-25 17:55:23 +0000325error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
326 StringRef &Result) const {
327 const coff_section *sec = toSec(Sec);
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000328 ArrayRef<uint8_t> Res;
329 error_code EC = getSectionContents(sec, Res);
330 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
331 return EC;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000332}
333
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000334error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
335 uint64_t &Res) const {
336 const coff_section *sec = toSec(Sec);
337 if (!sec)
338 return object_error::parse_failed;
339 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
340 return object_error::success;
341}
342
Michael J. Spencer25b15772011-06-25 17:55:23 +0000343error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
344 bool &Result) const {
345 const coff_section *sec = toSec(Sec);
346 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
347 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000348}
349
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000350error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
351 bool &Result) const {
352 const coff_section *sec = toSec(Sec);
353 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
354 return object_error::success;
355}
356
357error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
358 bool &Result) const {
359 const coff_section *sec = toSec(Sec);
360 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
361 return object_error::success;
362}
363
Preston Gurdc68dda82012-04-12 20:13:57 +0000364error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
365 bool &Result) const {
366 // FIXME: Unimplemented
367 Result = true;
368 return object_error::success;
369}
370
371error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
372 bool &Result) const {
373 const coff_section *sec = toSec(Sec);
374 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
375 return object_error::success;
376}
377
378error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
379 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000380 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000381 Result = false;
382 return object_error::success;
383}
384
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000385error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
386 bool &Result) const {
387 // FIXME: Unimplemented.
388 Result = false;
389 return object_error::success;
390}
391
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000392error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
393 DataRefImpl Symb,
394 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000395 const coff_section *sec = toSec(Sec);
396 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbara483fc82011-11-28 22:19:32 +0000397 const coff_section *symb_sec = 0;
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000398 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
399 if (symb_sec == sec)
400 Result = true;
401 else
402 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000403 return object_error::success;
404}
405
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000406relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
407 const coff_section *sec = toSec(Sec);
408 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000409 if (sec->NumberOfRelocations == 0)
410 ret.p = 0;
411 else
412 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
413
414 return relocation_iterator(RelocationRef(ret, this));
415}
416
417relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
418 const coff_section *sec = toSec(Sec);
419 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000420 if (sec->NumberOfRelocations == 0)
421 ret.p = 0;
422 else
423 ret.p = reinterpret_cast<uintptr_t>(
424 reinterpret_cast<const coff_relocation*>(
425 base() + sec->PointerToRelocations)
426 + sec->NumberOfRelocations);
427
428 return relocation_iterator(RelocationRef(ret, this));
429}
430
Michael J. Spencer001c9202011-06-25 17:54:50 +0000431COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
Rafael Espindola2c6f9972013-04-07 16:40:00 +0000432 : ObjectFile(Binary::ID_COFF, Object)
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000433 , COFFHeader(0)
434 , PE32Header(0)
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000435 , SectionTable(0)
436 , SymbolTable(0)
437 , StringTable(0)
438 , StringTableSize(0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000439 // Check that we at least have enough room for a header.
440 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000441
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000442 // The current location in the file where we are looking at.
443 uint64_t CurPtr = 0;
444
445 // PE header is optional and is present only in executables. If it exists,
446 // it is placed right after COFF header.
447 bool hasPEHeader = false;
Eric Christopher539d8d82011-04-03 22:53:19 +0000448
Michael J. Spencer25b15772011-06-25 17:55:23 +0000449 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000450 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000451 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
452 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000453 if (!checkSize(Data, ec, 0x3c + 8)) return;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000454 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
455 // Check the PE magic bytes. ("PE\0\0")
456 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000457 ec = object_error::parse_failed;
458 return;
459 }
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000460 CurPtr += 4; // Skip the PE magic bytes.
461 hasPEHeader = true;
Eric Christopher539d8d82011-04-03 22:53:19 +0000462 }
463
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000464 COFFHeader = reinterpret_cast<const coff_file_header *>(base() + CurPtr);
465 if (!checkAddr(Data, ec, uintptr_t(COFFHeader), sizeof(coff_file_header)))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000466 return;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000467 CurPtr += sizeof(coff_file_header);
468
469 if (hasPEHeader) {
470 PE32Header = reinterpret_cast<const pe32_header *>(base() + CurPtr);
471 if (!checkAddr(Data, ec, uintptr_t(PE32Header), sizeof(pe32_header)))
472 return;
473 // We only support PE32. If this is PE32 (not PE32+), the magic byte
474 // should be 0x10b. If this is not PE32, continue as if there's no PE
475 // header in this file.
476 if (PE32Header->Magic != 0x10b)
477 PE32Header = 0;
478 // There may be optional data directory after PE header. Skip them.
479 CurPtr += COFFHeader->SizeOfOptionalHeader;
480 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000481
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000482 SectionTable =
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000483 reinterpret_cast<const coff_section *>(base() + CurPtr);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000484 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000485 COFFHeader->NumberOfSections * sizeof(coff_section)))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000486 return;
487
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000488 if (COFFHeader->PointerToSymbolTable != 0) {
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000489 SymbolTable =
490 reinterpret_cast<const coff_symbol *>(base()
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000491 + COFFHeader->PointerToSymbolTable);
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000492 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000493 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000494 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000495
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000496 // Find string table.
497 StringTable = reinterpret_cast<const char *>(base())
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000498 + COFFHeader->PointerToSymbolTable
499 + COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000500 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
501 return;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000502
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000503 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
504 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
505 return;
506 // Check that the string table is null terminated if has any in it.
507 if (StringTableSize < 4
508 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
509 ec = object_error::parse_failed;
510 return;
511 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000512 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000513
Michael J. Spencer25b15772011-06-25 17:55:23 +0000514 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000515}
516
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000517symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000518 DataRefImpl ret;
519 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
520 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000521}
522
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000523symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000524 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000525 DataRefImpl ret;
526 ret.p = reinterpret_cast<intptr_t>(StringTable);
527 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000528}
529
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000530symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
531 // TODO: implement
532 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
533}
534
535symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
536 // TODO: implement
537 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
538}
539
David Meyer5c2b4ea2012-03-01 01:36:50 +0000540library_iterator COFFObjectFile::begin_libraries_needed() const {
541 // TODO: implement
542 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
543}
544
545library_iterator COFFObjectFile::end_libraries_needed() const {
546 // TODO: implement
547 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
548}
549
David Meyer97f77872012-03-01 22:19:54 +0000550StringRef COFFObjectFile::getLoadName() const {
551 // COFF does not have this field.
552 return "";
553}
554
555
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000556section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000557 DataRefImpl ret;
558 ret.p = reinterpret_cast<intptr_t>(SectionTable);
559 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000560}
561
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000562section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000563 DataRefImpl ret;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000564 ret.p = reinterpret_cast<intptr_t>(SectionTable + COFFHeader->NumberOfSections);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000565 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000566}
567
568uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000569 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000570}
571
572StringRef COFFObjectFile::getFileFormatName() const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000573 switch(COFFHeader->Machine) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000574 case COFF::IMAGE_FILE_MACHINE_I386:
575 return "COFF-i386";
576 case COFF::IMAGE_FILE_MACHINE_AMD64:
577 return "COFF-x86-64";
578 default:
579 return "COFF-<unknown arch>";
580 }
581}
582
583unsigned COFFObjectFile::getArch() const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000584 switch(COFFHeader->Machine) {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000585 case COFF::IMAGE_FILE_MACHINE_I386:
586 return Triple::x86;
587 case COFF::IMAGE_FILE_MACHINE_AMD64:
588 return Triple::x86_64;
589 default:
590 return Triple::UnknownArch;
591 }
592}
593
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000594// This method is kept here because lld uses this. As soon as we make
595// lld to use getCOFFHeader, this method will be removed.
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000596error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000597 return getCOFFHeader(Res);
598}
599
600error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
601 Res = COFFHeader;
602 return object_error::success;
603}
604
605error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
606 Res = PE32Header;
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000607 return object_error::success;
608}
609
Michael J. Spencer25b15772011-06-25 17:55:23 +0000610error_code COFFObjectFile::getSection(int32_t index,
611 const coff_section *&Result) const {
612 // Check for special index values.
613 if (index == COFF::IMAGE_SYM_UNDEFINED ||
614 index == COFF::IMAGE_SYM_ABSOLUTE ||
615 index == COFF::IMAGE_SYM_DEBUG)
616 Result = NULL;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000617 else if (index > 0 && index <= COFFHeader->NumberOfSections)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000618 // We already verified the section table data, so no need to check again.
619 Result = SectionTable + (index - 1);
620 else
621 return object_error::parse_failed;
622 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000623}
624
Michael J. Spencer25b15772011-06-25 17:55:23 +0000625error_code COFFObjectFile::getString(uint32_t offset,
626 StringRef &Result) const {
627 if (StringTableSize <= 4)
628 // Tried to get a string from an empty string table.
629 return object_error::parse_failed;
630 if (offset >= StringTableSize)
631 return object_error::unexpected_eof;
632 Result = StringRef(StringTable + offset);
633 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000634}
635
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000636error_code COFFObjectFile::getSymbol(uint32_t index,
637 const coff_symbol *&Result) const {
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000638 if (index < COFFHeader->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000639 Result = SymbolTable + index;
640 else
641 return object_error::parse_failed;
642 return object_error::success;
643}
644
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000645error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
646 StringRef &Res) const {
647 // Check for string table entry. First 4 bytes are 0.
648 if (symbol->Name.Offset.Zeroes == 0) {
649 uint32_t Offset = symbol->Name.Offset.Offset;
650 if (error_code ec = getString(Offset, Res))
651 return ec;
652 return object_error::success;
653 }
654
655 if (symbol->Name.ShortName[7] == 0)
656 // Null terminated, let ::strlen figure out the length.
657 Res = StringRef(symbol->Name.ShortName);
658 else
659 // Not null terminated, use all 8 bytes.
660 Res = StringRef(symbol->Name.ShortName, 8);
661 return object_error::success;
662}
663
Marshall Clowd4d03e02012-06-15 01:08:25 +0000664ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
665 const coff_symbol *symbol) const {
666 const uint8_t *aux = NULL;
667
668 if ( symbol->NumberOfAuxSymbols > 0 ) {
669 // AUX data comes immediately after the symbol in COFF
670 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
671# ifndef NDEBUG
672 // Verify that the aux symbol points to a valid entry in the symbol table.
673 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000674 if (offset < COFFHeader->PointerToSymbolTable
675 || offset >= COFFHeader->PointerToSymbolTable
676 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
Marshall Clowd4d03e02012-06-15 01:08:25 +0000677 report_fatal_error("Aux Symbol data was outside of symbol table.");
678
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000679 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
Marshall Clowd4d03e02012-06-15 01:08:25 +0000680 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
Marshall Clowd4d03e02012-06-15 01:08:25 +0000681# endif
Marshall Clow45aad162012-06-15 01:15:47 +0000682 }
Marshall Clowd4d03e02012-06-15 01:08:25 +0000683 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
684}
685
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000686error_code COFFObjectFile::getSectionName(const coff_section *Sec,
687 StringRef &Res) const {
688 StringRef Name;
689 if (Sec->Name[7] == 0)
690 // Null terminated, let ::strlen figure out the length.
691 Name = Sec->Name;
692 else
693 // Not null terminated, use all 8 bytes.
694 Name = StringRef(Sec->Name, 8);
695
696 // Check for string table entry. First byte is '/'.
697 if (Name[0] == '/') {
698 uint32_t Offset;
699 if (Name.substr(1).getAsInteger(10, Offset))
700 return object_error::parse_failed;
701 if (error_code ec = getString(Offset, Name))
702 return ec;
703 }
704
705 Res = Name;
706 return object_error::success;
707}
708
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000709error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
710 ArrayRef<uint8_t> &Res) const {
711 // The only thing that we need to verify is that the contents is contained
712 // within the file bounds. We don't need to make sure it doesn't cover other
713 // data, as there's nothing that says that is not allowed.
714 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
715 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
716 if (ConEnd > uintptr_t(Data->getBufferEnd()))
717 return object_error::parse_failed;
718 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
719 Sec->SizeOfRawData);
720 return object_error::success;
721}
722
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000723const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000724 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000725}
726error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
727 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000728 Rel.p = reinterpret_cast<uintptr_t>(
729 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000730 Res = RelocationRef(Rel, this);
731 return object_error::success;
732}
733error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
734 uint64_t &Res) const {
Rafael Espindola956ca722013-04-25 12:28:45 +0000735 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000736}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000737error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
738 uint64_t &Res) const {
739 Res = toRel(Rel)->VirtualAddress;
740 return object_error::success;
741}
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000742symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000743 const coff_relocation* R = toRel(Rel);
744 DataRefImpl Symb;
745 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000746 return symbol_iterator(SymbolRef(Symb, this));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000747}
748error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000749 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000750 const coff_relocation* R = toRel(Rel);
751 Res = R->Type;
752 return object_error::success;
753}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000754
Marshall Clowd4d03e02012-06-15 01:08:25 +0000755const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
756 return toSec(It->getRawDataRefImpl());
757}
758
759const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
760 return toSymb(It->getRawDataRefImpl());
761}
762
Marshall Clow9ac0f1d2012-06-18 19:47:16 +0000763const coff_relocation *COFFObjectFile::getCOFFRelocation(
764 relocation_iterator &It) const {
765 return toRel(It->getRawDataRefImpl());
766}
767
Marshall Clowd4d03e02012-06-15 01:08:25 +0000768
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000769#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
770 case COFF::enum: res = #enum; break;
771
772error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
773 SmallVectorImpl<char> &Result) const {
774 const coff_relocation *reloc = toRel(Rel);
775 StringRef res;
Rui Ueyama4bf771b2013-06-12 19:10:33 +0000776 switch (COFFHeader->Machine) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000777 case COFF::IMAGE_FILE_MACHINE_AMD64:
778 switch (reloc->Type) {
779 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
780 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
781 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
782 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
783 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
784 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
785 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
786 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
787 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
788 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
789 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
790 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
791 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
792 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
793 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
794 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
795 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
796 default:
797 res = "Unknown";
798 }
799 break;
800 case COFF::IMAGE_FILE_MACHINE_I386:
801 switch (reloc->Type) {
802 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
803 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
804 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
805 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
806 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
807 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
808 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
809 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
810 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
811 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
812 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
813 default:
814 res = "Unknown";
815 }
816 break;
817 default:
818 res = "Unknown";
819 }
820 Result.append(res.begin(), res.end());
821 return object_error::success;
822}
823
824#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
825
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000826error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
827 SmallVectorImpl<char> &Result) const {
828 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000829 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000830 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
831 DataRefImpl sym;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000832 sym.p = reinterpret_cast<uintptr_t>(symb);
833 StringRef symname;
834 if (error_code ec = getSymbolName(sym, symname)) return ec;
835 Result.append(symname.begin(), symname.end());
836 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000837}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000838
David Meyer5c2b4ea2012-03-01 01:36:50 +0000839error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
840 LibraryRef &Result) const {
841 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
842}
843
844error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
845 StringRef &Result) const {
846 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
847}
848
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000849namespace llvm {
850
851 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000852 error_code ec;
853 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000854 }
855
856} // end namespace llvm