blob: bf278785e68eb5d7172ec1051bbcb92658c5e6a1 [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) {
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
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 Meyerc46255a2012-02-28 23:47:53 +0000167 // TODO: Set SF_FormatSpecific.
168
169 // TODO: This are certainly too restrictive.
170 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
171 Result |= SymbolRef::SF_Global;
172
173 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
174 Result |= SymbolRef::SF_Weak;
175
176 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
177 Result |= SymbolRef::SF_Absolute;
178
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000179 return object_error::success;
180}
181
Michael J. Spencer25b15772011-06-25 17:55:23 +0000182error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
183 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000184 // FIXME: Return the correct size. This requires looking at all the symbols
185 // in the same section as this symbol, and looking for either the next
186 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000187 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000188 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000189 if (error_code ec = getSection(symb->SectionNumber, Section))
190 return ec;
191 char Type;
192 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
193 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000194 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000195 Result = UnknownAddressOrSize;
196 else if (Section)
197 Result = Section->SizeOfRawData - symb->Value;
198 else
199 Result = 0;
200 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000201}
202
Michael J. Spencer25b15772011-06-25 17:55:23 +0000203error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
204 char &Result) const {
205 const coff_symbol *symb = toSymb(Symb);
206 StringRef name;
207 if (error_code ec = getSymbolName(Symb, name))
208 return ec;
209 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000210 .StartsWith(".debug", 'N')
211 .StartsWith(".sxdata", 'N')
212 .Default('?');
213
Michael J. Spencer25b15772011-06-25 17:55:23 +0000214 if (ret != '?') {
215 Result = ret;
216 return object_error::success;
217 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000218
219 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000220 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000221 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000222 if (error_code ec = getSection(symb->SectionNumber, Section))
223 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000224 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000225 }
226
227 switch (symb->SectionNumber) {
228 case COFF::IMAGE_SYM_UNDEFINED:
229 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000230 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
231 Result = 'w';
232 return object_error::success; // Don't do ::toupper.
Michael J. Spencer11ba26d2011-11-16 23:36:12 +0000233 } else if (symb->Value != 0) // Check for common symbols.
234 ret = 'c';
235 else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000236 ret = 'u';
237 break;
238 case COFF::IMAGE_SYM_ABSOLUTE:
239 ret = 'a';
240 break;
241 case COFF::IMAGE_SYM_DEBUG:
242 ret = 'n';
243 break;
244 default:
245 // Check section type.
246 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
247 ret = 't';
248 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
249 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
250 ret = 'r';
251 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
252 ret = 'd';
253 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
254 ret = 'b';
255 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
256 ret = 'i';
257
258 // Check for section symbol.
259 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
260 && symb->Value == 0)
261 ret = 's';
262 }
263
264 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
265 ret = ::toupper(ret);
266
Michael J. Spencer25b15772011-06-25 17:55:23 +0000267 Result = ret;
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::getSymbolSection(DataRefImpl Symb,
272 section_iterator &Result) const {
273 const coff_symbol *symb = toSymb(Symb);
274 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
275 Result = end_sections();
276 else {
Daniel Dunbara483fc82011-11-28 22:19:32 +0000277 const coff_section *sec = 0;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000278 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
279 DataRefImpl Sec;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000280 std::memset(&Sec, 0, sizeof(Sec));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000281 Sec.p = reinterpret_cast<uintptr_t>(sec);
282 Result = section_iterator(SectionRef(Sec, this));
283 }
284 return object_error::success;
285}
286
Michael J. Spencer25b15772011-06-25 17:55:23 +0000287error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
288 SectionRef &Result) const {
289 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000290 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000291 Sec.p = reinterpret_cast<uintptr_t>(sec);
292 Result = SectionRef(Sec, this);
293 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000294}
295
Michael J. Spencer25b15772011-06-25 17:55:23 +0000296error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
297 StringRef &Result) const {
298 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000299 StringRef name;
300 if (sec->Name[7] == 0)
301 // Null terminated, let ::strlen figure out the length.
302 name = sec->Name;
303 else
304 // Not null terminated, use all 8 bytes.
305 name = StringRef(sec->Name, 8);
306
307 // Check for string table entry. First byte is '/'.
308 if (name[0] == '/') {
309 uint32_t Offset;
Eric Christopher539d8d82011-04-03 22:53:19 +0000310 name.substr(1).getAsInteger(10, Offset);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000311 if (error_code ec = getString(Offset, name))
312 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000313 }
314
Michael J. Spencer25b15772011-06-25 17:55:23 +0000315 Result = name;
316 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000317}
318
Michael J. Spencer25b15772011-06-25 17:55:23 +0000319error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
320 uint64_t &Result) const {
321 const coff_section *sec = toSec(Sec);
322 Result = sec->VirtualAddress;
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::getSectionSize(DataRefImpl Sec,
327 uint64_t &Result) const {
328 const coff_section *sec = toSec(Sec);
329 Result = sec->SizeOfRawData;
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::getSectionContents(DataRefImpl Sec,
334 StringRef &Result) const {
335 const coff_section *sec = toSec(Sec);
336 // The only thing that we need to verify is that the contents is contained
337 // within the file bounds. We don't need to make sure it doesn't cover other
338 // data, as there's nothing that says that is not allowed.
339 uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData;
340 uintptr_t con_end = con_start + sec->SizeOfRawData;
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000341 if (con_end > uintptr_t(Data->getBufferEnd()))
Michael J. Spencer25b15772011-06-25 17:55:23 +0000342 return object_error::parse_failed;
343 Result = StringRef(reinterpret_cast<const char*>(con_start),
344 sec->SizeOfRawData);
345 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000346}
347
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000348error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
349 uint64_t &Res) const {
350 const coff_section *sec = toSec(Sec);
351 if (!sec)
352 return object_error::parse_failed;
353 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
354 return object_error::success;
355}
356
Michael J. Spencer25b15772011-06-25 17:55:23 +0000357error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
358 bool &Result) const {
359 const coff_section *sec = toSec(Sec);
360 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
361 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000362}
363
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000364error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
365 bool &Result) const {
366 const coff_section *sec = toSec(Sec);
367 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
368 return object_error::success;
369}
370
371error_code COFFObjectFile::isSectionBSS(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
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000378error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
379 DataRefImpl Symb,
380 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000381 const coff_section *sec = toSec(Sec);
382 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbara483fc82011-11-28 22:19:32 +0000383 const coff_section *symb_sec = 0;
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000384 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
385 if (symb_sec == sec)
386 Result = true;
387 else
388 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000389 return object_error::success;
390}
391
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000392relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
393 const coff_section *sec = toSec(Sec);
394 DataRefImpl ret;
395 std::memset(&ret, 0, sizeof(ret));
396 if (sec->NumberOfRelocations == 0)
397 ret.p = 0;
398 else
399 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
400
401 return relocation_iterator(RelocationRef(ret, this));
402}
403
404relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
405 const coff_section *sec = toSec(Sec);
406 DataRefImpl ret;
407 std::memset(&ret, 0, sizeof(ret));
408 if (sec->NumberOfRelocations == 0)
409 ret.p = 0;
410 else
411 ret.p = reinterpret_cast<uintptr_t>(
412 reinterpret_cast<const coff_relocation*>(
413 base() + sec->PointerToRelocations)
414 + sec->NumberOfRelocations);
415
416 return relocation_iterator(RelocationRef(ret, this));
417}
418
Michael J. Spencer001c9202011-06-25 17:54:50 +0000419COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000420 : ObjectFile(Binary::isCOFF, Object, ec)
421 , Header(0)
422 , SectionTable(0)
423 , SymbolTable(0)
424 , StringTable(0)
425 , StringTableSize(0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000426 // Check that we at least have enough room for a header.
427 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000428
Michael J. Spencer25b15772011-06-25 17:55:23 +0000429 // The actual starting location of the COFF header in the file. This can be
430 // non-zero in PE/COFF files.
431 uint64_t HeaderStart = 0;
Eric Christopher539d8d82011-04-03 22:53:19 +0000432
Michael J. Spencer25b15772011-06-25 17:55:23 +0000433 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000434 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000435 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
436 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000437 if (!checkSize(Data, ec, 0x3c + 8)) return;
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000438 HeaderStart = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000439 // Check the PE header. ("PE\0\0")
Benjamin Kramer3209a032011-07-05 20:28:00 +0000440 if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000441 ec = object_error::parse_failed;
442 return;
443 }
444 HeaderStart += 4; // Skip the PE Header.
Eric Christopher539d8d82011-04-03 22:53:19 +0000445 }
446
Michael J. Spencer25b15772011-06-25 17:55:23 +0000447 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
448 if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
449 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000450
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000451 SectionTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000452 reinterpret_cast<const coff_section *>( base()
Michael J. Spencer25b15772011-06-25 17:55:23 +0000453 + HeaderStart
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000454 + sizeof(coff_file_header)
455 + Header->SizeOfOptionalHeader);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000456 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
457 Header->NumberOfSections * sizeof(coff_section)))
458 return;
459
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000460 if (Header->PointerToSymbolTable != 0) {
461 SymbolTable =
462 reinterpret_cast<const coff_symbol *>(base()
463 + Header->PointerToSymbolTable);
464 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
465 Header->NumberOfSymbols * sizeof(coff_symbol)))
466 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000467
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000468 // Find string table.
469 StringTable = reinterpret_cast<const char *>(base())
470 + Header->PointerToSymbolTable
471 + Header->NumberOfSymbols * sizeof(coff_symbol);
472 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
473 return;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000474
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000475 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
476 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
477 return;
478 // Check that the string table is null terminated if has any in it.
479 if (StringTableSize < 4
480 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
481 ec = object_error::parse_failed;
482 return;
483 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000484 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000485
Michael J. Spencer25b15772011-06-25 17:55:23 +0000486 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000487}
488
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000489symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000490 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000491 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000492 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
493 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000494}
495
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000496symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000497 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000498 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000499 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000500 ret.p = reinterpret_cast<intptr_t>(StringTable);
501 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000502}
503
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000504symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
505 // TODO: implement
506 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
507}
508
509symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
510 // TODO: implement
511 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
512}
513
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000514section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000515 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000516 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000517 ret.p = reinterpret_cast<intptr_t>(SectionTable);
518 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000519}
520
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000521section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000522 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000523 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000524 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
525 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000526}
527
528uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000529 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000530}
531
532StringRef COFFObjectFile::getFileFormatName() const {
533 switch(Header->Machine) {
534 case COFF::IMAGE_FILE_MACHINE_I386:
535 return "COFF-i386";
536 case COFF::IMAGE_FILE_MACHINE_AMD64:
537 return "COFF-x86-64";
538 default:
539 return "COFF-<unknown arch>";
540 }
541}
542
543unsigned COFFObjectFile::getArch() const {
544 switch(Header->Machine) {
545 case COFF::IMAGE_FILE_MACHINE_I386:
546 return Triple::x86;
547 case COFF::IMAGE_FILE_MACHINE_AMD64:
548 return Triple::x86_64;
549 default:
550 return Triple::UnknownArch;
551 }
552}
553
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000554error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
555 Res = Header;
556 return object_error::success;
557}
558
Michael J. Spencer25b15772011-06-25 17:55:23 +0000559error_code COFFObjectFile::getSection(int32_t index,
560 const coff_section *&Result) const {
561 // Check for special index values.
562 if (index == COFF::IMAGE_SYM_UNDEFINED ||
563 index == COFF::IMAGE_SYM_ABSOLUTE ||
564 index == COFF::IMAGE_SYM_DEBUG)
565 Result = NULL;
566 else if (index > 0 && index <= Header->NumberOfSections)
567 // We already verified the section table data, so no need to check again.
568 Result = SectionTable + (index - 1);
569 else
570 return object_error::parse_failed;
571 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000572}
573
Michael J. Spencer25b15772011-06-25 17:55:23 +0000574error_code COFFObjectFile::getString(uint32_t offset,
575 StringRef &Result) const {
576 if (StringTableSize <= 4)
577 // Tried to get a string from an empty string table.
578 return object_error::parse_failed;
579 if (offset >= StringTableSize)
580 return object_error::unexpected_eof;
581 Result = StringRef(StringTable + offset);
582 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000583}
584
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000585error_code COFFObjectFile::getSymbol(uint32_t index,
586 const coff_symbol *&Result) const {
Michael J. Spencer7c246652011-10-18 19:51:36 +0000587 if (index < Header->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000588 Result = SymbolTable + index;
589 else
590 return object_error::parse_failed;
591 return object_error::success;
592}
593
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000594error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
595 StringRef &Res) const {
596 // Check for string table entry. First 4 bytes are 0.
597 if (symbol->Name.Offset.Zeroes == 0) {
598 uint32_t Offset = symbol->Name.Offset.Offset;
599 if (error_code ec = getString(Offset, Res))
600 return ec;
601 return object_error::success;
602 }
603
604 if (symbol->Name.ShortName[7] == 0)
605 // Null terminated, let ::strlen figure out the length.
606 Res = StringRef(symbol->Name.ShortName);
607 else
608 // Not null terminated, use all 8 bytes.
609 Res = StringRef(symbol->Name.ShortName, 8);
610 return object_error::success;
611}
612
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000613const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000614 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000615}
616error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
617 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000618 Rel.p = reinterpret_cast<uintptr_t>(
619 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000620 Res = RelocationRef(Rel, this);
621 return object_error::success;
622}
623error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
624 uint64_t &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000625 Res = toRel(Rel)->VirtualAddress;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000626 return object_error::success;
627}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000628error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
629 uint64_t &Res) const {
630 Res = toRel(Rel)->VirtualAddress;
631 return object_error::success;
632}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000633error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
634 SymbolRef &Res) const {
635 const coff_relocation* R = toRel(Rel);
636 DataRefImpl Symb;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000637 std::memset(&Symb, 0, sizeof(Symb));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000638 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
639 Res = SymbolRef(Symb, this);
640 return object_error::success;
641}
642error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000643 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000644 const coff_relocation* R = toRel(Rel);
645 Res = R->Type;
646 return object_error::success;
647}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000648
649#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
650 case COFF::enum: res = #enum; break;
651
652error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
653 SmallVectorImpl<char> &Result) const {
654 const coff_relocation *reloc = toRel(Rel);
655 StringRef res;
656 switch (Header->Machine) {
657 case COFF::IMAGE_FILE_MACHINE_AMD64:
658 switch (reloc->Type) {
659 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
660 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
661 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
662 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
663 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
664 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
665 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
666 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
667 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
668 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
669 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
670 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
671 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
672 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
673 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
674 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
675 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
676 default:
677 res = "Unknown";
678 }
679 break;
680 case COFF::IMAGE_FILE_MACHINE_I386:
681 switch (reloc->Type) {
682 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
683 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
684 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
685 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
686 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
687 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
688 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
689 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
690 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
691 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
692 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
693 default:
694 res = "Unknown";
695 }
696 break;
697 default:
698 res = "Unknown";
699 }
700 Result.append(res.begin(), res.end());
701 return object_error::success;
702}
703
704#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
705
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000706error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
707 int64_t &Res) const {
708 Res = 0;
709 return object_error::success;
710}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000711error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
712 SmallVectorImpl<char> &Result) const {
713 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000714 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000715 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
716 DataRefImpl sym;
717 ::memset(&sym, 0, sizeof(sym));
718 sym.p = reinterpret_cast<uintptr_t>(symb);
719 StringRef symname;
720 if (error_code ec = getSymbolName(sym, symname)) return ec;
721 Result.append(symname.begin(), symname.end());
722 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000723}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000724
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000725namespace llvm {
726
727 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000728 error_code ec;
729 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000730 }
731
732} // end namespace llvm