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