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