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 | f77dea1 | 2011-10-13 03:30:21 +0000 | [diff] [blame] | 299 | DenseMap<const Elf_Sym*, ELF::Elf64_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; |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 328 | virtual error_code getSymbolFileOffset(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; |
Michael J. Spencer | c38c36a | 2011-10-17 23:54:22 +0000 | [diff] [blame] | 334 | virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const; |
Michael J. Spencer | 1130a79 | 2011-10-17 20:19:29 +0000 | [diff] [blame] | 335 | virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const; |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 336 | virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const; |
| 337 | virtual error_code getSymbolSection(DataRefImpl Symb, |
| 338 | section_iterator &Res) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 339 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 340 | virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; |
| 341 | virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; |
| 342 | virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; |
| 343 | virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; |
| 344 | virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 345 | virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 346 | virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 347 | virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; |
| 348 | virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 349 | virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, |
| 350 | bool &Result) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 351 | virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; |
| 352 | virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 353 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 354 | virtual error_code getRelocationNext(DataRefImpl Rel, |
| 355 | RelocationRef &Res) const; |
| 356 | virtual error_code getRelocationAddress(DataRefImpl Rel, |
| 357 | uint64_t &Res) const; |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 358 | virtual error_code getRelocationOffset(DataRefImpl Rel, |
| 359 | uint64_t &Res) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 360 | virtual error_code getRelocationSymbol(DataRefImpl Rel, |
| 361 | SymbolRef &Res) const; |
| 362 | virtual error_code getRelocationType(DataRefImpl Rel, |
Owen Anderson | 9472b8d | 2011-10-26 17:08:49 +0000 | [diff] [blame] | 363 | uint64_t &Res) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 364 | virtual error_code getRelocationTypeName(DataRefImpl Rel, |
| 365 | SmallVectorImpl<char> &Result) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 366 | virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, |
| 367 | int64_t &Res) const; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 368 | virtual error_code getRelocationValueString(DataRefImpl Rel, |
| 369 | SmallVectorImpl<char> &Result) const; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 370 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 371 | public: |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 372 | ELFObjectFile(MemoryBuffer *Object, error_code &ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 373 | virtual symbol_iterator begin_symbols() const; |
| 374 | virtual symbol_iterator end_symbols() const; |
| 375 | virtual section_iterator begin_sections() const; |
| 376 | virtual section_iterator end_sections() const; |
| 377 | |
| 378 | virtual uint8_t getBytesInAddress() const; |
| 379 | virtual StringRef getFileFormatName() const; |
| 380 | virtual unsigned getArch() const; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 381 | |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 382 | uint64_t getNumSections() const; |
| 383 | uint64_t getStringTableIndex() const; |
Nick Lewycky | f77dea1 | 2011-10-13 03:30:21 +0000 | [diff] [blame] | 384 | ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 385 | const Elf_Shdr *getSection(const Elf_Sym *symb) const; |
Michael J. Spencer | ab6bcf3 | 2011-10-17 23:53:37 +0000 | [diff] [blame] | 386 | |
| 387 | static inline bool classof(const Binary *v) { |
| 388 | return v->getType() == isELF; |
| 389 | } |
| 390 | static inline bool classof(const ELFObjectFile *v) { return true; } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 391 | }; |
| 392 | } // end namespace |
| 393 | |
| 394 | template<support::endianness target_endianness, bool is64Bits> |
| 395 | void ELFObjectFile<target_endianness, is64Bits> |
| 396 | ::validateSymbol(DataRefImpl Symb) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 397 | const Elf_Sym *symb = getSymbol(Symb); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 398 | const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 399 | // FIXME: We really need to do proper error handling in the case of an invalid |
| 400 | // input file. Because we don't use exceptions, I think we'll just pass |
| 401 | // an error object around. |
| 402 | if (!( symb |
| 403 | && SymbolTableSection |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 404 | && symb >= (const Elf_Sym*)(base() |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 405 | + SymbolTableSection->sh_offset) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 406 | && symb < (const Elf_Sym*)(base() |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 407 | + SymbolTableSection->sh_offset |
| 408 | + SymbolTableSection->sh_size))) |
| 409 | // FIXME: Proper error handling. |
| 410 | report_fatal_error("Symb must point to a valid symbol!"); |
| 411 | } |
| 412 | |
| 413 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 414 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 415 | ::getSymbolNext(DataRefImpl Symb, |
| 416 | SymbolRef &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 417 | validateSymbol(Symb); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 418 | const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 419 | |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 420 | ++Symb.d.a; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 421 | // 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] | 422 | if (Symb.d.a >= SymbolTableSection->getEntityCount()) { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 423 | // 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] | 424 | ++Symb.d.b; |
| 425 | Symb.d.a = 1; // The 0th symbol in ELF is fake. |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 426 | // Otherwise return the terminator. |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 427 | if (Symb.d.b >= SymbolTableSections.size()) { |
| 428 | Symb.d.a = std::numeric_limits<uint32_t>::max(); |
| 429 | Symb.d.b = std::numeric_limits<uint32_t>::max(); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 433 | Result = SymbolRef(Symb, this); |
| 434 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 438 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 439 | ::getSymbolName(DataRefImpl Symb, |
| 440 | StringRef &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 441 | validateSymbol(Symb); |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 442 | const Elf_Sym *symb = getSymbol(Symb); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 443 | return getSymbolName(symb, Result); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | template<support::endianness target_endianness, bool is64Bits> |
Nick Lewycky | f77dea1 | 2011-10-13 03:30:21 +0000 | [diff] [blame] | 447 | ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits> |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 448 | ::getSymbolTableIndex(const Elf_Sym *symb) const { |
| 449 | if (symb->st_shndx == ELF::SHN_XINDEX) |
| 450 | return ExtendedSymbolTable.lookup(symb); |
| 451 | return symb->st_shndx; |
| 452 | } |
| 453 | |
| 454 | template<support::endianness target_endianness, bool is64Bits> |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 455 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * |
| 456 | ELFObjectFile<target_endianness, is64Bits> |
| 457 | ::getSection(const Elf_Sym *symb) const { |
Nick Lewycky | f77dea1 | 2011-10-13 03:30:21 +0000 | [diff] [blame] | 458 | if (symb->st_shndx == ELF::SHN_XINDEX) |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 459 | return getSection(ExtendedSymbolTable.lookup(symb)); |
| 460 | if (symb->st_shndx >= ELF::SHN_LORESERVE) |
| 461 | return 0; |
| 462 | return getSection(symb->st_shndx); |
| 463 | } |
| 464 | |
| 465 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 466 | error_code ELFObjectFile<target_endianness, is64Bits> |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 467 | ::getSymbolFileOffset(DataRefImpl Symb, |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 468 | uint64_t &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 469 | validateSymbol(Symb); |
| 470 | const Elf_Sym *symb = getSymbol(Symb); |
| 471 | const Elf_Shdr *Section; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 472 | switch (getSymbolTableIndex(symb)) { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 473 | case ELF::SHN_COMMON: |
| 474 | // Undefined symbols have no address yet. |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 475 | case ELF::SHN_UNDEF: |
| 476 | Result = UnknownAddressOrSize; |
| 477 | return object_error::success; |
| 478 | case ELF::SHN_ABS: |
| 479 | Result = symb->st_value; |
| 480 | return object_error::success; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 481 | default: Section = getSection(symb); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | switch (symb->getType()) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 485 | case ELF::STT_SECTION: |
| 486 | Result = Section ? Section->sh_addr : UnknownAddressOrSize; |
| 487 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 488 | case ELF::STT_FUNC: |
| 489 | case ELF::STT_OBJECT: |
| 490 | case ELF::STT_NOTYPE: |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 491 | Result = symb->st_value + |
| 492 | (Section ? Section->sh_offset - Section->sh_addr : 0); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 493 | return object_error::success; |
| 494 | default: |
| 495 | Result = UnknownAddressOrSize; |
| 496 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
| 500 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 501 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 502 | ::getSymbolAddress(DataRefImpl Symb, |
| 503 | uint64_t &Result) const { |
| 504 | validateSymbol(Symb); |
| 505 | const Elf_Sym *symb = getSymbol(Symb); |
| 506 | const Elf_Shdr *Section; |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 507 | switch (getSymbolTableIndex(symb)) { |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 508 | case ELF::SHN_COMMON: |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 509 | // Undefined symbols have no address yet. |
| 510 | case ELF::SHN_UNDEF: |
| 511 | Result = UnknownAddressOrSize; |
| 512 | return object_error::success; |
| 513 | case ELF::SHN_ABS: |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 514 | Result = symb->st_value; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 515 | return object_error::success; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 516 | default: Section = getSection(symb); |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 517 | } |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 518 | |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 519 | switch (symb->getType()) { |
| 520 | case ELF::STT_SECTION: |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 521 | Result = Section ? Section->sh_addr : UnknownAddressOrSize; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 522 | return object_error::success; |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 523 | case ELF::STT_FUNC: |
| 524 | case ELF::STT_OBJECT: |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 525 | case ELF::STT_NOTYPE: |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 526 | Result = symb->st_value; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 527 | return object_error::success; |
| 528 | default: |
| 529 | Result = UnknownAddressOrSize; |
| 530 | return object_error::success; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | template<support::endianness target_endianness, bool is64Bits> |
| 535 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 536 | ::getSymbolSize(DataRefImpl Symb, |
| 537 | uint64_t &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 538 | validateSymbol(Symb); |
| 539 | const Elf_Sym *symb = getSymbol(Symb); |
| 540 | if (symb->st_size == 0) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 541 | Result = UnknownAddressOrSize; |
| 542 | Result = symb->st_size; |
| 543 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 547 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 548 | ::getSymbolNMTypeChar(DataRefImpl Symb, |
| 549 | char &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 550 | validateSymbol(Symb); |
| 551 | const Elf_Sym *symb = getSymbol(Symb); |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 552 | const Elf_Shdr *Section = getSection(symb); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 553 | |
| 554 | char ret = '?'; |
| 555 | |
| 556 | if (Section) { |
| 557 | switch (Section->sh_type) { |
| 558 | case ELF::SHT_PROGBITS: |
| 559 | case ELF::SHT_DYNAMIC: |
| 560 | switch (Section->sh_flags) { |
| 561 | case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR): |
| 562 | ret = 't'; break; |
| 563 | case (ELF::SHF_ALLOC | ELF::SHF_WRITE): |
| 564 | ret = 'd'; break; |
| 565 | case ELF::SHF_ALLOC: |
| 566 | case (ELF::SHF_ALLOC | ELF::SHF_MERGE): |
| 567 | case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS): |
| 568 | ret = 'r'; break; |
| 569 | } |
| 570 | break; |
| 571 | case ELF::SHT_NOBITS: ret = 'b'; |
| 572 | } |
| 573 | } |
| 574 | |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 575 | switch (getSymbolTableIndex(symb)) { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 576 | case ELF::SHN_UNDEF: |
| 577 | if (ret == '?') |
| 578 | ret = 'U'; |
| 579 | break; |
| 580 | case ELF::SHN_ABS: ret = 'a'; break; |
| 581 | case ELF::SHN_COMMON: ret = 'c'; break; |
| 582 | } |
| 583 | |
| 584 | switch (symb->getBinding()) { |
| 585 | case ELF::STB_GLOBAL: ret = ::toupper(ret); break; |
| 586 | case ELF::STB_WEAK: |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 587 | if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 588 | ret = 'w'; |
| 589 | else |
| 590 | if (symb->getType() == ELF::STT_OBJECT) |
| 591 | ret = 'V'; |
| 592 | else |
| 593 | ret = 'W'; |
| 594 | } |
| 595 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 596 | if (ret == '?' && symb->getType() == ELF::STT_SECTION) { |
| 597 | StringRef name; |
| 598 | if (error_code ec = getSymbolName(Symb, name)) |
| 599 | return ec; |
| 600 | Result = StringSwitch<char>(name) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 601 | .StartsWith(".debug", 'N') |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 602 | .StartsWith(".note", 'n') |
| 603 | .Default('?'); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 604 | return object_error::success; |
| 605 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 606 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 607 | Result = ret; |
| 608 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 612 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 613 | ::getSymbolType(DataRefImpl Symb, |
Michael J. Spencer | 1130a79 | 2011-10-17 20:19:29 +0000 | [diff] [blame] | 614 | SymbolRef::Type &Result) const { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 615 | validateSymbol(Symb); |
| 616 | const Elf_Sym *symb = getSymbol(Symb); |
| 617 | |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 618 | if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) { |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 619 | Result = SymbolRef::ST_External; |
| 620 | return object_error::success; |
| 621 | } |
| 622 | |
| 623 | switch (symb->getType()) { |
Michael J. Spencer | 206d17c | 2011-10-17 23:55:06 +0000 | [diff] [blame] | 624 | case ELF::STT_SECTION: |
| 625 | Result = SymbolRef::ST_Debug; |
| 626 | break; |
| 627 | case ELF::STT_FILE: |
| 628 | Result = SymbolRef::ST_File; |
| 629 | break; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 630 | case ELF::STT_FUNC: |
| 631 | Result = SymbolRef::ST_Function; |
| 632 | break; |
| 633 | case ELF::STT_OBJECT: |
| 634 | Result = SymbolRef::ST_Data; |
| 635 | break; |
| 636 | default: |
| 637 | Result = SymbolRef::ST_Other; |
| 638 | break; |
| 639 | } |
| 640 | return object_error::success; |
| 641 | } |
| 642 | |
| 643 | template<support::endianness target_endianness, bool is64Bits> |
| 644 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 645 | ::isSymbolGlobal(DataRefImpl Symb, |
| 646 | bool &Result) const { |
| 647 | validateSymbol(Symb); |
| 648 | const Elf_Sym *symb = getSymbol(Symb); |
| 649 | |
| 650 | Result = symb->getBinding() == ELF::STB_GLOBAL; |
| 651 | return object_error::success; |
| 652 | } |
| 653 | |
| 654 | template<support::endianness target_endianness, bool is64Bits> |
| 655 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | c38c36a | 2011-10-17 23:54:22 +0000 | [diff] [blame] | 656 | ::isSymbolWeak(DataRefImpl Symb, |
| 657 | bool &Result) const { |
| 658 | validateSymbol(Symb); |
| 659 | const Elf_Sym *symb = getSymbol(Symb); |
| 660 | |
| 661 | Result = symb->getBinding() == ELF::STB_WEAK; |
| 662 | return object_error::success; |
| 663 | } |
| 664 | |
| 665 | template<support::endianness target_endianness, bool is64Bits> |
| 666 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 667 | ::isSymbolAbsolute(DataRefImpl Symb, bool &Res) const { |
| 668 | validateSymbol(Symb); |
| 669 | const Elf_Sym *symb = getSymbol(Symb); |
| 670 | Res = symb->st_shndx == ELF::SHN_ABS; |
| 671 | return object_error::success; |
| 672 | } |
| 673 | |
| 674 | template<support::endianness target_endianness, bool is64Bits> |
| 675 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 676 | ::getSymbolSection(DataRefImpl Symb, |
| 677 | section_iterator &Res) const { |
| 678 | validateSymbol(Symb); |
| 679 | const Elf_Sym *symb = getSymbol(Symb); |
| 680 | const Elf_Shdr *sec = getSection(symb); |
| 681 | if (!sec) |
| 682 | Res = end_sections(); |
| 683 | else { |
| 684 | DataRefImpl Sec; |
| 685 | Sec.p = reinterpret_cast<intptr_t>(sec); |
| 686 | Res = section_iterator(SectionRef(Sec, this)); |
| 687 | } |
| 688 | return object_error::success; |
| 689 | } |
| 690 | |
| 691 | template<support::endianness target_endianness, bool is64Bits> |
| 692 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 693 | ::isSymbolInternal(DataRefImpl Symb, |
| 694 | bool &Result) const { |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 695 | validateSymbol(Symb); |
| 696 | const Elf_Sym *symb = getSymbol(Symb); |
| 697 | |
| 698 | if ( symb->getType() == ELF::STT_FILE |
| 699 | || symb->getType() == ELF::STT_SECTION) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 700 | Result = true; |
| 701 | Result = false; |
| 702 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 706 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 707 | ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 708 | const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 709 | sec += Header->e_shentsize; |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 710 | Sec.p = reinterpret_cast<intptr_t>(sec); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 711 | Result = SectionRef(Sec, this); |
| 712 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 716 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 717 | ::getSectionName(DataRefImpl Sec, |
| 718 | StringRef &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 719 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 720 | Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name)); |
| 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> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 725 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 726 | ::getSectionAddress(DataRefImpl Sec, |
| 727 | uint64_t &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 728 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 729 | Result = sec->sh_addr; |
| 730 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 734 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 735 | ::getSectionSize(DataRefImpl Sec, |
| 736 | uint64_t &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 737 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 738 | Result = sec->sh_size; |
| 739 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 743 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 744 | ::getSectionContents(DataRefImpl Sec, |
| 745 | StringRef &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 746 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 747 | const char *start = (const char*)base() + sec->sh_offset; |
| 748 | Result = StringRef(start, sec->sh_size); |
| 749 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 753 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | e2f2f07 | 2011-10-10 21:55:43 +0000 | [diff] [blame] | 754 | ::getSectionAlignment(DataRefImpl Sec, |
| 755 | uint64_t &Result) const { |
| 756 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 757 | Result = sec->sh_addralign; |
| 758 | return object_error::success; |
| 759 | } |
| 760 | |
| 761 | template<support::endianness target_endianness, bool is64Bits> |
| 762 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 763 | ::isSectionText(DataRefImpl Sec, |
| 764 | bool &Result) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 765 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 766 | if (sec->sh_flags & ELF::SHF_EXECINSTR) |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 767 | Result = true; |
| 768 | else |
| 769 | Result = false; |
| 770 | return object_error::success; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | template<support::endianness target_endianness, bool is64Bits> |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 774 | error_code ELFObjectFile<target_endianness, is64Bits> |
Michael J. Spencer | 13afc5e | 2011-09-28 20:57:30 +0000 | [diff] [blame] | 775 | ::isSectionData(DataRefImpl Sec, |
| 776 | bool &Result) const { |
| 777 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 778 | if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) |
| 779 | && sec->sh_type == ELF::SHT_PROGBITS) |
| 780 | Result = true; |
| 781 | else |
| 782 | Result = false; |
| 783 | return object_error::success; |
| 784 | } |
| 785 | |
| 786 | template<support::endianness target_endianness, bool is64Bits> |
| 787 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 788 | ::isSectionBSS(DataRefImpl Sec, |
| 789 | bool &Result) const { |
| 790 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 791 | if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) |
| 792 | && sec->sh_type == ELF::SHT_NOBITS) |
| 793 | Result = true; |
| 794 | else |
| 795 | Result = false; |
| 796 | return object_error::success; |
| 797 | } |
| 798 | |
| 799 | template<support::endianness target_endianness, bool is64Bits> |
| 800 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 801 | ::sectionContainsSymbol(DataRefImpl Sec, |
| 802 | DataRefImpl Symb, |
| 803 | bool &Result) const { |
| 804 | // FIXME: Unimplemented. |
| 805 | Result = false; |
| 806 | return object_error::success; |
| 807 | } |
| 808 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 809 | template<support::endianness target_endianness, bool is64Bits> |
| 810 | relocation_iterator ELFObjectFile<target_endianness, is64Bits> |
| 811 | ::getSectionRelBegin(DataRefImpl Sec) const { |
| 812 | DataRefImpl RelData; |
| 813 | memset(&RelData, 0, sizeof(RelData)); |
| 814 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 815 | typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); |
| 816 | if (sec != 0 && ittr != SectionRelocMap.end()) { |
Michael J. Spencer | 63b2f8c | 2011-10-13 22:30:10 +0000 | [diff] [blame] | 817 | RelData.w.a = getSection(ittr->second[0])->sh_info; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 818 | RelData.w.b = ittr->second[0]; |
| 819 | RelData.w.c = 0; |
| 820 | } |
| 821 | return relocation_iterator(RelocationRef(RelData, this)); |
| 822 | } |
| 823 | |
| 824 | template<support::endianness target_endianness, bool is64Bits> |
| 825 | relocation_iterator ELFObjectFile<target_endianness, is64Bits> |
| 826 | ::getSectionRelEnd(DataRefImpl Sec) const { |
| 827 | DataRefImpl RelData; |
| 828 | memset(&RelData, 0, sizeof(RelData)); |
| 829 | const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 830 | typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); |
| 831 | if (sec != 0 && ittr != SectionRelocMap.end()) { |
| 832 | // Get the index of the last relocation section for this section. |
| 833 | std::size_t relocsecindex = ittr->second[ittr->second.size() - 1]; |
| 834 | const Elf_Shdr *relocsec = getSection(relocsecindex); |
Michael J. Spencer | 63b2f8c | 2011-10-13 22:30:10 +0000 | [diff] [blame] | 835 | RelData.w.a = relocsec->sh_info; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 836 | RelData.w.b = relocsecindex; |
| 837 | RelData.w.c = relocsec->sh_size / relocsec->sh_entsize; |
| 838 | } |
| 839 | return relocation_iterator(RelocationRef(RelData, this)); |
| 840 | } |
| 841 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 842 | // Relocations |
| 843 | template<support::endianness target_endianness, bool is64Bits> |
| 844 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 845 | ::getRelocationNext(DataRefImpl Rel, |
| 846 | RelocationRef &Result) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 847 | ++Rel.w.c; |
| 848 | const Elf_Shdr *relocsec = getSection(Rel.w.b); |
| 849 | if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) { |
| 850 | // We have reached the end of the relocations for this section. See if there |
| 851 | // is another relocation section. |
Michael J. Spencer | 01a4db3 | 2011-10-07 19:46:12 +0000 | [diff] [blame] | 852 | typename RelocMap_t::mapped_type relocseclist = |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 853 | SectionRelocMap.lookup(getSection(Rel.w.a)); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 854 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 855 | // Do a binary search for the current reloc section index (which must be |
| 856 | // present). Then get the next one. |
| 857 | typename RelocMap_t::mapped_type::const_iterator loc = |
| 858 | std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b); |
| 859 | ++loc; |
| 860 | |
| 861 | // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel |
| 862 | // to the end iterator. |
| 863 | if (loc != relocseclist.end()) { |
| 864 | Rel.w.b = *loc; |
| 865 | Rel.w.a = 0; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 868 | Result = RelocationRef(Rel, this); |
| 869 | return object_error::success; |
| 870 | } |
| 871 | |
| 872 | template<support::endianness target_endianness, bool is64Bits> |
| 873 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 874 | ::getRelocationSymbol(DataRefImpl Rel, |
| 875 | SymbolRef &Result) const { |
| 876 | uint32_t symbolIdx; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 877 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 878 | switch (sec->sh_type) { |
| 879 | default : |
| 880 | report_fatal_error("Invalid section type in Rel!"); |
| 881 | case ELF::SHT_REL : { |
| 882 | symbolIdx = getRel(Rel)->getSymbol(); |
| 883 | break; |
| 884 | } |
| 885 | case ELF::SHT_RELA : { |
| 886 | symbolIdx = getRela(Rel)->getSymbol(); |
| 887 | break; |
| 888 | } |
| 889 | } |
| 890 | DataRefImpl SymbolData; |
| 891 | IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link); |
| 892 | if (it == SymbolTableSectionsIndexMap.end()) |
| 893 | report_fatal_error("Relocation symbol table not found!"); |
| 894 | SymbolData.d.a = symbolIdx; |
| 895 | SymbolData.d.b = it->second; |
| 896 | Result = SymbolRef(SymbolData, this); |
| 897 | return object_error::success; |
| 898 | } |
| 899 | |
| 900 | template<support::endianness target_endianness, bool is64Bits> |
| 901 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 902 | ::getRelocationAddress(DataRefImpl Rel, |
| 903 | uint64_t &Result) const { |
| 904 | uint64_t offset; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 905 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 906 | switch (sec->sh_type) { |
| 907 | default : |
| 908 | report_fatal_error("Invalid section type in Rel!"); |
| 909 | case ELF::SHT_REL : { |
| 910 | offset = getRel(Rel)->r_offset; |
| 911 | break; |
| 912 | } |
| 913 | case ELF::SHT_RELA : { |
| 914 | offset = getRela(Rel)->r_offset; |
| 915 | break; |
| 916 | } |
| 917 | } |
| 918 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 919 | Result = offset; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 920 | return object_error::success; |
| 921 | } |
| 922 | |
| 923 | template<support::endianness target_endianness, bool is64Bits> |
| 924 | error_code ELFObjectFile<target_endianness, is64Bits> |
Danil Malyshev | 9b24738 | 2011-11-27 10:12:52 +0000 | [diff] [blame] | 925 | ::getRelocationOffset(DataRefImpl Rel, |
| 926 | uint64_t &Result) const { |
| 927 | uint64_t offset; |
| 928 | const Elf_Shdr *sec = getSection(Rel.w.b); |
| 929 | switch (sec->sh_type) { |
| 930 | default : |
| 931 | report_fatal_error("Invalid section type in Rel!"); |
| 932 | case ELF::SHT_REL : { |
| 933 | offset = getRel(Rel)->r_offset; |
| 934 | break; |
| 935 | } |
| 936 | case ELF::SHT_RELA : { |
| 937 | offset = getRela(Rel)->r_offset; |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | Result = offset - sec->sh_addr; |
| 943 | return object_error::success; |
| 944 | } |
| 945 | |
| 946 | template<support::endianness target_endianness, bool is64Bits> |
| 947 | error_code ELFObjectFile<target_endianness, is64Bits> |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 948 | ::getRelocationType(DataRefImpl Rel, |
Owen Anderson | 9472b8d | 2011-10-26 17:08:49 +0000 | [diff] [blame] | 949 | uint64_t &Result) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 950 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 951 | switch (sec->sh_type) { |
| 952 | default : |
| 953 | report_fatal_error("Invalid section type in Rel!"); |
| 954 | case ELF::SHT_REL : { |
| 955 | Result = getRel(Rel)->getType(); |
| 956 | break; |
| 957 | } |
| 958 | case ELF::SHT_RELA : { |
| 959 | Result = getRela(Rel)->getType(); |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | return object_error::success; |
| 964 | } |
| 965 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 966 | #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \ |
| 967 | case ELF::enum: res = #enum; break; |
| 968 | |
| 969 | template<support::endianness target_endianness, bool is64Bits> |
| 970 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 971 | ::getRelocationTypeName(DataRefImpl Rel, |
| 972 | SmallVectorImpl<char> &Result) const { |
| 973 | const Elf_Shdr *sec = getSection(Rel.w.b); |
| 974 | uint8_t type; |
| 975 | StringRef res; |
| 976 | switch (sec->sh_type) { |
| 977 | default : |
| 978 | return object_error::parse_failed; |
| 979 | case ELF::SHT_REL : { |
| 980 | type = getRel(Rel)->getType(); |
| 981 | break; |
| 982 | } |
| 983 | case ELF::SHT_RELA : { |
| 984 | type = getRela(Rel)->getType(); |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | switch (Header->e_machine) { |
| 989 | case ELF::EM_X86_64: |
| 990 | switch (type) { |
| 991 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE); |
| 992 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64); |
| 993 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32); |
| 994 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32); |
| 995 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32); |
| 996 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY); |
| 997 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT); |
| 998 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT); |
| 999 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE); |
| 1000 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL); |
| 1001 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32); |
| 1002 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S); |
| 1003 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16); |
| 1004 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16); |
| 1005 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8); |
| 1006 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8); |
| 1007 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64); |
| 1008 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64); |
| 1009 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64); |
| 1010 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD); |
| 1011 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD); |
| 1012 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32); |
| 1013 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF); |
| 1014 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32); |
| 1015 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64); |
| 1016 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64); |
| 1017 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32); |
| 1018 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32); |
| 1019 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64); |
| 1020 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC); |
| 1021 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL); |
| 1022 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC); |
| 1023 | default: |
| 1024 | res = "Unknown"; |
| 1025 | } |
| 1026 | break; |
| 1027 | case ELF::EM_386: |
| 1028 | switch (type) { |
| 1029 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE); |
| 1030 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32); |
| 1031 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32); |
| 1032 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32); |
| 1033 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32); |
| 1034 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY); |
| 1035 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT); |
| 1036 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT); |
| 1037 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE); |
| 1038 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF); |
| 1039 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC); |
| 1040 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT); |
| 1041 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF); |
| 1042 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE); |
| 1043 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE); |
| 1044 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE); |
| 1045 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD); |
| 1046 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM); |
| 1047 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16); |
| 1048 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16); |
| 1049 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8); |
| 1050 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8); |
| 1051 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32); |
| 1052 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH); |
| 1053 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL); |
| 1054 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP); |
| 1055 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32); |
| 1056 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH); |
| 1057 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL); |
| 1058 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP); |
| 1059 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32); |
| 1060 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32); |
| 1061 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32); |
| 1062 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32); |
| 1063 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32); |
| 1064 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32); |
| 1065 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC); |
| 1066 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL); |
| 1067 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC); |
| 1068 | LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE); |
| 1069 | default: |
| 1070 | res = "Unknown"; |
| 1071 | } |
| 1072 | break; |
| 1073 | default: |
| 1074 | res = "Unknown"; |
| 1075 | } |
| 1076 | Result.append(res.begin(), res.end()); |
| 1077 | return object_error::success; |
| 1078 | } |
| 1079 | |
| 1080 | #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME |
| 1081 | |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1082 | template<support::endianness target_endianness, bool is64Bits> |
| 1083 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 1084 | ::getRelocationAdditionalInfo(DataRefImpl Rel, |
| 1085 | int64_t &Result) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1086 | const Elf_Shdr *sec = getSection(Rel.w.b); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1087 | switch (sec->sh_type) { |
| 1088 | default : |
| 1089 | report_fatal_error("Invalid section type in Rel!"); |
| 1090 | case ELF::SHT_REL : { |
| 1091 | Result = 0; |
| 1092 | return object_error::success; |
| 1093 | } |
| 1094 | case ELF::SHT_RELA : { |
| 1095 | Result = getRela(Rel)->r_addend; |
| 1096 | return object_error::success; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1101 | template<support::endianness target_endianness, bool is64Bits> |
| 1102 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 1103 | ::getRelocationValueString(DataRefImpl Rel, |
| 1104 | SmallVectorImpl<char> &Result) const { |
| 1105 | const Elf_Shdr *sec = getSection(Rel.w.b); |
| 1106 | uint8_t type; |
| 1107 | StringRef res; |
| 1108 | int64_t addend = 0; |
| 1109 | uint16_t symbol_index = 0; |
| 1110 | switch (sec->sh_type) { |
| 1111 | default : |
| 1112 | return object_error::parse_failed; |
| 1113 | case ELF::SHT_REL : { |
| 1114 | type = getRel(Rel)->getType(); |
| 1115 | symbol_index = getRel(Rel)->getSymbol(); |
| 1116 | // TODO: Read implicit addend from section data. |
| 1117 | break; |
| 1118 | } |
| 1119 | case ELF::SHT_RELA : { |
| 1120 | type = getRela(Rel)->getType(); |
| 1121 | symbol_index = getRela(Rel)->getSymbol(); |
| 1122 | addend = getRela(Rel)->r_addend; |
| 1123 | break; |
| 1124 | } |
| 1125 | } |
| 1126 | const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index); |
| 1127 | StringRef symname; |
| 1128 | if (error_code ec = getSymbolName(symb, symname)) |
| 1129 | return ec; |
| 1130 | switch (Header->e_machine) { |
| 1131 | case ELF::EM_X86_64: |
| 1132 | switch (type) { |
| 1133 | case ELF::R_X86_64_32S: |
| 1134 | res = symname; |
| 1135 | break; |
| 1136 | case ELF::R_X86_64_PC32: { |
| 1137 | std::string fmtbuf; |
| 1138 | raw_string_ostream fmt(fmtbuf); |
| 1139 | fmt << symname << (addend < 0 ? "" : "+") << addend << "-P"; |
| 1140 | fmt.flush(); |
| 1141 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 1142 | } |
| 1143 | break; |
| 1144 | default: |
| 1145 | res = "Unknown"; |
| 1146 | } |
| 1147 | break; |
| 1148 | default: |
| 1149 | res = "Unknown"; |
| 1150 | } |
| 1151 | if (Result.empty()) |
| 1152 | Result.append(res.begin(), res.end()); |
| 1153 | return object_error::success; |
| 1154 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1155 | |
Benjamin Kramer | 07ea23a | 2011-07-15 18:39:21 +0000 | [diff] [blame] | 1156 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1157 | ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object |
| 1158 | , error_code &ec) |
| 1159 | : ObjectFile(Binary::isELF, Object, ec) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1160 | , SectionHeaderTable(0) |
| 1161 | , dot_shstrtab_sec(0) |
| 1162 | , dot_strtab_sec(0) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1163 | Header = reinterpret_cast<const Elf_Ehdr *>(base()); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (Header->e_shoff == 0) |
| 1166 | return; |
| 1167 | |
| 1168 | SectionHeaderTable = |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1169 | reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff); |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1170 | uint64_t SectionTableSize = getNumSections() * Header->e_shentsize; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1171 | if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1172 | <= base() + Data->getBufferSize())) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1173 | // FIXME: Proper error handling. |
| 1174 | report_fatal_error("Section table goes past end of file!"); |
| 1175 | |
Nick Lewycky | fb05d3d | 2011-10-11 00:38:56 +0000 | [diff] [blame] | 1176 | |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 1177 | // To find the symbol tables we walk the section table to find SHT_SYMTAB. |
| 1178 | const Elf_Shdr* SymbolTableSectionHeaderIndex = 0; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1179 | const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable); |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1180 | for (uint64_t i = 0, e = getNumSections(); i != e; ++i) { |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 1181 | if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) { |
| 1182 | if (SymbolTableSectionHeaderIndex) |
| 1183 | // FIXME: Proper error handling. |
| 1184 | report_fatal_error("More than one .symtab_shndx!"); |
| 1185 | SymbolTableSectionHeaderIndex = sh; |
| 1186 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1187 | if (sh->sh_type == ELF::SHT_SYMTAB) { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1188 | SymbolTableSectionsIndexMap[i] = SymbolTableSections.size(); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1189 | SymbolTableSections.push_back(sh); |
| 1190 | } |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1191 | if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) { |
Michael J. Spencer | 63b2f8c | 2011-10-13 22:30:10 +0000 | [diff] [blame] | 1192 | SectionRelocMap[getSection(sh->sh_info)].push_back(i); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1193 | } |
| 1194 | ++sh; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1197 | // Sort section relocation lists by index. |
| 1198 | for (typename RelocMap_t::iterator i = SectionRelocMap.begin(), |
| 1199 | e = SectionRelocMap.end(); i != e; ++i) { |
| 1200 | std::sort(i->second.begin(), i->second.end()); |
| 1201 | } |
| 1202 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1203 | // Get string table sections. |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1204 | dot_shstrtab_sec = getSection(getStringTableIndex()); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1205 | if (dot_shstrtab_sec) { |
| 1206 | // 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] | 1207 | if (((const char*)base() + dot_shstrtab_sec->sh_offset) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1208 | [dot_shstrtab_sec->sh_size - 1] != 0) |
| 1209 | // FIXME: Proper error handling. |
| 1210 | report_fatal_error("String table must end with a null terminator!"); |
| 1211 | } |
| 1212 | |
| 1213 | // Merge this into the above loop. |
| 1214 | for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable), |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1215 | *e = i + getNumSections() * Header->e_shentsize; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1216 | i != e; i += Header->e_shentsize) { |
| 1217 | const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i); |
| 1218 | if (sh->sh_type == ELF::SHT_STRTAB) { |
| 1219 | StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name)); |
| 1220 | if (SectionName == ".strtab") { |
| 1221 | if (dot_strtab_sec != 0) |
| 1222 | // FIXME: Proper error handling. |
| 1223 | report_fatal_error("Already found section named .strtab!"); |
| 1224 | dot_strtab_sec = sh; |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1225 | const char *dot_strtab = (const char*)base() + sh->sh_offset; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1226 | if (dot_strtab[sh->sh_size - 1] != 0) |
| 1227 | // FIXME: Proper error handling. |
| 1228 | report_fatal_error("String table must end with a null terminator!"); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
Nick Lewycky | 15c3f72 | 2011-10-11 02:57:48 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Build symbol name side-mapping if there is one. |
| 1234 | if (SymbolTableSectionHeaderIndex) { |
| 1235 | const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() + |
| 1236 | SymbolTableSectionHeaderIndex->sh_offset); |
| 1237 | error_code ec; |
| 1238 | for (symbol_iterator si = begin_symbols(), |
| 1239 | se = end_symbols(); si != se; si.increment(ec)) { |
| 1240 | if (ec) |
| 1241 | report_fatal_error("Fewer extended symbol table entries than symbols!"); |
| 1242 | if (*ShndxTable != ELF::SHN_UNDEF) |
| 1243 | ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable; |
| 1244 | ++ShndxTable; |
| 1245 | } |
| 1246 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1250 | symbol_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1251 | ::begin_symbols() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1252 | DataRefImpl SymbolData; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1253 | memset(&SymbolData, 0, sizeof(SymbolData)); |
| 1254 | if (SymbolTableSections.size() == 0) { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1255 | SymbolData.d.a = std::numeric_limits<uint32_t>::max(); |
| 1256 | SymbolData.d.b = std::numeric_limits<uint32_t>::max(); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1257 | } else { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1258 | SymbolData.d.a = 1; // The 0th symbol in ELF is fake. |
| 1259 | SymbolData.d.b = 0; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1260 | } |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1261 | return symbol_iterator(SymbolRef(SymbolData, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1265 | symbol_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1266 | ::end_symbols() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1267 | DataRefImpl SymbolData; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1268 | memset(&SymbolData, 0, sizeof(SymbolData)); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1269 | SymbolData.d.a = std::numeric_limits<uint32_t>::max(); |
| 1270 | SymbolData.d.b = std::numeric_limits<uint32_t>::max(); |
| 1271 | return symbol_iterator(SymbolRef(SymbolData, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1275 | section_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1276 | ::begin_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1277 | DataRefImpl ret; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 1278 | memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1279 | ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1280 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | template<support::endianness target_endianness, bool is64Bits> |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1284 | section_iterator ELFObjectFile<target_endianness, is64Bits> |
| 1285 | ::end_sections() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1286 | DataRefImpl ret; |
Eric Christopher | 539d8d8 | 2011-04-03 22:53:19 +0000 | [diff] [blame] | 1287 | memset(&ret, 0, sizeof(DataRefImpl)); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1288 | ret.p = reinterpret_cast<intptr_t>(base() |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1289 | + Header->e_shoff |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1290 | + (Header->e_shentsize*getNumSections())); |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1291 | return section_iterator(SectionRef(ret, this)); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | template<support::endianness target_endianness, bool is64Bits> |
| 1295 | uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1296 | return is64Bits ? 8 : 4; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | template<support::endianness target_endianness, bool is64Bits> |
| 1300 | StringRef ELFObjectFile<target_endianness, is64Bits> |
| 1301 | ::getFileFormatName() const { |
| 1302 | switch(Header->e_ident[ELF::EI_CLASS]) { |
| 1303 | case ELF::ELFCLASS32: |
| 1304 | switch(Header->e_machine) { |
| 1305 | case ELF::EM_386: |
| 1306 | return "ELF32-i386"; |
| 1307 | case ELF::EM_X86_64: |
| 1308 | return "ELF32-x86-64"; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1309 | case ELF::EM_ARM: |
| 1310 | return "ELF32-arm"; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1311 | default: |
| 1312 | return "ELF32-unknown"; |
| 1313 | } |
| 1314 | case ELF::ELFCLASS64: |
| 1315 | switch(Header->e_machine) { |
| 1316 | case ELF::EM_386: |
| 1317 | return "ELF64-i386"; |
| 1318 | case ELF::EM_X86_64: |
| 1319 | return "ELF64-x86-64"; |
| 1320 | default: |
| 1321 | return "ELF64-unknown"; |
| 1322 | } |
| 1323 | default: |
| 1324 | // FIXME: Proper error handling. |
| 1325 | report_fatal_error("Invalid ELFCLASS!"); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | template<support::endianness target_endianness, bool is64Bits> |
| 1330 | unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const { |
| 1331 | switch(Header->e_machine) { |
| 1332 | case ELF::EM_386: |
| 1333 | return Triple::x86; |
| 1334 | case ELF::EM_X86_64: |
| 1335 | return Triple::x86_64; |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1336 | case ELF::EM_ARM: |
| 1337 | return Triple::arm; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1338 | default: |
| 1339 | return Triple::UnknownArch; |
| 1340 | } |
| 1341 | } |
| 1342 | |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1343 | template<support::endianness target_endianness, bool is64Bits> |
| 1344 | uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const { |
| 1345 | if (Header->e_shnum == ELF::SHN_UNDEF) |
| 1346 | return SectionHeaderTable->sh_size; |
| 1347 | return Header->e_shnum; |
| 1348 | } |
| 1349 | |
| 1350 | template<support::endianness target_endianness, bool is64Bits> |
| 1351 | uint64_t |
| 1352 | ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const { |
| 1353 | if (Header->e_shnum == ELF::SHN_UNDEF) { |
| 1354 | if (Header->e_shstrndx == ELF::SHN_HIRESERVE) |
| 1355 | return SectionHeaderTable->sh_link; |
| 1356 | if (Header->e_shstrndx >= getNumSections()) |
| 1357 | return 0; |
| 1358 | } |
| 1359 | return Header->e_shstrndx; |
| 1360 | } |
| 1361 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1362 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1363 | template<support::endianness target_endianness, bool is64Bits> |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1364 | template<typename T> |
| 1365 | inline const T * |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1366 | ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section, |
| 1367 | uint32_t Entry) const { |
| 1368 | return getEntry<T>(getSection(Section), Entry); |
| 1369 | } |
| 1370 | |
| 1371 | template<support::endianness target_endianness, bool is64Bits> |
| 1372 | template<typename T> |
| 1373 | inline const T * |
| 1374 | ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section, |
| 1375 | uint32_t Entry) const { |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1376 | return reinterpret_cast<const T *>( |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1377 | base() |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1378 | + Section->sh_offset |
| 1379 | + (Entry * Section->sh_entsize)); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | template<support::endianness target_endianness, bool is64Bits> |
| 1383 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym * |
| 1384 | ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1385 | return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | template<support::endianness target_endianness, bool is64Bits> |
| 1389 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel * |
| 1390 | ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1391 | return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c); |
Benjamin Kramer | 0fcab07 | 2011-09-08 20:52:17 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | template<support::endianness target_endianness, bool is64Bits> |
| 1395 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela * |
| 1396 | ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const { |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1397 | return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | template<support::endianness target_endianness, bool is64Bits> |
| 1401 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * |
| 1402 | ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const { |
Michael J. Spencer | 7acdb4d | 2011-01-21 02:27:02 +0000 | [diff] [blame] | 1403 | const Elf_Shdr *sec = getSection(Symb.d.b); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1404 | 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] | 1405 | // FIXME: Proper error handling. |
| 1406 | report_fatal_error("Invalid symbol table section!"); |
| 1407 | return sec; |
| 1408 | } |
| 1409 | |
| 1410 | template<support::endianness target_endianness, bool is64Bits> |
| 1411 | const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1412 | ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const { |
| 1413 | if (index == 0) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1414 | return 0; |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1415 | if (!SectionHeaderTable || index >= getNumSections()) |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1416 | // FIXME: Proper error handling. |
| 1417 | report_fatal_error("Invalid section index!"); |
| 1418 | |
| 1419 | return reinterpret_cast<const Elf_Shdr *>( |
| 1420 | reinterpret_cast<const char *>(SectionHeaderTable) |
| 1421 | + (index * Header->e_shentsize)); |
| 1422 | } |
| 1423 | |
| 1424 | template<support::endianness target_endianness, bool is64Bits> |
| 1425 | const char *ELFObjectFile<target_endianness, is64Bits> |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1426 | ::getString(uint32_t section, |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1427 | ELF::Elf32_Word offset) const { |
| 1428 | return getString(getSection(section), offset); |
| 1429 | } |
| 1430 | |
| 1431 | template<support::endianness target_endianness, bool is64Bits> |
| 1432 | const char *ELFObjectFile<target_endianness, is64Bits> |
| 1433 | ::getString(const Elf_Shdr *section, |
| 1434 | ELF::Elf32_Word offset) const { |
| 1435 | assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!"); |
| 1436 | if (offset >= section->sh_size) |
| 1437 | // FIXME: Proper error handling. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1438 | report_fatal_error("Symbol name offset outside of string table!"); |
| 1439 | return (const char *)base() + section->sh_offset + offset; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1442 | template<support::endianness target_endianness, bool is64Bits> |
| 1443 | error_code ELFObjectFile<target_endianness, is64Bits> |
| 1444 | ::getSymbolName(const Elf_Sym *symb, |
| 1445 | StringRef &Result) const { |
| 1446 | if (symb->st_name == 0) { |
Nick Lewycky | bfbbe32 | 2011-10-11 03:18:58 +0000 | [diff] [blame] | 1447 | const Elf_Shdr *section = getSection(symb); |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 1448 | if (!section) |
| 1449 | Result = ""; |
| 1450 | else |
| 1451 | Result = getString(dot_shstrtab_sec, section->sh_name); |
| 1452 | return object_error::success; |
| 1453 | } |
| 1454 | |
| 1455 | // Use the default symbol table name section. |
| 1456 | Result = getString(dot_strtab_sec, symb->st_name); |
| 1457 | return object_error::success; |
| 1458 | } |
| 1459 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1460 | // EI_CLASS, EI_DATA. |
| 1461 | static std::pair<unsigned char, unsigned char> |
| 1462 | getElfArchType(MemoryBuffer *Object) { |
| 1463 | if (Object->getBufferSize() < ELF::EI_NIDENT) |
| 1464 | return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); |
| 1465 | return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS] |
| 1466 | , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]); |
| 1467 | } |
| 1468 | |
| 1469 | namespace llvm { |
| 1470 | |
| 1471 | ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { |
| 1472 | std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1473 | error_code ec; |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1474 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1475 | return new ELFObjectFile<support::little, false>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1476 | else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1477 | return new ELFObjectFile<support::big, false>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1478 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1479 | return new ELFObjectFile<support::little, true>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1480 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 1481 | return new ELFObjectFile<support::big, true>(Object, ec); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1482 | // FIXME: Proper error handling. |
| 1483 | report_fatal_error("Not an ELF object file!"); |
| 1484 | } |
| 1485 | |
| 1486 | } // end namespace llvm |