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