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