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 Ref) const {
60 const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.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 Ref) const {
78 const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.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 Ref,
95 SymbolRef &Result) const {
96 const coff_symbol *Symb = toSymb(Ref);
97 Symb += 1 + Symb->NumberOfAuxSymbols;
98 Ref.p = reinterpret_cast<uintptr_t>(Symb);
99 Result = SymbolRef(Ref, this);
100 return object_error::success;
103 error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
104 StringRef &Result) const {
105 const coff_symbol *Symb = toSymb(Ref);
106 return getSymbolName(Symb, Result);
109 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref,
110 uint64_t &Result) const {
111 const coff_symbol *Symb = toSymb(Ref);
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 Ref,
126 uint64_t &Result) const {
127 const coff_symbol *Symb = toSymb(Ref);
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 Ref,
142 SymbolRef::Type &Result) const {
143 const coff_symbol *Symb = toSymb(Ref);
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;
152 uint32_t Characteristics = 0;
153 if (Symb->SectionNumber > 0) {
154 const coff_section *Section = NULL;
155 if (error_code EC = getSection(Symb->SectionNumber, Section))
157 Characteristics = Section->Characteristics;
159 if (Characteristics & COFF::IMAGE_SCN_MEM_READ &&
160 ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
161 Result = SymbolRef::ST_Data;
164 return object_error::success;
167 error_code COFFObjectFile::getSymbolFlags(DataRefImpl Ref,
168 uint32_t &Result) const {
169 const coff_symbol *Symb = toSymb(Ref);
170 Result = SymbolRef::SF_None;
172 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
174 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
175 Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
176 Result |= SymbolRef::SF_Undefined;
178 // TODO: This are certainly too restrictive.
179 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
180 Result |= SymbolRef::SF_Global;
182 if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
183 Result |= SymbolRef::SF_Weak;
185 if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
186 Result |= SymbolRef::SF_Absolute;
188 return object_error::success;
191 error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref,
192 uint64_t &Result) const {
193 // FIXME: Return the correct size. This requires looking at all the symbols
194 // in the same section as this symbol, and looking for either the next
195 // symbol, or the end of the section.
196 const coff_symbol *Symb = toSymb(Ref);
197 const coff_section *Section = NULL;
198 if (error_code EC = getSection(Symb->SectionNumber, Section))
201 if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
202 Result = UnknownAddressOrSize;
204 Result = Section->SizeOfRawData - Symb->Value;
207 return object_error::success;
210 error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
211 section_iterator &Result) const {
212 const coff_symbol *Symb = toSymb(Ref);
213 if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
214 Result = end_sections();
216 const coff_section *Sec = 0;
217 if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
219 Ref.p = reinterpret_cast<uintptr_t>(Sec);
220 Result = section_iterator(SectionRef(Ref, this));
222 return object_error::success;
225 error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref,
226 uint64_t &Val) const {
227 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
230 error_code COFFObjectFile::getSectionNext(DataRefImpl Ref,
231 SectionRef &Result) const {
232 const coff_section *Sec = toSec(Ref);
234 Ref.p = reinterpret_cast<uintptr_t>(Sec);
235 Result = SectionRef(Ref, this);
236 return object_error::success;
239 error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
240 StringRef &Result) const {
241 const coff_section *Sec = toSec(Ref);
242 return getSectionName(Sec, Result);
245 error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref,
246 uint64_t &Result) const {
247 const coff_section *Sec = toSec(Ref);
248 Result = Sec->VirtualAddress;
249 return object_error::success;
252 error_code COFFObjectFile::getSectionSize(DataRefImpl Ref,
253 uint64_t &Result) const {
254 const coff_section *Sec = toSec(Ref);
255 Result = Sec->SizeOfRawData;
256 return object_error::success;
259 error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
260 StringRef &Result) const {
261 const coff_section *Sec = toSec(Ref);
262 ArrayRef<uint8_t> Res;
263 error_code EC = getSectionContents(Sec, Res);
264 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
268 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref,
269 uint64_t &Res) const {
270 const coff_section *Sec = toSec(Ref);
272 return object_error::parse_failed;
273 Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1);
274 return object_error::success;
277 error_code COFFObjectFile::isSectionText(DataRefImpl Ref,
278 bool &Result) const {
279 const coff_section *Sec = toSec(Ref);
280 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
281 return object_error::success;
284 error_code COFFObjectFile::isSectionData(DataRefImpl Ref,
285 bool &Result) const {
286 const coff_section *Sec = toSec(Ref);
287 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
288 return object_error::success;
291 error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref,
292 bool &Result) const {
293 const coff_section *Sec = toSec(Ref);
294 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
295 return object_error::success;
298 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref,
299 bool &Result) const {
300 // FIXME: Unimplemented
302 return object_error::success;
305 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref,
306 bool &Result) const {
307 const coff_section *Sec = toSec(Ref);
308 Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
309 return object_error::success;
312 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref,
313 bool &Result) const {
314 // FIXME: Unimplemented.
316 return object_error::success;
319 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref,
320 bool &Result) const {
321 // FIXME: Unimplemented.
323 return object_error::success;
326 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
328 bool &Result) const {
329 const coff_section *Sec = toSec(SecRef);
330 const coff_symbol *Symb = toSymb(SymbRef);
331 const coff_section *SymbSec = 0;
332 if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC;
337 return object_error::success;
340 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const {
341 const coff_section *Sec = toSec(Ref);
343 if (Sec->NumberOfRelocations == 0)
346 Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations);
348 return relocation_iterator(RelocationRef(Ret, this));
351 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const {
352 const coff_section *Sec = toSec(Ref);
354 if (Sec->NumberOfRelocations == 0)
357 Ret.p = reinterpret_cast<uintptr_t>(
358 reinterpret_cast<const coff_relocation*>(
359 base() + Sec->PointerToRelocations)
360 + Sec->NumberOfRelocations);
362 return relocation_iterator(RelocationRef(Ret, this));
365 // Initialize the pointer to the symbol table.
366 error_code COFFObjectFile::initSymbolTablePtr() {
367 if (error_code EC = getObject(
368 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
369 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
372 // Find string table. The first four byte of the string table contains the
373 // total size of the string table, including the size field itself. If the
374 // string table is empty, the value of the first four byte would be 4.
375 const uint8_t *StringTableAddr =
376 base() + COFFHeader->PointerToSymbolTable +
377 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
378 const ulittle32_t *StringTableSizePtr;
379 if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
381 StringTableSize = *StringTableSizePtr;
383 getObject(StringTable, Data, StringTableAddr, StringTableSize))
386 // Check that the string table is null terminated if has any in it.
387 if (StringTableSize < 4 ||
388 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
389 return object_error::parse_failed;
390 return object_error::success;
393 // Returns the file offset for the given RVA.
394 error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
396 for (section_iterator I = begin_sections(), E = end_sections(); I != E;
400 const coff_section *Section = getCOFFSection(I);
401 uint32_t SectionStart = Section->VirtualAddress;
402 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
403 if (SectionStart <= Rva && Rva < SectionEnd) {
404 uint32_t Offset = Rva - SectionStart;
405 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
406 return object_error::success;
409 return object_error::parse_failed;
412 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
414 error_code COFFObjectFile::
415 getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
416 uintptr_t IntPtr = 0;
417 if (error_code EC = getRvaPtr(Rva, IntPtr))
419 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
420 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
421 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
422 return object_error::success;
425 // Find the import table.
426 error_code COFFObjectFile::initImportTablePtr() {
427 // First, we get the RVA of the import table. If the file lacks a pointer to
428 // the import table, do nothing.
429 const data_directory *DataEntry;
430 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
431 return object_error::success;
433 // Do nothing if the pointer to import table is NULL.
434 if (DataEntry->RelativeVirtualAddress == 0)
435 return object_error::success;
437 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
438 NumberOfImportDirectory = DataEntry->Size /
439 sizeof(import_directory_table_entry);
441 // Find the section that contains the RVA. This is needed because the RVA is
442 // the import table's memory address which is different from its file offset.
443 uintptr_t IntPtr = 0;
444 if (error_code EC = getRvaPtr(ImportTableRva, IntPtr))
446 ImportDirectory = reinterpret_cast<
447 const import_directory_table_entry *>(IntPtr);
448 return object_error::success;
451 // Find the export table.
452 error_code COFFObjectFile::initExportTablePtr() {
453 // First, we get the RVA of the export table. If the file lacks a pointer to
454 // the export table, do nothing.
455 const data_directory *DataEntry;
456 if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
457 return object_error::success;
459 // Do nothing if the pointer to export table is NULL.
460 if (DataEntry->RelativeVirtualAddress == 0)
461 return object_error::success;
463 uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
464 uintptr_t IntPtr = 0;
465 if (error_code EC = getRvaPtr(ExportTableRva, IntPtr))
467 ExportDirectory = reinterpret_cast<const export_directory_table_entry *>(IntPtr);
468 return object_error::success;
471 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC)
472 : ObjectFile(Binary::ID_COFF, Object), COFFHeader(0), PE32Header(0),
473 DataDirectory(0), SectionTable(0), SymbolTable(0), StringTable(0),
474 StringTableSize(0), ImportDirectory(0), NumberOfImportDirectory(0),
476 // Check that we at least have enough room for a header.
477 if (!checkSize(Data, EC, sizeof(coff_file_header))) return;
479 // The current location in the file where we are looking at.
482 // PE header is optional and is present only in executables. If it exists,
483 // it is placed right after COFF header.
484 bool HasPEHeader = false;
486 // Check if this is a PE/COFF file.
487 if (base()[0] == 0x4d && base()[1] == 0x5a) {
488 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
489 // PE signature to find 'normal' COFF header.
490 if (!checkSize(Data, EC, 0x3c + 8)) return;
491 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
492 // Check the PE magic bytes. ("PE\0\0")
493 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
494 EC = object_error::parse_failed;
497 CurPtr += 4; // Skip the PE magic bytes.
501 if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
503 CurPtr += sizeof(coff_file_header);
506 if ((EC = getObject(PE32Header, Data, base() + CurPtr)))
508 if (PE32Header->Magic != 0x10b) {
509 // We only support PE32. If this is PE32 (not PE32+), the magic byte
510 // should be 0x10b. If this is not PE32, continue as if there's no PE
511 // header in this file.
513 } else if (PE32Header->NumberOfRvaAndSize > 0) {
514 const uint8_t *Addr = base() + CurPtr + sizeof(pe32_header);
515 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
516 if ((EC = getObject(DataDirectory, Data, Addr, size)))
519 CurPtr += COFFHeader->SizeOfOptionalHeader;
522 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 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
558 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
561 symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
563 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
566 library_iterator COFFObjectFile::begin_libraries_needed() const {
568 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
571 library_iterator COFFObjectFile::end_libraries_needed() const {
573 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
576 StringRef COFFObjectFile::getLoadName() const {
577 // COFF does not have this field.
581 import_directory_iterator COFFObjectFile::import_directory_begin() const {
582 return import_directory_iterator(
583 ImportDirectoryEntryRef(ImportDirectory, 0, this));
586 import_directory_iterator COFFObjectFile::import_directory_end() const {
587 return import_directory_iterator(
588 ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this));
591 export_directory_iterator COFFObjectFile::export_directory_begin() const {
592 return export_directory_iterator(
593 ExportDirectoryEntryRef(ExportDirectory, 0, this));
596 export_directory_iterator COFFObjectFile::export_directory_end() const {
597 if (ExportDirectory == 0)
598 return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this));
599 ExportDirectoryEntryRef Ref(ExportDirectory,
600 ExportDirectory->AddressTableEntries, this);
601 return export_directory_iterator(Ref);
604 section_iterator COFFObjectFile::begin_sections() const {
606 Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
607 return section_iterator(SectionRef(Ret, this));
610 section_iterator COFFObjectFile::end_sections() const {
612 int NumSections = COFFHeader->isImportLibrary()
613 ? 0 : COFFHeader->NumberOfSections;
614 Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
615 return section_iterator(SectionRef(Ret, this));
618 uint8_t COFFObjectFile::getBytesInAddress() const {
619 return getArch() == Triple::x86_64 ? 8 : 4;
622 StringRef COFFObjectFile::getFileFormatName() const {
623 switch(COFFHeader->Machine) {
624 case COFF::IMAGE_FILE_MACHINE_I386:
626 case COFF::IMAGE_FILE_MACHINE_AMD64:
627 return "COFF-x86-64";
629 return "COFF-<unknown arch>";
633 unsigned COFFObjectFile::getArch() const {
634 switch(COFFHeader->Machine) {
635 case COFF::IMAGE_FILE_MACHINE_I386:
637 case COFF::IMAGE_FILE_MACHINE_AMD64:
638 return Triple::x86_64;
640 return Triple::UnknownArch;
644 // This method is kept here because lld uses this. As soon as we make
645 // lld to use getCOFFHeader, this method will be removed.
646 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
647 return getCOFFHeader(Res);
650 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
652 return object_error::success;
655 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
657 return object_error::success;
660 error_code COFFObjectFile::getDataDirectory(uint32_t Index,
661 const data_directory *&Res) const {
662 // Error if if there's no data directory or the index is out of range.
663 if (!DataDirectory || Index > PE32Header->NumberOfRvaAndSize)
664 return object_error::parse_failed;
665 Res = &DataDirectory[Index];
666 return object_error::success;
669 error_code COFFObjectFile::getSection(int32_t Index,
670 const coff_section *&Result) const {
671 // Check for special index values.
672 if (Index == COFF::IMAGE_SYM_UNDEFINED ||
673 Index == COFF::IMAGE_SYM_ABSOLUTE ||
674 Index == COFF::IMAGE_SYM_DEBUG)
676 else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
677 // We already verified the section table data, so no need to check again.
678 Result = SectionTable + (Index - 1);
680 return object_error::parse_failed;
681 return object_error::success;
684 error_code COFFObjectFile::getString(uint32_t Offset,
685 StringRef &Result) const {
686 if (StringTableSize <= 4)
687 // Tried to get a string from an empty string table.
688 return object_error::parse_failed;
689 if (Offset >= StringTableSize)
690 return object_error::unexpected_eof;
691 Result = StringRef(StringTable + Offset);
692 return object_error::success;
695 error_code COFFObjectFile::getSymbol(uint32_t Index,
696 const coff_symbol *&Result) const {
697 if (Index < COFFHeader->NumberOfSymbols)
698 Result = SymbolTable + Index;
700 return object_error::parse_failed;
701 return object_error::success;
704 error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol,
705 StringRef &Res) const {
706 // Check for string table entry. First 4 bytes are 0.
707 if (Symbol->Name.Offset.Zeroes == 0) {
708 uint32_t Offset = Symbol->Name.Offset.Offset;
709 if (error_code EC = getString(Offset, Res))
711 return object_error::success;
714 if (Symbol->Name.ShortName[7] == 0)
715 // Null terminated, let ::strlen figure out the length.
716 Res = StringRef(Symbol->Name.ShortName);
718 // Not null terminated, use all 8 bytes.
719 Res = StringRef(Symbol->Name.ShortName, 8);
720 return object_error::success;
723 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
724 const coff_symbol *Symbol) const {
725 const uint8_t *Aux = NULL;
727 if (Symbol->NumberOfAuxSymbols > 0) {
728 // AUX data comes immediately after the symbol in COFF
729 Aux = reinterpret_cast<const uint8_t *>(Symbol + 1);
731 // Verify that the Aux symbol points to a valid entry in the symbol table.
732 uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
733 if (Offset < COFFHeader->PointerToSymbolTable
734 || Offset >= COFFHeader->PointerToSymbolTable
735 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
736 report_fatal_error("Aux Symbol data was outside of symbol table.");
738 assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
739 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
742 return ArrayRef<uint8_t>(Aux, Symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
745 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
746 StringRef &Res) const {
748 if (Sec->Name[7] == 0)
749 // Null terminated, let ::strlen figure out the length.
752 // Not null terminated, use all 8 bytes.
753 Name = StringRef(Sec->Name, 8);
755 // Check for string table entry. First byte is '/'.
756 if (Name[0] == '/') {
758 if (Name.substr(1).getAsInteger(10, Offset))
759 return object_error::parse_failed;
760 if (error_code EC = getString(Offset, Name))
765 return object_error::success;
768 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
769 ArrayRef<uint8_t> &Res) const {
770 // The only thing that we need to verify is that the contents is contained
771 // within the file bounds. We don't need to make sure it doesn't cover other
772 // data, as there's nothing that says that is not allowed.
773 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
774 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
775 if (ConEnd > uintptr_t(Data->getBufferEnd()))
776 return object_error::parse_failed;
777 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
779 return object_error::success;
782 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
783 return reinterpret_cast<const coff_relocation*>(Rel.p);
786 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
787 RelocationRef &Res) const {
788 Rel.p = reinterpret_cast<uintptr_t>(
789 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
790 Res = RelocationRef(Rel, this);
791 return object_error::success;
794 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
795 uint64_t &Res) const {
796 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
799 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
800 uint64_t &Res) const {
801 Res = toRel(Rel)->VirtualAddress;
802 return object_error::success;
805 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
806 const coff_relocation* R = toRel(Rel);
808 Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
809 return symbol_iterator(SymbolRef(Ref, this));
812 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
813 uint64_t &Res) const {
814 const coff_relocation* R = toRel(Rel);
816 return object_error::success;
819 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
820 return toSec(It->getRawDataRefImpl());
823 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
824 return toSymb(It->getRawDataRefImpl());
827 const coff_relocation *COFFObjectFile::getCOFFRelocation(
828 relocation_iterator &It) const {
829 return toRel(It->getRawDataRefImpl());
832 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
833 case COFF::enum: Res = #enum; break;
835 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
836 SmallVectorImpl<char> &Result) const {
837 const coff_relocation *Reloc = toRel(Rel);
839 switch (COFFHeader->Machine) {
840 case COFF::IMAGE_FILE_MACHINE_AMD64:
841 switch (Reloc->Type) {
842 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
843 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
844 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
845 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
846 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
847 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
848 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
849 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
850 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
851 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
852 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
853 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
854 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
855 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
856 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
857 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
858 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
863 case COFF::IMAGE_FILE_MACHINE_I386:
864 switch (Reloc->Type) {
865 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
866 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
867 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
868 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
869 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
870 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
871 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
872 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
873 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
874 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
875 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
883 Result.append(Res.begin(), Res.end());
884 return object_error::success;
887 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
889 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
890 SmallVectorImpl<char> &Result) const {
891 const coff_relocation *Reloc = toRel(Rel);
892 const coff_symbol *Symb = 0;
893 if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC;
895 Sym.p = reinterpret_cast<uintptr_t>(Symb);
897 if (error_code EC = getSymbolName(Sym, SymName)) return EC;
898 Result.append(SymName.begin(), SymName.end());
899 return object_error::success;
902 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
903 LibraryRef &Result) const {
904 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
907 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
908 StringRef &Result) const {
909 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
912 bool ImportDirectoryEntryRef::
913 operator==(const ImportDirectoryEntryRef &Other) const {
914 return ImportTable == Other.ImportTable && Index == Other.Index;
918 ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
919 Result = ImportDirectoryEntryRef(ImportTable, Index + 1, OwningObject);
920 return object_error::success;
923 error_code ImportDirectoryEntryRef::
924 getImportTableEntry(const import_directory_table_entry *&Result) const {
925 Result = ImportTable;
926 return object_error::success;
929 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
930 uintptr_t IntPtr = 0;
931 if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr))
933 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
934 return object_error::success;
937 error_code ImportDirectoryEntryRef::getImportLookupEntry(
938 const import_lookup_table_entry32 *&Result) const {
939 uintptr_t IntPtr = 0;
941 OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr))
943 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
944 return object_error::success;
947 bool ExportDirectoryEntryRef::
948 operator==(const ExportDirectoryEntryRef &Other) const {
949 return ExportTable == Other.ExportTable && Index == Other.Index;
953 ExportDirectoryEntryRef::getNext(ExportDirectoryEntryRef &Result) const {
954 Result = ExportDirectoryEntryRef(ExportTable, Index + 1, OwningObject);
955 return object_error::success;
958 // Returns the export ordinal of the current export symbol.
959 error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
960 Result = ExportTable->OrdinalBase + Index;
961 return object_error::success;
964 // Returns the address of the current export symbol.
965 error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
966 uintptr_t IntPtr = 0;
967 if (error_code EC = OwningObject->getRvaPtr(
968 ExportTable->ExportAddressTableRVA, IntPtr))
970 const export_address_table_entry *entry = reinterpret_cast<const export_address_table_entry *>(IntPtr);
971 Result = entry[Index].ExportRVA;
972 return object_error::success;
975 // Returns the name of the current export symbol. If the symbol is exported only
976 // by ordinal, the empty string is set as a result.
977 error_code ExportDirectoryEntryRef::getName(StringRef &Result) const {
978 uintptr_t IntPtr = 0;
979 if (error_code EC = OwningObject->getRvaPtr(
980 ExportTable->OrdinalTableRVA, IntPtr))
982 const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
984 uint32_t NumEntries = ExportTable->NumberOfNamePointers;
986 for (const ulittle16_t *I = Start, *E = Start + NumEntries;
987 I < E; ++I, ++Offset) {
990 if (error_code EC = OwningObject->getRvaPtr(
991 ExportTable->NamePointerRVA, IntPtr))
993 const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
994 if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
996 Result = StringRef(reinterpret_cast<const char *>(IntPtr));
997 return object_error::success;
1000 return object_error::success;
1004 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
1006 return new COFFObjectFile(Object, EC);
1008 } // end namespace llvm