ELFObjectFile.h 43 KB

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