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 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 117 | error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb, |
| 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 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 135 | error_code COFFObjectFile::getSymbolSize(DataRefImpl Symb, |
| 136 | uint64_t &Result) const { |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 137 | // FIXME: Return the correct size. This requires looking at all the symbols |
| 138 | // in the same section as this symbol, and looking for either the next |
| 139 | // symbol, or the end of the section. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 140 | const coff_symbol *symb = toSymb(Symb); |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 141 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 142 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 143 | return ec; |
| 144 | char Type; |
| 145 | if (error_code ec = getSymbolNMTypeChar(Symb, Type)) |
| 146 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 147 | if (Type == 'U' || Type == 'w') |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 148 | Result = UnknownAddressOrSize; |
| 149 | else if (Section) |
| 150 | Result = Section->SizeOfRawData - symb->Value; |
| 151 | else |
| 152 | Result = 0; |
| 153 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 156 | error_code COFFObjectFile::getSymbolNMTypeChar(DataRefImpl Symb, |
| 157 | char &Result) const { |
| 158 | const coff_symbol *symb = toSymb(Symb); |
| 159 | StringRef name; |
| 160 | if (error_code ec = getSymbolName(Symb, name)) |
| 161 | return ec; |
| 162 | char ret = StringSwitch<char>(name) |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 163 | .StartsWith(".debug", 'N') |
| 164 | .StartsWith(".sxdata", 'N') |
| 165 | .Default('?'); |
| 166 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 167 | if (ret != '?') { |
| 168 | Result = ret; |
| 169 | return object_error::success; |
| 170 | } |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 171 | |
| 172 | uint32_t Characteristics = 0; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 173 | if (symb->SectionNumber > 0) { |
Michael J. Spencer | 64388ce | 2011-07-05 14:48:59 +0000 | [diff] [blame] | 174 | const coff_section *Section = NULL; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 175 | if (error_code ec = getSection(symb->SectionNumber, Section)) |
| 176 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 177 | Characteristics = Section->Characteristics; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | switch (symb->SectionNumber) { |
| 181 | case COFF::IMAGE_SYM_UNDEFINED: |
| 182 | // Check storage classes. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 183 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL) { |
| 184 | Result = 'w'; |
| 185 | return object_error::success; // Don't do ::toupper. |
| 186 | } else |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 187 | ret = 'u'; |
| 188 | break; |
| 189 | case COFF::IMAGE_SYM_ABSOLUTE: |
| 190 | ret = 'a'; |
| 191 | break; |
| 192 | case COFF::IMAGE_SYM_DEBUG: |
| 193 | ret = 'n'; |
| 194 | break; |
| 195 | default: |
| 196 | // Check section type. |
| 197 | if (Characteristics & COFF::IMAGE_SCN_CNT_CODE) |
| 198 | ret = 't'; |
| 199 | else if ( Characteristics & COFF::IMAGE_SCN_MEM_READ |
| 200 | && ~Characteristics & COFF::IMAGE_SCN_MEM_WRITE) // Read only. |
| 201 | ret = 'r'; |
| 202 | else if (Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) |
| 203 | ret = 'd'; |
| 204 | else if (Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) |
| 205 | ret = 'b'; |
| 206 | else if (Characteristics & COFF::IMAGE_SCN_LNK_INFO) |
| 207 | ret = 'i'; |
| 208 | |
| 209 | // Check for section symbol. |
| 210 | else if ( symb->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC |
| 211 | && symb->Value == 0) |
| 212 | ret = 's'; |
| 213 | } |
| 214 | |
| 215 | if (symb->StorageClass == COFF::IMAGE_SYM_CLASS_EXTERNAL) |
| 216 | ret = ::toupper(ret); |
| 217 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 218 | Result = ret; |
| 219 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 222 | error_code COFFObjectFile::isSymbolInternal(DataRefImpl Symb, |
| 223 | bool &Result) const { |
| 224 | Result = false; |
| 225 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 228 | error_code COFFObjectFile::getSectionNext(DataRefImpl Sec, |
| 229 | SectionRef &Result) const { |
| 230 | const coff_section *sec = toSec(Sec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 231 | sec += 1; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 232 | Sec.p = reinterpret_cast<uintptr_t>(sec); |
| 233 | Result = SectionRef(Sec, this); |
| 234 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 237 | error_code COFFObjectFile::getSectionName(DataRefImpl Sec, |
| 238 | StringRef &Result) const { |
| 239 | const coff_section *sec = toSec(Sec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 240 | StringRef name; |
| 241 | if (sec->Name[7] == 0) |
| 242 | // Null terminated, let ::strlen figure out the length. |
| 243 | name = sec->Name; |
| 244 | else |
| 245 | // Not null terminated, use all 8 bytes. |
| 246 | name = StringRef(sec->Name, 8); |
| 247 | |
| 248 | // Check for string table entry. First byte is '/'. |
| 249 | if (name[0] == '/') { |
| 250 | uint32_t Offset; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 251 | name.substr(1).getAsInteger(10, Offset); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 252 | if (error_code ec = getString(Offset, name)) |
| 253 | return ec; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 256 | Result = name; |
| 257 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 260 | error_code COFFObjectFile::getSectionAddress(DataRefImpl Sec, |
| 261 | uint64_t &Result) const { |
| 262 | const coff_section *sec = toSec(Sec); |
| 263 | Result = sec->VirtualAddress; |
| 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::getSectionSize(DataRefImpl Sec, |
| 268 | uint64_t &Result) const { |
| 269 | const coff_section *sec = toSec(Sec); |
| 270 | Result = sec->SizeOfRawData; |
| 271 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 274 | error_code COFFObjectFile::getSectionContents(DataRefImpl Sec, |
| 275 | StringRef &Result) const { |
| 276 | const coff_section *sec = toSec(Sec); |
| 277 | // The only thing that we need to verify is that the contents is contained |
| 278 | // within the file bounds. We don't need to make sure it doesn't cover other |
| 279 | // data, as there's nothing that says that is not allowed. |
| 280 | uintptr_t con_start = uintptr_t(base()) + sec->PointerToRawData; |
| 281 | uintptr_t con_end = con_start + sec->SizeOfRawData; |
| 282 | if (con_end >= uintptr_t(Data->getBufferEnd())) |
| 283 | return object_error::parse_failed; |
| 284 | Result = StringRef(reinterpret_cast<const char*>(con_start), |
| 285 | sec->SizeOfRawData); |
| 286 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 289 | error_code COFFObjectFile::isSectionText(DataRefImpl Sec, |
| 290 | bool &Result) const { |
| 291 | const coff_section *sec = toSec(Sec); |
| 292 | Result = sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE; |
| 293 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 296 | error_code COFFObjectFile::sectionContainsSymbol(DataRefImpl Sec, |
| 297 | DataRefImpl Symb, |
| 298 | bool &Result) const { |
| 299 | // FIXME: Unimplemented. |
| 300 | Result = false; |
| 301 | return object_error::success; |
| 302 | } |
| 303 | |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 304 | COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec) |
| 305 | : ObjectFile(Binary::isCOFF, Object, ec) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 306 | // Check that we at least have enough room for a header. |
| 307 | if (!checkSize(Data, ec, sizeof(coff_file_header))) return; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 308 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 309 | // The actual starting location of the COFF header in the file. This can be |
| 310 | // non-zero in PE/COFF files. |
| 311 | uint64_t HeaderStart = 0; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 312 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 313 | // Check if this is a PE/COFF file. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 314 | if (base()[0] == 0x4d && base()[1] == 0x5a) { |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 315 | // PE/COFF, seek through MS-DOS compatibility stub and 4-byte |
| 316 | // PE signature to find 'normal' COFF header. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 317 | if (!checkSize(Data, ec, 0x3c + 8)) return; |
| 318 | HeaderStart += *reinterpret_cast<const ulittle32_t *>(base() + 0x3c); |
| 319 | // Check the PE header. ("PE\0\0") |
Benjamin Kramer | 3209a03 | 2011-07-05 20:28:00 +0000 | [diff] [blame] | 320 | if (std::memcmp(base() + HeaderStart, "PE\0\0", 4) != 0) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 321 | ec = object_error::parse_failed; |
| 322 | return; |
| 323 | } |
| 324 | HeaderStart += 4; // Skip the PE Header. |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 327 | Header = reinterpret_cast<const coff_file_header *>(base() + HeaderStart); |
| 328 | if (!checkAddr(Data, ec, uintptr_t(Header), sizeof(coff_file_header))) |
| 329 | return; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 330 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 331 | SectionTable = |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 332 | reinterpret_cast<const coff_section *>( base() |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 333 | + HeaderStart |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 334 | + sizeof(coff_file_header) |
| 335 | + Header->SizeOfOptionalHeader); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 336 | if (!checkAddr(Data, ec, uintptr_t(SectionTable), |
| 337 | Header->NumberOfSections * sizeof(coff_section))) |
| 338 | return; |
| 339 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 340 | SymbolTable = |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 341 | reinterpret_cast<const coff_symbol *>(base() |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 342 | + Header->PointerToSymbolTable); |
| 343 | if (!checkAddr(Data, ec, uintptr_t(SymbolTable), |
| 344 | Header->NumberOfSymbols * sizeof(coff_symbol))) |
| 345 | return; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 346 | |
| 347 | // Find string table. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 348 | StringTable = reinterpret_cast<const char *>(base()) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 349 | + Header->PointerToSymbolTable |
| 350 | + Header->NumberOfSymbols * sizeof(coff_symbol); |
| 351 | if (!checkAddr(Data, ec, uintptr_t(StringTable), sizeof(ulittle32_t))) |
| 352 | return; |
| 353 | |
| 354 | StringTableSize = *reinterpret_cast<const ulittle32_t *>(StringTable); |
| 355 | if (!checkAddr(Data, ec, uintptr_t(StringTable), StringTableSize)) |
| 356 | return; |
| 357 | // Check that the string table is null terminated if has any in it. |
| 358 | if (StringTableSize < 4 |
| 359 | || (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)) { |
| 360 | ec = object_error::parse_failed; |
| 361 | return; |
| 362 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 363 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 364 | ec = object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 368 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 369 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 370 | ret.p = reinterpret_cast<intptr_t>(SymbolTable); |
| 371 | return symbol_iterator(SymbolRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const { |
| 375 | // The symbol table ends where the string table begins. |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 376 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 377 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 378 | ret.p = reinterpret_cast<intptr_t>(StringTable); |
| 379 | return symbol_iterator(SymbolRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | ObjectFile::section_iterator COFFObjectFile::begin_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 383 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 384 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 385 | ret.p = reinterpret_cast<intptr_t>(SectionTable); |
| 386 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | ObjectFile::section_iterator COFFObjectFile::end_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 390 | DataRefImpl ret; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 391 | std::memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 392 | ret.p = reinterpret_cast<intptr_t>(SectionTable + Header->NumberOfSections); |
| 393 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | uint8_t COFFObjectFile::getBytesInAddress() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 397 | return getArch() == Triple::x86_64 ? 8 : 4; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | StringRef COFFObjectFile::getFileFormatName() const { |
| 401 | switch(Header->Machine) { |
| 402 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 403 | return "COFF-i386"; |
| 404 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 405 | return "COFF-x86-64"; |
| 406 | default: |
| 407 | return "COFF-<unknown arch>"; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | unsigned COFFObjectFile::getArch() const { |
| 412 | switch(Header->Machine) { |
| 413 | case COFF::IMAGE_FILE_MACHINE_I386: |
| 414 | return Triple::x86; |
| 415 | case COFF::IMAGE_FILE_MACHINE_AMD64: |
| 416 | return Triple::x86_64; |
| 417 | default: |
| 418 | return Triple::UnknownArch; |
| 419 | } |
| 420 | } |
| 421 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 422 | error_code COFFObjectFile::getSection(int32_t index, |
| 423 | const coff_section *&Result) const { |
| 424 | // Check for special index values. |
| 425 | if (index == COFF::IMAGE_SYM_UNDEFINED || |
| 426 | index == COFF::IMAGE_SYM_ABSOLUTE || |
| 427 | index == COFF::IMAGE_SYM_DEBUG) |
| 428 | Result = NULL; |
| 429 | else if (index > 0 && index <= Header->NumberOfSections) |
| 430 | // We already verified the section table data, so no need to check again. |
| 431 | Result = SectionTable + (index - 1); |
| 432 | else |
| 433 | return object_error::parse_failed; |
| 434 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 437 | error_code COFFObjectFile::getString(uint32_t offset, |
| 438 | StringRef &Result) const { |
| 439 | if (StringTableSize <= 4) |
| 440 | // Tried to get a string from an empty string table. |
| 441 | return object_error::parse_failed; |
| 442 | if (offset >= StringTableSize) |
| 443 | return object_error::unexpected_eof; |
| 444 | Result = StringRef(StringTable + offset); |
| 445 | return object_error::success; |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 448 | const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const { |
| 449 | assert(Rel.d.b < Header->NumberOfSections && "Section index out of range!"); |
Benjamin Kramer | cee58a6 | 2011-09-13 01:59:24 +0000 | [diff] [blame] | 450 | const coff_section *Sect = NULL; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 451 | getSection(Rel.d.b, Sect); |
| 452 | assert(Rel.d.a < Sect->NumberOfRelocations && "Relocation index out of range!"); |
| 453 | return |
| 454 | reinterpret_cast<const coff_relocation*>(base() + |
| 455 | Sect->PointerToRelocations) + |
| 456 | Rel.d.a; |
| 457 | } |
| 458 | error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel, |
| 459 | RelocationRef &Res) const { |
| 460 | const coff_section *Sect = NULL; |
| 461 | if (error_code ec = getSection(Rel.d.b, Sect)) |
| 462 | return ec; |
| 463 | if (++Rel.d.a >= Sect->NumberOfRelocations) { |
| 464 | Rel.d.a = 0; |
| 465 | while (++Rel.d.b < Header->NumberOfSections) { |
Benjamin Kramer | cee58a6 | 2011-09-13 01:59:24 +0000 | [diff] [blame] | 466 | const coff_section *Sect = NULL; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 467 | getSection(Rel.d.b, Sect); |
| 468 | if (Sect->NumberOfRelocations > 0) |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | Res = RelocationRef(Rel, this); |
| 473 | return object_error::success; |
| 474 | } |
| 475 | error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel, |
| 476 | uint64_t &Res) const { |
Benjamin Kramer | cee58a6 | 2011-09-13 01:59:24 +0000 | [diff] [blame] | 477 | const coff_section *Sect = NULL; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 478 | if (error_code ec = getSection(Rel.d.b, Sect)) |
| 479 | return ec; |
| 480 | const coff_relocation* R = toRel(Rel); |
| 481 | Res = reinterpret_cast<uintptr_t>(base() + |
| 482 | Sect->PointerToRawData + |
| 483 | R->VirtualAddress); |
| 484 | return object_error::success; |
| 485 | } |
| 486 | error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel, |
| 487 | SymbolRef &Res) const { |
| 488 | const coff_relocation* R = toRel(Rel); |
| 489 | DataRefImpl Symb; |
| 490 | Symb.p = reinterpret_cast<uintptr_t>(SymbolTable + R->SymbolTableIndex); |
| 491 | Res = SymbolRef(Symb, this); |
| 492 | return object_error::success; |
| 493 | } |
| 494 | error_code COFFObjectFile::getRelocationType(DataRefImpl Rel, |
| 495 | uint32_t &Res) const { |
| 496 | const coff_relocation* R = toRel(Rel); |
| 497 | Res = R->Type; |
| 498 | return object_error::success; |
| 499 | } |
| 500 | error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 501 | int64_t &Res) const { |
| 502 | Res = 0; |
| 503 | return object_error::success; |
| 504 | } |
| 505 | ObjectFile::relocation_iterator COFFObjectFile::begin_relocations() const { |
| 506 | DataRefImpl ret; |
| 507 | ret.d.a = 0; |
| 508 | ret.d.b = 1; |
| 509 | return relocation_iterator(RelocationRef(ret, this)); |
| 510 | } |
| 511 | ObjectFile::relocation_iterator COFFObjectFile::end_relocations() const { |
| 512 | DataRefImpl ret; |
| 513 | ret.d.a = 0; |
| 514 | ret.d.b = Header->NumberOfSections; |
| 515 | return relocation_iterator(RelocationRef(ret, this)); |
| 516 | } |
| 517 | |
| 518 | |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 519 | namespace llvm { |
| 520 | |
| 521 | ObjectFile *ObjectFile::createCOFFObjectFile(MemoryBuffer *Object) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 522 | error_code ec; |
| 523 | return new COFFObjectFile(Object, ec); |
Michael J. Spencer | a1ef8ef | 2011-01-20 06:38:34 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | } // end namespace llvm |