ELFObjectFile.h 42 KB

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