blob: 22894a348cfa6dfac0ed7a838376bcb76fb32610 [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. Spencer4344b1e2011-10-07 19:25:32 +000015#include "llvm/ADT/SmallString.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000016#include "llvm/ADT/StringSwitch.h"
17#include "llvm/ADT/Triple.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000018
19using namespace llvm;
20using namespace object;
21
22namespace {
23using support::ulittle8_t;
24using support::ulittle16_t;
25using support::ulittle32_t;
26using support::little16_t;
27}
28
Michael J. Spencer25b15772011-06-25 17:55:23 +000029namespace {
30// Returns false if size is greater than the buffer size. And sets ec.
31bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
32 if (m->getBufferSize() < size) {
33 ec = object_error::unexpected_eof;
34 return false;
35 }
36 return true;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000037}
38
Michael J. Spencer25b15772011-06-25 17:55:23 +000039// Returns false if any bytes in [addr, addr + size) fall outsize of m.
40bool checkAddr(const MemoryBuffer *m,
41 error_code &ec,
42 uintptr_t addr,
43 uint64_t size) {
44 if (addr + size < addr ||
45 addr + size < size ||
46 addr + size > uintptr_t(m->getBufferEnd())) {
47 ec = object_error::unexpected_eof;
48 return false;
49 }
50 return true;
51}
52}
53
54const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
55 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
56
57# ifndef NDEBUG
58 // Verify that the symbol points to a valid entry in the symbol table.
59 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
60 if (offset < Header->PointerToSymbolTable
61 || offset >= Header->PointerToSymbolTable
62 + (Header->NumberOfSymbols * sizeof(coff_symbol)))
63 report_fatal_error("Symbol was outside of symbol table.");
64
65 assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol)
66 == 0 && "Symbol did not point to the beginning of a symbol");
67# endif
68
69 return addr;
70}
71
72const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
73 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
74
75# ifndef NDEBUG
76 // Verify that the section points to a valid entry in the section table.
77 if (addr < SectionTable
78 || addr >= (SectionTable + Header->NumberOfSections))
79 report_fatal_error("Section was outside of section table.");
80
81 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
82 assert(offset % sizeof(coff_section) == 0 &&
83 "Section did not point to the beginning of a section");
84# endif
85
86 return addr;
87}
88
89error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
90 SymbolRef &Result) const {
91 const coff_symbol *symb = toSymb(Symb);
92 symb += 1 + symb->NumberOfAuxSymbols;
93 Symb.p = reinterpret_cast<uintptr_t>(symb);
94 Result = SymbolRef(Symb, this);
95 return object_error::success;
96}
97
98 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
99 StringRef &Result) const {
100 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000101 return getSymbolName(symb, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000102}
103
Danil Malyshevb0436a72011-11-29 17:40:10 +0000104error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000105 uint64_t &Result) const {
106 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000107 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000108 if (error_code ec = getSection(symb->SectionNumber, Section))
109 return ec;
110 char Type;
111 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
112 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000113 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000114 Result = UnknownAddressOrSize;
115 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000116 Result = Section->PointerToRawData + symb->Value;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000117 else
118 Result = symb->Value;
119 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000120}
121
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000122error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
123 uint64_t &Result) const {
124 const coff_symbol *symb = toSymb(Symb);
125 const coff_section *Section = NULL;
126 if (error_code ec = getSection(symb->SectionNumber, Section))
127 return ec;
128 char Type;
129 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
130 return ec;
131 if (Type == 'U' || Type == 'w')
132 Result = UnknownAddressOrSize;
133 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000134 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000135 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000136 Result = symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000137 return object_error::success;
138}
139
140error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000141 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000142 const coff_symbol *symb = toSymb(Symb);
143 Result = SymbolRef::ST_Other;
144 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
145 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer2c677272012-02-29 02:11:55 +0000146 Result = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000147 } else {
Michael J. Spencer5e3a0822011-10-18 19:31:59 +0000148 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000149 Result = SymbolRef::ST_Function;
150 } else {
151 char Type;
152 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
153 return ec;
154 if (Type == 'r' || Type == 'R') {
155 Result = SymbolRef::ST_Data;
156 }
157 }
158 }
159 return object_error::success;
160}
161
David Meyerc46255a2012-02-28 23:47:53 +0000162error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
163 uint32_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000164 const coff_symbol *symb = toSymb(Symb);
David Meyerc46255a2012-02-28 23:47:53 +0000165 Result = SymbolRef::SF_None;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000166
David Meyer2c677272012-02-29 02:11:55 +0000167 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
168
169 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
170 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
171 Result |= SymbolRef::SF_Undefined;
David Meyerc46255a2012-02-28 23:47:53 +0000172
173 // TODO: This are certainly too restrictive.
174 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
175 Result |= SymbolRef::SF_Global;
176
177 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
178 Result |= SymbolRef::SF_Weak;
179
180 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
181 Result |= SymbolRef::SF_Absolute;
182
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000183 return object_error::success;
184}
185
Michael J. Spencer25b15772011-06-25 17:55:23 +0000186error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
187 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000188 // FIXME: Return the correct size. This requires looking at all the symbols
189 // in the same section as this symbol, and looking for either the next
190 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000191 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000192 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000193 if (error_code ec = getSection(symb->SectionNumber, Section))
194 return ec;
195 char Type;
196 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
197 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000198 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000199 Result = UnknownAddressOrSize;
200 else if (Section)
201 Result = Section->SizeOfRawData - symb->Value;
202 else
203 Result = 0;
204 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000205}
206
Michael J. Spencer25b15772011-06-25 17:55:23 +0000207error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
208 char &Result) const {
209 const coff_symbol *symb = toSymb(Symb);
210 StringRef name;
211 if (error_code ec = getSymbolName(Symb, name))
212 return ec;
213 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000214 .StartsWith(".debug", 'N')
215 .StartsWith(".sxdata", 'N')
216 .Default('?');
217
Michael J. Spencer25b15772011-06-25 17:55:23 +0000218 if (ret != '?') {
219 Result = ret;
220 return object_error::success;
221 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000222
223 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000224 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000225 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000226 if (error_code ec = getSection(symb->SectionNumber, Section))
227 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000228 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000229 }
230
231 switch (symb->SectionNumber) {
232 case COFF::IMAGE_SYM_UNDEFINED:
233 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000234 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
235 Result = 'w';
236 return object_error::success; // Don't do ::toupper.
Michael J. Spencer11ba26d2011-11-16 23:36:12 +0000237 } else if (symb->Value != 0) // Check for common symbols.
238 ret = 'c';
239 else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000240 ret = 'u';
241 break;
242 case COFF::IMAGE_SYM_ABSOLUTE:
243 ret = 'a';
244 break;
245 case COFF::IMAGE_SYM_DEBUG:
246 ret = 'n';
247 break;
248 default:
249 // Check section type.
250 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
251 ret = 't';
252 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
253 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
254 ret = 'r';
255 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
256 ret = 'd';
257 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
258 ret = 'b';
259 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
260 ret = 'i';
261
262 // Check for section symbol.
263 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
264 && symb->Value == 0)
265 ret = 's';
266 }
267
268 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
269 ret = ::toupper(ret);
270
Michael J. Spencer25b15772011-06-25 17:55:23 +0000271 Result = ret;
272 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000273}
274
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000275error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
276 section_iterator &Result) const {
277 const coff_symbol *symb = toSymb(Symb);
278 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
279 Result = end_sections();
280 else {
Daniel Dunbara483fc82011-11-28 22:19:32 +0000281 const coff_section *sec = 0;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000282 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
283 DataRefImpl Sec;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000284 std::memset(&Sec, 0, sizeof(Sec));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000285 Sec.p = reinterpret_cast<uintptr_t>(sec);
286 Result = section_iterator(SectionRef(Sec, this));
287 }
288 return object_error::success;
289}
290
Michael J. Spencer25b15772011-06-25 17:55:23 +0000291error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
292 SectionRef &Result) const {
293 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000294 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000295 Sec.p = reinterpret_cast<uintptr_t>(sec);
296 Result = SectionRef(Sec, this);
297 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000298}
299
Michael J. Spencer25b15772011-06-25 17:55:23 +0000300error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
301 StringRef &Result) const {
302 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000303 StringRef name;
304 if (sec->Name[7] == 0)
305 // Null terminated, let ::strlen figure out the length.
306 name = sec->Name;
307 else
308 // Not null terminated, use all 8 bytes.
309 name = StringRef(sec->Name, 8);
310
311 // Check for string table entry. First byte is '/'.
312 if (name[0] == '/') {
313 uint32_t Offset;
Michael J. Spencer33a18052012-03-15 17:49:29 +0000314 if (name.substr(1).getAsInteger(10, Offset))
315 return object_error::parse_failed;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000316 if (error_code ec = getString(Offset, name))
317 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000318 }
319
Michael J. Spencer25b15772011-06-25 17:55:23 +0000320 Result = name;
321 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000322}
323
Michael J. Spencer25b15772011-06-25 17:55:23 +0000324error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
325 uint64_t &Result) const {
326 const coff_section *sec = toSec(Sec);
327 Result = sec->VirtualAddress;
328 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000329}
330
Michael J. Spencer25b15772011-06-25 17:55:23 +0000331error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
332 uint64_t &Result) const {
333 const coff_section *sec = toSec(Sec);
334 Result = sec->SizeOfRawData;
335 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000336}
337
Michael J. Spencer25b15772011-06-25 17:55:23 +0000338error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
339 StringRef &Result) const {
340 const coff_section *sec = toSec(Sec);
341 // The only thing that we need to verify is that the contents is contained
342 // within the file bounds. We don't need to make sure it doesn't cover other
343 // data, as there's nothing that says that is not allowed.
344 uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData;
345 uintptr_t con_end = con_start + sec->SizeOfRawData;
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000346 if (con_end > uintptr_t(Data->getBufferEnd()))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000347 return object_error::parse_failed;
348 Result = StringRef(reinterpret_cast<const char*>(con_start),
349 sec->SizeOfRawData);
350 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000351}
352
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000353error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
354 uint64_t &Res) const {
355 const coff_section *sec = toSec(Sec);
356 if (!sec)
357 return object_error::parse_failed;
358 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
359 return object_error::success;
360}
361
Michael J. Spencer25b15772011-06-25 17:55:23 +0000362error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
363 bool &Result) const {
364 const coff_section *sec = toSec(Sec);
365 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
366 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000367}
368
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000369error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
370 bool &Result) const {
371 const coff_section *sec = toSec(Sec);
372 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
373 return object_error::success;
374}
375
376error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
377 bool &Result) const {
378 const coff_section *sec = toSec(Sec);
379 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
380 return object_error::success;
381}
382
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000383error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
384 DataRefImpl Symb,
385 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000386 const coff_section *sec = toSec(Sec);
387 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbara483fc82011-11-28 22:19:32 +0000388 const coff_section *symb_sec = 0;
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000389 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
390 if (symb_sec == sec)
391 Result = true;
392 else
393 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000394 return object_error::success;
395}
396
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000397relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
398 const coff_section *sec = toSec(Sec);
399 DataRefImpl ret;
400 std::memset(&ret, 0, sizeof(ret));
401 if (sec->NumberOfRelocations == 0)
402 ret.p = 0;
403 else
404 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
405
406 return relocation_iterator(RelocationRef(ret, this));
407}
408
409relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
410 const coff_section *sec = toSec(Sec);
411 DataRefImpl ret;
412 std::memset(&ret, 0, sizeof(ret));
413 if (sec->NumberOfRelocations == 0)
414 ret.p = 0;
415 else
416 ret.p = reinterpret_cast<uintptr_t>(
417 reinterpret_cast<const coff_relocation*>(
418 base() + sec->PointerToRelocations)
419 + sec->NumberOfRelocations);
420
421 return relocation_iterator(RelocationRef(ret, this));
422}
423
Michael J. Spencer001c9202011-06-25 17:54:50 +0000424COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
David Meyer6f9489a2012-03-09 20:41:57 +0000425 : ObjectFile(Binary::ID_COFF, Object, ec)
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000426 , Header(0)
427 , SectionTable(0)
428 , SymbolTable(0)
429 , StringTable(0)
430 , StringTableSize(0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000431 // Check that we at least have enough room for a header.
432 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000433
Michael J. Spencer25b15772011-06-25 17:55:23 +0000434 // The actual starting location of the COFF header in the file. This can be
435 // non-zero in PE/COFF files.
436 uint64_t HeaderStart = 0;
Eric Christopher539d8d82011-04-03 22:53:19 +0000437
Michael J. Spencer25b15772011-06-25 17:55:23 +0000438 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000439 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000440 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
441 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000442 if (!checkSize(Data, ec, 0x3c + 8)) return;
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000443 HeaderStart = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000444 // Check the PE header. ("PE\0\0")
Benjamin Kramer3209a032011-07-05 20:28:00 +0000445 if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000446 ec = object_error::parse_failed;
447 return;
448 }
449 HeaderStart += 4; // Skip the PE Header.
Eric Christopher539d8d82011-04-03 22:53:19 +0000450 }
451
Michael J. Spencer25b15772011-06-25 17:55:23 +0000452 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
453 if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
454 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000455
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000456 SectionTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000457 reinterpret_cast<const coff_section *>( base()
Michael J. Spencer25b15772011-06-25 17:55:23 +0000458 + HeaderStart
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000459 + sizeof(coff_file_header)
460 + Header->SizeOfOptionalHeader);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000461 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
462 Header->NumberOfSections * sizeof(coff_section)))
463 return;
464
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000465 if (Header->PointerToSymbolTable != 0) {
466 SymbolTable =
467 reinterpret_cast<const coff_symbol *>(base()
468 + Header->PointerToSymbolTable);
469 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
470 Header->NumberOfSymbols * sizeof(coff_symbol)))
471 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000472
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000473 // Find string table.
474 StringTable = reinterpret_cast<const char *>(base())
475 + Header->PointerToSymbolTable
476 + Header->NumberOfSymbols * sizeof(coff_symbol);
477 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
478 return;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000479
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000480 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
481 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
482 return;
483 // Check that the string table is null terminated if has any in it.
484 if (StringTableSize < 4
485 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
486 ec = object_error::parse_failed;
487 return;
488 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000489 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000490
Michael J. Spencer25b15772011-06-25 17:55:23 +0000491 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000492}
493
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000494symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000495 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000496 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000497 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
498 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000499}
500
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000501symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000502 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000503 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000504 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000505 ret.p = reinterpret_cast<intptr_t>(StringTable);
506 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000507}
508
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000509symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
510 // TODO: implement
511 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
512}
513
514symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
515 // TODO: implement
516 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
517}
518
David Meyer5c2b4ea2012-03-01 01:36:50 +0000519library_iterator COFFObjectFile::begin_libraries_needed() const {
520 // TODO: implement
521 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
522}
523
524library_iterator COFFObjectFile::end_libraries_needed() const {
525 // TODO: implement
526 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
527}
528
David Meyer97f77872012-03-01 22:19:54 +0000529StringRef COFFObjectFile::getLoadName() const {
530 // COFF does not have this field.
531 return "";
532}
533
534
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000535section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000536 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000537 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000538 ret.p = reinterpret_cast<intptr_t>(SectionTable);
539 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000540}
541
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000542section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000543 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000544 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000545 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
546 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000547}
548
549uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000550 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000551}
552
553StringRef COFFObjectFile::getFileFormatName() const {
554 switch(Header->Machine) {
555 case COFF::IMAGE_FILE_MACHINE_I386:
556 return "COFF-i386";
557 case COFF::IMAGE_FILE_MACHINE_AMD64:
558 return "COFF-x86-64";
559 default:
560 return "COFF-<unknown arch>";
561 }
562}
563
564unsigned COFFObjectFile::getArch() const {
565 switch(Header->Machine) {
566 case COFF::IMAGE_FILE_MACHINE_I386:
567 return Triple::x86;
568 case COFF::IMAGE_FILE_MACHINE_AMD64:
569 return Triple::x86_64;
570 default:
571 return Triple::UnknownArch;
572 }
573}
574
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000575error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
576 Res = Header;
577 return object_error::success;
578}
579
Michael J. Spencer25b15772011-06-25 17:55:23 +0000580error_code COFFObjectFile::getSection(int32_t index,
581 const coff_section *&Result) const {
582 // Check for special index values.
583 if (index == COFF::IMAGE_SYM_UNDEFINED ||
584 index == COFF::IMAGE_SYM_ABSOLUTE ||
585 index == COFF::IMAGE_SYM_DEBUG)
586 Result = NULL;
587 else if (index > 0 && index <= Header->NumberOfSections)
588 // We already verified the section table data, so no need to check again.
589 Result = SectionTable + (index - 1);
590 else
591 return object_error::parse_failed;
592 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000593}
594
Michael J. Spencer25b15772011-06-25 17:55:23 +0000595error_code COFFObjectFile::getString(uint32_t offset,
596 StringRef &Result) const {
597 if (StringTableSize <= 4)
598 // Tried to get a string from an empty string table.
599 return object_error::parse_failed;
600 if (offset >= StringTableSize)
601 return object_error::unexpected_eof;
602 Result = StringRef(StringTable + offset);
603 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000604}
605
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000606error_code COFFObjectFile::getSymbol(uint32_t index,
607 const coff_symbol *&Result) const {
Michael J. Spencer7c246652011-10-18 19:51:36 +0000608 if (index < Header->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000609 Result = SymbolTable + index;
610 else
611 return object_error::parse_failed;
612 return object_error::success;
613}
614
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000615error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
616 StringRef &Res) const {
617 // Check for string table entry. First 4 bytes are 0.
618 if (symbol->Name.Offset.Zeroes == 0) {
619 uint32_t Offset = symbol->Name.Offset.Offset;
620 if (error_code ec = getString(Offset, Res))
621 return ec;
622 return object_error::success;
623 }
624
625 if (symbol->Name.ShortName[7] == 0)
626 // Null terminated, let ::strlen figure out the length.
627 Res = StringRef(symbol->Name.ShortName);
628 else
629 // Not null terminated, use all 8 bytes.
630 Res = StringRef(symbol->Name.ShortName, 8);
631 return object_error::success;
632}
633
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000634const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000635 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000636}
637error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
638 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000639 Rel.p = reinterpret_cast<uintptr_t>(
640 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000641 Res = RelocationRef(Rel, this);
642 return object_error::success;
643}
644error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
645 uint64_t &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000646 Res = toRel(Rel)->VirtualAddress;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000647 return object_error::success;
648}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000649error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
650 uint64_t &Res) const {
651 Res = toRel(Rel)->VirtualAddress;
652 return object_error::success;
653}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000654error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
655 SymbolRef &Res) const {
656 const coff_relocation* R = toRel(Rel);
657 DataRefImpl Symb;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000658 std::memset(&Symb, 0, sizeof(Symb));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000659 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
660 Res = SymbolRef(Symb, this);
661 return object_error::success;
662}
663error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000664 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000665 const coff_relocation* R = toRel(Rel);
666 Res = R->Type;
667 return object_error::success;
668}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000669
670#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
671 case COFF::enum: res = #enum; break;
672
673error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
674 SmallVectorImpl<char> &Result) const {
675 const coff_relocation *reloc = toRel(Rel);
676 StringRef res;
677 switch (Header->Machine) {
678 case COFF::IMAGE_FILE_MACHINE_AMD64:
679 switch (reloc->Type) {
680 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
681 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
682 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
683 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
684 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
685 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
686 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
687 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
688 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
689 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
690 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
691 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
692 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
693 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
694 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
695 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
696 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
697 default:
698 res = "Unknown";
699 }
700 break;
701 case COFF::IMAGE_FILE_MACHINE_I386:
702 switch (reloc->Type) {
703 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
704 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
705 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
706 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
707 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
708 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
709 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
710 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
711 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
712 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
713 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
714 default:
715 res = "Unknown";
716 }
717 break;
718 default:
719 res = "Unknown";
720 }
721 Result.append(res.begin(), res.end());
722 return object_error::success;
723}
724
725#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
726
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000727error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
728 int64_t &Res) const {
729 Res = 0;
730 return object_error::success;
731}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000732error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
733 SmallVectorImpl<char> &Result) const {
734 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000735 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000736 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
737 DataRefImpl sym;
738 ::memset(&sym, 0, sizeof(sym));
739 sym.p = reinterpret_cast<uintptr_t>(symb);
740 StringRef symname;
741 if (error_code ec = getSymbolName(sym, symname)) return ec;
742 Result.append(symname.begin(), symname.end());
743 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000744}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000745
David Meyer5c2b4ea2012-03-01 01:36:50 +0000746error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
747 LibraryRef &Result) const {
748 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
749}
750
751error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
752 StringRef &Result) const {
753 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
754}
755
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000756namespace llvm {
757
758 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000759 error_code ec;
760 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000761 }
762
763} // end namespace llvm