Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 1 | //===- COFFObjectFile.cpp - COFF object file implementation -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the COFFObjectFile class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 14 | #include "llvm/Object/COFF.h" |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ArrayRef.h" |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
| 18 | #include "llvm/ADT/Triple.h" |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 21 | #include <cctype> |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | using namespace object; |
| 25 | |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 26 | using support::ulittle8_t; |
| 27 | using support::ulittle16_t; |
| 28 | using support::ulittle32_t; |
| 29 | using support::little16_t; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 30 | |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 31 | // Returns false if size is greater than the buffer size. And sets ec. |
Rui Ueyama | 686738e | 2014-01-16 20:30:36 +0000 | [diff] [blame^] | 32 | static bool checkSize(const MemoryBuffer *M, error_code &EC, uint64_t Size) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 33 | if (M->getBufferSize() < Size) { |
| 34 | EC = object_error::unexpected_eof; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 35 | return false; |
| 36 | } |
| 37 | return true; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 40 | // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. |
| 41 | // Returns unexpected_eof if error. |
| 42 | template<typename T> |
Rui Ueyama | 686738e | 2014-01-16 20:30:36 +0000 | [diff] [blame^] | 43 | static error_code getObject(const T *&Obj, const MemoryBuffer *M, |
| 44 | const uint8_t *Ptr, const size_t Size = sizeof(T)) { |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 45 | uintptr_t Addr = uintptr_t(Ptr); |
| 46 | if (Addr + Size < Addr || |
| 47 | Addr + Size < Size || |
| 48 | Addr + Size > uintptr_t(M->getBufferEnd())) { |
| 49 | return object_error::unexpected_eof; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 50 | } |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 51 | Obj = reinterpret_cast<const T *>(Addr); |
| 52 | return object_error::success; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 53 | } |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 54 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 55 | const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Ref) const { |
| 56 | const coff_symbol *Addr = reinterpret_cast<const coff_symbol*>(Ref.p); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 57 | |
| 58 | # ifndef NDEBUG |
| 59 | // Verify that the symbol points to a valid entry in the symbol table. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 60 | uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); |
| 61 | if (Offset < COFFHeader->PointerToSymbolTable |
| 62 | || Offset >= COFFHeader->PointerToSymbolTable |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 63 | + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 64 | report_fatal_error("Symbol was outside of symbol table."); |
| 65 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 66 | assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 67 | == 0 && "Symbol did not point to the beginning of a symbol"); |
| 68 | # endif |
| 69 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 70 | return Addr; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 73 | const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { |
| 74 | const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 75 | |
| 76 | # ifndef NDEBUG |
| 77 | // Verify that the section points to a valid entry in the section table. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 78 | if (Addr < SectionTable |
| 79 | || Addr >= (SectionTable + COFFHeader->NumberOfSections)) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 80 | report_fatal_error("Section was outside of section table."); |
| 81 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 82 | uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); |
| 83 | assert(Offset % sizeof(coff_section) == 0 && |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 84 | "Section did not point to the beginning of a section"); |
| 85 | # endif |
| 86 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 87 | return Addr; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 90 | error_code COFFObjectFile::getSymbolNext(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 91 | SymbolRef &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 92 | const coff_symbol *Symb = toSymb(Ref); |
| 93 | Symb += 1 + Symb->NumberOfAuxSymbols; |
| 94 | Ref.p = reinterpret_cast<uintptr_t>(Symb); |
| 95 | Result = SymbolRef(Ref, this); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 96 | return object_error::success; |
| 97 | } |
| 98 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 99 | error_code COFFObjectFile::getSymbolName(DataRefImpl Ref, |
| 100 | StringRef &Result) const { |
| 101 | const coff_symbol *Symb = toSymb(Ref); |
| 102 | return getSymbolName(Symb, Result); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 105 | error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 106 | uint64_t &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 107 | const coff_symbol *Symb = toSymb(Ref); |
Michael J. Spencer | 5ebaed2 | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 108 | const coff_section *Section = NULL; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 109 | if (error_code EC = getSection(Symb->SectionNumber, Section)) |
| 110 | return EC; |
Rafael Espindola | e62ab11 | 2013-11-02 18:07:48 +0000 | [diff] [blame] | 111 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 112 | if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 113 | Result = UnknownAddressOrSize; |
| 114 | else if (Section) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 115 | Result = Section->PointerToRawData + Symb->Value; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 116 | else |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 117 | Result = Symb->Value; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 118 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 121 | error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref, |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 122 | uint64_t &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 123 | const coff_symbol *Symb = toSymb(Ref); |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 124 | const coff_section *Section = NULL; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 125 | if (error_code EC = getSection(Symb->SectionNumber, Section)) |
| 126 | return EC; |
Rafael Espindola | e62ab11 | 2013-11-02 18:07:48 +0000 | [diff] [blame] | 127 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 128 | if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 129 | Result = UnknownAddressOrSize; |
| 130 | else if (Section) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 131 | Result = Section->VirtualAddress + Symb->Value; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 132 | else |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 133 | Result = Symb->Value; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 134 | return object_error::success; |
| 135 | } |
| 136 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 137 | error_code COFFObjectFile::getSymbolType(DataRefImpl Ref, |
Michael J. Spencer | d394667 | 2011-10-17 20:19:29 +0000 | [diff] [blame] | 138 | SymbolRef::Type &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 139 | const coff_symbol *Symb = toSymb(Ref); |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 140 | Result = SymbolRef::ST_Other; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 141 | if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && |
| 142 | Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { |
David Meyer | 7e4b976 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 143 | Result = SymbolRef::ST_Unknown; |
Rui Ueyama | 5efa665 | 2014-01-16 20:22:55 +0000 | [diff] [blame] | 144 | } else if (Symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) { |
| 145 | Result = SymbolRef::ST_Function; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 146 | } else { |
Rui Ueyama | 5efa665 | 2014-01-16 20:22:55 +0000 | [diff] [blame] | 147 | uint32_t Characteristics = 0; |
| 148 | if (Symb->SectionNumber > 0) { |
| 149 | const coff_section *Section = NULL; |
| 150 | if (error_code EC = getSection(Symb->SectionNumber, Section)) |
| 151 | return EC; |
| 152 | Characteristics = Section->Characteristics; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 153 | } |
Rui Ueyama | 5efa665 | 2014-01-16 20:22:55 +0000 | [diff] [blame] | 154 | if (Characteristics & COFF::IMAGE_SCN_MEM_READ && |
| 155 | ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. |
| 156 | Result = SymbolRef::ST_Data; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 157 | } |
| 158 | return object_error::success; |
| 159 | } |
| 160 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 161 | error_code COFFObjectFile::getSymbolFlags(DataRefImpl Ref, |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 162 | uint32_t &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 163 | const coff_symbol *Symb = toSymb(Ref); |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 164 | Result = SymbolRef::SF_None; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 165 | |
David Meyer | 7e4b976 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 166 | // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common |
| 167 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 168 | if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && |
| 169 | Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) |
David Meyer | 7e4b976 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 170 | Result |= SymbolRef::SF_Undefined; |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 171 | |
| 172 | // TODO: This are certainly too restrictive. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 173 | if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 174 | Result |= SymbolRef::SF_Global; |
| 175 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 176 | if (Symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 177 | Result |= SymbolRef::SF_Weak; |
| 178 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 179 | if (Symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE) |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 180 | Result |= SymbolRef::SF_Absolute; |
| 181 | |
Michael J. Spencer | 0175975 | 2011-10-17 23:54:22 +0000 | [diff] [blame] | 182 | return object_error::success; |
| 183 | } |
| 184 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 185 | error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 186 | uint64_t &Result) const { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 187 | // FIXME: Return the correct size. This requires looking at all the symbols |
| 188 | // in the same section as this symbol, and looking for either the next |
| 189 | // symbol, or the end of the section. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 190 | const coff_symbol *Symb = toSymb(Ref); |
Michael J. Spencer | 5ebaed2 | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 191 | const coff_section *Section = NULL; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 192 | if (error_code EC = getSection(Symb->SectionNumber, Section)) |
| 193 | return EC; |
Rafael Espindola | e62ab11 | 2013-11-02 18:07:48 +0000 | [diff] [blame] | 194 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 195 | if (Symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 196 | Result = UnknownAddressOrSize; |
| 197 | else if (Section) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 198 | Result = Section->SizeOfRawData - Symb->Value; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 199 | else |
| 200 | Result = 0; |
| 201 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 204 | error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref, |
Michael J. Spencer | 321731539 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 205 | section_iterator &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 206 | const coff_symbol *Symb = toSymb(Ref); |
| 207 | if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED) |
Michael J. Spencer | 321731539 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 208 | Result = end_sections(); |
| 209 | else { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 210 | const coff_section *Sec = 0; |
| 211 | if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC; |
| 212 | DataRefImpl Ref; |
| 213 | Ref.p = reinterpret_cast<uintptr_t>(Sec); |
| 214 | Result = section_iterator(SectionRef(Ref, this)); |
Michael J. Spencer | 321731539 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 215 | } |
| 216 | return object_error::success; |
| 217 | } |
| 218 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 219 | error_code COFFObjectFile::getSymbolValue(DataRefImpl Ref, |
Tim Northover | 4f223bf7 | 2012-10-29 10:47:00 +0000 | [diff] [blame] | 220 | uint64_t &Val) const { |
| 221 | report_fatal_error("getSymbolValue unimplemented in COFFObjectFile"); |
| 222 | } |
| 223 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 224 | error_code COFFObjectFile::getSectionNext(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 225 | SectionRef &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 226 | const coff_section *Sec = toSec(Ref); |
| 227 | Sec += 1; |
| 228 | Ref.p = reinterpret_cast<uintptr_t>(Sec); |
| 229 | Result = SectionRef(Ref, this); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 230 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 233 | error_code COFFObjectFile::getSectionName(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 234 | StringRef &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 235 | const coff_section *Sec = toSec(Ref); |
| 236 | return getSectionName(Sec, Result); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 239 | error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 240 | uint64_t &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 241 | const coff_section *Sec = toSec(Ref); |
| 242 | Result = Sec->VirtualAddress; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 243 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 246 | error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 247 | uint64_t &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 248 | const coff_section *Sec = toSec(Ref); |
| 249 | Result = Sec->SizeOfRawData; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 250 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 253 | error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 254 | StringRef &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 255 | const coff_section *Sec = toSec(Ref); |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 256 | ArrayRef<uint8_t> Res; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 257 | error_code EC = getSectionContents(Sec, Res); |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 258 | Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); |
| 259 | return EC; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 262 | error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, |
Michael J. Spencer | 7989460 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 263 | uint64_t &Res) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 264 | const coff_section *Sec = toSec(Ref); |
| 265 | if (!Sec) |
Michael J. Spencer | 7989460 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 266 | return object_error::parse_failed; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 267 | Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); |
Michael J. Spencer | 7989460 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 268 | return object_error::success; |
| 269 | } |
| 270 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 271 | error_code COFFObjectFile::isSectionText(DataRefImpl Ref, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 272 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 273 | const coff_section *Sec = toSec(Ref); |
| 274 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 275 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 278 | error_code COFFObjectFile::isSectionData(DataRefImpl Ref, |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 279 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 280 | const coff_section *Sec = toSec(Ref); |
| 281 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 282 | return object_error::success; |
| 283 | } |
| 284 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 285 | error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 286 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 287 | const coff_section *Sec = toSec(Ref); |
| 288 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 289 | return object_error::success; |
| 290 | } |
| 291 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 292 | error_code COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref, |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 293 | bool &Result) const { |
| 294 | // FIXME: Unimplemented |
| 295 | Result = true; |
| 296 | return object_error::success; |
| 297 | } |
| 298 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 299 | error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 300 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 301 | const coff_section *Sec = toSec(Ref); |
| 302 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 303 | return object_error::success; |
| 304 | } |
| 305 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 306 | error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 307 | bool &Result) const { |
Andrew Kaylor | b96a320 | 2012-10-10 01:45:52 +0000 | [diff] [blame] | 308 | // FIXME: Unimplemented. |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 309 | Result = false; |
| 310 | return object_error::success; |
| 311 | } |
| 312 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 313 | error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, |
Andrew Kaylor | 3f31fa0 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 314 | bool &Result) const { |
| 315 | // FIXME: Unimplemented. |
| 316 | Result = false; |
| 317 | return object_error::success; |
| 318 | } |
| 319 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 320 | error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, |
| 321 | DataRefImpl SymbRef, |
Benjamin Kramer | f6f3e81 | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 322 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 323 | const coff_section *Sec = toSec(SecRef); |
| 324 | const coff_symbol *Symb = toSymb(SymbRef); |
| 325 | const coff_section *SymbSec = 0; |
| 326 | if (error_code EC = getSection(Symb->SectionNumber, SymbSec)) return EC; |
| 327 | if (SymbSec == Sec) |
Michael J. Spencer | 9a28851 | 2011-10-13 20:36:54 +0000 | [diff] [blame] | 328 | Result = true; |
| 329 | else |
| 330 | Result = false; |
Benjamin Kramer | f6f3e81 | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 331 | return object_error::success; |
| 332 | } |
| 333 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 334 | relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { |
| 335 | const coff_section *Sec = toSec(Ref); |
| 336 | DataRefImpl Ret; |
| 337 | if (Sec->NumberOfRelocations == 0) |
| 338 | Ret.p = 0; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 339 | else |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 340 | Ret.p = reinterpret_cast<uintptr_t>(base() + Sec->PointerToRelocations); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 341 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 342 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 345 | relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { |
| 346 | const coff_section *Sec = toSec(Ref); |
| 347 | DataRefImpl Ret; |
| 348 | if (Sec->NumberOfRelocations == 0) |
| 349 | Ret.p = 0; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 350 | else |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 351 | Ret.p = reinterpret_cast<uintptr_t>( |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 352 | reinterpret_cast<const coff_relocation*>( |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 353 | base() + Sec->PointerToRelocations) |
| 354 | + Sec->NumberOfRelocations); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 355 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 356 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 359 | // Initialize the pointer to the symbol table. |
| 360 | error_code COFFObjectFile::initSymbolTablePtr() { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 361 | if (error_code EC = getObject( |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 362 | SymbolTable, Data, base() + COFFHeader->PointerToSymbolTable, |
| 363 | COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 364 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 365 | |
| 366 | // Find string table. The first four byte of the string table contains the |
| 367 | // total size of the string table, including the size field itself. If the |
| 368 | // string table is empty, the value of the first four byte would be 4. |
| 369 | const uint8_t *StringTableAddr = |
| 370 | base() + COFFHeader->PointerToSymbolTable + |
| 371 | COFFHeader->NumberOfSymbols * sizeof(coff_symbol); |
| 372 | const ulittle32_t *StringTableSizePtr; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 373 | if (error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) |
| 374 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 375 | StringTableSize = *StringTableSizePtr; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 376 | if (error_code EC = |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 377 | getObject(StringTable, Data, StringTableAddr, StringTableSize)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 378 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 379 | |
| 380 | // Check that the string table is null terminated if has any in it. |
| 381 | if (StringTableSize < 4 || |
| 382 | (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) |
| 383 | return object_error::parse_failed; |
| 384 | return object_error::success; |
| 385 | } |
| 386 | |
| 387 | // Returns the file offset for the given RVA. |
| 388 | error_code COFFObjectFile::getRvaPtr(uint32_t Rva, uintptr_t &Res) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 389 | error_code EC; |
| 390 | for (section_iterator I = begin_sections(), E = end_sections(); I != E; |
| 391 | I.increment(EC)) { |
| 392 | if (EC) |
| 393 | return EC; |
| 394 | const coff_section *Section = getCOFFSection(I); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 395 | uint32_t SectionStart = Section->VirtualAddress; |
| 396 | uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; |
| 397 | if (SectionStart <= Rva && Rva < SectionEnd) { |
| 398 | uint32_t Offset = Rva - SectionStart; |
| 399 | Res = uintptr_t(base()) + Section->PointerToRawData + Offset; |
| 400 | return object_error::success; |
| 401 | } |
| 402 | } |
| 403 | return object_error::parse_failed; |
| 404 | } |
| 405 | |
| 406 | // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name |
| 407 | // table entry. |
| 408 | error_code COFFObjectFile:: |
| 409 | getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const { |
| 410 | uintptr_t IntPtr = 0; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 411 | if (error_code EC = getRvaPtr(Rva, IntPtr)) |
| 412 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 413 | const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); |
| 414 | Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); |
| 415 | Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); |
| 416 | return object_error::success; |
| 417 | } |
| 418 | |
| 419 | // Find the import table. |
| 420 | error_code COFFObjectFile::initImportTablePtr() { |
| 421 | // First, we get the RVA of the import table. If the file lacks a pointer to |
| 422 | // the import table, do nothing. |
| 423 | const data_directory *DataEntry; |
| 424 | if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) |
| 425 | return object_error::success; |
| 426 | |
| 427 | // Do nothing if the pointer to import table is NULL. |
| 428 | if (DataEntry->RelativeVirtualAddress == 0) |
| 429 | return object_error::success; |
| 430 | |
| 431 | uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; |
| 432 | NumberOfImportDirectory = DataEntry->Size / |
| 433 | sizeof(import_directory_table_entry); |
| 434 | |
| 435 | // Find the section that contains the RVA. This is needed because the RVA is |
| 436 | // the import table's memory address which is different from its file offset. |
| 437 | uintptr_t IntPtr = 0; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 438 | if (error_code EC = getRvaPtr(ImportTableRva, IntPtr)) |
| 439 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 440 | ImportDirectory = reinterpret_cast< |
| 441 | const import_directory_table_entry *>(IntPtr); |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 442 | return object_error::success; |
| 443 | } |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 444 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 445 | // Find the export table. |
| 446 | error_code COFFObjectFile::initExportTablePtr() { |
| 447 | // First, we get the RVA of the export table. If the file lacks a pointer to |
| 448 | // the export table, do nothing. |
| 449 | const data_directory *DataEntry; |
| 450 | if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) |
| 451 | return object_error::success; |
| 452 | |
| 453 | // Do nothing if the pointer to export table is NULL. |
| 454 | if (DataEntry->RelativeVirtualAddress == 0) |
| 455 | return object_error::success; |
| 456 | |
| 457 | uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; |
| 458 | uintptr_t IntPtr = 0; |
| 459 | if (error_code EC = getRvaPtr(ExportTableRva, IntPtr)) |
| 460 | return EC; |
| 461 | ExportDirectory = reinterpret_cast<const export_directory_table_entry *>(IntPtr); |
| 462 | return object_error::success; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 465 | COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &EC) |
| 466 | : ObjectFile(Binary::ID_COFF, Object), COFFHeader(0), PE32Header(0), |
| 467 | DataDirectory(0), SectionTable(0), SymbolTable(0), StringTable(0), |
| 468 | StringTableSize(0), ImportDirectory(0), NumberOfImportDirectory(0), |
| 469 | ExportDirectory(0) { |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 470 | // Check that we at least have enough room for a header. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 471 | if (!checkSize(Data, EC, sizeof(coff_file_header))) return; |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 472 | |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 473 | // The current location in the file where we are looking at. |
| 474 | uint64_t CurPtr = 0; |
| 475 | |
| 476 | // PE header is optional and is present only in executables. If it exists, |
| 477 | // it is placed right after COFF header. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 478 | bool HasPEHeader = false; |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 479 | |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 480 | // Check if this is a PE/COFF file. |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 481 | if (base()[0] == 0x4d && base()[1] == 0x5a) { |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 482 | // PE/COFF, seek through MS-DOS compatibility stub and 4-byte |
| 483 | // PE signature to find 'normal' COFF header. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 484 | if (!checkSize(Data, EC, 0x3c + 8)) return; |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 485 | CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); |
| 486 | // Check the PE magic bytes. ("PE\0\0") |
| 487 | if (std::memcmp(base() + CurPtr, "PE\0\0", 4) != 0) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 488 | EC = object_error::parse_failed; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 489 | return; |
| 490 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 491 | CurPtr += 4; // Skip the PE magic bytes. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 492 | HasPEHeader = true; |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 495 | if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 496 | return; |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 497 | CurPtr += sizeof(coff_file_header); |
| 498 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 499 | if (HasPEHeader) { |
| 500 | if ((EC = getObject(PE32Header, Data, base() + CurPtr))) |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 501 | return; |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 502 | if (PE32Header->Magic != 0x10b) { |
| 503 | // We only support PE32. If this is PE32 (not PE32+), the magic byte |
| 504 | // should be 0x10b. If this is not PE32, continue as if there's no PE |
| 505 | // header in this file. |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 506 | PE32Header = 0; |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 507 | } else if (PE32Header->NumberOfRvaAndSize > 0) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 508 | const uint8_t *Addr = base() + CurPtr + sizeof(pe32_header); |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 509 | uint64_t size = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 510 | if ((EC = getObject(DataDirectory, Data, Addr, size))) |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 511 | return; |
| 512 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 513 | CurPtr += COFFHeader->SizeOfOptionalHeader; |
| 514 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 515 | |
Rui Ueyama | 15ba1e2 | 2013-11-15 20:23:25 +0000 | [diff] [blame] | 516 | if (!COFFHeader->isImportLibrary()) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 517 | if ((EC = getObject(SectionTable, Data, base() + CurPtr, |
Rui Ueyama | 15ba1e2 | 2013-11-15 20:23:25 +0000 | [diff] [blame] | 518 | COFFHeader->NumberOfSections * sizeof(coff_section)))) |
| 519 | return; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 520 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 521 | // Initialize the pointer to the symbol table. |
| 522 | if (COFFHeader->PointerToSymbolTable != 0) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 523 | if ((EC = initSymbolTablePtr())) |
Michael J. Spencer | d5930ca | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 524 | return; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 525 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 526 | // Initialize the pointer to the beginning of the import table. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 527 | if ((EC = initImportTablePtr())) |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 528 | return; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 529 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 530 | // Initialize the pointer to the export table. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 531 | if ((EC = initExportTablePtr())) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 532 | return; |
| 533 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 534 | EC = object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 537 | symbol_iterator COFFObjectFile::begin_symbols() const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 538 | DataRefImpl Ret; |
| 539 | Ret.p = reinterpret_cast<uintptr_t>(SymbolTable); |
| 540 | return symbol_iterator(SymbolRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 543 | symbol_iterator COFFObjectFile::end_symbols() const { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 544 | // The symbol table ends where the string table begins. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 545 | DataRefImpl Ret; |
| 546 | Ret.p = reinterpret_cast<uintptr_t>(StringTable); |
| 547 | return symbol_iterator(SymbolRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Michael J. Spencer | 8c4729f | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 550 | symbol_iterator COFFObjectFile::begin_dynamic_symbols() const { |
| 551 | // TODO: implement |
| 552 | report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); |
| 553 | } |
| 554 | |
| 555 | symbol_iterator COFFObjectFile::end_dynamic_symbols() const { |
| 556 | // TODO: implement |
| 557 | report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); |
| 558 | } |
| 559 | |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 560 | library_iterator COFFObjectFile::begin_libraries_needed() const { |
| 561 | // TODO: implement |
| 562 | report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); |
| 563 | } |
| 564 | |
| 565 | library_iterator COFFObjectFile::end_libraries_needed() const { |
| 566 | // TODO: implement |
| 567 | report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); |
| 568 | } |
| 569 | |
David Meyer | c429b80 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 570 | StringRef COFFObjectFile::getLoadName() const { |
| 571 | // COFF does not have this field. |
| 572 | return ""; |
| 573 | } |
| 574 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 575 | import_directory_iterator COFFObjectFile::import_directory_begin() const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 576 | return import_directory_iterator( |
| 577 | ImportDirectoryEntryRef(ImportDirectory, 0, this)); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 580 | import_directory_iterator COFFObjectFile::import_directory_end() const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 581 | return import_directory_iterator( |
| 582 | ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 583 | } |
David Meyer | c429b80 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 584 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 585 | export_directory_iterator COFFObjectFile::export_directory_begin() const { |
| 586 | return export_directory_iterator( |
| 587 | ExportDirectoryEntryRef(ExportDirectory, 0, this)); |
| 588 | } |
| 589 | |
| 590 | export_directory_iterator COFFObjectFile::export_directory_end() const { |
| 591 | if (ExportDirectory == 0) |
| 592 | return export_directory_iterator(ExportDirectoryEntryRef(0, 0, this)); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 593 | ExportDirectoryEntryRef Ref(ExportDirectory, |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 594 | ExportDirectory->AddressTableEntries, this); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 595 | return export_directory_iterator(Ref); |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 598 | section_iterator COFFObjectFile::begin_sections() const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 599 | DataRefImpl Ret; |
| 600 | Ret.p = reinterpret_cast<uintptr_t>(SectionTable); |
| 601 | return section_iterator(SectionRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 604 | section_iterator COFFObjectFile::end_sections() const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 605 | DataRefImpl Ret; |
| 606 | int NumSections = COFFHeader->isImportLibrary() |
Rui Ueyama | 15ba1e2 | 2013-11-15 20:23:25 +0000 | [diff] [blame] | 607 | ? 0 : COFFHeader->NumberOfSections; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 608 | Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); |
| 609 | return section_iterator(SectionRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | uint8_t COFFObjectFile::getBytesInAddress() const { |
Michael J. Spencer | 0324b67 | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 613 | return getArch() == Triple::x86_64 ? 8 : 4; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | StringRef COFFObjectFile::getFileFormatName() const { |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 617 | switch(COFFHeader->Machine) { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 618 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 619 | return "COFF-i386"; |
| 620 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 621 | return "COFF-x86-64"; |
| 622 | default: |
| 623 | return "COFF-<unknown arch>"; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | unsigned COFFObjectFile::getArch() const { |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 628 | switch(COFFHeader->Machine) { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 629 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 630 | return Triple::x86; |
| 631 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 632 | return Triple::x86_64; |
| 633 | default: |
| 634 | return Triple::UnknownArch; |
| 635 | } |
| 636 | } |
| 637 | |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 638 | // This method is kept here because lld uses this. As soon as we make |
| 639 | // lld to use getCOFFHeader, this method will be removed. |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 640 | error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const { |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 641 | return getCOFFHeader(Res); |
| 642 | } |
| 643 | |
| 644 | error_code COFFObjectFile::getCOFFHeader(const coff_file_header *&Res) const { |
| 645 | Res = COFFHeader; |
| 646 | return object_error::success; |
| 647 | } |
| 648 | |
| 649 | error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { |
| 650 | Res = PE32Header; |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 651 | return object_error::success; |
| 652 | } |
| 653 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 654 | error_code COFFObjectFile::getDataDirectory(uint32_t Index, |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 655 | const data_directory *&Res) const { |
| 656 | // Error if if there's no data directory or the index is out of range. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 657 | if (!DataDirectory || Index > PE32Header->NumberOfRvaAndSize) |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 658 | return object_error::parse_failed; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 659 | Res = &DataDirectory[Index]; |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 660 | return object_error::success; |
| 661 | } |
| 662 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 663 | error_code COFFObjectFile::getSection(int32_t Index, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 664 | const coff_section *&Result) const { |
| 665 | // Check for special index values. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 666 | if (Index == COFF::IMAGE_SYM_UNDEFINED || |
| 667 | Index == COFF::IMAGE_SYM_ABSOLUTE || |
| 668 | Index == COFF::IMAGE_SYM_DEBUG) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 669 | Result = NULL; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 670 | else if (Index > 0 && Index <= COFFHeader->NumberOfSections) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 671 | // We already verified the section table data, so no need to check again. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 672 | Result = SectionTable + (Index - 1); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 673 | else |
| 674 | return object_error::parse_failed; |
| 675 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 678 | error_code COFFObjectFile::getString(uint32_t Offset, |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 679 | StringRef &Result) const { |
| 680 | if (StringTableSize <= 4) |
| 681 | // Tried to get a string from an empty string table. |
| 682 | return object_error::parse_failed; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 683 | if (Offset >= StringTableSize) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 684 | return object_error::unexpected_eof; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 685 | Result = StringRef(StringTable + Offset); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 686 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 689 | error_code COFFObjectFile::getSymbol(uint32_t Index, |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 690 | const coff_symbol *&Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 691 | if (Index < COFFHeader->NumberOfSymbols) |
| 692 | Result = SymbolTable + Index; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 693 | else |
| 694 | return object_error::parse_failed; |
| 695 | return object_error::success; |
| 696 | } |
| 697 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 698 | error_code COFFObjectFile::getSymbolName(const coff_symbol *Symbol, |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 699 | StringRef &Res) const { |
| 700 | // Check for string table entry. First 4 bytes are 0. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 701 | if (Symbol->Name.Offset.Zeroes == 0) { |
| 702 | uint32_t Offset = Symbol->Name.Offset.Offset; |
| 703 | if (error_code EC = getString(Offset, Res)) |
| 704 | return EC; |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 705 | return object_error::success; |
| 706 | } |
| 707 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 708 | if (Symbol->Name.ShortName[7] == 0) |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 709 | // Null terminated, let ::strlen figure out the length. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 710 | Res = StringRef(Symbol->Name.ShortName); |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 711 | else |
| 712 | // Not null terminated, use all 8 bytes. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 713 | Res = StringRef(Symbol->Name.ShortName, 8); |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 714 | return object_error::success; |
| 715 | } |
| 716 | |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 717 | ArrayRef<uint8_t> COFFObjectFile::getSymbolAuxData( |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 718 | const coff_symbol *Symbol) const { |
| 719 | const uint8_t *Aux = NULL; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 720 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 721 | if (Symbol->NumberOfAuxSymbols > 0) { |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 722 | // AUX data comes immediately after the symbol in COFF |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 723 | Aux = reinterpret_cast<const uint8_t *>(Symbol + 1); |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 724 | # ifndef NDEBUG |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 725 | // Verify that the Aux symbol points to a valid entry in the symbol table. |
| 726 | uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); |
| 727 | if (Offset < COFFHeader->PointerToSymbolTable |
| 728 | || Offset >= COFFHeader->PointerToSymbolTable |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 729 | + (COFFHeader->NumberOfSymbols * sizeof(coff_symbol))) |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 730 | report_fatal_error("Aux Symbol data was outside of symbol table."); |
| 731 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 732 | assert((Offset - COFFHeader->PointerToSymbolTable) % sizeof(coff_symbol) |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 733 | == 0 && "Aux Symbol data did not point to the beginning of a symbol"); |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 734 | # endif |
Marshall Clow | bfb85e6 | 2012-06-15 01:15:47 +0000 | [diff] [blame] | 735 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 736 | return ArrayRef<uint8_t>(Aux, Symbol->NumberOfAuxSymbols * sizeof(coff_symbol)); |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Michael J. Spencer | 53c2d54 | 2012-03-19 20:27:15 +0000 | [diff] [blame] | 739 | error_code COFFObjectFile::getSectionName(const coff_section *Sec, |
| 740 | StringRef &Res) const { |
| 741 | StringRef Name; |
| 742 | if (Sec->Name[7] == 0) |
| 743 | // Null terminated, let ::strlen figure out the length. |
| 744 | Name = Sec->Name; |
| 745 | else |
| 746 | // Not null terminated, use all 8 bytes. |
| 747 | Name = StringRef(Sec->Name, 8); |
| 748 | |
| 749 | // Check for string table entry. First byte is '/'. |
| 750 | if (Name[0] == '/') { |
| 751 | uint32_t Offset; |
| 752 | if (Name.substr(1).getAsInteger(10, Offset)) |
| 753 | return object_error::parse_failed; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 754 | if (error_code EC = getString(Offset, Name)) |
| 755 | return EC; |
Michael J. Spencer | 53c2d54 | 2012-03-19 20:27:15 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | Res = Name; |
| 759 | return object_error::success; |
| 760 | } |
| 761 | |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 762 | error_code COFFObjectFile::getSectionContents(const coff_section *Sec, |
| 763 | ArrayRef<uint8_t> &Res) const { |
| 764 | // The only thing that we need to verify is that the contents is contained |
| 765 | // within the file bounds. We don't need to make sure it doesn't cover other |
| 766 | // data, as there's nothing that says that is not allowed. |
| 767 | uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; |
| 768 | uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; |
| 769 | if (ConEnd > uintptr_t(Data->getBufferEnd())) |
| 770 | return object_error::parse_failed; |
| 771 | Res = ArrayRef<uint8_t>(reinterpret_cast<const unsigned char*>(ConStart), |
| 772 | Sec->SizeOfRawData); |
| 773 | return object_error::success; |
| 774 | } |
| 775 | |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 776 | const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 777 | return reinterpret_cast<const coff_relocation*>(Rel.p); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 778 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 779 | |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 780 | error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel, |
| 781 | RelocationRef &Res) const { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 782 | Rel.p = reinterpret_cast<uintptr_t>( |
| 783 | reinterpret_cast<const coff_relocation*>(Rel.p) + 1); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 784 | Res = RelocationRef(Rel, this); |
| 785 | return object_error::success; |
| 786 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 787 | |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 788 | error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, |
| 789 | uint64_t &Res) const { |
Rafael Espindola | 1e48387 | 2013-04-25 12:28:45 +0000 | [diff] [blame] | 790 | report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 791 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 792 | |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 793 | error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, |
| 794 | uint64_t &Res) const { |
| 795 | Res = toRel(Rel)->VirtualAddress; |
| 796 | return object_error::success; |
| 797 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 798 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 799 | symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 800 | const coff_relocation* R = toRel(Rel); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 801 | DataRefImpl Ref; |
| 802 | Ref.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); |
| 803 | return symbol_iterator(SymbolRef(Ref, this)); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 804 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 805 | |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 806 | error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, |
Owen Anderson | 7be7659 | 2011-10-26 17:08:49 +0000 | [diff] [blame] | 807 | uint64_t &Res) const { |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 808 | const coff_relocation* R = toRel(Rel); |
| 809 | Res = R->Type; |
| 810 | return object_error::success; |
| 811 | } |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 812 | |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 813 | const coff_section *COFFObjectFile::getCOFFSection(section_iterator &It) const { |
| 814 | return toSec(It->getRawDataRefImpl()); |
| 815 | } |
| 816 | |
| 817 | const coff_symbol *COFFObjectFile::getCOFFSymbol(symbol_iterator &It) const { |
| 818 | return toSymb(It->getRawDataRefImpl()); |
| 819 | } |
| 820 | |
Marshall Clow | d3e2a76 | 2012-06-18 19:47:16 +0000 | [diff] [blame] | 821 | const coff_relocation *COFFObjectFile::getCOFFRelocation( |
| 822 | relocation_iterator &It) const { |
| 823 | return toRel(It->getRawDataRefImpl()); |
| 824 | } |
| 825 | |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 826 | #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \ |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 827 | case COFF::enum: Res = #enum; break; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 828 | |
| 829 | error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 830 | SmallVectorImpl<char> &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 831 | const coff_relocation *Reloc = toRel(Rel); |
| 832 | StringRef Res; |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 833 | switch (COFFHeader->Machine) { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 834 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 835 | switch (Reloc->Type) { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 836 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); |
| 837 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); |
| 838 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); |
| 839 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); |
| 840 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); |
| 841 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); |
| 842 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); |
| 843 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); |
| 844 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); |
| 845 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); |
| 846 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); |
| 847 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); |
| 848 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); |
| 849 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); |
| 850 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); |
| 851 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); |
| 852 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); |
| 853 | default: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 854 | Res = "Unknown"; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 855 | } |
| 856 | break; |
| 857 | case COFF::IMAGE_FILE_MACHINE_I386: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 858 | switch (Reloc->Type) { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 859 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); |
| 860 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); |
| 861 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); |
| 862 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); |
| 863 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); |
| 864 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); |
| 865 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); |
| 866 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); |
| 867 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); |
| 868 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); |
| 869 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); |
| 870 | default: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 871 | Res = "Unknown"; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 872 | } |
| 873 | break; |
| 874 | default: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 875 | Res = "Unknown"; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 876 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 877 | Result.append(Res.begin(), Res.end()); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 878 | return object_error::success; |
| 879 | } |
| 880 | |
| 881 | #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME |
| 882 | |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 883 | error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel, |
| 884 | SmallVectorImpl<char> &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 885 | const coff_relocation *Reloc = toRel(Rel); |
| 886 | const coff_symbol *Symb = 0; |
| 887 | if (error_code EC = getSymbol(Reloc->SymbolTableIndex, Symb)) return EC; |
| 888 | DataRefImpl Sym; |
| 889 | Sym.p = reinterpret_cast<uintptr_t>(Symb); |
| 890 | StringRef SymName; |
| 891 | if (error_code EC = getSymbolName(Sym, SymName)) return EC; |
| 892 | Result.append(SymName.begin(), SymName.end()); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 893 | return object_error::success; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 894 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 895 | |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 896 | error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData, |
| 897 | LibraryRef &Result) const { |
| 898 | report_fatal_error("getLibraryNext not implemented in COFFObjectFile"); |
| 899 | } |
| 900 | |
| 901 | error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData, |
| 902 | StringRef &Result) const { |
| 903 | report_fatal_error("getLibraryPath not implemented in COFFObjectFile"); |
| 904 | } |
| 905 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 906 | bool ImportDirectoryEntryRef:: |
| 907 | operator==(const ImportDirectoryEntryRef &Other) const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 908 | return ImportTable == Other.ImportTable && Index == Other.Index; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | error_code |
| 912 | ImportDirectoryEntryRef::getNext(ImportDirectoryEntryRef &Result) const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 913 | Result = ImportDirectoryEntryRef(ImportTable, Index + 1, OwningObject); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 914 | return object_error::success; |
| 915 | } |
| 916 | |
| 917 | error_code ImportDirectoryEntryRef:: |
| 918 | getImportTableEntry(const import_directory_table_entry *&Result) const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 919 | Result = ImportTable; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 920 | return object_error::success; |
| 921 | } |
| 922 | |
| 923 | error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 924 | uintptr_t IntPtr = 0; |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 925 | if (error_code EC = OwningObject->getRvaPtr(ImportTable->NameRVA, IntPtr)) |
| 926 | return EC; |
| 927 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 928 | return object_error::success; |
| 929 | } |
| 930 | |
| 931 | error_code ImportDirectoryEntryRef::getImportLookupEntry( |
| 932 | const import_lookup_table_entry32 *&Result) const { |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 933 | uintptr_t IntPtr = 0; |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 934 | if (error_code EC = |
| 935 | OwningObject->getRvaPtr(ImportTable->ImportLookupTableRVA, IntPtr)) |
| 936 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 937 | Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); |
| 938 | return object_error::success; |
| 939 | } |
| 940 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 941 | bool ExportDirectoryEntryRef:: |
| 942 | operator==(const ExportDirectoryEntryRef &Other) const { |
| 943 | return ExportTable == Other.ExportTable && Index == Other.Index; |
| 944 | } |
| 945 | |
| 946 | error_code |
| 947 | ExportDirectoryEntryRef::getNext(ExportDirectoryEntryRef &Result) const { |
| 948 | Result = ExportDirectoryEntryRef(ExportTable, Index + 1, OwningObject); |
| 949 | return object_error::success; |
| 950 | } |
| 951 | |
| 952 | // Returns the export ordinal of the current export symbol. |
| 953 | error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { |
| 954 | Result = ExportTable->OrdinalBase + Index; |
| 955 | return object_error::success; |
| 956 | } |
| 957 | |
| 958 | // Returns the address of the current export symbol. |
| 959 | error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { |
| 960 | uintptr_t IntPtr = 0; |
| 961 | if (error_code EC = OwningObject->getRvaPtr( |
| 962 | ExportTable->ExportAddressTableRVA, IntPtr)) |
| 963 | return EC; |
| 964 | const export_address_table_entry *entry = reinterpret_cast<const export_address_table_entry *>(IntPtr); |
| 965 | Result = entry[Index].ExportRVA; |
| 966 | return object_error::success; |
| 967 | } |
| 968 | |
| 969 | // Returns the name of the current export symbol. If the symbol is exported only |
| 970 | // by ordinal, the empty string is set as a result. |
| 971 | error_code ExportDirectoryEntryRef::getName(StringRef &Result) const { |
| 972 | uintptr_t IntPtr = 0; |
| 973 | if (error_code EC = OwningObject->getRvaPtr( |
| 974 | ExportTable->OrdinalTableRVA, IntPtr)) |
| 975 | return EC; |
| 976 | const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); |
| 977 | |
| 978 | uint32_t NumEntries = ExportTable->NumberOfNamePointers; |
| 979 | int Offset = 0; |
| 980 | for (const ulittle16_t *I = Start, *E = Start + NumEntries; |
| 981 | I < E; ++I, ++Offset) { |
| 982 | if (*I != Index) |
| 983 | continue; |
| 984 | if (error_code EC = OwningObject->getRvaPtr( |
| 985 | ExportTable->NamePointerRVA, IntPtr)) |
| 986 | return EC; |
| 987 | const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); |
| 988 | if (error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) |
| 989 | return EC; |
| 990 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); |
| 991 | return object_error::success; |
| 992 | } |
| 993 | Result = ""; |
| 994 | return object_error::success; |
| 995 | } |
| 996 | |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 997 | namespace llvm { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 998 | ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 999 | error_code EC; |
| 1000 | return new COFFObjectFile(Object, EC); |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 1001 | } |
Rui Ueyama | 686738e | 2014-01-16 20:30:36 +0000 | [diff] [blame^] | 1002 | } |