1 //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the COFFObjectFile class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Triple.h"
19 using namespace object;
22 using support::ulittle8_t;
23 using support::ulittle16_t;
24 using support::ulittle32_t;
25 using support::little16_t;
29 // Returns false if size is greater than the buffer size. And sets ec.
30 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
31 if (m->getBufferSize() < size) {
32 ec = object_error::unexpected_eof;
38 // Returns false if any bytes in [addr, addr + size) fall outsize of m.
39 bool checkAddr(const MemoryBuffer *m,
43 if (addr + size < addr ||
45 addr + size > uintptr_t(m->getBufferEnd())) {
46 ec = object_error::unexpected_eof;
53 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
54 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
57 // Verify that the symbol points to a valid entry in the symbol table.
58 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
59 if (offset < Header->PointerToSymbolTable
60 || offset >= Header->PointerToSymbolTable
61 + (Header->NumberOfSymbols * sizeof(coff_symbol)))
62 report_fatal_error("Symbol was outside of symbol table.");
64 assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol)
65 == 0 && "Symbol did not point to the beginning of a symbol");
71 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
72 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
75 // Verify that the section points to a valid entry in the section table.
76 if (addr < SectionTable
77 || addr >= (SectionTable + Header->NumberOfSections))
78 report_fatal_error("Section was outside of section table.");
80 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
81 assert(offset % sizeof(coff_section) == 0 &&
82 "Section did not point to the beginning of a section");
88 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
89 SymbolRef &Result) const {
90 const coff_symbol *symb = toSymb(Symb);
91 symb += 1 + symb->NumberOfAuxSymbols;
92 Symb.p = reinterpret_cast<uintptr_t>(symb);
93 Result = SymbolRef(Symb, this);
94 return object_error::success;
97 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
98 StringRef &Result) const {
99 const coff_symbol *symb = toSymb(Symb);
100 // Check for string table entry. First 4 bytes are 0.
101 if (symb->Name.Offset.Zeroes == 0) {
102 uint32_t Offset = symb->Name.Offset.Offset;
103 if (error_code ec = getString(Offset, Result))
105 return object_error::success;
108 if (symb->Name.ShortName[7] == 0)
109 // Null terminated, let ::strlen figure out the length.
110 Result = StringRef(symb->Name.ShortName);
112 // Not null terminated, use all 8 bytes.
113 Result = StringRef(symb->Name.ShortName, 8);
114 return object_error::success;
117 error_code COFFObjectFile::getSymbolOffset(DataRefImpl Symb,
118 uint64_t &Result) const {
119 const coff_symbol *symb = toSymb(Symb);
120 const coff_section *Section = NULL;
121 if (error_code ec = getSection(symb->SectionNumber, Section))
124 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
126 if (Type == 'U' || Type == 'w')
127 Result = UnknownAddressOrSize;
129 Result = Section->VirtualAddress + symb->Value;
131 Result = symb->Value;
132 return object_error::success;
135 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
136 uint64_t &Result) const {
137 const coff_symbol *symb = toSymb(Symb);
138 const coff_section *Section = NULL;
139 if (error_code ec = getSection(symb->SectionNumber, Section))
142 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
144 if (Type == 'U' || Type == 'w')
145 Result = UnknownAddressOrSize;
147 Result = reinterpret_cast<uintptr_t>(base() +
148 Section->PointerToRawData +
151 Result = reinterpret_cast<uintptr_t>(base() + symb->Value);
152 return object_error::success;
155 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
156 SymbolRef::SymbolType &Result) const {
157 const coff_symbol *symb = toSymb(Symb);
158 Result = SymbolRef::ST_Other;
159 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
160 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
161 Result = SymbolRef::ST_External;
163 if (symb->Type.ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
164 Result = SymbolRef::ST_Function;
167 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
169 if (Type == 'r' || Type == 'R') {
170 Result = SymbolRef::ST_Data;
174 return object_error::success;
177 error_code COFFObjectFile::isSymbolGlobal(DataRefImpl Symb,
178 bool &Result) const {
179 const coff_symbol *symb = toSymb(Symb);
180 Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL);
181 return object_error::success;
184 error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
185 uint64_t &Result) const {
186 // FIXME: Return the correct size. This requires looking at all the symbols
187 // in the same section as this symbol, and looking for either the next
188 // symbol, or the end of the section.
189 const coff_symbol *symb = toSymb(Symb);
190 const coff_section *Section = NULL;
191 if (error_code ec = getSection(symb->SectionNumber, Section))
194 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
196 if (Type == 'U' || Type == 'w')
197 Result = UnknownAddressOrSize;
199 Result = Section->SizeOfRawData - symb->Value;
202 return object_error::success;
205 error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
206 char &Result) const {
207 const coff_symbol *symb = toSymb(Symb);
209 if (error_code ec = getSymbolName(Symb, name))
211 char ret = StringSwitch<char>(name)
212 .StartsWith(".debug", 'N')
213 .StartsWith(".sxdata", 'N')
218 return object_error::success;
221 uint32_t Characteristics = 0;
222 if (symb->SectionNumber > 0) {
223 const coff_section *Section = NULL;
224 if (error_code ec = getSection(symb->SectionNumber, Section))
226 Characteristics = Section->Characteristics;
229 switch (symb->SectionNumber) {
230 case COFF::IMAGE_SYM_UNDEFINED:
231 // Check storage classes.
232 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
234 return object_error::success; // Don't do ::toupper.
238 case COFF::IMAGE_SYM_ABSOLUTE:
241 case COFF::IMAGE_SYM_DEBUG:
245 // Check section type.
246 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
248 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
249 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
251 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
253 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
255 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
258 // Check for section symbol.
259 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
264 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
265 ret = ::toupper(ret);
268 return object_error::success;
271 error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb,
272 bool &Result) const {
274 return object_error::success;
277 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
278 SectionRef &Result) const {
279 const coff_section *sec = toSec(Sec);
281 Sec.p = reinterpret_cast<uintptr_t>(sec);
282 Result = SectionRef(Sec, this);
283 return object_error::success;
286 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
287 StringRef &Result) const {
288 const coff_section *sec = toSec(Sec);
290 if (sec->Name[7] == 0)
291 // Null terminated, let ::strlen figure out the length.
294 // Not null terminated, use all 8 bytes.
295 name = StringRef(sec->Name, 8);
297 // Check for string table entry. First byte is '/'.
298 if (name[0] == '/') {
300 name.substr(1).getAsInteger(10, Offset);
301 if (error_code ec = getString(Offset, name))
306 return object_error::success;
309 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
310 uint64_t &Result) const {
311 const coff_section *sec = toSec(Sec);
312 Result = sec->VirtualAddress;
313 return object_error::success;
316 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
317 uint64_t &Result) const {
318 const coff_section *sec = toSec(Sec);
319 Result = sec->SizeOfRawData;
320 return object_error::success;
323 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
324 StringRef &Result) const {
325 const coff_section *sec = toSec(Sec);
326 // The only thing that we need to verify is that the contents is contained
327 // within the file bounds. We don't need to make sure it doesn't cover other
328 // data, as there's nothing that says that is not allowed.
329 uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData;
330 uintptr_t con_end = con_start + sec->SizeOfRawData;
331 if (con_end >= uintptr_t(Data->getBufferEnd()))
332 return object_error::parse_failed;
333 Result = StringRef(reinterpret_cast<const char*>(con_start),
335 return object_error::success;
338 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
339 bool &Result) const {
340 const coff_section *sec = toSec(Sec);
341 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
342 return object_error::success;
345 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
347 bool &Result) const {
348 // FIXME: Unimplemented.
350 return object_error::success;
353 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
354 : ObjectFile(Binary::isCOFF, Object, ec) {
355 // Check that we at least have enough room for a header.
356 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
358 // The actual starting location of the COFF header in the file. This can be
359 // non-zero in PE/COFF files.
360 uint64_t HeaderStart = 0;
362 // Check if this is a PE/COFF file.
363 if (base()[0] == 0x4d && base()[1] == 0x5a) {
364 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
365 // PE signature to find 'normal' COFF header.
366 if (!checkSize(Data, ec, 0x3c + 8)) return;
367 HeaderStart += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c);
368 // Check the PE header. ("PE\0\0")
369 if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) {
370 ec = object_error::parse_failed;
373 HeaderStart += 4; // Skip the PE Header.
376 Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart);
377 if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header)))
381 reinterpret_cast<const coff_section *>( base()
383 + sizeof(coff_file_header)
384 + Header->SizeOfOptionalHeader);
385 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
386 Header->NumberOfSections * sizeof(coff_section)))
390 reinterpret_cast<const coff_symbol *>(base()
391 + Header->PointerToSymbolTable);
392 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
393 Header->NumberOfSymbols * sizeof(coff_symbol)))
396 // Find string table.
397 StringTable = reinterpret_cast<const char *>(base())
398 + Header->PointerToSymbolTable
399 + Header->NumberOfSymbols * sizeof(coff_symbol);
400 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
403 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
404 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
406 // Check that the string table is null terminated if has any in it.
407 if (StringTableSize < 4
408 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
409 ec = object_error::parse_failed;
413 ec = object_error::success;
416 ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
418 std::memset(&ret, 0, sizeof(DataRefImpl));
419 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
420 return symbol_iterator(SymbolRef(ret, this));
423 ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
424 // The symbol table ends where the string table begins.
426 std::memset(&ret, 0, sizeof(DataRefImpl));
427 ret.p = reinterpret_cast<intptr_t>(StringTable);
428 return symbol_iterator(SymbolRef(ret, this));
431 ObjectFile::section_iterator COFFObjectFile::begin_sections() const {
433 std::memset(&ret, 0, sizeof(DataRefImpl));
434 ret.p = reinterpret_cast<intptr_t>(SectionTable);
435 return section_iterator(SectionRef(ret, this));
438 ObjectFile::section_iterator COFFObjectFile::end_sections() const {
440 std::memset(&ret, 0, sizeof(DataRefImpl));
441 ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections);
442 return section_iterator(SectionRef(ret, this));
445 uint8_t COFFObjectFile::getBytesInAddress() const {
446 return getArch() == Triple::x86_64 ? 8 : 4;
449 StringRef COFFObjectFile::getFileFormatName() const {
450 switch(Header->Machine) {
451 case COFF::IMAGE_FILE_MACHINE_I386:
453 case COFF::IMAGE_FILE_MACHINE_AMD64:
454 return "COFF-x86-64";
456 return "COFF-<unknown arch>";
460 unsigned COFFObjectFile::getArch() const {
461 switch(Header->Machine) {
462 case COFF::IMAGE_FILE_MACHINE_I386:
464 case COFF::IMAGE_FILE_MACHINE_AMD64:
465 return Triple::x86_64;
467 return Triple::UnknownArch;
471 error_code COFFObjectFile::getSection(int32_t index,
472 const coff_section *&Result) const {
473 // Check for special index values.
474 if (index == COFF::IMAGE_SYM_UNDEFINED ||
475 index == COFF::IMAGE_SYM_ABSOLUTE ||
476 index == COFF::IMAGE_SYM_DEBUG)
478 else if (index > 0 && index <= Header->NumberOfSections)
479 // We already verified the section table data, so no need to check again.
480 Result = SectionTable + (index - 1);
482 return object_error::parse_failed;
483 return object_error::success;
486 error_code COFFObjectFile::getString(uint32_t offset,
487 StringRef &Result) const {
488 if (StringTableSize <= 4)
489 // Tried to get a string from an empty string table.
490 return object_error::parse_failed;
491 if (offset >= StringTableSize)
492 return object_error::unexpected_eof;
493 Result = StringRef(StringTable + offset);
494 return object_error::success;
497 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
498 assert(Rel.d.b < Header->NumberOfSections && "Section index out of range!");
499 const coff_section *Sect = NULL;
500 getSection(Rel.d.b, Sect);
501 assert(Rel.d.a < Sect->NumberOfRelocations && "Relocation index out of range!");
503 reinterpret_cast<const coff_relocation*>(base() +
504 Sect->PointerToRelocations) +
507 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
508 RelocationRef &Res) const {
509 const coff_section *Sect = NULL;
510 if (error_code ec = getSection(Rel.d.b, Sect))
512 if (++Rel.d.a >= Sect->NumberOfRelocations) {
514 while (++Rel.d.b < Header->NumberOfSections) {
515 const coff_section *Sect = NULL;
516 getSection(Rel.d.b, Sect);
517 if (Sect->NumberOfRelocations > 0)
521 Res = RelocationRef(Rel, this);
522 return object_error::success;
524 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
525 uint64_t &Res) const {
526 const coff_section *Sect = NULL;
527 if (error_code ec = getSection(Rel.d.b, Sect))
529 const coff_relocation* R = toRel(Rel);
530 Res = reinterpret_cast<uintptr_t>(base() +
531 Sect->PointerToRawData +
533 return object_error::success;
535 error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
536 SymbolRef &Res) const {
537 const coff_relocation* R = toRel(Rel);
539 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
540 Res = SymbolRef(Symb, this);
541 return object_error::success;
543 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
544 uint32_t &Res) const {
545 const coff_relocation* R = toRel(Rel);
547 return object_error::success;
549 error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
550 int64_t &Res) const {
552 return object_error::success;
554 ObjectFile::relocation_iterator COFFObjectFile::begin_relocations() const {
558 return relocation_iterator(RelocationRef(ret, this));
560 ObjectFile::relocation_iterator COFFObjectFile::end_relocations() const {
563 ret.d.b = Header->NumberOfSections;
564 return relocation_iterator(RelocationRef(ret, this));
570 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
572 return new COFFObjectFile(Object, ec);
575 } // end namespace llvm