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