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