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"
21 using namespace object;
24 using support::ulittle8_t;
25 using support::ulittle16_t;
26 using support::ulittle32_t;
27 using support::little16_t;
31 // Returns false if size is greater than the buffer size. And sets ec.
32 bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) {
33 if (m->getBufferSize() < size) {
34 ec = object_error::unexpected_eof;
40 // Returns false if any bytes in [addr, addr + size) fall outsize of m.
41 bool checkAddr(const MemoryBuffer *m,
45 if (addr + size < addr ||
47 addr + size > uintptr_t(m->getBufferEnd())) {
48 ec = object_error::unexpected_eof;
55 const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const {
56 const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p);
59 // Verify that the symbol points to a valid entry in the symbol table.
60 uintptr_t offset = uintptr_t(addr) - uintptr_t(base());
61 if (offset < COFFHeader->PointerToSymbolTable
62 || offset >= COFFHeader->PointerToSymbolTable
63 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
64 report_fatal_error("Symbol was outside of symbol table.");
66 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
67 == 0 && "Symbol did not point to the beginning of a symbol");
73 const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const {
74 const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p);
77 // Verify that the section points to a valid entry in the section table.
78 if (addr < SectionTable
79 || addr >= (SectionTable + COFFHeader->NumberOfSections))
80 report_fatal_error("Section was outside of section table.");
82 uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable);
83 assert(offset % sizeof(coff_section) == 0 &&
84 "Section did not point to the beginning of a section");
90 error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
91 SymbolRef &Result) const {
92 const coff_symbol *symb = toSymb(Symb);
93 symb += 1 + symb->NumberOfAuxSymbols;
94 Symb.p = reinterpret_cast<uintptr_t>(symb);
95 Result = SymbolRef(Symb, this);
96 return object_error::success;
99 error_code COFFObjectFile::getSymbolName(DataRefImpl Symb,
100 StringRef &Result) const {
101 const coff_symbol *symb = toSymb(Symb);
102 return getSymbolName(symb, Result);
105 error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
106 uint64_t &Result) const {
107 const coff_symbol *symb = toSymb(Symb);
108 const coff_section *Section = NULL;
109 if (error_code ec = getSection(symb->SectionNumber, Section))
112 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
114 if (Type == 'U' || Type == 'w')
115 Result = UnknownAddressOrSize;
117 Result = Section->PointerToRawData + symb->Value;
119 Result = symb->Value;
120 return object_error::success;
123 error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
124 uint64_t &Result) const {
125 const coff_symbol *symb = toSymb(Symb);
126 const coff_section *Section = NULL;
127 if (error_code ec = getSection(symb->SectionNumber, Section))
130 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
132 if (Type == 'U' || Type == 'w')
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;
153 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
155 if (Type == 'r' || Type == 'R') {
156 Result = SymbolRef::ST_Data;
160 return object_error::success;
163 error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb,
164 uint32_t &Result) const {
165 const coff_symbol *symb = toSymb(Symb);
166 Result = SymbolRef::SF_None;
168 // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common
170 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
171 symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED)
172 Result |= SymbolRef::SF_Undefined;
174 // TODO: This are certainly too restrictive.
175 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
176 Result |= SymbolRef::SF_Global;
178 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
179 Result |= SymbolRef::SF_Weak;
181 if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE)
182 Result |= SymbolRef::SF_Absolute;
184 return object_error::success;
187 error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb,
188 uint64_t &Result) const {
189 // FIXME: Return the correct size. This requires looking at all the symbols
190 // in the same section as this symbol, and looking for either the next
191 // symbol, or the end of the section.
192 const coff_symbol *symb = toSymb(Symb);
193 const coff_section *Section = NULL;
194 if (error_code ec = getSection(symb->SectionNumber, Section))
197 if (error_code ec = getSymbolNMTypeChar(Symb, Type))
199 if (Type == 'U' || Type == 'w')
200 Result = UnknownAddressOrSize;
202 Result = Section->SizeOfRawData - symb->Value;
205 return object_error::success;
208 error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
209 char &Result) const {
210 const coff_symbol *symb = toSymb(Symb);
212 if (error_code ec = getSymbolName(Symb, name))
214 char ret = StringSwitch<char>(name)
215 .StartsWith(".debug", 'N')
216 .StartsWith(".sxdata", 'N')
221 return object_error::success;
224 uint32_t Characteristics = 0;
225 if (symb->SectionNumber > 0) {
226 const coff_section *Section = NULL;
227 if (error_code ec = getSection(symb->SectionNumber, Section))
229 Characteristics = Section->Characteristics;
232 switch (symb->SectionNumber) {
233 case COFF::IMAGE_SYM_UNDEFINED:
234 // Check storage classes.
235 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) {
237 return object_error::success; // Don't do ::toupper.
238 } else if (symb->Value != 0) // Check for common symbols.
243 case COFF::IMAGE_SYM_ABSOLUTE:
246 case COFF::IMAGE_SYM_DEBUG:
250 // Check section type.
251 if (Characteristics & COFF::IMAGE_SCN_CNT_CODE)
253 else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ
254 && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only.
256 else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
258 else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
260 else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO)
263 // Check for section symbol.
264 else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
269 if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL)
270 ret = ::toupper(static_cast<unsigned char>(ret));
273 return object_error::success;
276 error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb,
277 section_iterator &Result) const {
278 const coff_symbol *symb = toSymb(Symb);
279 if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
280 Result = end_sections();
282 const coff_section *sec = 0;
283 if (error_code ec = getSection(symb->SectionNumber, sec)) return ec;
285 Sec.p = reinterpret_cast<uintptr_t>(sec);
286 Result = section_iterator(SectionRef(Sec, this));
288 return object_error::success;
291 error_code COFFObjectFile::getSymbolValue(DataRefImpl Symb,
292 uint64_t &Val) const {
293 report_fatal_error("getSymbolValue unimplemented in COFFObjectFile");
296 error_code COFFObjectFile::getSectionNext(DataRefImpl Sec,
297 SectionRef &Result) const {
298 const coff_section *sec = toSec(Sec);
300 Sec.p = reinterpret_cast<uintptr_t>(sec);
301 Result = SectionRef(Sec, this);
302 return object_error::success;
305 error_code COFFObjectFile::getSectionName(DataRefImpl Sec,
306 StringRef &Result) const {
307 const coff_section *sec = toSec(Sec);
308 return getSectionName(sec, Result);
311 error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec,
312 uint64_t &Result) const {
313 const coff_section *sec = toSec(Sec);
314 Result = sec->VirtualAddress;
315 return object_error::success;
318 error_code COFFObjectFile::getSectionSize(DataRefImpl Sec,
319 uint64_t &Result) const {
320 const coff_section *sec = toSec(Sec);
321 Result = sec->SizeOfRawData;
322 return object_error::success;
325 error_code COFFObjectFile::getSectionContents(DataRefImpl Sec,
326 StringRef &Result) const {
327 const coff_section *sec = toSec(Sec);
328 ArrayRef<uint8_t> Res;
329 error_code EC = getSectionContents(sec, Res);
330 Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
334 error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec,
335 uint64_t &Res) const {
336 const coff_section *sec = toSec(Sec);
338 return object_error::parse_failed;
339 Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1);
340 return object_error::success;
343 error_code COFFObjectFile::isSectionText(DataRefImpl Sec,
344 bool &Result) const {
345 const coff_section *sec = toSec(Sec);
346 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE;
347 return object_error::success;
350 error_code COFFObjectFile::isSectionData(DataRefImpl Sec,
351 bool &Result) const {
352 const coff_section *sec = toSec(Sec);
353 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
354 return object_error::success;
357 error_code COFFObjectFile::isSectionBSS(DataRefImpl Sec,
358 bool &Result) const {
359 const coff_section *sec = toSec(Sec);
360 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
361 return object_error::success;
364 error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
365 bool &Result) const {
366 // FIXME: Unimplemented
368 return object_error::success;
371 error_code COFFObjectFile::isSectionVirtual(DataRefImpl Sec,
372 bool &Result) const {
373 const coff_section *sec = toSec(Sec);
374 Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
375 return object_error::success;
378 error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Sec,
379 bool &Result) const {
380 // FIXME: Unimplemented.
382 return object_error::success;
385 error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
386 bool &Result) const {
387 // FIXME: Unimplemented.
389 return object_error::success;
392 error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec,
394 bool &Result) const {
395 const coff_section *sec = toSec(Sec);
396 const coff_symbol *symb = toSymb(Symb);
397 const coff_section *symb_sec = 0;
398 if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec;
403 return object_error::success;
406 relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
407 const coff_section *sec = toSec(Sec);
409 if (sec->NumberOfRelocations == 0)
412 ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations);
414 return relocation_iterator(RelocationRef(ret, this));
417 relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
418 const coff_section *sec = toSec(Sec);
420 if (sec->NumberOfRelocations == 0)
423 ret.p = reinterpret_cast<uintptr_t>(
424 reinterpret_cast<const coff_relocation*>(
425 base() + sec->PointerToRelocations)
426 + sec->NumberOfRelocations);
428 return relocation_iterator(RelocationRef(ret, this));
431 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
432 : ObjectFile(Binary::ID_COFF, Object)
438 , StringTableSize(0) {
439 // Check that we at least have enough room for a header.
440 if (!checkSize(Data, ec, sizeof(coff_file_header))) return;
442 // The current location in the file where we are looking at.
445 // PE header is optional and is present only in executables. If it exists,
446 // it is placed right after COFF header.
447 bool hasPEHeader = false;
449 // Check if this is a PE/COFF file.
450 if (base()[0] == 0x4d && base()[1] == 0x5a) {
451 // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
452 // PE signature to find 'normal' COFF header.
453 if (!checkSize(Data, ec, 0x3c + 8)) return;
454 CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c);
455 // Check the PE magic bytes. ("PE\0\0")
456 if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) {
457 ec = object_error::parse_failed;
460 CurPtr += 4; // Skip the PE magic bytes.
464 COFFHeader = reinterpret_cast<const coff_file_header *>(base() + CurPtr);
465 if (!checkAddr(Data, ec, uintptr_t(COFFHeader), sizeof(coff_file_header)))
467 CurPtr += sizeof(coff_file_header);
470 PE32Header = reinterpret_cast<const pe32_header *>(base() + CurPtr);
471 if (!checkAddr(Data, ec, uintptr_t(PE32Header), sizeof(pe32_header)))
473 // We only support PE32. If this is PE32 (not PE32+), the magic byte
474 // should be 0x10b. If this is not PE32, continue as if there's no PE
475 // header in this file.
476 if (PE32Header->Magic != 0x10b)
478 // There may be optional data directory after PE header. Skip them.
479 CurPtr += COFFHeader->SizeOfOptionalHeader;
483 reinterpret_cast<const coff_section *>(base() + CurPtr);
484 if (!checkAddr(Data, ec, uintptr_t(SectionTable),
485 COFFHeader->NumberOfSections * sizeof(coff_section)))
488 if (COFFHeader->PointerToSymbolTable != 0) {
490 reinterpret_cast<const coff_symbol *>(base()
491 + COFFHeader->PointerToSymbolTable);
492 if (!checkAddr(Data, ec, uintptr_t(SymbolTable),
493 COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
496 // Find string table.
497 StringTable = reinterpret_cast<const char *>(base())
498 + COFFHeader->PointerToSymbolTable
499 + COFFHeader->NumberOfSymbols * sizeof(coff_symbol);
500 if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t)))
503 StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable);
504 if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize))
506 // Check that the string table is null terminated if has any in it.
507 if (StringTableSize < 4
508 || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) {
509 ec = object_error::parse_failed;
514 ec = object_error::success;
517 symbol_iterator COFFObjectFile::begin_symbols() const {
519 ret.p = reinterpret_cast<intptr_t>(SymbolTable);
520 return symbol_iterator(SymbolRef(ret, this));
523 symbol_iterator COFFObjectFile::end_symbols() const {
524 // The symbol table ends where the string table begins.
526 ret.p = reinterpret_cast<intptr_t>(StringTable);
527 return symbol_iterator(SymbolRef(ret, this));
530 symbol_iterator COFFObjectFile::begin_dynamic_symbols() const {
532 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
535 symbol_iterator COFFObjectFile::end_dynamic_symbols() const {
537 report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile");
540 library_iterator COFFObjectFile::begin_libraries_needed() const {
542 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
545 library_iterator COFFObjectFile::end_libraries_needed() const {
547 report_fatal_error("Libraries needed unimplemented in COFFObjectFile");
550 StringRef COFFObjectFile::getLoadName() const {
551 // COFF does not have this field.
556 section_iterator COFFObjectFile::begin_sections() const {
558 ret.p = reinterpret_cast<intptr_t>(SectionTable);
559 return section_iterator(SectionRef(ret, this));
562 section_iterator COFFObjectFile::end_sections() const {
564 ret.p = reinterpret_cast<intptr_t>(SectionTable + COFFHeader->NumberOfSections);
565 return section_iterator(SectionRef(ret, this));
568 uint8_t COFFObjectFile::getBytesInAddress() const {
569 return getArch() == Triple::x86_64 ? 8 : 4;
572 StringRef COFFObjectFile::getFileFormatName() const {
573 switch(COFFHeader->Machine) {
574 case COFF::IMAGE_FILE_MACHINE_I386:
576 case COFF::IMAGE_FILE_MACHINE_AMD64:
577 return "COFF-x86-64";
579 return "COFF-<unknown arch>";
583 unsigned COFFObjectFile::getArch() const {
584 switch(COFFHeader->Machine) {
585 case COFF::IMAGE_FILE_MACHINE_I386:
587 case COFF::IMAGE_FILE_MACHINE_AMD64:
588 return Triple::x86_64;
590 return Triple::UnknownArch;
594 // This method is kept here because lld uses this. As soon as we make
595 // lld to use getCOFFHeader, this method will be removed.
596 error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const {
597 return getCOFFHeader(Res);
600 error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const {
602 return object_error::success;
605 error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
607 return object_error::success;
610 error_code COFFObjectFile::getSection(int32_t index,
611 const coff_section *&Result) const {
612 // Check for special index values.
613 if (index == COFF::IMAGE_SYM_UNDEFINED ||
614 index == COFF::IMAGE_SYM_ABSOLUTE ||
615 index == COFF::IMAGE_SYM_DEBUG)
617 else if (index > 0 && index <= COFFHeader->NumberOfSections)
618 // We already verified the section table data, so no need to check again.
619 Result = SectionTable + (index - 1);
621 return object_error::parse_failed;
622 return object_error::success;
625 error_code COFFObjectFile::getString(uint32_t offset,
626 StringRef &Result) const {
627 if (StringTableSize <= 4)
628 // Tried to get a string from an empty string table.
629 return object_error::parse_failed;
630 if (offset >= StringTableSize)
631 return object_error::unexpected_eof;
632 Result = StringRef(StringTable + offset);
633 return object_error::success;
636 error_code COFFObjectFile::getSymbol(uint32_t index,
637 const coff_symbol *&Result) const {
638 if (index < COFFHeader->NumberOfSymbols)
639 Result = SymbolTable + index;
641 return object_error::parse_failed;
642 return object_error::success;
645 error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol,
646 StringRef &Res) const {
647 // Check for string table entry. First 4 bytes are 0.
648 if (symbol->Name.Offset.Zeroes == 0) {
649 uint32_t Offset = symbol->Name.Offset.Offset;
650 if (error_code ec = getString(Offset, Res))
652 return object_error::success;
655 if (symbol->Name.ShortName[7] == 0)
656 // Null terminated, let ::strlen figure out the length.
657 Res = StringRef(symbol->Name.ShortName);
659 // Not null terminated, use all 8 bytes.
660 Res = StringRef(symbol->Name.ShortName, 8);
661 return object_error::success;
664 ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData(
665 const coff_symbol *symbol) const {
666 const uint8_t *aux = NULL;
668 if ( symbol->NumberOfAuxSymbols > 0 ) {
669 // AUX data comes immediately after the symbol in COFF
670 aux = reinterpret_cast<const uint8_t *>(symbol + 1);
672 // Verify that the aux symbol points to a valid entry in the symbol table.
673 uintptr_t offset = uintptr_t(aux) - uintptr_t(base());
674 if (offset < COFFHeader->PointerToSymbolTable
675 || offset >= COFFHeader->PointerToSymbolTable
676 + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol)))
677 report_fatal_error("Aux Symbol data was outside of symbol table.");
679 assert((offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol)
680 == 0 && "Aux Symbol data did not point to the beginning of a symbol");
683 return ArrayRef<uint8_t>(aux, symbol->NumberOfAuxSymbols * sizeof(coff_symbol));
686 error_code COFFObjectFile::getSectionName(const coff_section *Sec,
687 StringRef &Res) const {
689 if (Sec->Name[7] == 0)
690 // Null terminated, let ::strlen figure out the length.
693 // Not null terminated, use all 8 bytes.
694 Name = StringRef(Sec->Name, 8);
696 // Check for string table entry. First byte is '/'.
697 if (Name[0] == '/') {
699 if (Name.substr(1).getAsInteger(10, Offset))
700 return object_error::parse_failed;
701 if (error_code ec = getString(Offset, Name))
706 return object_error::success;
709 error_code COFFObjectFile::getSectionContents(const coff_section *Sec,
710 ArrayRef<uint8_t> &Res) const {
711 // The only thing that we need to verify is that the contents is contained
712 // within the file bounds. We don't need to make sure it doesn't cover other
713 // data, as there's nothing that says that is not allowed.
714 uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
715 uintptr_t ConEnd = ConStart + Sec->SizeOfRawData;
716 if (ConEnd > uintptr_t(Data->getBufferEnd()))
717 return object_error::parse_failed;
718 Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart),
720 return object_error::success;
723 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
724 return reinterpret_cast<const coff_relocation*>(Rel.p);
726 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
727 RelocationRef &Res) const {
728 Rel.p = reinterpret_cast<uintptr_t>(
729 reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
730 Res = RelocationRef(Rel, this);
731 return object_error::success;
733 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
734 uint64_t &Res) const {
735 report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
737 error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
738 uint64_t &Res) const {
739 Res = toRel(Rel)->VirtualAddress;
740 return object_error::success;
742 symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
743 const coff_relocation* R = toRel(Rel);
745 Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex);
746 return symbol_iterator(SymbolRef(Symb, this));
748 error_code COFFObjectFile::getRelocationType(DataRefImpl Rel,
749 uint64_t &Res) const {
750 const coff_relocation* R = toRel(Rel);
752 return object_error::success;
755 const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const {
756 return toSec(It->getRawDataRefImpl());
759 const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const {
760 return toSymb(It->getRawDataRefImpl());
763 const coff_relocation *COFFObjectFile::getCOFFRelocation(
764 relocation_iterator &It) const {
765 return toRel(It->getRawDataRefImpl());
769 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
770 case COFF::enum: res = #enum; break;
772 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
773 SmallVectorImpl<char> &Result) const {
774 const coff_relocation *reloc = toRel(Rel);
776 switch (COFFHeader->Machine) {
777 case COFF::IMAGE_FILE_MACHINE_AMD64:
778 switch (reloc->Type) {
779 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
780 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
781 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
782 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
783 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
784 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
785 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
786 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
787 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
788 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
789 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
790 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
791 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
792 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
793 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
794 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
795 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
800 case COFF::IMAGE_FILE_MACHINE_I386:
801 switch (reloc->Type) {
802 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
803 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
804 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
805 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
806 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
807 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
808 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
809 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
810 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
811 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
812 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
820 Result.append(res.begin(), res.end());
821 return object_error::success;
824 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
826 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
827 SmallVectorImpl<char> &Result) const {
828 const coff_relocation *reloc = toRel(Rel);
829 const coff_symbol *symb = 0;
830 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
832 sym.p = reinterpret_cast<uintptr_t>(symb);
834 if (error_code ec = getSymbolName(sym, symname)) return ec;
835 Result.append(symname.begin(), symname.end());
836 return object_error::success;
839 error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData,
840 LibraryRef &Result) const {
841 report_fatal_error("getLibraryNext not implemented in COFFObjectFile");
844 error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData,
845 StringRef &Result) const {
846 report_fatal_error("getLibraryPath not implemented in COFFObjectFile");
851 ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) {
853 return new COFFObjectFile(Object, ec);
856 } // end namespace llvm