ELFObjectFile.cpp 56 KB

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