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"
23 using namespace object;
26 using support::ulittle8_t;
27 using support::ulittle16_t;
28 using support::ulittle32_t;
29 using support::little16_t;
33 // Returns false if size is greater than the buffer size. And sets ec.
34 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
35 if (m->getBufferSize() < size) {
36 ec = object_error::unexpected_eof;
42 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
43 // Returns unexpected_eof if error.
45 error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
46 const size_t Size = sizeof(T)) {
47 uintptr_t Addr = uintptr_t(Ptr);
48 if (Addr + Size < Addr ||
50 Addr + Size > uintptr_t(M->getBufferEnd())) {
51 return object_error::unexpected_eof;
53 Obj = reinterpret_cast<const T *>(Addr);
54 return object_error::success;
58 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
59 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
62 // Verify that the symbol points to a valid entry in the symbol table.
63 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
64 if (offset < COFFHeader->PointerToSymbolTable
65 || offset >= COFFHeader->PointerToSymbolTable
66 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
67 report_fatal_error("Symbol was outside of symbol table.");
69 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
70 == 0 && "Symbol did not point to the beginning of a symbol");
76 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
77 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
80 // Verify that the section points to a valid entry in the section table.
81 if (addr < SectionTable
82 || addr >= (SectionTable + COFFHeader->NumberOfSections))
83 report_fatal_error("Section was outside of section table.");
85 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
86 assert(offset % sizeof(coff_section) == 0 &&
87 "Section did not point to the beginning of a section");
93 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
94 SymbolRef &Result) const {
95 const coff_symbol *symb = toSymb(Symb);
96 symb += 1 + symb->NumberOfAuxSymbols;
97 Symb.p = reinterpret_cast<uintptr_t>(symb);
98 Result = SymbolRef(Symb, this);
99 return object_error::success;
102 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
103 StringRef &Result) const {
104 const coff_symbol *symb = toSymb(Symb);
105 return getSymbolName(symb, Result);
108 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
109 uint64_t &Result) const {
110 const coff_symbol *symb = toSymb(Symb);
111 const coff_section *Section = NULL;
112 if (error_code ec = getSection(symb->SectionNumber, Section))
115 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
117 if (Type == 'U' || Type == 'w')
118 Result = UnknownAddressOrSize;
120 Result = Section->PointerToRawData + symb->Value;
122 Result = symb->Value;
123 return object_error::success;
126 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
127 uint64_t &Result) const {
128 const coff_symbol *symb = toSymb(Symb);
129 const coff_section *Section = NULL;
130 if (error_code ec = getSection(symb->SectionNumber, Section))
133 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
135 if (Type == 'U' || Type == 'w')
136 Result = UnknownAddressOrSize;
138 Result = Section->VirtualAddress + symb->Value;
140 Result = symb->Value;
141 return object_error::success;
144 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
145 SymbolRef::Type &Result) const {
146 const coff_symbol *symb = toSymb(Symb);
147 Result = SymbolRef::ST_Other;
148 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
149 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
150 Result = SymbolRef::ST_Unknown;
152 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
153 Result = SymbolRef::ST_Function;
156 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
158 if (Type == 'r' || Type == 'R') {
159 Result = SymbolRef::ST_Data;
163 return object_error::success;
166 error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
167 uint32_t &Result) const {
168 const coff_symbol *symb = toSymb(Symb);
169 Result = SymbolRef::SF_None;
171 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
173 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
174 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
175 Result |= SymbolRef::SF_Undefined;
177 // TODO: This are certainly too restrictive.
178 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
179 Result |= SymbolRef::SF_Global;
181 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
182 Result |= SymbolRef::SF_Weak;
184 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
185 Result |= SymbolRef::SF_Absolute;
187 return object_error::success;
190 error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
191 uint64_t &Result) const {
192 // FIXME: Return the correct size. This requires looking at all the symbols
193 // in the same section as this symbol, and looking for either the next
194 // symbol, or the end of the section.
195 const coff_symbol *symb = toSymb(Symb);
196 const coff_section *Section = NULL;
197 if (error_code ec = getSection(symb->SectionNumber, Section))
200 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
202 if (Type == 'U' || Type == 'w')
203 Result = UnknownAddressOrSize;
205 Result = Section->SizeOfRawData - symb->Value;
208 return object_error::success;
211 error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
212 char &Result) const {
213 const coff_symbol *symb = toSymb(Symb);
215 if (error_code ec = getSymbolName(Symb, name))
217 char ret = StringSwitch<char>(name)
218 .StartsWith(".debug", 'N')
219 .StartsWith(".sxdata", 'N')
224 return object_error::success;
227 uint32_t Characteristics = 0;
228 if (symb->SectionNumber > 0) {
229 const coff_section *Section = NULL;
230 if (error_code ec = getSection(symb->SectionNumber, Section))
232 Characteristics = Section->Characteristics;
235 switch (symb->SectionNumber) {
236 case COFF::IMAGE_SYM_UNDEFINED:
237 // Check storage classes.
238 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
240 return object_error::success; // Don't do ::toupper.
241 } else if (symb->Value != 0) // Check for common symbols.
246 case COFF::IMAGE_SYM_ABSOLUTE:
249 case COFF::IMAGE_SYM_DEBUG:
253 // Check section type.
254 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
256 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
257 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
259 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
261 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
263 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
266 // Check for section symbol.
267 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
272 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
273 ret = ::toupper(static_cast<unsigned char>(ret));
276 return object_error::success;
279 error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
280 section_iterator &Result) const {
281 const coff_symbol *symb = toSymb(Symb);
282 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
283 Result = end_sections();
285 const coff_section *sec = 0;
286 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
288 Sec.p = reinterpret_cast<uintptr_t>(sec);
289 Result = section_iterator(SectionRef(Sec, this));
291 return object_error::success;
294 error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
295 uint64_t &Val) const {
296 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
299 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
300 SectionRef &Result) const {
301 const coff_section *sec = toSec(Sec);
303 Sec.p = reinterpret_cast<uintptr_t>(sec);
304 Result = SectionRef(Sec, this);
305 return object_error::success;
308 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
309 StringRef &Result) const {
310 const coff_section *sec = toSec(Sec);
311 return getSectionName(sec, Result);
314 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
315 uint64_t &Result) const {
316 const coff_section *sec = toSec(Sec);
317 Result = sec->VirtualAddress;
318 return object_error::success;
321 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
322 uint64_t &Result) const {
323 const coff_section *sec = toSec(Sec);
324 Result = sec->SizeOfRawData;
325 return object_error::success;
328 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
329 StringRef &Result) const {
330 const coff_section *sec = toSec(Sec);
331 ArrayRef<uint8_t> Res;
332 error_code EC = getSectionContents(sec, Res);
333 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
337 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
338 uint64_t &Res) const {
339 const coff_section *sec = toSec(Sec);
341 return object_error::parse_failed;
342 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
343 return object_error::success;
346 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
347 bool &Result) const {
348 const coff_section *sec = toSec(Sec);
349 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
350 return object_error::success;
353 error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
354 bool &Result) const {
355 const coff_section *sec = toSec(Sec);
356 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
357 return object_error::success;
360 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
361 bool &Result) const {
362 const coff_section *sec = toSec(Sec);
363 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
364 return object_error::success;
367 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
368 bool &Result) const {
369 // FIXME: Unimplemented
371 return object_error::success;
374 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
375 bool &Result) const {
376 const coff_section *sec = toSec(Sec);
377 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
378 return object_error::success;
381 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
382 bool &Result) const {
383 // FIXME: Unimplemented.
385 return object_error::success;
388 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
389 bool &Result) const {
390 // FIXME: Unimplemented.
392 return object_error::success;
395 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
397 bool &Result) const {
398 const coff_section *sec = toSec(Sec);
399 const coff_symbol *symb = toSymb(Symb);
400 const coff_section *symb_sec = 0;
401 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
406 return object_error::success;
409 relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
410 const coff_section *sec = toSec(Sec);
412 if (sec->NumberOfRelocations == 0)
415 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
417 return relocation_iterator(RelocationRef(ret, this));
420 relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
421 const coff_section *sec = toSec(Sec);
423 if (sec->NumberOfRelocations == 0)
426 ret.p = reinterpret_cast<uintptr_t>(
427 reinterpret_cast<const coff_relocation*>(
428 base() + sec->PointerToRelocations)
429 + sec->NumberOfRelocations);
431 return relocation_iterator(RelocationRef(ret, this));
434 // Initialize the pointer to the symbol table.
435 error_code COFFObjectFile::initSymbolTablePtr() {
436 if (error_code ec = getObject(
437 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
438 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
441 // Find string table. The first four byte of the string table contains the
442 // total size of the string table, including the size field itself. If the
443 // string table is empty, the value of the first four byte would be 4.
444 const uint8_t *StringTableAddr =
445 base() + COFFHeader->PointerToSymbolTable +
446 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
447 const ulittle32_t *StringTableSizePtr;
448 if (error_code ec = getObject(StringTableSizePtr, Data, StringTableAddr))
450 StringTableSize = *StringTableSizePtr;
452 getObject(StringTable, Data, StringTableAddr, StringTableSize))
455 // Check that the string table is null terminated if has any in it.
456 if (StringTableSize < 4 ||
457 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
458 return object_error::parse_failed;
459 return object_error::success;
462 // Returns the file offset for the given RVA.
463 error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
465 for (section_iterator i = begin_sections(), e = end_sections(); i != e;
469 const coff_section *Section = getCOFFSection(i);
470 uint32_t SectionStart = Section->VirtualAddress;
471 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
472 if (SectionStart <= Rva && Rva < SectionEnd) {
473 uint32_t Offset = Rva - SectionStart;
474 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
475 return object_error::success;
478 return object_error::parse_failed;
481 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
483 error_code COFFObjectFile::
484 getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
485 uintptr_t IntPtr = 0;
486 if (error_code ec = getRvaPtr(Rva, IntPtr))
488 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
489 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
490 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
491 return object_error::success;
494 // Find the import table.
495 error_code COFFObjectFile::initImportTablePtr() {
496 // First, we get the RVA of the import table. If the file lacks a pointer to
497 // the import table, do nothing.
498 const data_directory *DataEntry;
499 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
500 return object_error::success;
502 // Do nothing if the pointer to import table is NULL.
503 if (DataEntry->RelativeVirtualAddress == 0)
504 return object_error::success;
506 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
507 NumberOfImportDirectory = DataEntry->Size /
508 sizeof(import_directory_table_entry);
510 // Find the section that contains the RVA. This is needed because the RVA is
511 // the import table's memory address which is different from its file offset.
512 uintptr_t IntPtr = 0;
513 if (error_code ec = getRvaPtr(ImportTableRva, IntPtr))
515 ImportDirectory = reinterpret_cast<
516 const import_directory_table_entry *>(IntPtr);
518 // It's an error if there's no section containing the Import Table RVA.
519 return object_error::parse_failed;
522 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
523 : ObjectFile(Binary::ID_COFF, Object)
532 , NumberOfImportDirectory(0) {
533 // Check that we at least have enough room for a header.
534 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
536 // The current location in the file where we are looking at.
539 // PE header is optional and is present only in executables. If it exists,
540 // it is placed right after COFF header.
541 bool hasPEHeader = false;
543 // Check if this is a PE/COFF file.
544 if (base()[0] == 0x4d && base()[1] == 0x5a) {
545 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
546 // PE signature to find 'normal' COFF header.
547 if (!checkSize(Data, ec, 0x3c + 8)) return;
548 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
549 // Check the PE magic bytes. ("PE\0\0")
550 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
551 ec = object_error::parse_failed;
554 CurPtr += 4; // Skip the PE magic bytes.
558 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
560 CurPtr += sizeof(coff_file_header);
563 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
565 if (PE32Header->Magic != 0x10b) {
566 // We only support PE32. If this is PE32 (not PE32+), the magic byte
567 // should be 0x10b. If this is not PE32, continue as if there's no PE
568 // header in this file.
570 } else if (PE32Header->NumberOfRvaAndSize > 0) {
571 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
572 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
573 if ((ec = getObject(DataDirectory, Data, addr, size)))
576 CurPtr += COFFHeader->SizeOfOptionalHeader;
579 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
580 COFFHeader->NumberOfSections * sizeof(coff_section))))
583 // Initialize the pointer to the symbol table.
584 if (COFFHeader->PointerToSymbolTable != 0)
585 if ((ec = initSymbolTablePtr()))
588 // Initialize the pointer to the beginning of the import table.
589 if ((ec = initImportTablePtr()))
592 ec = object_error::success;
595 symbol_iterator COFFObjectFile::begin_symbols() const {
597 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
598 return symbol_iterator(SymbolRef(ret, this));
601 symbol_iterator COFFObjectFile::end_symbols() const {
602 // The symbol table ends where the string table begins.
604 ret.p = reinterpret_cast<uintptr_t>(StringTable);
605 return symbol_iterator(SymbolRef(ret, this));
608 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
610 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
613 symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
615 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
618 library_iterator COFFObjectFile::begin_libraries_needed() const {
620 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
623 library_iterator COFFObjectFile::end_libraries_needed() const {
625 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
628 StringRef COFFObjectFile::getLoadName() const {
629 // COFF does not have this field.
633 import_directory_iterator COFFObjectFile::getImportDirectoryBegin() const {
635 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
636 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
639 import_directory_iterator COFFObjectFile::getImportDirectoryEnd() const {
641 if (ImportDirectory) {
642 Imp.p = reinterpret_cast<uintptr_t>(
643 ImportDirectory + (NumberOfImportDirectory - 1));
647 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
650 section_iterator COFFObjectFile::begin_sections() const {
652 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
653 return section_iterator(SectionRef(ret, this));
656 section_iterator COFFObjectFile::end_sections() const {
658 ret.p = reinterpret_cast<uintptr_t>(SectionTable + COFFHeader->NumberOfSections);
659 return section_iterator(SectionRef(ret, this));
662 uint8_t COFFObjectFile::getBytesInAddress() const {
663 return getArch() == Triple::x86_64 ? 8 : 4;
666 StringRef COFFObjectFile::getFileFormatName() const {
667 switch(COFFHeader->Machine) {
668 case COFF::IMAGE_FILE_MACHINE_I386:
670 case COFF::IMAGE_FILE_MACHINE_AMD64:
671 return "COFF-x86-64";
673 return "COFF-<unknown arch>";
677 unsigned COFFObjectFile::getArch() const {
678 switch(COFFHeader->Machine) {
679 case COFF::IMAGE_FILE_MACHINE_I386:
681 case COFF::IMAGE_FILE_MACHINE_AMD64:
682 return Triple::x86_64;
684 return Triple::UnknownArch;
688 // This method is kept here because lld uses this. As soon as we make
689 // lld to use getCOFFHeader, this method will be removed.
690 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
691 return getCOFFHeader(Res);
694 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
696 return object_error::success;
699 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
701 return object_error::success;
704 error_code COFFObjectFile::getDataDirectory(uint32_t index,
705 const data_directory *&Res) const {
706 // Error if if there's no data directory or the index is out of range.
707 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
708 return object_error::parse_failed;
709 Res = &DataDirectory[index];
710 return object_error::success;
713 error_code COFFObjectFile::getSection(int32_t index,
714 const coff_section *&Result) const {
715 // Check for special index values.
716 if (index == COFF::IMAGE_SYM_UNDEFINED ||
717 index == COFF::IMAGE_SYM_ABSOLUTE ||
718 index == COFF::IMAGE_SYM_DEBUG)
720 else if (index > 0 && index <= COFFHeader->NumberOfSections)
721 // We already verified the section table data, so no need to check again.
722 Result = SectionTable + (index - 1);
724 return object_error::parse_failed;
725 return object_error::success;
728 error_code COFFObjectFile::getString(uint32_t offset,
729 StringRef &Result) const {
730 if (StringTableSize <= 4)
731 // Tried to get a string from an empty string table.
732 return object_error::parse_failed;
733 if (offset >= StringTableSize)
734 return object_error::unexpected_eof;
735 Result = StringRef(StringTable + offset);
736 return object_error::success;
739 error_code COFFObjectFile::getSymbol(uint32_t index,
740 const coff_symbol *&Result) const {
741 if (index < COFFHeader->NumberOfSymbols)
742 Result = SymbolTable + index;
744 return object_error::parse_failed;
745 return object_error::success;
748 error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
749 StringRef &Res) const {
750 // Check for string table entry. First 4 bytes are 0.
751 if (symbol->Name.Offset.Zeroes == 0) {
752 uint32_t Offset = symbol->Name.Offset.Offset;
753 if (error_code ec = getString(Offset, Res))
755 return object_error::success;
758 if (symbol->Name.ShortName[7] == 0)
759 // Null terminated, let ::strlen figure out the length.
760 Res = StringRef(symbol->Name.ShortName);
762 // Not null terminated, use all 8 bytes.
763 Res = StringRef(symbol->Name.ShortName, 8);
764 return object_error::success;
767 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
768 const coff_symbol *symbol) const {
769 const uint8_t *aux = NULL;
771 if ( symbol->NumberOfAuxSymbols > 0 ) {
772 // AUX data comes immediately after the symbol in COFF
773 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
775 // Verify that the aux symbol points to a valid entry in the symbol table.
776 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
777 if (offset < COFFHeader->PointerToSymbolTable
778 || offset >= COFFHeader->PointerToSymbolTable
779 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
780 report_fatal_error("Aux Symbol data was outside of symbol table.");
782 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
783 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
786 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
789 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
790 StringRef &Res) const {
792 if (Sec->Name[7] == 0)
793 // Null terminated, let ::strlen figure out the length.
796 // Not null terminated, use all 8 bytes.
797 Name = StringRef(Sec->Name, 8);
799 // Check for string table entry. First byte is '/'.
800 if (Name[0] == '/') {
802 if (Name.substr(1).getAsInteger(10, Offset))
803 return object_error::parse_failed;
804 if (error_code ec = getString(Offset, Name))
809 return object_error::success;
812 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
813 ArrayRef<uint8_t> &Res) const {
814 // The only thing that we need to verify is that the contents is contained
815 // within the file bounds. We don't need to make sure it doesn't cover other
816 // data, as there's nothing that says that is not allowed.
817 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
818 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
819 if (ConEnd > uintptr_t(Data->getBufferEnd()))
820 return object_error::parse_failed;
821 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
823 return object_error::success;
826 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
827 return reinterpret_cast<const coff_relocation*>(Rel.p);
829 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
830 RelocationRef &Res) const {
831 Rel.p = reinterpret_cast<uintptr_t>(
832 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
833 Res = RelocationRef(Rel, this);
834 return object_error::success;
836 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
837 uint64_t &Res) const {
838 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
840 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
841 uint64_t &Res) const {
842 Res = toRel(Rel)->VirtualAddress;
843 return object_error::success;
845 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
846 const coff_relocation* R = toRel(Rel);
848 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
849 return symbol_iterator(SymbolRef(Symb, this));
851 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
852 uint64_t &Res) const {
853 const coff_relocation* R = toRel(Rel);
855 return object_error::success;
858 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
859 return toSec(It->getRawDataRefImpl());
862 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
863 return toSymb(It->getRawDataRefImpl());
866 const coff_relocation *COFFObjectFile::getCOFFRelocation(
867 relocation_iterator &It) const {
868 return toRel(It->getRawDataRefImpl());
871 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
872 case COFF::enum: res = #enum; break;
874 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
875 SmallVectorImpl<char> &Result) const {
876 const coff_relocation *reloc = toRel(Rel);
878 switch (COFFHeader->Machine) {
879 case COFF::IMAGE_FILE_MACHINE_AMD64:
880 switch (reloc->Type) {
881 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
882 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
883 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
884 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
885 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
886 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
887 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
888 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
889 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
890 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
891 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
892 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
893 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
894 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
895 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
896 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
897 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
902 case COFF::IMAGE_FILE_MACHINE_I386:
903 switch (reloc->Type) {
904 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
905 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
906 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
907 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
908 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
909 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
910 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
911 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
912 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
913 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
914 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
922 Result.append(res.begin(), res.end());
923 return object_error::success;
926 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
928 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
929 SmallVectorImpl<char> &Result) const {
930 const coff_relocation *reloc = toRel(Rel);
931 const coff_symbol *symb = 0;
932 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
934 sym.p = reinterpret_cast<uintptr_t>(symb);
936 if (error_code ec = getSymbolName(sym, symname)) return ec;
937 Result.append(symname.begin(), symname.end());
938 return object_error::success;
941 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
942 LibraryRef &Result) const {
943 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
946 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
947 StringRef &Result) const {
948 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
951 bool ImportDirectoryEntryRef::
952 operator==(const ImportDirectoryEntryRef &Other) const {
953 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
956 static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
957 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
961 ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
962 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
965 Next.p = reinterpret_cast<uintptr_t>(Dir);
966 Result = ImportDirectoryEntryRef(Next, OwningObject);
967 return object_error::success;
970 error_code ImportDirectoryEntryRef::
971 getImportTableEntry(const import_directory_table_entry *&Result) const {
972 Result = toImportEntry(ImportDirectoryPimpl);
973 return object_error::success;
976 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
977 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
978 uintptr_t IntPtr = 0;
979 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
981 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
982 Result = StringRef(Ptr);
983 return object_error::success;
986 error_code ImportDirectoryEntryRef::getImportLookupEntry(
987 const COFF::ImportLookupTableEntry32 *&Result) const {
988 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
989 uintptr_t IntPtr = 0;
990 if (error_code ec = OwningObject->getRvaPtr(
991 Dir->ImportLookupTableRVA, IntPtr))
993 Result = reinterpret_cast<const COFF::ImportLookupTableEntry32 *>(IntPtr);
994 return object_error::success;
999 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
1001 return new COFFObjectFile(Object, ec);
1004 } // end namespace llvm