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