blob: 6fdb263d8e36102e72061c51f04762ad20b3e1ab [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
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000104error_code COFFObjectFile::getSymbolOffset(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)
116 Result = Section->VirtualAddress + symb->Value;
117 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)
134 Result = reinterpret_cast<uintptr_t>(base() +
135 Section->PointerToRawData +
136 symb->Value);
137 else
138 Result = reinterpret_cast<uintptr_t>(base() + symb->Value);
139 return object_error::success;
140}
141
142error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000143 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000144 const coff_symbol *symb = toSymb(Symb);
145 Result = SymbolRef::ST_Other;
146 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
147 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
148 Result = SymbolRef::ST_External;
149 } else {
150 if (symb->Type.ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
151 Result = SymbolRef::ST_Function;
152 } else {
153 char Type;
154 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
155 return ec;
156 if (Type == 'r' || Type == 'R') {
157 Result = SymbolRef::ST_Data;
158 }
159 }
160 }
161 return object_error::success;
162}
163
164error_code COFFObjectFile::isSymbolGlobal(DataRefImpl Symb,
165 bool &Result) const {
166 const coff_symbol *symb = toSymb(Symb);
167 Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL);
168 return object_error::success;
169}
170
Michael J. Spencer25b15772011-06-25 17:55:23 +0000171error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
172 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000173 // FIXME: Return the correct size. This requires looking at all the symbols
174 // in the same section as this symbol, and looking for either the next
175 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000176 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000177 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000178 if (error_code ec = getSection(symb->SectionNumber, Section))
179 return ec;
180 char Type;
181 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
182 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000183 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000184 Result = UnknownAddressOrSize;
185 else if (Section)
186 Result = Section->SizeOfRawData - symb->Value;
187 else
188 Result = 0;
189 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000190}
191
Michael J. Spencer25b15772011-06-25 17:55:23 +0000192error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
193 char &Result) const {
194 const coff_symbol *symb = toSymb(Symb);
195 StringRef name;
196 if (error_code ec = getSymbolName(Symb, name))
197 return ec;
198 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000199 .StartsWith(".debug", 'N')
200 .StartsWith(".sxdata", 'N')
201 .Default('?');
202
Michael J. Spencer25b15772011-06-25 17:55:23 +0000203 if (ret != '?') {
204 Result = ret;
205 return object_error::success;
206 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000207
208 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000209 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000210 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000211 if (error_code ec = getSection(symb->SectionNumber, Section))
212 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000213 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000214 }
215
216 switch (symb->SectionNumber) {
217 case COFF::IMAGE_SYM_UNDEFINED:
218 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000219 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
220 Result = 'w';
221 return object_error::success; // Don't do ::toupper.
222 } else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000223 ret = 'u';
224 break;
225 case COFF::IMAGE_SYM_ABSOLUTE:
226 ret = 'a';
227 break;
228 case COFF::IMAGE_SYM_DEBUG:
229 ret = 'n';
230 break;
231 default:
232 // Check section type.
233 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
234 ret = 't';
235 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
236 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
237 ret = 'r';
238 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
239 ret = 'd';
240 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
241 ret = 'b';
242 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
243 ret = 'i';
244
245 // Check for section symbol.
246 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
247 && symb->Value == 0)
248 ret = 's';
249 }
250
251 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
252 ret = ::toupper(ret);
253
Michael J. Spencer25b15772011-06-25 17:55:23 +0000254 Result = ret;
255 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000256}
257
Michael J. Spencer25b15772011-06-25 17:55:23 +0000258error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb,
259 bool &Result) const {
260 Result = false;
261 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000262}
263
Michael J. Spencer25b15772011-06-25 17:55:23 +0000264error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
265 SectionRef &Result) const {
266 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000267 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000268 Sec.p = reinterpret_cast<uintptr_t>(sec);
269 Result = SectionRef(Sec, this);
270 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000271}
272
Michael J. Spencer25b15772011-06-25 17:55:23 +0000273error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
274 StringRef &Result) const {
275 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000276 StringRef name;
277 if (sec->Name[7] == 0)
278 // Null terminated, let ::strlen figure out the length.
279 name = sec->Name;
280 else
281 // Not null terminated, use all 8 bytes.
282 name = StringRef(sec->Name, 8);
283
284 // Check for string table entry. First byte is '/'.
285 if (name[0] == '/') {
286 uint32_t Offset;
Eric Christopher539d8d82011-04-03 22:53:19 +0000287 name.substr(1).getAsInteger(10, Offset);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000288 if (error_code ec = getString(Offset, name))
289 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000290 }
291
Michael J. Spencer25b15772011-06-25 17:55:23 +0000292 Result = name;
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::getSectionAddress(DataRefImpl Sec,
297 uint64_t &Result) const {
298 const coff_section *sec = toSec(Sec);
299 Result = sec->VirtualAddress;
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::getSectionSize(DataRefImpl Sec,
304 uint64_t &Result) const {
305 const coff_section *sec = toSec(Sec);
306 Result = sec->SizeOfRawData;
307 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000308}
309
Michael J. Spencer25b15772011-06-25 17:55:23 +0000310error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
311 StringRef &Result) const {
312 const coff_section *sec = toSec(Sec);
313 // The only thing that we need to verify is that the contents is contained
314 // within the file bounds. We don't need to make sure it doesn't cover other
315 // data, as there's nothing that says that is not allowed.
316 uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData;
317 uintptr_t con_end = con_start + sec->SizeOfRawData;
318 if (con_end >= uintptr_t(Data->getBufferEnd()))
319 return object_error::parse_failed;
320 Result = StringRef(reinterpret_cast<const char*>(con_start),
321 sec->SizeOfRawData);
322 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000323}
324
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000325error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
326 uint64_t &Res) const {
327 const coff_section *sec = toSec(Sec);
328 if (!sec)
329 return object_error::parse_failed;
330 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
331 return object_error::success;
332}
333
Michael J. Spencer25b15772011-06-25 17:55:23 +0000334error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
335 bool &Result) const {
336 const coff_section *sec = toSec(Sec);
337 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
338 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000339}
340
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000341error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
342 bool &Result) const {
343 const coff_section *sec = toSec(Sec);
344 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
345 return object_error::success;
346}
347
348error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
349 bool &Result) const {
350 const coff_section *sec = toSec(Sec);
351 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
352 return object_error::success;
353}
354
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000355error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
356 DataRefImpl Symb,
357 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000358 const coff_section *sec = toSec(Sec);
359 const coff_symbol *symb = toSymb(Symb);
360 const coff_section *symb_sec;
361 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
362 if (symb_sec == sec)
363 Result = true;
364 else
365 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000366 return object_error::success;
367}
368
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000369relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
370 const coff_section *sec = toSec(Sec);
371 DataRefImpl ret;
372 std::memset(&ret, 0, sizeof(ret));
373 if (sec->NumberOfRelocations == 0)
374 ret.p = 0;
375 else
376 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
377
378 return relocation_iterator(RelocationRef(ret, this));
379}
380
381relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
382 const coff_section *sec = toSec(Sec);
383 DataRefImpl ret;
384 std::memset(&ret, 0, sizeof(ret));
385 if (sec->NumberOfRelocations == 0)
386 ret.p = 0;
387 else
388 ret.p = reinterpret_cast<uintptr_t>(
389 reinterpret_cast<const coff_relocation*>(
390 base() + sec->PointerToRelocations)
391 + sec->NumberOfRelocations);
392
393 return relocation_iterator(RelocationRef(ret, this));
394}
395
Michael J. Spencer001c9202011-06-25 17:54:50 +0000396COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
397 : ObjectFile(Binary::isCOFF, Object, ec) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000398 // Check that we at least have enough room for a header.
399 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000400
Michael J. Spencer25b15772011-06-25 17:55:23 +0000401 // The actual starting location of the COFF header in the file. This can be
402 // non-zero in PE/COFF files.
403 uint64_t HeaderStart = 0;
Eric Christopher539d8d82011-04-03 22:53:19 +0000404
Michael J. Spencer25b15772011-06-25 17:55:23 +0000405 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000406 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000407 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
408 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000409 if (!checkSize(Data, ec, 0x3c + 8)) return;
410 HeaderStart += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c);
411 // Check the PE header. ("PE\0\0")
Benjamin Kramer3209a032011-07-05 20:28:00 +0000412 if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000413 ec = object_error::parse_failed;
414 return;
415 }
416 HeaderStart += 4; // Skip the PE Header.
Eric Christopher539d8d82011-04-03 22:53:19 +0000417 }
418
Michael J. Spencer25b15772011-06-25 17:55:23 +0000419 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
420 if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
421 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000422
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000423 SectionTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000424 reinterpret_cast<const coff_section *>( base()
Michael J. Spencer25b15772011-06-25 17:55:23 +0000425 + HeaderStart
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000426 + sizeof(coff_file_header)
427 + Header->SizeOfOptionalHeader);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000428 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
429 Header->NumberOfSections * sizeof(coff_section)))
430 return;
431
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000432 SymbolTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000433 reinterpret_cast<const coff_symbol *>(base()
Michael J. Spencer25b15772011-06-25 17:55:23 +0000434 + Header->PointerToSymbolTable);
435 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
436 Header->NumberOfSymbols * sizeof(coff_symbol)))
437 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000438
439 // Find string table.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000440 StringTable = reinterpret_cast<const char *>(base())
Michael J. Spencer25b15772011-06-25 17:55:23 +0000441 + Header->PointerToSymbolTable
442 + Header->NumberOfSymbols * sizeof(coff_symbol);
443 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
444 return;
445
446 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
447 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
448 return;
449 // Check that the string table is null terminated if has any in it.
450 if (StringTableSize < 4
451 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
452 ec = object_error::parse_failed;
453 return;
454 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000455
Michael J. Spencer25b15772011-06-25 17:55:23 +0000456 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000457}
458
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000459symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000460 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000461 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000462 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
463 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000464}
465
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000466symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000467 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000468 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000469 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000470 ret.p = reinterpret_cast<intptr_t>(StringTable);
471 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000472}
473
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000474section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000475 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000476 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000477 ret.p = reinterpret_cast<intptr_t>(SectionTable);
478 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000479}
480
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000481section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000482 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000483 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000484 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
485 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000486}
487
488uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000489 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000490}
491
492StringRef COFFObjectFile::getFileFormatName() const {
493 switch(Header->Machine) {
494 case COFF::IMAGE_FILE_MACHINE_I386:
495 return "COFF-i386";
496 case COFF::IMAGE_FILE_MACHINE_AMD64:
497 return "COFF-x86-64";
498 default:
499 return "COFF-<unknown arch>";
500 }
501}
502
503unsigned COFFObjectFile::getArch() const {
504 switch(Header->Machine) {
505 case COFF::IMAGE_FILE_MACHINE_I386:
506 return Triple::x86;
507 case COFF::IMAGE_FILE_MACHINE_AMD64:
508 return Triple::x86_64;
509 default:
510 return Triple::UnknownArch;
511 }
512}
513
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000514error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
515 Res = Header;
516 return object_error::success;
517}
518
Michael J. Spencer25b15772011-06-25 17:55:23 +0000519error_code COFFObjectFile::getSection(int32_t index,
520 const coff_section *&Result) const {
521 // Check for special index values.
522 if (index == COFF::IMAGE_SYM_UNDEFINED ||
523 index == COFF::IMAGE_SYM_ABSOLUTE ||
524 index == COFF::IMAGE_SYM_DEBUG)
525 Result = NULL;
526 else if (index > 0 && index <= Header->NumberOfSections)
527 // We already verified the section table data, so no need to check again.
528 Result = SectionTable + (index - 1);
529 else
530 return object_error::parse_failed;
531 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000532}
533
Michael J. Spencer25b15772011-06-25 17:55:23 +0000534error_code COFFObjectFile::getString(uint32_t offset,
535 StringRef &Result) const {
536 if (StringTableSize <= 4)
537 // Tried to get a string from an empty string table.
538 return object_error::parse_failed;
539 if (offset >= StringTableSize)
540 return object_error::unexpected_eof;
541 Result = StringRef(StringTable + offset);
542 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000543}
544
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000545error_code COFFObjectFile::getSymbol(uint32_t index,
546 const coff_symbol *&Result) const {
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000547 if (index >= 0 && index < Header->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000548 Result = SymbolTable + index;
549 else
550 return object_error::parse_failed;
551 return object_error::success;
552}
553
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000554error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
555 StringRef &Res) const {
556 // Check for string table entry. First 4 bytes are 0.
557 if (symbol->Name.Offset.Zeroes == 0) {
558 uint32_t Offset = symbol->Name.Offset.Offset;
559 if (error_code ec = getString(Offset, Res))
560 return ec;
561 return object_error::success;
562 }
563
564 if (symbol->Name.ShortName[7] == 0)
565 // Null terminated, let ::strlen figure out the length.
566 Res = StringRef(symbol->Name.ShortName);
567 else
568 // Not null terminated, use all 8 bytes.
569 Res = StringRef(symbol->Name.ShortName, 8);
570 return object_error::success;
571}
572
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000573const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000574 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000575}
576error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
577 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000578 Rel.p = reinterpret_cast<uintptr_t>(
579 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000580 Res = RelocationRef(Rel, this);
581 return object_error::success;
582}
583error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
584 uint64_t &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000585 Res = toRel(Rel)->VirtualAddress;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000586 return object_error::success;
587}
588error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
589 SymbolRef &Res) const {
590 const coff_relocation* R = toRel(Rel);
591 DataRefImpl Symb;
592 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
593 Res = SymbolRef(Symb, this);
594 return object_error::success;
595}
596error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
597 uint32_t &Res) const {
598 const coff_relocation* R = toRel(Rel);
599 Res = R->Type;
600 return object_error::success;
601}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000602
603#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
604 case COFF::enum: res = #enum; break;
605
606error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
607 SmallVectorImpl<char> &Result) const {
608 const coff_relocation *reloc = toRel(Rel);
609 StringRef res;
610 switch (Header->Machine) {
611 case COFF::IMAGE_FILE_MACHINE_AMD64:
612 switch (reloc->Type) {
613 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
614 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
615 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
616 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
617 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
618 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
619 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
620 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
621 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
622 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
623 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
624 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
625 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
626 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
627 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
628 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
629 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
630 default:
631 res = "Unknown";
632 }
633 break;
634 case COFF::IMAGE_FILE_MACHINE_I386:
635 switch (reloc->Type) {
636 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
637 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
638 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
639 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
640 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
641 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
642 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
643 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
644 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
645 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
646 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
647 default:
648 res = "Unknown";
649 }
650 break;
651 default:
652 res = "Unknown";
653 }
654 Result.append(res.begin(), res.end());
655 return object_error::success;
656}
657
658#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
659
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000660error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
661 int64_t &Res) const {
662 Res = 0;
663 return object_error::success;
664}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000665error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
666 SmallVectorImpl<char> &Result) const {
667 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000668 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000669 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
670 DataRefImpl sym;
671 ::memset(&sym, 0, sizeof(sym));
672 sym.p = reinterpret_cast<uintptr_t>(symb);
673 StringRef symname;
674 if (error_code ec = getSymbolName(sym, symname)) return ec;
675 Result.append(symname.begin(), symname.end());
676 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000677}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000678
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000679namespace llvm {
680
681 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000682 error_code ec;
683 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000684 }
685
686} // end namespace llvm