Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1 | //===- ELFObjectFile.cpp - ELF 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 defines the ELFObjectFile class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | #include "llvm/ADT/StringSwitch.h" |
| 16 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ObjectFile.h" |
| 19 | #include "llvm/Support/ELF.h" |
| 20 | #include "llvm/Support/Endian.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include <algorithm> |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 25 | #include <limits> |
| 26 | #include <utility> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace object; |
| 30 | |
| 31 | // Templates to choose Elf_Addr and Elf_Off depending on is64Bits. |
| 32 | namespace { |
| 33 | template<support::endianness target_endianness> |
| 34 | struct ELFDataTypeTypedefHelperCommon { |
| 35 | typedef support::detail::packed_endian_specific_integral |
| 36 | <uint16_t, target_endianness, support::aligned> Elf_Half; |
| 37 | typedef support::detail::packed_endian_specific_integral |
| 38 | <uint32_t, target_endianness, support::aligned> Elf_Word; |
| 39 | typedef support::detail::packed_endian_specific_integral |
| 40 | <int32_t, target_endianness, support::aligned> Elf_Sword; |
| 41 | typedef support::detail::packed_endian_specific_integral |
| 42 | <uint64_t, target_endianness, support::aligned> Elf_Xword; |
| 43 | typedef support::detail::packed_endian_specific_integral |
| 44 | <int64_t, target_endianness, support::aligned> Elf_Sxword; |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | namespace { |
| 49 | template<support::endianness target_endianness, bool is64Bits> |
| 50 | struct ELFDataTypeTypedefHelper; |
| 51 | |
| 52 | /// ELF 32bit types. |
| 53 | template<support::endianness target_endianness> |
| 54 | struct ELFDataTypeTypedefHelper<target_endianness, false> |
| 55 | : ELFDataTypeTypedefHelperCommon<target_endianness> { |
| 56 | typedef support::detail::packed_endian_specific_integral |
| 57 | <uint32_t, target_endianness, support::aligned> Elf_Addr; |
| 58 | typedef support::detail::packed_endian_specific_integral |
| 59 | <uint32_t, target_endianness, support::aligned> Elf_Off; |
| 60 | }; |
| 61 | |
| 62 | /// ELF 64bit types. |
| 63 | template<support::endianness target_endianness> |
| 64 | struct ELFDataTypeTypedefHelper<target_endianness, true> |
| 65 | : ELFDataTypeTypedefHelperCommon<target_endianness>{ |
| 66 | typedef support::detail::packed_endian_specific_integral |
| 67 | <uint64_t, target_endianness, support::aligned> Elf_Addr; |
| 68 | typedef support::detail::packed_endian_specific_integral |
| 69 | <uint64_t, target_endianness, support::aligned> Elf_Off; |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | // I really don't like doing this, but the alternative is copypasta. |
| 74 | #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \ |
| 75 | typedef typename \ |
| 76 | ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \ |
| 77 | typedef typename \ |
| 78 | ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \ |
| 79 | typedef typename \ |
| 80 | ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \ |
| 81 | typedef typename \ |
| 82 | ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \ |
| 83 | typedef typename \ |
| 84 | ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \ |
| 85 | typedef typename \ |
| 86 | ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \ |
| 87 | typedef typename \ |
| 88 | ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword; |
| 89 | |
| 90 | // Section header. |
| 91 | namespace { |
| 92 | template<support::endianness target_endianness, bool is64Bits> |
| 93 | struct Elf_Shdr_Base; |
| 94 | |
| 95 | template<support::endianness target_endianness> |
| 96 | struct Elf_Shdr_Base<target_endianness, false> { |
| 97 | LLVM_ELF_IMPORT_TYPES(target_endianness, false) |
| 98 | Elf_Word sh_name; // Section name (index into string table) |
| 99 | Elf_Word sh_type; // Section type (SHT_*) |
| 100 | Elf_Word sh_flags; // Section flags (SHF_*) |
| 101 | Elf_Addr sh_addr; // Address where section is to be loaded |
| 102 | Elf_Off sh_offset; // File offset of section data, in bytes |
| 103 | Elf_Word sh_size; // Size of section, in bytes |
| 104 | Elf_Word sh_link; // Section type-specific header table index link |
| 105 | Elf_Word sh_info; // Section type-specific extra information |
| 106 | Elf_Word sh_addralign;// Section address alignment |
| 107 | Elf_Word sh_entsize; // Size of records contained within the section |
| 108 | }; |
| 109 | |
| 110 | template<support::endianness target_endianness> |
| 111 | struct Elf_Shdr_Base<target_endianness, true> { |
| 112 | LLVM_ELF_IMPORT_TYPES(target_endianness, true) |
| 113 | Elf_Word sh_name; // Section name (index into string table) |
| 114 | Elf_Word sh_type; // Section type (SHT_*) |
| 115 | Elf_Xword sh_flags; // Section flags (SHF_*) |
| 116 | Elf_Addr sh_addr; // Address where section is to be loaded |
| 117 | Elf_Off sh_offset; // File offset of section data, in bytes |
| 118 | Elf_Xword sh_size; // Size of section, in bytes |
| 119 | Elf_Word sh_link; // Section type-specific header table index link |
| 120 | Elf_Word sh_info; // Section type-specific extra information |
| 121 | Elf_Xword sh_addralign;// Section address alignment |
| 122 | Elf_Xword sh_entsize; // Size of records contained within the section |
| 123 | }; |
| 124 | |
| 125 | template<support::endianness target_endianness, bool is64Bits> |
| 126 | struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> { |
| 127 | using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize; |
| 128 | using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size; |
| 129 | |
| 130 | /// @brief Get the number of entities this section contains if it has any. |
| 131 | unsigned getEntityCount() const { |
| 132 | if (sh_entsize == 0) |
| 133 | return 0; |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 134 | return sh_size / sh_entsize; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 135 | } |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | namespace { |
| 140 | template<support::endianness target_endianness, bool is64Bits> |
| 141 | struct Elf_Sym_Base; |
| 142 | |
| 143 | template<support::endianness target_endianness> |
| 144 | struct Elf_Sym_Base<target_endianness, false> { |
| 145 | LLVM_ELF_IMPORT_TYPES(target_endianness, false) |
| 146 | Elf_Word st_name; // Symbol name (index into string table) |
| 147 | Elf_Addr st_value; // Value or address associated with the symbol |
| 148 | Elf_Word st_size; // Size of the symbol |
| 149 | unsigned char st_info; // Symbol's type and binding attributes |
| 150 | unsigned char st_other; // Must be zero; reserved |
| 151 | Elf_Half st_shndx; // Which section (header table index) it's defined in |
| 152 | }; |
| 153 | |
| 154 | template<support::endianness target_endianness> |
| 155 | struct Elf_Sym_Base<target_endianness, true> { |
| 156 | LLVM_ELF_IMPORT_TYPES(target_endianness, true) |
| 157 | Elf_Word st_name; // Symbol name (index into string table) |
| 158 | unsigned char st_info; // Symbol's type and binding attributes |
| 159 | unsigned char st_other; // Must be zero; reserved |
| 160 | Elf_Half st_shndx; // Which section (header table index) it's defined in |
| 161 | Elf_Addr st_value; // Value or address associated with the symbol |
| 162 | Elf_Xword st_size; // Size of the symbol |
| 163 | }; |
| 164 | |
| 165 | template<support::endianness target_endianness, bool is64Bits> |
| 166 | struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> { |
| 167 | using Elf_Sym_Base<target_endianness, is64Bits>::st_info; |
| 168 | |
| 169 | // These accessors and mutators correspond to the ELF32_ST_BIND, |
| 170 | // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: |
| 171 | unsigned char getBinding() const { return st_info >> 4; } |
| 172 | unsigned char getType() const { return st_info & 0x0f; } |
| 173 | void setBinding(unsigned char b) { setBindingAndType(b, getType()); } |
| 174 | void setType(unsigned char t) { setBindingAndType(getBinding(), t); } |
| 175 | void setBindingAndType(unsigned char b, unsigned char t) { |
| 176 | st_info = (b << 4) + (t & 0x0f); |
| 177 | } |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | namespace { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 182 | template<support::endianness target_endianness, bool is64Bits, bool isRela> |
| 183 | struct Elf_Rel_Base; |
| 184 | |
| 185 | template<support::endianness target_endianness> |
| 186 | struct Elf_Rel_Base<target_endianness, false, false> { |
| 187 | LLVM_ELF_IMPORT_TYPES(target_endianness, false) |
| 188 | Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) |
| 189 | Elf_Word r_info; // Symbol table index and type of relocation to apply |
| 190 | }; |
| 191 | |
| 192 | template<support::endianness target_endianness> |
| 193 | struct Elf_Rel_Base<target_endianness, true, false> { |
| 194 | LLVM_ELF_IMPORT_TYPES(target_endianness, true) |
| 195 | Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) |
| 196 | Elf_Xword r_info; // Symbol table index and type of relocation to apply |
| 197 | }; |
| 198 | |
| 199 | template<support::endianness target_endianness> |
| 200 | struct Elf_Rel_Base<target_endianness, false, true> { |
| 201 | LLVM_ELF_IMPORT_TYPES(target_endianness, false) |
| 202 | Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) |
| 203 | Elf_Word r_info; // Symbol table index and type of relocation to apply |
| 204 | Elf_Sword r_addend; // Compute value for relocatable field by adding this |
| 205 | }; |
| 206 | |
| 207 | template<support::endianness target_endianness> |
| 208 | struct Elf_Rel_Base<target_endianness, true, true> { |
| 209 | LLVM_ELF_IMPORT_TYPES(target_endianness, true) |
| 210 | Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) |
| 211 | Elf_Xword r_info; // Symbol table index and type of relocation to apply |
| 212 | Elf_Sxword r_addend; // Compute value for relocatable field by adding this. |
| 213 | }; |
| 214 | |
| 215 | template<support::endianness target_endianness, bool is64Bits, bool isRela> |
| 216 | struct Elf_Rel_Impl; |
| 217 | |
| 218 | template<support::endianness target_endianness, bool isRela> |
| 219 | struct Elf_Rel_Impl<target_endianness, true, isRela> |
| 220 | : Elf_Rel_Base<target_endianness, true, isRela> { |
| 221 | using Elf_Rel_Base<target_endianness, true, isRela>::r_info; |
| 222 | LLVM_ELF_IMPORT_TYPES(target_endianness, true) |
| 223 | |
| 224 | // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, |
| 225 | // and ELF64_R_INFO macros defined in the ELF specification: |
| 226 | uint64_t getSymbol() const { return (r_info >> 32); } |
| 227 | unsigned char getType() const { |
| 228 | return (unsigned char) (r_info & 0xffffffffL); |
| 229 | } |
| 230 | void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); } |
| 231 | void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } |
| 232 | void setSymbolAndType(uint64_t s, unsigned char t) { |
| 233 | r_info = (s << 32) + (t&0xffffffffL); |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | template<support::endianness target_endianness, bool isRela> |
| 238 | struct Elf_Rel_Impl<target_endianness, false, isRela> |
| 239 | : Elf_Rel_Base<target_endianness, false, isRela> { |
| 240 | using Elf_Rel_Base<target_endianness, false, isRela>::r_info; |
| 241 | LLVM_ELF_IMPORT_TYPES(target_endianness, false) |
| 242 | |
| 243 | // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, |
| 244 | // and ELF32_R_INFO macros defined in the ELF specification: |
| 245 | uint32_t getSymbol() const { return (r_info >> 8); } |
| 246 | unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); } |
| 247 | void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); } |
| 248 | void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } |
| 249 | void setSymbolAndType(uint32_t s, unsigned char t) { |
| 250 | r_info = (s << 8) + t; |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | } |
| 255 | |
| 256 | namespace { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 257 | template<support::endianness target_endianness, bool is64Bits> |
| 258 | class ELFObjectFile : public ObjectFile { |
| 259 | LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) |
| 260 | |
| 261 | typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; |
| 262 | typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 263 | typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; |
| 264 | typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 265 | |
| 266 | struct Elf_Ehdr { |
| 267 | unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes |
| 268 | Elf_Half e_type; // Type of file (see ET_*) |
| 269 | Elf_Half e_machine; // Required architecture for this file (see EM_*) |
| 270 | Elf_Word e_version; // Must be equal to 1 |
| 271 | Elf_Addr e_entry; // Address to jump to in order to start program |
| 272 | Elf_Off e_phoff; // Program header table's file offset, in bytes |
| 273 | Elf_Off e_shoff; // Section header table's file offset, in bytes |
| 274 | Elf_Word e_flags; // Processor-specific flags |
| 275 | Elf_Half e_ehsize; // Size of ELF header, in bytes |
| 276 | Elf_Half e_phentsize;// Size of an entry in the program header table |
| 277 | Elf_Half e_phnum; // Number of entries in the program header table |
| 278 | Elf_Half e_shentsize;// Size of an entry in the section header table |
| 279 | Elf_Half e_shnum; // Number of entries in the section header table |
| 280 | Elf_Half e_shstrndx; // Section header table index of section name |
| 281 | // string table |
| 282 | bool checkMagic() const { |
| 283 | return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; |
| 284 | } |
| 285 | unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } |
| 286 | unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } |
| 287 | }; |
| 288 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 289 | typedef SmallVector<const Elf_Shdr*, 1> Sections_t; |
| 290 | typedef DenseMap<unsigned, unsigned> IndexMap_t; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 291 | typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 292 | |
| 293 | const Elf_Ehdr *Header; |
| 294 | const Elf_Shdr *SectionHeaderTable; |
| 295 | const Elf_Shdr *dot_shstrtab_sec; // Section header string table. |
| 296 | const Elf_Shdr *dot_strtab_sec; // Symbol header string table. |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 297 | Sections_t SymbolTableSections; |
| 298 | IndexMap_t SymbolTableSectionsIndexMap; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 299 | DenseMap<const Elf_Sym*, Elf_Word> ExtendedSymbolTable; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 300 | |
| 301 | /// @brief Map sections to an array of relocation sections that reference |
| 302 | /// them sorted by section index. |
| 303 | RelocMap_t SectionRelocMap; |
| 304 | |
| 305 | /// @brief Get the relocation section that contains \a Rel. |
| 306 | const Elf_Shdr *getRelSection(DataRefImpl Rel) const { |
| 307 | return getSection(Rel.w.b); |
| 308 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 309 | |
| 310 | void validateSymbol(DataRefImpl Symb) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 311 | bool isRelocationHasAddend(DataRefImpl Rel) const; |
| 312 | template<typename T> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 313 | const T *getEntry(uint16_t Section, uint32_t Entry) const; |
| 314 | template<typename T> |
| 315 | const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 316 | const Elf_Sym *getSymbol(DataRefImpl Symb) const; |
| 317 | const Elf_Shdr *getSection(DataRefImpl index) const; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 318 | const Elf_Shdr *getSection(uint32_t index) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 319 | const Elf_Rel *getRel(DataRefImpl Rel) const; |
| 320 | const Elf_Rela *getRela(DataRefImpl Rela) const; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 321 | const char *getString(uint32_t section, uint32_t offset) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 322 | const char *getString(const Elf_Shdr *section, uint32_t offset) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 323 | error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 324 | |
| 325 | protected: |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 326 | virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; |
| 327 | virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 328 | virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 329 | virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; |
| 330 | virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; |
| 331 | virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; |
| 332 | virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 333 | virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const; |
| 334 | virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 335 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 336 | virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; |
| 337 | virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; |
| 338 | virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; |
| 339 | virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; |
| 340 | virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 341 | virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 342 | virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 343 | virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; |
| 344 | virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 345 | virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, |
| 346 | bool &Result) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 347 | virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; |
| 348 | virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 349 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 350 | virtual error_code getRelocationNext(DataRefImpl Rel, |
| 351 | RelocationRef &Res) const; |
| 352 | virtual error_code getRelocationAddress(DataRefImpl Rel, |
| 353 | uint64_t &Res) const; |
| 354 | virtual error_code getRelocationSymbol(DataRefImpl Rel, |
| 355 | SymbolRef &Res) const; |
| 356 | virtual error_code getRelocationType(DataRefImpl Rel, |
| 357 | uint32_t &Res) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 358 | virtual error_code getRelocationTypeName(DataRefImpl Rel, |
| 359 | SmallVectorImpl<char> &Result) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 360 | virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, |
| 361 | int64_t &Res) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 362 | virtual error_code getRelocationValueString(DataRefImpl Rel, |
| 363 | SmallVectorImpl<char> &Result) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 364 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 365 | public: |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 366 | ELFObjectFile(MemoryBuffer *Object, error_code &ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 367 | virtual symbol_iterator begin_symbols() const; |
| 368 | virtual symbol_iterator end_symbols() const; |
| 369 | virtual section_iterator begin_sections() const; |
| 370 | virtual section_iterator end_sections() const; |
| 371 | |
| 372 | virtual uint8_t getBytesInAddress() const; |
| 373 | virtual StringRef getFileFormatName() const; |
| 374 | virtual unsigned getArch() const; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 375 | |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 376 | uint64_t getNumSections() const; |
| 377 | uint64_t getStringTableIndex() const; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 378 | uint64_t getSymbolTableIndex(const Elf_Sym *symb) const; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 379 | const Elf_Shdr *getSection(const Elf_Sym *symb) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 380 | }; |
| 381 | } // end namespace |
| 382 | |
| 383 | template<support::endianness target_endianness, bool is64Bits> |
| 384 | void ELFObjectFile<target_endianness, is64Bits> |
| 385 | ::validateSymbol(DataRefImpl Symb) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 386 | const Elf_Sym *symb = getSymbol(Symb); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 387 | const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 388 | // FIXME: We really need to do proper error handling in the case of an invalid |
| 389 | // input file. Because we don't use exceptions, I think we'll just pass |
| 390 | // an error object around. |
| 391 | if (!( symb |
| 392 | && SymbolTableSection |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 393 | && symb >= (const Elf_Sym*)(base() |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 394 | + SymbolTableSection->sh_offset) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 395 | && symb < (const Elf_Sym*)(base() |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 396 | + SymbolTableSection->sh_offset |
| 397 | + SymbolTableSection->sh_size))) |
| 398 | // FIXME: Proper error handling. |
| 399 | report_fatal_error("Symb must point to a valid symbol!"); |
| 400 | } |
| 401 | |
| 402 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 403 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 404 | ::getSymbolNext(DataRefImpl Symb, |
| 405 | SymbolRef &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 406 | validateSymbol(Symb); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 407 | const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 408 | |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 409 | ++Symb.d.a; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 410 | // Check to see if we are at the end of this symbol table. |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 411 | if (Symb.d.a >= SymbolTableSection->getEntityCount()) { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 412 | // We are at the end. If there are other symbol tables, jump to them. |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 413 | ++Symb.d.b; |
| 414 | Symb.d.a = 1; // The 0th symbol in ELF is fake. |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 415 | // Otherwise return the terminator. |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 416 | if (Symb.d.b >= SymbolTableSections.size()) { |
| 417 | Symb.d.a = std::numeric_limits<uint32_t>::max(); |
| 418 | Symb.d.b = std::numeric_limits<uint32_t>::max(); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 422 | Result = SymbolRef(Symb, this); |
| 423 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 427 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 428 | ::getSymbolName(DataRefImpl Symb, |
| 429 | StringRef &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 430 | validateSymbol(Symb); |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 431 | const Elf_Sym *symb = getSymbol(Symb); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 432 | return getSymbolName(symb, Result); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | template<support::endianness target_endianness, bool is64Bits> |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 436 | uint64_t ELFObjectFile<target_endianness, is64Bits> |
| 437 | ::getSymbolTableIndex(const Elf_Sym *symb) const { |
| 438 | if (symb->st_shndx == ELF::SHN_XINDEX) |
| 439 | return ExtendedSymbolTable.lookup(symb); |
| 440 | return symb->st_shndx; |
| 441 | } |
| 442 | |
| 443 | template<support::endianness target_endianness, bool is64Bits> |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 444 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * |
| 445 | ELFObjectFile<target_endianness, is64Bits> |
| 446 | ::getSection(const Elf_Sym *symb) const { |
NAKAMURA Takumi | 18dcb87 | 2011-10-12 10:28:55 +0000 | [diff] [blame^] | 447 | if (symb->st_shndx == ELF::SHN_XINDEX) { |
| 448 | if (!ExtendedSymbolTable.count(symb)) |
| 449 | return 0; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 450 | return getSection(ExtendedSymbolTable.lookup(symb)); |
NAKAMURA Takumi | 18dcb87 | 2011-10-12 10:28:55 +0000 | [diff] [blame^] | 451 | } |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 452 | if (symb->st_shndx >= ELF::SHN_LORESERVE) |
| 453 | return 0; |
| 454 | return getSection(symb->st_shndx); |
| 455 | } |
| 456 | |
| 457 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 458 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 459 | ::getSymbolOffset(DataRefImpl Symb, |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 460 | uint64_t &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 461 | validateSymbol(Symb); |
| 462 | const Elf_Sym *symb = getSymbol(Symb); |
| 463 | const Elf_Shdr *Section; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 464 | switch (getSymbolTableIndex(symb)) { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 465 | case ELF::SHN_COMMON: |
| 466 | // Undefined symbols have no address yet. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 467 | case ELF::SHN_UNDEF: |
| 468 | Result = UnknownAddressOrSize; |
| 469 | return object_error::success; |
| 470 | case ELF::SHN_ABS: |
| 471 | Result = symb->st_value; |
| 472 | return object_error::success; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 473 | default: Section = getSection(symb); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | switch (symb->getType()) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 477 | case ELF::STT_SECTION: |
| 478 | Result = Section ? Section->sh_addr : UnknownAddressOrSize; |
| 479 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 480 | case ELF::STT_FUNC: |
| 481 | case ELF::STT_OBJECT: |
| 482 | case ELF::STT_NOTYPE: |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 483 | Result = symb->st_value; |
| 484 | return object_error::success; |
| 485 | default: |
| 486 | Result = UnknownAddressOrSize; |
| 487 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 492 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 493 | ::getSymbolAddress(DataRefImpl Symb, |
| 494 | uint64_t &Result) const { |
| 495 | validateSymbol(Symb); |
| 496 | const Elf_Sym *symb = getSymbol(Symb); |
| 497 | const Elf_Shdr *Section; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 498 | switch (getSymbolTableIndex(symb)) { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 499 | case ELF::SHN_COMMON: // Fall through. |
| 500 | // Undefined symbols have no address yet. |
| 501 | case ELF::SHN_UNDEF: |
| 502 | Result = UnknownAddressOrSize; |
| 503 | return object_error::success; |
| 504 | case ELF::SHN_ABS: |
| 505 | Result = reinterpret_cast<uintptr_t>(base()+symb->st_value); |
| 506 | return object_error::success; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 507 | default: Section = getSection(symb); |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 508 | } |
| 509 | const uint8_t* addr = base(); |
| 510 | if (Section) |
| 511 | addr += Section->sh_offset; |
| 512 | switch (symb->getType()) { |
| 513 | case ELF::STT_SECTION: |
| 514 | Result = reinterpret_cast<uintptr_t>(addr); |
| 515 | return object_error::success; |
| 516 | case ELF::STT_FUNC: // Fall through. |
| 517 | case ELF::STT_OBJECT: // Fall through. |
| 518 | case ELF::STT_NOTYPE: |
| 519 | addr += symb->st_value; |
| 520 | Result = reinterpret_cast<uintptr_t>(addr); |
| 521 | return object_error::success; |
| 522 | default: |
| 523 | Result = UnknownAddressOrSize; |
| 524 | return object_error::success; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | template<support::endianness target_endianness, bool is64Bits> |
| 529 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 530 | ::getSymbolSize(DataRefImpl Symb, |
| 531 | uint64_t &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 532 | validateSymbol(Symb); |
| 533 | const Elf_Sym *symb = getSymbol(Symb); |
| 534 | if (symb->st_size == 0) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 535 | Result = UnknownAddressOrSize; |
| 536 | Result = symb->st_size; |
| 537 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 541 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 542 | ::getSymbolNMTypeChar(DataRefImpl Symb, |
| 543 | char &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 544 | validateSymbol(Symb); |
| 545 | const Elf_Sym *symb = getSymbol(Symb); |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 546 | const Elf_Shdr *Section = getSection(symb); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 547 | |
| 548 | char ret = '?'; |
| 549 | |
| 550 | if (Section) { |
| 551 | switch (Section->sh_type) { |
| 552 | case ELF::SHT_PROGBITS: |
| 553 | case ELF::SHT_DYNAMIC: |
| 554 | switch (Section->sh_flags) { |
| 555 | case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR): |
| 556 | ret = 't'; break; |
| 557 | case (ELF::SHF_ALLOC | ELF::SHF_WRITE): |
| 558 | ret = 'd'; break; |
| 559 | case ELF::SHF_ALLOC: |
| 560 | case (ELF::SHF_ALLOC | ELF::SHF_MERGE): |
| 561 | case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS): |
| 562 | ret = 'r'; break; |
| 563 | } |
| 564 | break; |
| 565 | case ELF::SHT_NOBITS: ret = 'b'; |
| 566 | } |
| 567 | } |
| 568 | |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 569 | switch (getSymbolTableIndex(symb)) { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 570 | case ELF::SHN_UNDEF: |
| 571 | if (ret == '?') |
| 572 | ret = 'U'; |
| 573 | break; |
| 574 | case ELF::SHN_ABS: ret = 'a'; break; |
| 575 | case ELF::SHN_COMMON: ret = 'c'; break; |
| 576 | } |
| 577 | |
| 578 | switch (symb->getBinding()) { |
| 579 | case ELF::STB_GLOBAL: ret = ::toupper(ret); break; |
| 580 | case ELF::STB_WEAK: |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 581 | if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 582 | ret = 'w'; |
| 583 | else |
| 584 | if (symb->getType() == ELF::STT_OBJECT) |
| 585 | ret = 'V'; |
| 586 | else |
| 587 | ret = 'W'; |
| 588 | } |
| 589 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 590 | if (ret == '?' && symb->getType() == ELF::STT_SECTION) { |
| 591 | StringRef name; |
| 592 | if (error_code ec = getSymbolName(Symb, name)) |
| 593 | return ec; |
| 594 | Result = StringSwitch<char>(name) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 595 | .StartsWith(".debug", 'N') |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 596 | .StartsWith(".note", 'n') |
| 597 | .Default('?'); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 598 | return object_error::success; |
| 599 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 600 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 601 | Result = ret; |
| 602 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 606 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 607 | ::getSymbolType(DataRefImpl Symb, |
| 608 | SymbolRef::SymbolType &Result) const { |
| 609 | validateSymbol(Symb); |
| 610 | const Elf_Sym *symb = getSymbol(Symb); |
| 611 | |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 612 | if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 613 | Result = SymbolRef::ST_External; |
| 614 | return object_error::success; |
| 615 | } |
| 616 | |
| 617 | switch (symb->getType()) { |
| 618 | case ELF::STT_FUNC: |
| 619 | Result = SymbolRef::ST_Function; |
| 620 | break; |
| 621 | case ELF::STT_OBJECT: |
| 622 | Result = SymbolRef::ST_Data; |
| 623 | break; |
| 624 | default: |
| 625 | Result = SymbolRef::ST_Other; |
| 626 | break; |
| 627 | } |
| 628 | return object_error::success; |
| 629 | } |
| 630 | |
| 631 | template<support::endianness target_endianness, bool is64Bits> |
| 632 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 633 | ::isSymbolGlobal(DataRefImpl Symb, |
| 634 | bool &Result) const { |
| 635 | validateSymbol(Symb); |
| 636 | const Elf_Sym *symb = getSymbol(Symb); |
| 637 | |
| 638 | Result = symb->getBinding() == ELF::STB_GLOBAL; |
| 639 | return object_error::success; |
| 640 | } |
| 641 | |
| 642 | template<support::endianness target_endianness, bool is64Bits> |
| 643 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 644 | ::isSymbolInternal(DataRefImpl Symb, |
| 645 | bool &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 646 | validateSymbol(Symb); |
| 647 | const Elf_Sym *symb = getSymbol(Symb); |
| 648 | |
| 649 | if ( symb->getType() == ELF::STT_FILE |
| 650 | || symb->getType() == ELF::STT_SECTION) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 651 | Result = true; |
| 652 | Result = false; |
| 653 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 657 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 658 | ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 659 | const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 660 | sec += Header->e_shentsize; |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 661 | Sec.p = reinterpret_cast<intptr_t>(sec); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 662 | Result = SectionRef(Sec, this); |
| 663 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 667 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 668 | ::getSectionName(DataRefImpl Sec, |
| 669 | StringRef &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 670 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 671 | Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name)); |
| 672 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 676 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 677 | ::getSectionAddress(DataRefImpl Sec, |
| 678 | uint64_t &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 679 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 680 | Result = sec->sh_addr; |
| 681 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 685 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 686 | ::getSectionSize(DataRefImpl Sec, |
| 687 | uint64_t &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 688 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 689 | Result = sec->sh_size; |
| 690 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 694 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 695 | ::getSectionContents(DataRefImpl Sec, |
| 696 | StringRef &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 697 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 698 | const char *start = (const char*)base() + sec->sh_offset; |
| 699 | Result = StringRef(start, sec->sh_size); |
| 700 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 704 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 705 | ::getSectionAlignment(DataRefImpl Sec, |
| 706 | uint64_t &Result) const { |
| 707 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 708 | Result = sec->sh_addralign; |
| 709 | return object_error::success; |
| 710 | } |
| 711 | |
| 712 | template<support::endianness target_endianness, bool is64Bits> |
| 713 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 714 | ::isSectionText(DataRefImpl Sec, |
| 715 | bool &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 716 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 717 | if (sec->sh_flags & ELF::SHF_EXECINSTR) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 718 | Result = true; |
| 719 | else |
| 720 | Result = false; |
| 721 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | template<support::endianness target_endianness, bool is64Bits> |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 725 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 726 | ::isSectionData(DataRefImpl Sec, |
| 727 | bool &Result) const { |
| 728 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 729 | if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) |
| 730 | && sec->sh_type == ELF::SHT_PROGBITS) |
| 731 | Result = true; |
| 732 | else |
| 733 | Result = false; |
| 734 | return object_error::success; |
| 735 | } |
| 736 | |
| 737 | template<support::endianness target_endianness, bool is64Bits> |
| 738 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 739 | ::isSectionBSS(DataRefImpl Sec, |
| 740 | bool &Result) const { |
| 741 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 742 | if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) |
| 743 | && sec->sh_type == ELF::SHT_NOBITS) |
| 744 | Result = true; |
| 745 | else |
| 746 | Result = false; |
| 747 | return object_error::success; |
| 748 | } |
| 749 | |
| 750 | template<support::endianness target_endianness, bool is64Bits> |
| 751 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 752 | ::sectionContainsSymbol(DataRefImpl Sec, |
| 753 | DataRefImpl Symb, |
| 754 | bool &Result) const { |
| 755 | // FIXME: Unimplemented. |
| 756 | Result = false; |
| 757 | return object_error::success; |
| 758 | } |
| 759 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 760 | template<support::endianness target_endianness, bool is64Bits> |
| 761 | relocation_iterator ELFObjectFile<target_endianness, is64Bits> |
| 762 | ::getSectionRelBegin(DataRefImpl Sec) const { |
| 763 | DataRefImpl RelData; |
| 764 | memset(&RelData, 0, sizeof(RelData)); |
| 765 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 766 | typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); |
| 767 | if (sec != 0 && ittr != SectionRelocMap.end()) { |
| 768 | RelData.w.a = getSection(ittr->second[0])->sh_link; |
| 769 | RelData.w.b = ittr->second[0]; |
| 770 | RelData.w.c = 0; |
| 771 | } |
| 772 | return relocation_iterator(RelocationRef(RelData, this)); |
| 773 | } |
| 774 | |
| 775 | template<support::endianness target_endianness, bool is64Bits> |
| 776 | relocation_iterator ELFObjectFile<target_endianness, is64Bits> |
| 777 | ::getSectionRelEnd(DataRefImpl Sec) const { |
| 778 | DataRefImpl RelData; |
| 779 | memset(&RelData, 0, sizeof(RelData)); |
| 780 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 781 | typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); |
| 782 | if (sec != 0 && ittr != SectionRelocMap.end()) { |
| 783 | // Get the index of the last relocation section for this section. |
| 784 | std::size_t relocsecindex = ittr->second[ittr->second.size() - 1]; |
| 785 | const Elf_Shdr *relocsec = getSection(relocsecindex); |
| 786 | RelData.w.a = relocsec->sh_link; |
| 787 | RelData.w.b = relocsecindex; |
| 788 | RelData.w.c = relocsec->sh_size / relocsec->sh_entsize; |
| 789 | } |
| 790 | return relocation_iterator(RelocationRef(RelData, this)); |
| 791 | } |
| 792 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 793 | // Relocations |
| 794 | template<support::endianness target_endianness, bool is64Bits> |
| 795 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 796 | ::getRelocationNext(DataRefImpl Rel, |
| 797 | RelocationRef &Result) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 798 | ++Rel.w.c; |
| 799 | const Elf_Shdr *relocsec = getSection(Rel.w.b); |
| 800 | if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) { |
| 801 | // We have reached the end of the relocations for this section. See if there |
| 802 | // is another relocation section. |
Michael J. Spencer | 01a4db3 | 2011-10-07 19:46:12 +0000 | [diff] [blame] | 803 | typename RelocMap_t::mapped_type relocseclist = |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 804 | SectionRelocMap.lookup(getSection(Rel.w.a)); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 805 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 806 | // Do a binary search for the current reloc section index (which must be |
| 807 | // present). Then get the next one. |
| 808 | typename RelocMap_t::mapped_type::const_iterator loc = |
| 809 | std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b); |
| 810 | ++loc; |
| 811 | |
| 812 | // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel |
| 813 | // to the end iterator. |
| 814 | if (loc != relocseclist.end()) { |
| 815 | Rel.w.b = *loc; |
| 816 | Rel.w.a = 0; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 819 | Result = RelocationRef(Rel, this); |
| 820 | return object_error::success; |
| 821 | } |
| 822 | |
| 823 | template<support::endianness target_endianness, bool is64Bits> |
| 824 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 825 | ::getRelocationSymbol(DataRefImpl Rel, |
| 826 | SymbolRef &Result) const { |
| 827 | uint32_t symbolIdx; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 828 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 829 | switch (sec->sh_type) { |
| 830 | default : |
| 831 | report_fatal_error("Invalid section type in Rel!"); |
| 832 | case ELF::SHT_REL : { |
| 833 | symbolIdx = getRel(Rel)->getSymbol(); |
| 834 | break; |
| 835 | } |
| 836 | case ELF::SHT_RELA : { |
| 837 | symbolIdx = getRela(Rel)->getSymbol(); |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | DataRefImpl SymbolData; |
| 842 | IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link); |
| 843 | if (it == SymbolTableSectionsIndexMap.end()) |
| 844 | report_fatal_error("Relocation symbol table not found!"); |
| 845 | SymbolData.d.a = symbolIdx; |
| 846 | SymbolData.d.b = it->second; |
| 847 | Result = SymbolRef(SymbolData, this); |
| 848 | return object_error::success; |
| 849 | } |
| 850 | |
| 851 | template<support::endianness target_endianness, bool is64Bits> |
| 852 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 853 | ::getRelocationAddress(DataRefImpl Rel, |
| 854 | uint64_t &Result) const { |
| 855 | uint64_t offset; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 856 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 857 | switch (sec->sh_type) { |
| 858 | default : |
| 859 | report_fatal_error("Invalid section type in Rel!"); |
| 860 | case ELF::SHT_REL : { |
| 861 | offset = getRel(Rel)->r_offset; |
| 862 | break; |
| 863 | } |
| 864 | case ELF::SHT_RELA : { |
| 865 | offset = getRela(Rel)->r_offset; |
| 866 | break; |
| 867 | } |
| 868 | } |
| 869 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 870 | Result = offset; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 871 | return object_error::success; |
| 872 | } |
| 873 | |
| 874 | template<support::endianness target_endianness, bool is64Bits> |
| 875 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 876 | ::getRelocationType(DataRefImpl Rel, |
| 877 | uint32_t &Result) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 878 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 879 | switch (sec->sh_type) { |
| 880 | default : |
| 881 | report_fatal_error("Invalid section type in Rel!"); |
| 882 | case ELF::SHT_REL : { |
| 883 | Result = getRel(Rel)->getType(); |
| 884 | break; |
| 885 | } |
| 886 | case ELF::SHT_RELA : { |
| 887 | Result = getRela(Rel)->getType(); |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | return object_error::success; |
| 892 | } |
| 893 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 894 | #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \ |
| 895 | case ELF::enum: res = #enum; break; |
| 896 | |
| 897 | template<support::endianness target_endianness, bool is64Bits> |
| 898 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 899 | ::getRelocationTypeName(DataRefImpl Rel, |
| 900 | SmallVectorImpl<char> &Result) const { |
| 901 | const Elf_Shdr *sec = getSection(Rel.w.b); |
| 902 | uint8_t type; |
| 903 | StringRef res; |
| 904 | switch (sec->sh_type) { |
| 905 | default : |
| 906 | return object_error::parse_failed; |
| 907 | case ELF::SHT_REL : { |
| 908 | type = getRel(Rel)->getType(); |
| 909 | break; |
| 910 | } |
| 911 | case ELF::SHT_RELA : { |
| 912 | type = getRela(Rel)->getType(); |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | switch (Header->e_machine) { |
| 917 | case ELF::EM_X86_64: |
| 918 | switch (type) { |
| 919 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE); |
| 920 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64); |
| 921 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32); |
| 922 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32); |
| 923 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32); |
| 924 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY); |
| 925 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT); |
| 926 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT); |
| 927 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE); |
| 928 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL); |
| 929 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32); |
| 930 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S); |
| 931 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16); |
| 932 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16); |
| 933 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8); |
| 934 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8); |
| 935 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64); |
| 936 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64); |
| 937 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64); |
| 938 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD); |
| 939 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD); |
| 940 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32); |
| 941 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF); |
| 942 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32); |
| 943 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64); |
| 944 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64); |
| 945 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32); |
| 946 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32); |
| 947 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64); |
| 948 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC); |
| 949 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL); |
| 950 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC); |
| 951 | default: |
| 952 | res = "Unknown"; |
| 953 | } |
| 954 | break; |
| 955 | case ELF::EM_386: |
| 956 | switch (type) { |
| 957 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE); |
| 958 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32); |
| 959 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32); |
| 960 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32); |
| 961 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32); |
| 962 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY); |
| 963 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT); |
| 964 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT); |
| 965 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE); |
| 966 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF); |
| 967 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC); |
| 968 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT); |
| 969 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF); |
| 970 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE); |
| 971 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE); |
| 972 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE); |
| 973 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD); |
| 974 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM); |
| 975 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16); |
| 976 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16); |
| 977 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8); |
| 978 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8); |
| 979 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32); |
| 980 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH); |
| 981 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL); |
| 982 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP); |
| 983 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32); |
| 984 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH); |
| 985 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL); |
| 986 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP); |
| 987 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32); |
| 988 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32); |
| 989 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32); |
| 990 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32); |
| 991 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32); |
| 992 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32); |
| 993 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC); |
| 994 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL); |
| 995 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC); |
| 996 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE); |
| 997 | default: |
| 998 | res = "Unknown"; |
| 999 | } |
| 1000 | break; |
| 1001 | default: |
| 1002 | res = "Unknown"; |
| 1003 | } |
| 1004 | Result.append(res.begin(), res.end()); |
| 1005 | return object_error::success; |
| 1006 | } |
| 1007 | |
| 1008 | #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME |
| 1009 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1010 | template<support::endianness target_endianness, bool is64Bits> |
| 1011 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 1012 | ::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 1013 | int64_t &Result) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1014 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1015 | switch (sec->sh_type) { |
| 1016 | default : |
| 1017 | report_fatal_error("Invalid section type in Rel!"); |
| 1018 | case ELF::SHT_REL : { |
| 1019 | Result = 0; |
| 1020 | return object_error::success; |
| 1021 | } |
| 1022 | case ELF::SHT_RELA : { |
| 1023 | Result = getRela(Rel)->r_addend; |
| 1024 | return object_error::success; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1029 | template<support::endianness target_endianness, bool is64Bits> |
| 1030 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 1031 | ::getRelocationValueString(DataRefImpl Rel, |
| 1032 | SmallVectorImpl<char> &Result) const { |
| 1033 | const Elf_Shdr *sec = getSection(Rel.w.b); |
| 1034 | uint8_t type; |
| 1035 | StringRef res; |
| 1036 | int64_t addend = 0; |
| 1037 | uint16_t symbol_index = 0; |
| 1038 | switch (sec->sh_type) { |
| 1039 | default : |
| 1040 | return object_error::parse_failed; |
| 1041 | case ELF::SHT_REL : { |
| 1042 | type = getRel(Rel)->getType(); |
| 1043 | symbol_index = getRel(Rel)->getSymbol(); |
| 1044 | // TODO: Read implicit addend from section data. |
| 1045 | break; |
| 1046 | } |
| 1047 | case ELF::SHT_RELA : { |
| 1048 | type = getRela(Rel)->getType(); |
| 1049 | symbol_index = getRela(Rel)->getSymbol(); |
| 1050 | addend = getRela(Rel)->r_addend; |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
| 1054 | const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index); |
| 1055 | StringRef symname; |
| 1056 | if (error_code ec = getSymbolName(symb, symname)) |
| 1057 | return ec; |
| 1058 | switch (Header->e_machine) { |
| 1059 | case ELF::EM_X86_64: |
| 1060 | switch (type) { |
| 1061 | case ELF::R_X86_64_32S: |
| 1062 | res = symname; |
| 1063 | break; |
| 1064 | case ELF::R_X86_64_PC32: { |
| 1065 | std::string fmtbuf; |
| 1066 | raw_string_ostream fmt(fmtbuf); |
| 1067 | fmt << symname << (addend < 0 ? "" : "+") << addend << "-P"; |
| 1068 | fmt.flush(); |
| 1069 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 1070 | } |
| 1071 | break; |
| 1072 | default: |
| 1073 | res = "Unknown"; |
| 1074 | } |
| 1075 | break; |
| 1076 | default: |
| 1077 | res = "Unknown"; |
| 1078 | } |
| 1079 | if (Result.empty()) |
| 1080 | Result.append(res.begin(), res.end()); |
| 1081 | return object_error::success; |
| 1082 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1083 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 1084 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1085 | ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object |
| 1086 | , error_code &ec) |
| 1087 | : ObjectFile(Binary::isELF, Object, ec) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1088 | , SectionHeaderTable(0) |
| 1089 | , dot_shstrtab_sec(0) |
| 1090 | , dot_strtab_sec(0) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1091 | Header = reinterpret_cast<const Elf_Ehdr *>(base()); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1092 | |
| 1093 | if (Header->e_shoff == 0) |
| 1094 | return; |
| 1095 | |
| 1096 | SectionHeaderTable = |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1097 | reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff); |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1098 | uint64_t SectionTableSize = getNumSections() * Header->e_shentsize; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1099 | if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1100 | <= base() + Data->getBufferSize())) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1101 | // FIXME: Proper error handling. |
| 1102 | report_fatal_error("Section table goes past end of file!"); |
| 1103 | |
Nick Lewycky | fb05d3d | 2011-10-11 00:38:56 +0000 | [diff] [blame] | 1104 | |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 1105 | // To find the symbol tables we walk the section table to find SHT_SYMTAB. |
| 1106 | const Elf_Shdr* SymbolTableSectionHeaderIndex = 0; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1107 | const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable); |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1108 | for (uint64_t i = 0, e = getNumSections(); i != e; ++i) { |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 1109 | if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) { |
| 1110 | if (SymbolTableSectionHeaderIndex) |
| 1111 | // FIXME: Proper error handling. |
| 1112 | report_fatal_error("More than one .symtab_shndx!"); |
| 1113 | SymbolTableSectionHeaderIndex = sh; |
| 1114 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1115 | if (sh->sh_type == ELF::SHT_SYMTAB) { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1116 | SymbolTableSectionsIndexMap[i] = SymbolTableSections.size(); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1117 | SymbolTableSections.push_back(sh); |
| 1118 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1119 | if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1120 | SectionRelocMap[getSection(sh->sh_link)].push_back(i); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1121 | } |
| 1122 | ++sh; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1125 | // Sort section relocation lists by index. |
| 1126 | for (typename RelocMap_t::iterator i = SectionRelocMap.begin(), |
| 1127 | e = SectionRelocMap.end(); i != e; ++i) { |
| 1128 | std::sort(i->second.begin(), i->second.end()); |
| 1129 | } |
| 1130 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1131 | // Get string table sections. |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1132 | dot_shstrtab_sec = getSection(getStringTableIndex()); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1133 | if (dot_shstrtab_sec) { |
| 1134 | // Verify that the last byte in the string table in a null. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1135 | if (((const char*)base() + dot_shstrtab_sec->sh_offset) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1136 | [dot_shstrtab_sec->sh_size - 1] != 0) |
| 1137 | // FIXME: Proper error handling. |
| 1138 | report_fatal_error("String table must end with a null terminator!"); |
| 1139 | } |
| 1140 | |
| 1141 | // Merge this into the above loop. |
| 1142 | for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable), |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1143 | *e = i + getNumSections() * Header->e_shentsize; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1144 | i != e; i += Header->e_shentsize) { |
| 1145 | const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i); |
| 1146 | if (sh->sh_type == ELF::SHT_STRTAB) { |
| 1147 | StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name)); |
| 1148 | if (SectionName == ".strtab") { |
| 1149 | if (dot_strtab_sec != 0) |
| 1150 | // FIXME: Proper error handling. |
| 1151 | report_fatal_error("Already found section named .strtab!"); |
| 1152 | dot_strtab_sec = sh; |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1153 | const char *dot_strtab = (const char*)base() + sh->sh_offset; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1154 | if (dot_strtab[sh->sh_size - 1] != 0) |
| 1155 | // FIXME: Proper error handling. |
| 1156 | report_fatal_error("String table must end with a null terminator!"); |
| 1157 | } |
| 1158 | } |
| 1159 | } |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Build symbol name side-mapping if there is one. |
| 1162 | if (SymbolTableSectionHeaderIndex) { |
| 1163 | const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() + |
| 1164 | SymbolTableSectionHeaderIndex->sh_offset); |
| 1165 | error_code ec; |
| 1166 | for (symbol_iterator si = begin_symbols(), |
| 1167 | se = end_symbols(); si != se; si.increment(ec)) { |
| 1168 | if (ec) |
| 1169 | report_fatal_error("Fewer extended symbol table entries than symbols!"); |
| 1170 | if (*ShndxTable != ELF::SHN_UNDEF) |
| 1171 | ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable; |
| 1172 | ++ShndxTable; |
| 1173 | } |
| 1174 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1178 | symbol_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1179 | ::begin_symbols() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1180 | DataRefImpl SymbolData; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1181 | memset(&SymbolData, 0, sizeof(SymbolData)); |
| 1182 | if (SymbolTableSections.size() == 0) { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1183 | SymbolData.d.a = std::numeric_limits<uint32_t>::max(); |
| 1184 | SymbolData.d.b = std::numeric_limits<uint32_t>::max(); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1185 | } else { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1186 | SymbolData.d.a = 1; // The 0th symbol in ELF is fake. |
| 1187 | SymbolData.d.b = 0; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1188 | } |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1189 | return symbol_iterator(SymbolRef(SymbolData, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1193 | symbol_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1194 | ::end_symbols() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1195 | DataRefImpl SymbolData; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1196 | memset(&SymbolData, 0, sizeof(SymbolData)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1197 | SymbolData.d.a = std::numeric_limits<uint32_t>::max(); |
| 1198 | SymbolData.d.b = std::numeric_limits<uint32_t>::max(); |
| 1199 | return symbol_iterator(SymbolRef(SymbolData, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1203 | section_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1204 | ::begin_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1205 | DataRefImpl ret; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 1206 | memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1207 | ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1208 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1212 | section_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1213 | ::end_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1214 | DataRefImpl ret; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 1215 | memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1216 | ret.p = reinterpret_cast<intptr_t>(base() |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1217 | + Header->e_shoff |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1218 | + (Header->e_shentsize*getNumSections())); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1219 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | template<support::endianness target_endianness, bool is64Bits> |
| 1223 | uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1224 | return is64Bits ? 8 : 4; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | template<support::endianness target_endianness, bool is64Bits> |
| 1228 | StringRef ELFObjectFile<target_endianness, is64Bits> |
| 1229 | ::getFileFormatName() const { |
| 1230 | switch(Header->e_ident[ELF::EI_CLASS]) { |
| 1231 | case ELF::ELFCLASS32: |
| 1232 | switch(Header->e_machine) { |
| 1233 | case ELF::EM_386: |
| 1234 | return "ELF32-i386"; |
| 1235 | case ELF::EM_X86_64: |
| 1236 | return "ELF32-x86-64"; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1237 | case ELF::EM_ARM: |
| 1238 | return "ELF32-arm"; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1239 | default: |
| 1240 | return "ELF32-unknown"; |
| 1241 | } |
| 1242 | case ELF::ELFCLASS64: |
| 1243 | switch(Header->e_machine) { |
| 1244 | case ELF::EM_386: |
| 1245 | return "ELF64-i386"; |
| 1246 | case ELF::EM_X86_64: |
| 1247 | return "ELF64-x86-64"; |
| 1248 | default: |
| 1249 | return "ELF64-unknown"; |
| 1250 | } |
| 1251 | default: |
| 1252 | // FIXME: Proper error handling. |
| 1253 | report_fatal_error("Invalid ELFCLASS!"); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | template<support::endianness target_endianness, bool is64Bits> |
| 1258 | unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const { |
| 1259 | switch(Header->e_machine) { |
| 1260 | case ELF::EM_386: |
| 1261 | return Triple::x86; |
| 1262 | case ELF::EM_X86_64: |
| 1263 | return Triple::x86_64; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1264 | case ELF::EM_ARM: |
| 1265 | return Triple::arm; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1266 | default: |
| 1267 | return Triple::UnknownArch; |
| 1268 | } |
| 1269 | } |
| 1270 | |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1271 | template<support::endianness target_endianness, bool is64Bits> |
| 1272 | uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const { |
| 1273 | if (Header->e_shnum == ELF::SHN_UNDEF) |
| 1274 | return SectionHeaderTable->sh_size; |
| 1275 | return Header->e_shnum; |
| 1276 | } |
| 1277 | |
| 1278 | template<support::endianness target_endianness, bool is64Bits> |
| 1279 | uint64_t |
| 1280 | ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const { |
| 1281 | if (Header->e_shnum == ELF::SHN_UNDEF) { |
| 1282 | if (Header->e_shstrndx == ELF::SHN_HIRESERVE) |
| 1283 | return SectionHeaderTable->sh_link; |
| 1284 | if (Header->e_shstrndx >= getNumSections()) |
| 1285 | return 0; |
| 1286 | } |
| 1287 | return Header->e_shstrndx; |
| 1288 | } |
| 1289 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1290 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1291 | template<support::endianness target_endianness, bool is64Bits> |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1292 | template<typename T> |
| 1293 | inline const T * |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1294 | ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section, |
| 1295 | uint32_t Entry) const { |
| 1296 | return getEntry<T>(getSection(Section), Entry); |
| 1297 | } |
| 1298 | |
| 1299 | template<support::endianness target_endianness, bool is64Bits> |
| 1300 | template<typename T> |
| 1301 | inline const T * |
| 1302 | ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section, |
| 1303 | uint32_t Entry) const { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1304 | return reinterpret_cast<const T *>( |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1305 | base() |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1306 | + Section->sh_offset |
| 1307 | + (Entry * Section->sh_entsize)); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | template<support::endianness target_endianness, bool is64Bits> |
| 1311 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym * |
| 1312 | ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1313 | return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | template<support::endianness target_endianness, bool is64Bits> |
| 1317 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel * |
| 1318 | ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1319 | return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | template<support::endianness target_endianness, bool is64Bits> |
| 1323 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela * |
| 1324 | ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1325 | return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | template<support::endianness target_endianness, bool is64Bits> |
| 1329 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * |
| 1330 | ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1331 | const Elf_Shdr *sec = getSection(Symb.d.b); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1332 | if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1333 | // FIXME: Proper error handling. |
| 1334 | report_fatal_error("Invalid symbol table section!"); |
| 1335 | return sec; |
| 1336 | } |
| 1337 | |
| 1338 | template<support::endianness target_endianness, bool is64Bits> |
| 1339 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1340 | ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const { |
| 1341 | if (index == 0) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1342 | return 0; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1343 | if (!SectionHeaderTable || index >= getNumSections()) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1344 | // FIXME: Proper error handling. |
| 1345 | report_fatal_error("Invalid section index!"); |
| 1346 | |
| 1347 | return reinterpret_cast<const Elf_Shdr *>( |
| 1348 | reinterpret_cast<const char *>(SectionHeaderTable) |
| 1349 | + (index * Header->e_shentsize)); |
| 1350 | } |
| 1351 | |
| 1352 | template<support::endianness target_endianness, bool is64Bits> |
| 1353 | const char *ELFObjectFile<target_endianness, is64Bits> |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1354 | ::getString(uint32_t section, |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1355 | ELF::Elf32_Word offset) const { |
| 1356 | return getString(getSection(section), offset); |
| 1357 | } |
| 1358 | |
| 1359 | template<support::endianness target_endianness, bool is64Bits> |
| 1360 | const char *ELFObjectFile<target_endianness, is64Bits> |
| 1361 | ::getString(const Elf_Shdr *section, |
| 1362 | ELF::Elf32_Word offset) const { |
| 1363 | assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!"); |
| 1364 | if (offset >= section->sh_size) |
| 1365 | // FIXME: Proper error handling. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1366 | report_fatal_error("Symbol name offset outside of string table!"); |
| 1367 | return (const char *)base() + section->sh_offset + offset; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1370 | template<support::endianness target_endianness, bool is64Bits> |
| 1371 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 1372 | ::getSymbolName(const Elf_Sym *symb, |
| 1373 | StringRef &Result) const { |
| 1374 | if (symb->st_name == 0) { |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1375 | const Elf_Shdr *section = getSection(symb); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1376 | if (!section) |
| 1377 | Result = ""; |
| 1378 | else |
| 1379 | Result = getString(dot_shstrtab_sec, section->sh_name); |
| 1380 | return object_error::success; |
| 1381 | } |
| 1382 | |
| 1383 | // Use the default symbol table name section. |
| 1384 | Result = getString(dot_strtab_sec, symb->st_name); |
| 1385 | return object_error::success; |
| 1386 | } |
| 1387 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1388 | // EI_CLASS, EI_DATA. |
| 1389 | static std::pair<unsigned char, unsigned char> |
| 1390 | getElfArchType(MemoryBuffer *Object) { |
| 1391 | if (Object->getBufferSize() < ELF::EI_NIDENT) |
| 1392 | return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); |
| 1393 | return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS] |
| 1394 | , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]); |
| 1395 | } |
| 1396 | |
| 1397 | namespace llvm { |
| 1398 | |
| 1399 | ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { |
| 1400 | std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1401 | error_code ec; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1402 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1403 | return new ELFObjectFile<support::little, false>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1404 | else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1405 | return new ELFObjectFile<support::big, false>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1406 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1407 | return new ELFObjectFile<support::little, true>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1408 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1409 | return new ELFObjectFile<support::big, true>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1410 | // FIXME: Proper error handling. |
| 1411 | report_fatal_error("Not an ELF object file!"); |
| 1412 | } |
| 1413 | |
| 1414 | } // end namespace llvm |