blob: b8ba90535eaf31a8c5c3422064175eb856072a5d [file] [log] [blame]
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +00001//===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the COFFObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer001c9202011-06-25 17:54:50 +000014#include "llvm/Object/COFF.h"
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +000015#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000016#include "llvm/ADT/SmallString.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Triple.h"
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000019
20using namespace llvm;
21using namespace object;
22
23namespace {
24using support::ulittle8_t;
25using support::ulittle16_t;
26using support::ulittle32_t;
27using support::little16_t;
28}
29
Michael J. Spencer25b15772011-06-25 17:55:23 +000030namespace {
31// Returns false if size is greater than the buffer size. And sets ec.
32bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
33 if (m->getBufferSize() < size) {
34 ec = object_error::unexpected_eof;
35 return false;
36 }
37 return true;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +000038}
39
Michael J. Spencer25b15772011-06-25 17:55:23 +000040// Returns false if any bytes in [addr, addr + size) fall outsize of m.
41bool checkAddr(const MemoryBuffer *m,
42 error_code &ec,
43 uintptr_t addr,
44 uint64_t size) {
45 if (addr + size < addr ||
46 addr + size < size ||
47 addr + size > uintptr_t(m->getBufferEnd())) {
48 ec = object_error::unexpected_eof;
49 return false;
50 }
51 return true;
52}
53}
54
55const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
56 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
57
58# ifndef NDEBUG
59 // Verify that the symbol points to a valid entry in the symbol table.
60 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
61 if (offset < Header->PointerToSymbolTable
62 || offset >= Header->PointerToSymbolTable
63 + (Header->NumberOfSymbols * sizeof(coff_symbol)))
64 report_fatal_error("Symbol was outside of symbol table.");
65
66 assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol)
67 == 0 && "Symbol did not point to the beginning of a symbol");
68# endif
69
70 return addr;
71}
72
73const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
74 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
75
76# ifndef NDEBUG
77 // Verify that the section points to a valid entry in the section table.
78 if (addr < SectionTable
79 || addr >= (SectionTable + Header->NumberOfSections))
80 report_fatal_error("Section was outside of section table.");
81
82 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
83 assert(offset % sizeof(coff_section) == 0 &&
84 "Section did not point to the beginning of a section");
85# endif
86
87 return addr;
88}
89
90error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
91 SymbolRef &Result) const {
92 const coff_symbol *symb = toSymb(Symb);
93 symb += 1 + symb->NumberOfAuxSymbols;
94 Symb.p = reinterpret_cast<uintptr_t>(symb);
95 Result = SymbolRef(Symb, this);
96 return object_error::success;
97}
98
99 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
100 StringRef &Result) const {
101 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000102 return getSymbolName(symb, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000103}
104
Danil Malyshevb0436a72011-11-29 17:40:10 +0000105error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000106 uint64_t &Result) const {
107 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000108 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000109 if (error_code ec = getSection(symb->SectionNumber, Section))
110 return ec;
111 char Type;
112 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
113 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000114 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000115 Result = UnknownAddressOrSize;
116 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000117 Result = Section->PointerToRawData + symb->Value;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000118 else
119 Result = symb->Value;
120 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000121}
122
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000123error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
124 uint64_t &Result) const {
125 const coff_symbol *symb = toSymb(Symb);
126 const coff_section *Section = NULL;
127 if (error_code ec = getSection(symb->SectionNumber, Section))
128 return ec;
129 char Type;
130 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
131 return ec;
132 if (Type == 'U' || Type == 'w')
133 Result = UnknownAddressOrSize;
134 else if (Section)
Danil Malyshevb0436a72011-11-29 17:40:10 +0000135 Result = Section->VirtualAddress + symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000136 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000137 Result = symb->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000138 return object_error::success;
139}
140
141error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000142 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000143 const coff_symbol *symb = toSymb(Symb);
144 Result = SymbolRef::ST_Other;
145 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
146 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
David Meyer2c677272012-02-29 02:11:55 +0000147 Result = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000148 } else {
Michael J. Spencer5e3a0822011-10-18 19:31:59 +0000149 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000150 Result = SymbolRef::ST_Function;
151 } else {
152 char Type;
153 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
154 return ec;
155 if (Type == 'r' || Type == 'R') {
156 Result = SymbolRef::ST_Data;
157 }
158 }
159 }
160 return object_error::success;
161}
162
David Meyerc46255a2012-02-28 23:47:53 +0000163error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
164 uint32_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000165 const coff_symbol *symb = toSymb(Symb);
David Meyerc46255a2012-02-28 23:47:53 +0000166 Result = SymbolRef::SF_None;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000167
David Meyer2c677272012-02-29 02:11:55 +0000168 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
169
170 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
171 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
172 Result |= SymbolRef::SF_Undefined;
David Meyerc46255a2012-02-28 23:47:53 +0000173
174 // TODO: This are certainly too restrictive.
175 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
176 Result |= SymbolRef::SF_Global;
177
178 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
179 Result |= SymbolRef::SF_Weak;
180
181 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
182 Result |= SymbolRef::SF_Absolute;
183
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000184 return object_error::success;
185}
186
Michael J. Spencer25b15772011-06-25 17:55:23 +0000187error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
188 uint64_t &Result) const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000189 // FIXME: Return the correct size. This requires looking at all the symbols
190 // in the same section as this symbol, and looking for either the next
191 // symbol, or the end of the section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000192 const coff_symbol *symb = toSymb(Symb);
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000193 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000194 if (error_code ec = getSection(symb->SectionNumber, Section))
195 return ec;
196 char Type;
197 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
198 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000199 if (Type == 'U' || Type == 'w')
Michael J. Spencer25b15772011-06-25 17:55:23 +0000200 Result = UnknownAddressOrSize;
201 else if (Section)
202 Result = Section->SizeOfRawData - symb->Value;
203 else
204 Result = 0;
205 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000206}
207
Michael J. Spencer25b15772011-06-25 17:55:23 +0000208error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
209 char &Result) const {
210 const coff_symbol *symb = toSymb(Symb);
211 StringRef name;
212 if (error_code ec = getSymbolName(Symb, name))
213 return ec;
214 char ret = StringSwitch<char>(name)
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000215 .StartsWith(".debug", 'N')
216 .StartsWith(".sxdata", 'N')
217 .Default('?');
218
Michael J. Spencer25b15772011-06-25 17:55:23 +0000219 if (ret != '?') {
220 Result = ret;
221 return object_error::success;
222 }
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000223
224 uint32_t Characteristics = 0;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000225 if (symb->SectionNumber > 0) {
Michael J. Spencer64388ce2011-07-05 14:48:59 +0000226 const coff_section *Section = NULL;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000227 if (error_code ec = getSection(symb->SectionNumber, Section))
228 return ec;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000229 Characteristics = Section->Characteristics;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000230 }
231
232 switch (symb->SectionNumber) {
233 case COFF::IMAGE_SYM_UNDEFINED:
234 // Check storage classes.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000235 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
236 Result = 'w';
237 return object_error::success; // Don't do ::toupper.
Michael J. Spencer11ba26d2011-11-16 23:36:12 +0000238 } else if (symb->Value != 0) // Check for common symbols.
239 ret = 'c';
240 else
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000241 ret = 'u';
242 break;
243 case COFF::IMAGE_SYM_ABSOLUTE:
244 ret = 'a';
245 break;
246 case COFF::IMAGE_SYM_DEBUG:
247 ret = 'n';
248 break;
249 default:
250 // Check section type.
251 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
252 ret = 't';
253 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
254 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
255 ret = 'r';
256 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
257 ret = 'd';
258 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
259 ret = 'b';
260 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
261 ret = 'i';
262
263 // Check for section symbol.
264 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
265 && symb->Value == 0)
266 ret = 's';
267 }
268
269 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
270 ret = ::toupper(ret);
271
Michael J. Spencer25b15772011-06-25 17:55:23 +0000272 Result = ret;
273 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000274}
275
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000276error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
277 section_iterator &Result) const {
278 const coff_symbol *symb = toSymb(Symb);
279 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
280 Result = end_sections();
281 else {
Daniel Dunbara483fc82011-11-28 22:19:32 +0000282 const coff_section *sec = 0;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000283 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
284 DataRefImpl Sec;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000285 std::memset(&Sec, 0, sizeof(Sec));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000286 Sec.p = reinterpret_cast<uintptr_t>(sec);
287 Result = section_iterator(SectionRef(Sec, this));
288 }
289 return object_error::success;
290}
291
Michael J. Spencer25b15772011-06-25 17:55:23 +0000292error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
293 SectionRef &Result) const {
294 const coff_section *sec = toSec(Sec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000295 sec += 1;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000296 Sec.p = reinterpret_cast<uintptr_t>(sec);
297 Result = SectionRef(Sec, this);
298 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000299}
300
Michael J. Spencer25b15772011-06-25 17:55:23 +0000301error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
302 StringRef &Result) const {
303 const coff_section *sec = toSec(Sec);
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000304 return getSectionName(sec, Result);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000305}
306
Michael J. Spencer25b15772011-06-25 17:55:23 +0000307error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
308 uint64_t &Result) const {
309 const coff_section *sec = toSec(Sec);
310 Result = sec->VirtualAddress;
311 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000312}
313
Michael J. Spencer25b15772011-06-25 17:55:23 +0000314error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
315 uint64_t &Result) const {
316 const coff_section *sec = toSec(Sec);
317 Result = sec->SizeOfRawData;
318 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000319}
320
Michael J. Spencer25b15772011-06-25 17:55:23 +0000321error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
322 StringRef &Result) const {
323 const coff_section *sec = toSec(Sec);
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000324 ArrayRef<uint8_t> Res;
325 error_code EC = getSectionContents(sec, Res);
326 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
327 return EC;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000328}
329
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000330error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
331 uint64_t &Res) const {
332 const coff_section *sec = toSec(Sec);
333 if (!sec)
334 return object_error::parse_failed;
335 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
336 return object_error::success;
337}
338
Michael J. Spencer25b15772011-06-25 17:55:23 +0000339error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
340 bool &Result) const {
341 const coff_section *sec = toSec(Sec);
342 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
343 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000344}
345
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000346error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
347 bool &Result) const {
348 const coff_section *sec = toSec(Sec);
349 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
350 return object_error::success;
351}
352
353error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
354 bool &Result) const {
355 const coff_section *sec = toSec(Sec);
356 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
357 return object_error::success;
358}
359
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000360error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
361 DataRefImpl Symb,
362 bool &Result) const {
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000363 const coff_section *sec = toSec(Sec);
364 const coff_symbol *symb = toSymb(Symb);
Daniel Dunbara483fc82011-11-28 22:19:32 +0000365 const coff_section *symb_sec = 0;
Michael J. Spencerbff6f862011-10-13 20:36:54 +0000366 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
367 if (symb_sec == sec)
368 Result = true;
369 else
370 Result = false;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000371 return object_error::success;
372}
373
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000374relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
375 const coff_section *sec = toSec(Sec);
376 DataRefImpl ret;
377 std::memset(&ret, 0, sizeof(ret));
378 if (sec->NumberOfRelocations == 0)
379 ret.p = 0;
380 else
381 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
382
383 return relocation_iterator(RelocationRef(ret, this));
384}
385
386relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
387 const coff_section *sec = toSec(Sec);
388 DataRefImpl ret;
389 std::memset(&ret, 0, sizeof(ret));
390 if (sec->NumberOfRelocations == 0)
391 ret.p = 0;
392 else
393 ret.p = reinterpret_cast<uintptr_t>(
394 reinterpret_cast<const coff_relocation*>(
395 base() + sec->PointerToRelocations)
396 + sec->NumberOfRelocations);
397
398 return relocation_iterator(RelocationRef(ret, this));
399}
400
Michael J. Spencer001c9202011-06-25 17:54:50 +0000401COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
David Meyer6f9489a2012-03-09 20:41:57 +0000402 : ObjectFile(Binary::ID_COFF, Object, ec)
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000403 , Header(0)
404 , SectionTable(0)
405 , SymbolTable(0)
406 , StringTable(0)
407 , StringTableSize(0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000408 // Check that we at least have enough room for a header.
409 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
Eric Christopher539d8d82011-04-03 22:53:19 +0000410
Michael J. Spencer25b15772011-06-25 17:55:23 +0000411 // The actual starting location of the COFF header in the file. This can be
412 // non-zero in PE/COFF files.
413 uint64_t HeaderStart = 0;
Eric Christopher539d8d82011-04-03 22:53:19 +0000414
Michael J. Spencer25b15772011-06-25 17:55:23 +0000415 // Check if this is a PE/COFF file.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000416 if (base()[0] == 0x4d && base()[1] == 0x5a) {
Eric Christopher539d8d82011-04-03 22:53:19 +0000417 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
418 // PE signature to find 'normal' COFF header.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000419 if (!checkSize(Data, ec, 0x3c + 8)) return;
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000420 HeaderStart = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000421 // Check the PE header. ("PE\0\0")
Benjamin Kramer3209a032011-07-05 20:28:00 +0000422 if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000423 ec = object_error::parse_failed;
424 return;
425 }
426 HeaderStart += 4; // Skip the PE Header.
Eric Christopher539d8d82011-04-03 22:53:19 +0000427 }
428
Michael J. Spencer25b15772011-06-25 17:55:23 +0000429 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
430 if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
431 return;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000432
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000433 SectionTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000434 reinterpret_cast<const coff_section *>( base()
Michael J. Spencer25b15772011-06-25 17:55:23 +0000435 + HeaderStart
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000436 + sizeof(coff_file_header)
437 + Header->SizeOfOptionalHeader);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000438 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
439 Header->NumberOfSections * sizeof(coff_section)))
440 return;
441
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000442 if (Header->PointerToSymbolTable != 0) {
443 SymbolTable =
444 reinterpret_cast<const coff_symbol *>(base()
445 + Header->PointerToSymbolTable);
446 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
447 Header->NumberOfSymbols * sizeof(coff_symbol)))
448 return;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000449
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000450 // Find string table.
451 StringTable = reinterpret_cast<const char *>(base())
452 + Header->PointerToSymbolTable
453 + Header->NumberOfSymbols * sizeof(coff_symbol);
454 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
455 return;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000456
Michael J. Spencer7151ddd2011-11-08 23:34:07 +0000457 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
458 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
459 return;
460 // Check that the string table is null terminated if has any in it.
461 if (StringTableSize < 4
462 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
463 ec = object_error::parse_failed;
464 return;
465 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000466 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000467
Michael J. Spencer25b15772011-06-25 17:55:23 +0000468 ec = object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000469}
470
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000471symbol_iterator COFFObjectFile::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000472 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000473 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000474 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
475 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000476}
477
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000478symbol_iterator COFFObjectFile::end_symbols() const {
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000479 // The symbol table ends where the string table begins.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000480 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000481 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000482 ret.p = reinterpret_cast<intptr_t>(StringTable);
483 return symbol_iterator(SymbolRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000484}
485
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000486symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
487 // TODO: implement
488 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
489}
490
491symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
492 // TODO: implement
493 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
494}
495
David Meyer5c2b4ea2012-03-01 01:36:50 +0000496library_iterator COFFObjectFile::begin_libraries_needed() const {
497 // TODO: implement
498 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
499}
500
501library_iterator COFFObjectFile::end_libraries_needed() const {
502 // TODO: implement
503 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
504}
505
David Meyer97f77872012-03-01 22:19:54 +0000506StringRef COFFObjectFile::getLoadName() const {
507 // COFF does not have this field.
508 return "";
509}
510
511
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000512section_iterator COFFObjectFile::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000513 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000514 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000515 ret.p = reinterpret_cast<intptr_t>(SectionTable);
516 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000517}
518
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000519section_iterator COFFObjectFile::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000520 DataRefImpl ret;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000521 std::memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000522 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
523 return section_iterator(SectionRef(ret, this));
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000524}
525
526uint8_t COFFObjectFile::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000527 return getArch() == Triple::x86_64 ? 8 : 4;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000528}
529
530StringRef COFFObjectFile::getFileFormatName() const {
531 switch(Header->Machine) {
532 case COFF::IMAGE_FILE_MACHINE_I386:
533 return "COFF-i386";
534 case COFF::IMAGE_FILE_MACHINE_AMD64:
535 return "COFF-x86-64";
536 default:
537 return "COFF-<unknown arch>";
538 }
539}
540
541unsigned COFFObjectFile::getArch() const {
542 switch(Header->Machine) {
543 case COFF::IMAGE_FILE_MACHINE_I386:
544 return Triple::x86;
545 case COFF::IMAGE_FILE_MACHINE_AMD64:
546 return Triple::x86_64;
547 default:
548 return Triple::UnknownArch;
549 }
550}
551
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000552error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
553 Res = Header;
554 return object_error::success;
555}
556
Michael J. Spencer25b15772011-06-25 17:55:23 +0000557error_code COFFObjectFile::getSection(int32_t index,
558 const coff_section *&Result) const {
559 // Check for special index values.
560 if (index == COFF::IMAGE_SYM_UNDEFINED ||
561 index == COFF::IMAGE_SYM_ABSOLUTE ||
562 index == COFF::IMAGE_SYM_DEBUG)
563 Result = NULL;
564 else if (index > 0 && index <= Header->NumberOfSections)
565 // We already verified the section table data, so no need to check again.
566 Result = SectionTable + (index - 1);
567 else
568 return object_error::parse_failed;
569 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000570}
571
Michael J. Spencer25b15772011-06-25 17:55:23 +0000572error_code COFFObjectFile::getString(uint32_t offset,
573 StringRef &Result) const {
574 if (StringTableSize <= 4)
575 // Tried to get a string from an empty string table.
576 return object_error::parse_failed;
577 if (offset >= StringTableSize)
578 return object_error::unexpected_eof;
579 Result = StringRef(StringTable + offset);
580 return object_error::success;
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000581}
582
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000583error_code COFFObjectFile::getSymbol(uint32_t index,
584 const coff_symbol *&Result) const {
Michael J. Spencer7c246652011-10-18 19:51:36 +0000585 if (index < Header->NumberOfSymbols)
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000586 Result = SymbolTable + index;
587 else
588 return object_error::parse_failed;
589 return object_error::success;
590}
591
Michael J. Spencer0e752cb2011-10-17 23:53:56 +0000592error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
593 StringRef &Res) const {
594 // Check for string table entry. First 4 bytes are 0.
595 if (symbol->Name.Offset.Zeroes == 0) {
596 uint32_t Offset = symbol->Name.Offset.Offset;
597 if (error_code ec = getString(Offset, Res))
598 return ec;
599 return object_error::success;
600 }
601
602 if (symbol->Name.ShortName[7] == 0)
603 // Null terminated, let ::strlen figure out the length.
604 Res = StringRef(symbol->Name.ShortName);
605 else
606 // Not null terminated, use all 8 bytes.
607 Res = StringRef(symbol->Name.ShortName, 8);
608 return object_error::success;
609}
610
Michael J. Spencerb35a8962012-03-19 20:27:15 +0000611error_code COFFObjectFile::getSectionName(const coff_section *Sec,
612 StringRef &Res) const {
613 StringRef Name;
614 if (Sec->Name[7] == 0)
615 // Null terminated, let ::strlen figure out the length.
616 Name = Sec->Name;
617 else
618 // Not null terminated, use all 8 bytes.
619 Name = StringRef(Sec->Name, 8);
620
621 // Check for string table entry. First byte is '/'.
622 if (Name[0] == '/') {
623 uint32_t Offset;
624 if (Name.substr(1).getAsInteger(10, Offset))
625 return object_error::parse_failed;
626 if (error_code ec = getString(Offset, Name))
627 return ec;
628 }
629
630 Res = Name;
631 return object_error::success;
632}
633
Michael J. Spencer1f6e3f92012-03-19 20:27:37 +0000634error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
635 ArrayRef<uint8_t> &Res) const {
636 // The only thing that we need to verify is that the contents is contained
637 // within the file bounds. We don't need to make sure it doesn't cover other
638 // data, as there's nothing that says that is not allowed.
639 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
640 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
641 if (ConEnd > uintptr_t(Data->getBufferEnd()))
642 return object_error::parse_failed;
643 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
644 Sec->SizeOfRawData);
645 return object_error::success;
646}
647
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000648const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000649 return reinterpret_cast<const coff_relocation*>(Rel.p);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000650}
651error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
652 RelocationRef &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000653 Rel.p = reinterpret_cast<uintptr_t>(
654 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000655 Res = RelocationRef(Rel, this);
656 return object_error::success;
657}
658error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
659 uint64_t &Res) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000660 Res = toRel(Rel)->VirtualAddress;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000661 return object_error::success;
662}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000663error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
664 uint64_t &Res) const {
665 Res = toRel(Rel)->VirtualAddress;
666 return object_error::success;
667}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000668error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
669 SymbolRef &Res) const {
670 const coff_relocation* R = toRel(Rel);
671 DataRefImpl Symb;
Michael J. Spencer783d8872011-11-02 19:33:26 +0000672 std::memset(&Symb, 0, sizeof(Symb));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000673 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
674 Res = SymbolRef(Symb, this);
675 return object_error::success;
676}
677error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000678 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000679 const coff_relocation* R = toRel(Rel);
680 Res = R->Type;
681 return object_error::success;
682}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000683
684#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
685 case COFF::enum: res = #enum; break;
686
687error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
688 SmallVectorImpl<char> &Result) const {
689 const coff_relocation *reloc = toRel(Rel);
690 StringRef res;
691 switch (Header->Machine) {
692 case COFF::IMAGE_FILE_MACHINE_AMD64:
693 switch (reloc->Type) {
694 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
695 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
696 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
697 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
698 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
699 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
700 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
701 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
702 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
703 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
704 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
705 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
706 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
707 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
708 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
709 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
710 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
711 default:
712 res = "Unknown";
713 }
714 break;
715 case COFF::IMAGE_FILE_MACHINE_I386:
716 switch (reloc->Type) {
717 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
718 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
719 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
720 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
721 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
722 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
723 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
724 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
725 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
726 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
727 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
728 default:
729 res = "Unknown";
730 }
731 break;
732 default:
733 res = "Unknown";
734 }
735 Result.append(res.begin(), res.end());
736 return object_error::success;
737}
738
739#undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
740
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000741error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
742 int64_t &Res) const {
743 Res = 0;
744 return object_error::success;
745}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000746error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
747 SmallVectorImpl<char> &Result) const {
748 const coff_relocation *reloc = toRel(Rel);
NAKAMURA Takumi48f248a2011-10-08 11:22:53 +0000749 const coff_symbol *symb = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000750 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
751 DataRefImpl sym;
752 ::memset(&sym, 0, sizeof(sym));
753 sym.p = reinterpret_cast<uintptr_t>(symb);
754 StringRef symname;
755 if (error_code ec = getSymbolName(sym, symname)) return ec;
756 Result.append(symname.begin(), symname.end());
757 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000758}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000759
David Meyer5c2b4ea2012-03-01 01:36:50 +0000760error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
761 LibraryRef &Result) const {
762 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
763}
764
765error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
766 StringRef &Result) const {
767 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
768}
769
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000770namespace llvm {
771
772 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000773 error_code ec;
774 return new COFFObjectFile(Object, ec);
Michael J. Spencera1ef8ef2011-01-20 06:38:34 +0000775 }
776
777} // end namespace llvm