1 //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file declares the ELFObjectFile template class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_OBJECT_ELF_H 15 #define LLVM_OBJECT_ELF_H 16 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/PointerIntPair.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/ADT/Triple.h" 22 #include "llvm/Object/ObjectFile.h" 23 #include "llvm/Support/Casting.h" 24 #include "llvm/Support/ELF.h" 25 #include "llvm/Support/Endian.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/MemoryBuffer.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <algorithm> 30 #include <limits> 31 #include <utility> 32 33 #include <ctype.h> 34 35 namespace llvm { 36 namespace object { 37 38 using support::endianness; 39 40 template<endianness target_endianness, std::size_t max_alignment, bool is64Bits> 41 struct ELFType { 42 static const endianness TargetEndianness = target_endianness; 43 static const std::size_t MaxAlignment = max_alignment; 44 static const bool Is64Bits = is64Bits; 45 }; 46 47 template<typename T, int max_align> 48 struct MaximumAlignment { 49 enum {value = AlignOf<T>::Alignment > max_align ? max_align 50 : AlignOf<T>::Alignment}; 51 }; 52 53 // Subclasses of ELFObjectFile may need this for template instantiation 54 inline std::pair<unsigned char, unsigned char> 55 getElfArchType(MemoryBuffer *Object) { 56 if (Object->getBufferSize() < ELF::EI_NIDENT) 57 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); 58 return std::make_pair((uint8_t) Object->getBufferStart()[ELF::EI_CLASS], 59 (uint8_t) Object->getBufferStart()[ELF::EI_DATA]); 60 } 61 62 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits. 63 template<endianness target_endianness, std::size_t max_alignment> 64 struct ELFDataTypeTypedefHelperCommon { 65 typedef support::detail::packed_endian_specific_integral 66 <uint16_t, target_endianness, 67 MaximumAlignment<uint16_t, max_alignment>::value> Elf_Half; 68 typedef support::detail::packed_endian_specific_integral 69 <uint32_t, target_endianness, 70 MaximumAlignment<uint32_t, max_alignment>::value> Elf_Word; 71 typedef support::detail::packed_endian_specific_integral 72 <int32_t, target_endianness, 73 MaximumAlignment<int32_t, max_alignment>::value> Elf_Sword; 74 typedef support::detail::packed_endian_specific_integral 75 <uint64_t, target_endianness, 76 MaximumAlignment<uint64_t, max_alignment>::value> Elf_Xword; 77 typedef support::detail::packed_endian_specific_integral 78 <int64_t, target_endianness, 79 MaximumAlignment<int64_t, max_alignment>::value> Elf_Sxword; 80 }; 81 82 template<class ELFT> 83 struct ELFDataTypeTypedefHelper; 84 85 /// ELF 32bit types. 86 template<endianness TargetEndianness, std::size_t MaxAlign> 87 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, MaxAlign, false> > 88 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> { 89 typedef uint32_t value_type; 90 typedef support::detail::packed_endian_specific_integral 91 <value_type, TargetEndianness, 92 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr; 93 typedef support::detail::packed_endian_specific_integral 94 <value_type, TargetEndianness, 95 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off; 96 }; 97 98 /// ELF 64bit types. 99 template<endianness TargetEndianness, std::size_t MaxAlign> 100 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, MaxAlign, true> > 101 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> { 102 typedef uint64_t value_type; 103 typedef support::detail::packed_endian_specific_integral 104 <value_type, TargetEndianness, 105 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr; 106 typedef support::detail::packed_endian_specific_integral 107 <value_type, TargetEndianness, 108 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off; 109 }; 110 111 // I really don't like doing this, but the alternative is copypasta. 112 #define LLVM_ELF_IMPORT_TYPES(E, M, W) \ 113 typedef typename ELFDataTypeTypedefHelper<ELFType<E,M,W> >::Elf_Addr Elf_Addr; \ 114 typedef typename ELFDataTypeTypedefHelper<ELFType<E,M,W> >::Elf_Off Elf_Off; \ 115 typedef typename ELFDataTypeTypedefHelper<ELFType<E,M,W> >::Elf_Half Elf_Half; \ 116 typedef typename ELFDataTypeTypedefHelper<ELFType<E,M,W> >::Elf_Word Elf_Word; \ 117 typedef typename \ 118 ELFDataTypeTypedefHelper<ELFType<E,M,W> >::Elf_Sword Elf_Sword; \ 119 typedef typename \ 120 ELFDataTypeTypedefHelper<ELFType<E,M,W> >::Elf_Xword Elf_Xword; \ 121 typedef typename \ 122 ELFDataTypeTypedefHelper<ELFType<E,M,W> >::Elf_Sxword Elf_Sxword; 123 124 #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \ 125 LLVM_ELF_IMPORT_TYPES(ELFT::TargetEndianness, ELFT::MaxAlignment, \ 126 ELFT::Is64Bits) 127 128 // Section header. 129 template<class ELFT> 130 struct Elf_Shdr_Base; 131 132 template<endianness TargetEndianness, std::size_t MaxAlign> 133 struct Elf_Shdr_Base<ELFType<TargetEndianness, MaxAlign, false> > { 134 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 135 Elf_Word sh_name; // Section name (index into string table) 136 Elf_Word sh_type; // Section type (SHT_*) 137 Elf_Word sh_flags; // Section flags (SHF_*) 138 Elf_Addr sh_addr; // Address where section is to be loaded 139 Elf_Off sh_offset; // File offset of section data, in bytes 140 Elf_Word sh_size; // Size of section, in bytes 141 Elf_Word sh_link; // Section type-specific header table index link 142 Elf_Word sh_info; // Section type-specific extra information 143 Elf_Word sh_addralign;// Section address alignment 144 Elf_Word sh_entsize; // Size of records contained within the section 145 }; 146 147 template<endianness TargetEndianness, std::size_t MaxAlign> 148 struct Elf_Shdr_Base<ELFType<TargetEndianness, MaxAlign, true> > { 149 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 150 Elf_Word sh_name; // Section name (index into string table) 151 Elf_Word sh_type; // Section type (SHT_*) 152 Elf_Xword sh_flags; // Section flags (SHF_*) 153 Elf_Addr sh_addr; // Address where section is to be loaded 154 Elf_Off sh_offset; // File offset of section data, in bytes 155 Elf_Xword sh_size; // Size of section, in bytes 156 Elf_Word sh_link; // Section type-specific header table index link 157 Elf_Word sh_info; // Section type-specific extra information 158 Elf_Xword sh_addralign;// Section address alignment 159 Elf_Xword sh_entsize; // Size of records contained within the section 160 }; 161 162 template<class ELFT> 163 struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> { 164 using Elf_Shdr_Base<ELFT>::sh_entsize; 165 using Elf_Shdr_Base<ELFT>::sh_size; 166 167 /// @brief Get the number of entities this section contains if it has any. 168 unsigned getEntityCount() const { 169 if (sh_entsize == 0) 170 return 0; 171 return sh_size / sh_entsize; 172 } 173 }; 174 175 template<class ELFT> 176 struct Elf_Sym_Base; 177 178 template<endianness TargetEndianness, std::size_t MaxAlign> 179 struct Elf_Sym_Base<ELFType<TargetEndianness, MaxAlign, false> > { 180 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 181 Elf_Word st_name; // Symbol name (index into string table) 182 Elf_Addr st_value; // Value or address associated with the symbol 183 Elf_Word st_size; // Size of the symbol 184 unsigned char st_info; // Symbol's type and binding attributes 185 unsigned char st_other; // Must be zero; reserved 186 Elf_Half st_shndx; // Which section (header table index) it's defined in 187 }; 188 189 template<endianness TargetEndianness, std::size_t MaxAlign> 190 struct Elf_Sym_Base<ELFType<TargetEndianness, MaxAlign, true> > { 191 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 192 Elf_Word st_name; // Symbol name (index into string table) 193 unsigned char st_info; // Symbol's type and binding attributes 194 unsigned char st_other; // Must be zero; reserved 195 Elf_Half st_shndx; // Which section (header table index) it's defined in 196 Elf_Addr st_value; // Value or address associated with the symbol 197 Elf_Xword st_size; // Size of the symbol 198 }; 199 200 template<class ELFT> 201 struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> { 202 using Elf_Sym_Base<ELFT>::st_info; 203 204 // These accessors and mutators correspond to the ELF32_ST_BIND, 205 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: 206 unsigned char getBinding() const { return st_info >> 4; } 207 unsigned char getType() const { return st_info & 0x0f; } 208 void setBinding(unsigned char b) { setBindingAndType(b, getType()); } 209 void setType(unsigned char t) { setBindingAndType(getBinding(), t); } 210 void setBindingAndType(unsigned char b, unsigned char t) { 211 st_info = (b << 4) + (t & 0x0f); 212 } 213 }; 214 215 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section 216 /// (.gnu.version). This structure is identical for ELF32 and ELF64. 217 template<class ELFT> 218 struct Elf_Versym_Impl { 219 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 220 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN) 221 }; 222 223 template<class ELFT> 224 struct Elf_Verdaux_Impl; 225 226 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section 227 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64. 228 template<class ELFT> 229 struct Elf_Verdef_Impl { 230 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 231 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux; 232 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT) 233 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*) 234 Elf_Half vd_ndx; // Version index, used in .gnu.version entries 235 Elf_Half vd_cnt; // Number of Verdaux entries 236 Elf_Word vd_hash; // Hash of name 237 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes) 238 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes) 239 240 /// Get the first Verdaux entry for this Verdef. 241 const Elf_Verdaux *getAux() const { 242 return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux); 243 } 244 }; 245 246 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef 247 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64. 248 template<class ELFT> 249 struct Elf_Verdaux_Impl { 250 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 251 Elf_Word vda_name; // Version name (offset in string table) 252 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes) 253 }; 254 255 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed 256 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. 257 template<class ELFT> 258 struct Elf_Verneed_Impl { 259 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 260 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT) 261 Elf_Half vn_cnt; // Number of associated Vernaux entries 262 Elf_Word vn_file; // Library name (string table offset) 263 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes) 264 Elf_Word vn_next; // Offset to next Verneed entry (in bytes) 265 }; 266 267 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed 268 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. 269 template<class ELFT> 270 struct Elf_Vernaux_Impl { 271 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 272 Elf_Word vna_hash; // Hash of dependency name 273 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*) 274 Elf_Half vna_other; // Version index, used in .gnu.version entries 275 Elf_Word vna_name; // Dependency name 276 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes) 277 }; 278 279 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic 280 /// table section (.dynamic) look like. 281 template<class ELFT> 282 struct Elf_Dyn_Base; 283 284 template<endianness TargetEndianness, std::size_t MaxAlign> 285 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, false> > { 286 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 287 Elf_Sword d_tag; 288 union { 289 Elf_Word d_val; 290 Elf_Addr d_ptr; 291 } d_un; 292 }; 293 294 template<endianness TargetEndianness, std::size_t MaxAlign> 295 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, true> > { 296 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 297 Elf_Sxword d_tag; 298 union { 299 Elf_Xword d_val; 300 Elf_Addr d_ptr; 301 } d_un; 302 }; 303 304 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters. 305 template<class ELFT> 306 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> { 307 using Elf_Dyn_Base<ELFT>::d_tag; 308 using Elf_Dyn_Base<ELFT>::d_un; 309 int64_t getTag() const { return d_tag; } 310 uint64_t getVal() const { return d_un.d_val; } 311 uint64_t getPtr() const { return d_un.ptr; } 312 }; 313 314 // Elf_Rel: Elf Relocation 315 template<class ELFT, bool isRela> 316 struct Elf_Rel_Base; 317 318 template<endianness TargetEndianness, std::size_t MaxAlign> 319 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, false> { 320 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 321 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 322 Elf_Word r_info; // Symbol table index and type of relocation to apply 323 324 uint32_t getRInfo(bool isMips64EL) const { 325 assert(!isMips64EL); 326 return r_info; 327 } 328 void setRInfo(uint32_t R) { 329 r_info = R; 330 } 331 }; 332 333 template<endianness TargetEndianness, std::size_t MaxAlign> 334 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, false> { 335 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 336 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 337 Elf_Xword r_info; // Symbol table index and type of relocation to apply 338 339 uint64_t getRInfo(bool isMips64EL) const { 340 uint64_t t = r_info; 341 if (!isMips64EL) 342 return t; 343 // Mips64 little endian has a "special" encoding of r_info. Instead of one 344 // 64 bit little endian number, it is a little endian 32 bit number followed 345 // by a 32 bit big endian number. 346 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | 347 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); 348 } 349 void setRInfo(uint64_t R) { 350 // FIXME: Add mips64el support. 351 r_info = R; 352 } 353 }; 354 355 template<endianness TargetEndianness, std::size_t MaxAlign> 356 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, true> { 357 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 358 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 359 Elf_Word r_info; // Symbol table index and type of relocation to apply 360 Elf_Sword r_addend; // Compute value for relocatable field by adding this 361 362 uint32_t getRInfo(bool isMips64EL) const { 363 assert(!isMips64EL); 364 return r_info; 365 } 366 void setRInfo(uint32_t R) { 367 r_info = R; 368 } 369 }; 370 371 template<endianness TargetEndianness, std::size_t MaxAlign> 372 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, true> { 373 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 374 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 375 Elf_Xword r_info; // Symbol table index and type of relocation to apply 376 Elf_Sxword r_addend; // Compute value for relocatable field by adding this. 377 378 uint64_t getRInfo(bool isMips64EL) const { 379 // Mips64 little endian has a "special" encoding of r_info. Instead of one 380 // 64 bit little endian number, it is a little endian 32 bit number followed 381 // by a 32 bit big endian number. 382 uint64_t t = r_info; 383 if (!isMips64EL) 384 return t; 385 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | 386 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); 387 } 388 void setRInfo(uint64_t R) { 389 // FIXME: Add mips64el support. 390 r_info = R; 391 } 392 }; 393 394 template<class ELFT, bool isRela> 395 struct Elf_Rel_Impl; 396 397 template<endianness TargetEndianness, std::size_t MaxAlign, bool isRela> 398 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, true>, isRela> 399 : Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, isRela> { 400 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 401 402 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, 403 // and ELF64_R_INFO macros defined in the ELF specification: 404 uint32_t getSymbol(bool isMips64EL) const { 405 return (uint32_t) (this->getRInfo(isMips64EL) >> 32); 406 } 407 uint32_t getType(bool isMips64EL) const { 408 return (uint32_t) (this->getRInfo(isMips64EL) & 0xffffffffL); 409 } 410 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); } 411 void setType(uint32_t t) { setSymbolAndType(getSymbol(), t); } 412 void setSymbolAndType(uint32_t s, uint32_t t) { 413 this->setRInfo(((uint64_t)s << 32) + (t&0xffffffffL)); 414 } 415 }; 416 417 template<endianness TargetEndianness, std::size_t MaxAlign, bool isRela> 418 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, false>, isRela> 419 : Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, isRela> { 420 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 421 422 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 423 // and ELF32_R_INFO macros defined in the ELF specification: 424 uint32_t getSymbol(bool isMips64EL) const { 425 return this->getRInfo(isMips64EL) >> 8; 426 } 427 unsigned char getType(bool isMips64EL) const { 428 return (unsigned char) (this->getRInfo(isMips64EL) & 0x0ff); 429 } 430 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); } 431 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } 432 void setSymbolAndType(uint32_t s, unsigned char t) { 433 this->setRInfo((s << 8) + t); 434 } 435 }; 436 437 template<class ELFT> 438 struct Elf_Ehdr_Impl { 439 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 440 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes 441 Elf_Half e_type; // Type of file (see ET_*) 442 Elf_Half e_machine; // Required architecture for this file (see EM_*) 443 Elf_Word e_version; // Must be equal to 1 444 Elf_Addr e_entry; // Address to jump to in order to start program 445 Elf_Off e_phoff; // Program header table's file offset, in bytes 446 Elf_Off e_shoff; // Section header table's file offset, in bytes 447 Elf_Word e_flags; // Processor-specific flags 448 Elf_Half e_ehsize; // Size of ELF header, in bytes 449 Elf_Half e_phentsize;// Size of an entry in the program header table 450 Elf_Half e_phnum; // Number of entries in the program header table 451 Elf_Half e_shentsize;// Size of an entry in the section header table 452 Elf_Half e_shnum; // Number of entries in the section header table 453 Elf_Half e_shstrndx; // Section header table index of section name 454 // string table 455 bool checkMagic() const { 456 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; 457 } 458 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } 459 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } 460 }; 461 462 template<class ELFT> 463 struct Elf_Phdr_Impl; 464 465 template<endianness TargetEndianness, std::size_t MaxAlign> 466 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, false> > { 467 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 468 Elf_Word p_type; // Type of segment 469 Elf_Off p_offset; // FileOffset where segment is located, in bytes 470 Elf_Addr p_vaddr; // Virtual Address of beginning of segment 471 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) 472 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero) 473 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero) 474 Elf_Word p_flags; // Segment flags 475 Elf_Word p_align; // Segment alignment constraint 476 }; 477 478 template<endianness TargetEndianness, std::size_t MaxAlign> 479 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, true> > { 480 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 481 Elf_Word p_type; // Type of segment 482 Elf_Word p_flags; // Segment flags 483 Elf_Off p_offset; // FileOffset where segment is located, in bytes 484 Elf_Addr p_vaddr; // Virtual Address of beginning of segment 485 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) 486 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero) 487 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero) 488 Elf_Xword p_align; // Segment alignment constraint 489 }; 490 491 template<class ELFT> 492 class ELFObjectFile : public ObjectFile { 493 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 494 495 public: 496 /// \brief Iterate over constant sized entities. 497 template<class EntT> 498 class ELFEntityIterator { 499 public: 500 typedef ptrdiff_t difference_type; 501 typedef EntT value_type; 502 typedef std::random_access_iterator_tag iterator_category; 503 typedef value_type &reference; 504 typedef value_type *pointer; 505 506 /// \brief Default construct iterator. 507 ELFEntityIterator() : EntitySize(0), Current(0) {} 508 ELFEntityIterator(uint64_t EntSize, const char *Start) 509 : EntitySize(EntSize) 510 , Current(Start) {} 511 512 reference operator *() { 513 assert(Current && "Attempted to dereference an invalid iterator!"); 514 return *reinterpret_cast<pointer>(Current); 515 } 516 517 pointer operator ->() { 518 assert(Current && "Attempted to dereference an invalid iterator!"); 519 return reinterpret_cast<pointer>(Current); 520 } 521 522 bool operator ==(const ELFEntityIterator &Other) { 523 return Current == Other.Current; 524 } 525 526 bool operator !=(const ELFEntityIterator &Other) { 527 return !(*this == Other); 528 } 529 530 ELFEntityIterator &operator ++() { 531 assert(Current && "Attempted to increment an invalid iterator!"); 532 Current += EntitySize; 533 return *this; 534 } 535 536 ELFEntityIterator operator ++(int) { 537 ELFEntityIterator Tmp = *this; 538 ++*this; 539 return Tmp; 540 } 541 542 ELFEntityIterator &operator =(const ELFEntityIterator &Other) { 543 EntitySize = Other.EntitySize; 544 Current = Other.Current; 545 return *this; 546 } 547 548 difference_type operator -(const ELFEntityIterator &Other) const { 549 assert(EntitySize == Other.EntitySize && 550 "Subtracting iterators of different EntitiySize!"); 551 return (Current - Other.Current) / EntitySize; 552 } 553 554 const char *get() const { return Current; } 555 556 private: 557 uint64_t EntitySize; 558 const char *Current; 559 }; 560 561 typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr; 562 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr; 563 typedef Elf_Sym_Impl<ELFT> Elf_Sym; 564 typedef Elf_Dyn_Impl<ELFT> Elf_Dyn; 565 typedef Elf_Phdr_Impl<ELFT> Elf_Phdr; 566 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel; 567 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela; 568 typedef Elf_Verdef_Impl<ELFT> Elf_Verdef; 569 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux; 570 typedef Elf_Verneed_Impl<ELFT> Elf_Verneed; 571 typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux; 572 typedef Elf_Versym_Impl<ELFT> Elf_Versym; 573 typedef ELFEntityIterator<const Elf_Dyn> Elf_Dyn_iterator; 574 typedef ELFEntityIterator<const Elf_Sym> Elf_Sym_iterator; 575 typedef ELFEntityIterator<const Elf_Rela> Elf_Rela_Iter; 576 typedef ELFEntityIterator<const Elf_Rel> Elf_Rel_Iter; 577 578 protected: 579 // This flag is used for classof, to distinguish ELFObjectFile from 580 // its subclass. If more subclasses will be created, this flag will 581 // have to become an enum. 582 bool isDyldELFObject; 583 584 private: 585 const Elf_Ehdr *Header; 586 const Elf_Shdr *SectionHeaderTable; 587 const Elf_Shdr *dot_shstrtab_sec; // Section header string table. 588 const Elf_Shdr *dot_strtab_sec; // Symbol header string table. 589 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table. 590 591 int SymbolTableIndex; 592 int DynamicSymbolTableIndex; 593 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable; 594 595 const Elf_Shdr *dot_dynamic_sec; // .dynamic 596 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version 597 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r 598 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d 599 600 // Pointer to SONAME entry in dynamic string table 601 // This is set the first time getLoadName is called. 602 mutable const char *dt_soname; 603 604 private: 605 uint64_t getROffset(DataRefImpl Rel) const; 606 607 // Records for each version index the corresponding Verdef or Vernaux entry. 608 // This is filled the first time LoadVersionMap() is called. 609 class VersionMapEntry : public PointerIntPair<const void*, 1> { 610 public: 611 // If the integer is 0, this is an Elf_Verdef*. 612 // If the integer is 1, this is an Elf_Vernaux*. 613 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { } 614 VersionMapEntry(const Elf_Verdef *verdef) 615 : PointerIntPair<const void*, 1>(verdef, 0) { } 616 VersionMapEntry(const Elf_Vernaux *vernaux) 617 : PointerIntPair<const void*, 1>(vernaux, 1) { } 618 bool isNull() const { return getPointer() == NULL; } 619 bool isVerdef() const { return !isNull() && getInt() == 0; } 620 bool isVernaux() const { return !isNull() && getInt() == 1; } 621 const Elf_Verdef *getVerdef() const { 622 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL; 623 } 624 const Elf_Vernaux *getVernaux() const { 625 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL; 626 } 627 }; 628 mutable SmallVector<VersionMapEntry, 16> VersionMap; 629 void LoadVersionDefs(const Elf_Shdr *sec) const; 630 void LoadVersionNeeds(const Elf_Shdr *ec) const; 631 void LoadVersionMap() const; 632 633 /// @brief Get the relocation section that contains \a Rel. 634 const Elf_Shdr *getRelSection(DataRefImpl Rel) const { 635 return getSection(Rel.d.a); 636 } 637 638 public: 639 bool isRelocationHasAddend(DataRefImpl Rel) const; 640 template<typename T> 641 const T *getEntry(uint32_t Section, uint32_t Entry) const; 642 template<typename T> 643 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const; 644 const Elf_Shdr *getSection(DataRefImpl index) const; 645 const Elf_Shdr *getSection(uint32_t index) const; 646 const Elf_Rel *getRel(DataRefImpl Rel) const; 647 const Elf_Rela *getRela(DataRefImpl Rela) const; 648 const char *getString(uint32_t section, uint32_t offset) const; 649 const char *getString(const Elf_Shdr *section, uint32_t offset) const; 650 error_code getSymbolVersion(const Elf_Shdr *section, 651 const Elf_Sym *Symb, 652 StringRef &Version, 653 bool &IsDefault) const; 654 void VerifyStrTab(const Elf_Shdr *sh) const; 655 656 protected: 657 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private? 658 void validateSymbol(DataRefImpl Symb) const; 659 StringRef getRelocationTypeName(uint32_t Type) const; 660 661 public: 662 error_code getSymbolName(const Elf_Shdr *section, 663 const Elf_Sym *Symb, 664 StringRef &Res) const; 665 error_code getSectionName(const Elf_Shdr *section, 666 StringRef &Res) const; 667 const Elf_Dyn *getDyn(DataRefImpl DynData) const; 668 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, 669 bool &IsDefault) const; 670 uint64_t getSymbolIndex(const Elf_Sym *sym) const; 671 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const; 672 protected: 673 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; 674 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; 675 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const; 676 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; 677 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const; 678 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; 679 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; 680 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const; 681 virtual error_code getSymbolType(DataRefImpl Symb, 682 SymbolRef::Type &Res) const; 683 virtual error_code getSymbolSection(DataRefImpl Symb, 684 section_iterator &Res) const; 685 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const; 686 687 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const; 688 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const; 689 690 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; 691 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; 692 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; 693 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; 694 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; 695 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; 696 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; 697 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; 698 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; 699 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, 700 bool &Res) const; 701 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const; 702 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const; 703 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const; 704 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 705 bool &Result) const; 706 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; 707 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; 708 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const; 709 710 virtual error_code getRelocationNext(DataRefImpl Rel, 711 RelocationRef &Res) const; 712 virtual error_code getRelocationAddress(DataRefImpl Rel, 713 uint64_t &Res) const; 714 virtual error_code getRelocationOffset(DataRefImpl Rel, 715 uint64_t &Res) const; 716 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const; 717 virtual error_code getRelocationType(DataRefImpl Rel, 718 uint64_t &Res) const; 719 virtual error_code getRelocationTypeName(DataRefImpl Rel, 720 SmallVectorImpl<char> &Result) const; 721 virtual error_code getRelocationValueString(DataRefImpl Rel, 722 SmallVectorImpl<char> &Result) const; 723 724 public: 725 ELFObjectFile(MemoryBuffer *Object, error_code &ec); 726 727 bool isMips64EL() const { 728 return Header->e_machine == ELF::EM_MIPS && 729 Header->getFileClass() == ELF::ELFCLASS64 && 730 Header->getDataEncoding() == ELF::ELFDATA2LSB; 731 } 732 733 virtual symbol_iterator begin_symbols() const; 734 virtual symbol_iterator end_symbols() const; 735 736 virtual symbol_iterator begin_dynamic_symbols() const; 737 virtual symbol_iterator end_dynamic_symbols() const; 738 739 virtual section_iterator begin_sections() const; 740 virtual section_iterator end_sections() const; 741 742 virtual library_iterator begin_libraries_needed() const; 743 virtual library_iterator end_libraries_needed() const; 744 745 const Elf_Shdr *getDynamicSymbolTableSectionHeader() const { 746 return getSection(DynamicSymbolTableIndex); 747 } 748 749 const Elf_Shdr *getDynamicStringTableSectionHeader() const { 750 return dot_dynstr_sec; 751 } 752 753 Elf_Dyn_iterator begin_dynamic_table() const; 754 /// \param NULLEnd use one past the first DT_NULL entry as the end instead of 755 /// the section size. 756 Elf_Dyn_iterator end_dynamic_table(bool NULLEnd = false) const; 757 758 Elf_Sym_iterator begin_elf_dynamic_symbols() const { 759 const Elf_Shdr *DynSymtab = getDynamicSymbolTableSectionHeader(); 760 if (DynSymtab) 761 return Elf_Sym_iterator(DynSymtab->sh_entsize, 762 (const char *)base() + DynSymtab->sh_offset); 763 return Elf_Sym_iterator(0, 0); 764 } 765 766 Elf_Sym_iterator end_elf_dynamic_symbols() const { 767 const Elf_Shdr *DynSymtab = getDynamicSymbolTableSectionHeader(); 768 if (DynSymtab) 769 return Elf_Sym_iterator(DynSymtab->sh_entsize, (const char *)base() + 770 DynSymtab->sh_offset + DynSymtab->sh_size); 771 return Elf_Sym_iterator(0, 0); 772 } 773 774 Elf_Rela_Iter beginELFRela(const Elf_Shdr *sec) const { 775 return Elf_Rela_Iter(sec->sh_entsize, 776 (const char *)(base() + sec->sh_offset)); 777 } 778 779 Elf_Rela_Iter endELFRela(const Elf_Shdr *sec) const { 780 return Elf_Rela_Iter(sec->sh_entsize, (const char *) 781 (base() + sec->sh_offset + sec->sh_size)); 782 } 783 784 Elf_Rel_Iter beginELFRel(const Elf_Shdr *sec) const { 785 return Elf_Rel_Iter(sec->sh_entsize, 786 (const char *)(base() + sec->sh_offset)); 787 } 788 789 Elf_Rel_Iter endELFRel(const Elf_Shdr *sec) const { 790 return Elf_Rel_Iter(sec->sh_entsize, (const char *) 791 (base() + sec->sh_offset + sec->sh_size)); 792 } 793 794 /// \brief Iterate over program header table. 795 typedef ELFEntityIterator<const Elf_Phdr> Elf_Phdr_Iter; 796 797 Elf_Phdr_Iter begin_program_headers() const { 798 return Elf_Phdr_Iter(Header->e_phentsize, 799 (const char*)base() + Header->e_phoff); 800 } 801 802 Elf_Phdr_Iter end_program_headers() const { 803 return Elf_Phdr_Iter(Header->e_phentsize, 804 (const char*)base() + 805 Header->e_phoff + 806 (Header->e_phnum * Header->e_phentsize)); 807 } 808 809 virtual uint8_t getBytesInAddress() const; 810 virtual StringRef getFileFormatName() const; 811 virtual StringRef getObjectType() const { return "ELF"; } 812 virtual unsigned getArch() const; 813 virtual StringRef getLoadName() const; 814 virtual error_code getSectionContents(const Elf_Shdr *sec, 815 StringRef &Res) const; 816 817 uint64_t getNumSections() const; 818 uint64_t getStringTableIndex() const; 819 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const; 820 const Elf_Ehdr *getElfHeader() const; 821 const Elf_Shdr *getSection(const Elf_Sym *symb) const; 822 const Elf_Shdr *getElfSection(section_iterator &It) const; 823 const Elf_Sym *getElfSymbol(symbol_iterator &It) const; 824 const Elf_Sym *getElfSymbol(uint32_t index) const; 825 826 // Methods for type inquiry through isa, cast, and dyn_cast 827 bool isDyldType() const { return isDyldELFObject; } 828 static inline bool classof(const Binary *v) { 829 return v->getType() == getELFType(ELFT::TargetEndianness == support::little, 830 ELFT::Is64Bits); 831 } 832 }; 833 834 // Use an alignment of 2 for the typedefs since that is the worst case for 835 // ELF files in archives. 836 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile; 837 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile; 838 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile; 839 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile; 840 841 // Iterate through the version definitions, and place each Elf_Verdef 842 // in the VersionMap according to its index. 843 template<class ELFT> 844 void ELFObjectFile<ELFT>::LoadVersionDefs(const Elf_Shdr *sec) const { 845 unsigned vd_size = sec->sh_size; // Size of section in bytes 846 unsigned vd_count = sec->sh_info; // Number of Verdef entries 847 const char *sec_start = (const char*)base() + sec->sh_offset; 848 const char *sec_end = sec_start + vd_size; 849 // The first Verdef entry is at the start of the section. 850 const char *p = sec_start; 851 for (unsigned i = 0; i < vd_count; i++) { 852 if (p + sizeof(Elf_Verdef) > sec_end) 853 report_fatal_error("Section ended unexpectedly while scanning " 854 "version definitions."); 855 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p); 856 if (vd->vd_version != ELF::VER_DEF_CURRENT) 857 report_fatal_error("Unexpected verdef version"); 858 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION; 859 if (index >= VersionMap.size()) 860 VersionMap.resize(index+1); 861 VersionMap[index] = VersionMapEntry(vd); 862 p += vd->vd_next; 863 } 864 } 865 866 // Iterate through the versions needed section, and place each Elf_Vernaux 867 // in the VersionMap according to its index. 868 template<class ELFT> 869 void ELFObjectFile<ELFT>::LoadVersionNeeds(const Elf_Shdr *sec) const { 870 unsigned vn_size = sec->sh_size; // Size of section in bytes 871 unsigned vn_count = sec->sh_info; // Number of Verneed entries 872 const char *sec_start = (const char*)base() + sec->sh_offset; 873 const char *sec_end = sec_start + vn_size; 874 // The first Verneed entry is at the start of the section. 875 const char *p = sec_start; 876 for (unsigned i = 0; i < vn_count; i++) { 877 if (p + sizeof(Elf_Verneed) > sec_end) 878 report_fatal_error("Section ended unexpectedly while scanning " 879 "version needed records."); 880 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p); 881 if (vn->vn_version != ELF::VER_NEED_CURRENT) 882 report_fatal_error("Unexpected verneed version"); 883 // Iterate through the Vernaux entries 884 const char *paux = p + vn->vn_aux; 885 for (unsigned j = 0; j < vn->vn_cnt; j++) { 886 if (paux + sizeof(Elf_Vernaux) > sec_end) 887 report_fatal_error("Section ended unexpected while scanning auxiliary " 888 "version needed records."); 889 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux); 890 size_t index = vna->vna_other & ELF::VERSYM_VERSION; 891 if (index >= VersionMap.size()) 892 VersionMap.resize(index+1); 893 VersionMap[index] = VersionMapEntry(vna); 894 paux += vna->vna_next; 895 } 896 p += vn->vn_next; 897 } 898 } 899 900 template<class ELFT> 901 void ELFObjectFile<ELFT>::LoadVersionMap() const { 902 // If there is no dynamic symtab or version table, there is nothing to do. 903 if (getDynamicStringTableSectionHeader() == NULL || 904 dot_gnu_version_sec == NULL) 905 return; 906 907 // Has the VersionMap already been loaded? 908 if (VersionMap.size() > 0) 909 return; 910 911 // The first two version indexes are reserved. 912 // Index 0 is LOCAL, index 1 is GLOBAL. 913 VersionMap.push_back(VersionMapEntry()); 914 VersionMap.push_back(VersionMapEntry()); 915 916 if (dot_gnu_version_d_sec) 917 LoadVersionDefs(dot_gnu_version_d_sec); 918 919 if (dot_gnu_version_r_sec) 920 LoadVersionNeeds(dot_gnu_version_r_sec); 921 } 922 923 template<class ELFT> 924 void ELFObjectFile<ELFT>::validateSymbol(DataRefImpl Symb) const { 925 #ifndef NDEBUG 926 const Elf_Sym *symb = getSymbol(Symb); 927 const Elf_Shdr *SymbolTableSection = getSection(Symb.d.b); 928 // FIXME: We really need to do proper error handling in the case of an invalid 929 // input file. Because we don't use exceptions, I think we'll just pass 930 // an error object around. 931 if (!( symb 932 && SymbolTableSection 933 && symb >= (const Elf_Sym*)(base() 934 + SymbolTableSection->sh_offset) 935 && symb < (const Elf_Sym*)(base() 936 + SymbolTableSection->sh_offset 937 + SymbolTableSection->sh_size))) 938 // FIXME: Proper error handling. 939 report_fatal_error("Symb must point to a valid symbol!"); 940 #endif 941 } 942 943 template<class ELFT> 944 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb, 945 SymbolRef &Result) const { 946 validateSymbol(Symb); 947 ++Symb.d.a; 948 Result = SymbolRef(Symb, this); 949 return object_error::success; 950 } 951 952 template<class ELFT> 953 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb, 954 StringRef &Result) const { 955 validateSymbol(Symb); 956 const Elf_Sym *symb = getSymbol(Symb); 957 return getSymbolName(getSection(Symb.d.b), symb, Result); 958 } 959 960 template<class ELFT> 961 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef, 962 StringRef &Version, 963 bool &IsDefault) const { 964 DataRefImpl Symb = SymRef.getRawDataRefImpl(); 965 validateSymbol(Symb); 966 const Elf_Sym *symb = getSymbol(Symb); 967 return getSymbolVersion(getSection(Symb.d.b), symb, Version, IsDefault); 968 } 969 970 template<class ELFT> 971 ELF::Elf64_Word ELFObjectFile<ELFT> 972 ::getSymbolTableIndex(const Elf_Sym *symb) const { 973 if (symb->st_shndx == ELF::SHN_XINDEX) 974 return ExtendedSymbolTable.lookup(symb); 975 return symb->st_shndx; 976 } 977 978 template<class ELFT> 979 const typename ELFObjectFile<ELFT>::Elf_Shdr * 980 ELFObjectFile<ELFT>::getSection(const Elf_Sym *symb) const { 981 if (symb->st_shndx == ELF::SHN_XINDEX) 982 return getSection(ExtendedSymbolTable.lookup(symb)); 983 if (symb->st_shndx >= ELF::SHN_LORESERVE) 984 return 0; 985 return getSection(symb->st_shndx); 986 } 987 988 template<class ELFT> 989 const typename ELFObjectFile<ELFT>::Elf_Ehdr * 990 ELFObjectFile<ELFT>::getElfHeader() const { 991 return Header; 992 } 993 994 template<class ELFT> 995 const typename ELFObjectFile<ELFT>::Elf_Shdr * 996 ELFObjectFile<ELFT>::getElfSection(section_iterator &It) const { 997 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl(); 998 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p); 999 } 1000 1001 template<class ELFT> 1002 const typename ELFObjectFile<ELFT>::Elf_Sym * 1003 ELFObjectFile<ELFT>::getElfSymbol(symbol_iterator &It) const { 1004 return getSymbol(It->getRawDataRefImpl()); 1005 } 1006 1007 template<class ELFT> 1008 const typename ELFObjectFile<ELFT>::Elf_Sym * 1009 ELFObjectFile<ELFT>::getElfSymbol(uint32_t index) const { 1010 DataRefImpl SymbolData; 1011 SymbolData.d.a = index; 1012 SymbolData.d.b = SymbolTableIndex; 1013 return getSymbol(SymbolData); 1014 } 1015 1016 template<class ELFT> 1017 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb, 1018 uint64_t &Result) const { 1019 validateSymbol(Symb); 1020 const Elf_Sym *symb = getSymbol(Symb); 1021 const Elf_Shdr *Section; 1022 switch (getSymbolTableIndex(symb)) { 1023 case ELF::SHN_COMMON: 1024 // Unintialized symbols have no offset in the object file 1025 case ELF::SHN_UNDEF: 1026 Result = UnknownAddressOrSize; 1027 return object_error::success; 1028 case ELF::SHN_ABS: 1029 Result = symb->st_value; 1030 return object_error::success; 1031 default: Section = getSection(symb); 1032 } 1033 1034 switch (symb->getType()) { 1035 case ELF::STT_SECTION: 1036 Result = Section ? Section->sh_offset : UnknownAddressOrSize; 1037 return object_error::success; 1038 case ELF::STT_FUNC: 1039 case ELF::STT_OBJECT: 1040 case ELF::STT_NOTYPE: 1041 Result = symb->st_value + 1042 (Section ? Section->sh_offset : 0); 1043 return object_error::success; 1044 default: 1045 Result = UnknownAddressOrSize; 1046 return object_error::success; 1047 } 1048 } 1049 1050 template<class ELFT> 1051 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb, 1052 uint64_t &Result) const { 1053 validateSymbol(Symb); 1054 const Elf_Sym *symb = getSymbol(Symb); 1055 const Elf_Shdr *Section; 1056 switch (getSymbolTableIndex(symb)) { 1057 case ELF::SHN_COMMON: 1058 case ELF::SHN_UNDEF: 1059 Result = UnknownAddressOrSize; 1060 return object_error::success; 1061 case ELF::SHN_ABS: 1062 Result = symb->st_value; 1063 return object_error::success; 1064 default: Section = getSection(symb); 1065 } 1066 1067 switch (symb->getType()) { 1068 case ELF::STT_SECTION: 1069 Result = Section ? Section->sh_addr : UnknownAddressOrSize; 1070 return object_error::success; 1071 case ELF::STT_FUNC: 1072 case ELF::STT_OBJECT: 1073 case ELF::STT_NOTYPE: 1074 bool IsRelocatable; 1075 switch(Header->e_type) { 1076 case ELF::ET_EXEC: 1077 case ELF::ET_DYN: 1078 IsRelocatable = false; 1079 break; 1080 default: 1081 IsRelocatable = true; 1082 } 1083 Result = symb->st_value; 1084 1085 // Clear the ARM/Thumb indicator flag. 1086 if (Header->e_machine == ELF::EM_ARM) 1087 Result &= ~1; 1088 1089 if (IsRelocatable && Section != 0) 1090 Result += Section->sh_addr; 1091 return object_error::success; 1092 default: 1093 Result = UnknownAddressOrSize; 1094 return object_error::success; 1095 } 1096 } 1097 1098 template<class ELFT> 1099 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb, 1100 uint32_t &Res) const { 1101 uint32_t flags; 1102 getSymbolFlags(Symb, flags); 1103 if (flags & SymbolRef::SF_Common) { 1104 uint64_t Value; 1105 getSymbolValue(Symb, Value); 1106 Res = Value; 1107 } else { 1108 Res = 0; 1109 } 1110 return object_error::success; 1111 } 1112 1113 template<class ELFT> 1114 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb, 1115 uint64_t &Result) const { 1116 validateSymbol(Symb); 1117 const Elf_Sym *symb = getSymbol(Symb); 1118 if (symb->st_size == 0) 1119 Result = UnknownAddressOrSize; 1120 Result = symb->st_size; 1121 return object_error::success; 1122 } 1123 1124 template<class ELFT> 1125 error_code ELFObjectFile<ELFT>::getSymbolNMTypeChar(DataRefImpl Symb, 1126 char &Result) const { 1127 validateSymbol(Symb); 1128 const Elf_Sym *symb = getSymbol(Symb); 1129 const Elf_Shdr *Section = getSection(symb); 1130 1131 char ret = '?'; 1132 1133 if (Section) { 1134 switch (Section->sh_type) { 1135 case ELF::SHT_PROGBITS: 1136 case ELF::SHT_DYNAMIC: 1137 switch (Section->sh_flags) { 1138 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR): 1139 ret = 't'; break; 1140 case (ELF::SHF_ALLOC | ELF::SHF_WRITE): 1141 ret = 'd'; break; 1142 case ELF::SHF_ALLOC: 1143 case (ELF::SHF_ALLOC | ELF::SHF_MERGE): 1144 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS): 1145 ret = 'r'; break; 1146 } 1147 break; 1148 case ELF::SHT_NOBITS: ret = 'b'; 1149 } 1150 } 1151 1152 switch (getSymbolTableIndex(symb)) { 1153 case ELF::SHN_UNDEF: 1154 if (ret == '?') 1155 ret = 'U'; 1156 break; 1157 case ELF::SHN_ABS: ret = 'a'; break; 1158 case ELF::SHN_COMMON: ret = 'c'; break; 1159 } 1160 1161 switch (symb->getBinding()) { 1162 case ELF::STB_GLOBAL: ret = ::toupper(ret); break; 1163 case ELF::STB_WEAK: 1164 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) 1165 ret = 'w'; 1166 else 1167 if (symb->getType() == ELF::STT_OBJECT) 1168 ret = 'V'; 1169 else 1170 ret = 'W'; 1171 } 1172 1173 if (ret == '?' && symb->getType() == ELF::STT_SECTION) { 1174 StringRef name; 1175 if (error_code ec = getSymbolName(Symb, name)) 1176 return ec; 1177 Result = StringSwitch<char>(name) 1178 .StartsWith(".debug", 'N') 1179 .StartsWith(".note", 'n') 1180 .Default('?'); 1181 return object_error::success; 1182 } 1183 1184 Result = ret; 1185 return object_error::success; 1186 } 1187 1188 template<class ELFT> 1189 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb, 1190 SymbolRef::Type &Result) const { 1191 validateSymbol(Symb); 1192 const Elf_Sym *symb = getSymbol(Symb); 1193 1194 switch (symb->getType()) { 1195 case ELF::STT_NOTYPE: 1196 Result = SymbolRef::ST_Unknown; 1197 break; 1198 case ELF::STT_SECTION: 1199 Result = SymbolRef::ST_Debug; 1200 break; 1201 case ELF::STT_FILE: 1202 Result = SymbolRef::ST_File; 1203 break; 1204 case ELF::STT_FUNC: 1205 Result = SymbolRef::ST_Function; 1206 break; 1207 case ELF::STT_OBJECT: 1208 case ELF::STT_COMMON: 1209 case ELF::STT_TLS: 1210 Result = SymbolRef::ST_Data; 1211 break; 1212 default: 1213 Result = SymbolRef::ST_Other; 1214 break; 1215 } 1216 return object_error::success; 1217 } 1218 1219 template<class ELFT> 1220 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb, 1221 uint32_t &Result) const { 1222 validateSymbol(Symb); 1223 const Elf_Sym *symb = getSymbol(Symb); 1224 1225 Result = SymbolRef::SF_None; 1226 1227 if (symb->getBinding() != ELF::STB_LOCAL) 1228 Result |= SymbolRef::SF_Global; 1229 1230 if (symb->getBinding() == ELF::STB_WEAK) 1231 Result |= SymbolRef::SF_Weak; 1232 1233 if (symb->st_shndx == ELF::SHN_ABS) 1234 Result |= SymbolRef::SF_Absolute; 1235 1236 if (symb->getType() == ELF::STT_FILE || 1237 symb->getType() == ELF::STT_SECTION || 1238 Symb == begin_symbols()->getRawDataRefImpl()) 1239 Result |= SymbolRef::SF_FormatSpecific; 1240 1241 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) 1242 Result |= SymbolRef::SF_Undefined; 1243 1244 if (symb->getType() == ELF::STT_COMMON || 1245 getSymbolTableIndex(symb) == ELF::SHN_COMMON) 1246 Result |= SymbolRef::SF_Common; 1247 1248 if (symb->getType() == ELF::STT_TLS) 1249 Result |= SymbolRef::SF_ThreadLocal; 1250 1251 return object_error::success; 1252 } 1253 1254 template<class ELFT> 1255 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb, 1256 section_iterator &Res) const { 1257 validateSymbol(Symb); 1258 const Elf_Sym *symb = getSymbol(Symb); 1259 const Elf_Shdr *sec = getSection(symb); 1260 if (!sec) 1261 Res = end_sections(); 1262 else { 1263 DataRefImpl Sec; 1264 Sec.p = reinterpret_cast<intptr_t>(sec); 1265 Res = section_iterator(SectionRef(Sec, this)); 1266 } 1267 return object_error::success; 1268 } 1269 1270 template<class ELFT> 1271 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb, 1272 uint64_t &Val) const { 1273 validateSymbol(Symb); 1274 const Elf_Sym *symb = getSymbol(Symb); 1275 Val = symb->st_value; 1276 return object_error::success; 1277 } 1278 1279 template<class ELFT> 1280 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec, 1281 SectionRef &Result) const { 1282 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p); 1283 sec += Header->e_shentsize; 1284 Sec.p = reinterpret_cast<intptr_t>(sec); 1285 Result = SectionRef(Sec, this); 1286 return object_error::success; 1287 } 1288 1289 template<class ELFT> 1290 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec, 1291 StringRef &Result) const { 1292 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1293 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name)); 1294 return object_error::success; 1295 } 1296 1297 template<class ELFT> 1298 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec, 1299 uint64_t &Result) const { 1300 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1301 Result = sec->sh_addr; 1302 return object_error::success; 1303 } 1304 1305 template<class ELFT> 1306 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec, 1307 uint64_t &Result) const { 1308 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1309 Result = sec->sh_size; 1310 return object_error::success; 1311 } 1312 1313 template<class ELFT> 1314 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec, 1315 StringRef &Result) const { 1316 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1317 const char *start = (const char*)base() + sec->sh_offset; 1318 Result = StringRef(start, sec->sh_size); 1319 return object_error::success; 1320 } 1321 1322 template<class ELFT> 1323 error_code ELFObjectFile<ELFT>::getSectionContents(const Elf_Shdr *Sec, 1324 StringRef &Result) const { 1325 const char *start = (const char*)base() + Sec->sh_offset; 1326 Result = StringRef(start, Sec->sh_size); 1327 return object_error::success; 1328 } 1329 1330 template<class ELFT> 1331 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec, 1332 uint64_t &Result) const { 1333 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1334 Result = sec->sh_addralign; 1335 return object_error::success; 1336 } 1337 1338 template<class ELFT> 1339 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec, 1340 bool &Result) const { 1341 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1342 if (sec->sh_flags & ELF::SHF_EXECINSTR) 1343 Result = true; 1344 else 1345 Result = false; 1346 return object_error::success; 1347 } 1348 1349 template<class ELFT> 1350 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec, 1351 bool &Result) const { 1352 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1353 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 1354 && sec->sh_type == ELF::SHT_PROGBITS) 1355 Result = true; 1356 else 1357 Result = false; 1358 return object_error::success; 1359 } 1360 1361 template<class ELFT> 1362 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec, 1363 bool &Result) const { 1364 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1365 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 1366 && sec->sh_type == ELF::SHT_NOBITS) 1367 Result = true; 1368 else 1369 Result = false; 1370 return object_error::success; 1371 } 1372 1373 template<class ELFT> 1374 error_code ELFObjectFile<ELFT>::isSectionRequiredForExecution( 1375 DataRefImpl Sec, bool &Result) const { 1376 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1377 if (sec->sh_flags & ELF::SHF_ALLOC) 1378 Result = true; 1379 else 1380 Result = false; 1381 return object_error::success; 1382 } 1383 1384 template<class ELFT> 1385 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec, 1386 bool &Result) const { 1387 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1388 if (sec->sh_type == ELF::SHT_NOBITS) 1389 Result = true; 1390 else 1391 Result = false; 1392 return object_error::success; 1393 } 1394 1395 template<class ELFT> 1396 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec, 1397 bool &Result) const { 1398 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1399 // For ELF, all zero-init sections are virtual (that is, they occupy no space 1400 // in the object image) and vice versa. 1401 Result = sec->sh_type == ELF::SHT_NOBITS; 1402 return object_error::success; 1403 } 1404 1405 template<class ELFT> 1406 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec, 1407 bool &Result) const { 1408 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1409 if (sec->sh_flags & ELF::SHF_WRITE || sec->sh_flags & ELF::SHF_EXECINSTR) 1410 Result = false; 1411 else 1412 Result = true; 1413 return object_error::success; 1414 } 1415 1416 template<class ELFT> 1417 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec, 1418 DataRefImpl Symb, 1419 bool &Result) const { 1420 validateSymbol(Symb); 1421 1422 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1423 const Elf_Sym *symb = getSymbol(Symb); 1424 1425 unsigned shndx = symb->st_shndx; 1426 bool Reserved = shndx >= ELF::SHN_LORESERVE 1427 && shndx <= ELF::SHN_HIRESERVE; 1428 1429 Result = !Reserved && (sec == getSection(symb->st_shndx)); 1430 return object_error::success; 1431 } 1432 1433 template<class ELFT> 1434 relocation_iterator 1435 ELFObjectFile<ELFT>::getSectionRelBegin(DataRefImpl Sec) const { 1436 DataRefImpl RelData; 1437 uintptr_t SHT = reinterpret_cast<uintptr_t>(SectionHeaderTable); 1438 RelData.d.a = (Sec.p - SHT) / Header->e_shentsize; 1439 RelData.d.b = 0; 1440 return relocation_iterator(RelocationRef(RelData, this)); 1441 } 1442 1443 template<class ELFT> 1444 relocation_iterator 1445 ELFObjectFile<ELFT>::getSectionRelEnd(DataRefImpl Sec) const { 1446 DataRefImpl RelData; 1447 uintptr_t SHT = reinterpret_cast<uintptr_t>(SectionHeaderTable); 1448 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1449 RelData.d.a = (Sec.p - SHT) / Header->e_shentsize; 1450 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL) 1451 RelData.d.b = 0; 1452 else 1453 RelData.d.b = S->sh_size / S->sh_entsize; 1454 1455 return relocation_iterator(RelocationRef(RelData, this)); 1456 } 1457 1458 template <class ELFT> 1459 section_iterator 1460 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const { 1461 if (Header->e_type != ELF::ET_REL) 1462 return end_sections(); 1463 1464 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1465 unsigned sh_type = S->sh_type; 1466 if (sh_type != ELF::SHT_RELA && sh_type != ELF::SHT_REL) 1467 return end_sections(); 1468 1469 assert(S->sh_info != 0); 1470 const Elf_Shdr *R = getSection(S->sh_info); 1471 DataRefImpl D; 1472 D.p = reinterpret_cast<uintptr_t>(R); 1473 return section_iterator(SectionRef(D, this)); 1474 } 1475 1476 // Relocations 1477 template<class ELFT> 1478 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel, 1479 RelocationRef &Result) const { 1480 ++Rel.d.b; 1481 Result = RelocationRef(Rel, this); 1482 return object_error::success; 1483 } 1484 1485 template <class ELFT> 1486 symbol_iterator 1487 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const { 1488 uint32_t symbolIdx; 1489 const Elf_Shdr *sec = getRelSection(Rel); 1490 switch (sec->sh_type) { 1491 default : 1492 report_fatal_error("Invalid section type in Rel!"); 1493 case ELF::SHT_REL : { 1494 symbolIdx = getRel(Rel)->getSymbol(isMips64EL()); 1495 break; 1496 } 1497 case ELF::SHT_RELA : { 1498 symbolIdx = getRela(Rel)->getSymbol(isMips64EL()); 1499 break; 1500 } 1501 } 1502 if (!symbolIdx) 1503 return end_symbols(); 1504 1505 DataRefImpl SymbolData; 1506 SymbolData.d.a = symbolIdx; 1507 SymbolData.d.b = sec->sh_link; 1508 return symbol_iterator(SymbolRef(SymbolData, this)); 1509 } 1510 1511 template<class ELFT> 1512 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel, 1513 uint64_t &Result) const { 1514 assert((Header->e_type == ELF::ET_EXEC || Header->e_type == ELF::ET_DYN) && 1515 "Only executable and shared objects files have addresses"); 1516 Result = getROffset(Rel); 1517 return object_error::success; 1518 } 1519 1520 template<class ELFT> 1521 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel, 1522 uint64_t &Result) const { 1523 assert(Header->e_type == ELF::ET_REL && 1524 "Only relocatable object files have relocation offsets"); 1525 Result = getROffset(Rel); 1526 return object_error::success; 1527 } 1528 1529 template<class ELFT> 1530 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const { 1531 const Elf_Shdr *sec = getRelSection(Rel); 1532 switch (sec->sh_type) { 1533 default: 1534 report_fatal_error("Invalid section type in Rel!"); 1535 case ELF::SHT_REL: 1536 return getRel(Rel)->r_offset; 1537 case ELF::SHT_RELA: 1538 return getRela(Rel)->r_offset; 1539 } 1540 } 1541 1542 template<class ELFT> 1543 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel, 1544 uint64_t &Result) const { 1545 const Elf_Shdr *sec = getRelSection(Rel); 1546 switch (sec->sh_type) { 1547 default : 1548 report_fatal_error("Invalid section type in Rel!"); 1549 case ELF::SHT_REL : { 1550 Result = getRel(Rel)->getType(isMips64EL()); 1551 break; 1552 } 1553 case ELF::SHT_RELA : { 1554 Result = getRela(Rel)->getType(isMips64EL()); 1555 break; 1556 } 1557 } 1558 return object_error::success; 1559 } 1560 1561 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \ 1562 case ELF::enum: Res = #enum; break; 1563 1564 template<class ELFT> 1565 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const { 1566 StringRef Res = "Unknown"; 1567 switch (Header->e_machine) { 1568 case ELF::EM_X86_64: 1569 switch (Type) { 1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE); 1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64); 1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32); 1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32); 1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32); 1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY); 1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT); 1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT); 1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE); 1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL); 1580 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32); 1581 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S); 1582 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16); 1583 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16); 1584 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8); 1585 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8); 1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64); 1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64); 1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64); 1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD); 1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD); 1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32); 1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF); 1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32); 1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64); 1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64); 1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32); 1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT64); 1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL64); 1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC64); 1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPLT64); 1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLTOFF64); 1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32); 1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64); 1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC); 1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL); 1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC); 1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_IRELATIVE); 1608 default: break; 1609 } 1610 break; 1611 case ELF::EM_386: 1612 switch (Type) { 1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE); 1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32); 1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32); 1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32); 1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32); 1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY); 1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT); 1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT); 1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE); 1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF); 1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC); 1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT); 1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF); 1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE); 1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE); 1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE); 1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD); 1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM); 1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16); 1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16); 1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8); 1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8); 1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32); 1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH); 1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL); 1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP); 1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32); 1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH); 1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL); 1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP); 1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32); 1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32); 1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32); 1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32); 1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32); 1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32); 1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC); 1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL); 1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC); 1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE); 1653 default: break; 1654 } 1655 break; 1656 case ELF::EM_MIPS: 1657 switch (Type) { 1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_NONE); 1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_16); 1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_32); 1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_REL32); 1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_26); 1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_HI16); 1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_LO16); 1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GPREL16); 1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_LITERAL); 1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT16); 1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_PC16); 1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_CALL16); 1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GPREL32); 1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_UNUSED1); 1672 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_UNUSED2); 1673 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SHIFT5); 1674 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SHIFT6); 1675 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_64); 1676 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_DISP); 1677 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_PAGE); 1678 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_OFST); 1679 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_HI16); 1680 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GOT_LO16); 1681 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SUB); 1682 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_INSERT_A); 1683 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_INSERT_B); 1684 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_DELETE); 1685 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_HIGHER); 1686 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_HIGHEST); 1687 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_CALL_HI16); 1688 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_CALL_LO16); 1689 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_SCN_DISP); 1690 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_REL16); 1691 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_ADD_IMMEDIATE); 1692 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_PJUMP); 1693 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_RELGOT); 1694 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_JALR); 1695 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPMOD32); 1696 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL32); 1697 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPMOD64); 1698 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL64); 1699 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_GD); 1700 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_LDM); 1701 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL_HI16); 1702 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_DTPREL_LO16); 1703 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_GOTTPREL); 1704 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL32); 1705 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL64); 1706 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL_HI16); 1707 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_TLS_TPREL_LO16); 1708 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_GLOB_DAT); 1709 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_COPY); 1710 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_JUMP_SLOT); 1711 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_MIPS_NUM); 1712 default: break; 1713 } 1714 break; 1715 case ELF::EM_AARCH64: 1716 switch (Type) { 1717 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_NONE); 1718 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS64); 1719 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS32); 1720 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ABS16); 1721 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL64); 1722 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL32); 1723 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_PREL16); 1724 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G0); 1725 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G0_NC); 1726 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G1); 1727 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G1_NC); 1728 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G2); 1729 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G2_NC); 1730 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_UABS_G3); 1731 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G0); 1732 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G1); 1733 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_MOVW_SABS_G2); 1734 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LD_PREL_LO19); 1735 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_PREL_LO21); 1736 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_PREL_PG_HI21); 1737 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADD_ABS_LO12_NC); 1738 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST8_ABS_LO12_NC); 1739 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TSTBR14); 1740 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_CONDBR19); 1741 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_JUMP26); 1742 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_CALL26); 1743 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST16_ABS_LO12_NC); 1744 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST32_ABS_LO12_NC); 1745 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST64_ABS_LO12_NC); 1746 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LDST128_ABS_LO12_NC); 1747 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_ADR_GOT_PAGE); 1748 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_LD64_GOT_LO12_NC); 1749 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G2); 1750 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G1); 1751 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G1_NC); 1752 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G0); 1753 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_MOVW_DTPREL_G0_NC); 1754 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_HI12); 1755 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_LO12); 1756 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_ADD_DTPREL_LO12_NC); 1757 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST8_DTPREL_LO12); 1758 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST8_DTPREL_LO12_NC); 1759 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST16_DTPREL_LO12); 1760 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST16_DTPREL_LO12_NC); 1761 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST32_DTPREL_LO12); 1762 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST32_DTPREL_LO12_NC); 1763 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST64_DTPREL_LO12); 1764 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLD_LDST64_DTPREL_LO12_NC); 1765 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_MOVW_GOTTPREL_G1); 1766 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_MOVW_GOTTPREL_G0_NC); 1767 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21); 1768 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC); 1769 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSIE_LD_GOTTPREL_PREL19); 1770 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G2); 1771 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G1); 1772 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G1_NC); 1773 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G0); 1774 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_MOVW_TPREL_G0_NC); 1775 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_HI12); 1776 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_LO12); 1777 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_ADD_TPREL_LO12_NC); 1778 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST8_TPREL_LO12); 1779 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST8_TPREL_LO12_NC); 1780 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST16_TPREL_LO12); 1781 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST16_TPREL_LO12_NC); 1782 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST32_TPREL_LO12); 1783 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST32_TPREL_LO12_NC); 1784 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST64_TPREL_LO12); 1785 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSLE_LDST64_TPREL_LO12_NC); 1786 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_ADR_PAGE); 1787 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_LD64_LO12_NC); 1788 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_ADD_LO12_NC); 1789 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_AARCH64_TLSDESC_CALL); 1790 default: break; 1791 } 1792 break; 1793 case ELF::EM_ARM: 1794 switch (Type) { 1795 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE); 1796 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24); 1797 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32); 1798 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32); 1799 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0); 1800 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16); 1801 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12); 1802 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5); 1803 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8); 1804 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32); 1805 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL); 1806 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8); 1807 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ); 1808 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC); 1809 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8); 1810 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25); 1811 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22); 1812 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32); 1813 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32); 1814 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32); 1815 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY); 1816 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT); 1817 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT); 1818 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE); 1819 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32); 1820 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL); 1821 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL); 1822 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32); 1823 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL); 1824 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24); 1825 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24); 1826 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS); 1827 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0); 1828 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8); 1829 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15); 1830 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC); 1831 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC); 1832 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK); 1833 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1); 1834 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31); 1835 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX); 1836 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2); 1837 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31); 1838 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC); 1839 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS); 1840 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC); 1841 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL); 1842 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC); 1843 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS); 1844 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC); 1845 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL); 1846 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19); 1847 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6); 1848 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0); 1849 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12); 1850 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI); 1851 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI); 1852 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC); 1853 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0); 1854 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC); 1855 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1); 1856 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2); 1857 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1); 1858 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2); 1859 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0); 1860 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1); 1861 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2); 1862 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0); 1863 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1); 1864 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2); 1865 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC); 1866 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0); 1867 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC); 1868 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1); 1869 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2); 1870 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0); 1871 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1); 1872 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2); 1873 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0); 1874 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1); 1875 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2); 1876 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0); 1877 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1); 1878 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2); 1879 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC); 1880 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL); 1881 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL); 1882 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC); 1883 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL); 1884 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL); 1885 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC); 1886 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL); 1887 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ); 1888 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL); 1889 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS); 1890 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS); 1891 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL); 1892 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12); 1893 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12); 1894 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX); 1895 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY); 1896 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT); 1897 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11); 1898 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8); 1899 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32); 1900 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32); 1901 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32); 1902 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32); 1903 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32); 1904 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12); 1905 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12); 1906 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP); 1907 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0); 1908 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1); 1909 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2); 1910 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3); 1911 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4); 1912 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5); 1913 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6); 1914 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7); 1915 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8); 1916 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9); 1917 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10); 1918 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11); 1919 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12); 1920 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13); 1921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14); 1922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15); 1923 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO); 1924 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16); 1925 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32); 1926 default: break; 1927 } 1928 break; 1929 case ELF::EM_HEXAGON: 1930 switch (Type) { 1931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE); 1932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL); 1933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL); 1934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL); 1935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16); 1936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16); 1937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32); 1938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16); 1939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8); 1940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0); 1941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1); 1942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2); 1943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3); 1944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16); 1945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL); 1946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL); 1947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X); 1948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X); 1949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X); 1950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X); 1951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X); 1952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X); 1953 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X); 1954 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X); 1955 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X); 1956 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X); 1957 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X); 1958 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X); 1959 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X); 1960 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X); 1961 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X); 1962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL); 1963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY); 1964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT); 1965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT); 1966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE); 1967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL); 1968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16); 1969 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16); 1970 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32); 1971 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16); 1972 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16); 1973 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32); 1974 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16); 1975 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32); 1976 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16); 1977 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16); 1978 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32); 1979 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16); 1980 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL); 1981 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16); 1982 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16); 1983 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32); 1984 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16); 1985 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16); 1986 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16); 1987 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32); 1988 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16); 1989 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16); 1990 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32); 1991 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16); 1992 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16); 1993 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16); 1994 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32); 1995 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16); 1996 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X); 1997 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X); 1998 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X); 1999 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X); 2000 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X); 2001 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X); 2002 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X); 2003 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X); 2004 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X); 2005 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X); 2006 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X); 2007 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X); 2008 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X); 2009 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X); 2010 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X); 2011 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X); 2012 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X); 2013 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X); 2014 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X); 2015 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X); 2016 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X); 2017 default: break; 2018 } 2019 break; 2020 case ELF::EM_PPC: 2021 switch (Type) { 2022 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_NONE); 2023 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR32); 2024 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR24); 2025 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16); 2026 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16_LO); 2027 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16_HI); 2028 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR16_HA); 2029 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR14); 2030 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR14_BRTAKEN); 2031 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_ADDR14_BRNTAKEN); 2032 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL24); 2033 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL14); 2034 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL14_BRTAKEN); 2035 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL14_BRNTAKEN); 2036 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16); 2037 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16_LO); 2038 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16_HI); 2039 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT16_HA); 2040 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL32); 2041 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TLS); 2042 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPMOD32); 2043 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16); 2044 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16_LO); 2045 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16_HI); 2046 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL16_HA); 2047 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TPREL32); 2048 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16); 2049 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16_LO); 2050 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16_HI); 2051 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL16_HA); 2052 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_DTPREL32); 2053 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16); 2054 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16_LO); 2055 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16_HI); 2056 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSGD16_HA); 2057 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16); 2058 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16_LO); 2059 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16_HI); 2060 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TLSLD16_HA); 2061 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16); 2062 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16_LO); 2063 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16_HI); 2064 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_TPREL16_HA); 2065 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16); 2066 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16_LO); 2067 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16_HI); 2068 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_GOT_DTPREL16_HA); 2069 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TLSGD); 2070 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_TLSLD); 2071 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16); 2072 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16_LO); 2073 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16_HI); 2074 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC_REL16_HA); 2075 default: break; 2076 } 2077 break; 2078 case ELF::EM_PPC64: 2079 switch (Type) { 2080 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_NONE); 2081 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR32); 2082 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR24); 2083 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16); 2084 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_LO); 2085 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HI); 2086 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HA); 2087 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR14); 2088 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR14_BRTAKEN); 2089 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR14_BRNTAKEN); 2090 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL24); 2091 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL14); 2092 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL14_BRTAKEN); 2093 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL14_BRNTAKEN); 2094 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16); 2095 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_LO); 2096 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_HI); 2097 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_HA); 2098 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL32); 2099 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR64); 2100 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHER); 2101 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHERA); 2102 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHEST); 2103 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_HIGHESTA); 2104 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL64); 2105 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16); 2106 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_LO); 2107 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_HI); 2108 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_HA); 2109 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC); 2110 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_DS); 2111 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_ADDR16_LO_DS); 2112 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_DS); 2113 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT16_LO_DS); 2114 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_DS); 2115 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TOC16_LO_DS); 2116 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TLS); 2117 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPMOD64); 2118 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16); 2119 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_LO); 2120 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HI); 2121 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HA); 2122 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL64); 2123 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16); 2124 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_LO); 2125 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HI); 2126 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HA); 2127 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL64); 2128 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16); 2129 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16_LO); 2130 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16_HI); 2131 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSGD16_HA); 2132 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16); 2133 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16_LO); 2134 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16_HI); 2135 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TLSLD16_HA); 2136 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_DS); 2137 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_LO_DS); 2138 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_HI); 2139 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_TPREL16_HA); 2140 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_DS); 2141 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_LO_DS); 2142 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_HI); 2143 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_GOT_DTPREL16_HA); 2144 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_DS); 2145 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_LO_DS); 2146 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHER); 2147 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHERA); 2148 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHEST); 2149 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TPREL16_HIGHESTA); 2150 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_DS); 2151 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_LO_DS); 2152 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHER); 2153 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHERA); 2154 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHEST); 2155 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_DTPREL16_HIGHESTA); 2156 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TLSGD); 2157 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_TLSLD); 2158 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16); 2159 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16_LO); 2160 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16_HI); 2161 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_PPC64_REL16_HA); 2162 default: break; 2163 } 2164 break; 2165 case ELF::EM_S390: 2166 switch (Type) { 2167 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_NONE); 2168 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_8); 2169 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_12); 2170 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_16); 2171 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_32); 2172 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC32); 2173 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT12); 2174 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT32); 2175 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT32); 2176 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_COPY); 2177 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GLOB_DAT); 2178 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_JMP_SLOT); 2179 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_RELATIVE); 2180 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTOFF); 2181 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPC); 2182 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT16); 2183 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC16); 2184 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC16DBL); 2185 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT16DBL); 2186 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC32DBL); 2187 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT32DBL); 2188 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPCDBL); 2189 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_64); 2190 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PC64); 2191 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT64); 2192 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLT64); 2193 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTENT); 2194 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTOFF16); 2195 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTOFF64); 2196 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT12); 2197 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT16); 2198 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT32); 2199 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT64); 2200 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLTENT); 2201 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLTOFF16); 2202 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLTOFF32); 2203 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_PLTOFF64); 2204 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LOAD); 2205 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GDCALL); 2206 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDCALL); 2207 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GD32); 2208 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GD64); 2209 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE12); 2210 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE32); 2211 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE64); 2212 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDM32); 2213 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDM64); 2214 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_IE32); 2215 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_IE64); 2216 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_IEENT); 2217 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LE32); 2218 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LE64); 2219 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDO32); 2220 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_LDO64); 2221 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_DTPMOD); 2222 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_DTPOFF); 2223 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_TPOFF); 2224 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_20); 2225 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOT20); 2226 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_GOTPLT20); 2227 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_TLS_GOTIE20); 2228 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_390_IRELATIVE); 2229 default: break; 2230 } 2231 break; 2232 default: break; 2233 } 2234 return Res; 2235 } 2236 2237 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME 2238 2239 template<class ELFT> 2240 error_code ELFObjectFile<ELFT>::getRelocationTypeName( 2241 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 2242 const Elf_Shdr *sec = getRelSection(Rel); 2243 uint32_t type; 2244 switch (sec->sh_type) { 2245 default : 2246 return object_error::parse_failed; 2247 case ELF::SHT_REL : { 2248 type = getRel(Rel)->getType(isMips64EL()); 2249 break; 2250 } 2251 case ELF::SHT_RELA : { 2252 type = getRela(Rel)->getType(isMips64EL()); 2253 break; 2254 } 2255 } 2256 2257 if (!isMips64EL()) { 2258 StringRef Name = getRelocationTypeName(type); 2259 Result.append(Name.begin(), Name.end()); 2260 } else { 2261 uint8_t Type1 = (type >> 0) & 0xFF; 2262 uint8_t Type2 = (type >> 8) & 0xFF; 2263 uint8_t Type3 = (type >> 16) & 0xFF; 2264 2265 // Concat all three relocation type names. 2266 StringRef Name = getRelocationTypeName(Type1); 2267 Result.append(Name.begin(), Name.end()); 2268 2269 Name = getRelocationTypeName(Type2); 2270 Result.append(1, '/'); 2271 Result.append(Name.begin(), Name.end()); 2272 2273 Name = getRelocationTypeName(Type3); 2274 Result.append(1, '/'); 2275 Result.append(Name.begin(), Name.end()); 2276 } 2277 2278 return object_error::success; 2279 } 2280 2281 template<class ELFT> 2282 error_code ELFObjectFile<ELFT>::getRelocationAddend( 2283 DataRefImpl Rel, int64_t &Result) const { 2284 const Elf_Shdr *sec = getRelSection(Rel); 2285 switch (sec->sh_type) { 2286 default : 2287 report_fatal_error("Invalid section type in Rel!"); 2288 case ELF::SHT_REL : { 2289 Result = 0; 2290 return object_error::success; 2291 } 2292 case ELF::SHT_RELA : { 2293 Result = getRela(Rel)->r_addend; 2294 return object_error::success; 2295 } 2296 } 2297 } 2298 2299 template<class ELFT> 2300 error_code ELFObjectFile<ELFT>::getRelocationValueString( 2301 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 2302 const Elf_Shdr *sec = getRelSection(Rel); 2303 uint8_t type; 2304 StringRef res; 2305 int64_t addend = 0; 2306 uint16_t symbol_index = 0; 2307 switch (sec->sh_type) { 2308 default: 2309 return object_error::parse_failed; 2310 case ELF::SHT_REL: { 2311 type = getRel(Rel)->getType(isMips64EL()); 2312 symbol_index = getRel(Rel)->getSymbol(isMips64EL()); 2313 // TODO: Read implicit addend from section data. 2314 break; 2315 } 2316 case ELF::SHT_RELA: { 2317 type = getRela(Rel)->getType(isMips64EL()); 2318 symbol_index = getRela(Rel)->getSymbol(isMips64EL()); 2319 addend = getRela(Rel)->r_addend; 2320 break; 2321 } 2322 } 2323 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index); 2324 StringRef symname; 2325 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname)) 2326 return ec; 2327 switch (Header->e_machine) { 2328 case ELF::EM_X86_64: 2329 switch (type) { 2330 case ELF::R_X86_64_PC8: 2331 case ELF::R_X86_64_PC16: 2332 case ELF::R_X86_64_PC32: { 2333 std::string fmtbuf; 2334 raw_string_ostream fmt(fmtbuf); 2335 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P"; 2336 fmt.flush(); 2337 Result.append(fmtbuf.begin(), fmtbuf.end()); 2338 } 2339 break; 2340 case ELF::R_X86_64_8: 2341 case ELF::R_X86_64_16: 2342 case ELF::R_X86_64_32: 2343 case ELF::R_X86_64_32S: 2344 case ELF::R_X86_64_64: { 2345 std::string fmtbuf; 2346 raw_string_ostream fmt(fmtbuf); 2347 fmt << symname << (addend < 0 ? "" : "+") << addend; 2348 fmt.flush(); 2349 Result.append(fmtbuf.begin(), fmtbuf.end()); 2350 } 2351 break; 2352 default: 2353 res = "Unknown"; 2354 } 2355 break; 2356 case ELF::EM_AARCH64: { 2357 std::string fmtbuf; 2358 raw_string_ostream fmt(fmtbuf); 2359 fmt << symname; 2360 if (addend != 0) 2361 fmt << (addend < 0 ? "" : "+") << addend; 2362 fmt.flush(); 2363 Result.append(fmtbuf.begin(), fmtbuf.end()); 2364 break; 2365 } 2366 case ELF::EM_ARM: 2367 case ELF::EM_HEXAGON: 2368 res = symname; 2369 break; 2370 default: 2371 res = "Unknown"; 2372 } 2373 if (Result.empty()) 2374 Result.append(res.begin(), res.end()); 2375 return object_error::success; 2376 } 2377 2378 // Verify that the last byte in the string table in a null. 2379 template<class ELFT> 2380 void ELFObjectFile<ELFT>::VerifyStrTab(const Elf_Shdr *sh) const { 2381 const char *strtab = (const char*)base() + sh->sh_offset; 2382 if (strtab[sh->sh_size - 1] != 0) 2383 // FIXME: Proper error handling. 2384 report_fatal_error("String table must end with a null terminator!"); 2385 } 2386 2387 template<class ELFT> 2388 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec) 2389 : ObjectFile(getELFType( 2390 static_cast<endianness>(ELFT::TargetEndianness) == support::little, 2391 ELFT::Is64Bits), 2392 Object) 2393 , isDyldELFObject(false) 2394 , SectionHeaderTable(0) 2395 , dot_shstrtab_sec(0) 2396 , dot_strtab_sec(0) 2397 , dot_dynstr_sec(0) 2398 , dot_dynamic_sec(0) 2399 , dot_gnu_version_sec(0) 2400 , dot_gnu_version_r_sec(0) 2401 , dot_gnu_version_d_sec(0) 2402 , dt_soname(0) 2403 { 2404 2405 const uint64_t FileSize = Data->getBufferSize(); 2406 2407 if (sizeof(Elf_Ehdr) > FileSize) 2408 // FIXME: Proper error handling. 2409 report_fatal_error("File too short!"); 2410 2411 Header = reinterpret_cast<const Elf_Ehdr *>(base()); 2412 2413 if (Header->e_shoff == 0) 2414 return; 2415 2416 const uint64_t SectionTableOffset = Header->e_shoff; 2417 2418 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) 2419 // FIXME: Proper error handling. 2420 report_fatal_error("Section header table goes past end of file!"); 2421 2422 // The getNumSections() call below depends on SectionHeaderTable being set. 2423 SectionHeaderTable = 2424 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset); 2425 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize; 2426 2427 if (SectionTableOffset + SectionTableSize > FileSize) 2428 // FIXME: Proper error handling. 2429 report_fatal_error("Section table goes past end of file!"); 2430 2431 // To find the symbol tables we walk the section table to find SHT_SYMTAB. 2432 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0; 2433 const Elf_Shdr* sh = SectionHeaderTable; 2434 2435 SymbolTableIndex = -1; 2436 DynamicSymbolTableIndex = -1; 2437 2438 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) { 2439 switch (sh->sh_type) { 2440 case ELF::SHT_SYMTAB_SHNDX: { 2441 if (SymbolTableSectionHeaderIndex) 2442 // FIXME: Proper error handling. 2443 report_fatal_error("More than one .symtab_shndx!"); 2444 SymbolTableSectionHeaderIndex = sh; 2445 break; 2446 } 2447 case ELF::SHT_SYMTAB: { 2448 if (SymbolTableIndex != -1) 2449 report_fatal_error("More than one SHT_SYMTAB!"); 2450 SymbolTableIndex = i; 2451 break; 2452 } 2453 case ELF::SHT_DYNSYM: { 2454 if (DynamicSymbolTableIndex != -1) 2455 // FIXME: Proper error handling. 2456 report_fatal_error("More than one SHT_DYNSYM!"); 2457 DynamicSymbolTableIndex = i; 2458 break; 2459 } 2460 case ELF::SHT_REL: 2461 case ELF::SHT_RELA: 2462 break; 2463 case ELF::SHT_DYNAMIC: { 2464 if (dot_dynamic_sec != NULL) 2465 // FIXME: Proper error handling. 2466 report_fatal_error("More than one .dynamic!"); 2467 dot_dynamic_sec = sh; 2468 break; 2469 } 2470 case ELF::SHT_GNU_versym: { 2471 if (dot_gnu_version_sec != NULL) 2472 // FIXME: Proper error handling. 2473 report_fatal_error("More than one .gnu.version section!"); 2474 dot_gnu_version_sec = sh; 2475 break; 2476 } 2477 case ELF::SHT_GNU_verdef: { 2478 if (dot_gnu_version_d_sec != NULL) 2479 // FIXME: Proper error handling. 2480 report_fatal_error("More than one .gnu.version_d section!"); 2481 dot_gnu_version_d_sec = sh; 2482 break; 2483 } 2484 case ELF::SHT_GNU_verneed: { 2485 if (dot_gnu_version_r_sec != NULL) 2486 // FIXME: Proper error handling. 2487 report_fatal_error("More than one .gnu.version_r section!"); 2488 dot_gnu_version_r_sec = sh; 2489 break; 2490 } 2491 } 2492 ++sh; 2493 } 2494 2495 // Get string table sections. 2496 dot_shstrtab_sec = getSection(getStringTableIndex()); 2497 if (dot_shstrtab_sec) { 2498 // Verify that the last byte in the string table in a null. 2499 VerifyStrTab(dot_shstrtab_sec); 2500 } 2501 2502 // Merge this into the above loop. 2503 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable), 2504 *e = i + getNumSections() * Header->e_shentsize; 2505 i != e; i += Header->e_shentsize) { 2506 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i); 2507 if (sh->sh_type == ELF::SHT_STRTAB) { 2508 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name)); 2509 if (SectionName == ".strtab") { 2510 if (dot_strtab_sec != 0) 2511 // FIXME: Proper error handling. 2512 report_fatal_error("Already found section named .strtab!"); 2513 dot_strtab_sec = sh; 2514 VerifyStrTab(dot_strtab_sec); 2515 } else if (SectionName == ".dynstr") { 2516 if (dot_dynstr_sec != 0) 2517 // FIXME: Proper error handling. 2518 report_fatal_error("Already found section named .dynstr!"); 2519 dot_dynstr_sec = sh; 2520 VerifyStrTab(dot_dynstr_sec); 2521 } 2522 } 2523 } 2524 2525 // Build symbol name side-mapping if there is one. 2526 if (SymbolTableSectionHeaderIndex) { 2527 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() + 2528 SymbolTableSectionHeaderIndex->sh_offset); 2529 error_code ec; 2530 for (symbol_iterator si = begin_symbols(), 2531 se = end_symbols(); si != se; si.increment(ec)) { 2532 if (ec) 2533 report_fatal_error("Fewer extended symbol table entries than symbols!"); 2534 if (*ShndxTable != ELF::SHN_UNDEF) 2535 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable; 2536 ++ShndxTable; 2537 } 2538 } 2539 } 2540 2541 // Get the symbol table index in the symtab section given a symbol 2542 template<class ELFT> 2543 uint64_t ELFObjectFile<ELFT>::getSymbolIndex(const Elf_Sym *Sym) const { 2544 const Elf_Shdr *SymTab = getSection(SymbolTableIndex); 2545 uintptr_t SymLoc = uintptr_t(Sym); 2546 uintptr_t SymTabLoc = uintptr_t(base() + SymTab->sh_offset); 2547 assert(SymLoc > SymTabLoc && "Symbol not in symbol table!"); 2548 uint64_t SymOffset = SymLoc - SymTabLoc; 2549 assert(SymOffset % SymTab->sh_entsize == 0 && 2550 "Symbol not multiple of symbol size!"); 2551 return SymOffset / SymTab->sh_entsize; 2552 } 2553 2554 template<class ELFT> 2555 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const { 2556 DataRefImpl SymbolData; 2557 if (SymbolTableIndex == -1) { 2558 SymbolData.d.a = 0; 2559 SymbolData.d.b = 0; 2560 } else { 2561 SymbolData.d.a = 0; 2562 SymbolData.d.b = SymbolTableIndex; 2563 } 2564 return symbol_iterator(SymbolRef(SymbolData, this)); 2565 } 2566 2567 template<class ELFT> 2568 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const { 2569 DataRefImpl SymbolData; 2570 if (SymbolTableIndex == -1) { 2571 SymbolData.d.a = 0; 2572 SymbolData.d.b = 0; 2573 } else { 2574 const Elf_Shdr *SymbolTableSection = getSection(SymbolTableIndex); 2575 SymbolData.d.a = SymbolTableSection->getEntityCount(); 2576 SymbolData.d.b = SymbolTableIndex; 2577 } 2578 return symbol_iterator(SymbolRef(SymbolData, this)); 2579 } 2580 2581 template<class ELFT> 2582 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const { 2583 DataRefImpl SymbolData; 2584 if (DynamicSymbolTableIndex == -1) { 2585 SymbolData.d.a = 0; 2586 SymbolData.d.b = 0; 2587 } else { 2588 SymbolData.d.a = 0; 2589 SymbolData.d.b = DynamicSymbolTableIndex; 2590 } 2591 return symbol_iterator(SymbolRef(SymbolData, this)); 2592 } 2593 2594 template<class ELFT> 2595 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const { 2596 DataRefImpl SymbolData; 2597 if (DynamicSymbolTableIndex == -1) { 2598 SymbolData.d.a = 0; 2599 SymbolData.d.b = 0; 2600 } else { 2601 const Elf_Shdr *SymbolTableSection = getSection(DynamicSymbolTableIndex); 2602 SymbolData.d.a = SymbolTableSection->getEntityCount(); 2603 SymbolData.d.b = DynamicSymbolTableIndex; 2604 } 2605 return symbol_iterator(SymbolRef(SymbolData, this)); 2606 } 2607 2608 template<class ELFT> 2609 section_iterator ELFObjectFile<ELFT>::begin_sections() const { 2610 DataRefImpl ret; 2611 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff); 2612 return section_iterator(SectionRef(ret, this)); 2613 } 2614 2615 template<class ELFT> 2616 section_iterator ELFObjectFile<ELFT>::end_sections() const { 2617 DataRefImpl ret; 2618 ret.p = reinterpret_cast<intptr_t>(base() 2619 + Header->e_shoff 2620 + (Header->e_shentsize*getNumSections())); 2621 return section_iterator(SectionRef(ret, this)); 2622 } 2623 2624 template<class ELFT> 2625 typename ELFObjectFile<ELFT>::Elf_Dyn_iterator 2626 ELFObjectFile<ELFT>::begin_dynamic_table() const { 2627 if (dot_dynamic_sec) 2628 return Elf_Dyn_iterator(dot_dynamic_sec->sh_entsize, 2629 (const char *)base() + dot_dynamic_sec->sh_offset); 2630 return Elf_Dyn_iterator(0, 0); 2631 } 2632 2633 template<class ELFT> 2634 typename ELFObjectFile<ELFT>::Elf_Dyn_iterator 2635 ELFObjectFile<ELFT>::end_dynamic_table(bool NULLEnd) const { 2636 if (dot_dynamic_sec) { 2637 Elf_Dyn_iterator Ret(dot_dynamic_sec->sh_entsize, 2638 (const char *)base() + dot_dynamic_sec->sh_offset + 2639 dot_dynamic_sec->sh_size); 2640 2641 if (NULLEnd) { 2642 Elf_Dyn_iterator Start = begin_dynamic_table(); 2643 while (Start != Ret && Start->getTag() != ELF::DT_NULL) 2644 ++Start; 2645 2646 // Include the DT_NULL. 2647 if (Start != Ret) 2648 ++Start; 2649 Ret = Start; 2650 } 2651 return Ret; 2652 } 2653 return Elf_Dyn_iterator(0, 0); 2654 } 2655 2656 template<class ELFT> 2657 StringRef ELFObjectFile<ELFT>::getLoadName() const { 2658 if (!dt_soname) { 2659 // Find the DT_SONAME entry 2660 Elf_Dyn_iterator it = begin_dynamic_table(); 2661 Elf_Dyn_iterator ie = end_dynamic_table(); 2662 while (it != ie && it->getTag() != ELF::DT_SONAME) 2663 ++it; 2664 2665 if (it != ie) { 2666 if (dot_dynstr_sec == NULL) 2667 report_fatal_error("Dynamic string table is missing"); 2668 dt_soname = getString(dot_dynstr_sec, it->getVal()); 2669 } else { 2670 dt_soname = ""; 2671 } 2672 } 2673 return dt_soname; 2674 } 2675 2676 template<class ELFT> 2677 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const { 2678 // Find the first DT_NEEDED entry 2679 Elf_Dyn_iterator i = begin_dynamic_table(); 2680 Elf_Dyn_iterator e = end_dynamic_table(); 2681 while (i != e && i->getTag() != ELF::DT_NEEDED) 2682 ++i; 2683 2684 DataRefImpl DRI; 2685 DRI.p = reinterpret_cast<uintptr_t>(i.get()); 2686 return library_iterator(LibraryRef(DRI, this)); 2687 } 2688 2689 template<class ELFT> 2690 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data, 2691 LibraryRef &Result) const { 2692 // Use the same DataRefImpl format as DynRef. 2693 Elf_Dyn_iterator i = Elf_Dyn_iterator(dot_dynamic_sec->sh_entsize, 2694 reinterpret_cast<const char *>(Data.p)); 2695 Elf_Dyn_iterator e = end_dynamic_table(); 2696 2697 // Skip the current dynamic table entry and find the next DT_NEEDED entry. 2698 do 2699 ++i; 2700 while (i != e && i->getTag() != ELF::DT_NEEDED); 2701 2702 DataRefImpl DRI; 2703 DRI.p = reinterpret_cast<uintptr_t>(i.get()); 2704 Result = LibraryRef(DRI, this); 2705 return object_error::success; 2706 } 2707 2708 template<class ELFT> 2709 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data, 2710 StringRef &Res) const { 2711 Elf_Dyn_iterator i = Elf_Dyn_iterator(dot_dynamic_sec->sh_entsize, 2712 reinterpret_cast<const char *>(Data.p)); 2713 if (i == end_dynamic_table()) 2714 report_fatal_error("getLibraryPath() called on iterator end"); 2715 2716 if (i->getTag() != ELF::DT_NEEDED) 2717 report_fatal_error("Invalid library_iterator"); 2718 2719 // This uses .dynstr to lookup the name of the DT_NEEDED entry. 2720 // THis works as long as DT_STRTAB == .dynstr. This is true most of 2721 // the time, but the specification allows exceptions. 2722 // TODO: This should really use DT_STRTAB instead. Doing this requires 2723 // reading the program headers. 2724 if (dot_dynstr_sec == NULL) 2725 report_fatal_error("Dynamic string table is missing"); 2726 Res = getString(dot_dynstr_sec, i->getVal()); 2727 return object_error::success; 2728 } 2729 2730 template<class ELFT> 2731 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const { 2732 Elf_Dyn_iterator e = end_dynamic_table(); 2733 DataRefImpl DRI; 2734 DRI.p = reinterpret_cast<uintptr_t>(e.get()); 2735 return library_iterator(LibraryRef(DRI, this)); 2736 } 2737 2738 template<class ELFT> 2739 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const { 2740 return ELFT::Is64Bits ? 8 : 4; 2741 } 2742 2743 template<class ELFT> 2744 StringRef ELFObjectFile<ELFT>::getFileFormatName() const { 2745 switch(Header->e_ident[ELF::EI_CLASS]) { 2746 case ELF::ELFCLASS32: 2747 switch(Header->e_machine) { 2748 case ELF::EM_386: 2749 return "ELF32-i386"; 2750 case ELF::EM_X86_64: 2751 return "ELF32-x86-64"; 2752 case ELF::EM_ARM: 2753 return "ELF32-arm"; 2754 case ELF::EM_HEXAGON: 2755 return "ELF32-hexagon"; 2756 case ELF::EM_MIPS: 2757 return "ELF32-mips"; 2758 case ELF::EM_PPC: 2759 return "ELF32-ppc"; 2760 default: 2761 return "ELF32-unknown"; 2762 } 2763 case ELF::ELFCLASS64: 2764 switch(Header->e_machine) { 2765 case ELF::EM_386: 2766 return "ELF64-i386"; 2767 case ELF::EM_X86_64: 2768 return "ELF64-x86-64"; 2769 case ELF::EM_AARCH64: 2770 return "ELF64-aarch64"; 2771 case ELF::EM_PPC64: 2772 return "ELF64-ppc64"; 2773 case ELF::EM_S390: 2774 return "ELF64-s390"; 2775 default: 2776 return "ELF64-unknown"; 2777 } 2778 default: 2779 // FIXME: Proper error handling. 2780 report_fatal_error("Invalid ELFCLASS!"); 2781 } 2782 } 2783 2784 template<class ELFT> 2785 unsigned ELFObjectFile<ELFT>::getArch() const { 2786 switch(Header->e_machine) { 2787 case ELF::EM_386: 2788 return Triple::x86; 2789 case ELF::EM_X86_64: 2790 return Triple::x86_64; 2791 case ELF::EM_AARCH64: 2792 return Triple::aarch64; 2793 case ELF::EM_ARM: 2794 return Triple::arm; 2795 case ELF::EM_HEXAGON: 2796 return Triple::hexagon; 2797 case ELF::EM_MIPS: 2798 return (ELFT::TargetEndianness == support::little) ? 2799 Triple::mipsel : Triple::mips; 2800 case ELF::EM_PPC64: 2801 return (ELFT::TargetEndianness == support::little) ? 2802 Triple::ppc64le : Triple::ppc64; 2803 case ELF::EM_S390: 2804 return Triple::systemz; 2805 default: 2806 return Triple::UnknownArch; 2807 } 2808 } 2809 2810 template<class ELFT> 2811 uint64_t ELFObjectFile<ELFT>::getNumSections() const { 2812 assert(Header && "Header not initialized!"); 2813 if (Header->e_shnum == ELF::SHN_UNDEF) { 2814 assert(SectionHeaderTable && "SectionHeaderTable not initialized!"); 2815 return SectionHeaderTable->sh_size; 2816 } 2817 return Header->e_shnum; 2818 } 2819 2820 template<class ELFT> 2821 uint64_t 2822 ELFObjectFile<ELFT>::getStringTableIndex() const { 2823 if (Header->e_shnum == ELF::SHN_UNDEF) { 2824 if (Header->e_shstrndx == ELF::SHN_HIRESERVE) 2825 return SectionHeaderTable->sh_link; 2826 if (Header->e_shstrndx >= getNumSections()) 2827 return 0; 2828 } 2829 return Header->e_shstrndx; 2830 } 2831 2832 template<class ELFT> 2833 template<typename T> 2834 inline const T * 2835 ELFObjectFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const { 2836 return getEntry<T>(getSection(Section), Entry); 2837 } 2838 2839 template<class ELFT> 2840 template<typename T> 2841 inline const T * 2842 ELFObjectFile<ELFT>::getEntry(const Elf_Shdr * Section, uint32_t Entry) const { 2843 return reinterpret_cast<const T *>( 2844 base() 2845 + Section->sh_offset 2846 + (Entry * Section->sh_entsize)); 2847 } 2848 2849 template<class ELFT> 2850 const typename ELFObjectFile<ELFT>::Elf_Sym * 2851 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const { 2852 return getEntry<Elf_Sym>(Symb.d.b, Symb.d.a); 2853 } 2854 2855 template<class ELFT> 2856 const typename ELFObjectFile<ELFT>::Elf_Rel * 2857 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const { 2858 return getEntry<Elf_Rel>(Rel.d.a, Rel.d.b); 2859 } 2860 2861 template<class ELFT> 2862 const typename ELFObjectFile<ELFT>::Elf_Rela * 2863 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const { 2864 return getEntry<Elf_Rela>(Rela.d.a, Rela.d.b); 2865 } 2866 2867 template<class ELFT> 2868 const typename ELFObjectFile<ELFT>::Elf_Shdr * 2869 ELFObjectFile<ELFT>::getSection(DataRefImpl Symb) const { 2870 const Elf_Shdr *sec = getSection(Symb.d.b); 2871 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM) 2872 // FIXME: Proper error handling. 2873 report_fatal_error("Invalid symbol table section!"); 2874 return sec; 2875 } 2876 2877 template<class ELFT> 2878 const typename ELFObjectFile<ELFT>::Elf_Shdr * 2879 ELFObjectFile<ELFT>::getSection(uint32_t index) const { 2880 if (index == 0) 2881 return 0; 2882 if (!SectionHeaderTable || index >= getNumSections()) 2883 // FIXME: Proper error handling. 2884 report_fatal_error("Invalid section index!"); 2885 2886 return reinterpret_cast<const Elf_Shdr *>( 2887 reinterpret_cast<const char *>(SectionHeaderTable) 2888 + (index * Header->e_shentsize)); 2889 } 2890 2891 template<class ELFT> 2892 const char *ELFObjectFile<ELFT>::getString(uint32_t section, 2893 ELF::Elf32_Word offset) const { 2894 return getString(getSection(section), offset); 2895 } 2896 2897 template<class ELFT> 2898 const char *ELFObjectFile<ELFT>::getString(const Elf_Shdr *section, 2899 ELF::Elf32_Word offset) const { 2900 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!"); 2901 if (offset >= section->sh_size) 2902 // FIXME: Proper error handling. 2903 report_fatal_error("Symbol name offset outside of string table!"); 2904 return (const char *)base() + section->sh_offset + offset; 2905 } 2906 2907 template<class ELFT> 2908 error_code ELFObjectFile<ELFT>::getSymbolName(const Elf_Shdr *section, 2909 const Elf_Sym *symb, 2910 StringRef &Result) const { 2911 if (symb->st_name == 0) { 2912 const Elf_Shdr *section = getSection(symb); 2913 if (!section) 2914 Result = ""; 2915 else 2916 Result = getString(dot_shstrtab_sec, section->sh_name); 2917 return object_error::success; 2918 } 2919 2920 if (DynamicSymbolTableIndex != -1 && 2921 section == getSection(DynamicSymbolTableIndex)) { 2922 // Symbol is in .dynsym, use .dynstr string table 2923 Result = getString(dot_dynstr_sec, symb->st_name); 2924 } else { 2925 // Use the default symbol table name section. 2926 Result = getString(dot_strtab_sec, symb->st_name); 2927 } 2928 return object_error::success; 2929 } 2930 2931 template<class ELFT> 2932 error_code ELFObjectFile<ELFT>::getSectionName(const Elf_Shdr *section, 2933 StringRef &Result) const { 2934 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name)); 2935 return object_error::success; 2936 } 2937 2938 template<class ELFT> 2939 error_code ELFObjectFile<ELFT>::getSymbolVersion(const Elf_Shdr *section, 2940 const Elf_Sym *symb, 2941 StringRef &Version, 2942 bool &IsDefault) const { 2943 // Handle non-dynamic symbols. 2944 if (section != getSection(DynamicSymbolTableIndex)) { 2945 // Non-dynamic symbols can have versions in their names 2946 // A name of the form 'foo@V1' indicates version 'V1', non-default. 2947 // A name of the form 'foo@@V2' indicates version 'V2', default version. 2948 StringRef Name; 2949 error_code ec = getSymbolName(section, symb, Name); 2950 if (ec != object_error::success) 2951 return ec; 2952 size_t atpos = Name.find('@'); 2953 if (atpos == StringRef::npos) { 2954 Version = ""; 2955 IsDefault = false; 2956 return object_error::success; 2957 } 2958 ++atpos; 2959 if (atpos < Name.size() && Name[atpos] == '@') { 2960 IsDefault = true; 2961 ++atpos; 2962 } else { 2963 IsDefault = false; 2964 } 2965 Version = Name.substr(atpos); 2966 return object_error::success; 2967 } 2968 2969 // This is a dynamic symbol. Look in the GNU symbol version table. 2970 if (dot_gnu_version_sec == NULL) { 2971 // No version table. 2972 Version = ""; 2973 IsDefault = false; 2974 return object_error::success; 2975 } 2976 2977 // Determine the position in the symbol table of this entry. 2978 const char *sec_start = (const char*)base() + section->sh_offset; 2979 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize; 2980 2981 // Get the corresponding version index entry 2982 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index); 2983 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION; 2984 2985 // Special markers for unversioned symbols. 2986 if (version_index == ELF::VER_NDX_LOCAL || 2987 version_index == ELF::VER_NDX_GLOBAL) { 2988 Version = ""; 2989 IsDefault = false; 2990 return object_error::success; 2991 } 2992 2993 // Lookup this symbol in the version table 2994 LoadVersionMap(); 2995 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull()) 2996 report_fatal_error("Symbol has version index without corresponding " 2997 "define or reference entry"); 2998 const VersionMapEntry &entry = VersionMap[version_index]; 2999 3000 // Get the version name string 3001 size_t name_offset; 3002 if (entry.isVerdef()) { 3003 // The first Verdaux entry holds the name. 3004 name_offset = entry.getVerdef()->getAux()->vda_name; 3005 } else { 3006 name_offset = entry.getVernaux()->vna_name; 3007 } 3008 Version = getString(dot_dynstr_sec, name_offset); 3009 3010 // Set IsDefault 3011 if (entry.isVerdef()) { 3012 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN); 3013 } else { 3014 IsDefault = false; 3015 } 3016 3017 return object_error::success; 3018 } 3019 3020 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template 3021 /// and make these member functions? 3022 static inline error_code getELFRelocationAddend(const RelocationRef R, 3023 int64_t &Addend) { 3024 const ObjectFile *Obj = R.getObjectFile(); 3025 DataRefImpl DRI = R.getRawDataRefImpl(); 3026 // Little-endian 32-bit 3027 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 3028 return ELFObj->getRelocationAddend(DRI, Addend); 3029 3030 // Big-endian 32-bit 3031 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 3032 return ELFObj->getRelocationAddend(DRI, Addend); 3033 3034 // Little-endian 64-bit 3035 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 3036 return ELFObj->getRelocationAddend(DRI, Addend); 3037 3038 // Big-endian 64-bit 3039 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 3040 return ELFObj->getRelocationAddend(DRI, Addend); 3041 3042 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF"); 3043 } 3044 3045 /// This is a generic interface for retrieving GNU symbol version 3046 /// information from an ELFObjectFile. 3047 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj, 3048 const SymbolRef &Sym, 3049 StringRef &Version, 3050 bool &IsDefault) { 3051 // Little-endian 32-bit 3052 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) 3053 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 3054 3055 // Big-endian 32-bit 3056 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj)) 3057 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 3058 3059 // Little-endian 64-bit 3060 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj)) 3061 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 3062 3063 // Big-endian 64-bit 3064 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj)) 3065 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 3066 3067 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF"); 3068 } 3069 3070 /// This function returns the hash value for a symbol in the .dynsym section 3071 /// Name of the API remains consistent as specified in the libelf 3072 /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash 3073 static inline unsigned elf_hash(StringRef &symbolName) { 3074 unsigned h = 0, g; 3075 for (unsigned i = 0, j = symbolName.size(); i < j; i++) { 3076 h = (h << 4) + symbolName[i]; 3077 g = h & 0xf0000000L; 3078 if (g != 0) 3079 h ^= g >> 24; 3080 h &= ~g; 3081 } 3082 return h; 3083 } 3084 3085 } 3086 } 3087 3088 #endif 3089