ObjectFile.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //===- ObjectFile.h - File format independent object file -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file declares a file format independent ObjectFile class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_OBJECT_OBJECTFILE_H
  13. #define LLVM_OBJECT_OBJECTFILE_H
  14. #include "llvm/ADT/DenseMapInfo.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/ADT/iterator_range.h"
  18. #include "llvm/BinaryFormat/Magic.h"
  19. #include "llvm/Object/Binary.h"
  20. #include "llvm/Object/Error.h"
  21. #include "llvm/Object/SymbolicFile.h"
  22. #include "llvm/Support/Casting.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <memory>
  28. #include <system_error>
  29. namespace llvm {
  30. class ARMAttributeParser;
  31. class SubtargetFeatures;
  32. namespace object {
  33. class COFFObjectFile;
  34. class MachOObjectFile;
  35. class ObjectFile;
  36. class SectionRef;
  37. class SymbolRef;
  38. class symbol_iterator;
  39. class WasmObjectFile;
  40. using section_iterator = content_iterator<SectionRef>;
  41. /// This is a value type class that represents a single relocation in the list
  42. /// of relocations in the object file.
  43. class RelocationRef {
  44. DataRefImpl RelocationPimpl;
  45. const ObjectFile *OwningObject = nullptr;
  46. public:
  47. RelocationRef() = default;
  48. RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
  49. bool operator==(const RelocationRef &Other) const;
  50. void moveNext();
  51. uint64_t getOffset() const;
  52. symbol_iterator getSymbol() const;
  53. uint64_t getType() const;
  54. /// Get a string that represents the type of this relocation.
  55. ///
  56. /// This is for display purposes only.
  57. void getTypeName(SmallVectorImpl<char> &Result) const;
  58. DataRefImpl getRawDataRefImpl() const;
  59. const ObjectFile *getObject() const;
  60. };
  61. using relocation_iterator = content_iterator<RelocationRef>;
  62. /// This is a value type class that represents a single section in the list of
  63. /// sections in the object file.
  64. class SectionRef {
  65. friend class SymbolRef;
  66. DataRefImpl SectionPimpl;
  67. const ObjectFile *OwningObject = nullptr;
  68. public:
  69. SectionRef() = default;
  70. SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
  71. bool operator==(const SectionRef &Other) const;
  72. bool operator!=(const SectionRef &Other) const;
  73. bool operator<(const SectionRef &Other) const;
  74. void moveNext();
  75. Expected<StringRef> getName() const;
  76. uint64_t getAddress() const;
  77. uint64_t getIndex() const;
  78. uint64_t getSize() const;
  79. Expected<StringRef> getContents() const;
  80. /// Get the alignment of this section as the actual value (not log 2).
  81. uint64_t getAlignment() const;
  82. bool isCompressed() const;
  83. /// Whether this section contains instructions.
  84. bool isText() const;
  85. /// Whether this section contains data, not instructions.
  86. bool isData() const;
  87. /// Whether this section contains BSS uninitialized data.
  88. bool isBSS() const;
  89. bool isVirtual() const;
  90. bool isBitcode() const;
  91. bool isStripped() const;
  92. /// Whether this section will be placed in the text segment, according to the
  93. /// Berkeley size format. This is true if the section is allocatable, and
  94. /// contains either code or readonly data.
  95. bool isBerkeleyText() const;
  96. /// Whether this section will be placed in the data segment, according to the
  97. /// Berkeley size format. This is true if the section is allocatable and
  98. /// contains data (e.g. PROGBITS), but is not text.
  99. bool isBerkeleyData() const;
  100. /// Whether this section is a debug section.
  101. bool isDebugSection() const;
  102. bool containsSymbol(SymbolRef S) const;
  103. relocation_iterator relocation_begin() const;
  104. relocation_iterator relocation_end() const;
  105. iterator_range<relocation_iterator> relocations() const {
  106. return make_range(relocation_begin(), relocation_end());
  107. }
  108. Expected<section_iterator> getRelocatedSection() const;
  109. DataRefImpl getRawDataRefImpl() const;
  110. const ObjectFile *getObject() const;
  111. };
  112. struct SectionedAddress {
  113. const static uint64_t UndefSection = UINT64_MAX;
  114. uint64_t Address = 0;
  115. uint64_t SectionIndex = UndefSection;
  116. };
  117. inline bool operator<(const SectionedAddress &LHS,
  118. const SectionedAddress &RHS) {
  119. return std::tie(LHS.SectionIndex, LHS.Address) <
  120. std::tie(RHS.SectionIndex, RHS.Address);
  121. }
  122. inline bool operator==(const SectionedAddress &LHS,
  123. const SectionedAddress &RHS) {
  124. return std::tie(LHS.SectionIndex, LHS.Address) ==
  125. std::tie(RHS.SectionIndex, RHS.Address);
  126. }
  127. raw_ostream &operator<<(raw_ostream &OS, const SectionedAddress &Addr);
  128. /// This is a value type class that represents a single symbol in the list of
  129. /// symbols in the object file.
  130. class SymbolRef : public BasicSymbolRef {
  131. friend class SectionRef;
  132. public:
  133. enum Type {
  134. ST_Unknown, // Type not specified
  135. ST_Data,
  136. ST_Debug,
  137. ST_File,
  138. ST_Function,
  139. ST_Other
  140. };
  141. SymbolRef() = default;
  142. SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
  143. SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
  144. assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
  145. }
  146. Expected<StringRef> getName() const;
  147. /// Returns the symbol virtual address (i.e. address at which it will be
  148. /// mapped).
  149. Expected<uint64_t> getAddress() const;
  150. /// Return the value of the symbol depending on the object this can be an
  151. /// offset or a virtual address.
  152. Expected<uint64_t> getValue() const;
  153. /// Get the alignment of this symbol as the actual value (not log 2).
  154. uint32_t getAlignment() const;
  155. uint64_t getCommonSize() const;
  156. Expected<SymbolRef::Type> getType() const;
  157. /// Get section this symbol is defined in reference to. Result is
  158. /// end_sections() if it is undefined or is an absolute symbol.
  159. Expected<section_iterator> getSection() const;
  160. const ObjectFile *getObject() const;
  161. };
  162. class symbol_iterator : public basic_symbol_iterator {
  163. public:
  164. symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
  165. symbol_iterator(const basic_symbol_iterator &B)
  166. : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
  167. cast<ObjectFile>(B->getObject()))) {}
  168. const SymbolRef *operator->() const {
  169. const BasicSymbolRef &P = basic_symbol_iterator::operator *();
  170. return static_cast<const SymbolRef*>(&P);
  171. }
  172. const SymbolRef &operator*() const {
  173. const BasicSymbolRef &P = basic_symbol_iterator::operator *();
  174. return static_cast<const SymbolRef&>(P);
  175. }
  176. };
  177. /// This class is the base class for all object file types. Concrete instances
  178. /// of this object are created by createObjectFile, which figures out which type
  179. /// to create.
  180. class ObjectFile : public SymbolicFile {
  181. virtual void anchor();
  182. protected:
  183. ObjectFile(unsigned int Type, MemoryBufferRef Source);
  184. const uint8_t *base() const {
  185. return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
  186. }
  187. // These functions are for SymbolRef to call internally. The main goal of
  188. // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
  189. // entry in the memory mapped object file. SymbolPimpl cannot contain any
  190. // virtual functions because then it could not point into the memory mapped
  191. // file.
  192. //
  193. // Implementations assume that the DataRefImpl is valid and has not been
  194. // modified externally. It's UB otherwise.
  195. friend class SymbolRef;
  196. virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
  197. Error printSymbolName(raw_ostream &OS,
  198. DataRefImpl Symb) const override;
  199. virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
  200. virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
  201. virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
  202. virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
  203. virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
  204. virtual Expected<section_iterator>
  205. getSymbolSection(DataRefImpl Symb) const = 0;
  206. // Same as above for SectionRef.
  207. friend class SectionRef;
  208. virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
  209. virtual Expected<StringRef> getSectionName(DataRefImpl Sec) const = 0;
  210. virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
  211. virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
  212. virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
  213. virtual Expected<ArrayRef<uint8_t>>
  214. getSectionContents(DataRefImpl Sec) const = 0;
  215. virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
  216. virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
  217. virtual bool isSectionText(DataRefImpl Sec) const = 0;
  218. virtual bool isSectionData(DataRefImpl Sec) const = 0;
  219. virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
  220. // A section is 'virtual' if its contents aren't present in the object image.
  221. virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
  222. virtual bool isSectionBitcode(DataRefImpl Sec) const;
  223. virtual bool isSectionStripped(DataRefImpl Sec) const;
  224. virtual bool isBerkeleyText(DataRefImpl Sec) const;
  225. virtual bool isBerkeleyData(DataRefImpl Sec) const;
  226. virtual bool isDebugSection(DataRefImpl Sec) const;
  227. virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
  228. virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
  229. virtual Expected<section_iterator> getRelocatedSection(DataRefImpl Sec) const;
  230. // Same as above for RelocationRef.
  231. friend class RelocationRef;
  232. virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
  233. virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
  234. virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
  235. virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
  236. virtual void getRelocationTypeName(DataRefImpl Rel,
  237. SmallVectorImpl<char> &Result) const = 0;
  238. Expected<uint64_t> getSymbolValue(DataRefImpl Symb) const;
  239. public:
  240. ObjectFile() = delete;
  241. ObjectFile(const ObjectFile &other) = delete;
  242. uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
  243. Expected<uint32_t> SymbolFlagsOrErr = getSymbolFlags(Symb);
  244. if (!SymbolFlagsOrErr)
  245. // TODO: Actually report errors helpfully.
  246. report_fatal_error(SymbolFlagsOrErr.takeError());
  247. assert(*SymbolFlagsOrErr & SymbolRef::SF_Common);
  248. return getCommonSymbolSizeImpl(Symb);
  249. }
  250. virtual std::vector<SectionRef> dynamic_relocation_sections() const {
  251. return std::vector<SectionRef>();
  252. }
  253. using symbol_iterator_range = iterator_range<symbol_iterator>;
  254. symbol_iterator_range symbols() const {
  255. return symbol_iterator_range(symbol_begin(), symbol_end());
  256. }
  257. virtual section_iterator section_begin() const = 0;
  258. virtual section_iterator section_end() const = 0;
  259. using section_iterator_range = iterator_range<section_iterator>;
  260. section_iterator_range sections() const {
  261. return section_iterator_range(section_begin(), section_end());
  262. }
  263. /// The number of bytes used to represent an address in this object
  264. /// file format.
  265. virtual uint8_t getBytesInAddress() const = 0;
  266. virtual StringRef getFileFormatName() const = 0;
  267. virtual Triple::ArchType getArch() const = 0;
  268. virtual SubtargetFeatures getFeatures() const = 0;
  269. virtual Optional<StringRef> tryGetCPUName() const { return None; };
  270. virtual void setARMSubArch(Triple &TheTriple) const { }
  271. virtual Expected<uint64_t> getStartAddress() const {
  272. return errorCodeToError(object_error::parse_failed);
  273. };
  274. /// Create a triple from the data in this object file.
  275. Triple makeTriple() const;
  276. /// Maps a debug section name to a standard DWARF section name.
  277. virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; }
  278. /// True if this is a relocatable object (.o/.obj).
  279. virtual bool isRelocatableObject() const = 0;
  280. /// @returns Pointer to ObjectFile subclass to handle this type of object.
  281. /// @param ObjectPath The path to the object file. ObjectPath.isObject must
  282. /// return true.
  283. /// Create ObjectFile from path.
  284. static Expected<OwningBinary<ObjectFile>>
  285. createObjectFile(StringRef ObjectPath);
  286. static Expected<std::unique_ptr<ObjectFile>>
  287. createObjectFile(MemoryBufferRef Object, llvm::file_magic Type,
  288. bool InitContent = true);
  289. static Expected<std::unique_ptr<ObjectFile>>
  290. createObjectFile(MemoryBufferRef Object) {
  291. return createObjectFile(Object, llvm::file_magic::unknown);
  292. }
  293. static bool classof(const Binary *v) {
  294. return v->isObject();
  295. }
  296. static Expected<std::unique_ptr<COFFObjectFile>>
  297. createCOFFObjectFile(MemoryBufferRef Object);
  298. static Expected<std::unique_ptr<ObjectFile>>
  299. createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType);
  300. static Expected<std::unique_ptr<ObjectFile>>
  301. createELFObjectFile(MemoryBufferRef Object, bool InitContent = true);
  302. static Expected<std::unique_ptr<MachOObjectFile>>
  303. createMachOObjectFile(MemoryBufferRef Object,
  304. uint32_t UniversalCputype = 0,
  305. uint32_t UniversalIndex = 0);
  306. static Expected<std::unique_ptr<WasmObjectFile>>
  307. createWasmObjectFile(MemoryBufferRef Object);
  308. };
  309. // Inline function definitions.
  310. inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
  311. : BasicSymbolRef(SymbolP, Owner) {}
  312. inline Expected<StringRef> SymbolRef::getName() const {
  313. return getObject()->getSymbolName(getRawDataRefImpl());
  314. }
  315. inline Expected<uint64_t> SymbolRef::getAddress() const {
  316. return getObject()->getSymbolAddress(getRawDataRefImpl());
  317. }
  318. inline Expected<uint64_t> SymbolRef::getValue() const {
  319. return getObject()->getSymbolValue(getRawDataRefImpl());
  320. }
  321. inline uint32_t SymbolRef::getAlignment() const {
  322. return getObject()->getSymbolAlignment(getRawDataRefImpl());
  323. }
  324. inline uint64_t SymbolRef::getCommonSize() const {
  325. return getObject()->getCommonSymbolSize(getRawDataRefImpl());
  326. }
  327. inline Expected<section_iterator> SymbolRef::getSection() const {
  328. return getObject()->getSymbolSection(getRawDataRefImpl());
  329. }
  330. inline Expected<SymbolRef::Type> SymbolRef::getType() const {
  331. return getObject()->getSymbolType(getRawDataRefImpl());
  332. }
  333. inline const ObjectFile *SymbolRef::getObject() const {
  334. const SymbolicFile *O = BasicSymbolRef::getObject();
  335. return cast<ObjectFile>(O);
  336. }
  337. /// SectionRef
  338. inline SectionRef::SectionRef(DataRefImpl SectionP,
  339. const ObjectFile *Owner)
  340. : SectionPimpl(SectionP)
  341. , OwningObject(Owner) {}
  342. inline bool SectionRef::operator==(const SectionRef &Other) const {
  343. return OwningObject == Other.OwningObject &&
  344. SectionPimpl == Other.SectionPimpl;
  345. }
  346. inline bool SectionRef::operator!=(const SectionRef &Other) const {
  347. return !(*this == Other);
  348. }
  349. inline bool SectionRef::operator<(const SectionRef &Other) const {
  350. assert(OwningObject == Other.OwningObject);
  351. return SectionPimpl < Other.SectionPimpl;
  352. }
  353. inline void SectionRef::moveNext() {
  354. return OwningObject->moveSectionNext(SectionPimpl);
  355. }
  356. inline Expected<StringRef> SectionRef::getName() const {
  357. return OwningObject->getSectionName(SectionPimpl);
  358. }
  359. inline uint64_t SectionRef::getAddress() const {
  360. return OwningObject->getSectionAddress(SectionPimpl);
  361. }
  362. inline uint64_t SectionRef::getIndex() const {
  363. return OwningObject->getSectionIndex(SectionPimpl);
  364. }
  365. inline uint64_t SectionRef::getSize() const {
  366. return OwningObject->getSectionSize(SectionPimpl);
  367. }
  368. inline Expected<StringRef> SectionRef::getContents() const {
  369. Expected<ArrayRef<uint8_t>> Res =
  370. OwningObject->getSectionContents(SectionPimpl);
  371. if (!Res)
  372. return Res.takeError();
  373. return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
  374. }
  375. inline uint64_t SectionRef::getAlignment() const {
  376. return OwningObject->getSectionAlignment(SectionPimpl);
  377. }
  378. inline bool SectionRef::isCompressed() const {
  379. return OwningObject->isSectionCompressed(SectionPimpl);
  380. }
  381. inline bool SectionRef::isText() const {
  382. return OwningObject->isSectionText(SectionPimpl);
  383. }
  384. inline bool SectionRef::isData() const {
  385. return OwningObject->isSectionData(SectionPimpl);
  386. }
  387. inline bool SectionRef::isBSS() const {
  388. return OwningObject->isSectionBSS(SectionPimpl);
  389. }
  390. inline bool SectionRef::isVirtual() const {
  391. return OwningObject->isSectionVirtual(SectionPimpl);
  392. }
  393. inline bool SectionRef::isBitcode() const {
  394. return OwningObject->isSectionBitcode(SectionPimpl);
  395. }
  396. inline bool SectionRef::isStripped() const {
  397. return OwningObject->isSectionStripped(SectionPimpl);
  398. }
  399. inline bool SectionRef::isBerkeleyText() const {
  400. return OwningObject->isBerkeleyText(SectionPimpl);
  401. }
  402. inline bool SectionRef::isBerkeleyData() const {
  403. return OwningObject->isBerkeleyData(SectionPimpl);
  404. }
  405. inline bool SectionRef::isDebugSection() const {
  406. return OwningObject->isDebugSection(SectionPimpl);
  407. }
  408. inline relocation_iterator SectionRef::relocation_begin() const {
  409. return OwningObject->section_rel_begin(SectionPimpl);
  410. }
  411. inline relocation_iterator SectionRef::relocation_end() const {
  412. return OwningObject->section_rel_end(SectionPimpl);
  413. }
  414. inline Expected<section_iterator> SectionRef::getRelocatedSection() const {
  415. return OwningObject->getRelocatedSection(SectionPimpl);
  416. }
  417. inline DataRefImpl SectionRef::getRawDataRefImpl() const {
  418. return SectionPimpl;
  419. }
  420. inline const ObjectFile *SectionRef::getObject() const {
  421. return OwningObject;
  422. }
  423. /// RelocationRef
  424. inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
  425. const ObjectFile *Owner)
  426. : RelocationPimpl(RelocationP)
  427. , OwningObject(Owner) {}
  428. inline bool RelocationRef::operator==(const RelocationRef &Other) const {
  429. return RelocationPimpl == Other.RelocationPimpl;
  430. }
  431. inline void RelocationRef::moveNext() {
  432. return OwningObject->moveRelocationNext(RelocationPimpl);
  433. }
  434. inline uint64_t RelocationRef::getOffset() const {
  435. return OwningObject->getRelocationOffset(RelocationPimpl);
  436. }
  437. inline symbol_iterator RelocationRef::getSymbol() const {
  438. return OwningObject->getRelocationSymbol(RelocationPimpl);
  439. }
  440. inline uint64_t RelocationRef::getType() const {
  441. return OwningObject->getRelocationType(RelocationPimpl);
  442. }
  443. inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
  444. return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
  445. }
  446. inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
  447. return RelocationPimpl;
  448. }
  449. inline const ObjectFile *RelocationRef::getObject() const {
  450. return OwningObject;
  451. }
  452. } // end namespace object
  453. template <> struct DenseMapInfo<object::SectionRef> {
  454. static bool isEqual(const object::SectionRef &A,
  455. const object::SectionRef &B) {
  456. return A == B;
  457. }
  458. static object::SectionRef getEmptyKey() {
  459. return object::SectionRef({}, nullptr);
  460. }
  461. static object::SectionRef getTombstoneKey() {
  462. object::DataRefImpl TS;
  463. TS.p = (uintptr_t)-1;
  464. return object::SectionRef(TS, nullptr);
  465. }
  466. static unsigned getHashValue(const object::SectionRef &Sec) {
  467. object::DataRefImpl Raw = Sec.getRawDataRefImpl();
  468. return hash_combine(Raw.p, Raw.d.a, Raw.d.b);
  469. }
  470. };
  471. } // end namespace llvm
  472. #endif // LLVM_OBJECT_OBJECTFILE_H