1 //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the COFFObjectFile class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
24 using namespace object;
27 using support::ulittle8_t;
28 using support::ulittle16_t;
29 using support::ulittle32_t;
30 using support::little16_t;
34 // Returns false if size is greater than the buffer size. And sets ec.
35 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
36 if (m->getBufferSize() < size) {
37 ec = object_error::unexpected_eof;
43 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
44 // Returns unexpected_eof if error.
46 error_code getObject(const T *&Obj, const MemoryBuffer *M, const uint8_t *Ptr,
47 const size_t Size = sizeof(T)) {
48 uintptr_t Addr = uintptr_t(Ptr);
49 if (Addr + Size < Addr ||
51 Addr + Size > uintptr_t(M->getBufferEnd())) {
52 return object_error::unexpected_eof;
54 Obj = reinterpret_cast<const T *>(Addr);
55 return object_error::success;
59 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
60 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
63 // Verify that the symbol points to a valid entry in the symbol table.
64 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
65 if (offset < COFFHeader->PointerToSymbolTable
66 || offset >= COFFHeader->PointerToSymbolTable
67 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
68 report_fatal_error("Symbol was outside of symbol table.");
70 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
71 == 0 && "Symbol did not point to the beginning of a symbol");
77 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
78 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
81 // Verify that the section points to a valid entry in the section table.
82 if (addr < SectionTable
83 || addr >= (SectionTable + COFFHeader->NumberOfSections))
84 report_fatal_error("Section was outside of section table.");
86 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
87 assert(offset % sizeof(coff_section) == 0 &&
88 "Section did not point to the beginning of a section");
94 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
95 SymbolRef &Result) const {
96 const coff_symbol *symb = toSymb(Symb);
97 symb += 1 + symb->NumberOfAuxSymbols;
98 Symb.p = reinterpret_cast<uintptr_t>(symb);
99 Result = SymbolRef(Symb, this);
100 return object_error::success;
103 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
104 StringRef &Result) const {
105 const coff_symbol *symb = toSymb(Symb);
106 return getSymbolName(symb, Result);
109 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
110 uint64_t &Result) const {
111 const coff_symbol *symb = toSymb(Symb);
112 const coff_section *Section = NULL;
113 if (error_code ec = getSection(symb->SectionNumber, Section))
116 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
117 Result = UnknownAddressOrSize;
119 Result = Section->PointerToRawData + symb->Value;
121 Result = symb->Value;
122 return object_error::success;
125 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
126 uint64_t &Result) const {
127 const coff_symbol *symb = toSymb(Symb);
128 const coff_section *Section = NULL;
129 if (error_code ec = getSection(symb->SectionNumber, Section))
132 if (symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
133 Result = UnknownAddressOrSize;
135 Result = Section->VirtualAddress + symb->Value;
137 Result = symb->Value;
138 return object_error::success;
141 error_code COFFObjectFile::getSymbolType(DataRefImpl Symb,
142 SymbolRef::Type &Result) const {
143 const coff_symbol *symb = toSymb(Symb);
144 Result = SymbolRef::ST_Other;
145 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
146 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) {
147 Result = SymbolRef::ST_Unknown;
149 if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
150 Result = SymbolRef::ST_Function;
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 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 (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 Symb,
211 section_iterator &Result) const {
212 const coff_symbol *symb = toSymb(Symb);
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 Sec.p = reinterpret_cast<uintptr_t>(sec);
220 Result = section_iterator(SectionRef(Sec, this));
222 return object_error::success;
225 error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
226 uint64_t &Val) const {
227 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
230 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
231 SectionRef &Result) const {
232 const coff_section *sec = toSec(Sec);
234 Sec.p = reinterpret_cast<uintptr_t>(sec);
235 Result = SectionRef(Sec, this);
236 return object_error::success;
239 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
240 StringRef &Result) const {
241 const coff_section *sec = toSec(Sec);
242 return getSectionName(sec, Result);
245 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
246 uint64_t &Result) const {
247 const coff_section *sec = toSec(Sec);
248 Result = sec->VirtualAddress;
249 return object_error::success;
252 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
253 uint64_t &Result) const {
254 const coff_section *sec = toSec(Sec);
255 Result = sec->SizeOfRawData;
256 return object_error::success;
259 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
260 StringRef &Result) const {
261 const coff_section *sec = toSec(Sec);
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 Sec,
269 uint64_t &Res) const {
270 const coff_section *sec = toSec(Sec);
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 Sec,
278 bool &Result) const {
279 const coff_section *sec = toSec(Sec);
280 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
281 return object_error::success;
284 error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
285 bool &Result) const {
286 const coff_section *sec = toSec(Sec);
287 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
288 return object_error::success;
291 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
292 bool &Result) const {
293 const coff_section *sec = toSec(Sec);
294 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
295 return object_error::success;
298 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
299 bool &Result) const {
300 // FIXME: Unimplemented
302 return object_error::success;
305 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
306 bool &Result) const {
307 const coff_section *sec = toSec(Sec);
308 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
309 return object_error::success;
312 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
313 bool &Result) const {
314 // FIXME: Unimplemented.
316 return object_error::success;
319 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
320 bool &Result) const {
321 // FIXME: Unimplemented.
323 return object_error::success;
326 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
328 bool &Result) const {
329 const coff_section *sec = toSec(Sec);
330 const coff_symbol *symb = toSymb(Symb);
331 const coff_section *symb_sec = 0;
332 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
337 return object_error::success;
340 relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Sec) const {
341 const coff_section *sec = toSec(Sec);
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 Sec) const {
352 const coff_section *sec = toSec(Sec);
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);
449 // It's an error if there's no section containing the Import Table RVA.
450 return object_error::parse_failed;
453 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
454 : ObjectFile(Binary::ID_COFF, Object)
463 , NumberOfImportDirectory(0) {
464 // Check that we at least have enough room for a header.
465 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
467 // The current location in the file where we are looking at.
470 // PE header is optional and is present only in executables. If it exists,
471 // it is placed right after COFF header.
472 bool hasPEHeader = false;
474 // Check if this is a PE/COFF file.
475 if (base()[0] == 0x4d && base()[1] == 0x5a) {
476 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
477 // PE signature to find 'normal' COFF header.
478 if (!checkSize(Data, ec, 0x3c + 8)) return;
479 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
480 // Check the PE magic bytes. ("PE\0\0")
481 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
482 ec = object_error::parse_failed;
485 CurPtr += 4; // Skip the PE magic bytes.
489 if ((ec = getObject(COFFHeader, Data, base() + CurPtr)))
491 CurPtr += sizeof(coff_file_header);
494 if ((ec = getObject(PE32Header, Data, base() + CurPtr)))
496 if (PE32Header->Magic != 0x10b) {
497 // We only support PE32. If this is PE32 (not PE32+), the magic byte
498 // should be 0x10b. If this is not PE32, continue as if there's no PE
499 // header in this file.
501 } else if (PE32Header->NumberOfRvaAndSize > 0) {
502 const uint8_t *addr = base() + CurPtr + sizeof(pe32_header);
503 uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
504 if ((ec = getObject(DataDirectory, Data, addr, size)))
507 CurPtr += COFFHeader->SizeOfOptionalHeader;
510 if ((ec = getObject(SectionTable, Data, base() + CurPtr,
511 COFFHeader->NumberOfSections * sizeof(coff_section))))
514 // Initialize the pointer to the symbol table.
515 if (COFFHeader->PointerToSymbolTable != 0)
516 if ((ec = initSymbolTablePtr()))
519 // Initialize the pointer to the beginning of the import table.
520 if ((ec = initImportTablePtr()))
523 ec = object_error::success;
526 symbol_iterator COFFObjectFile::begin_symbols() const {
528 ret.p = reinterpret_cast<uintptr_t>(SymbolTable);
529 return symbol_iterator(SymbolRef(ret, this));
532 symbol_iterator COFFObjectFile::end_symbols() const {
533 // The symbol table ends where the string table begins.
535 ret.p = reinterpret_cast<uintptr_t>(StringTable);
536 return symbol_iterator(SymbolRef(ret, this));
539 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
541 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
544 symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
546 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
549 library_iterator COFFObjectFile::begin_libraries_needed() const {
551 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
554 library_iterator COFFObjectFile::end_libraries_needed() const {
556 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
559 StringRef COFFObjectFile::getLoadName() const {
560 // COFF does not have this field.
564 import_directory_iterator COFFObjectFile::import_directory_begin() const {
566 Imp.p = reinterpret_cast<uintptr_t>(ImportDirectory);
567 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
570 import_directory_iterator COFFObjectFile::import_directory_end() const {
572 if (ImportDirectory) {
573 Imp.p = reinterpret_cast<uintptr_t>(
574 ImportDirectory + (NumberOfImportDirectory - 1));
578 return import_directory_iterator(ImportDirectoryEntryRef(Imp, this));
581 section_iterator COFFObjectFile::begin_sections() const {
583 ret.p = reinterpret_cast<uintptr_t>(SectionTable);
584 return section_iterator(SectionRef(ret, this));
587 section_iterator COFFObjectFile::end_sections() const {
589 ret.p = reinterpret_cast<uintptr_t>(SectionTable + COFFHeader->NumberOfSections);
590 return section_iterator(SectionRef(ret, this));
593 uint8_t COFFObjectFile::getBytesInAddress() const {
594 return getArch() == Triple::x86_64 ? 8 : 4;
597 StringRef COFFObjectFile::getFileFormatName() const {
598 switch(COFFHeader->Machine) {
599 case COFF::IMAGE_FILE_MACHINE_I386:
601 case COFF::IMAGE_FILE_MACHINE_AMD64:
602 return "COFF-x86-64";
604 return "COFF-<unknown arch>";
608 unsigned COFFObjectFile::getArch() const {
609 switch(COFFHeader->Machine) {
610 case COFF::IMAGE_FILE_MACHINE_I386:
612 case COFF::IMAGE_FILE_MACHINE_AMD64:
613 return Triple::x86_64;
615 return Triple::UnknownArch;
619 // This method is kept here because lld uses this. As soon as we make
620 // lld to use getCOFFHeader, this method will be removed.
621 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
622 return getCOFFHeader(Res);
625 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
627 return object_error::success;
630 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
632 return object_error::success;
635 error_code COFFObjectFile::getDataDirectory(uint32_t index,
636 const data_directory *&Res) const {
637 // Error if if there's no data directory or the index is out of range.
638 if (!DataDirectory || index > PE32Header->NumberOfRvaAndSize)
639 return object_error::parse_failed;
640 Res = &DataDirectory[index];
641 return object_error::success;
644 error_code COFFObjectFile::getSection(int32_t index,
645 const coff_section *&Result) const {
646 // Check for special index values.
647 if (index == COFF::IMAGE_SYM_UNDEFINED ||
648 index == COFF::IMAGE_SYM_ABSOLUTE ||
649 index == COFF::IMAGE_SYM_DEBUG)
651 else if (index > 0 && index <= COFFHeader->NumberOfSections)
652 // We already verified the section table data, so no need to check again.
653 Result = SectionTable + (index - 1);
655 return object_error::parse_failed;
656 return object_error::success;
659 error_code COFFObjectFile::getString(uint32_t offset,
660 StringRef &Result) const {
661 if (StringTableSize <= 4)
662 // Tried to get a string from an empty string table.
663 return object_error::parse_failed;
664 if (offset >= StringTableSize)
665 return object_error::unexpected_eof;
666 Result = StringRef(StringTable + offset);
667 return object_error::success;
670 error_code COFFObjectFile::getSymbol(uint32_t index,
671 const coff_symbol *&Result) const {
672 if (index < COFFHeader->NumberOfSymbols)
673 Result = SymbolTable + index;
675 return object_error::parse_failed;
676 return object_error::success;
679 error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
680 StringRef &Res) const {
681 // Check for string table entry. First 4 bytes are 0.
682 if (symbol->Name.Offset.Zeroes == 0) {
683 uint32_t Offset = symbol->Name.Offset.Offset;
684 if (error_code ec = getString(Offset, Res))
686 return object_error::success;
689 if (symbol->Name.ShortName[7] == 0)
690 // Null terminated, let ::strlen figure out the length.
691 Res = StringRef(symbol->Name.ShortName);
693 // Not null terminated, use all 8 bytes.
694 Res = StringRef(symbol->Name.ShortName, 8);
695 return object_error::success;
698 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
699 const coff_symbol *symbol) const {
700 const uint8_t *aux = NULL;
702 if ( symbol->NumberOfAuxSymbols > 0 ) {
703 // AUX data comes immediately after the symbol in COFF
704 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
706 // Verify that the aux symbol points to a valid entry in the symbol table.
707 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
708 if (offset < COFFHeader->PointerToSymbolTable
709 || offset >= COFFHeader->PointerToSymbolTable
710 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
711 report_fatal_error("Aux Symbol data was outside of symbol table.");
713 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
714 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
717 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
720 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
721 StringRef &Res) const {
723 if (Sec->Name[7] == 0)
724 // Null terminated, let ::strlen figure out the length.
727 // Not null terminated, use all 8 bytes.
728 Name = StringRef(Sec->Name, 8);
730 // Check for string table entry. First byte is '/'.
731 if (Name[0] == '/') {
733 if (Name.substr(1).getAsInteger(10, Offset))
734 return object_error::parse_failed;
735 if (error_code ec = getString(Offset, Name))
740 return object_error::success;
743 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
744 ArrayRef<uint8_t> &Res) const {
745 // The only thing that we need to verify is that the contents is contained
746 // within the file bounds. We don't need to make sure it doesn't cover other
747 // data, as there's nothing that says that is not allowed.
748 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
749 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
750 if (ConEnd > uintptr_t(Data->getBufferEnd()))
751 return object_error::parse_failed;
752 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
754 return object_error::success;
757 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
758 return reinterpret_cast<const coff_relocation*>(Rel.p);
760 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
761 RelocationRef &Res) const {
762 Rel.p = reinterpret_cast<uintptr_t>(
763 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
764 Res = RelocationRef(Rel, this);
765 return object_error::success;
767 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
768 uint64_t &Res) const {
769 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
771 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
772 uint64_t &Res) const {
773 Res = toRel(Rel)->VirtualAddress;
774 return object_error::success;
776 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
777 const coff_relocation* R = toRel(Rel);
779 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
780 return symbol_iterator(SymbolRef(Symb, this));
782 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
783 uint64_t &Res) const {
784 const coff_relocation* R = toRel(Rel);
786 return object_error::success;
789 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
790 return toSec(It->getRawDataRefImpl());
793 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
794 return toSymb(It->getRawDataRefImpl());
797 const coff_relocation *COFFObjectFile::getCOFFRelocation(
798 relocation_iterator &It) const {
799 return toRel(It->getRawDataRefImpl());
802 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
803 case COFF::enum: res = #enum; break;
805 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
806 SmallVectorImpl<char> &Result) const {
807 const coff_relocation *reloc = toRel(Rel);
809 switch (COFFHeader->Machine) {
810 case COFF::IMAGE_FILE_MACHINE_AMD64:
811 switch (reloc->Type) {
812 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
813 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
814 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
815 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
816 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
817 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
818 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
819 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
820 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
821 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
822 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
823 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
824 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
825 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
826 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
827 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
828 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
833 case COFF::IMAGE_FILE_MACHINE_I386:
834 switch (reloc->Type) {
835 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
836 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
837 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
838 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
839 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
840 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
841 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
842 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
843 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
844 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
845 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
853 Result.append(res.begin(), res.end());
854 return object_error::success;
857 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
859 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
860 SmallVectorImpl<char> &Result) const {
861 const coff_relocation *reloc = toRel(Rel);
862 const coff_symbol *symb = 0;
863 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
865 sym.p = reinterpret_cast<uintptr_t>(symb);
867 if (error_code ec = getSymbolName(sym, symname)) return ec;
868 Result.append(symname.begin(), symname.end());
869 return object_error::success;
872 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
873 LibraryRef &Result) const {
874 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
877 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
878 StringRef &Result) const {
879 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
882 bool ImportDirectoryEntryRef::
883 operator==(const ImportDirectoryEntryRef &Other) const {
884 return ImportDirectoryPimpl == Other.ImportDirectoryPimpl;
887 static const import_directory_table_entry *toImportEntry(DataRefImpl Imp) {
888 return reinterpret_cast<const import_directory_table_entry *>(Imp.p);
892 ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const {
893 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
896 Next.p = reinterpret_cast<uintptr_t>(Dir);
897 Result = ImportDirectoryEntryRef(Next, OwningObject);
898 return object_error::success;
901 error_code ImportDirectoryEntryRef::
902 getImportTableEntry(const import_directory_table_entry *&Result) const {
903 Result = toImportEntry(ImportDirectoryPimpl);
904 return object_error::success;
907 error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
908 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
909 uintptr_t IntPtr = 0;
910 if (error_code ec = OwningObject->getRvaPtr(Dir->NameRVA, IntPtr))
912 const char *Ptr = reinterpret_cast<const char *>(IntPtr);
913 Result = StringRef(Ptr);
914 return object_error::success;
917 error_code ImportDirectoryEntryRef::getImportLookupEntry(
918 const import_lookup_table_entry32 *&Result) const {
919 const import_directory_table_entry *Dir = toImportEntry(ImportDirectoryPimpl);
920 uintptr_t IntPtr = 0;
921 if (error_code ec = OwningObject->getRvaPtr(
922 Dir->ImportLookupTableRVA, IntPtr))
924 Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr);
925 return object_error::success;
930 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
932 return new COFFObjectFile(Object, ec);
935 } // end namespace llvm