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/ArrayRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
24 using namespace object;
27 using support::ulittle8_t;
28 using support::ulittle16_t;
29 using support::ulittle32_t;
30 using support::little16_t;
34 // Returns false if size is greater than the buffer size. And sets ec.
35 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
36 if (m->getBufferSize() < size) {
37 ec = object_error::unexpected_eof;
43 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
44 // Returns unexpected_eof if error.
46 error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
47 const size_t Size = sizeof(T)) {
48 uintptr_t Addr = uintptr_t(Ptr);
49 if (Addr + Size < Addr ||
51 Addr + Size > uintptr_t(M->getBufferEnd())) {
52 return object_error::unexpected_eof;
54 Obj = reinterpret_cast<const T *>(Addr);
55 return object_error::success;
59 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
60 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
63 // Verify that the symbol points to a valid entry in the symbol table.
64 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
65 if (offset < COFFHeader->PointerToSymbolTable
66 || offset >= COFFHeader->PointerToSymbolTable
67 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
68 report_fatal_error("Symbol was outside of symbol table.");
70 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
71 == 0 && "Symbol did not point to the beginning of a symbol");
77 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
78 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
81 // Verify that the section points to a valid entry in the section table.
82 if (addr < SectionTable
83 || addr >= (SectionTable + COFFHeader->NumberOfSections))
84 report_fatal_error("Section was outside of section table.");
86 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
87 assert(offset % sizeof(coff_section) == 0 &&
88 "Section did not point to the beginning of a section");
94 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
95 SymbolRef &Result) const {
96 const coff_symbol *symb = toSymb(Symb);
97 symb += 1 + symb->NumberOfAuxSymbols;
98 Symb.p = reinterpret_cast<uintptr_t>(symb);
99 Result = SymbolRef(Symb, this);
100 return object_error::success;
103 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
104 StringRef &Result) const {
105 const coff_symbol *symb = toSymb(Symb);
106 return getSymbolName(symb, Result);
109 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
110 uint64_t &Result) const {
111 const coff_symbol *symb = toSymb(Symb);
112 const coff_section *Section = NULL;
113 if (error_code ec = getSection(symb->SectionNumber, Section))
116 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
117 Result = UnknownAddressOrSize;
119 Result = Section->PointerToRawData + symb->Value;
121 Result = symb->Value;
122 return object_error::success;
125 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
126 uint64_t &Result) const {
127 const coff_symbol *symb = toSymb(Symb);
128 const coff_section *Section = NULL;
129 if (error_code ec = getSection(symb->SectionNumber, Section))
132 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
133 Result = UnknownAddressOrSize;
135 Result = Section->VirtualAddress + symb->Value;
137 Result = symb->Value;
138 return object_error::success;
141 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
142 SymbolRef::Type &Result) const {
143 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) {
147 Result = SymbolRef::ST_Unknown;
149 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
150 Result = SymbolRef::ST_Function;
153 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
155 if (Type == 'r' || Type == 'R') {
156 Result = SymbolRef::ST_Data;
160 return object_error::success;
163 error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
164 uint32_t &Result) const {
165 const coff_symbol *symb = toSymb(Symb);
166 Result = SymbolRef::SF_None;
168 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
170 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
171 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
172 Result |= SymbolRef::SF_Undefined;
174 // TODO: This are certainly too restrictive.
175 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
176 Result |= SymbolRef::SF_Global;
178 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
179 Result |= SymbolRef::SF_Weak;
181 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
182 Result |= SymbolRef::SF_Absolute;
184 return object_error::success;
187 error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
188 uint64_t &Result) const {
189 // 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.
192 const coff_symbol *symb = toSymb(Symb);
193 const coff_section *Section = NULL;
194 if (error_code ec = getSection(symb->SectionNumber, Section))
197 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
198 Result = UnknownAddressOrSize;
200 Result = Section->SizeOfRawData - symb->Value;
203 return object_error::success;
206 error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
207 char &Result) const {
208 const coff_symbol *symb = toSymb(Symb);
210 if (error_code ec = getSymbolName(Symb, name))
212 char ret = StringSwitch<char>(name)
213 .StartsWith(".debug", 'N')
214 .StartsWith(".sxdata", 'N')
219 return object_error::success;
222 uint32_t Characteristics = 0;
223 if (symb->SectionNumber > 0) {
224 const coff_section *Section = NULL;
225 if (error_code ec = getSection(symb->SectionNumber, Section))
227 Characteristics = Section->Characteristics;
230 switch (symb->SectionNumber) {
231 case COFF::IMAGE_SYM_UNDEFINED:
232 // Check storage classes.
233 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
235 return object_error::success; // Don't do ::toupper.
236 } else if (symb->Value != 0) // Check for common symbols.
241 case COFF::IMAGE_SYM_ABSOLUTE:
244 case COFF::IMAGE_SYM_DEBUG:
248 // Check section type.
249 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
251 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
252 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
254 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
256 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
258 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
261 // Check for section symbol.
262 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
267 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
268 ret = ::toupper(static_cast<unsigned char>(ret));
271 return object_error::success;
274 error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
275 section_iterator &Result) const {
276 const coff_symbol *symb = toSymb(Symb);
277 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
278 Result = end_sections();
280 const coff_section *sec = 0;
281 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
283 Sec.p = reinterpret_cast<uintptr_t>(sec);
284 Result = section_iterator(SectionRef(Sec, this));
286 return object_error::success;
289 error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
290 uint64_t &Val) const {
291 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
294 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
295 SectionRef &Result) const {
296 const coff_section *sec = toSec(Sec);
298 Sec.p = reinterpret_cast<uintptr_t>(sec);
299 Result = SectionRef(Sec, this);
300 return object_error::success;
303 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
304 StringRef &Result) const {
305 const coff_section *sec = toSec(Sec);
306 return getSectionName(sec, Result);
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 ArrayRef<uint8_t> Res;
327 error_code EC = getSectionContents(sec, Res);
328 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
332 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
333 uint64_t &Res) const {
334 const coff_section *sec = toSec(Sec);
336 return object_error::parse_failed;
337 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
338 return object_error::success;
341 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
342 bool &Result) const {
343 const coff_section *sec = toSec(Sec);
344 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
345 return object_error::success;
348 error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
349 bool &Result) const {
350 const coff_section *sec = toSec(Sec);
351 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
352 return object_error::success;
355 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
356 bool &Result) const {
357 const coff_section *sec = toSec(Sec);
358 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
359 return object_error::success;
362 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
363 bool &Result) const {
364 // FIXME: Unimplemented
366 return object_error::success;
369 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
370 bool &Result) const {
371 const coff_section *sec = toSec(Sec);
372 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
373 return object_error::success;
376 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
377 bool &Result) const {
378 // FIXME: Unimplemented.
380 return object_error::success;
383 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
384 bool &Result) const {
385 // FIXME: Unimplemented.
387 return object_error::success;
390 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
392 bool &Result) const {
393 const coff_section *sec = toSec(Sec);
394 const coff_symbol *symb = toSymb(Symb);
395 const coff_section *symb_sec = 0;
396 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
401 return object_error::success;
404 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
405 const coff_section *sec = toSec(Sec);
407 if (sec->NumberOfRelocations == 0)
410 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
412 return relocation_iterator(RelocationRef(ret, this));
415 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Sec) const {
416 const coff_section *sec = toSec(Sec);
418 if (sec->NumberOfRelocations == 0)
421 ret.p = reinterpret_cast<uintptr_t>(
422 reinterpret_cast<const coff_relocation*>(
423 base() + sec->PointerToRelocations)
424 + sec->NumberOfRelocations);
426 return relocation_iterator(RelocationRef(ret, this));
429 // Initialize the pointer to the symbol table.
430 error_code COFFObjectFile::initSymbolTablePtr() {
431 if (error_code ec = getObject(
432 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
433 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
436 // Find string table. The first four byte of the string table contains the
437 // total size of the string table, including the size field itself. If the
438 // string table is empty, the value of the first four byte would be 4.
439 const uint8_t *StringTableAddr =
440 base() + COFFHeader->PointerToSymbolTable +
441 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
442 const ulittle32_t *StringTableSizePtr;
443 if (error_code ec = getObject(StringTableSizePtr, Data, StringTableAddr))
445 StringTableSize = *StringTableSizePtr;
447 getObject(StringTable, Data, StringTableAddr, StringTableSize))
450 // Check that the string table is null terminated if has any in it.
451 if (StringTableSize < 4 ||
452 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
453 return object_error::parse_failed;
454 return object_error::success;
457 // Returns the file offset for the given RVA.
458 error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
460 for (section_iterator i = begin_sections(), e = end_sections(); i != e;
464 const coff_section *Section = getCOFFSection(i);
465 uint32_t SectionStart = Section->VirtualAddress;
466 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
467 if (SectionStart <= Rva && Rva < SectionEnd) {
468 uint32_t Offset = Rva - SectionStart;
469 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
470 return object_error::success;
473 return object_error::parse_failed;
476 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
478 error_code COFFObjectFile::
479 getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
480 uintptr_t IntPtr = 0;
481 if (error_code ec = getRvaPtr(Rva, IntPtr))
483 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
484 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
485 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
486 return object_error::success;
489 // Find the import table.
490 error_code COFFObjectFile::initImportTablePtr() {
491 // First, we get the RVA of the import table. If the file lacks a pointer to
492 // the import table, do nothing.
493 const data_directory *DataEntry;
494 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
495 return object_error::success;
497 // Do nothing if the pointer to import table is NULL.
498 if (DataEntry->RelativeVirtualAddress == 0)
499 return object_error::success;
501 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
502 NumberOfImportDirectory = DataEntry->Size /
503 sizeof(import_directory_table_entry);
505 // Find the section that contains the RVA. This is needed because the RVA is
506 // the import table's memory address which is different from its file offset.
507 uintptr_t IntPtr = 0;
508 if (error_code ec = getRvaPtr(ImportTableRva, IntPtr))
510 ImportDirectory = reinterpret_cast<
511 const import_directory_table_entry *>(IntPtr);
513 // It's an error if there's no section containing the Import Table RVA.
514 return object_error::parse_failed;
517 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
518 : ObjectFile(Binary::ID_COFF, Object)
527 , NumberOfImportDirectory(0) {
528 // Check that we at least have enough room for a header.
529 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
531 // The current location in the file where we are looking at.
534 // PE header is optional and is present only in executables. If it exists,
535 // it is placed right after COFF header.
536 bool hasPEHeader = false;
538 // Check if this is a PE/COFF file.
539 if (base()[0] == 0x4d && base()[1] == 0x5a) {
540 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
541 // PE signature to find 'normal' COFF header.
542 if (!checkSize(Data, ec, 0x3c + 8)) return;
543 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
544 // Check the PE magic bytes. ("PE\0\0")
545 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
546 ec = object_error::parse_failed;
549 CurPtr += 4; // Skip the PE magic bytes.
553 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
555 CurPtr += sizeof(coff_file_header);
558 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
560 if (PE32Header->Magic != 0x10b) {
561 // We only support PE32. If this is PE32 (not PE32+), the magic byte
562 // should be 0x10b. If this is not PE32, continue as if there's no PE
563 // header in this file.
565 } else if (PE32Header->NumberOfRvaAndSize > 0) {
566 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
567 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
568 if ((ec = getObject(DataDirectory, Data, addr, size)))
571 CurPtr += COFFHeader->SizeOfOptionalHeader;
574 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
575 COFFHeader->NumberOfSections * sizeof(coff_section))))
578 // Initialize the pointer to the symbol table.
579 if (COFFHeader->PointerToSymbolTable != 0)
580 if ((ec = initSymbolTablePtr()))
583 // Initialize the pointer to the beginning of the import table.
584 if ((ec = initImportTablePtr()))
587 ec = object_error::success;
590 symbol_iterator COFFObjectFile::begin_symbols() const {
592 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
593 return symbol_iterator(SymbolRef(ret, this));
596 symbol_iterator COFFObjectFile::end_symbols() const {
597 // The symbol table ends where the string table begins.
599 ret.p = reinterpret_cast<uintptr_t>(StringTable);
600 return symbol_iterator(SymbolRef(ret, this));
603 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
605 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
608 symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
610 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
613 library_iterator COFFObjectFile::begin_libraries_needed() const {
615 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
618 library_iterator COFFObjectFile::end_libraries_needed() const {
620 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
623 StringRef COFFObjectFile::getLoadName() const {
624 // COFF does not have this field.
628 import_directory_iterator COFFObjectFile::import_directory_begin() const {
630 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
631 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
634 import_directory_iterator COFFObjectFile::import_directory_end() const {
636 if (ImportDirectory) {
637 Imp.p = reinterpret_cast<uintptr_t>(
638 ImportDirectory + (NumberOfImportDirectory - 1));
642 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
645 section_iterator COFFObjectFile::begin_sections() const {
647 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
648 return section_iterator(SectionRef(ret, this));
651 section_iterator COFFObjectFile::end_sections() const {
653 ret.p = reinterpret_cast<uintptr_t>(SectionTable + COFFHeader->NumberOfSections);
654 return section_iterator(SectionRef(ret, this));
657 uint8_t COFFObjectFile::getBytesInAddress() const {
658 return getArch() == Triple::x86_64 ? 8 : 4;
661 StringRef COFFObjectFile::getFileFormatName() const {
662 switch(COFFHeader->Machine) {
663 case COFF::IMAGE_FILE_MACHINE_I386:
665 case COFF::IMAGE_FILE_MACHINE_AMD64:
666 return "COFF-x86-64";
668 return "COFF-<unknown arch>";
672 unsigned COFFObjectFile::getArch() const {
673 switch(COFFHeader->Machine) {
674 case COFF::IMAGE_FILE_MACHINE_I386:
676 case COFF::IMAGE_FILE_MACHINE_AMD64:
677 return Triple::x86_64;
679 return Triple::UnknownArch;
683 // This method is kept here because lld uses this. As soon as we make
684 // lld to use getCOFFHeader, this method will be removed.
685 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
686 return getCOFFHeader(Res);
689 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
691 return object_error::success;
694 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
696 return object_error::success;
699 error_code COFFObjectFile::getDataDirectory(uint32_t index,
700 const data_directory *&Res) const {
701 // Error if if there's no data directory or the index is out of range.
702 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
703 return object_error::parse_failed;
704 Res = &DataDirectory[index];
705 return object_error::success;
708 error_code COFFObjectFile::getSection(int32_t index,
709 const coff_section *&Result) const {
710 // Check for special index values.
711 if (index == COFF::IMAGE_SYM_UNDEFINED ||
712 index == COFF::IMAGE_SYM_ABSOLUTE ||
713 index == COFF::IMAGE_SYM_DEBUG)
715 else if (index > 0 && index <= COFFHeader->NumberOfSections)
716 // We already verified the section table data, so no need to check again.
717 Result = SectionTable + (index - 1);
719 return object_error::parse_failed;
720 return object_error::success;
723 error_code COFFObjectFile::getString(uint32_t offset,
724 StringRef &Result) const {
725 if (StringTableSize <= 4)
726 // Tried to get a string from an empty string table.
727 return object_error::parse_failed;
728 if (offset >= StringTableSize)
729 return object_error::unexpected_eof;
730 Result = StringRef(StringTable + offset);
731 return object_error::success;
734 error_code COFFObjectFile::getSymbol(uint32_t index,
735 const coff_symbol *&Result) const {
736 if (index < COFFHeader->NumberOfSymbols)
737 Result = SymbolTable + index;
739 return object_error::parse_failed;
740 return object_error::success;
743 error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
744 StringRef &Res) const {
745 // Check for string table entry. First 4 bytes are 0.
746 if (symbol->Name.Offset.Zeroes == 0) {
747 uint32_t Offset = symbol->Name.Offset.Offset;
748 if (error_code ec = getString(Offset, Res))
750 return object_error::success;
753 if (symbol->Name.ShortName[7] == 0)
754 // Null terminated, let ::strlen figure out the length.
755 Res = StringRef(symbol->Name.ShortName);
757 // Not null terminated, use all 8 bytes.
758 Res = StringRef(symbol->Name.ShortName, 8);
759 return object_error::success;
762 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
763 const coff_symbol *symbol) const {
764 const uint8_t *aux = NULL;
766 if ( symbol->NumberOfAuxSymbols > 0 ) {
767 // AUX data comes immediately after the symbol in COFF
768 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
770 // Verify that the aux symbol points to a valid entry in the symbol table.
771 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
772 if (offset < COFFHeader->PointerToSymbolTable
773 || offset >= COFFHeader->PointerToSymbolTable
774 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
775 report_fatal_error("Aux Symbol data was outside of symbol table.");
777 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
778 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
781 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
784 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
785 StringRef &Res) const {
787 if (Sec->Name[7] == 0)
788 // Null terminated, let ::strlen figure out the length.
791 // Not null terminated, use all 8 bytes.
792 Name = StringRef(Sec->Name, 8);
794 // Check for string table entry. First byte is '/'.
795 if (Name[0] == '/') {
797 if (Name.substr(1).getAsInteger(10, Offset))
798 return object_error::parse_failed;
799 if (error_code ec = getString(Offset, Name))
804 return object_error::success;
807 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
808 ArrayRef<uint8_t> &Res) const {
809 // The only thing that we need to verify is that the contents is contained
810 // within the file bounds. We don't need to make sure it doesn't cover other
811 // data, as there's nothing that says that is not allowed.
812 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
813 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
814 if (ConEnd > uintptr_t(Data->getBufferEnd()))
815 return object_error::parse_failed;
816 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
818 return object_error::success;
821 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
822 return reinterpret_cast<const coff_relocation*>(Rel.p);
824 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
825 RelocationRef &Res) const {
826 Rel.p = reinterpret_cast<uintptr_t>(
827 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
828 Res = RelocationRef(Rel, this);
829 return object_error::success;
831 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
832 uint64_t &Res) const {
833 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
835 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
836 uint64_t &Res) const {
837 Res = toRel(Rel)->VirtualAddress;
838 return object_error::success;
840 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
841 const coff_relocation* R = toRel(Rel);
843 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
844 return symbol_iterator(SymbolRef(Symb, this));
846 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
847 uint64_t &Res) const {
848 const coff_relocation* R = toRel(Rel);
850 return object_error::success;
853 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
854 return toSec(It->getRawDataRefImpl());
857 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
858 return toSymb(It->getRawDataRefImpl());
861 const coff_relocation *COFFObjectFile::getCOFFRelocation(
862 relocation_iterator &It) const {
863 return toRel(It->getRawDataRefImpl());
866 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
867 case COFF::enum: res = #enum; break;
869 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
870 SmallVectorImpl<char> &Result) const {
871 const coff_relocation *reloc = toRel(Rel);
873 switch (COFFHeader->Machine) {
874 case COFF::IMAGE_FILE_MACHINE_AMD64:
875 switch (reloc->Type) {
876 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
877 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
878 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
879 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
880 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
881 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
882 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
883 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
884 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
885 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
886 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
887 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
888 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
889 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
890 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
891 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
892 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
897 case COFF::IMAGE_FILE_MACHINE_I386:
898 switch (reloc->Type) {
899 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
900 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
901 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
902 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
903 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
904 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
905 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
906 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
907 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
908 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
909 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
917 Result.append(res.begin(), res.end());
918 return object_error::success;
921 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
923 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
924 SmallVectorImpl<char> &Result) const {
925 const coff_relocation *reloc = toRel(Rel);
926 const coff_symbol *symb = 0;
927 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
929 sym.p = reinterpret_cast<uintptr_t>(symb);
931 if (error_code ec = getSymbolName(sym, symname)) return ec;
932 Result.append(symname.begin(), symname.end());
933 return object_error::success;
936 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
937 LibraryRef &Result) const {
938 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
941 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
942 StringRef &Result) const {
943 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
946 bool ImportDirectoryEntryRef::
947 operator==(const ImportDirectoryEntryRef &Other) const {
948 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
951 static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
952 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
956 ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
957 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
960 Next.p = reinterpret_cast<uintptr_t>(Dir);
961 Result = ImportDirectoryEntryRef(Next, OwningObject);
962 return object_error::success;
965 error_code ImportDirectoryEntryRef::
966 getImportTableEntry(const import_directory_table_entry *&Result) const {
967 Result = toImportEntry(ImportDirectoryPimpl);
968 return object_error::success;
971 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
972 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
973 uintptr_t IntPtr = 0;
974 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
976 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
977 Result = StringRef(Ptr);
978 return object_error::success;
981 error_code ImportDirectoryEntryRef::getImportLookupEntry(
982 const import_lookup_table_entry32 *&Result) const {
983 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
984 uintptr_t IntPtr = 0;
985 if (error_code ec = OwningObject->getRvaPtr(
986 Dir->ImportLookupTableRVA, IntPtr))
988 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
989 return object_error::success;
994 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
996 return new COFFObjectFile(Object, ec);
999 } // end namespace llvm