ObjectFile.h 19 KB

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