ELFObjectFile.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the ELFObjectFile template class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
  14. #define LLVM_OBJECT_ELFOBJECTFILE_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/PointerIntPair.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringSwitch.h"
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/Object/ELF.h"
  21. #include "llvm/Object/ObjectFile.h"
  22. #include "llvm/Support/Casting.h"
  23. #include "llvm/Support/ELF.h"
  24. #include "llvm/Support/Endian.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/MemoryBuffer.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <algorithm>
  29. #include <cctype>
  30. #include <limits>
  31. #include <utility>
  32. namespace llvm {
  33. namespace object {
  34. class elf_symbol_iterator;
  35. class ELFSymbolRef;
  36. class ELFRelocationRef;
  37. class ELFObjectFileBase : public ObjectFile {
  38. friend class ELFSymbolRef;
  39. friend class ELFSectionRef;
  40. friend class ELFRelocationRef;
  41. protected:
  42. ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
  43. virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
  44. virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
  45. virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
  46. virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
  47. virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
  48. virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
  49. public:
  50. typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
  51. virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
  52. elf_symbol_iterator_range symbols() const;
  53. static inline bool classof(const Binary *v) { return v->isELF(); }
  54. };
  55. class ELFSectionRef : public SectionRef {
  56. public:
  57. ELFSectionRef(const SectionRef &B) : SectionRef(B) {
  58. assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
  59. }
  60. const ELFObjectFileBase *getObject() const {
  61. return cast<ELFObjectFileBase>(SectionRef::getObject());
  62. }
  63. uint32_t getType() const {
  64. return getObject()->getSectionType(getRawDataRefImpl());
  65. }
  66. uint64_t getFlags() const {
  67. return getObject()->getSectionFlags(getRawDataRefImpl());
  68. }
  69. };
  70. class elf_section_iterator : public section_iterator {
  71. public:
  72. elf_section_iterator(const section_iterator &B) : section_iterator(B) {
  73. assert(isa<ELFObjectFileBase>(B->getObject()));
  74. }
  75. const ELFSectionRef *operator->() const {
  76. return static_cast<const ELFSectionRef *>(section_iterator::operator->());
  77. }
  78. const ELFSectionRef &operator*() const {
  79. return static_cast<const ELFSectionRef &>(section_iterator::operator*());
  80. }
  81. };
  82. class ELFSymbolRef : public SymbolRef {
  83. public:
  84. ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
  85. assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
  86. }
  87. const ELFObjectFileBase *getObject() const {
  88. return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
  89. }
  90. uint64_t getSize() const {
  91. return getObject()->getSymbolSize(getRawDataRefImpl());
  92. }
  93. uint8_t getOther() const {
  94. return getObject()->getSymbolOther(getRawDataRefImpl());
  95. }
  96. uint8_t getELFType() const {
  97. return getObject()->getSymbolELFType(getRawDataRefImpl());
  98. }
  99. };
  100. class elf_symbol_iterator : public symbol_iterator {
  101. public:
  102. elf_symbol_iterator(const basic_symbol_iterator &B)
  103. : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
  104. cast<ELFObjectFileBase>(B->getObject()))) {}
  105. const ELFSymbolRef *operator->() const {
  106. return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
  107. }
  108. const ELFSymbolRef &operator*() const {
  109. return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
  110. }
  111. };
  112. class ELFRelocationRef : public RelocationRef {
  113. public:
  114. ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
  115. assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
  116. }
  117. const ELFObjectFileBase *getObject() const {
  118. return cast<ELFObjectFileBase>(RelocationRef::getObject());
  119. }
  120. ErrorOr<int64_t> getAddend() const {
  121. return getObject()->getRelocationAddend(getRawDataRefImpl());
  122. }
  123. };
  124. class elf_relocation_iterator : public relocation_iterator {
  125. public:
  126. elf_relocation_iterator(const relocation_iterator &B)
  127. : relocation_iterator(RelocationRef(
  128. B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
  129. const ELFRelocationRef *operator->() const {
  130. return static_cast<const ELFRelocationRef *>(
  131. relocation_iterator::operator->());
  132. }
  133. const ELFRelocationRef &operator*() const {
  134. return static_cast<const ELFRelocationRef &>(
  135. relocation_iterator::operator*());
  136. }
  137. };
  138. inline ELFObjectFileBase::elf_symbol_iterator_range
  139. ELFObjectFileBase::symbols() const {
  140. return elf_symbol_iterator_range(symbol_begin(), symbol_end());
  141. }
  142. template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
  143. uint64_t getSymbolSize(DataRefImpl Sym) const override;
  144. public:
  145. LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
  146. typedef typename ELFFile<ELFT>::uintX_t uintX_t;
  147. typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
  148. typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
  149. typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
  150. typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
  151. typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
  152. typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
  153. typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
  154. protected:
  155. ELFFile<ELFT> EF;
  156. void moveSymbolNext(DataRefImpl &Symb) const override;
  157. ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
  158. ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
  159. uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
  160. uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
  161. uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
  162. uint32_t getSymbolFlags(DataRefImpl Symb) const override;
  163. uint8_t getSymbolOther(DataRefImpl Symb) const override;
  164. uint8_t getSymbolELFType(DataRefImpl Symb) const override;
  165. SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
  166. section_iterator getSymbolSection(const Elf_Sym *Symb) const;
  167. std::error_code getSymbolSection(DataRefImpl Symb,
  168. section_iterator &Res) const override;
  169. void moveSectionNext(DataRefImpl &Sec) const override;
  170. std::error_code getSectionName(DataRefImpl Sec,
  171. StringRef &Res) const override;
  172. uint64_t getSectionAddress(DataRefImpl Sec) const override;
  173. uint64_t getSectionSize(DataRefImpl Sec) const override;
  174. std::error_code getSectionContents(DataRefImpl Sec,
  175. StringRef &Res) const override;
  176. uint64_t getSectionAlignment(DataRefImpl Sec) const override;
  177. bool isSectionText(DataRefImpl Sec) const override;
  178. bool isSectionData(DataRefImpl Sec) const override;
  179. bool isSectionBSS(DataRefImpl Sec) const override;
  180. bool isSectionVirtual(DataRefImpl Sec) const override;
  181. relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
  182. relocation_iterator section_rel_end(DataRefImpl Sec) const override;
  183. section_iterator getRelocatedSection(DataRefImpl Sec) const override;
  184. void moveRelocationNext(DataRefImpl &Rel) const override;
  185. uint64_t getRelocationOffset(DataRefImpl Rel) const override;
  186. symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
  187. uint64_t getRelocationType(DataRefImpl Rel) const override;
  188. void getRelocationTypeName(DataRefImpl Rel,
  189. SmallVectorImpl<char> &Result) const override;
  190. uint32_t getSectionType(DataRefImpl Sec) const override;
  191. uint64_t getSectionFlags(DataRefImpl Sec) const override;
  192. StringRef getRelocationTypeName(uint32_t Type) const;
  193. /// \brief Get the relocation section that contains \a Rel.
  194. const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
  195. return *EF.getSection(Rel.d.a);
  196. }
  197. const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
  198. return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
  199. }
  200. DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
  201. DataRefImpl DRI;
  202. if (!SymTable) {
  203. DRI.d.a = 0;
  204. DRI.d.b = 0;
  205. return DRI;
  206. }
  207. assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
  208. SymTable->sh_type == ELF::SHT_DYNSYM);
  209. uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
  210. unsigned SymTableIndex =
  211. (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
  212. DRI.d.a = SymTableIndex;
  213. DRI.d.b = SymbolNum;
  214. return DRI;
  215. }
  216. const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
  217. return reinterpret_cast<const Elf_Shdr *>(Sec.p);
  218. }
  219. DataRefImpl toDRI(const Elf_Shdr *Sec) const {
  220. DataRefImpl DRI;
  221. DRI.p = reinterpret_cast<uintptr_t>(Sec);
  222. return DRI;
  223. }
  224. DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
  225. DataRefImpl DRI;
  226. DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
  227. return DRI;
  228. }
  229. bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
  230. unsigned char Binding = ESym->getBinding();
  231. unsigned char Visibility = ESym->getVisibility();
  232. // A symbol is exported if its binding is either GLOBAL or WEAK, and its
  233. // visibility is either DEFAULT or PROTECTED. All other symbols are not
  234. // exported.
  235. if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
  236. (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
  237. return true;
  238. return false;
  239. }
  240. // This flag is used for classof, to distinguish ELFObjectFile from
  241. // its subclass. If more subclasses will be created, this flag will
  242. // have to become an enum.
  243. bool isDyldELFObject;
  244. public:
  245. ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
  246. const Elf_Rel *getRel(DataRefImpl Rel) const;
  247. const Elf_Rela *getRela(DataRefImpl Rela) const;
  248. const Elf_Sym *getSymbol(DataRefImpl Symb) const;
  249. basic_symbol_iterator symbol_begin_impl() const override;
  250. basic_symbol_iterator symbol_end_impl() const override;
  251. elf_symbol_iterator dynamic_symbol_begin() const;
  252. elf_symbol_iterator dynamic_symbol_end() const;
  253. section_iterator section_begin() const override;
  254. section_iterator section_end() const override;
  255. ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
  256. uint8_t getBytesInAddress() const override;
  257. StringRef getFileFormatName() const override;
  258. unsigned getArch() const override;
  259. StringRef getLoadName() const;
  260. std::error_code getPlatformFlags(unsigned &Result) const override {
  261. Result = EF.getHeader()->e_flags;
  262. return std::error_code();
  263. }
  264. const ELFFile<ELFT> *getELFFile() const { return &EF; }
  265. bool isDyldType() const { return isDyldELFObject; }
  266. static inline bool classof(const Binary *v) {
  267. return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
  268. ELFT::Is64Bits);
  269. }
  270. elf_symbol_iterator_range getDynamicSymbolIterators() const override;
  271. bool isRelocatableObject() const override;
  272. };
  273. typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
  274. typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
  275. typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
  276. typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
  277. template <class ELFT>
  278. void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
  279. ++Sym.d.b;
  280. }
  281. template <class ELFT>
  282. ErrorOr<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
  283. const Elf_Sym *ESym = toELFSymIter(Sym);
  284. const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
  285. const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
  286. StringRef SymTable = *EF.getStringTable(StringTableSec);
  287. return ESym->getName(SymTable);
  288. }
  289. template <class ELFT>
  290. uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
  291. return toELFShdrIter(Sec)->sh_flags;
  292. }
  293. template <class ELFT>
  294. uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
  295. return toELFShdrIter(Sec)->sh_type;
  296. }
  297. template <class ELFT>
  298. uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
  299. const Elf_Sym *ESym = getSymbol(Symb);
  300. uint64_t Ret = ESym->st_value;
  301. if (ESym->st_shndx == ELF::SHN_ABS)
  302. return Ret;
  303. const Elf_Ehdr *Header = EF.getHeader();
  304. // Clear the ARM/Thumb or microMIPS indicator flag.
  305. if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
  306. ESym->getType() == ELF::STT_FUNC)
  307. Ret &= ~1;
  308. return Ret;
  309. }
  310. template <class ELFT>
  311. ErrorOr<uint64_t>
  312. ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
  313. uint64_t Result = getSymbolValue(Symb);
  314. const Elf_Sym *ESym = getSymbol(Symb);
  315. switch (ESym->st_shndx) {
  316. case ELF::SHN_COMMON:
  317. case ELF::SHN_UNDEF:
  318. case ELF::SHN_ABS:
  319. return Result;
  320. }
  321. const Elf_Ehdr *Header = EF.getHeader();
  322. if (Header->e_type == ELF::ET_REL) {
  323. ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
  324. if (std::error_code EC = SectionOrErr.getError())
  325. return EC;
  326. const Elf_Shdr *Section = *SectionOrErr;
  327. if (Section)
  328. Result += Section->sh_addr;
  329. }
  330. return Result;
  331. }
  332. template <class ELFT>
  333. uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
  334. const Elf_Sym *Sym = toELFSymIter(Symb);
  335. if (Sym->st_shndx == ELF::SHN_COMMON)
  336. return Sym->st_value;
  337. return 0;
  338. }
  339. template <class ELFT>
  340. uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
  341. return toELFSymIter(Sym)->st_size;
  342. }
  343. template <class ELFT>
  344. uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
  345. return toELFSymIter(Symb)->st_size;
  346. }
  347. template <class ELFT>
  348. uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
  349. return toELFSymIter(Symb)->st_other;
  350. }
  351. template <class ELFT>
  352. uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
  353. return toELFSymIter(Symb)->getType();
  354. }
  355. template <class ELFT>
  356. SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
  357. const Elf_Sym *ESym = getSymbol(Symb);
  358. switch (ESym->getType()) {
  359. case ELF::STT_NOTYPE:
  360. return SymbolRef::ST_Unknown;
  361. case ELF::STT_SECTION:
  362. return SymbolRef::ST_Debug;
  363. case ELF::STT_FILE:
  364. return SymbolRef::ST_File;
  365. case ELF::STT_FUNC:
  366. return SymbolRef::ST_Function;
  367. case ELF::STT_OBJECT:
  368. case ELF::STT_COMMON:
  369. case ELF::STT_TLS:
  370. return SymbolRef::ST_Data;
  371. default:
  372. return SymbolRef::ST_Other;
  373. }
  374. }
  375. template <class ELFT>
  376. uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
  377. const Elf_Sym *ESym = toELFSymIter(Sym);
  378. uint32_t Result = SymbolRef::SF_None;
  379. if (ESym->getBinding() != ELF::STB_LOCAL)
  380. Result |= SymbolRef::SF_Global;
  381. if (ESym->getBinding() == ELF::STB_WEAK)
  382. Result |= SymbolRef::SF_Weak;
  383. if (ESym->st_shndx == ELF::SHN_ABS)
  384. Result |= SymbolRef::SF_Absolute;
  385. if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
  386. ESym == EF.symbol_begin() || ESym == EF.dynamic_symbol_begin())
  387. Result |= SymbolRef::SF_FormatSpecific;
  388. if (EF.getHeader()->e_machine == ELF::EM_ARM) {
  389. if (ErrorOr<StringRef> NameOrErr = getSymbolName(Sym)) {
  390. StringRef Name = *NameOrErr;
  391. if (Name.startswith("$d") || Name.startswith("$t") ||
  392. Name.startswith("$a"))
  393. Result |= SymbolRef::SF_FormatSpecific;
  394. }
  395. }
  396. if (ESym->st_shndx == ELF::SHN_UNDEF)
  397. Result |= SymbolRef::SF_Undefined;
  398. if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
  399. Result |= SymbolRef::SF_Common;
  400. if (isExportedToOtherDSO(ESym))
  401. Result |= SymbolRef::SF_Exported;
  402. if (ESym->getVisibility() == ELF::STV_HIDDEN)
  403. Result |= SymbolRef::SF_Hidden;
  404. return Result;
  405. }
  406. template <class ELFT>
  407. section_iterator
  408. ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
  409. ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
  410. if (std::error_code EC = ESecOrErr.getError())
  411. report_fatal_error(EC.message());
  412. const Elf_Shdr *ESec = *ESecOrErr;
  413. if (!ESec)
  414. return section_end();
  415. DataRefImpl Sec;
  416. Sec.p = reinterpret_cast<intptr_t>(ESec);
  417. return section_iterator(SectionRef(Sec, this));
  418. }
  419. template <class ELFT>
  420. std::error_code
  421. ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
  422. section_iterator &Res) const {
  423. Res = getSymbolSection(getSymbol(Symb));
  424. return std::error_code();
  425. }
  426. template <class ELFT>
  427. void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
  428. const Elf_Shdr *ESec = toELFShdrIter(Sec);
  429. Sec = toDRI(++ESec);
  430. }
  431. template <class ELFT>
  432. std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
  433. StringRef &Result) const {
  434. ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
  435. if (!Name)
  436. return Name.getError();
  437. Result = *Name;
  438. return std::error_code();
  439. }
  440. template <class ELFT>
  441. uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
  442. return toELFShdrIter(Sec)->sh_addr;
  443. }
  444. template <class ELFT>
  445. uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
  446. return toELFShdrIter(Sec)->sh_size;
  447. }
  448. template <class ELFT>
  449. std::error_code
  450. ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
  451. StringRef &Result) const {
  452. const Elf_Shdr *EShdr = toELFShdrIter(Sec);
  453. Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
  454. return std::error_code();
  455. }
  456. template <class ELFT>
  457. uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
  458. return toELFShdrIter(Sec)->sh_addralign;
  459. }
  460. template <class ELFT>
  461. bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
  462. return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
  463. }
  464. template <class ELFT>
  465. bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
  466. const Elf_Shdr *EShdr = toELFShdrIter(Sec);
  467. return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
  468. EShdr->sh_type == ELF::SHT_PROGBITS;
  469. }
  470. template <class ELFT>
  471. bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
  472. const Elf_Shdr *EShdr = toELFShdrIter(Sec);
  473. return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
  474. EShdr->sh_type == ELF::SHT_NOBITS;
  475. }
  476. template <class ELFT>
  477. bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
  478. return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
  479. }
  480. template <class ELFT>
  481. relocation_iterator
  482. ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
  483. DataRefImpl RelData;
  484. uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
  485. RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
  486. RelData.d.b = 0;
  487. const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
  488. if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
  489. return relocation_iterator(RelocationRef(RelData, this));
  490. const Elf_Shdr *RelSec = getRelSection(RelData);
  491. ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
  492. if (std::error_code EC = SymSecOrErr.getError())
  493. report_fatal_error(EC.message());
  494. const Elf_Shdr *SymSec = *SymSecOrErr;
  495. uint32_t SymSecType = SymSec->sh_type;
  496. if (SymSecType != ELF::SHT_SYMTAB && SymSecType != ELF::SHT_DYNSYM)
  497. report_fatal_error("Invalid symbol table section type!");
  498. if (SymSecType == ELF::SHT_DYNSYM)
  499. RelData.d.b = 1;
  500. return relocation_iterator(RelocationRef(RelData, this));
  501. }
  502. template <class ELFT>
  503. relocation_iterator
  504. ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
  505. const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
  506. relocation_iterator Begin = section_rel_begin(Sec);
  507. if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
  508. return Begin;
  509. DataRefImpl RelData = Begin->getRawDataRefImpl();
  510. RelData.d.b += (S->sh_size / S->sh_entsize) << 1;
  511. return relocation_iterator(RelocationRef(RelData, this));
  512. }
  513. template <class ELFT>
  514. section_iterator
  515. ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
  516. if (EF.getHeader()->e_type != ELF::ET_REL)
  517. return section_end();
  518. const Elf_Shdr *EShdr = toELFShdrIter(Sec);
  519. uintX_t Type = EShdr->sh_type;
  520. if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
  521. return section_end();
  522. ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
  523. if (std::error_code EC = R.getError())
  524. report_fatal_error(EC.message());
  525. return section_iterator(SectionRef(toDRI(*R), this));
  526. }
  527. // Relocations
  528. template <class ELFT>
  529. void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
  530. Rel.d.b += 2;
  531. }
  532. template <class ELFT>
  533. symbol_iterator
  534. ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
  535. uint32_t symbolIdx;
  536. const Elf_Shdr *sec = getRelSection(Rel);
  537. if (sec->sh_type == ELF::SHT_REL)
  538. symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
  539. else
  540. symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
  541. if (!symbolIdx)
  542. return symbol_end();
  543. bool IsDyn = Rel.d.b & 1;
  544. DataRefImpl SymbolData;
  545. if (IsDyn)
  546. SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx);
  547. else
  548. SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx);
  549. return symbol_iterator(SymbolRef(SymbolData, this));
  550. }
  551. template <class ELFT>
  552. uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
  553. assert(EF.getHeader()->e_type == ELF::ET_REL &&
  554. "Only relocatable object files have relocation offsets");
  555. const Elf_Shdr *sec = getRelSection(Rel);
  556. if (sec->sh_type == ELF::SHT_REL)
  557. return getRel(Rel)->r_offset;
  558. return getRela(Rel)->r_offset;
  559. }
  560. template <class ELFT>
  561. uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
  562. const Elf_Shdr *sec = getRelSection(Rel);
  563. if (sec->sh_type == ELF::SHT_REL)
  564. return getRel(Rel)->getType(EF.isMips64EL());
  565. else
  566. return getRela(Rel)->getType(EF.isMips64EL());
  567. }
  568. template <class ELFT>
  569. StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
  570. return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
  571. }
  572. template <class ELFT>
  573. void ELFObjectFile<ELFT>::getRelocationTypeName(
  574. DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
  575. uint32_t type = getRelocationType(Rel);
  576. EF.getRelocationTypeName(type, Result);
  577. }
  578. template <class ELFT>
  579. ErrorOr<int64_t>
  580. ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
  581. if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
  582. return object_error::parse_failed;
  583. return (int64_t)getRela(Rel)->r_addend;
  584. }
  585. template <class ELFT>
  586. const typename ELFFile<ELFT>::Elf_Sym *
  587. ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
  588. return &*toELFSymIter(Symb);
  589. }
  590. template <class ELFT>
  591. const typename ELFObjectFile<ELFT>::Elf_Rel *
  592. ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
  593. assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
  594. return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b >> 1);
  595. }
  596. template <class ELFT>
  597. const typename ELFObjectFile<ELFT>::Elf_Rela *
  598. ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
  599. assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
  600. return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b >> 1);
  601. }
  602. template <class ELFT>
  603. ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
  604. : ELFObjectFileBase(
  605. getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
  606. support::little,
  607. ELFT::Is64Bits),
  608. Object),
  609. EF(Data.getBuffer(), EC) {}
  610. template <class ELFT>
  611. basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
  612. DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0);
  613. return basic_symbol_iterator(SymbolRef(Sym, this));
  614. }
  615. template <class ELFT>
  616. basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
  617. const Elf_Shdr *SymTab = EF.getDotSymtabSec();
  618. if (!SymTab)
  619. return symbol_begin_impl();
  620. DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
  621. return basic_symbol_iterator(SymbolRef(Sym, this));
  622. }
  623. template <class ELFT>
  624. elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
  625. DataRefImpl Sym = toDRI(EF.getDotDynSymSec(), 0);
  626. return symbol_iterator(SymbolRef(Sym, this));
  627. }
  628. template <class ELFT>
  629. elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
  630. const Elf_Shdr *SymTab = EF.getDotDynSymSec();
  631. DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
  632. return basic_symbol_iterator(SymbolRef(Sym, this));
  633. }
  634. template <class ELFT>
  635. section_iterator ELFObjectFile<ELFT>::section_begin() const {
  636. return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
  637. }
  638. template <class ELFT>
  639. section_iterator ELFObjectFile<ELFT>::section_end() const {
  640. return section_iterator(SectionRef(toDRI(EF.section_end()), this));
  641. }
  642. template <class ELFT>
  643. StringRef ELFObjectFile<ELFT>::getLoadName() const {
  644. Elf_Dyn_Iter DI = EF.dynamic_table_begin();
  645. Elf_Dyn_Iter DE = EF.dynamic_table_end();
  646. while (DI != DE && DI->getTag() != ELF::DT_SONAME)
  647. ++DI;
  648. if (DI != DE)
  649. return EF.getDynamicString(DI->getVal());
  650. return "";
  651. }
  652. template <class ELFT>
  653. uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
  654. return ELFT::Is64Bits ? 8 : 4;
  655. }
  656. template <class ELFT>
  657. StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
  658. bool IsLittleEndian = ELFT::TargetEndianness == support::little;
  659. switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
  660. case ELF::ELFCLASS32:
  661. switch (EF.getHeader()->e_machine) {
  662. case ELF::EM_386:
  663. return "ELF32-i386";
  664. case ELF::EM_X86_64:
  665. return "ELF32-x86-64";
  666. case ELF::EM_ARM:
  667. return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
  668. case ELF::EM_HEXAGON:
  669. return "ELF32-hexagon";
  670. case ELF::EM_MIPS:
  671. return "ELF32-mips";
  672. case ELF::EM_PPC:
  673. return "ELF32-ppc";
  674. case ELF::EM_SPARC:
  675. case ELF::EM_SPARC32PLUS:
  676. return "ELF32-sparc";
  677. default:
  678. return "ELF32-unknown";
  679. }
  680. case ELF::ELFCLASS64:
  681. switch (EF.getHeader()->e_machine) {
  682. case ELF::EM_386:
  683. return "ELF64-i386";
  684. case ELF::EM_X86_64:
  685. return "ELF64-x86-64";
  686. case ELF::EM_AARCH64:
  687. return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
  688. case ELF::EM_PPC64:
  689. return "ELF64-ppc64";
  690. case ELF::EM_S390:
  691. return "ELF64-s390";
  692. case ELF::EM_SPARCV9:
  693. return "ELF64-sparc";
  694. case ELF::EM_MIPS:
  695. return "ELF64-mips";
  696. default:
  697. return "ELF64-unknown";
  698. }
  699. default:
  700. // FIXME: Proper error handling.
  701. report_fatal_error("Invalid ELFCLASS!");
  702. }
  703. }
  704. template <class ELFT>
  705. unsigned ELFObjectFile<ELFT>::getArch() const {
  706. bool IsLittleEndian = ELFT::TargetEndianness == support::little;
  707. switch (EF.getHeader()->e_machine) {
  708. case ELF::EM_386:
  709. return Triple::x86;
  710. case ELF::EM_X86_64:
  711. return Triple::x86_64;
  712. case ELF::EM_AARCH64:
  713. return Triple::aarch64;
  714. case ELF::EM_ARM:
  715. return Triple::arm;
  716. case ELF::EM_HEXAGON:
  717. return Triple::hexagon;
  718. case ELF::EM_MIPS:
  719. switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
  720. case ELF::ELFCLASS32:
  721. return IsLittleEndian ? Triple::mipsel : Triple::mips;
  722. case ELF::ELFCLASS64:
  723. return IsLittleEndian ? Triple::mips64el : Triple::mips64;
  724. default:
  725. report_fatal_error("Invalid ELFCLASS!");
  726. }
  727. case ELF::EM_PPC:
  728. return Triple::ppc;
  729. case ELF::EM_PPC64:
  730. return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
  731. case ELF::EM_S390:
  732. return Triple::systemz;
  733. case ELF::EM_SPARC:
  734. case ELF::EM_SPARC32PLUS:
  735. return IsLittleEndian ? Triple::sparcel : Triple::sparc;
  736. case ELF::EM_SPARCV9:
  737. return Triple::sparcv9;
  738. default:
  739. return Triple::UnknownArch;
  740. }
  741. }
  742. template <class ELFT>
  743. ELFObjectFileBase::elf_symbol_iterator_range
  744. ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
  745. return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
  746. }
  747. template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
  748. return EF.getHeader()->e_type == ELF::ET_REL;
  749. }
  750. }
  751. }
  752. #endif