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