Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2915 lines
111 KiB

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