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