ELFObjectFile.h 41 KB

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