ELFObjectFile.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file declares the ELFObjectFile template class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
  13. #define LLVM_OBJECT_ELFOBJECTFILE_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/ADT/iterator_range.h"
  20. #include "llvm/BinaryFormat/ELF.h"
  21. #include "llvm/MC/SubtargetFeature.h"
  22. #include "llvm/Object/Binary.h"
  23. #include "llvm/Object/ELF.h"
  24. #include "llvm/Object/ELFTypes.h"
  25. #include "llvm/Object/Error.h"
  26. #include "llvm/Object/ObjectFile.h"
  27. #include "llvm/Object/SymbolicFile.h"
  28. #include "llvm/Support/ARMAttributeParser.h"
  29. #include "llvm/Support/ARMBuildAttributes.h"
  30. #include "llvm/Support/Casting.h"
  31. #include "llvm/Support/Endian.h"
  32. #include "llvm/Support/Error.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include "llvm/Support/MemoryBuffer.h"
  35. #include <cassert>
  36. #include <cstdint>
  37. #include <system_error>
  38. namespace llvm {
  39. namespace object {
  40. constexpr int NumElfSymbolTypes = 8;
  41. extern const llvm::EnumEntry<unsigned> ElfSymbolTypes[NumElfSymbolTypes];
  42. class elf_symbol_iterator;
  43. class ELFObjectFileBase : public ObjectFile {
  44. friend class ELFRelocationRef;
  45. friend class ELFSectionRef;
  46. friend class ELFSymbolRef;
  47. protected:
  48. ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
  49. virtual uint16_t getEMachine() const = 0;
  50. virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
  51. virtual uint8_t getSymbolBinding(DataRefImpl Symb) const = 0;
  52. virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
  53. virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
  54. virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
  55. virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
  56. virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
  57. virtual Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
  58. virtual Error getBuildAttributes(ARMAttributeParser &Attributes) const = 0;
  59. public:
  60. using elf_symbol_iterator_range = iterator_range<elf_symbol_iterator>;
  61. virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
  62. /// Returns platform-specific object flags, if any.
  63. virtual unsigned getPlatformFlags() const = 0;
  64. elf_symbol_iterator_range symbols() const;
  65. static bool classof(const Binary *v) { return v->isELF(); }
  66. SubtargetFeatures getFeatures() const override;
  67. SubtargetFeatures getMIPSFeatures() const;
  68. SubtargetFeatures getARMFeatures() const;
  69. SubtargetFeatures getRISCVFeatures() const;
  70. void setARMSubArch(Triple &TheTriple) const override;
  71. virtual uint16_t getEType() const = 0;
  72. std::vector<std::pair<DataRefImpl, uint64_t>> getPltAddresses() const;
  73. };
  74. class ELFSectionRef : public SectionRef {
  75. public:
  76. ELFSectionRef(const SectionRef &B) : SectionRef(B) {
  77. assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
  78. }
  79. const ELFObjectFileBase *getObject() const {
  80. return cast<ELFObjectFileBase>(SectionRef::getObject());
  81. }
  82. uint32_t getType() const {
  83. return getObject()->getSectionType(getRawDataRefImpl());
  84. }
  85. uint64_t getFlags() const {
  86. return getObject()->getSectionFlags(getRawDataRefImpl());
  87. }
  88. uint64_t getOffset() const {
  89. return getObject()->getSectionOffset(getRawDataRefImpl());
  90. }
  91. };
  92. class elf_section_iterator : public section_iterator {
  93. public:
  94. elf_section_iterator(const section_iterator &B) : section_iterator(B) {
  95. assert(isa<ELFObjectFileBase>(B->getObject()));
  96. }
  97. const ELFSectionRef *operator->() const {
  98. return static_cast<const ELFSectionRef *>(section_iterator::operator->());
  99. }
  100. const ELFSectionRef &operator*() const {
  101. return static_cast<const ELFSectionRef &>(section_iterator::operator*());
  102. }
  103. };
  104. class ELFSymbolRef : public SymbolRef {
  105. public:
  106. ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
  107. assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
  108. }
  109. const ELFObjectFileBase *getObject() const {
  110. return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
  111. }
  112. uint64_t getSize() const {
  113. return getObject()->getSymbolSize(getRawDataRefImpl());
  114. }
  115. uint8_t getBinding() const {
  116. return getObject()->getSymbolBinding(getRawDataRefImpl());
  117. }
  118. uint8_t getOther() const {
  119. return getObject()->getSymbolOther(getRawDataRefImpl());
  120. }
  121. uint8_t getELFType() const {
  122. return getObject()->getSymbolELFType(getRawDataRefImpl());
  123. }
  124. StringRef getELFTypeName() const {
  125. uint8_t Type = getELFType();
  126. for (auto &EE : ElfSymbolTypes) {
  127. if (EE.Value == Type) {
  128. return EE.AltName;
  129. }
  130. }
  131. return "";
  132. }
  133. };
  134. class elf_symbol_iterator : public symbol_iterator {
  135. public:
  136. elf_symbol_iterator(const basic_symbol_iterator &B)
  137. : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
  138. cast<ELFObjectFileBase>(B->getObject()))) {}
  139. const ELFSymbolRef *operator->() const {
  140. return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
  141. }
  142. const ELFSymbolRef &operator*() const {
  143. return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
  144. }
  145. };
  146. class ELFRelocationRef : public RelocationRef {
  147. public:
  148. ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
  149. assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
  150. }
  151. const ELFObjectFileBase *getObject() const {
  152. return cast<ELFObjectFileBase>(RelocationRef::getObject());
  153. }
  154. Expected<int64_t> getAddend() const {
  155. return getObject()->getRelocationAddend(getRawDataRefImpl());
  156. }
  157. };
  158. class elf_relocation_iterator : public relocation_iterator {
  159. public:
  160. elf_relocation_iterator(const relocation_iterator &B)
  161. : relocation_iterator(RelocationRef(
  162. B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
  163. const ELFRelocationRef *operator->() const {
  164. return static_cast<const ELFRelocationRef *>(
  165. relocation_iterator::operator->());
  166. }
  167. const ELFRelocationRef &operator*() const {
  168. return static_cast<const ELFRelocationRef &>(
  169. relocation_iterator::operator*());
  170. }
  171. };
  172. inline ELFObjectFileBase::elf_symbol_iterator_range
  173. ELFObjectFileBase::symbols() const {
  174. return elf_symbol_iterator_range(symbol_begin(), symbol_end());
  175. }
  176. template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
  177. uint16_t getEMachine() const override;
  178. uint16_t getEType() const override;
  179. uint64_t getSymbolSize(DataRefImpl Sym) const override;
  180. public:
  181. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  182. using uintX_t = typename ELFT::uint;
  183. using Elf_Sym = typename ELFT::Sym;
  184. using Elf_Shdr = typename ELFT::Shdr;
  185. using Elf_Ehdr = typename ELFT::Ehdr;
  186. using Elf_Rel = typename ELFT::Rel;
  187. using Elf_Rela = typename ELFT::Rela;
  188. using Elf_Dyn = typename ELFT::Dyn;
  189. private:
  190. ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
  191. const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
  192. ArrayRef<Elf_Word> ShndxTable);
  193. protected:
  194. ELFFile<ELFT> EF;
  195. const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
  196. const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
  197. ArrayRef<Elf_Word> ShndxTable;
  198. void moveSymbolNext(DataRefImpl &Symb) const override;
  199. Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
  200. Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
  201. uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
  202. uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
  203. uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
  204. uint32_t getSymbolFlags(DataRefImpl Symb) const override;
  205. uint8_t getSymbolBinding(DataRefImpl Symb) const override;
  206. uint8_t getSymbolOther(DataRefImpl Symb) const override;
  207. uint8_t getSymbolELFType(DataRefImpl Symb) const override;
  208. Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
  209. Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb,
  210. const Elf_Shdr *SymTab) const;
  211. Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
  212. void moveSectionNext(DataRefImpl &Sec) const override;
  213. Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
  214. uint64_t getSectionAddress(DataRefImpl Sec) const override;
  215. uint64_t getSectionIndex(DataRefImpl Sec) const override;
  216. uint64_t getSectionSize(DataRefImpl Sec) const override;
  217. Expected<ArrayRef<uint8_t>>
  218. getSectionContents(DataRefImpl Sec) const override;
  219. uint64_t getSectionAlignment(DataRefImpl Sec) const override;
  220. bool isSectionCompressed(DataRefImpl Sec) const override;
  221. bool isSectionText(DataRefImpl Sec) const override;
  222. bool isSectionData(DataRefImpl Sec) const override;
  223. bool isSectionBSS(DataRefImpl Sec) const override;
  224. bool isSectionVirtual(DataRefImpl Sec) const override;
  225. bool isBerkeleyText(DataRefImpl Sec) const override;
  226. bool isBerkeleyData(DataRefImpl Sec) const override;
  227. relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
  228. relocation_iterator section_rel_end(DataRefImpl Sec) const override;
  229. std::vector<SectionRef> dynamic_relocation_sections() const override;
  230. section_iterator getRelocatedSection(DataRefImpl Sec) const override;
  231. void moveRelocationNext(DataRefImpl &Rel) const override;
  232. uint64_t getRelocationOffset(DataRefImpl Rel) const override;
  233. symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
  234. uint64_t getRelocationType(DataRefImpl Rel) const override;
  235. void getRelocationTypeName(DataRefImpl Rel,
  236. SmallVectorImpl<char> &Result) const override;
  237. uint32_t getSectionType(DataRefImpl Sec) const override;
  238. uint64_t getSectionFlags(DataRefImpl Sec) const override;
  239. uint64_t getSectionOffset(DataRefImpl Sec) const override;
  240. StringRef getRelocationTypeName(uint32_t Type) const;
  241. /// Get the relocation section that contains \a Rel.
  242. const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
  243. auto RelSecOrErr = EF.getSection(Rel.d.a);
  244. if (!RelSecOrErr)
  245. report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
  246. return *RelSecOrErr;
  247. }
  248. DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
  249. DataRefImpl DRI;
  250. if (!SymTable) {
  251. DRI.d.a = 0;
  252. DRI.d.b = 0;
  253. return DRI;
  254. }
  255. assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
  256. SymTable->sh_type == ELF::SHT_DYNSYM);
  257. auto SectionsOrErr = EF.sections();
  258. if (!SectionsOrErr) {
  259. DRI.d.a = 0;
  260. DRI.d.b = 0;
  261. return DRI;
  262. }
  263. uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
  264. unsigned SymTableIndex =
  265. (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
  266. DRI.d.a = SymTableIndex;
  267. DRI.d.b = SymbolNum;
  268. return DRI;
  269. }
  270. const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
  271. return reinterpret_cast<const Elf_Shdr *>(Sec.p);
  272. }
  273. DataRefImpl toDRI(const Elf_Shdr *Sec) const {
  274. DataRefImpl DRI;
  275. DRI.p = reinterpret_cast<uintptr_t>(Sec);
  276. return DRI;
  277. }
  278. DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
  279. DataRefImpl DRI;
  280. DRI.p = reinterpret_cast<uintptr_t>(Dyn);
  281. return DRI;
  282. }
  283. bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
  284. unsigned char Binding = ESym->getBinding();
  285. unsigned char Visibility = ESym->getVisibility();
  286. // A symbol is exported if its binding is either GLOBAL or WEAK, and its
  287. // visibility is either DEFAULT or PROTECTED. All other symbols are not
  288. // exported.
  289. return (
  290. (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
  291. Binding == ELF::STB_GNU_UNIQUE) &&
  292. (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
  293. }
  294. Error getBuildAttributes(ARMAttributeParser &Attributes) const override {
  295. auto SectionsOrErr = EF.sections();
  296. if (!SectionsOrErr)
  297. return SectionsOrErr.takeError();
  298. for (const Elf_Shdr &Sec : *SectionsOrErr) {
  299. if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) {
  300. auto ErrorOrContents = EF.getSectionContents(&Sec);
  301. if (!ErrorOrContents)
  302. return ErrorOrContents.takeError();
  303. auto Contents = ErrorOrContents.get();
  304. if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
  305. return Error::success();
  306. Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
  307. break;
  308. }
  309. }
  310. return Error::success();
  311. }
  312. // This flag is used for classof, to distinguish ELFObjectFile from
  313. // its subclass. If more subclasses will be created, this flag will
  314. // have to become an enum.
  315. bool isDyldELFObject;
  316. public:
  317. ELFObjectFile(ELFObjectFile<ELFT> &&Other);
  318. static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object);
  319. const Elf_Rel *getRel(DataRefImpl Rel) const;
  320. const Elf_Rela *getRela(DataRefImpl Rela) const;
  321. const Elf_Sym *getSymbol(DataRefImpl Sym) const {
  322. auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
  323. if (!Ret)
  324. report_fatal_error(errorToErrorCode(Ret.takeError()).message());
  325. return *Ret;
  326. }
  327. const Elf_Shdr *getSection(DataRefImpl Sec) const {
  328. return reinterpret_cast<const Elf_Shdr *>(Sec.p);
  329. }
  330. basic_symbol_iterator symbol_begin() const override;
  331. basic_symbol_iterator symbol_end() const override;
  332. elf_symbol_iterator dynamic_symbol_begin() const;
  333. elf_symbol_iterator dynamic_symbol_end() const;
  334. section_iterator section_begin() const override;
  335. section_iterator section_end() const override;
  336. Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
  337. uint8_t getBytesInAddress() const override;
  338. StringRef getFileFormatName() const override;
  339. Triple::ArchType getArch() const override;
  340. Expected<uint64_t> getStartAddress() const override;
  341. unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
  342. const ELFFile<ELFT> *getELFFile() const { return &EF; }
  343. bool isDyldType() const { return isDyldELFObject; }
  344. static bool classof(const Binary *v) {
  345. return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
  346. ELFT::Is64Bits);
  347. }
  348. elf_symbol_iterator_range getDynamicSymbolIterators() const override;
  349. bool isRelocatableObject() const override;
  350. };
  351. using ELF32LEObjectFile = ELFObjectFile<ELF32LE>;
  352. using ELF64LEObjectFile = ELFObjectFile<ELF64LE>;
  353. using ELF32BEObjectFile = ELFObjectFile<ELF32BE>;
  354. using ELF64BEObjectFile = ELFObjectFile<ELF64BE>;
  355. template <class ELFT>
  356. void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
  357. ++Sym.d.b;
  358. }
  359. template <class ELFT>
  360. Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
  361. const Elf_Sym *ESym = getSymbol(Sym);
  362. auto SymTabOrErr = EF.getSection(Sym.d.a);
  363. if (!SymTabOrErr)
  364. return SymTabOrErr.takeError();
  365. const Elf_Shdr *SymTableSec = *SymTabOrErr;
  366. auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
  367. if (!StrTabOrErr)
  368. return StrTabOrErr.takeError();
  369. const Elf_Shdr *StringTableSec = *StrTabOrErr;
  370. auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
  371. if (!SymStrTabOrErr)
  372. return SymStrTabOrErr.takeError();
  373. Expected<StringRef> Name = ESym->getName(*SymStrTabOrErr);
  374. // If the symbol name is empty use the section name.
  375. if ((!Name || Name->empty()) && ESym->getType() == ELF::STT_SECTION) {
  376. StringRef SecName;
  377. Expected<section_iterator> Sec = getSymbolSection(Sym);
  378. if (Sec && !(*Sec)->getName(SecName))
  379. return SecName;
  380. }
  381. return Name;
  382. }
  383. template <class ELFT>
  384. uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
  385. return getSection(Sec)->sh_flags;
  386. }
  387. template <class ELFT>
  388. uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
  389. return getSection(Sec)->sh_type;
  390. }
  391. template <class ELFT>
  392. uint64_t ELFObjectFile<ELFT>::getSectionOffset(DataRefImpl Sec) const {
  393. return getSection(Sec)->sh_offset;
  394. }
  395. template <class ELFT>
  396. uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
  397. const Elf_Sym *ESym = getSymbol(Symb);
  398. uint64_t Ret = ESym->st_value;
  399. if (ESym->st_shndx == ELF::SHN_ABS)
  400. return Ret;
  401. const Elf_Ehdr *Header = EF.getHeader();
  402. // Clear the ARM/Thumb or microMIPS indicator flag.
  403. if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
  404. ESym->getType() == ELF::STT_FUNC)
  405. Ret &= ~1;
  406. return Ret;
  407. }
  408. template <class ELFT>
  409. Expected<uint64_t>
  410. ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
  411. uint64_t Result = getSymbolValue(Symb);
  412. const Elf_Sym *ESym = getSymbol(Symb);
  413. switch (ESym->st_shndx) {
  414. case ELF::SHN_COMMON:
  415. case ELF::SHN_UNDEF:
  416. case ELF::SHN_ABS:
  417. return Result;
  418. }
  419. const Elf_Ehdr *Header = EF.getHeader();
  420. auto SymTabOrErr = EF.getSection(Symb.d.a);
  421. if (!SymTabOrErr)
  422. return SymTabOrErr.takeError();
  423. const Elf_Shdr *SymTab = *SymTabOrErr;
  424. if (Header->e_type == ELF::ET_REL) {
  425. auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
  426. if (!SectionOrErr)
  427. return SectionOrErr.takeError();
  428. const Elf_Shdr *Section = *SectionOrErr;
  429. if (Section)
  430. Result += Section->sh_addr;
  431. }
  432. return Result;
  433. }
  434. template <class ELFT>
  435. uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
  436. const Elf_Sym *Sym = getSymbol(Symb);
  437. if (Sym->st_shndx == ELF::SHN_COMMON)
  438. return Sym->st_value;
  439. return 0;
  440. }
  441. template <class ELFT>
  442. uint16_t ELFObjectFile<ELFT>::getEMachine() const {
  443. return EF.getHeader()->e_machine;
  444. }
  445. template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
  446. return EF.getHeader()->e_type;
  447. }
  448. template <class ELFT>
  449. uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
  450. return getSymbol(Sym)->st_size;
  451. }
  452. template <class ELFT>
  453. uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
  454. return getSymbol(Symb)->st_size;
  455. }
  456. template <class ELFT>
  457. uint8_t ELFObjectFile<ELFT>::getSymbolBinding(DataRefImpl Symb) const {
  458. return getSymbol(Symb)->getBinding();
  459. }
  460. template <class ELFT>
  461. uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
  462. return getSymbol(Symb)->st_other;
  463. }
  464. template <class ELFT>
  465. uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
  466. return getSymbol(Symb)->getType();
  467. }
  468. template <class ELFT>
  469. Expected<SymbolRef::Type>
  470. ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
  471. const Elf_Sym *ESym = getSymbol(Symb);
  472. switch (ESym->getType()) {
  473. case ELF::STT_NOTYPE:
  474. return SymbolRef::ST_Unknown;
  475. case ELF::STT_SECTION:
  476. return SymbolRef::ST_Debug;
  477. case ELF::STT_FILE:
  478. return SymbolRef::ST_File;
  479. case ELF::STT_FUNC:
  480. return SymbolRef::ST_Function;
  481. case ELF::STT_OBJECT:
  482. case ELF::STT_COMMON:
  483. case ELF::STT_TLS:
  484. return SymbolRef::ST_Data;
  485. default:
  486. return SymbolRef::ST_Other;
  487. }
  488. }
  489. template <class ELFT>
  490. uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
  491. const Elf_Sym *ESym = getSymbol(Sym);
  492. uint32_t Result = SymbolRef::SF_None;
  493. if (ESym->getBinding() != ELF::STB_LOCAL)
  494. Result |= SymbolRef::SF_Global;
  495. if (ESym->getBinding() == ELF::STB_WEAK)
  496. Result |= SymbolRef::SF_Weak;
  497. if (ESym->st_shndx == ELF::SHN_ABS)
  498. Result |= SymbolRef::SF_Absolute;
  499. if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
  500. Result |= SymbolRef::SF_FormatSpecific;
  501. auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
  502. if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
  503. Result |= SymbolRef::SF_FormatSpecific;
  504. auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
  505. if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
  506. Result |= SymbolRef::SF_FormatSpecific;
  507. if (EF.getHeader()->e_machine == ELF::EM_ARM) {
  508. if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
  509. StringRef Name = *NameOrErr;
  510. if (Name.startswith("$d") || Name.startswith("$t") ||
  511. Name.startswith("$a"))
  512. Result |= SymbolRef::SF_FormatSpecific;
  513. } else {
  514. // TODO: Actually report errors helpfully.
  515. consumeError(NameOrErr.takeError());
  516. }
  517. if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
  518. Result |= SymbolRef::SF_Thumb;
  519. }
  520. if (ESym->st_shndx == ELF::SHN_UNDEF)
  521. Result |= SymbolRef::SF_Undefined;
  522. if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
  523. Result |= SymbolRef::SF_Common;
  524. if (isExportedToOtherDSO(ESym))
  525. Result |= SymbolRef::SF_Exported;
  526. if (ESym->getVisibility() == ELF::STV_HIDDEN)
  527. Result |= SymbolRef::SF_Hidden;
  528. return Result;
  529. }
  530. template <class ELFT>
  531. Expected<section_iterator>
  532. ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
  533. const Elf_Shdr *SymTab) const {
  534. auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
  535. if (!ESecOrErr)
  536. return ESecOrErr.takeError();
  537. const Elf_Shdr *ESec = *ESecOrErr;
  538. if (!ESec)
  539. return section_end();
  540. DataRefImpl Sec;
  541. Sec.p = reinterpret_cast<intptr_t>(ESec);
  542. return section_iterator(SectionRef(Sec, this));
  543. }
  544. template <class ELFT>
  545. Expected<section_iterator>
  546. ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
  547. const Elf_Sym *Sym = getSymbol(Symb);
  548. auto SymTabOrErr = EF.getSection(Symb.d.a);
  549. if (!SymTabOrErr)
  550. return SymTabOrErr.takeError();
  551. const Elf_Shdr *SymTab = *SymTabOrErr;
  552. return getSymbolSection(Sym, SymTab);
  553. }
  554. template <class ELFT>
  555. void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
  556. const Elf_Shdr *ESec = getSection(Sec);
  557. Sec = toDRI(++ESec);
  558. }
  559. template <class ELFT>
  560. Expected<StringRef> ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec) const {
  561. return EF.getSectionName(&*getSection(Sec));
  562. }
  563. template <class ELFT>
  564. uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
  565. return getSection(Sec)->sh_addr;
  566. }
  567. template <class ELFT>
  568. uint64_t ELFObjectFile<ELFT>::getSectionIndex(DataRefImpl Sec) const {
  569. auto SectionsOrErr = EF.sections();
  570. handleAllErrors(std::move(SectionsOrErr.takeError()),
  571. [](const ErrorInfoBase &) {
  572. llvm_unreachable("unable to get section index");
  573. });
  574. const Elf_Shdr *First = SectionsOrErr->begin();
  575. return getSection(Sec) - First;
  576. }
  577. template <class ELFT>
  578. uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
  579. return getSection(Sec)->sh_size;
  580. }
  581. template <class ELFT>
  582. Expected<ArrayRef<uint8_t>>
  583. ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
  584. const Elf_Shdr *EShdr = getSection(Sec);
  585. if (std::error_code EC =
  586. checkOffset(getMemoryBufferRef(),
  587. (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
  588. return errorCodeToError(EC);
  589. return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
  590. EShdr->sh_size);
  591. }
  592. template <class ELFT>
  593. uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
  594. return getSection(Sec)->sh_addralign;
  595. }
  596. template <class ELFT>
  597. bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const {
  598. return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
  599. }
  600. template <class ELFT>
  601. bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
  602. return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
  603. }
  604. template <class ELFT>
  605. bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
  606. const Elf_Shdr *EShdr = getSection(Sec);
  607. return EShdr->sh_type == ELF::SHT_PROGBITS &&
  608. EShdr->sh_flags & ELF::SHF_ALLOC &&
  609. !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
  610. }
  611. template <class ELFT>
  612. bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
  613. const Elf_Shdr *EShdr = getSection(Sec);
  614. return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
  615. EShdr->sh_type == ELF::SHT_NOBITS;
  616. }
  617. template <class ELFT>
  618. std::vector<SectionRef>
  619. ELFObjectFile<ELFT>::dynamic_relocation_sections() const {
  620. std::vector<SectionRef> Res;
  621. std::vector<uintptr_t> Offsets;
  622. auto SectionsOrErr = EF.sections();
  623. if (!SectionsOrErr)
  624. return Res;
  625. for (const Elf_Shdr &Sec : *SectionsOrErr) {
  626. if (Sec.sh_type != ELF::SHT_DYNAMIC)
  627. continue;
  628. Elf_Dyn *Dynamic =
  629. reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
  630. for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
  631. if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
  632. Dynamic->d_tag == ELF::DT_JMPREL) {
  633. Offsets.push_back(Dynamic->d_un.d_val);
  634. }
  635. }
  636. }
  637. for (const Elf_Shdr &Sec : *SectionsOrErr) {
  638. if (is_contained(Offsets, Sec.sh_offset))
  639. Res.emplace_back(toDRI(&Sec), this);
  640. }
  641. return Res;
  642. }
  643. template <class ELFT>
  644. bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
  645. return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
  646. }
  647. template <class ELFT>
  648. bool ELFObjectFile<ELFT>::isBerkeleyText(DataRefImpl Sec) const {
  649. return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
  650. (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
  651. !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
  652. }
  653. template <class ELFT>
  654. bool ELFObjectFile<ELFT>::isBerkeleyData(DataRefImpl Sec) const {
  655. const Elf_Shdr *EShdr = getSection(Sec);
  656. return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
  657. EShdr->sh_flags & ELF::SHF_ALLOC;
  658. }
  659. template <class ELFT>
  660. relocation_iterator
  661. ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
  662. DataRefImpl RelData;
  663. auto SectionsOrErr = EF.sections();
  664. if (!SectionsOrErr)
  665. return relocation_iterator(RelocationRef());
  666. uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
  667. RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
  668. RelData.d.b = 0;
  669. return relocation_iterator(RelocationRef(RelData, this));
  670. }
  671. template <class ELFT>
  672. relocation_iterator
  673. ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
  674. const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
  675. relocation_iterator Begin = section_rel_begin(Sec);
  676. if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
  677. return Begin;
  678. DataRefImpl RelData = Begin->getRawDataRefImpl();
  679. const Elf_Shdr *RelSec = getRelSection(RelData);
  680. // Error check sh_link here so that getRelocationSymbol can just use it.
  681. auto SymSecOrErr = EF.getSection(RelSec->sh_link);
  682. if (!SymSecOrErr)
  683. report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
  684. RelData.d.b += S->sh_size / S->sh_entsize;
  685. return relocation_iterator(RelocationRef(RelData, this));
  686. }
  687. template <class ELFT>
  688. section_iterator
  689. ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
  690. if (EF.getHeader()->e_type != ELF::ET_REL)
  691. return section_end();
  692. const Elf_Shdr *EShdr = getSection(Sec);
  693. uintX_t Type = EShdr->sh_type;
  694. if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
  695. return section_end();
  696. auto R = EF.getSection(EShdr->sh_info);
  697. if (!R)
  698. report_fatal_error(errorToErrorCode(R.takeError()).message());
  699. return section_iterator(SectionRef(toDRI(*R), this));
  700. }
  701. // Relocations
  702. template <class ELFT>
  703. void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
  704. ++Rel.d.b;
  705. }
  706. template <class ELFT>
  707. symbol_iterator
  708. ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
  709. uint32_t symbolIdx;
  710. const Elf_Shdr *sec = getRelSection(Rel);
  711. if (sec->sh_type == ELF::SHT_REL)
  712. symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
  713. else
  714. symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
  715. if (!symbolIdx)
  716. return symbol_end();
  717. // FIXME: error check symbolIdx
  718. DataRefImpl SymbolData;
  719. SymbolData.d.a = sec->sh_link;
  720. SymbolData.d.b = symbolIdx;
  721. return symbol_iterator(SymbolRef(SymbolData, this));
  722. }
  723. template <class ELFT>
  724. uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
  725. const Elf_Shdr *sec = getRelSection(Rel);
  726. if (sec->sh_type == ELF::SHT_REL)
  727. return getRel(Rel)->r_offset;
  728. return getRela(Rel)->r_offset;
  729. }
  730. template <class ELFT>
  731. uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
  732. const Elf_Shdr *sec = getRelSection(Rel);
  733. if (sec->sh_type == ELF::SHT_REL)
  734. return getRel(Rel)->getType(EF.isMips64EL());
  735. else
  736. return getRela(Rel)->getType(EF.isMips64EL());
  737. }
  738. template <class ELFT>
  739. StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
  740. return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
  741. }
  742. template <class ELFT>
  743. void ELFObjectFile<ELFT>::getRelocationTypeName(
  744. DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
  745. uint32_t type = getRelocationType(Rel);
  746. EF.getRelocationTypeName(type, Result);
  747. }
  748. template <class ELFT>
  749. Expected<int64_t>
  750. ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
  751. if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
  752. return createError("Section is not SHT_RELA");
  753. return (int64_t)getRela(Rel)->r_addend;
  754. }
  755. template <class ELFT>
  756. const typename ELFObjectFile<ELFT>::Elf_Rel *
  757. ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
  758. assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
  759. auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
  760. if (!Ret)
  761. report_fatal_error(errorToErrorCode(Ret.takeError()).message());
  762. return *Ret;
  763. }
  764. template <class ELFT>
  765. const typename ELFObjectFile<ELFT>::Elf_Rela *
  766. ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
  767. assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
  768. auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
  769. if (!Ret)
  770. report_fatal_error(errorToErrorCode(Ret.takeError()).message());
  771. return *Ret;
  772. }
  773. template <class ELFT>
  774. Expected<ELFObjectFile<ELFT>>
  775. ELFObjectFile<ELFT>::create(MemoryBufferRef Object) {
  776. auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
  777. if (Error E = EFOrErr.takeError())
  778. return std::move(E);
  779. auto EF = std::move(*EFOrErr);
  780. auto SectionsOrErr = EF.sections();
  781. if (!SectionsOrErr)
  782. return SectionsOrErr.takeError();
  783. const Elf_Shdr *DotDynSymSec = nullptr;
  784. const Elf_Shdr *DotSymtabSec = nullptr;
  785. ArrayRef<Elf_Word> ShndxTable;
  786. for (const Elf_Shdr &Sec : *SectionsOrErr) {
  787. switch (Sec.sh_type) {
  788. case ELF::SHT_DYNSYM: {
  789. if (!DotDynSymSec)
  790. DotDynSymSec = &Sec;
  791. break;
  792. }
  793. case ELF::SHT_SYMTAB: {
  794. if (!DotSymtabSec)
  795. DotSymtabSec = &Sec;
  796. break;
  797. }
  798. case ELF::SHT_SYMTAB_SHNDX: {
  799. auto TableOrErr = EF.getSHNDXTable(Sec);
  800. if (!TableOrErr)
  801. return TableOrErr.takeError();
  802. ShndxTable = *TableOrErr;
  803. break;
  804. }
  805. }
  806. }
  807. return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
  808. ShndxTable);
  809. }
  810. template <class ELFT>
  811. ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
  812. const Elf_Shdr *DotDynSymSec,
  813. const Elf_Shdr *DotSymtabSec,
  814. ArrayRef<Elf_Word> ShndxTable)
  815. : ELFObjectFileBase(
  816. getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
  817. Object),
  818. EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
  819. ShndxTable(ShndxTable) {}
  820. template <class ELFT>
  821. ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
  822. : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
  823. Other.DotSymtabSec, Other.ShndxTable) {}
  824. template <class ELFT>
  825. basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const {
  826. DataRefImpl Sym =
  827. toDRI(DotSymtabSec,
  828. DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
  829. return basic_symbol_iterator(SymbolRef(Sym, this));
  830. }
  831. template <class ELFT>
  832. basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const {
  833. const Elf_Shdr *SymTab = DotSymtabSec;
  834. if (!SymTab)
  835. return symbol_begin();
  836. DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
  837. return basic_symbol_iterator(SymbolRef(Sym, this));
  838. }
  839. template <class ELFT>
  840. elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
  841. DataRefImpl Sym = toDRI(DotDynSymSec, 0);
  842. return symbol_iterator(SymbolRef(Sym, this));
  843. }
  844. template <class ELFT>
  845. elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
  846. const Elf_Shdr *SymTab = DotDynSymSec;
  847. if (!SymTab)
  848. return dynamic_symbol_begin();
  849. DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
  850. return basic_symbol_iterator(SymbolRef(Sym, this));
  851. }
  852. template <class ELFT>
  853. section_iterator ELFObjectFile<ELFT>::section_begin() const {
  854. auto SectionsOrErr = EF.sections();
  855. if (!SectionsOrErr)
  856. return section_iterator(SectionRef());
  857. return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
  858. }
  859. template <class ELFT>
  860. section_iterator ELFObjectFile<ELFT>::section_end() const {
  861. auto SectionsOrErr = EF.sections();
  862. if (!SectionsOrErr)
  863. return section_iterator(SectionRef());
  864. return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
  865. }
  866. template <class ELFT>
  867. uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
  868. return ELFT::Is64Bits ? 8 : 4;
  869. }
  870. template <class ELFT>
  871. StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
  872. bool IsLittleEndian = ELFT::TargetEndianness == support::little;
  873. switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
  874. case ELF::ELFCLASS32:
  875. switch (EF.getHeader()->e_machine) {
  876. case ELF::EM_386:
  877. return "ELF32-i386";
  878. case ELF::EM_IAMCU:
  879. return "ELF32-iamcu";
  880. case ELF::EM_X86_64:
  881. return "ELF32-x86-64";
  882. case ELF::EM_ARM:
  883. return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
  884. case ELF::EM_AVR:
  885. return "ELF32-avr";
  886. case ELF::EM_HEXAGON:
  887. return "ELF32-hexagon";
  888. case ELF::EM_LANAI:
  889. return "ELF32-lanai";
  890. case ELF::EM_MIPS:
  891. return "ELF32-mips";
  892. case ELF::EM_MSP430:
  893. return "ELF32-msp430";
  894. case ELF::EM_PPC:
  895. return "ELF32-ppc";
  896. case ELF::EM_RISCV:
  897. return "ELF32-riscv";
  898. case ELF::EM_SPARC:
  899. case ELF::EM_SPARC32PLUS:
  900. return "ELF32-sparc";
  901. case ELF::EM_AMDGPU:
  902. return "ELF32-amdgpu";
  903. default:
  904. return "ELF32-unknown";
  905. }
  906. case ELF::ELFCLASS64:
  907. switch (EF.getHeader()->e_machine) {
  908. case ELF::EM_386:
  909. return "ELF64-i386";
  910. case ELF::EM_X86_64:
  911. return "ELF64-x86-64";
  912. case ELF::EM_AARCH64:
  913. return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
  914. case ELF::EM_PPC64:
  915. return "ELF64-ppc64";
  916. case ELF::EM_RISCV:
  917. return "ELF64-riscv";
  918. case ELF::EM_S390:
  919. return "ELF64-s390";
  920. case ELF::EM_SPARCV9:
  921. return "ELF64-sparc";
  922. case ELF::EM_MIPS:
  923. return "ELF64-mips";
  924. case ELF::EM_AMDGPU:
  925. return "ELF64-amdgpu";
  926. case ELF::EM_BPF:
  927. return "ELF64-BPF";
  928. default:
  929. return "ELF64-unknown";
  930. }
  931. default:
  932. // FIXME: Proper error handling.
  933. report_fatal_error("Invalid ELFCLASS!");
  934. }
  935. }
  936. template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
  937. bool IsLittleEndian = ELFT::TargetEndianness == support::little;
  938. switch (EF.getHeader()->e_machine) {
  939. case ELF::EM_386:
  940. case ELF::EM_IAMCU:
  941. return Triple::x86;
  942. case ELF::EM_X86_64:
  943. return Triple::x86_64;
  944. case ELF::EM_AARCH64:
  945. return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
  946. case ELF::EM_ARM:
  947. return Triple::arm;
  948. case ELF::EM_AVR:
  949. return Triple::avr;
  950. case ELF::EM_HEXAGON:
  951. return Triple::hexagon;
  952. case ELF::EM_LANAI:
  953. return Triple::lanai;
  954. case ELF::EM_MIPS:
  955. switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
  956. case ELF::ELFCLASS32:
  957. return IsLittleEndian ? Triple::mipsel : Triple::mips;
  958. case ELF::ELFCLASS64:
  959. return IsLittleEndian ? Triple::mips64el : Triple::mips64;
  960. default:
  961. report_fatal_error("Invalid ELFCLASS!");
  962. }
  963. case ELF::EM_MSP430:
  964. return Triple::msp430;
  965. case ELF::EM_PPC:
  966. return Triple::ppc;
  967. case ELF::EM_PPC64:
  968. return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
  969. case ELF::EM_RISCV:
  970. switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
  971. case ELF::ELFCLASS32:
  972. return Triple::riscv32;
  973. case ELF::ELFCLASS64:
  974. return Triple::riscv64;
  975. default:
  976. report_fatal_error("Invalid ELFCLASS!");
  977. }
  978. case ELF::EM_S390:
  979. return Triple::systemz;
  980. case ELF::EM_SPARC:
  981. case ELF::EM_SPARC32PLUS:
  982. return IsLittleEndian ? Triple::sparcel : Triple::sparc;
  983. case ELF::EM_SPARCV9:
  984. return Triple::sparcv9;
  985. case ELF::EM_AMDGPU: {
  986. if (!IsLittleEndian)
  987. return Triple::UnknownArch;
  988. unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH;
  989. if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
  990. MACH <= ELF::EF_AMDGPU_MACH_R600_LAST)
  991. return Triple::r600;
  992. if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
  993. MACH <= ELF::EF_AMDGPU_MACH_AMDGCN_LAST)
  994. return Triple::amdgcn;
  995. return Triple::UnknownArch;
  996. }
  997. case ELF::EM_BPF:
  998. return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
  999. default:
  1000. return Triple::UnknownArch;
  1001. }
  1002. }
  1003. template <class ELFT>
  1004. Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const {
  1005. return EF.getHeader()->e_entry;
  1006. }
  1007. template <class ELFT>
  1008. ELFObjectFileBase::elf_symbol_iterator_range
  1009. ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
  1010. return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
  1011. }
  1012. template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
  1013. return EF.getHeader()->e_type == ELF::ET_REL;
  1014. }
  1015. } // end namespace object
  1016. } // end namespace llvm
  1017. #endif // LLVM_OBJECT_ELFOBJECTFILE_H