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