1 //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the COFFObjectFile class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
24 using namespace object;
27 using support::ulittle8_t;
28 using support::ulittle16_t;
29 using support::ulittle32_t;
30 using support::little16_t;
34 // Returns false if size is greater than the buffer size. And sets ec.
35 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
36 if (m->getBufferSize() < size) {
37 ec = object_error::unexpected_eof;
43 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
44 // Returns unexpected_eof if error.
46 error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
47 const size_t Size = sizeof(T)) {
48 uintptr_t Addr = uintptr_t(Ptr);
49 if (Addr + Size < Addr ||
51 Addr + Size > uintptr_t(M->getBufferEnd())) {
52 return object_error::unexpected_eof;
54 Obj = reinterpret_cast<const T *>(Addr);
55 return object_error::success;
59 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
60 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
63 // Verify that the symbol points to a valid entry in the symbol table.
64 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
65 if (offset < COFFHeader->PointerToSymbolTable
66 || offset >= COFFHeader->PointerToSymbolTable
67 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
68 report_fatal_error("Symbol was outside of symbol table.");
70 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
71 == 0 && "Symbol did not point to the beginning of a symbol");
77 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
78 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
81 // Verify that the section points to a valid entry in the section table.
82 if (addr < SectionTable
83 || addr >= (SectionTable + COFFHeader->NumberOfSections))
84 report_fatal_error("Section was outside of section table.");
86 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
87 assert(offset % sizeof(coff_section) == 0 &&
88 "Section did not point to the beginning of a section");
94 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
95 SymbolRef &Result) const {
96 const coff_symbol *symb = toSymb(Symb);
97 symb += 1 + symb->NumberOfAuxSymbols;
98 Symb.p = reinterpret_cast<uintptr_t>(symb);
99 Result = SymbolRef(Symb, this);
100 return object_error::success;
103 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
104 StringRef &Result) const {
105 const coff_symbol *symb = toSymb(Symb);
106 return getSymbolName(symb, Result);
109 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
110 uint64_t &Result) const {
111 const coff_symbol *symb = toSymb(Symb);
112 const coff_section *Section = NULL;
113 if (error_code ec = getSection(symb->SectionNumber, Section))
116 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
118 if (Type == 'U' || Type == 'w')
119 Result = UnknownAddressOrSize;
121 Result = Section->PointerToRawData + symb->Value;
123 Result = symb->Value;
124 return object_error::success;
127 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
128 uint64_t &Result) const {
129 const coff_symbol *symb = toSymb(Symb);
130 const coff_section *Section = NULL;
131 if (error_code ec = getSection(symb->SectionNumber, Section))
134 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
136 if (Type == 'U' || Type == 'w')
137 Result = UnknownAddressOrSize;
139 Result = Section->VirtualAddress + symb->Value;
141 Result = symb->Value;
142 return object_error::success;
145 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
146 SymbolRef::Type &Result) const {
147 const coff_symbol *symb = toSymb(Symb);
148 Result = SymbolRef::ST_Other;
149 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
150 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
151 Result = SymbolRef::ST_Unknown;
153 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
154 Result = SymbolRef::ST_Function;
157 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
159 if (Type == 'r' || Type == 'R') {
160 Result = SymbolRef::ST_Data;
164 return object_error::success;
167 error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
168 uint32_t &Result) const {
169 const coff_symbol *symb = toSymb(Symb);
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 Symb,
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(Symb);
197 const coff_section *Section = NULL;
198 if (error_code ec = getSection(symb->SectionNumber, Section))
201 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
203 if (Type == 'U' || Type == 'w')
204 Result = UnknownAddressOrSize;
206 Result = Section->SizeOfRawData - symb->Value;
209 return object_error::success;
212 error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
213 char &Result) const {
214 const coff_symbol *symb = toSymb(Symb);
216 if (error_code ec = getSymbolName(Symb, name))
218 char ret = StringSwitch<char>(name)
219 .StartsWith(".debug", 'N')
220 .StartsWith(".sxdata", 'N')
225 return object_error::success;
228 uint32_t Characteristics = 0;
229 if (symb->SectionNumber > 0) {
230 const coff_section *Section = NULL;
231 if (error_code ec = getSection(symb->SectionNumber, Section))
233 Characteristics = Section->Characteristics;
236 switch (symb->SectionNumber) {
237 case COFF::IMAGE_SYM_UNDEFINED:
238 // Check storage classes.
239 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
241 return object_error::success; // Don't do ::toupper.
242 } else if (symb->Value != 0) // Check for common symbols.
247 case COFF::IMAGE_SYM_ABSOLUTE:
250 case COFF::IMAGE_SYM_DEBUG:
254 // Check section type.
255 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
257 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
258 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
260 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
262 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
264 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
267 // Check for section symbol.
268 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
273 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
274 ret = ::toupper(static_cast<unsigned char>(ret));
277 return object_error::success;
280 error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
281 section_iterator &Result) const {
282 const coff_symbol *symb = toSymb(Symb);
283 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
284 Result = end_sections();
286 const coff_section *sec = 0;
287 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
289 Sec.p = reinterpret_cast<uintptr_t>(sec);
290 Result = section_iterator(SectionRef(Sec, this));
292 return object_error::success;
295 error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
296 uint64_t &Val) const {
297 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
300 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
301 SectionRef &Result) const {
302 const coff_section *sec = toSec(Sec);
304 Sec.p = reinterpret_cast<uintptr_t>(sec);
305 Result = SectionRef(Sec, this);
306 return object_error::success;
309 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
310 StringRef &Result) const {
311 const coff_section *sec = toSec(Sec);
312 return getSectionName(sec, Result);
315 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
316 uint64_t &Result) const {
317 const coff_section *sec = toSec(Sec);
318 Result = sec->VirtualAddress;
319 return object_error::success;
322 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
323 uint64_t &Result) const {
324 const coff_section *sec = toSec(Sec);
325 Result = sec->SizeOfRawData;
326 return object_error::success;
329 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
330 StringRef &Result) const {
331 const coff_section *sec = toSec(Sec);
332 ArrayRef<uint8_t> Res;
333 error_code EC = getSectionContents(sec, Res);
334 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
338 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
339 uint64_t &Res) const {
340 const coff_section *sec = toSec(Sec);
342 return object_error::parse_failed;
343 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
344 return object_error::success;
347 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
348 bool &Result) const {
349 const coff_section *sec = toSec(Sec);
350 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
351 return object_error::success;
354 error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
355 bool &Result) const {
356 const coff_section *sec = toSec(Sec);
357 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
358 return object_error::success;
361 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
362 bool &Result) const {
363 const coff_section *sec = toSec(Sec);
364 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
365 return object_error::success;
368 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
369 bool &Result) const {
370 // FIXME: Unimplemented
372 return object_error::success;
375 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
376 bool &Result) const {
377 const coff_section *sec = toSec(Sec);
378 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
379 return object_error::success;
382 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
383 bool &Result) const {
384 // FIXME: Unimplemented.
386 return object_error::success;
389 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
390 bool &Result) const {
391 // FIXME: Unimplemented.
393 return object_error::success;
396 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
398 bool &Result) const {
399 const coff_section *sec = toSec(Sec);
400 const coff_symbol *symb = toSymb(Symb);
401 const coff_section *symb_sec = 0;
402 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
407 return object_error::success;
410 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
411 const coff_section *sec = toSec(Sec);
413 if (sec->NumberOfRelocations == 0)
416 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
418 return relocation_iterator(RelocationRef(ret, this));
421 relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Sec) const {
422 const coff_section *sec = toSec(Sec);
424 if (sec->NumberOfRelocations == 0)
427 ret.p = reinterpret_cast<uintptr_t>(
428 reinterpret_cast<const coff_relocation*>(
429 base() + sec->PointerToRelocations)
430 + sec->NumberOfRelocations);
432 return relocation_iterator(RelocationRef(ret, this));
435 // Initialize the pointer to the symbol table.
436 error_code COFFObjectFile::initSymbolTablePtr() {
437 if (error_code ec = getObject(
438 SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable,
439 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
442 // Find string table. The first four byte of the string table contains the
443 // total size of the string table, including the size field itself. If the
444 // string table is empty, the value of the first four byte would be 4.
445 const uint8_t *StringTableAddr =
446 base() + COFFHeader->PointerToSymbolTable +
447 COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
448 const ulittle32_t *StringTableSizePtr;
449 if (error_code ec = getObject(StringTableSizePtr, Data, StringTableAddr))
451 StringTableSize = *StringTableSizePtr;
453 getObject(StringTable, Data, StringTableAddr, StringTableSize))
456 // Check that the string table is null terminated if has any in it.
457 if (StringTableSize < 4 ||
458 (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0))
459 return object_error::parse_failed;
460 return object_error::success;
463 // Returns the file offset for the given RVA.
464 error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const {
466 for (section_iterator i = begin_sections(), e = end_sections(); i != e;
470 const coff_section *Section = getCOFFSection(i);
471 uint32_t SectionStart = Section->VirtualAddress;
472 uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
473 if (SectionStart <= Rva && Rva < SectionEnd) {
474 uint32_t Offset = Rva - SectionStart;
475 Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
476 return object_error::success;
479 return object_error::parse_failed;
482 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
484 error_code COFFObjectFile::
485 getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const {
486 uintptr_t IntPtr = 0;
487 if (error_code ec = getRvaPtr(Rva, IntPtr))
489 const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
490 Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
491 Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
492 return object_error::success;
495 // Find the import table.
496 error_code COFFObjectFile::initImportTablePtr() {
497 // First, we get the RVA of the import table. If the file lacks a pointer to
498 // the import table, do nothing.
499 const data_directory *DataEntry;
500 if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
501 return object_error::success;
503 // Do nothing if the pointer to import table is NULL.
504 if (DataEntry->RelativeVirtualAddress == 0)
505 return object_error::success;
507 uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
508 NumberOfImportDirectory = DataEntry->Size /
509 sizeof(import_directory_table_entry);
511 // Find the section that contains the RVA. This is needed because the RVA is
512 // the import table's memory address which is different from its file offset.
513 uintptr_t IntPtr = 0;
514 if (error_code ec = getRvaPtr(ImportTableRva, IntPtr))
516 ImportDirectory = reinterpret_cast<
517 const import_directory_table_entry *>(IntPtr);
519 // It's an error if there's no section containing the Import Table RVA.
520 return object_error::parse_failed;
523 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
524 : ObjectFile(Binary::ID_COFF, Object)
533 , NumberOfImportDirectory(0) {
534 // Check that we at least have enough room for a header.
535 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
537 // The current location in the file where we are looking at.
540 // PE header is optional and is present only in executables. If it exists,
541 // it is placed right after COFF header.
542 bool hasPEHeader = false;
544 // Check if this is a PE/COFF file.
545 if (base()[0] == 0x4d && base()[1] == 0x5a) {
546 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
547 // PE signature to find 'normal' COFF header.
548 if (!checkSize(Data, ec, 0x3c + 8)) return;
549 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
550 // Check the PE magic bytes. ("PE\0\0")
551 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
552 ec = object_error::parse_failed;
555 CurPtr += 4; // Skip the PE magic bytes.
559 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
561 CurPtr += sizeof(coff_file_header);
564 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
566 if (PE32Header->Magic != 0x10b) {
567 // We only support PE32. If this is PE32 (not PE32+), the magic byte
568 // should be 0x10b. If this is not PE32, continue as if there's no PE
569 // header in this file.
571 } else if (PE32Header->NumberOfRvaAndSize > 0) {
572 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
573 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
574 if ((ec = getObject(DataDirectory, Data, addr, size)))
577 CurPtr += COFFHeader->SizeOfOptionalHeader;
580 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
581 COFFHeader->NumberOfSections * sizeof(coff_section))))
584 // Initialize the pointer to the symbol table.
585 if (COFFHeader->PointerToSymbolTable != 0)
586 if ((ec = initSymbolTablePtr()))
589 // Initialize the pointer to the beginning of the import table.
590 if ((ec = initImportTablePtr()))
593 ec = object_error::success;
596 symbol_iterator COFFObjectFile::begin_symbols() const {
598 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
599 return symbol_iterator(SymbolRef(ret, this));
602 symbol_iterator COFFObjectFile::end_symbols() const {
603 // The symbol table ends where the string table begins.
605 ret.p = reinterpret_cast<uintptr_t>(StringTable);
606 return symbol_iterator(SymbolRef(ret, this));
609 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
611 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
614 symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
616 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
619 library_iterator COFFObjectFile::begin_libraries_needed() const {
621 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
624 library_iterator COFFObjectFile::end_libraries_needed() const {
626 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
629 StringRef COFFObjectFile::getLoadName() const {
630 // COFF does not have this field.
634 import_directory_iterator COFFObjectFile::import_directory_begin() const {
636 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
637 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
640 import_directory_iterator COFFObjectFile::import_directory_end() const {
642 if (ImportDirectory) {
643 Imp.p = reinterpret_cast<uintptr_t>(
644 ImportDirectory + (NumberOfImportDirectory - 1));
648 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
651 section_iterator COFFObjectFile::begin_sections() const {
653 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
654 return section_iterator(SectionRef(ret, this));
657 section_iterator COFFObjectFile::end_sections() const {
659 ret.p = reinterpret_cast<uintptr_t>(SectionTable + COFFHeader->NumberOfSections);
660 return section_iterator(SectionRef(ret, this));
663 uint8_t COFFObjectFile::getBytesInAddress() const {
664 return getArch() == Triple::x86_64 ? 8 : 4;
667 StringRef COFFObjectFile::getFileFormatName() const {
668 switch(COFFHeader->Machine) {
669 case COFF::IMAGE_FILE_MACHINE_I386:
671 case COFF::IMAGE_FILE_MACHINE_AMD64:
672 return "COFF-x86-64";
674 return "COFF-<unknown arch>";
678 unsigned COFFObjectFile::getArch() const {
679 switch(COFFHeader->Machine) {
680 case COFF::IMAGE_FILE_MACHINE_I386:
682 case COFF::IMAGE_FILE_MACHINE_AMD64:
683 return Triple::x86_64;
685 return Triple::UnknownArch;
689 // This method is kept here because lld uses this. As soon as we make
690 // lld to use getCOFFHeader, this method will be removed.
691 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
692 return getCOFFHeader(Res);
695 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
697 return object_error::success;
700 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
702 return object_error::success;
705 error_code COFFObjectFile::getDataDirectory(uint32_t index,
706 const data_directory *&Res) const {
707 // Error if if there's no data directory or the index is out of range.
708 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
709 return object_error::parse_failed;
710 Res = &DataDirectory[index];
711 return object_error::success;
714 error_code COFFObjectFile::getSection(int32_t index,
715 const coff_section *&Result) const {
716 // Check for special index values.
717 if (index == COFF::IMAGE_SYM_UNDEFINED ||
718 index == COFF::IMAGE_SYM_ABSOLUTE ||
719 index == COFF::IMAGE_SYM_DEBUG)
721 else if (index > 0 && index <= COFFHeader->NumberOfSections)
722 // We already verified the section table data, so no need to check again.
723 Result = SectionTable + (index - 1);
725 return object_error::parse_failed;
726 return object_error::success;
729 error_code COFFObjectFile::getString(uint32_t offset,
730 StringRef &Result) const {
731 if (StringTableSize <= 4)
732 // Tried to get a string from an empty string table.
733 return object_error::parse_failed;
734 if (offset >= StringTableSize)
735 return object_error::unexpected_eof;
736 Result = StringRef(StringTable + offset);
737 return object_error::success;
740 error_code COFFObjectFile::getSymbol(uint32_t index,
741 const coff_symbol *&Result) const {
742 if (index < COFFHeader->NumberOfSymbols)
743 Result = SymbolTable + index;
745 return object_error::parse_failed;
746 return object_error::success;
749 error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
750 StringRef &Res) const {
751 // Check for string table entry. First 4 bytes are 0.
752 if (symbol->Name.Offset.Zeroes == 0) {
753 uint32_t Offset = symbol->Name.Offset.Offset;
754 if (error_code ec = getString(Offset, Res))
756 return object_error::success;
759 if (symbol->Name.ShortName[7] == 0)
760 // Null terminated, let ::strlen figure out the length.
761 Res = StringRef(symbol->Name.ShortName);
763 // Not null terminated, use all 8 bytes.
764 Res = StringRef(symbol->Name.ShortName, 8);
765 return object_error::success;
768 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
769 const coff_symbol *symbol) const {
770 const uint8_t *aux = NULL;
772 if ( symbol->NumberOfAuxSymbols > 0 ) {
773 // AUX data comes immediately after the symbol in COFF
774 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
776 // Verify that the aux symbol points to a valid entry in the symbol table.
777 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
778 if (offset < COFFHeader->PointerToSymbolTable
779 || offset >= COFFHeader->PointerToSymbolTable
780 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
781 report_fatal_error("Aux Symbol data was outside of symbol table.");
783 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
784 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
787 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
790 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
791 StringRef &Res) const {
793 if (Sec->Name[7] == 0)
794 // Null terminated, let ::strlen figure out the length.
797 // Not null terminated, use all 8 bytes.
798 Name = StringRef(Sec->Name, 8);
800 // Check for string table entry. First byte is '/'.
801 if (Name[0] == '/') {
803 if (Name.substr(1).getAsInteger(10, Offset))
804 return object_error::parse_failed;
805 if (error_code ec = getString(Offset, Name))
810 return object_error::success;
813 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
814 ArrayRef<uint8_t> &Res) const {
815 // The only thing that we need to verify is that the contents is contained
816 // within the file bounds. We don't need to make sure it doesn't cover other
817 // data, as there's nothing that says that is not allowed.
818 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
819 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
820 if (ConEnd > uintptr_t(Data->getBufferEnd()))
821 return object_error::parse_failed;
822 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
824 return object_error::success;
827 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
828 return reinterpret_cast<const coff_relocation*>(Rel.p);
830 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
831 RelocationRef &Res) const {
832 Rel.p = reinterpret_cast<uintptr_t>(
833 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
834 Res = RelocationRef(Rel, this);
835 return object_error::success;
837 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
838 uint64_t &Res) const {
839 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
841 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
842 uint64_t &Res) const {
843 Res = toRel(Rel)->VirtualAddress;
844 return object_error::success;
846 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
847 const coff_relocation* R = toRel(Rel);
849 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
850 return symbol_iterator(SymbolRef(Symb, this));
852 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
853 uint64_t &Res) const {
854 const coff_relocation* R = toRel(Rel);
856 return object_error::success;
859 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
860 return toSec(It->getRawDataRefImpl());
863 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
864 return toSymb(It->getRawDataRefImpl());
867 const coff_relocation *COFFObjectFile::getCOFFRelocation(
868 relocation_iterator &It) const {
869 return toRel(It->getRawDataRefImpl());
872 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
873 case COFF::enum: res = #enum; break;
875 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
876 SmallVectorImpl<char> &Result) const {
877 const coff_relocation *reloc = toRel(Rel);
879 switch (COFFHeader->Machine) {
880 case COFF::IMAGE_FILE_MACHINE_AMD64:
881 switch (reloc->Type) {
882 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
883 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
884 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
885 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
886 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
887 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
888 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
889 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
890 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
891 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
892 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
893 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
894 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
895 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
896 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
897 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
898 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
903 case COFF::IMAGE_FILE_MACHINE_I386:
904 switch (reloc->Type) {
905 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
906 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
907 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
908 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
909 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
910 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
911 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
912 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
913 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
914 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
915 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
923 Result.append(res.begin(), res.end());
924 return object_error::success;
927 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
929 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
930 SmallVectorImpl<char> &Result) const {
931 const coff_relocation *reloc = toRel(Rel);
932 const coff_symbol *symb = 0;
933 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
935 sym.p = reinterpret_cast<uintptr_t>(symb);
937 if (error_code ec = getSymbolName(sym, symname)) return ec;
938 Result.append(symname.begin(), symname.end());
939 return object_error::success;
942 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
943 LibraryRef &Result) const {
944 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
947 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
948 StringRef &Result) const {
949 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
952 bool ImportDirectoryEntryRef::
953 operator==(const ImportDirectoryEntryRef &Other) const {
954 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
957 static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
958 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
962 ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
963 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
966 Next.p = reinterpret_cast<uintptr_t>(Dir);
967 Result = ImportDirectoryEntryRef(Next, OwningObject);
968 return object_error::success;
971 error_code ImportDirectoryEntryRef::
972 getImportTableEntry(const import_directory_table_entry *&Result) const {
973 Result = toImportEntry(ImportDirectoryPimpl);
974 return object_error::success;
977 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
978 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
979 uintptr_t IntPtr = 0;
980 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
982 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
983 Result = StringRef(Ptr);
984 return object_error::success;
987 error_code ImportDirectoryEntryRef::getImportLookupEntry(
988 const import_lookup_table_entry32 *&Result) const {
989 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
990 uintptr_t IntPtr = 0;
991 if (error_code ec = OwningObject->getRvaPtr(
992 Dir->ImportLookupTableRVA, IntPtr))
994 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
995 return object_error::success;
1000 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
1002 return new COFFObjectFile(Object, ec);
1005 } // end namespace llvm