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;
26 using support::ulittle8_t;
27 using support::ulittle16_t;
28 using support::ulittle32_t;
29 using support::little16_t;
31 // Returns false if size is greater than the buffer size. And sets ec.
32 static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) {
33 if (M->getBufferSize() < Size) {
34 EC = object_error::unexpected_eof;
40 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
41 // Returns unexpected_eof if error.
43 static error_code getObject(const T *&Obj, const MemoryBuffer *M,
44 const uint8_t *Ptr, const size_t Size = sizeof(T)) {
45 uintptr_t Addr = uintptr_t(Ptr);
46 if (Addr + Size < Addr ||
48 Addr + Size > uintptr_t(M->getBufferEnd())) {
49 return object_error::unexpected_eof;
51 Obj = reinterpret_cast<const T *>(Addr);
52 return object_error::success;
55 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const {
56 const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p);
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 < COFFHeader->PointerToSymbolTable
62 || Offset >= COFFHeader->PointerToSymbolTable
63 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
64 report_fatal_error("Symbol was outside of symbol table.");
66 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
67 == 0 && "Symbol did not point to the beginning of a symbol");
73 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
74 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
77 // Verify that the section points to a valid entry in the section table.
78 if (Addr < SectionTable
79 || Addr >= (SectionTable + COFFHeader->NumberOfSections))
80 report_fatal_error("Section was outside of section table.");
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");
90 void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const {
91 const coff_symbol *Symb = toSymb(Ref);
92 Symb += 1 + Symb->NumberOfAuxSymbols;
93 Ref.p = reinterpret_cast<uintptr_t>(Symb);
96 error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
97 StringRef &Result) const {
98 const coff_symbol *Symb = toSymb(Ref);
99 return getSymbolName(Symb, Result);
102 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
103 uint64_t &Result) const {
104 const coff_symbol *Symb = toSymb(Ref);
105 const coff_section *Section = NULL;
106 if (error_code EC = getSection(Symb->SectionNumber, Section))
109 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
110 Result = UnknownAddressOrSize;
112 Result = Section->PointerToRawData + Symb->Value;
114 Result = Symb->Value;
115 return object_error::success;
118 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
119 uint64_t &Result) const {
120 const coff_symbol *Symb = toSymb(Ref);
121 const coff_section *Section = NULL;
122 if (error_code EC = getSection(Symb->SectionNumber, Section))
125 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
126 Result = UnknownAddressOrSize;
128 Result = Section->VirtualAddress + Symb->Value;
130 Result = Symb->Value;
131 return object_error::success;
134 error_code COFFObjectFile::getSymbolType(DataRefImpl Ref,
135 SymbolRef::Type &Result) const {
136 const coff_symbol *Symb = toSymb(Ref);
137 Result = SymbolRef::ST_Other;
138 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
139 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
140 Result = SymbolRef::ST_Unknown;
141 } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
142 Result = SymbolRef::ST_Function;
144 uint32_t Characteristics = 0;
145 if (Symb->SectionNumber > 0) {
146 const coff_section *Section = NULL;
147 if (error_code EC = getSection(Symb->SectionNumber, Section))
149 Characteristics = Section->Characteristics;
151 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
152 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
153 Result = SymbolRef::ST_Data;
155 return object_error::success;
158 uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
159 const coff_symbol *Symb = toSymb(Ref);
160 uint32_t Result = SymbolRef::SF_None;
162 // TODO: Correctly set SF_FormatSpecific, SF_Common
164 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
165 if (Symb->Value == 0)
166 Result |= SymbolRef::SF_Undefined;
168 Result |= SymbolRef::SF_Common;
172 // TODO: This are certainly too restrictive.
173 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
174 Result |= SymbolRef::SF_Global;
176 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
177 Result |= SymbolRef::SF_Weak;
179 if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
180 Result |= SymbolRef::SF_Absolute;
185 error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
186 uint64_t &Result) const {
187 // FIXME: Return the correct size. This requires looking at all the symbols
188 // in the same section as this symbol, and looking for either the next
189 // symbol, or the end of the section.
190 const coff_symbol *Symb = toSymb(Ref);
191 const coff_section *Section = NULL;
192 if (error_code EC = getSection(Symb->SectionNumber, Section))
195 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
196 Result = UnknownAddressOrSize;
198 Result = Section->SizeOfRawData - Symb->Value;
201 return object_error::success;
204 error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
205 section_iterator &Result) const {
206 const coff_symbol *Symb = toSymb(Ref);
207 if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
208 Result = end_sections();
210 const coff_section *Sec = 0;
211 if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
213 Ref.p = reinterpret_cast<uintptr_t>(Sec);
214 Result = section_iterator(SectionRef(Ref, this));
216 return object_error::success;
219 error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
220 uint64_t &Val) const {
221 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
224 void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const {
225 const coff_section *Sec = toSec(Ref);
227 Ref.p = reinterpret_cast<uintptr_t>(Sec);
230 error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
231 StringRef &Result) const {
232 const coff_section *Sec = toSec(Ref);
233 return getSectionName(Sec, Result);
236 error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
237 uint64_t &Result) const {
238 const coff_section *Sec = toSec(Ref);
239 Result = Sec->VirtualAddress;
240 return object_error::success;
243 error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
244 uint64_t &Result) const {
245 const coff_section *Sec = toSec(Ref);
246 Result = Sec->SizeOfRawData;
247 return object_error::success;
250 error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
251 StringRef &Result) const {
252 const coff_section *Sec = toSec(Ref);
253 ArrayRef<uint8_t> Res;
254 error_code EC = getSectionContents(Sec, Res);
255 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
259 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
260 uint64_t &Res) const {
261 const coff_section *Sec = toSec(Ref);
263 return object_error::parse_failed;
264 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
265 return object_error::success;
268 error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
269 bool &Result) const {
270 const coff_section *Sec = toSec(Ref);
271 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
272 return object_error::success;
275 error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
276 bool &Result) const {
277 const coff_section *Sec = toSec(Ref);
278 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
279 return object_error::success;
282 error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
283 bool &Result) const {
284 const coff_section *Sec = toSec(Ref);
285 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
286 return object_error::success;
289 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
290 bool &Result) const {
291 // FIXME: Unimplemented
293 return object_error::success;
296 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
297 bool &Result) const {
298 const coff_section *Sec = toSec(Ref);
299 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
300 return object_error::success;
303 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
304 bool &Result) const {
305 // FIXME: Unimplemented.
307 return object_error::success;
310 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
311 bool &Result) const {
312 // FIXME: Unimplemented.
314 return object_error::success;
317 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
319 bool &Result) const {
320 const coff_section *Sec = toSec(SecRef);
321 const coff_symbol *Symb = toSymb(SymbRef);
322 const coff_section *SymbSec = 0;
323 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
328 return object_error::success;
331 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
332 const coff_section *Sec = toSec(Ref);
334 if (Sec->NumberOfRelocations == 0)
337 Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations);
339 return relocation_iterator(RelocationRef(Ret, this));
342 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
343 const coff_section *Sec = toSec(Ref);
345 if (Sec->NumberOfRelocations == 0)
348 Ret.p = reinterpret_cast<uintptr_t>(
349 reinterpret_cast<const coff_relocation*>(
350 base() + Sec->PointerToRelocations)
351 + Sec->NumberOfRelocations);
353 return relocation_iterator(RelocationRef(Ret, this));
356 // Initialize the pointer to the symbol table.
357 error_code COFFObjectFile::initSymbolTablePtr() {
358 if (error_code EC = getObject(
359 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
360 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
363 // Find string table. The first four byte of the string table contains the
364 // total size of the string table, including the size field itself. If the
365 // string table is empty, the value of the first four byte would be 4.
366 const uint8_t *StringTableAddr =
367 base() + COFFHeader->PointerToSymbolTable +
368 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
369 const ulittle32_t *StringTableSizePtr;
370 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
372 StringTableSize = *StringTableSizePtr;
374 getObject(StringTable, Data, StringTableAddr, StringTableSize))
377 // Check that the string table is null terminated if has any in it.
378 if (StringTableSize < 4 ||
379 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
380 return object_error::parse_failed;
381 return object_error::success;
384 // Returns the file offset for the given RVA.
385 error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
386 for (section_iterator I = begin_sections(), E = end_sections(); I != E;
388 const coff_section *Section = getCOFFSection(I);
389 uint32_t SectionStart = Section->VirtualAddress;
390 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
391 if (SectionStart <= Rva && Rva < SectionEnd) {
392 uint32_t Offset = Rva - SectionStart;
393 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
394 return object_error::success;
397 return object_error::parse_failed;
400 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
402 error_code COFFObjectFile::
403 getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
404 uintptr_t IntPtr = 0;
405 if (error_code EC = getRvaPtr(Rva, IntPtr))
407 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
408 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
409 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
410 return object_error::success;
413 // Find the import table.
414 error_code COFFObjectFile::initImportTablePtr() {
415 // First, we get the RVA of the import table. If the file lacks a pointer to
416 // the import table, do nothing.
417 const data_directory *DataEntry;
418 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
419 return object_error::success;
421 // Do nothing if the pointer to import table is NULL.
422 if (DataEntry->RelativeVirtualAddress == 0)
423 return object_error::success;
425 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
426 NumberOfImportDirectory = DataEntry->Size /
427 sizeof(import_directory_table_entry);
429 // Find the section that contains the RVA. This is needed because the RVA is
430 // the import table's memory address which is different from its file offset.
431 uintptr_t IntPtr = 0;
432 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
434 ImportDirectory = reinterpret_cast<
435 const import_directory_table_entry *>(IntPtr);
436 return object_error::success;
439 // Find the export table.
440 error_code COFFObjectFile::initExportTablePtr() {
441 // First, we get the RVA of the export table. If the file lacks a pointer to
442 // the export table, do nothing.
443 const data_directory *DataEntry;
444 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
445 return object_error::success;
447 // Do nothing if the pointer to export table is NULL.
448 if (DataEntry->RelativeVirtualAddress == 0)
449 return object_error::success;
451 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
452 uintptr_t IntPtr = 0;
453 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
456 reinterpret_cast<const export_directory_table_entry *>(IntPtr);
457 return object_error::success;
460 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC,
462 : ObjectFile(Binary::ID_COFF, Object, BufferOwned), COFFHeader(0),
463 PE32Header(0), PE32PlusHeader(0), DataDirectory(0), SectionTable(0),
464 SymbolTable(0), StringTable(0), StringTableSize(0), ImportDirectory(0),
465 NumberOfImportDirectory(0), ExportDirectory(0) {
466 // Check that we at least have enough room for a header.
467 if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
469 // The current location in the file where we are looking at.
472 // PE header is optional and is present only in executables. If it exists,
473 // it is placed right after COFF header.
474 bool HasPEHeader = false;
476 // Check if this is a PE/COFF file.
477 if (base()[0] == 0x4d && base()[1] == 0x5a) {
478 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
479 // PE signature to find 'normal' COFF header.
480 if (!checkSize(Data, EC, 0x3c + 8)) return;
481 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
482 // Check the PE magic bytes. ("PE\0\0")
483 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
484 EC = object_error::parse_failed;
487 CurPtr += 4; // Skip the PE magic bytes.
491 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
493 CurPtr += sizeof(coff_file_header);
496 const pe32_header *Header;
497 if ((EC = getObject(Header, Data, base() + CurPtr)))
500 const uint8_t *DataDirAddr;
501 uint64_t DataDirSize;
502 if (Header->Magic == 0x10b) {
504 DataDirAddr = base() + CurPtr + sizeof(pe32_header);
505 DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
506 } else if (Header->Magic == 0x20b) {
507 PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
508 DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
509 DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
511 // It's neither PE32 nor PE32+.
512 EC = object_error::parse_failed;
515 if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
517 CurPtr += COFFHeader->SizeOfOptionalHeader;
520 if (COFFHeader->isImportLibrary())
523 if ((EC = getObject(SectionTable, Data, base() + CurPtr,
524 COFFHeader->NumberOfSections * sizeof(coff_section))))
527 // Initialize the pointer to the symbol table.
528 if (COFFHeader->PointerToSymbolTable != 0)
529 if ((EC = initSymbolTablePtr()))
532 // Initialize the pointer to the beginning of the import table.
533 if ((EC = initImportTablePtr()))
536 // Initialize the pointer to the export table.
537 if ((EC = initExportTablePtr()))
540 EC = object_error::success;
543 symbol_iterator COFFObjectFile::begin_symbols() const {
545 Ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
546 return symbol_iterator(SymbolRef(Ret, this));
549 symbol_iterator COFFObjectFile::end_symbols() const {
550 // The symbol table ends where the string table begins.
552 Ret.p = reinterpret_cast<uintptr_t>(StringTable);
553 return symbol_iterator(SymbolRef(Ret, this));
556 library_iterator COFFObjectFile::begin_libraries_needed() const {
558 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
561 library_iterator COFFObjectFile::end_libraries_needed() const {
563 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
566 StringRef COFFObjectFile::getLoadName() const {
567 // COFF does not have this field.
571 import_directory_iterator COFFObjectFile::import_directory_begin() const {
572 return import_directory_iterator(
573 ImportDirectoryEntryRef(ImportDirectory, 0, this));
576 import_directory_iterator COFFObjectFile::import_directory_end() const {
577 return import_directory_iterator(
578 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
581 export_directory_iterator COFFObjectFile::export_directory_begin() const {
582 return export_directory_iterator(
583 ExportDirectoryEntryRef(ExportDirectory, 0, this));
586 export_directory_iterator COFFObjectFile::export_directory_end() const {
587 if (ExportDirectory == 0)
588 return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
589 ExportDirectoryEntryRef Ref(ExportDirectory,
590 ExportDirectory->AddressTableEntries, this);
591 return export_directory_iterator(Ref);
594 section_iterator COFFObjectFile::begin_sections() const {
596 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
597 return section_iterator(SectionRef(Ret, this));
600 section_iterator COFFObjectFile::end_sections() const {
602 int NumSections = COFFHeader->isImportLibrary()
603 ? 0 : COFFHeader->NumberOfSections;
604 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
605 return section_iterator(SectionRef(Ret, this));
608 uint8_t COFFObjectFile::getBytesInAddress() const {
609 return getArch() == Triple::x86_64 ? 8 : 4;
612 StringRef COFFObjectFile::getFileFormatName() const {
613 switch(COFFHeader->Machine) {
614 case COFF::IMAGE_FILE_MACHINE_I386:
616 case COFF::IMAGE_FILE_MACHINE_AMD64:
617 return "COFF-x86-64";
619 return "COFF-<unknown arch>";
623 unsigned COFFObjectFile::getArch() const {
624 switch(COFFHeader->Machine) {
625 case COFF::IMAGE_FILE_MACHINE_I386:
627 case COFF::IMAGE_FILE_MACHINE_AMD64:
628 return Triple::x86_64;
630 return Triple::UnknownArch;
634 // This method is kept here because lld uses this. As soon as we make
635 // lld to use getCOFFHeader, this method will be removed.
636 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
637 return getCOFFHeader(Res);
640 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
642 return object_error::success;
645 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
647 return object_error::success;
651 COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const {
652 Res = PE32PlusHeader;
653 return object_error::success;
656 error_code COFFObjectFile::getDataDirectory(uint32_t Index,
657 const data_directory *&Res) const {
658 // Error if if there's no data directory or the index is out of range.
660 return object_error::parse_failed;
661 assert(PE32Header || PE32PlusHeader);
662 uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
663 : PE32PlusHeader->NumberOfRvaAndSize;
665 return object_error::parse_failed;
666 Res = &DataDirectory[Index];
667 return object_error::success;
670 error_code COFFObjectFile::getSection(int32_t Index,
671 const coff_section *&Result) const {
672 // Check for special index values.
673 if (Index == COFF::IMAGE_SYM_UNDEFINED ||
674 Index == COFF::IMAGE_SYM_ABSOLUTE ||
675 Index == COFF::IMAGE_SYM_DEBUG)
677 else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
678 // We already verified the section table data, so no need to check again.
679 Result = SectionTable + (Index - 1);
681 return object_error::parse_failed;
682 return object_error::success;
685 error_code COFFObjectFile::getString(uint32_t Offset,
686 StringRef &Result) const {
687 if (StringTableSize <= 4)
688 // Tried to get a string from an empty string table.
689 return object_error::parse_failed;
690 if (Offset >= StringTableSize)
691 return object_error::unexpected_eof;
692 Result = StringRef(StringTable + Offset);
693 return object_error::success;
696 error_code COFFObjectFile::getSymbol(uint32_t Index,
697 const coff_symbol *&Result) const {
698 if (Index < COFFHeader->NumberOfSymbols)
699 Result = SymbolTable + Index;
701 return object_error::parse_failed;
702 return object_error::success;
705 error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
706 StringRef &Res) const {
707 // Check for string table entry. First 4 bytes are 0.
708 if (Symbol->Name.Offset.Zeroes == 0) {
709 uint32_t Offset = Symbol->Name.Offset.Offset;
710 if (error_code EC = getString(Offset, Res))
712 return object_error::success;
715 if (Symbol->Name.ShortName[7] == 0)
716 // Null terminated, let ::strlen figure out the length.
717 Res = StringRef(Symbol->Name.ShortName);
719 // Not null terminated, use all 8 bytes.
720 Res = StringRef(Symbol->Name.ShortName, 8);
721 return object_error::success;
724 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
725 const coff_symbol *Symbol) const {
726 const uint8_t *Aux = NULL;
728 if (Symbol->NumberOfAuxSymbols > 0) {
729 // AUX data comes immediately after the symbol in COFF
730 Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
732 // Verify that the Aux symbol points to a valid entry in the symbol table.
733 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
734 if (Offset < COFFHeader->PointerToSymbolTable
735 || Offset >= COFFHeader->PointerToSymbolTable
736 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
737 report_fatal_error("Aux Symbol data was outside of symbol table.");
739 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
740 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
743 return ArrayRef<uint8_t>(Aux,
744 Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
747 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
748 StringRef &Res) const {
750 if (Sec->Name[7] == 0)
751 // Null terminated, let ::strlen figure out the length.
754 // Not null terminated, use all 8 bytes.
755 Name = StringRef(Sec->Name, 8);
757 // Check for string table entry. First byte is '/'.
758 if (Name[0] == '/') {
760 if (Name.substr(1).getAsInteger(10, Offset))
761 return object_error::parse_failed;
762 if (error_code EC = getString(Offset, Name))
767 return object_error::success;
770 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
771 ArrayRef<uint8_t> &Res) const {
772 // The only thing that we need to verify is that the contents is contained
773 // within the file bounds. We don't need to make sure it doesn't cover other
774 // data, as there's nothing that says that is not allowed.
775 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
776 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
777 if (ConEnd > uintptr_t(Data->getBufferEnd()))
778 return object_error::parse_failed;
779 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
781 return object_error::success;
784 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
785 return reinterpret_cast<const coff_relocation*>(Rel.p);
788 void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
789 Rel.p = reinterpret_cast<uintptr_t>(
790 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
793 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
794 uint64_t &Res) const {
795 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
798 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
799 uint64_t &Res) const {
800 Res = toRel(Rel)->VirtualAddress;
801 return object_error::success;
804 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
805 const coff_relocation* R = toRel(Rel);
807 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
808 return symbol_iterator(SymbolRef(Ref, this));
811 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
812 uint64_t &Res) const {
813 const coff_relocation* R = toRel(Rel);
815 return object_error::success;
818 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
819 return toSec(It->getRawDataRefImpl());
822 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
823 return toSymb(It->getRawDataRefImpl());
826 const coff_relocation *COFFObjectFile::getCOFFRelocation(
827 relocation_iterator &It) const {
828 return toRel(It->getRawDataRefImpl());
831 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
832 case COFF::enum: Res = #enum; break;
834 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
835 SmallVectorImpl<char> &Result) const {
836 const coff_relocation *Reloc = toRel(Rel);
838 switch (COFFHeader->Machine) {
839 case COFF::IMAGE_FILE_MACHINE_AMD64:
840 switch (Reloc->Type) {
841 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
842 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
843 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
844 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
845 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
846 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
847 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
848 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
849 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
850 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
851 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
852 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
853 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
854 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
855 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
856 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
857 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
862 case COFF::IMAGE_FILE_MACHINE_I386:
863 switch (Reloc->Type) {
864 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
865 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
866 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
867 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
868 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
869 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
870 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
871 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
872 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
873 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
874 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
882 Result.append(Res.begin(), Res.end());
883 return object_error::success;
886 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
888 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
889 SmallVectorImpl<char> &Result) const {
890 const coff_relocation *Reloc = toRel(Rel);
891 const coff_symbol *Symb = 0;
892 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
894 Sym.p = reinterpret_cast<uintptr_t>(Symb);
896 if (error_code EC = getSymbolName(Sym, SymName)) return EC;
897 Result.append(SymName.begin(), SymName.end());
898 return object_error::success;
901 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
902 LibraryRef &Result) const {
903 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
906 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
907 StringRef &Result) const {
908 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
911 bool ImportDirectoryEntryRef::
912 operator==(const ImportDirectoryEntryRef &Other) const {
913 return ImportTable == Other.ImportTable && Index == Other.Index;
916 void ImportDirectoryEntryRef::moveNext() {
920 error_code ImportDirectoryEntryRef::
921 getImportTableEntry(const import_directory_table_entry *&Result) const {
922 Result = ImportTable;
923 return object_error::success;
926 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
927 uintptr_t IntPtr = 0;
928 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
930 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
931 return object_error::success;
934 error_code ImportDirectoryEntryRef::getImportLookupEntry(
935 const import_lookup_table_entry32 *&Result) const {
936 uintptr_t IntPtr = 0;
938 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
940 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
941 return object_error::success;
944 bool ExportDirectoryEntryRef::
945 operator==(const ExportDirectoryEntryRef &Other) const {
946 return ExportTable == Other.ExportTable && Index == Other.Index;
949 void ExportDirectoryEntryRef::moveNext() {
953 // Returns the name of the current export symbol. If the symbol is exported only
954 // by ordinal, the empty string is set as a result.
955 error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
956 uintptr_t IntPtr = 0;
957 if (error_code EC = OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
959 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
960 return object_error::success;
963 // Returns the starting ordinal number.
964 error_code ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const {
965 Result = ExportTable->OrdinalBase;
966 return object_error::success;
969 // Returns the export ordinal of the current export symbol.
970 error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
971 Result = ExportTable->OrdinalBase + Index;
972 return object_error::success;
975 // Returns the address of the current export symbol.
976 error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
977 uintptr_t IntPtr = 0;
978 if (error_code EC = OwningObject->getRvaPtr(
979 ExportTable->ExportAddressTableRVA, IntPtr))
981 const export_address_table_entry *entry =
982 reinterpret_cast<const export_address_table_entry *>(IntPtr);
983 Result = entry[Index].ExportRVA;
984 return object_error::success;
987 // Returns the name of the current export symbol. If the symbol is exported only
988 // by ordinal, the empty string is set as a result.
989 error_code ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const {
990 uintptr_t IntPtr = 0;
991 if (error_code EC = OwningObject->getRvaPtr(
992 ExportTable->OrdinalTableRVA, IntPtr))
994 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
996 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
998 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
999 I < E; ++I, ++Offset) {
1002 if (error_code EC = OwningObject->getRvaPtr(
1003 ExportTable->NamePointerRVA, IntPtr))
1005 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1006 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1008 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1009 return object_error::success;
1012 return object_error::success;
1015 ErrorOr<ObjectFile *> ObjectFile::createCOFFObjectFile(MemoryBuffer *Object,
1018 OwningPtr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC, BufferOwned));