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