blob: 8344ca071d34faa42d57e0e91d2d1ef5f4780d0a [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 Malyshev9b247382011-11-27 10:12:52 +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 Malyshev9b247382011-11-27 10:12:52 +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 Malyshev9b247382011-11-27 10:12:52 +0000134 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000135 else
Danil Malyshev9b247382011-11-27 10:12:52 +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) {
146 Result = SymbolRef::ST_External;
147 } 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
162error_code COFFObjectFile::isSymbolGlobal(DataRefImpl Symb,
163 bool &Result) const {
164 const coff_symbol *symb = toSymb(Symb);
165 Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL);
166 return object_error::success;
167}
168
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000169error_code COFFObjectFile::isSymbolWeak(DataRefImpl Symb,
170 bool &Result) const {
171 const coff_symbol *symb = toSymb(Symb);
172 Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL);
173 return object_error::success;
174}
175
Michael J. Spencer25b15772011-06-25 17:55:23 +0000176error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
177 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000178 // FIXME: Return the correct size. This requires looking at all the symbols
179 // in the same section as this symbol, and looking for either the next
180 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000181 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000182 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000183 if (error_code ec = getSection(symb->SectionNumber, Section))
184 return ec;
185 char Type;
186 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
187 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000188 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000189 Result = UnknownAddressOrSize;
190 else if (Section)
191 Result = Section->SizeOfRawData - symb->Value;
192 else
193 Result = 0;
194 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000195}
196
Michael J. Spencer25b15772011-06-25 17:55:23 +0000197error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
198 char &Result) const {
199 const coff_symbol *symb = toSymb(Symb);
200 StringRef name;
201 if (error_code ec = getSymbolName(Symb, name))
202 return ec;
203 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000204 .StartsWith(".debug", 'N')
205 .StartsWith(".sxdata", 'N')
206 .Default('?');
207
Michael J. Spencer25b15772011-06-25 17:55:23 +0000208 if (ret != '?') {
209 Result = ret;
210 return object_error::success;
211 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000212
213 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000214 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000215 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000216 if (error_code ec = getSection(symb->SectionNumber, Section))
217 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000218 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000219 }
220
221 switch (symb->SectionNumber) {
222 case COFF::IMAGE_SYM_UNDEFINED:
223 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000224 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
225 Result = 'w';
226 return object_error::success; // Don't do ::toupper.
Michael J. Spencer11ba26d2011-11-16 23:36:12 +0000227 } else if (symb->Value != 0) // Check for common symbols.
228 ret = 'c';
229 else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000230 ret = 'u';
231 break;
232 case COFF::IMAGE_SYM_ABSOLUTE:
233 ret = 'a';
234 break;
235 case COFF::IMAGE_SYM_DEBUG:
236 ret = 'n';
237 break;
238 default:
239 // Check section type.
240 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
241 ret = 't';
242 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
243 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
244 ret = 'r';
245 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
246 ret = 'd';
247 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
248 ret = 'b';
249 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
250 ret = 'i';
251
252 // Check for section symbol.
253 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
254 && symb->Value == 0)
255 ret = 's';
256 }
257
258 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
259 ret = ::toupper(ret);
260
Michael J. Spencer25b15772011-06-25 17:55:23 +0000261 Result = ret;
262 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000263}
264
Michael J. Spencer25b15772011-06-25 17:55:23 +0000265error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb,
266 bool &Result) const {
267 Result = false;
268 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000269}
270
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000271error_code COFFObjectFile::isSymbolAbsolute(DataRefImpl Symb,
272 bool &Result) const {
273 const coff_symbol *symb = toSymb(Symb);
274 Result = symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE;
275 return object_error::success;
276}
277
278error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
279 section_iterator &Result) const {
280 const coff_symbol *symb = toSymb(Symb);
281 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
282 Result = end_sections();
283 else {
284 const coff_section *sec;
285 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
286 DataRefImpl Sec;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000287 std::memset(&Sec, 0, sizeof(Sec));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000288 Sec.p = reinterpret_cast<uintptr_t>(sec);
289 Result = section_iterator(SectionRef(Sec, this));
290 }
291 return object_error::success;
292}
293
Michael J. Spencer25b15772011-06-25 17:55:23 +0000294error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
295 SectionRef &Result) const {
296 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000297 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000298 Sec.p = reinterpret_cast<uintptr_t>(sec);
299 Result = SectionRef(Sec, this);
300 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000301}
302
Michael J. Spencer25b15772011-06-25 17:55:23 +0000303error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
304 StringRef &Result) const {
305 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000306 StringRef name;
307 if (sec->Name[7] == 0)
308 // Null terminated, let ::strlen figure out the length.
309 name = sec->Name;
310 else
311 // Not null terminated, use all 8 bytes.
312 name = StringRef(sec->Name, 8);
313
314 // Check for string table entry. First byte is '/'.
315 if (name[0] == '/') {
316 uint32_t Offset;
Eric Christopher539d8d82011-04-03 22:53:19 +0000317 name.substr(1).getAsInteger(10, Offset);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000318 if (error_code ec = getString(Offset, name))
319 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000320 }
321
Michael J. Spencer25b15772011-06-25 17:55:23 +0000322 Result = name;
323 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000324}
325
Michael J. Spencer25b15772011-06-25 17:55:23 +0000326error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
327 uint64_t &Result) const {
328 const coff_section *sec = toSec(Sec);
329 Result = sec->VirtualAddress;
330 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000331}
332
Michael J. Spencer25b15772011-06-25 17:55:23 +0000333error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
334 uint64_t &Result) const {
335 const coff_section *sec = toSec(Sec);
336 Result = sec->SizeOfRawData;
337 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000338}
339
Michael J. Spencer25b15772011-06-25 17:55:23 +0000340error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
341 StringRef &Result) const {
342 const coff_section *sec = toSec(Sec);
343 // The only thing that we need to verify is that the contents is contained
344 // within the file bounds. We don't need to make sure it doesn't cover other
345 // data, as there's nothing that says that is not allowed.
346 uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData;
347 uintptr_t con_end = con_start + sec->SizeOfRawData;
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000348 if (con_end > uintptr_t(Data->getBufferEnd()))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000349 return object_error::parse_failed;
350 Result = StringRef(reinterpret_cast<const char*>(con_start),
351 sec->SizeOfRawData);
352 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000353}
354
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000355error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
356 uint64_t &Res) const {
357 const coff_section *sec = toSec(Sec);
358 if (!sec)
359 return object_error::parse_failed;
360 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
361 return object_error::success;
362}
363
Michael J. Spencer25b15772011-06-25 17:55:23 +0000364error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
365 bool &Result) const {
366 const coff_section *sec = toSec(Sec);
367 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
368 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000369}
370
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000371error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
372 bool &Result) const {
373 const coff_section *sec = toSec(Sec);
374 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
375 return object_error::success;
376}
377
378error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
379 bool &Result) const {
380 const coff_section *sec = toSec(Sec);
381 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
382 return object_error::success;
383}
384
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000385error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
386 DataRefImpl Symb,
387 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000388 const coff_section *sec = toSec(Sec);
389 const coff_symbol *symb = toSymb(Symb);
390 const coff_section *symb_sec;
391 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
392 if (symb_sec == sec)
393 Result = true;
394 else
395 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000396 return object_error::success;
397}
398
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000399relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
400 const coff_section *sec = toSec(Sec);
401 DataRefImpl ret;
402 std::memset(&ret, 0, sizeof(ret));
403 if (sec->NumberOfRelocations == 0)
404 ret.p = 0;
405 else
406 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
407
408 return relocation_iterator(RelocationRef(ret, this));
409}
410
411relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
412 const coff_section *sec = toSec(Sec);
413 DataRefImpl ret;
414 std::memset(&ret, 0, sizeof(ret));
415 if (sec->NumberOfRelocations == 0)
416 ret.p = 0;
417 else
418 ret.p = reinterpret_cast<uintptr_t>(
419 reinterpret_cast<const coff_relocation*>(
420 base() + sec->PointerToRelocations)
421 + sec->NumberOfRelocations);
422
423 return relocation_iterator(RelocationRef(ret, this));
424}
425
Michael J. Spencer001c9202011-06-25 17:54:50 +0000426COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000427 : ObjectFile(Binary::isCOFF, Object, ec)
428 , Header(0)
429 , SectionTable(0)
430 , SymbolTable(0)
431 , StringTable(0)
432 , StringTableSize(0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000433 // Check that we at least have enough room for a header.
434 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000435
Michael J. Spencer25b15772011-06-25 17:55:23 +0000436 // The actual starting location of the COFF header in the file. This can be
437 // non-zero in PE/COFF files.
438 uint64_t HeaderStart = 0;
Eric Christopher539d8d82011-04-03 22:53:19 +0000439
Michael J. Spencer25b15772011-06-25 17:55:23 +0000440 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000441 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000442 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
443 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000444 if (!checkSize(Data, ec, 0x3c + 8)) return;
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000445 HeaderStart = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000446 // Check the PE header. ("PE\0\0")
Benjamin Kramer3209a032011-07-05 20:28:00 +0000447 if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000448 ec = object_error::parse_failed;
449 return;
450 }
451 HeaderStart += 4; // Skip the PE Header.
Eric Christopher539d8d82011-04-03 22:53:19 +0000452 }
453
Michael J. Spencer25b15772011-06-25 17:55:23 +0000454 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
455 if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
456 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000457
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000458 SectionTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000459 reinterpret_cast<const coff_section *>( base()
Michael J. Spencer25b15772011-06-25 17:55:23 +0000460 + HeaderStart
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000461 + sizeof(coff_file_header)
462 + Header->SizeOfOptionalHeader);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000463 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
464 Header->NumberOfSections * sizeof(coff_section)))
465 return;
466
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000467 if (Header->PointerToSymbolTable != 0) {
468 SymbolTable =
469 reinterpret_cast<const coff_symbol *>(base()
470 + Header->PointerToSymbolTable);
471 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
472 Header->NumberOfSymbols * sizeof(coff_symbol)))
473 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000474
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000475 // Find string table.
476 StringTable = reinterpret_cast<const char *>(base())
477 + Header->PointerToSymbolTable
478 + Header->NumberOfSymbols * sizeof(coff_symbol);
479 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
480 return;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000481
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000482 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
483 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
484 return;
485 // Check that the string table is null terminated if has any in it.
486 if (StringTableSize < 4
487 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
488 ec = object_error::parse_failed;
489 return;
490 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000491 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000492
Michael J. Spencer25b15772011-06-25 17:55:23 +0000493 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000494}
495
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000496symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000497 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000498 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000499 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
500 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000501}
502
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000503symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000504 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000505 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000506 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000507 ret.p = reinterpret_cast<intptr_t>(StringTable);
508 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000509}
510
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000511section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000512 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000513 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000514 ret.p = reinterpret_cast<intptr_t>(SectionTable);
515 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000516}
517
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000518section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000519 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000520 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000521 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
522 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000523}
524
525uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000526 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000527}
528
529StringRef COFFObjectFile::getFileFormatName() const {
530 switch(Header->Machine) {
531 case COFF::IMAGE_FILE_MACHINE_I386:
532 return "COFF-i386";
533 case COFF::IMAGE_FILE_MACHINE_AMD64:
534 return "COFF-x86-64";
535 default:
536 return "COFF-<unknown arch>";
537 }
538}
539
540unsigned COFFObjectFile::getArch() const {
541 switch(Header->Machine) {
542 case COFF::IMAGE_FILE_MACHINE_I386:
543 return Triple::x86;
544 case COFF::IMAGE_FILE_MACHINE_AMD64:
545 return Triple::x86_64;
546 default:
547 return Triple::UnknownArch;
548 }
549}
550
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000551error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
552 Res = Header;
553 return object_error::success;
554}
555
Michael J. Spencer25b15772011-06-25 17:55:23 +0000556error_code COFFObjectFile::getSection(int32_t index,
557 const coff_section *&Result) const {
558 // Check for special index values.
559 if (index == COFF::IMAGE_SYM_UNDEFINED ||
560 index == COFF::IMAGE_SYM_ABSOLUTE ||
561 index == COFF::IMAGE_SYM_DEBUG)
562 Result = NULL;
563 else if (index > 0 && index <= Header->NumberOfSections)
564 // We already verified the section table data, so no need to check again.
565 Result = SectionTable + (index - 1);
566 else
567 return object_error::parse_failed;
568 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000569}
570
Michael J. Spencer25b15772011-06-25 17:55:23 +0000571error_code COFFObjectFile::getString(uint32_t offset,
572 StringRef &Result) const {
573 if (StringTableSize <= 4)
574 // Tried to get a string from an empty string table.
575 return object_error::parse_failed;
576 if (offset >= StringTableSize)
577 return object_error::unexpected_eof;
578 Result = StringRef(StringTable + offset);
579 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000580}
581
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000582error_code COFFObjectFile::getSymbol(uint32_t index,
583 const coff_symbol *&Result) const {
Michael J. Spencer7c246652011-10-18 19:51:36 +0000584 if (index < Header->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000585 Result = SymbolTable + index;
586 else
587 return object_error::parse_failed;
588 return object_error::success;
589}
590
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000591error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
592 StringRef &Res) const {
593 // Check for string table entry. First 4 bytes are 0.
594 if (symbol->Name.Offset.Zeroes == 0) {
595 uint32_t Offset = symbol->Name.Offset.Offset;
596 if (error_code ec = getString(Offset, Res))
597 return ec;
598 return object_error::success;
599 }
600
601 if (symbol->Name.ShortName[7] == 0)
602 // Null terminated, let ::strlen figure out the length.
603 Res = StringRef(symbol->Name.ShortName);
604 else
605 // Not null terminated, use all 8 bytes.
606 Res = StringRef(symbol->Name.ShortName, 8);
607 return object_error::success;
608}
609
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000610const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000611 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000612}
613error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
614 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000615 Rel.p = reinterpret_cast<uintptr_t>(
616 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000617 Res = RelocationRef(Rel, this);
618 return object_error::success;
619}
620error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
621 uint64_t &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000622 Res = toRel(Rel)->VirtualAddress;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000623 return object_error::success;
624}
Danil Malyshev9b247382011-11-27 10:12:52 +0000625error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
626 uint64_t &Res) const {
627 Res = toRel(Rel)->VirtualAddress;
628 return object_error::success;
629}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000630error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
631 SymbolRef &Res) const {
632 const coff_relocation* R = toRel(Rel);
633 DataRefImpl Symb;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000634 std::memset(&Symb, 0, sizeof(Symb));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000635 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
636 Res = SymbolRef(Symb, this);
637 return object_error::success;
638}
639error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000640 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000641 const coff_relocation* R = toRel(Rel);
642 Res = R->Type;
643 return object_error::success;
644}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000645
646#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
647 case COFF::enum: res = #enum; break;
648
649error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
650 SmallVectorImpl<char> &Result) const {
651 const coff_relocation *reloc = toRel(Rel);
652 StringRef res;
653 switch (Header->Machine) {
654 case COFF::IMAGE_FILE_MACHINE_AMD64:
655 switch (reloc->Type) {
656 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
657 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
658 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
659 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
660 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
661 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
662 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
663 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
664 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
665 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
666 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
667 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
668 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
669 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
670 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
671 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
672 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
673 default:
674 res = "Unknown";
675 }
676 break;
677 case COFF::IMAGE_FILE_MACHINE_I386:
678 switch (reloc->Type) {
679 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
680 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
681 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
682 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
683 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
684 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
685 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
686 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
687 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
688 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
689 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
690 default:
691 res = "Unknown";
692 }
693 break;
694 default:
695 res = "Unknown";
696 }
697 Result.append(res.begin(), res.end());
698 return object_error::success;
699}
700
701#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
702
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000703error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
704 int64_t &Res) const {
705 Res = 0;
706 return object_error::success;
707}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000708error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
709 SmallVectorImpl<char> &Result) const {
710 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000711 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000712 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
713 DataRefImpl sym;
714 ::memset(&sym, 0, sizeof(sym));
715 sym.p = reinterpret_cast<uintptr_t>(symb);
716 StringRef symname;
717 if (error_code ec = getSymbolName(sym, symname)) return ec;
718 Result.append(symname.begin(), symname.end());
719 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000720}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000721
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000722namespace llvm {
723
724 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000725 error_code ec;
726 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000727 }
728
729} // end namespace llvm