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 | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | #include "llvm/ADT/Triple.h" |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace object; |
| 20 | |
| 21 | namespace { |
| 22 | using support::ulittle8_t; |
| 23 | using support::ulittle16_t; |
| 24 | using support::ulittle32_t; |
| 25 | using support::little16_t; |
| 26 | } |
| 27 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | // Returns false if size is greater than the buffer size. And sets ec. |
| 30 | bool checkSize(const MemoryBuffer *m, error_code &ec, uint64_t size) { |
| 31 | if (m->getBufferSize() < size) { |
| 32 | ec = object_error::unexpected_eof; |
| 33 | return false; |
| 34 | } |
| 35 | return true; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 38 | // Returns false if any bytes in [addr, addr + size) fall outsize of m. |
| 39 | bool checkAddr(const MemoryBuffer *m, |
| 40 | error_code &ec, |
| 41 | uintptr_t addr, |
| 42 | uint64_t size) { |
| 43 | if (addr + size < addr || |
| 44 | addr + size < size || |
| 45 | addr + size > uintptr_t(m->getBufferEnd())) { |
| 46 | ec = object_error::unexpected_eof; |
| 47 | return false; |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | const coff_symbol *COFFObjectFile::toSymb(DataRefImpl Symb) const { |
| 54 | const coff_symbol *addr = reinterpret_cast<const coff_symbol*>(Symb.p); |
| 55 | |
| 56 | # ifndef NDEBUG |
| 57 | // Verify that the symbol points to a valid entry in the symbol table. |
| 58 | uintptr_t offset = uintptr_t(addr) - uintptr_t(base()); |
| 59 | if (offset < Header->PointerToSymbolTable |
| 60 | || offset >= Header->PointerToSymbolTable |
| 61 | + (Header->NumberOfSymbols * sizeof(coff_symbol))) |
| 62 | report_fatal_error("Symbol was outside of symbol table."); |
| 63 | |
| 64 | assert((offset - Header->PointerToSymbolTable) % sizeof(coff_symbol) |
| 65 | == 0 && "Symbol did not point to the beginning of a symbol"); |
| 66 | # endif |
| 67 | |
| 68 | return addr; |
| 69 | } |
| 70 | |
| 71 | const coff_section *COFFObjectFile::toSec(DataRefImpl Sec) const { |
| 72 | const coff_section *addr = reinterpret_cast<const coff_section*>(Sec.p); |
| 73 | |
| 74 | # ifndef NDEBUG |
| 75 | // Verify that the section points to a valid entry in the section table. |
| 76 | if (addr < SectionTable |
| 77 | || addr >= (SectionTable + Header->NumberOfSections)) |
| 78 | report_fatal_error("Section was outside of section table."); |
| 79 | |
| 80 | uintptr_t offset = uintptr_t(addr) - uintptr_t(SectionTable); |
| 81 | assert(offset % sizeof(coff_section) == 0 && |
| 82 | "Section did not point to the beginning of a section"); |
| 83 | # endif |
| 84 | |
| 85 | return addr; |
| 86 | } |
| 87 | |
| 88 | error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb, |
| 89 | SymbolRef &Result) const { |
| 90 | const coff_symbol *symb = toSymb(Symb); |
| 91 | symb += 1 + symb->NumberOfAuxSymbols; |
| 92 | Symb.p = reinterpret_cast<uintptr_t>(symb); |
| 93 | Result = SymbolRef(Symb, this); |
| 94 | return object_error::success; |
| 95 | } |
| 96 | |
| 97 | error_code COFFObjectFile::getSymbolName(DataRefImpl Symb, |
| 98 | StringRef &Result) const { |
| 99 | const coff_symbol *symb = toSymb(Symb); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 100 | // Check for string table entry. First 4 bytes are 0. |
| 101 | if (symb->Name.Offset.Zeroes == 0) { |
| 102 | uint32_t Offset = symb->Name.Offset.Offset; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 103 | if (error_code ec = getString(Offset, Result)) |
| 104 | return ec; |
| 105 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (symb->Name.ShortName[7] == 0) |
| 109 | // Null terminated, let ::strlen figure out the length. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 110 | Result = StringRef(symb->Name.ShortName); |
| 111 | else |
| 112 | // Not null terminated, use all 8 bytes. |
| 113 | Result = StringRef(symb->Name.ShortName, 8); |
| 114 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 117 | error_code COFFObjectFile::getSymbolOffset(DataRefImpl Symb, |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 118 | uint64_t &Result) const { |
| 119 | const coff_symbol *symb = toSymb(Symb); |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 120 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 121 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 122 | return ec; |
| 123 | char Type; |
| 124 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 125 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 126 | if (Type == 'U' || Type == 'w') |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 127 | Result = UnknownAddressOrSize; |
| 128 | else if (Section) |
| 129 | Result = Section->VirtualAddress + symb->Value; |
| 130 | else |
| 131 | Result = symb->Value; |
| 132 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 135 | error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb, |
| 136 | uint64_t &Result) const { |
| 137 | const coff_symbol *symb = toSymb(Symb); |
| 138 | const coff_section *Section = NULL; |
| 139 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 140 | return ec; |
| 141 | char Type; |
| 142 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 143 | return ec; |
| 144 | if (Type == 'U' || Type == 'w') |
| 145 | Result = UnknownAddressOrSize; |
| 146 | else if (Section) |
| 147 | Result = reinterpret_cast<uintptr_t>(base() + |
| 148 | Section->PointerToRawData + |
| 149 | symb->Value); |
| 150 | else |
| 151 | Result = reinterpret_cast<uintptr_t>(base() + symb->Value); |
| 152 | return object_error::success; |
| 153 | } |
| 154 | |
| 155 | error_code COFFObjectFile::getSymbolType(DataRefImpl Symb, |
| 156 | SymbolRef::SymbolType &Result) const { |
| 157 | const coff_symbol *symb = toSymb(Symb); |
| 158 | Result = SymbolRef::ST_Other; |
| 159 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL && |
| 160 | symb->SectionNumber == COFF::IMAGE_SYM_UNDEFINED) { |
| 161 | Result = SymbolRef::ST_External; |
| 162 | } else { |
| 163 | if (symb->Type.ComplexType == COFF::IMAGE_SYM_DTYPE_FUNCTION) { |
| 164 | Result = SymbolRef::ST_Function; |
| 165 | } else { |
| 166 | char Type; |
| 167 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 168 | return ec; |
| 169 | if (Type == 'r' || Type == 'R') { |
| 170 | Result = SymbolRef::ST_Data; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return object_error::success; |
| 175 | } |
| 176 | |
| 177 | error_code COFFObjectFile::isSymbolGlobal(DataRefImpl Symb, |
| 178 | bool &Result) const { |
| 179 | const coff_symbol *symb = toSymb(Symb); |
| 180 | Result = (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL); |
| 181 | return object_error::success; |
| 182 | } |
| 183 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 184 | error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb, |
| 185 | uint64_t &Result) const { |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 186 | // FIXME: Return the correct size. This requires looking at all the symbols |
| 187 | // in the same section as this symbol, and looking for either the next |
| 188 | // symbol, or the end of the section. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 189 | const coff_symbol *symb = toSymb(Symb); |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 190 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 191 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 192 | return ec; |
| 193 | char Type; |
| 194 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 195 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 196 | if (Type == 'U' || Type == 'w') |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 197 | Result = UnknownAddressOrSize; |
| 198 | else if (Section) |
| 199 | Result = Section->SizeOfRawData - symb->Value; |
| 200 | else |
| 201 | Result = 0; |
| 202 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 205 | error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb, |
| 206 | char &Result) const { |
| 207 | const coff_symbol *symb = toSymb(Symb); |
| 208 | StringRef name; |
| 209 | if (error_code ec = getSymbolName(Symb, name)) |
| 210 | return ec; |
| 211 | char ret = StringSwitch<char>(name) |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 212 | .StartsWith(".debug", 'N') |
| 213 | .StartsWith(".sxdata", 'N') |
| 214 | .Default('?'); |
| 215 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 216 | if (ret != '?') { |
| 217 | Result = ret; |
| 218 | return object_error::success; |
| 219 | } |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 220 | |
| 221 | uint32_t Characteristics = 0; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 222 | if (symb->SectionNumber > 0) { |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 223 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 224 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 225 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 226 | Characteristics = Section->Characteristics; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | switch (symb->SectionNumber) { |
| 230 | case COFF::IMAGE_SYM_UNDEFINED: |
| 231 | // Check storage classes. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 232 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) { |
| 233 | Result = 'w'; |
| 234 | return object_error::success; // Don't do ::toupper. |
| 235 | } else |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 236 | ret = 'u'; |
| 237 | break; |
| 238 | case COFF::IMAGE_SYM_ABSOLUTE: |
| 239 | ret = 'a'; |
| 240 | break; |
| 241 | case COFF::IMAGE_SYM_DEBUG: |
| 242 | ret = 'n'; |
| 243 | break; |
| 244 | default: |
| 245 | // Check section type. |
| 246 | if (Characteristics & COFF::IMAGE_SCN_CNT_CODE) |
| 247 | ret = 't'; |
| 248 | else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ |
| 249 | && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. |
| 250 | ret = 'r'; |
| 251 | else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) |
| 252 | ret = 'd'; |
| 253 | else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) |
| 254 | ret = 'b'; |
| 255 | else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO) |
| 256 | ret = 'i'; |
| 257 | |
| 258 | // Check for section symbol. |
| 259 | else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC |
| 260 | && symb->Value == 0) |
| 261 | ret = 's'; |
| 262 | } |
| 263 | |
| 264 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) |
| 265 | ret = ::toupper(ret); |
| 266 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 267 | Result = ret; |
| 268 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 271 | error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb, |
| 272 | bool &Result) const { |
| 273 | Result = false; |
| 274 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 277 | error_code COFFObjectFile::getSectionNext(DataRefImpl Sec, |
| 278 | SectionRef &Result) const { |
| 279 | const coff_section *sec = toSec(Sec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 280 | sec += 1; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 281 | Sec.p = reinterpret_cast<uintptr_t>(sec); |
| 282 | Result = SectionRef(Sec, this); |
| 283 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 286 | error_code COFFObjectFile::getSectionName(DataRefImpl Sec, |
| 287 | StringRef &Result) const { |
| 288 | const coff_section *sec = toSec(Sec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 289 | StringRef name; |
| 290 | if (sec->Name[7] == 0) |
| 291 | // Null terminated, let ::strlen figure out the length. |
| 292 | name = sec->Name; |
| 293 | else |
| 294 | // Not null terminated, use all 8 bytes. |
| 295 | name = StringRef(sec->Name, 8); |
| 296 | |
| 297 | // Check for string table entry. First byte is '/'. |
| 298 | if (name[0] == '/') { |
| 299 | uint32_t Offset; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 300 | name.substr(1).getAsInteger(10, Offset); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 301 | if (error_code ec = getString(Offset, name)) |
| 302 | return ec; |
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 | Result = name; |
| 306 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 309 | error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec, |
| 310 | uint64_t &Result) const { |
| 311 | const coff_section *sec = toSec(Sec); |
| 312 | Result = sec->VirtualAddress; |
| 313 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 316 | error_code COFFObjectFile::getSectionSize(DataRefImpl Sec, |
| 317 | uint64_t &Result) const { |
| 318 | const coff_section *sec = toSec(Sec); |
| 319 | Result = sec->SizeOfRawData; |
| 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::getSectionContents(DataRefImpl Sec, |
| 324 | StringRef &Result) const { |
| 325 | const coff_section *sec = toSec(Sec); |
| 326 | // The only thing that we need to verify is that the contents is contained |
| 327 | // within the file bounds. We don't need to make sure it doesn't cover other |
| 328 | // data, as there's nothing that says that is not allowed. |
| 329 | uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData; |
| 330 | uintptr_t con_end = con_start + sec->SizeOfRawData; |
| 331 | if (con_end >= uintptr_t(Data->getBufferEnd())) |
| 332 | return object_error::parse_failed; |
| 333 | Result = StringRef(reinterpret_cast<const char*>(con_start), |
| 334 | sec->SizeOfRawData); |
| 335 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 359 | error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec, |
| 360 | DataRefImpl Symb, |
| 361 | bool &Result) const { |
| 362 | // FIXME: Unimplemented. |
| 363 | Result = false; |
| 364 | return object_error::success; |
| 365 | } |
| 366 | |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 367 | COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec) |
| 368 | : ObjectFile(Binary::isCOFF, Object, ec) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 369 | // Check that we at least have enough room for a header. |
| 370 | if (!checkSize(Data, ec, sizeof(coff_file_header))) return; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 371 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 372 | // The actual starting location of the COFF header in the file. This can be |
| 373 | // non-zero in PE/COFF files. |
| 374 | uint64_t HeaderStart = 0; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 375 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 376 | // Check if this is a PE/COFF file. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 377 | if (base()[0] == 0x4d && base()[1] == 0x5a) { |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 378 | // PE/COFF, seek through MS-DOS compatibility stub and 4-byte |
| 379 | // PE signature to find 'normal' COFF header. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 380 | if (!checkSize(Data, ec, 0x3c + 8)) return; |
| 381 | HeaderStart += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c); |
| 382 | // Check the PE header. ("PE\0\0") |
Benjamin Kramer | 3209a03 | 2011-07-05 20:28:00 +0000 | [diff] [blame] | 383 | if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 384 | ec = object_error::parse_failed; |
| 385 | return; |
| 386 | } |
| 387 | HeaderStart += 4; // Skip the PE Header. |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 390 | Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart); |
| 391 | if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header))) |
| 392 | return; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 393 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 394 | SectionTable = |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 395 | reinterpret_cast<const coff_section *>( base() |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 396 | + HeaderStart |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 397 | + sizeof(coff_file_header) |
| 398 | + Header->SizeOfOptionalHeader); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 399 | if (!checkAddr(Data, ec, uintptr_t(SectionTable), |
| 400 | Header->NumberOfSections * sizeof(coff_section))) |
| 401 | return; |
| 402 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 403 | SymbolTable = |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 404 | reinterpret_cast<const coff_symbol *>(base() |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 405 | + Header->PointerToSymbolTable); |
| 406 | if (!checkAddr(Data, ec, uintptr_t(SymbolTable), |
| 407 | Header->NumberOfSymbols * sizeof(coff_symbol))) |
| 408 | return; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 409 | |
| 410 | // Find string table. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 411 | StringTable = reinterpret_cast<const char *>(base()) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 412 | + Header->PointerToSymbolTable |
| 413 | + Header->NumberOfSymbols * sizeof(coff_symbol); |
| 414 | if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t))) |
| 415 | return; |
| 416 | |
| 417 | StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable); |
| 418 | if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize)) |
| 419 | return; |
| 420 | // Check that the string table is null terminated if has any in it. |
| 421 | if (StringTableSize < 4 |
| 422 | || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) { |
| 423 | ec = object_error::parse_failed; |
| 424 | return; |
| 425 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 426 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 427 | ec = object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 430 | ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 431 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 432 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 433 | ret.p = reinterpret_cast<intptr_t>(SymbolTable); |
| 434 | return symbol_iterator(SymbolRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 437 | ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const { |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 438 | // The symbol table ends where the string table begins. |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 439 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 440 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 441 | ret.p = reinterpret_cast<intptr_t>(StringTable); |
| 442 | return symbol_iterator(SymbolRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 445 | ObjectFile::section_iterator COFFObjectFile::begin_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 446 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 447 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 448 | ret.p = reinterpret_cast<intptr_t>(SectionTable); |
| 449 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 452 | ObjectFile::section_iterator COFFObjectFile::end_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 453 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 454 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 455 | ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections); |
| 456 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | uint8_t COFFObjectFile::getBytesInAddress() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 460 | return getArch() == Triple::x86_64 ? 8 : 4; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | StringRef COFFObjectFile::getFileFormatName() const { |
| 464 | switch(Header->Machine) { |
| 465 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 466 | return "COFF-i386"; |
| 467 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 468 | return "COFF-x86-64"; |
| 469 | default: |
| 470 | return "COFF-<unknown arch>"; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | unsigned COFFObjectFile::getArch() const { |
| 475 | switch(Header->Machine) { |
| 476 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 477 | return Triple::x86; |
| 478 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 479 | return Triple::x86_64; |
| 480 | default: |
| 481 | return Triple::UnknownArch; |
| 482 | } |
| 483 | } |
| 484 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 485 | error_code COFFObjectFile::getSection(int32_t index, |
| 486 | const coff_section *&Result) const { |
| 487 | // Check for special index values. |
| 488 | if (index == COFF::IMAGE_SYM_UNDEFINED || |
| 489 | index == COFF::IMAGE_SYM_ABSOLUTE || |
| 490 | index == COFF::IMAGE_SYM_DEBUG) |
| 491 | Result = NULL; |
| 492 | else if (index > 0 && index <= Header->NumberOfSections) |
| 493 | // We already verified the section table data, so no need to check again. |
| 494 | Result = SectionTable + (index - 1); |
| 495 | else |
| 496 | return object_error::parse_failed; |
| 497 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 500 | error_code COFFObjectFile::getString(uint32_t offset, |
| 501 | StringRef &Result) const { |
| 502 | if (StringTableSize <= 4) |
| 503 | // Tried to get a string from an empty string table. |
| 504 | return object_error::parse_failed; |
| 505 | if (offset >= StringTableSize) |
| 506 | return object_error::unexpected_eof; |
| 507 | Result = StringRef(StringTable + offset); |
| 508 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 511 | const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 512 | assert(Rel.d.b < Header->NumberOfSections && "Section index out of range!"); |
| 513 | const coff_section *Sect = NULL; |
| 514 | getSection(Rel.d.b, Sect); |
| 515 | assert(Rel.d.a < Sect->NumberOfRelocations && "Relocation index out of range!"); |
| 516 | return |
| 517 | reinterpret_cast<const coff_relocation*>(base() + |
| 518 | Sect->PointerToRelocations) + |
| 519 | Rel.d.a; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 520 | } |
| 521 | error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel, |
| 522 | RelocationRef &Res) const { |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 523 | const coff_section *Sect = NULL; |
| 524 | if (error_code ec = getSection(Rel.d.b, Sect)) |
| 525 | return ec; |
| 526 | if (++Rel.d.a >= Sect->NumberOfRelocations) { |
| 527 | Rel.d.a = 0; |
| 528 | while (++Rel.d.b < Header->NumberOfSections) { |
| 529 | const coff_section *Sect = NULL; |
| 530 | getSection(Rel.d.b, Sect); |
| 531 | if (Sect->NumberOfRelocations > 0) |
| 532 | break; |
| 533 | } |
| 534 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 535 | Res = RelocationRef(Rel, this); |
| 536 | return object_error::success; |
| 537 | } |
| 538 | error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, |
| 539 | uint64_t &Res) const { |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 540 | const coff_section *Sect = NULL; |
| 541 | if (error_code ec = getSection(Rel.d.b, Sect)) |
| 542 | return ec; |
| 543 | const coff_relocation* R = toRel(Rel); |
| 544 | Res = reinterpret_cast<uintptr_t>(base() + |
| 545 | Sect->PointerToRawData + |
| 546 | R->VirtualAddress); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 547 | return object_error::success; |
| 548 | } |
| 549 | error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel, |
| 550 | SymbolRef &Res) const { |
| 551 | const coff_relocation* R = toRel(Rel); |
| 552 | DataRefImpl Symb; |
| 553 | Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); |
| 554 | Res = SymbolRef(Symb, this); |
| 555 | return object_error::success; |
| 556 | } |
| 557 | error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, |
| 558 | uint32_t &Res) const { |
| 559 | const coff_relocation* R = toRel(Rel); |
| 560 | Res = R->Type; |
| 561 | return object_error::success; |
| 562 | } |
| 563 | error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 564 | int64_t &Res) const { |
| 565 | Res = 0; |
| 566 | return object_error::success; |
| 567 | } |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 568 | ObjectFile::relocation_iterator COFFObjectFile::begin_relocations() const { |
| 569 | DataRefImpl ret; |
| 570 | ret.d.a = 0; |
| 571 | ret.d.b = 1; |
| 572 | return relocation_iterator(RelocationRef(ret, this)); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 573 | } |
Bill Wendling | a48ad13 | 2011-10-07 18:25:37 +0000 | [diff] [blame] | 574 | ObjectFile::relocation_iterator COFFObjectFile::end_relocations() const { |
| 575 | DataRefImpl ret; |
| 576 | ret.d.a = 0; |
| 577 | ret.d.b = Header->NumberOfSections; |
| 578 | return relocation_iterator(RelocationRef(ret, this)); |
| 579 | } |
| 580 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 581 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 582 | namespace llvm { |
| 583 | |
| 584 | ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 585 | error_code ec; |
| 586 | return new COFFObjectFile(Object, ec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | } // end namespace llvm |