ELFObjectFile.h 41 KB

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