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