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 | f078eff | 2014-03-18 23:37:53 +0000 | [diff] [blame] | 19 | #include "llvm/Support/COFF.h" |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 22 | #include <cctype> |
Nico Rieck | 9d2c15e | 2014-02-22 16:12:20 +0000 | [diff] [blame] | 23 | #include <limits> |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | using namespace object; |
| 27 | |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 28 | using support::ulittle16_t; |
| 29 | using support::ulittle32_t; |
Rui Ueyama | 861021f | 2014-10-02 22:05:29 +0000 | [diff] [blame^] | 30 | using support::ulittle64_t; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 31 | using support::little16_t; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 32 | |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 33 | // Returns false if size is greater than the buffer size. And sets ec. |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 34 | static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) { |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 35 | if (M.getBufferSize() < Size) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 36 | EC = object_error::unexpected_eof; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | return true; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 42 | // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m. |
| 43 | // Returns unexpected_eof if error. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 44 | template <typename T> |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 45 | static std::error_code getObject(const T *&Obj, MemoryBufferRef M, |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 46 | const uint8_t *Ptr, |
| 47 | const size_t Size = sizeof(T)) { |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 48 | uintptr_t Addr = uintptr_t(Ptr); |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 49 | if (Addr + Size < Addr || Addr + Size < Size || |
| 50 | Addr + Size > uintptr_t(M.getBufferEnd())) { |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 51 | return object_error::unexpected_eof; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 52 | } |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 53 | Obj = reinterpret_cast<const T *>(Addr); |
| 54 | return object_error::success; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 55 | } |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 56 | |
Nico Rieck | 9d2c15e | 2014-02-22 16:12:20 +0000 | [diff] [blame] | 57 | // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without |
| 58 | // prefixed slashes. |
| 59 | static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) { |
| 60 | assert(Str.size() <= 6 && "String too long, possible overflow."); |
| 61 | if (Str.size() > 6) |
| 62 | return true; |
| 63 | |
| 64 | uint64_t Value = 0; |
| 65 | while (!Str.empty()) { |
| 66 | unsigned CharVal; |
| 67 | if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25 |
| 68 | CharVal = Str[0] - 'A'; |
| 69 | else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51 |
| 70 | CharVal = Str[0] - 'a' + 26; |
| 71 | else if (Str[0] >= '0' && Str[0] <= '9') // 52..61 |
| 72 | CharVal = Str[0] - '0' + 52; |
| 73 | else if (Str[0] == '+') // 62 |
Rui Ueyama | 5500b07 | 2014-02-25 23:49:11 +0000 | [diff] [blame] | 74 | CharVal = 62; |
Nico Rieck | 9d2c15e | 2014-02-22 16:12:20 +0000 | [diff] [blame] | 75 | else if (Str[0] == '/') // 63 |
Rui Ueyama | 5500b07 | 2014-02-25 23:49:11 +0000 | [diff] [blame] | 76 | CharVal = 63; |
Nico Rieck | 9d2c15e | 2014-02-22 16:12:20 +0000 | [diff] [blame] | 77 | else |
| 78 | return true; |
| 79 | |
| 80 | Value = (Value * 64) + CharVal; |
| 81 | Str = Str.substr(1); |
| 82 | } |
| 83 | |
| 84 | if (Value > std::numeric_limits<uint32_t>::max()) |
| 85 | return true; |
| 86 | |
| 87 | Result = static_cast<uint32_t>(Value); |
| 88 | return false; |
| 89 | } |
| 90 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 91 | template <typename coff_symbol_type> |
| 92 | const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const { |
| 93 | const coff_symbol_type *Addr = |
| 94 | reinterpret_cast<const coff_symbol_type *>(Ref.p); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 95 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 96 | #ifndef NDEBUG |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 97 | // 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] | 98 | uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base()); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 99 | if (Offset < getPointerToSymbolTable() || |
| 100 | Offset >= getPointerToSymbolTable() + |
| 101 | (getNumberOfSymbols() * sizeof(coff_symbol_type))) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 102 | report_fatal_error("Symbol was outside of symbol table."); |
| 103 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 104 | assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 && |
| 105 | "Symbol did not point to the beginning of a symbol"); |
| 106 | #endif |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 107 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 108 | return Addr; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 111 | const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const { |
| 112 | const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 113 | |
| 114 | # ifndef NDEBUG |
| 115 | // Verify that the section points to a valid entry in the section table. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 116 | if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections())) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 117 | report_fatal_error("Section was outside of section table."); |
| 118 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 119 | uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable); |
| 120 | assert(Offset % sizeof(coff_section) == 0 && |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 121 | "Section did not point to the beginning of a section"); |
| 122 | # endif |
| 123 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 124 | return Addr; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 127 | void COFFObjectFile::moveSymbolNext(DataRefImpl &Ref) const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 128 | if (SymbolTable16) { |
| 129 | const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref); |
| 130 | Symb += 1 + Symb->NumberOfAuxSymbols; |
| 131 | Ref.p = reinterpret_cast<uintptr_t>(Symb); |
| 132 | } else if (SymbolTable32) { |
| 133 | const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref); |
| 134 | Symb += 1 + Symb->NumberOfAuxSymbols; |
| 135 | Ref.p = reinterpret_cast<uintptr_t>(Symb); |
| 136 | } else { |
| 137 | llvm_unreachable("no symbol table pointer!"); |
| 138 | } |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 141 | std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref, |
| 142 | StringRef &Result) const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 143 | COFFSymbolRef Symb = getCOFFSymbol(Ref); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 144 | return getSymbolName(Symb, Result); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 147 | std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref, |
| 148 | uint64_t &Result) const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 149 | COFFSymbolRef Symb = getCOFFSymbol(Ref); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 150 | const coff_section *Section = nullptr; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 151 | if (std::error_code EC = getSection(Symb.getSectionNumber(), Section)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 152 | return EC; |
Rafael Espindola | e62ab11 | 2013-11-02 18:07:48 +0000 | [diff] [blame] | 153 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 154 | if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 155 | Result = UnknownAddressOrSize; |
| 156 | else if (Section) |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 157 | Result = Section->VirtualAddress + Symb.getValue(); |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 158 | else |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 159 | Result = Symb.getValue(); |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 160 | return object_error::success; |
| 161 | } |
| 162 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 163 | std::error_code COFFObjectFile::getSymbolType(DataRefImpl Ref, |
| 164 | SymbolRef::Type &Result) const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 165 | COFFSymbolRef Symb = getCOFFSymbol(Ref); |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 166 | Result = SymbolRef::ST_Other; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 167 | |
| 168 | if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL && |
| 169 | Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) { |
David Meyer | 7e4b976 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 170 | Result = SymbolRef::ST_Unknown; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 171 | } else if (Symb.isFunctionDefinition()) { |
Rui Ueyama | 5efa665 | 2014-01-16 20:22:55 +0000 | [diff] [blame] | 172 | Result = SymbolRef::ST_Function; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 173 | } else { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 174 | uint32_t Characteristics = 0; |
| 175 | if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) { |
| 176 | const coff_section *Section = nullptr; |
| 177 | if (std::error_code EC = getSection(Symb.getSectionNumber(), Section)) |
| 178 | return EC; |
| 179 | Characteristics = Section->Characteristics; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 180 | } |
Rui Ueyama | 5efa665 | 2014-01-16 20:22:55 +0000 | [diff] [blame] | 181 | if (Characteristics & COFF::IMAGE_SCN_MEM_READ && |
| 182 | ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. |
| 183 | Result = SymbolRef::ST_Data; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 184 | } |
| 185 | return object_error::success; |
| 186 | } |
| 187 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 188 | uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 189 | COFFSymbolRef Symb = getCOFFSymbol(Ref); |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 190 | uint32_t Result = SymbolRef::SF_None; |
Benjamin Kramer | 75d1cf33 | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 191 | |
Rafael Espindola | 975e115 | 2014-02-04 22:50:47 +0000 | [diff] [blame] | 192 | // TODO: Correctly set SF_FormatSpecific, SF_Common |
David Meyer | 7e4b976 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 193 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 194 | if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) { |
| 195 | if (Symb.getValue() == 0) |
Rafael Espindola | 22fe9c1 | 2014-02-05 05:19:19 +0000 | [diff] [blame] | 196 | Result |= SymbolRef::SF_Undefined; |
| 197 | else |
| 198 | Result |= SymbolRef::SF_Common; |
| 199 | } |
| 200 | |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 201 | |
| 202 | // TODO: This are certainly too restrictive. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 203 | if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL) |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 204 | Result |= SymbolRef::SF_Global; |
| 205 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 206 | if (Symb.getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 207 | Result |= SymbolRef::SF_Weak; |
| 208 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 209 | if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE) |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 210 | Result |= SymbolRef::SF_Absolute; |
| 211 | |
Rafael Espindola | 20122a4 | 2014-01-31 20:57:12 +0000 | [diff] [blame] | 212 | return Result; |
Michael J. Spencer | 0175975 | 2011-10-17 23:54:22 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 215 | std::error_code COFFObjectFile::getSymbolSize(DataRefImpl Ref, |
| 216 | uint64_t &Result) const { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 217 | // FIXME: Return the correct size. This requires looking at all the symbols |
| 218 | // in the same section as this symbol, and looking for either the next |
| 219 | // symbol, or the end of the section. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 220 | COFFSymbolRef Symb = getCOFFSymbol(Ref); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 221 | const coff_section *Section = nullptr; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 222 | if (std::error_code EC = getSection(Symb.getSectionNumber(), Section)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 223 | return EC; |
Rafael Espindola | e62ab11 | 2013-11-02 18:07:48 +0000 | [diff] [blame] | 224 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 225 | if (Symb.getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 226 | Result = UnknownAddressOrSize; |
| 227 | else if (Section) |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 228 | Result = Section->SizeOfRawData - Symb.getValue(); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 229 | else |
| 230 | Result = 0; |
| 231 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 234 | std::error_code |
| 235 | COFFObjectFile::getSymbolSection(DataRefImpl Ref, |
| 236 | section_iterator &Result) const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 237 | COFFSymbolRef Symb = getCOFFSymbol(Ref); |
| 238 | if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) { |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 239 | Result = section_end(); |
Rui Ueyama | f078eff | 2014-03-18 23:37:53 +0000 | [diff] [blame] | 240 | } else { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 241 | const coff_section *Sec = nullptr; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 242 | if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec)) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 243 | return EC; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 244 | DataRefImpl Ref; |
| 245 | Ref.p = reinterpret_cast<uintptr_t>(Sec); |
| 246 | Result = section_iterator(SectionRef(Ref, this)); |
Michael J. Spencer | 321731539 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 247 | } |
| 248 | return object_error::success; |
| 249 | } |
| 250 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 251 | void COFFObjectFile::moveSectionNext(DataRefImpl &Ref) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 252 | const coff_section *Sec = toSec(Ref); |
| 253 | Sec += 1; |
| 254 | Ref.p = reinterpret_cast<uintptr_t>(Sec); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 257 | std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref, |
| 258 | StringRef &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 259 | const coff_section *Sec = toSec(Ref); |
| 260 | return getSectionName(Sec, Result); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 263 | std::error_code COFFObjectFile::getSectionAddress(DataRefImpl Ref, |
| 264 | uint64_t &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 265 | const coff_section *Sec = toSec(Ref); |
| 266 | Result = Sec->VirtualAddress; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 267 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 270 | std::error_code COFFObjectFile::getSectionSize(DataRefImpl Ref, |
| 271 | uint64_t &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 272 | const coff_section *Sec = toSec(Ref); |
| 273 | Result = Sec->SizeOfRawData; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 274 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 277 | std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref, |
| 278 | StringRef &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 279 | const coff_section *Sec = toSec(Ref); |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 280 | ArrayRef<uint8_t> Res; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 281 | std::error_code EC = getSectionContents(Sec, Res); |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 282 | Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size()); |
| 283 | return EC; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 286 | std::error_code COFFObjectFile::getSectionAlignment(DataRefImpl Ref, |
| 287 | uint64_t &Res) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 288 | const coff_section *Sec = toSec(Ref); |
| 289 | if (!Sec) |
Michael J. Spencer | 7989460 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 290 | return object_error::parse_failed; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 291 | Res = uint64_t(1) << (((Sec->Characteristics & 0x00F00000) >> 20) - 1); |
Michael J. Spencer | 7989460 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 292 | return object_error::success; |
| 293 | } |
| 294 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 295 | std::error_code COFFObjectFile::isSectionText(DataRefImpl Ref, |
| 296 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 297 | const coff_section *Sec = toSec(Ref); |
| 298 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 299 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 302 | std::error_code COFFObjectFile::isSectionData(DataRefImpl Ref, |
| 303 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 304 | const coff_section *Sec = toSec(Ref); |
| 305 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 306 | return object_error::success; |
| 307 | } |
| 308 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 309 | std::error_code COFFObjectFile::isSectionBSS(DataRefImpl Ref, |
| 310 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 311 | const coff_section *Sec = toSec(Ref); |
| 312 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
Michael J. Spencer | 800619f | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 313 | return object_error::success; |
| 314 | } |
| 315 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 316 | std::error_code |
| 317 | COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref, |
| 318 | bool &Result) const { |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 319 | // FIXME: Unimplemented |
| 320 | Result = true; |
| 321 | return object_error::success; |
| 322 | } |
| 323 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 324 | std::error_code COFFObjectFile::isSectionVirtual(DataRefImpl Ref, |
| 325 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 326 | const coff_section *Sec = toSec(Ref); |
| 327 | Result = Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 328 | return object_error::success; |
| 329 | } |
| 330 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 331 | std::error_code COFFObjectFile::isSectionZeroInit(DataRefImpl Ref, |
| 332 | bool &Result) const { |
Andrew Kaylor | b96a320 | 2012-10-10 01:45:52 +0000 | [diff] [blame] | 333 | // FIXME: Unimplemented. |
Preston Gurd | 2138ef6 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 334 | Result = false; |
| 335 | return object_error::success; |
| 336 | } |
| 337 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 338 | std::error_code COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref, |
| 339 | bool &Result) const { |
Andrew Kaylor | 3f31fa0 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 340 | // FIXME: Unimplemented. |
| 341 | Result = false; |
| 342 | return object_error::success; |
| 343 | } |
| 344 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 345 | std::error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef, |
| 346 | DataRefImpl SymbRef, |
| 347 | bool &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 348 | const coff_section *Sec = toSec(SecRef); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 349 | COFFSymbolRef Symb = getCOFFSymbol(SymbRef); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 350 | const coff_section *SymbSec = nullptr; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 351 | if (std::error_code EC = getSection(Symb.getSectionNumber(), SymbSec)) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 352 | return EC; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 353 | if (SymbSec == Sec) |
Michael J. Spencer | 9a28851 | 2011-10-13 20:36:54 +0000 | [diff] [blame] | 354 | Result = true; |
| 355 | else |
| 356 | Result = false; |
Benjamin Kramer | f6f3e81 | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 357 | return object_error::success; |
| 358 | } |
| 359 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 360 | relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { |
| 361 | const coff_section *Sec = toSec(Ref); |
| 362 | DataRefImpl Ret; |
Rui Ueyama | 827c8a2 | 2014-03-21 00:44:19 +0000 | [diff] [blame] | 363 | if (Sec->NumberOfRelocations == 0) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 364 | Ret.p = 0; |
Rui Ueyama | 827c8a2 | 2014-03-21 00:44:19 +0000 | [diff] [blame] | 365 | } else { |
| 366 | auto begin = reinterpret_cast<const coff_relocation*>( |
| 367 | base() + Sec->PointerToRelocations); |
| 368 | if (Sec->hasExtendedRelocations()) { |
| 369 | // Skip the first relocation entry repurposed to store the number of |
| 370 | // relocations. |
| 371 | begin++; |
| 372 | } |
| 373 | Ret.p = reinterpret_cast<uintptr_t>(begin); |
| 374 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 375 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Rui Ueyama | 827c8a2 | 2014-03-21 00:44:19 +0000 | [diff] [blame] | 378 | static uint32_t getNumberOfRelocations(const coff_section *Sec, |
| 379 | const uint8_t *base) { |
| 380 | // The field for the number of relocations in COFF section table is only |
| 381 | // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to |
| 382 | // NumberOfRelocations field, and the actual relocation count is stored in the |
| 383 | // VirtualAddress field in the first relocation entry. |
| 384 | if (Sec->hasExtendedRelocations()) { |
| 385 | auto *FirstReloc = reinterpret_cast<const coff_relocation*>( |
| 386 | base + Sec->PointerToRelocations); |
| 387 | return FirstReloc->VirtualAddress; |
| 388 | } |
| 389 | return Sec->NumberOfRelocations; |
| 390 | } |
| 391 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 392 | relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { |
| 393 | const coff_section *Sec = toSec(Ref); |
| 394 | DataRefImpl Ret; |
Rui Ueyama | 827c8a2 | 2014-03-21 00:44:19 +0000 | [diff] [blame] | 395 | if (Sec->NumberOfRelocations == 0) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 396 | Ret.p = 0; |
Rui Ueyama | 827c8a2 | 2014-03-21 00:44:19 +0000 | [diff] [blame] | 397 | } else { |
| 398 | auto begin = reinterpret_cast<const coff_relocation*>( |
| 399 | base() + Sec->PointerToRelocations); |
| 400 | uint32_t NumReloc = getNumberOfRelocations(Sec, base()); |
| 401 | Ret.p = reinterpret_cast<uintptr_t>(begin + NumReloc); |
| 402 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 403 | return relocation_iterator(RelocationRef(Ret, this)); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 406 | // Initialize the pointer to the symbol table. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 407 | std::error_code COFFObjectFile::initSymbolTablePtr() { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 408 | if (COFFHeader) |
| 409 | if (std::error_code EC = |
| 410 | getObject(SymbolTable16, Data, base() + getPointerToSymbolTable(), |
| 411 | getNumberOfSymbols() * getSymbolTableEntrySize())) |
| 412 | return EC; |
| 413 | |
| 414 | if (COFFBigObjHeader) |
| 415 | if (std::error_code EC = |
| 416 | getObject(SymbolTable32, Data, base() + getPointerToSymbolTable(), |
| 417 | getNumberOfSymbols() * getSymbolTableEntrySize())) |
| 418 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 419 | |
| 420 | // Find string table. The first four byte of the string table contains the |
| 421 | // total size of the string table, including the size field itself. If the |
| 422 | // string table is empty, the value of the first four byte would be 4. |
| 423 | const uint8_t *StringTableAddr = |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 424 | base() + getPointerToSymbolTable() + |
| 425 | getNumberOfSymbols() * getSymbolTableEntrySize(); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 426 | const ulittle32_t *StringTableSizePtr; |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 427 | if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 428 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 429 | StringTableSize = *StringTableSizePtr; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 430 | if (std::error_code EC = |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 431 | getObject(StringTable, Data, StringTableAddr, StringTableSize)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 432 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 433 | |
Nico Rieck | 773a579 | 2014-02-26 19:51:44 +0000 | [diff] [blame] | 434 | // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some |
| 435 | // tools like cvtres write a size of 0 for an empty table instead of 4. |
| 436 | if (StringTableSize < 4) |
| 437 | StringTableSize = 4; |
| 438 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 439 | // Check that the string table is null terminated if has any in it. |
Nico Rieck | 773a579 | 2014-02-26 19:51:44 +0000 | [diff] [blame] | 440 | if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0) |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 441 | return object_error::parse_failed; |
| 442 | return object_error::success; |
| 443 | } |
| 444 | |
Rui Ueyama | 215a586 | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 445 | // Returns the file offset for the given VA. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 446 | std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const { |
Rui Ueyama | b6eb264 | 2014-02-20 19:32:00 +0000 | [diff] [blame] | 447 | uint64_t ImageBase = PE32Header ? (uint64_t)PE32Header->ImageBase |
| 448 | : (uint64_t)PE32PlusHeader->ImageBase; |
Rui Ueyama | b7a4008 | 2014-02-20 19:14:56 +0000 | [diff] [blame] | 449 | uint64_t Rva = Addr - ImageBase; |
| 450 | assert(Rva <= UINT32_MAX); |
| 451 | return getRvaPtr((uint32_t)Rva, Res); |
Rui Ueyama | 215a586 | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 454 | // Returns the file offset for the given RVA. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 455 | std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const { |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 456 | for (const SectionRef &S : sections()) { |
| 457 | const coff_section *Section = getCOFFSection(S); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 458 | uint32_t SectionStart = Section->VirtualAddress; |
| 459 | uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize; |
Rui Ueyama | 215a586 | 2014-02-20 06:51:07 +0000 | [diff] [blame] | 460 | if (SectionStart <= Addr && Addr < SectionEnd) { |
| 461 | uint32_t Offset = Addr - SectionStart; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 462 | Res = uintptr_t(base()) + Section->PointerToRawData + Offset; |
| 463 | return object_error::success; |
| 464 | } |
| 465 | } |
| 466 | return object_error::parse_failed; |
| 467 | } |
| 468 | |
| 469 | // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name |
| 470 | // table entry. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 471 | std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint, |
| 472 | StringRef &Name) const { |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 473 | uintptr_t IntPtr = 0; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 474 | if (std::error_code EC = getRvaPtr(Rva, IntPtr)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 475 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 476 | const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr); |
| 477 | Hint = *reinterpret_cast<const ulittle16_t *>(Ptr); |
| 478 | Name = StringRef(reinterpret_cast<const char *>(Ptr + 2)); |
| 479 | return object_error::success; |
| 480 | } |
| 481 | |
| 482 | // Find the import table. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 483 | std::error_code COFFObjectFile::initImportTablePtr() { |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 484 | // First, we get the RVA of the import table. If the file lacks a pointer to |
| 485 | // the import table, do nothing. |
| 486 | const data_directory *DataEntry; |
| 487 | if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry)) |
| 488 | return object_error::success; |
| 489 | |
| 490 | // Do nothing if the pointer to import table is NULL. |
| 491 | if (DataEntry->RelativeVirtualAddress == 0) |
| 492 | return object_error::success; |
| 493 | |
| 494 | uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 495 | // -1 because the last entry is the null entry. |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 496 | NumberOfImportDirectory = DataEntry->Size / |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 497 | sizeof(import_directory_table_entry) - 1; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 498 | |
| 499 | // Find the section that contains the RVA. This is needed because the RVA is |
| 500 | // the import table's memory address which is different from its file offset. |
| 501 | uintptr_t IntPtr = 0; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 502 | if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 503 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 504 | ImportDirectory = reinterpret_cast< |
| 505 | const import_directory_table_entry *>(IntPtr); |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 506 | return object_error::success; |
| 507 | } |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 508 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 509 | // Find the export table. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 510 | std::error_code COFFObjectFile::initExportTablePtr() { |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 511 | // First, we get the RVA of the export table. If the file lacks a pointer to |
| 512 | // the export table, do nothing. |
| 513 | const data_directory *DataEntry; |
| 514 | if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry)) |
| 515 | return object_error::success; |
| 516 | |
| 517 | // Do nothing if the pointer to export table is NULL. |
| 518 | if (DataEntry->RelativeVirtualAddress == 0) |
| 519 | return object_error::success; |
| 520 | |
| 521 | uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress; |
| 522 | uintptr_t IntPtr = 0; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 523 | if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr)) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 524 | return EC; |
Rui Ueyama | 24fc2d6 | 2014-01-17 22:11:27 +0000 | [diff] [blame] | 525 | ExportDirectory = |
| 526 | reinterpret_cast<const export_directory_table_entry *>(IntPtr); |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 527 | return object_error::success; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 530 | COFFObjectFile::COFFObjectFile(MemoryBufferRef Object, std::error_code &EC) |
| 531 | : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr), |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 532 | COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr), |
| 533 | DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr), |
| 534 | SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0), |
| 535 | ImportDirectory(nullptr), NumberOfImportDirectory(0), |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 536 | ExportDirectory(nullptr) { |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 537 | // Check that we at least have enough room for a header. |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 538 | if (!checkSize(Data, EC, sizeof(coff_file_header))) |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 539 | return; |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 540 | |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 541 | // The current location in the file where we are looking at. |
| 542 | uint64_t CurPtr = 0; |
| 543 | |
| 544 | // PE header is optional and is present only in executables. If it exists, |
| 545 | // it is placed right after COFF header. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 546 | bool HasPEHeader = false; |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 547 | |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 548 | // Check if this is a PE/COFF file. |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 549 | if (base()[0] == 0x4d && base()[1] == 0x5a) { |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 550 | // PE/COFF, seek through MS-DOS compatibility stub and 4-byte |
| 551 | // PE signature to find 'normal' COFF header. |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 552 | if (!checkSize(Data, EC, 0x3c + 8)) |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 553 | return; |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 554 | CurPtr = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); |
| 555 | // Check the PE magic bytes. ("PE\0\0") |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 556 | if (std::memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != |
| 557 | 0) { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 558 | EC = object_error::parse_failed; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 559 | return; |
| 560 | } |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 561 | CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 562 | HasPEHeader = true; |
Eric Christopher | ee066fc | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 565 | if ((EC = getObject(COFFHeader, Data, base() + CurPtr))) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 566 | return; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 567 | |
| 568 | // It might be a bigobj file, let's check. Note that COFF bigobj and COFF |
| 569 | // import libraries share a common prefix but bigobj is more restrictive. |
| 570 | if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN && |
| 571 | COFFHeader->NumberOfSections == uint16_t(0xffff) && |
| 572 | checkSize(Data, EC, sizeof(coff_bigobj_file_header))) { |
| 573 | if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr))) |
| 574 | return; |
| 575 | |
| 576 | // Verify that we are dealing with bigobj. |
| 577 | if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion && |
| 578 | std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic, |
| 579 | sizeof(COFF::BigObjMagic)) == 0) { |
| 580 | COFFHeader = nullptr; |
| 581 | CurPtr += sizeof(coff_bigobj_file_header); |
| 582 | } else { |
| 583 | // It's not a bigobj. |
| 584 | COFFBigObjHeader = nullptr; |
| 585 | } |
| 586 | } |
| 587 | if (COFFHeader) { |
| 588 | // The prior checkSize call may have failed. This isn't a hard error |
| 589 | // because we were just trying to sniff out bigobj. |
| 590 | EC = object_error::success; |
| 591 | CurPtr += sizeof(coff_file_header); |
| 592 | |
| 593 | if (COFFHeader->isImportLibrary()) |
| 594 | return; |
| 595 | } |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 596 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 597 | if (HasPEHeader) { |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 598 | const pe32_header *Header; |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 599 | if ((EC = getObject(Header, Data, base() + CurPtr))) |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 600 | return; |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 601 | |
| 602 | const uint8_t *DataDirAddr; |
| 603 | uint64_t DataDirSize; |
| 604 | if (Header->Magic == 0x10b) { |
| 605 | PE32Header = Header; |
| 606 | DataDirAddr = base() + CurPtr + sizeof(pe32_header); |
| 607 | DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize; |
| 608 | } else if (Header->Magic == 0x20b) { |
| 609 | PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header); |
| 610 | DataDirAddr = base() + CurPtr + sizeof(pe32plus_header); |
| 611 | DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize; |
| 612 | } else { |
| 613 | // It's neither PE32 nor PE32+. |
| 614 | EC = object_error::parse_failed; |
| 615 | return; |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 616 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 617 | if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize))) |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 618 | return; |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 619 | CurPtr += COFFHeader->SizeOfOptionalHeader; |
| 620 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 621 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 622 | if ((EC = getObject(SectionTable, Data, base() + CurPtr, |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 623 | getNumberOfSections() * sizeof(coff_section)))) |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 624 | return; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 625 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 626 | // Initialize the pointer to the symbol table. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 627 | if (getPointerToSymbolTable() != 0) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 628 | if ((EC = initSymbolTablePtr())) |
Michael J. Spencer | d5930ca | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 629 | return; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 630 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 631 | // Initialize the pointer to the beginning of the import table. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 632 | if ((EC = initImportTablePtr())) |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 633 | return; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 634 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 635 | // Initialize the pointer to the export table. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 636 | if ((EC = initExportTablePtr())) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 637 | return; |
| 638 | |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 639 | EC = object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 642 | basic_symbol_iterator COFFObjectFile::symbol_begin_impl() const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 643 | DataRefImpl Ret; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 644 | Ret.p = getSymbolTable(); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 645 | return basic_symbol_iterator(SymbolRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 648 | basic_symbol_iterator COFFObjectFile::symbol_end_impl() const { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 649 | // The symbol table ends where the string table begins. |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 650 | DataRefImpl Ret; |
| 651 | Ret.p = reinterpret_cast<uintptr_t>(StringTable); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 652 | return basic_symbol_iterator(SymbolRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 655 | import_directory_iterator COFFObjectFile::import_directory_begin() const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 656 | return import_directory_iterator( |
| 657 | ImportDirectoryEntryRef(ImportDirectory, 0, this)); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Rui Ueyama | bc654b1 | 2013-09-27 21:47:05 +0000 | [diff] [blame] | 660 | import_directory_iterator COFFObjectFile::import_directory_end() const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 661 | return import_directory_iterator( |
| 662 | ImportDirectoryEntryRef(ImportDirectory, NumberOfImportDirectory, this)); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 663 | } |
David Meyer | c429b80 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 664 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 665 | export_directory_iterator COFFObjectFile::export_directory_begin() const { |
| 666 | return export_directory_iterator( |
| 667 | ExportDirectoryEntryRef(ExportDirectory, 0, this)); |
| 668 | } |
| 669 | |
| 670 | export_directory_iterator COFFObjectFile::export_directory_end() const { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 671 | if (!ExportDirectory) |
| 672 | return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this)); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 673 | ExportDirectoryEntryRef Ref(ExportDirectory, |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 674 | ExportDirectory->AddressTableEntries, this); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 675 | return export_directory_iterator(Ref); |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 678 | section_iterator COFFObjectFile::section_begin() const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 679 | DataRefImpl Ret; |
| 680 | Ret.p = reinterpret_cast<uintptr_t>(SectionTable); |
| 681 | return section_iterator(SectionRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 684 | section_iterator COFFObjectFile::section_end() const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 685 | DataRefImpl Ret; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 686 | int NumSections = |
| 687 | COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections(); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 688 | Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections); |
| 689 | return section_iterator(SectionRef(Ret, this)); |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | uint8_t COFFObjectFile::getBytesInAddress() const { |
Michael J. Spencer | 0324b67 | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 693 | return getArch() == Triple::x86_64 ? 8 : 4; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | StringRef COFFObjectFile::getFileFormatName() const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 697 | switch(getMachine()) { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 698 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 699 | return "COFF-i386"; |
| 700 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 701 | return "COFF-x86-64"; |
Saleem Abdulrasool | 9b7c0af | 2014-03-13 07:02:35 +0000 | [diff] [blame] | 702 | case COFF::IMAGE_FILE_MACHINE_ARMNT: |
| 703 | return "COFF-ARM"; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 704 | default: |
| 705 | return "COFF-<unknown arch>"; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | unsigned COFFObjectFile::getArch() const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 710 | switch (getMachine()) { |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 711 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 712 | return Triple::x86; |
| 713 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 714 | return Triple::x86_64; |
Saleem Abdulrasool | 9b7c0af | 2014-03-13 07:02:35 +0000 | [diff] [blame] | 715 | case COFF::IMAGE_FILE_MACHINE_ARMNT: |
| 716 | return Triple::thumb; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 717 | default: |
| 718 | return Triple::UnknownArch; |
| 719 | } |
| 720 | } |
| 721 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 722 | std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const { |
Rui Ueyama | 82ebd8e | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 723 | Res = PE32Header; |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 724 | return object_error::success; |
| 725 | } |
| 726 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 727 | std::error_code |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 728 | COFFObjectFile::getPE32PlusHeader(const pe32plus_header *&Res) const { |
| 729 | Res = PE32PlusHeader; |
| 730 | return object_error::success; |
| 731 | } |
| 732 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 733 | std::error_code |
| 734 | COFFObjectFile::getDataDirectory(uint32_t Index, |
| 735 | const data_directory *&Res) const { |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 736 | // Error if if there's no data directory or the index is out of range. |
Rui Ueyama | 10ed9dd | 2014-01-26 04:15:52 +0000 | [diff] [blame] | 737 | if (!DataDirectory) |
| 738 | return object_error::parse_failed; |
| 739 | assert(PE32Header || PE32PlusHeader); |
| 740 | uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize |
| 741 | : PE32PlusHeader->NumberOfRvaAndSize; |
| 742 | if (Index > NumEnt) |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 743 | return object_error::parse_failed; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 744 | Res = &DataDirectory[Index]; |
Rui Ueyama | ed64342b | 2013-07-19 23:23:29 +0000 | [diff] [blame] | 745 | return object_error::success; |
| 746 | } |
| 747 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 748 | std::error_code COFFObjectFile::getSection(int32_t Index, |
| 749 | const coff_section *&Result) const { |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 750 | // Check for special index values. |
Rui Ueyama | f078eff | 2014-03-18 23:37:53 +0000 | [diff] [blame] | 751 | if (COFF::isReservedSectionNumber(Index)) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 752 | Result = nullptr; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 753 | else if (Index > 0 && static_cast<uint32_t>(Index) <= getNumberOfSections()) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 754 | // 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] | 755 | Result = SectionTable + (Index - 1); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 756 | else |
| 757 | return object_error::parse_failed; |
| 758 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 761 | std::error_code COFFObjectFile::getString(uint32_t Offset, |
| 762 | StringRef &Result) const { |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 763 | if (StringTableSize <= 4) |
| 764 | // Tried to get a string from an empty string table. |
| 765 | return object_error::parse_failed; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 766 | if (Offset >= StringTableSize) |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 767 | return object_error::unexpected_eof; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 768 | Result = StringRef(StringTable + Offset); |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 769 | return object_error::success; |
Michael J. Spencer | 8e90ada | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 770 | } |
| 771 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 772 | std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol, |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 773 | StringRef &Res) const { |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 774 | // Check for string table entry. First 4 bytes are 0. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 775 | if (Symbol.getStringTableOffset().Zeroes == 0) { |
| 776 | uint32_t Offset = Symbol.getStringTableOffset().Offset; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 777 | if (std::error_code EC = getString(Offset, Res)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 778 | return EC; |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 779 | return object_error::success; |
| 780 | } |
| 781 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 782 | if (Symbol.getShortName()[COFF::NameSize - 1] == 0) |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 783 | // Null terminated, let ::strlen figure out the length. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 784 | Res = StringRef(Symbol.getShortName()); |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 785 | else |
| 786 | // Not null terminated, use all 8 bytes. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 787 | Res = StringRef(Symbol.getShortName(), COFF::NameSize); |
Michael J. Spencer | 89a7a5e | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 788 | return object_error::success; |
| 789 | } |
| 790 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 791 | ArrayRef<uint8_t> |
| 792 | COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 793 | const uint8_t *Aux = nullptr; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 794 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 795 | size_t SymbolSize = getSymbolTableEntrySize(); |
| 796 | if (Symbol.getNumberOfAuxSymbols() > 0) { |
| 797 | // AUX data comes immediately after the symbol in COFF |
| 798 | Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize; |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 799 | # ifndef NDEBUG |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 800 | // Verify that the Aux symbol points to a valid entry in the symbol table. |
| 801 | uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base()); |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 802 | if (Offset < getPointerToSymbolTable() || |
| 803 | Offset >= |
| 804 | getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize)) |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 805 | report_fatal_error("Aux Symbol data was outside of symbol table."); |
| 806 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 807 | assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 && |
| 808 | "Aux Symbol data did not point to the beginning of a symbol"); |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 809 | # endif |
Marshall Clow | bfb85e6 | 2012-06-15 01:15:47 +0000 | [diff] [blame] | 810 | } |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 811 | return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize); |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 814 | std::error_code COFFObjectFile::getSectionName(const coff_section *Sec, |
| 815 | StringRef &Res) const { |
Michael J. Spencer | 53c2d54 | 2012-03-19 20:27:15 +0000 | [diff] [blame] | 816 | StringRef Name; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 817 | if (Sec->Name[COFF::NameSize - 1] == 0) |
Michael J. Spencer | 53c2d54 | 2012-03-19 20:27:15 +0000 | [diff] [blame] | 818 | // Null terminated, let ::strlen figure out the length. |
| 819 | Name = Sec->Name; |
| 820 | else |
| 821 | // Not null terminated, use all 8 bytes. |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 822 | Name = StringRef(Sec->Name, COFF::NameSize); |
Michael J. Spencer | 53c2d54 | 2012-03-19 20:27:15 +0000 | [diff] [blame] | 823 | |
| 824 | // Check for string table entry. First byte is '/'. |
| 825 | if (Name[0] == '/') { |
| 826 | uint32_t Offset; |
Nico Rieck | 9d2c15e | 2014-02-22 16:12:20 +0000 | [diff] [blame] | 827 | if (Name[1] == '/') { |
| 828 | if (decodeBase64StringEntry(Name.substr(2), Offset)) |
| 829 | return object_error::parse_failed; |
| 830 | } else { |
| 831 | if (Name.substr(1).getAsInteger(10, Offset)) |
| 832 | return object_error::parse_failed; |
| 833 | } |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 834 | if (std::error_code EC = getString(Offset, Name)) |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 835 | return EC; |
Michael J. Spencer | 53c2d54 | 2012-03-19 20:27:15 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | Res = Name; |
| 839 | return object_error::success; |
| 840 | } |
| 841 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 842 | std::error_code |
| 843 | COFFObjectFile::getSectionContents(const coff_section *Sec, |
| 844 | ArrayRef<uint8_t> &Res) const { |
David Majnemer | dac3985 | 2014-09-26 22:32:16 +0000 | [diff] [blame] | 845 | // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't |
| 846 | // do anything interesting for them. |
| 847 | assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 && |
| 848 | "BSS sections don't have contents!"); |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 849 | // The only thing that we need to verify is that the contents is contained |
| 850 | // within the file bounds. We don't need to make sure it doesn't cover other |
| 851 | // data, as there's nothing that says that is not allowed. |
| 852 | uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData; |
| 853 | uintptr_t ConEnd = ConStart + Sec->SizeOfRawData; |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 854 | if (ConEnd > uintptr_t(Data.getBufferEnd())) |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 855 | return object_error::parse_failed; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 856 | Res = makeArrayRef(reinterpret_cast<const uint8_t*>(ConStart), |
| 857 | Sec->SizeOfRawData); |
Michael J. Spencer | 9da9e69 | 2012-03-19 20:27:37 +0000 | [diff] [blame] | 858 | return object_error::success; |
| 859 | } |
| 860 | |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 861 | const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 862 | return reinterpret_cast<const coff_relocation*>(Rel.p); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 863 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 864 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 865 | void COFFObjectFile::moveRelocationNext(DataRefImpl &Rel) const { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 866 | Rel.p = reinterpret_cast<uintptr_t>( |
| 867 | reinterpret_cast<const coff_relocation*>(Rel.p) + 1); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 868 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 869 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 870 | std::error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, |
| 871 | uint64_t &Res) const { |
Rafael Espindola | 1e48387 | 2013-04-25 12:28:45 +0000 | [diff] [blame] | 872 | report_fatal_error("getRelocationAddress not implemented in COFFObjectFile"); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 873 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 874 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 875 | std::error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, |
| 876 | uint64_t &Res) const { |
Danil Malyshev | cbe72fc | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 877 | Res = toRel(Rel)->VirtualAddress; |
| 878 | return object_error::success; |
| 879 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 880 | |
Rafael Espindola | 806f006 | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 881 | symbol_iterator COFFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 882 | const coff_relocation *R = toRel(Rel); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 883 | DataRefImpl Ref; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 884 | if (SymbolTable16) |
| 885 | Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex); |
| 886 | else if (SymbolTable32) |
| 887 | Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex); |
| 888 | else |
| 889 | llvm_unreachable("no symbol table pointer!"); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 890 | return symbol_iterator(SymbolRef(Ref, this)); |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 891 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 892 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 893 | std::error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, |
| 894 | uint64_t &Res) const { |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 895 | const coff_relocation* R = toRel(Rel); |
| 896 | Res = R->Type; |
| 897 | return object_error::success; |
| 898 | } |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 899 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 900 | const coff_section * |
| 901 | COFFObjectFile::getCOFFSection(const SectionRef &Section) const { |
| 902 | return toSec(Section.getRawDataRefImpl()); |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 903 | } |
| 904 | |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 905 | COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const { |
| 906 | if (SymbolTable16) |
| 907 | return toSymb<coff_symbol16>(Ref); |
| 908 | if (SymbolTable32) |
| 909 | return toSymb<coff_symbol32>(Ref); |
| 910 | llvm_unreachable("no symbol table pointer!"); |
| 911 | } |
| 912 | |
| 913 | COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const { |
| 914 | return getCOFFSymbol(Symbol.getRawDataRefImpl()); |
Marshall Clow | 71757ef | 2012-06-15 01:08:25 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 917 | const coff_relocation * |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 918 | COFFObjectFile::getCOFFRelocation(const RelocationRef &Reloc) const { |
| 919 | return toRel(Reloc.getRawDataRefImpl()); |
Marshall Clow | d3e2a76 | 2012-06-18 19:47:16 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Alexey Samsonov | 27dc839 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 922 | #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \ |
| 923 | case COFF::reloc_type: \ |
| 924 | Res = #reloc_type; \ |
| 925 | break; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 926 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 927 | std::error_code |
| 928 | COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 929 | SmallVectorImpl<char> &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 930 | const coff_relocation *Reloc = toRel(Rel); |
| 931 | StringRef Res; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 932 | switch (getMachine()) { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 933 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 934 | switch (Reloc->Type) { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 935 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); |
| 936 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); |
| 937 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); |
| 938 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); |
| 939 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); |
| 940 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); |
| 941 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); |
| 942 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); |
| 943 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); |
| 944 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); |
| 945 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); |
| 946 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); |
| 947 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); |
| 948 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); |
| 949 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); |
| 950 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); |
| 951 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); |
| 952 | default: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 953 | Res = "Unknown"; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 954 | } |
| 955 | break; |
Saleem Abdulrasool | 5c503bf | 2014-04-09 06:18:28 +0000 | [diff] [blame] | 956 | case COFF::IMAGE_FILE_MACHINE_ARMNT: |
| 957 | switch (Reloc->Type) { |
| 958 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ABSOLUTE); |
| 959 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32); |
| 960 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_ADDR32NB); |
| 961 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24); |
| 962 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH11); |
| 963 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_TOKEN); |
| 964 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX24); |
| 965 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX11); |
| 966 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECTION); |
| 967 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_SECREL); |
| 968 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32A); |
| 969 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_MOV32T); |
| 970 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH20T); |
| 971 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BRANCH24T); |
| 972 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_ARM_BLX23T); |
| 973 | default: |
| 974 | Res = "Unknown"; |
| 975 | } |
| 976 | break; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 977 | case COFF::IMAGE_FILE_MACHINE_I386: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 978 | switch (Reloc->Type) { |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 979 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); |
| 980 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); |
| 981 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); |
| 982 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); |
| 983 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); |
| 984 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); |
| 985 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); |
| 986 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); |
| 987 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); |
| 988 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); |
| 989 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); |
| 990 | default: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 991 | Res = "Unknown"; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 992 | } |
| 993 | break; |
| 994 | default: |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 995 | Res = "Unknown"; |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 996 | } |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 997 | Result.append(Res.begin(), Res.end()); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 998 | return object_error::success; |
| 999 | } |
| 1000 | |
| 1001 | #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME |
| 1002 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1003 | std::error_code |
| 1004 | COFFObjectFile::getRelocationValueString(DataRefImpl Rel, |
| 1005 | SmallVectorImpl<char> &Result) const { |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 1006 | const coff_relocation *Reloc = toRel(Rel); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 1007 | DataRefImpl Sym; |
David Majnemer | 44f51e5 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 1008 | ErrorOr<COFFSymbolRef> Symb = getSymbol(Reloc->SymbolTableIndex); |
| 1009 | if (std::error_code EC = Symb.getError()) |
| 1010 | return EC; |
| 1011 | Sym.p = reinterpret_cast<uintptr_t>(Symb->getRawPtr()); |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 1012 | StringRef SymName; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1013 | if (std::error_code EC = getSymbolName(Sym, SymName)) |
| 1014 | return EC; |
Rui Ueyama | 8ff24d2 | 2014-01-16 20:11:48 +0000 | [diff] [blame] | 1015 | Result.append(SymName.begin(), SymName.end()); |
Michael J. Spencer | e5fd004 | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1016 | return object_error::success; |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1017 | } |
Benjamin Kramer | 022ecdf | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1018 | |
Rafael Espindola | c66d761 | 2014-08-17 19:09:37 +0000 | [diff] [blame] | 1019 | bool COFFObjectFile::isRelocatableObject() const { |
| 1020 | return !DataDirectory; |
| 1021 | } |
| 1022 | |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1023 | bool ImportDirectoryEntryRef:: |
| 1024 | operator==(const ImportDirectoryEntryRef &Other) const { |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 1025 | return ImportTable == Other.ImportTable && Index == Other.Index; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1028 | void ImportDirectoryEntryRef::moveNext() { |
| 1029 | ++Index; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1032 | std::error_code ImportDirectoryEntryRef::getImportTableEntry( |
| 1033 | const import_directory_table_entry *&Result) const { |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1034 | Result = ImportTable + Index; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1035 | return object_error::success; |
| 1036 | } |
| 1037 | |
Rui Ueyama | 861021f | 2014-10-02 22:05:29 +0000 | [diff] [blame^] | 1038 | static imported_symbol_iterator |
| 1039 | makeImportedSymbolIterator(const COFFObjectFile *OwningObject, |
| 1040 | uintptr_t Ptr, int Index) { |
| 1041 | if (OwningObject->getBytesInAddress() == 4) { |
| 1042 | auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr); |
| 1043 | return imported_symbol_iterator(ImportedSymbolRef(P, Index, OwningObject)); |
| 1044 | } |
| 1045 | auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr); |
| 1046 | return imported_symbol_iterator(ImportedSymbolRef(P, Index, OwningObject)); |
| 1047 | } |
| 1048 | |
| 1049 | imported_symbol_iterator |
| 1050 | ImportDirectoryEntryRef::imported_symbol_begin() const { |
| 1051 | uintptr_t IntPtr = 0; |
| 1052 | OwningObject->getRvaPtr(ImportTable[Index].ImportLookupTableRVA, IntPtr); |
| 1053 | return makeImportedSymbolIterator(OwningObject, IntPtr, 0); |
| 1054 | } |
| 1055 | |
| 1056 | imported_symbol_iterator |
| 1057 | ImportDirectoryEntryRef::imported_symbol_end() const { |
| 1058 | uintptr_t IntPtr = 0; |
| 1059 | OwningObject->getRvaPtr(ImportTable[Index].ImportLookupTableRVA, IntPtr); |
| 1060 | // Forward the pointer to the last entry which is null. |
| 1061 | int Index = 0; |
| 1062 | if (OwningObject->getBytesInAddress() == 4) { |
| 1063 | auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr); |
| 1064 | while (*Entry++) |
| 1065 | ++Index; |
| 1066 | } else { |
| 1067 | auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr); |
| 1068 | while (*Entry++) |
| 1069 | ++Index; |
| 1070 | } |
| 1071 | return makeImportedSymbolIterator(OwningObject, IntPtr, Index); |
| 1072 | } |
| 1073 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1074 | std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const { |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1075 | uintptr_t IntPtr = 0; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1076 | if (std::error_code EC = |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1077 | OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr)) |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 1078 | return EC; |
| 1079 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1080 | return object_error::success; |
| 1081 | } |
| 1082 | |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1083 | std::error_code |
| 1084 | ImportDirectoryEntryRef::getImportLookupTableRVA(uint32_t &Result) const { |
| 1085 | Result = ImportTable[Index].ImportLookupTableRVA; |
| 1086 | return object_error::success; |
| 1087 | } |
| 1088 | |
| 1089 | std::error_code |
| 1090 | ImportDirectoryEntryRef::getImportAddressTableRVA(uint32_t &Result) const { |
| 1091 | Result = ImportTable[Index].ImportAddressTableRVA; |
| 1092 | return object_error::success; |
| 1093 | } |
| 1094 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1095 | std::error_code ImportDirectoryEntryRef::getImportLookupEntry( |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1096 | const import_lookup_table_entry32 *&Result) const { |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1097 | uintptr_t IntPtr = 0; |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 1098 | uint32_t RVA = ImportTable[Index].ImportLookupTableRVA; |
| 1099 | if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) |
Rui Ueyama | a045b73 | 2014-01-16 03:13:19 +0000 | [diff] [blame] | 1100 | return EC; |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1101 | Result = reinterpret_cast<const import_lookup_table_entry32 *>(IntPtr); |
| 1102 | return object_error::success; |
| 1103 | } |
| 1104 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1105 | bool ExportDirectoryEntryRef:: |
| 1106 | operator==(const ExportDirectoryEntryRef &Other) const { |
| 1107 | return ExportTable == Other.ExportTable && Index == Other.Index; |
| 1108 | } |
| 1109 | |
Rafael Espindola | 5e812af | 2014-01-30 02:49:50 +0000 | [diff] [blame] | 1110 | void ExportDirectoryEntryRef::moveNext() { |
| 1111 | ++Index; |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Rui Ueyama | da49d0d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 1114 | // Returns the name of the current export symbol. If the symbol is exported only |
| 1115 | // by ordinal, the empty string is set as a result. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1116 | std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const { |
Rui Ueyama | da49d0d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 1117 | uintptr_t IntPtr = 0; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1118 | if (std::error_code EC = |
| 1119 | OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr)) |
Rui Ueyama | da49d0d | 2014-01-16 20:50:34 +0000 | [diff] [blame] | 1120 | return EC; |
| 1121 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); |
| 1122 | return object_error::success; |
| 1123 | } |
| 1124 | |
Rui Ueyama | e5df609 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 1125 | // Returns the starting ordinal number. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1126 | std::error_code |
| 1127 | ExportDirectoryEntryRef::getOrdinalBase(uint32_t &Result) const { |
Rui Ueyama | e5df609 | 2014-01-17 22:02:24 +0000 | [diff] [blame] | 1128 | Result = ExportTable->OrdinalBase; |
| 1129 | return object_error::success; |
| 1130 | } |
| 1131 | |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1132 | // Returns the export ordinal of the current export symbol. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1133 | std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const { |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1134 | Result = ExportTable->OrdinalBase + Index; |
| 1135 | return object_error::success; |
| 1136 | } |
| 1137 | |
| 1138 | // Returns the address of the current export symbol. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1139 | std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const { |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1140 | uintptr_t IntPtr = 0; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1141 | if (std::error_code EC = |
| 1142 | OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr)) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1143 | return EC; |
Rui Ueyama | 24fc2d6 | 2014-01-17 22:11:27 +0000 | [diff] [blame] | 1144 | const export_address_table_entry *entry = |
| 1145 | reinterpret_cast<const export_address_table_entry *>(IntPtr); |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1146 | Result = entry[Index].ExportRVA; |
| 1147 | return object_error::success; |
| 1148 | } |
| 1149 | |
| 1150 | // Returns the name of the current export symbol. If the symbol is exported only |
| 1151 | // by ordinal, the empty string is set as a result. |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1152 | std::error_code |
| 1153 | ExportDirectoryEntryRef::getSymbolName(StringRef &Result) const { |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1154 | uintptr_t IntPtr = 0; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1155 | if (std::error_code EC = |
| 1156 | OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr)) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1157 | return EC; |
| 1158 | const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr); |
| 1159 | |
| 1160 | uint32_t NumEntries = ExportTable->NumberOfNamePointers; |
| 1161 | int Offset = 0; |
| 1162 | for (const ulittle16_t *I = Start, *E = Start + NumEntries; |
| 1163 | I < E; ++I, ++Offset) { |
| 1164 | if (*I != Index) |
| 1165 | continue; |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1166 | if (std::error_code EC = |
| 1167 | OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr)) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1168 | return EC; |
| 1169 | const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1170 | if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr)) |
Rui Ueyama | ad882ba | 2014-01-16 07:05:49 +0000 | [diff] [blame] | 1171 | return EC; |
| 1172 | Result = StringRef(reinterpret_cast<const char *>(IntPtr)); |
| 1173 | return object_error::success; |
| 1174 | } |
| 1175 | Result = ""; |
| 1176 | return object_error::success; |
| 1177 | } |
| 1178 | |
Rui Ueyama | 861021f | 2014-10-02 22:05:29 +0000 | [diff] [blame^] | 1179 | bool ImportedSymbolRef:: |
| 1180 | operator==(const ImportedSymbolRef &Other) const { |
| 1181 | return Entry32 == Other.Entry32 && Entry64 == Other.Entry64 |
| 1182 | && Index == Other.Index; |
| 1183 | } |
| 1184 | |
| 1185 | void ImportedSymbolRef::moveNext() { |
| 1186 | ++Index; |
| 1187 | } |
| 1188 | |
| 1189 | std::error_code |
| 1190 | ImportedSymbolRef::getSymbolName(StringRef &Result) const { |
| 1191 | uint32_t RVA; |
| 1192 | if (Entry32) { |
| 1193 | // If a symbol is imported only by ordinal, it has no name. |
| 1194 | if (Entry32[Index].isOrdinal()) |
| 1195 | return object_error::success; |
| 1196 | RVA = Entry32[Index].getHintNameRVA(); |
| 1197 | } else { |
| 1198 | if (Entry64[Index].isOrdinal()) |
| 1199 | return object_error::success; |
| 1200 | RVA = Entry64[Index].getHintNameRVA(); |
| 1201 | } |
| 1202 | uintptr_t IntPtr = 0; |
| 1203 | if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) |
| 1204 | return EC; |
| 1205 | // +2 because the first two bytes is hint. |
| 1206 | Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2)); |
| 1207 | return object_error::success; |
| 1208 | } |
| 1209 | |
| 1210 | std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const { |
| 1211 | uint32_t RVA; |
| 1212 | if (Entry32) { |
| 1213 | if (Entry32[Index].isOrdinal()) { |
| 1214 | Result = Entry32[Index].getOrdinal(); |
| 1215 | return object_error::success; |
| 1216 | } |
| 1217 | RVA = Entry32[Index].getHintNameRVA(); |
| 1218 | } else { |
| 1219 | if (Entry64[Index].isOrdinal()) { |
| 1220 | Result = Entry64[Index].getOrdinal(); |
| 1221 | return object_error::success; |
| 1222 | } |
| 1223 | RVA = Entry64[Index].getHintNameRVA(); |
| 1224 | } |
| 1225 | uintptr_t IntPtr = 0; |
| 1226 | if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr)) |
| 1227 | return EC; |
| 1228 | Result = *reinterpret_cast<const ulittle16_t *>(IntPtr); |
| 1229 | return object_error::success; |
| 1230 | } |
| 1231 | |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 1232 | ErrorOr<std::unique_ptr<COFFObjectFile>> |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1233 | ObjectFile::createCOFFObjectFile(MemoryBufferRef Object) { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 1234 | std::error_code EC; |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1235 | std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC)); |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 1236 | if (EC) |
| 1237 | return EC; |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 1238 | return std::move(Ret); |
Rui Ueyama | 686738e | 2014-01-16 20:30:36 +0000 | [diff] [blame] | 1239 | } |