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 | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringSwitch.h" |
| 17 | #include "llvm/ADT/Triple.h" |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace object; |
| 21 | |
| 22 | namespace { |
| 23 | using support::ulittle8_t; |
| 24 | using support::ulittle16_t; |
| 25 | using support::ulittle32_t; |
| 26 | using support::little16_t; |
| 27 | } |
| 28 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | // Returns false if size is greater than the buffer size. And sets ec. |
| 31 | bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) { |
| 32 | if (m->getBufferSize() < size) { |
| 33 | ec = object_error::unexpected_eof; |
| 34 | return false; |
| 35 | } |
| 36 | return true; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 39 | // Returns false if any bytes in [addr, addr + size) fall outsize of m. |
| 40 | bool checkAddr(const MemoryBuffer *m, |
| 41 | error_code &ec, |
| 42 | uintptr_t addr, |
| 43 | uint64_t size) { |
| 44 | if (addr + size < addr || |
| 45 | addr + size < size || |
| 46 | addr + size > uintptr_t(m->getBufferEnd())) { |
| 47 | ec = object_error::unexpected_eof; |
| 48 | return false; |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const { |
| 55 | const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p); |
| 56 | |
| 57 | # ifndef NDEBUG |
| 58 | // Verify that the symbol points to a valid entry in the symbol table. |
| 59 | uintptr_t offset = uintptr_t(addr) - uintptr_t(base()); |
| 60 | if (offset < Header->PointerToSymbolTable |
| 61 | || offset >= Header->PointerToSymbolTable |
| 62 | + (Header->NumberOfSymbols * sizeof(coff_symbol))) |
| 63 | report_fatal_error("Symbol was outside of symbol table."); |
| 64 | |
| 65 | assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol) |
| 66 | == 0 && "Symbol did not point to the beginning of a symbol"); |
| 67 | # endif |
| 68 | |
| 69 | return addr; |
| 70 | } |
| 71 | |
| 72 | const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const { |
| 73 | const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p); |
| 74 | |
| 75 | # ifndef NDEBUG |
| 76 | // Verify that the section points to a valid entry in the section table. |
| 77 | if (addr < SectionTable |
| 78 | || addr >= (SectionTable + Header->NumberOfSections)) |
| 79 | report_fatal_error("Section was outside of section table."); |
| 80 | |
| 81 | uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable); |
| 82 | assert(offset % sizeof(coff_section) == 0 && |
| 83 | "Section did not point to the beginning of a section"); |
| 84 | # endif |
| 85 | |
| 86 | return addr; |
| 87 | } |
| 88 | |
| 89 | error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb, |
| 90 | SymbolRef &Result) const { |
| 91 | const coff_symbol *symb = toSymb(Symb); |
| 92 | symb += 1 + symb->NumberOfAuxSymbols; |
| 93 | Symb.p = reinterpret_cast<uintptr_t>(symb); |
| 94 | Result = SymbolRef(Symb, this); |
| 95 | return object_error::success; |
| 96 | } |
| 97 | |
| 98 | error_code COFFObjectFile::getSymbolName(DataRefImpl Symb, |
| 99 | StringRef &Result) const { |
| 100 | const coff_symbol *symb = toSymb(Symb); |
Michael J. Spencer | 0e752cb | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 101 | return getSymbolName(symb, Result); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 104 | error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb, |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 105 | uint64_t &Result) const { |
| 106 | const coff_symbol *symb = toSymb(Symb); |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 107 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 108 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 109 | return ec; |
| 110 | char Type; |
| 111 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 112 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 113 | if (Type == 'U' || Type == 'w') |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 114 | Result = UnknownAddressOrSize; |
| 115 | else if (Section) |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 116 | Result = Section->PointerToRawData + symb->Value; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 117 | else |
| 118 | Result = symb->Value; |
| 119 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 122 | error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb, |
| 123 | uint64_t &Result) const { |
| 124 | const coff_symbol *symb = toSymb(Symb); |
| 125 | const coff_section *Section = NULL; |
| 126 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 127 | return ec; |
| 128 | char Type; |
| 129 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 130 | return ec; |
| 131 | if (Type == 'U' || Type == 'w') |
| 132 | Result = UnknownAddressOrSize; |
| 133 | else if (Section) |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 134 | Result = Section->VirtualAddress + symb->Value; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 135 | else |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 136 | Result = symb->Value; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 137 | return object_error::success; |
| 138 | } |
| 139 | |
| 140 | error_code COFFObjectFile::getSymbolType(DataRefImpl Symb, |
Michael J. Spencer | 1130a79 | 2011-10-17 20:19:29 +0000 | [diff] [blame] | 141 | SymbolRef::Type &Result) const { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 142 | const coff_symbol *symb = toSymb(Symb); |
| 143 | Result = SymbolRef::ST_Other; |
| 144 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && |
| 145 | symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { |
David Meyer | 2c67727 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 146 | Result = SymbolRef::ST_Unknown; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 147 | } else { |
Michael J. Spencer | 5e3a082 | 2011-10-18 19:31:59 +0000 | [diff] [blame] | 148 | if (symb->getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 149 | Result = SymbolRef::ST_Function; |
| 150 | } else { |
| 151 | char Type; |
| 152 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 153 | return ec; |
| 154 | if (Type == 'r' || Type == 'R') { |
| 155 | Result = SymbolRef::ST_Data; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | return object_error::success; |
| 160 | } |
| 161 | |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 162 | error_code COFFObjectFile::getSymbolFlags(DataRefImpl Symb, |
| 163 | uint32_t &Result) const { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 164 | const coff_symbol *symb = toSymb(Symb); |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 165 | Result = SymbolRef::SF_None; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 166 | |
David Meyer | 2c67727 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 167 | // TODO: Correctly set SF_FormatSpecific, SF_ThreadLocal, SF_Common |
| 168 | |
| 169 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && |
| 170 | symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) |
| 171 | Result |= SymbolRef::SF_Undefined; |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 172 | |
| 173 | // TODO: This are certainly too restrictive. |
| 174 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) |
| 175 | Result |= SymbolRef::SF_Global; |
| 176 | |
| 177 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) |
| 178 | Result |= SymbolRef::SF_Weak; |
| 179 | |
| 180 | if (symb->SectionNumber == COFF::IMAGE_SYM_ABSOLUTE) |
| 181 | Result |= SymbolRef::SF_Absolute; |
| 182 | |
Michael J. Spencer | c38c36a | 2011-10-17 23:54:22 +0000 | [diff] [blame] | 183 | return object_error::success; |
| 184 | } |
| 185 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 186 | error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb, |
| 187 | uint64_t &Result) const { |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 188 | // FIXME: Return the correct size. This requires looking at all the symbols |
| 189 | // in the same section as this symbol, and looking for either the next |
| 190 | // symbol, or the end of the section. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 191 | const coff_symbol *symb = toSymb(Symb); |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 192 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 193 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 194 | return ec; |
| 195 | char Type; |
| 196 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 197 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 198 | if (Type == 'U' || Type == 'w') |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 199 | Result = UnknownAddressOrSize; |
| 200 | else if (Section) |
| 201 | Result = Section->SizeOfRawData - symb->Value; |
| 202 | else |
| 203 | Result = 0; |
| 204 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 207 | error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb, |
| 208 | char &Result) const { |
| 209 | const coff_symbol *symb = toSymb(Symb); |
| 210 | StringRef name; |
| 211 | if (error_code ec = getSymbolName(Symb, name)) |
| 212 | return ec; |
| 213 | char ret = StringSwitch<char>(name) |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 214 | .StartsWith(".debug", 'N') |
| 215 | .StartsWith(".sxdata", 'N') |
| 216 | .Default('?'); |
| 217 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 218 | if (ret != '?') { |
| 219 | Result = ret; |
| 220 | return object_error::success; |
| 221 | } |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 222 | |
| 223 | uint32_t Characteristics = 0; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 224 | if (symb->SectionNumber > 0) { |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 225 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 226 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 227 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 228 | Characteristics = Section->Characteristics; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | switch (symb->SectionNumber) { |
| 232 | case COFF::IMAGE_SYM_UNDEFINED: |
| 233 | // Check storage classes. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 234 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) { |
| 235 | Result = 'w'; |
| 236 | return object_error::success; // Don't do ::toupper. |
Michael J. Spencer | 11ba26d | 2011-11-16 23:36:12 +0000 | [diff] [blame] | 237 | } else if (symb->Value != 0) // Check for common symbols. |
| 238 | ret = 'c'; |
| 239 | else |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 240 | ret = 'u'; |
| 241 | break; |
| 242 | case COFF::IMAGE_SYM_ABSOLUTE: |
| 243 | ret = 'a'; |
| 244 | break; |
| 245 | case COFF::IMAGE_SYM_DEBUG: |
| 246 | ret = 'n'; |
| 247 | break; |
| 248 | default: |
| 249 | // Check section type. |
| 250 | if (Characteristics & COFF::IMAGE_SCN_CNT_CODE) |
| 251 | ret = 't'; |
| 252 | else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ |
| 253 | && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. |
| 254 | ret = 'r'; |
| 255 | else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) |
| 256 | ret = 'd'; |
| 257 | else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) |
| 258 | ret = 'b'; |
| 259 | else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO) |
| 260 | ret = 'i'; |
| 261 | |
| 262 | // Check for section symbol. |
| 263 | else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC |
| 264 | && symb->Value == 0) |
| 265 | ret = 's'; |
| 266 | } |
| 267 | |
| 268 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) |
| 269 | ret = ::toupper(ret); |
| 270 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 271 | Result = ret; |
| 272 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 275 | error_code COFFObjectFile::getSymbolSection(DataRefImpl Symb, |
| 276 | section_iterator &Result) const { |
| 277 | const coff_symbol *symb = toSymb(Symb); |
| 278 | if (symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED) |
| 279 | Result = end_sections(); |
| 280 | else { |
Daniel Dunbar | a483fc8 | 2011-11-28 22:19:32 +0000 | [diff] [blame] | 281 | const coff_section *sec = 0; |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 282 | if (error_code ec = getSection(symb->SectionNumber, sec)) return ec; |
| 283 | DataRefImpl Sec; |
Michael J. Spencer | 783d887 | 2011-11-02 19:33:26 +0000 | [diff] [blame] | 284 | std::memset(&Sec, 0, sizeof(Sec)); |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 285 | Sec.p = reinterpret_cast<uintptr_t>(sec); |
| 286 | Result = section_iterator(SectionRef(Sec, this)); |
| 287 | } |
| 288 | return object_error::success; |
| 289 | } |
| 290 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 291 | error_code COFFObjectFile::getSectionNext(DataRefImpl Sec, |
| 292 | SectionRef &Result) const { |
| 293 | const coff_section *sec = toSec(Sec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 294 | sec += 1; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 295 | Sec.p = reinterpret_cast<uintptr_t>(sec); |
| 296 | Result = SectionRef(Sec, this); |
| 297 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 300 | error_code COFFObjectFile::getSectionName(DataRefImpl Sec, |
| 301 | StringRef &Result) const { |
| 302 | const coff_section *sec = toSec(Sec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 303 | StringRef name; |
| 304 | if (sec->Name[7] == 0) |
| 305 | // Null terminated, let ::strlen figure out the length. |
| 306 | name = sec->Name; |
| 307 | else |
| 308 | // Not null terminated, use all 8 bytes. |
| 309 | name = StringRef(sec->Name, 8); |
| 310 | |
| 311 | // Check for string table entry. First byte is '/'. |
| 312 | if (name[0] == '/') { |
| 313 | uint32_t Offset; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 314 | name.substr(1).getAsInteger(10, Offset); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 315 | if (error_code ec = getString(Offset, name)) |
| 316 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 319 | Result = name; |
| 320 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 323 | error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec, |
| 324 | uint64_t &Result) const { |
| 325 | const coff_section *sec = toSec(Sec); |
| 326 | Result = sec->VirtualAddress; |
| 327 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 330 | error_code COFFObjectFile::getSectionSize(DataRefImpl Sec, |
| 331 | uint64_t &Result) const { |
| 332 | const coff_section *sec = toSec(Sec); |
| 333 | Result = sec->SizeOfRawData; |
| 334 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 337 | error_code COFFObjectFile::getSectionContents(DataRefImpl Sec, |
| 338 | StringRef &Result) const { |
| 339 | const coff_section *sec = toSec(Sec); |
| 340 | // The only thing that we need to verify is that the contents is contained |
| 341 | // within the file bounds. We don't need to make sure it doesn't cover other |
| 342 | // data, as there's nothing that says that is not allowed. |
| 343 | uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData; |
| 344 | uintptr_t con_end = con_start + sec->SizeOfRawData; |
Michael J. Spencer | 7151ddd | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 345 | if (con_end > uintptr_t(Data->getBufferEnd())) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 346 | return object_error::parse_failed; |
| 347 | Result = StringRef(reinterpret_cast<const char*>(con_start), |
| 348 | sec->SizeOfRawData); |
| 349 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 352 | error_code COFFObjectFile::getSectionAlignment(DataRefImpl Sec, |
| 353 | uint64_t &Res) const { |
| 354 | const coff_section *sec = toSec(Sec); |
| 355 | if (!sec) |
| 356 | return object_error::parse_failed; |
| 357 | Res = uint64_t(1) << (((sec->Characteristics & 0x00F00000) >> 20) - 1); |
| 358 | return object_error::success; |
| 359 | } |
| 360 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 361 | error_code COFFObjectFile::isSectionText(DataRefImpl Sec, |
| 362 | bool &Result) const { |
| 363 | const coff_section *sec = toSec(Sec); |
| 364 | Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; |
| 365 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 368 | error_code COFFObjectFile::isSectionData(DataRefImpl Sec, |
| 369 | bool &Result) const { |
| 370 | const coff_section *sec = toSec(Sec); |
| 371 | Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; |
| 372 | return object_error::success; |
| 373 | } |
| 374 | |
| 375 | error_code COFFObjectFile::isSectionBSS(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 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 382 | error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec, |
| 383 | DataRefImpl Symb, |
| 384 | bool &Result) const { |
Michael J. Spencer | bff6f86 | 2011-10-13 20:36:54 +0000 | [diff] [blame] | 385 | const coff_section *sec = toSec(Sec); |
| 386 | const coff_symbol *symb = toSymb(Symb); |
Daniel Dunbar | a483fc8 | 2011-11-28 22:19:32 +0000 | [diff] [blame] | 387 | const coff_section *symb_sec = 0; |
Michael J. Spencer | bff6f86 | 2011-10-13 20:36:54 +0000 | [diff] [blame] | 388 | if (error_code ec = getSection(symb->SectionNumber, symb_sec)) return ec; |
| 389 | if (symb_sec == sec) |
| 390 | Result = true; |
| 391 | else |
| 392 | Result = false; |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 393 | return object_error::success; |
| 394 | } |
| 395 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 396 | relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const { |
| 397 | const coff_section *sec = toSec(Sec); |
| 398 | DataRefImpl ret; |
| 399 | std::memset(&ret, 0, sizeof(ret)); |
| 400 | if (sec->NumberOfRelocations == 0) |
| 401 | ret.p = 0; |
| 402 | else |
| 403 | ret.p = reinterpret_cast<uintptr_t>(base() + sec->PointerToRelocations); |
| 404 | |
| 405 | return relocation_iterator(RelocationRef(ret, this)); |
| 406 | } |
| 407 | |
| 408 | relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const { |
| 409 | const coff_section *sec = toSec(Sec); |
| 410 | DataRefImpl ret; |
| 411 | std::memset(&ret, 0, sizeof(ret)); |
| 412 | if (sec->NumberOfRelocations == 0) |
| 413 | ret.p = 0; |
| 414 | else |
| 415 | ret.p = reinterpret_cast<uintptr_t>( |
| 416 | reinterpret_cast<const coff_relocation*>( |
| 417 | base() + sec->PointerToRelocations) |
| 418 | + sec->NumberOfRelocations); |
| 419 | |
| 420 | return relocation_iterator(RelocationRef(ret, this)); |
| 421 | } |
| 422 | |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 423 | COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec) |
Michael J. Spencer | 7151ddd | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 424 | : ObjectFile(Binary::isCOFF, Object, ec) |
| 425 | , Header(0) |
| 426 | , SectionTable(0) |
| 427 | , SymbolTable(0) |
| 428 | , StringTable(0) |
| 429 | , StringTableSize(0) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 430 | // Check that we at least have enough room for a header. |
| 431 | if (!checkSize(Data, ec, sizeof(coff_file_header))) return; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 432 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 433 | // The actual starting location of the COFF header in the file. This can be |
| 434 | // non-zero in PE/COFF files. |
| 435 | uint64_t HeaderStart = 0; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 436 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 437 | // Check if this is a PE/COFF file. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 438 | if (base()[0] == 0x4d && base()[1] == 0x5a) { |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 439 | // PE/COFF, seek through MS-DOS compatibility stub and 4-byte |
| 440 | // PE signature to find 'normal' COFF header. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 441 | if (!checkSize(Data, ec, 0x3c + 8)) return; |
Michael J. Spencer | 7151ddd | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 442 | HeaderStart = *reinterpret_cast<const ulittle16_t *>(base() + 0x3c); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 443 | // Check the PE header. ("PE\0\0") |
Benjamin Kramer | 3209a03 | 2011-07-05 20:28:00 +0000 | [diff] [blame] | 444 | if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 445 | ec = object_error::parse_failed; |
| 446 | return; |
| 447 | } |
| 448 | HeaderStart += 4; // Skip the PE Header. |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 451 | Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart); |
| 452 | if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header))) |
| 453 | return; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 454 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 455 | SectionTable = |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 456 | reinterpret_cast<const coff_section *>( base() |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 457 | + HeaderStart |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 458 | + sizeof(coff_file_header) |
| 459 | + Header->SizeOfOptionalHeader); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 460 | if (!checkAddr(Data, ec, uintptr_t(SectionTable), |
| 461 | Header->NumberOfSections * sizeof(coff_section))) |
| 462 | return; |
| 463 | |
Michael J. Spencer | 7151ddd | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 464 | if (Header->PointerToSymbolTable != 0) { |
| 465 | SymbolTable = |
| 466 | reinterpret_cast<const coff_symbol *>(base() |
| 467 | + Header->PointerToSymbolTable); |
| 468 | if (!checkAddr(Data, ec, uintptr_t(SymbolTable), |
| 469 | Header->NumberOfSymbols * sizeof(coff_symbol))) |
| 470 | return; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 471 | |
Michael J. Spencer | 7151ddd | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 472 | // Find string table. |
| 473 | StringTable = reinterpret_cast<const char *>(base()) |
| 474 | + Header->PointerToSymbolTable |
| 475 | + Header->NumberOfSymbols * sizeof(coff_symbol); |
| 476 | if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t))) |
| 477 | return; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 478 | |
Michael J. Spencer | 7151ddd | 2011-11-08 23:34:07 +0000 | [diff] [blame] | 479 | StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable); |
| 480 | if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize)) |
| 481 | return; |
| 482 | // Check that the string table is null terminated if has any in it. |
| 483 | if (StringTableSize < 4 |
| 484 | || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) { |
| 485 | ec = object_error::parse_failed; |
| 486 | return; |
| 487 | } |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 488 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 489 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 490 | ec = object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 493 | symbol_iterator COFFObjectFile::begin_symbols() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 494 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 495 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 496 | ret.p = reinterpret_cast<intptr_t>(SymbolTable); |
| 497 | return symbol_iterator(SymbolRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 500 | symbol_iterator COFFObjectFile::end_symbols() const { |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 501 | // The symbol table ends where the string table begins. |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 502 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 503 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 504 | ret.p = reinterpret_cast<intptr_t>(StringTable); |
| 505 | return symbol_iterator(SymbolRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Michael J. Spencer | dfa1896 | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 508 | symbol_iterator COFFObjectFile::begin_dynamic_symbols() const { |
| 509 | // TODO: implement |
| 510 | report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); |
| 511 | } |
| 512 | |
| 513 | symbol_iterator COFFObjectFile::end_dynamic_symbols() const { |
| 514 | // TODO: implement |
| 515 | report_fatal_error("Dynamic symbols unimplemented in COFFObjectFile"); |
| 516 | } |
| 517 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 518 | library_iterator COFFObjectFile::begin_libraries_needed() const { |
| 519 | // TODO: implement |
| 520 | report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); |
| 521 | } |
| 522 | |
| 523 | library_iterator COFFObjectFile::end_libraries_needed() const { |
| 524 | // TODO: implement |
| 525 | report_fatal_error("Libraries needed unimplemented in COFFObjectFile"); |
| 526 | } |
| 527 | |
David Meyer | 97f7787 | 2012-03-01 22:19:54 +0000 | [diff] [blame^] | 528 | StringRef COFFObjectFile::getLoadName() const { |
| 529 | // COFF does not have this field. |
| 530 | return ""; |
| 531 | } |
| 532 | |
| 533 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 534 | section_iterator COFFObjectFile::begin_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 535 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 536 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 537 | ret.p = reinterpret_cast<intptr_t>(SectionTable); |
| 538 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 541 | section_iterator COFFObjectFile::end_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 542 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 543 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 544 | ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections); |
| 545 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | uint8_t COFFObjectFile::getBytesInAddress() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 549 | return getArch() == Triple::x86_64 ? 8 : 4; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | StringRef COFFObjectFile::getFileFormatName() const { |
| 553 | switch(Header->Machine) { |
| 554 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 555 | return "COFF-i386"; |
| 556 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 557 | return "COFF-x86-64"; |
| 558 | default: |
| 559 | return "COFF-<unknown arch>"; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | unsigned COFFObjectFile::getArch() const { |
| 564 | switch(Header->Machine) { |
| 565 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 566 | return Triple::x86; |
| 567 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 568 | return Triple::x86_64; |
| 569 | default: |
| 570 | return Triple::UnknownArch; |
| 571 | } |
| 572 | } |
| 573 | |
Michael J. Spencer | 0e752cb | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 574 | error_code COFFObjectFile::getHeader(const coff_file_header *&Res) const { |
| 575 | Res = Header; |
| 576 | return object_error::success; |
| 577 | } |
| 578 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 579 | error_code COFFObjectFile::getSection(int32_t index, |
| 580 | const coff_section *&Result) const { |
| 581 | // Check for special index values. |
| 582 | if (index == COFF::IMAGE_SYM_UNDEFINED || |
| 583 | index == COFF::IMAGE_SYM_ABSOLUTE || |
| 584 | index == COFF::IMAGE_SYM_DEBUG) |
| 585 | Result = NULL; |
| 586 | else if (index > 0 && index <= Header->NumberOfSections) |
| 587 | // We already verified the section table data, so no need to check again. |
| 588 | Result = SectionTable + (index - 1); |
| 589 | else |
| 590 | return object_error::parse_failed; |
| 591 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 594 | error_code COFFObjectFile::getString(uint32_t offset, |
| 595 | StringRef &Result) const { |
| 596 | if (StringTableSize <= 4) |
| 597 | // Tried to get a string from an empty string table. |
| 598 | return object_error::parse_failed; |
| 599 | if (offset >= StringTableSize) |
| 600 | return object_error::unexpected_eof; |
| 601 | Result = StringRef(StringTable + offset); |
| 602 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 605 | error_code COFFObjectFile::getSymbol(uint32_t index, |
| 606 | const coff_symbol *&Result) const { |
Michael J. Spencer | 7c24665 | 2011-10-18 19:51:36 +0000 | [diff] [blame] | 607 | if (index < Header->NumberOfSymbols) |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 608 | Result = SymbolTable + index; |
| 609 | else |
| 610 | return object_error::parse_failed; |
| 611 | return object_error::success; |
| 612 | } |
| 613 | |
Michael J. Spencer | 0e752cb | 2011-10-17 23:53:56 +0000 | [diff] [blame] | 614 | error_code COFFObjectFile::getSymbolName(const coff_symbol *symbol, |
| 615 | StringRef &Res) const { |
| 616 | // Check for string table entry. First 4 bytes are 0. |
| 617 | if (symbol->Name.Offset.Zeroes == 0) { |
| 618 | uint32_t Offset = symbol->Name.Offset.Offset; |
| 619 | if (error_code ec = getString(Offset, Res)) |
| 620 | return ec; |
| 621 | return object_error::success; |
| 622 | } |
| 623 | |
| 624 | if (symbol->Name.ShortName[7] == 0) |
| 625 | // Null terminated, let ::strlen figure out the length. |
| 626 | Res = StringRef(symbol->Name.ShortName); |
| 627 | else |
| 628 | // Not null terminated, use all 8 bytes. |
| 629 | Res = StringRef(symbol->Name.ShortName, 8); |
| 630 | return object_error::success; |
| 631 | } |
| 632 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 633 | const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 634 | return reinterpret_cast<const coff_relocation*>(Rel.p); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 635 | } |
| 636 | error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel, |
| 637 | RelocationRef &Res) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 638 | Rel.p = reinterpret_cast<uintptr_t>( |
| 639 | reinterpret_cast<const coff_relocation*>(Rel.p) + 1); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 640 | Res = RelocationRef(Rel, this); |
| 641 | return object_error::success; |
| 642 | } |
| 643 | error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, |
| 644 | uint64_t &Res) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 645 | Res = toRel(Rel)->VirtualAddress; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 646 | return object_error::success; |
| 647 | } |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 648 | error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel, |
| 649 | uint64_t &Res) const { |
| 650 | Res = toRel(Rel)->VirtualAddress; |
| 651 | return object_error::success; |
| 652 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 653 | error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel, |
| 654 | SymbolRef &Res) const { |
| 655 | const coff_relocation* R = toRel(Rel); |
| 656 | DataRefImpl Symb; |
Michael J. Spencer | 783d887 | 2011-11-02 19:33:26 +0000 | [diff] [blame] | 657 | std::memset(&Symb, 0, sizeof(Symb)); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 658 | Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); |
| 659 | Res = SymbolRef(Symb, this); |
| 660 | return object_error::success; |
| 661 | } |
| 662 | error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, |
Owen Anderson | 9472b8d | 2011-10-26 17:08:49 +0000 | [diff] [blame] | 663 | uint64_t &Res) const { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 664 | const coff_relocation* R = toRel(Rel); |
| 665 | Res = R->Type; |
| 666 | return object_error::success; |
| 667 | } |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 668 | |
| 669 | #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \ |
| 670 | case COFF::enum: res = #enum; break; |
| 671 | |
| 672 | error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel, |
| 673 | SmallVectorImpl<char> &Result) const { |
| 674 | const coff_relocation *reloc = toRel(Rel); |
| 675 | StringRef res; |
| 676 | switch (Header->Machine) { |
| 677 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 678 | switch (reloc->Type) { |
| 679 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE); |
| 680 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64); |
| 681 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32); |
| 682 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB); |
| 683 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32); |
| 684 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1); |
| 685 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2); |
| 686 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3); |
| 687 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4); |
| 688 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5); |
| 689 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION); |
| 690 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL); |
| 691 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7); |
| 692 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN); |
| 693 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32); |
| 694 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR); |
| 695 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32); |
| 696 | default: |
| 697 | res = "Unknown"; |
| 698 | } |
| 699 | break; |
| 700 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 701 | switch (reloc->Type) { |
| 702 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE); |
| 703 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16); |
| 704 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16); |
| 705 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32); |
| 706 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB); |
| 707 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12); |
| 708 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION); |
| 709 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL); |
| 710 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN); |
| 711 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7); |
| 712 | LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32); |
| 713 | default: |
| 714 | res = "Unknown"; |
| 715 | } |
| 716 | break; |
| 717 | default: |
| 718 | res = "Unknown"; |
| 719 | } |
| 720 | Result.append(res.begin(), res.end()); |
| 721 | return object_error::success; |
| 722 | } |
| 723 | |
| 724 | #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME |
| 725 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 726 | error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 727 | int64_t &Res) const { |
| 728 | Res = 0; |
| 729 | return object_error::success; |
| 730 | } |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 731 | error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel, |
| 732 | SmallVectorImpl<char> &Result) const { |
| 733 | const coff_relocation *reloc = toRel(Rel); |
NAKAMURA Takumi | 48f248a | 2011-10-08 11:22:53 +0000 | [diff] [blame] | 734 | const coff_symbol *symb = 0; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 735 | if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec; |
| 736 | DataRefImpl sym; |
| 737 | ::memset(&sym, 0, sizeof(sym)); |
| 738 | sym.p = reinterpret_cast<uintptr_t>(symb); |
| 739 | StringRef symname; |
| 740 | if (error_code ec = getSymbolName(sym, symname)) return ec; |
| 741 | Result.append(symname.begin(), symname.end()); |
| 742 | return object_error::success; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 743 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 744 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 745 | error_code COFFObjectFile::getLibraryNext(DataRefImpl LibData, |
| 746 | LibraryRef &Result) const { |
| 747 | report_fatal_error("getLibraryNext not implemented in COFFObjectFile"); |
| 748 | } |
| 749 | |
| 750 | error_code COFFObjectFile::getLibraryPath(DataRefImpl LibData, |
| 751 | StringRef &Result) const { |
| 752 | report_fatal_error("getLibraryPath not implemented in COFFObjectFile"); |
| 753 | } |
| 754 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 755 | namespace llvm { |
| 756 | |
| 757 | ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 758 | error_code ec; |
| 759 | return new COFFObjectFile(Object, ec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | } // end namespace llvm |