ObjectFile.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. //===- ObjectFile.h - File format independent object file -------*- 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 a file format independent ObjectFile class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_OBJECT_OBJECTFILE_H
  14. #define LLVM_OBJECT_OBJECTFILE_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Object/SymbolicFile.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include "llvm/Support/FileSystem.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include <cstring>
  22. #include <vector>
  23. namespace llvm {
  24. namespace object {
  25. class ObjectFile;
  26. class COFFObjectFile;
  27. class MachOObjectFile;
  28. class SymbolRef;
  29. class symbol_iterator;
  30. class SectionRef;
  31. typedef content_iterator<SectionRef> section_iterator;
  32. /// This is a value type class that represents a single relocation in the list
  33. /// of relocations in the object file.
  34. class RelocationRef {
  35. DataRefImpl RelocationPimpl;
  36. const ObjectFile *OwningObject;
  37. public:
  38. RelocationRef() : OwningObject(nullptr) { }
  39. RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
  40. bool operator==(const RelocationRef &Other) const;
  41. void moveNext();
  42. uint64_t getOffset() const;
  43. symbol_iterator getSymbol() const;
  44. uint64_t getType() const;
  45. /// @brief Get a string that represents the type of this relocation.
  46. ///
  47. /// This is for display purposes only.
  48. void getTypeName(SmallVectorImpl<char> &Result) const;
  49. DataRefImpl getRawDataRefImpl() const;
  50. const ObjectFile *getObject() const;
  51. };
  52. typedef content_iterator<RelocationRef> relocation_iterator;
  53. /// This is a value type class that represents a single section in the list of
  54. /// sections in the object file.
  55. class SectionRef {
  56. friend class SymbolRef;
  57. DataRefImpl SectionPimpl;
  58. const ObjectFile *OwningObject;
  59. public:
  60. SectionRef() : OwningObject(nullptr) { }
  61. SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
  62. bool operator==(const SectionRef &Other) const;
  63. bool operator!=(const SectionRef &Other) const;
  64. bool operator<(const SectionRef &Other) const;
  65. void moveNext();
  66. std::error_code getName(StringRef &Result) const;
  67. uint64_t getAddress() const;
  68. uint64_t getSize() const;
  69. std::error_code getContents(StringRef &Result) const;
  70. /// @brief Get the alignment of this section as the actual value (not log 2).
  71. uint64_t getAlignment() const;
  72. bool isText() const;
  73. bool isData() const;
  74. bool isBSS() const;
  75. bool isVirtual() const;
  76. bool containsSymbol(SymbolRef S) const;
  77. relocation_iterator relocation_begin() const;
  78. relocation_iterator relocation_end() const;
  79. iterator_range<relocation_iterator> relocations() const {
  80. return iterator_range<relocation_iterator>(relocation_begin(),
  81. relocation_end());
  82. }
  83. section_iterator getRelocatedSection() const;
  84. DataRefImpl getRawDataRefImpl() const;
  85. const ObjectFile *getObject() const;
  86. };
  87. /// This is a value type class that represents a single symbol in the list of
  88. /// symbols in the object file.
  89. class SymbolRef : public BasicSymbolRef {
  90. friend class SectionRef;
  91. public:
  92. SymbolRef() : BasicSymbolRef() {}
  93. enum Type {
  94. ST_Unknown, // Type not specified
  95. ST_Data,
  96. ST_Debug,
  97. ST_File,
  98. ST_Function,
  99. ST_Other
  100. };
  101. SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
  102. SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
  103. assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
  104. }
  105. ErrorOr<StringRef> getName() const;
  106. /// Returns the symbol virtual address (i.e. address at which it will be
  107. /// mapped).
  108. ErrorOr<uint64_t> getAddress() const;
  109. /// Return the value of the symbol depending on the object this can be an
  110. /// offset or a virtual address.
  111. uint64_t getValue() const;
  112. /// @brief Get the alignment of this symbol as the actual value (not log 2).
  113. uint32_t getAlignment() const;
  114. uint64_t getCommonSize() const;
  115. SymbolRef::Type getType() const;
  116. /// @brief Get section this symbol is defined in reference to. Result is
  117. /// end_sections() if it is undefined or is an absolute symbol.
  118. std::error_code getSection(section_iterator &Result) const;
  119. const ObjectFile *getObject() const;
  120. };
  121. class symbol_iterator : public basic_symbol_iterator {
  122. public:
  123. symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
  124. symbol_iterator(const basic_symbol_iterator &B)
  125. : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
  126. cast<ObjectFile>(B->getObject()))) {}
  127. const SymbolRef *operator->() const {
  128. const BasicSymbolRef &P = basic_symbol_iterator::operator *();
  129. return static_cast<const SymbolRef*>(&P);
  130. }
  131. const SymbolRef &operator*() const {
  132. const BasicSymbolRef &P = basic_symbol_iterator::operator *();
  133. return static_cast<const SymbolRef&>(P);
  134. }
  135. };
  136. /// This class is the base class for all object file types. Concrete instances
  137. /// of this object are created by createObjectFile, which figures out which type
  138. /// to create.
  139. class ObjectFile : public SymbolicFile {
  140. virtual void anchor();
  141. ObjectFile() = delete;
  142. ObjectFile(const ObjectFile &other) = delete;
  143. protected:
  144. ObjectFile(unsigned int Type, MemoryBufferRef Source);
  145. const uint8_t *base() const {
  146. return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
  147. }
  148. // These functions are for SymbolRef to call internally. The main goal of
  149. // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
  150. // entry in the memory mapped object file. SymbolPimpl cannot contain any
  151. // virtual functions because then it could not point into the memory mapped
  152. // file.
  153. //
  154. // Implementations assume that the DataRefImpl is valid and has not been
  155. // modified externally. It's UB otherwise.
  156. friend class SymbolRef;
  157. virtual ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
  158. std::error_code printSymbolName(raw_ostream &OS,
  159. DataRefImpl Symb) const override;
  160. virtual ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
  161. virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
  162. virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
  163. virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
  164. virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
  165. virtual std::error_code getSymbolSection(DataRefImpl Symb,
  166. section_iterator &Res) const = 0;
  167. // Same as above for SectionRef.
  168. friend class SectionRef;
  169. virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
  170. virtual std::error_code getSectionName(DataRefImpl Sec,
  171. StringRef &Res) const = 0;
  172. virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
  173. virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
  174. virtual std::error_code getSectionContents(DataRefImpl Sec,
  175. StringRef &Res) const = 0;
  176. virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
  177. virtual bool isSectionText(DataRefImpl Sec) const = 0;
  178. virtual bool isSectionData(DataRefImpl Sec) const = 0;
  179. virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
  180. // A section is 'virtual' if its contents aren't present in the object image.
  181. virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
  182. virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
  183. virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
  184. virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
  185. // Same as above for RelocationRef.
  186. friend class RelocationRef;
  187. virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
  188. virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
  189. virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
  190. virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
  191. virtual void getRelocationTypeName(DataRefImpl Rel,
  192. SmallVectorImpl<char> &Result) const = 0;
  193. uint64_t getSymbolValue(DataRefImpl Symb) const;
  194. public:
  195. uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
  196. assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
  197. return getCommonSymbolSizeImpl(Symb);
  198. }
  199. typedef iterator_range<symbol_iterator> symbol_iterator_range;
  200. symbol_iterator_range symbols() const {
  201. return symbol_iterator_range(symbol_begin(), symbol_end());
  202. }
  203. virtual section_iterator section_begin() const = 0;
  204. virtual section_iterator section_end() const = 0;
  205. typedef iterator_range<section_iterator> section_iterator_range;
  206. section_iterator_range sections() const {
  207. return section_iterator_range(section_begin(), section_end());
  208. }
  209. /// @brief The number of bytes used to represent an address in this object
  210. /// file format.
  211. virtual uint8_t getBytesInAddress() const = 0;
  212. virtual StringRef getFileFormatName() const = 0;
  213. virtual /* Triple::ArchType */ unsigned getArch() const = 0;
  214. /// Returns platform-specific object flags, if any.
  215. virtual std::error_code getPlatformFlags(unsigned &Result) const {
  216. Result = 0;
  217. return object_error::invalid_file_type;
  218. }
  219. /// True if this is a relocatable object (.o/.obj).
  220. virtual bool isRelocatableObject() const = 0;
  221. /// @returns Pointer to ObjectFile subclass to handle this type of object.
  222. /// @param ObjectPath The path to the object file. ObjectPath.isObject must
  223. /// return true.
  224. /// @brief Create ObjectFile from path.
  225. static ErrorOr<OwningBinary<ObjectFile>>
  226. createObjectFile(StringRef ObjectPath);
  227. static ErrorOr<std::unique_ptr<ObjectFile>>
  228. createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
  229. static ErrorOr<std::unique_ptr<ObjectFile>>
  230. createObjectFile(MemoryBufferRef Object) {
  231. return createObjectFile(Object, sys::fs::file_magic::unknown);
  232. }
  233. static inline bool classof(const Binary *v) {
  234. return v->isObject();
  235. }
  236. static ErrorOr<std::unique_ptr<COFFObjectFile>>
  237. createCOFFObjectFile(MemoryBufferRef Object);
  238. static ErrorOr<std::unique_ptr<ObjectFile>>
  239. createELFObjectFile(MemoryBufferRef Object);
  240. static ErrorOr<std::unique_ptr<MachOObjectFile>>
  241. createMachOObjectFile(MemoryBufferRef Object);
  242. };
  243. // Inline function definitions.
  244. inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
  245. : BasicSymbolRef(SymbolP, Owner) {}
  246. inline ErrorOr<StringRef> SymbolRef::getName() const {
  247. return getObject()->getSymbolName(getRawDataRefImpl());
  248. }
  249. inline ErrorOr<uint64_t> SymbolRef::getAddress() const {
  250. return getObject()->getSymbolAddress(getRawDataRefImpl());
  251. }
  252. inline uint64_t SymbolRef::getValue() const {
  253. return getObject()->getSymbolValue(getRawDataRefImpl());
  254. }
  255. inline uint32_t SymbolRef::getAlignment() const {
  256. return getObject()->getSymbolAlignment(getRawDataRefImpl());
  257. }
  258. inline uint64_t SymbolRef::getCommonSize() const {
  259. return getObject()->getCommonSymbolSize(getRawDataRefImpl());
  260. }
  261. inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
  262. return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
  263. }
  264. inline SymbolRef::Type SymbolRef::getType() const {
  265. return getObject()->getSymbolType(getRawDataRefImpl());
  266. }
  267. inline const ObjectFile *SymbolRef::getObject() const {
  268. const SymbolicFile *O = BasicSymbolRef::getObject();
  269. return cast<ObjectFile>(O);
  270. }
  271. /// SectionRef
  272. inline SectionRef::SectionRef(DataRefImpl SectionP,
  273. const ObjectFile *Owner)
  274. : SectionPimpl(SectionP)
  275. , OwningObject(Owner) {}
  276. inline bool SectionRef::operator==(const SectionRef &Other) const {
  277. return SectionPimpl == Other.SectionPimpl;
  278. }
  279. inline bool SectionRef::operator!=(const SectionRef &Other) const {
  280. return SectionPimpl != Other.SectionPimpl;
  281. }
  282. inline bool SectionRef::operator<(const SectionRef &Other) const {
  283. return SectionPimpl < Other.SectionPimpl;
  284. }
  285. inline void SectionRef::moveNext() {
  286. return OwningObject->moveSectionNext(SectionPimpl);
  287. }
  288. inline std::error_code SectionRef::getName(StringRef &Result) const {
  289. return OwningObject->getSectionName(SectionPimpl, Result);
  290. }
  291. inline uint64_t SectionRef::getAddress() const {
  292. return OwningObject->getSectionAddress(SectionPimpl);
  293. }
  294. inline uint64_t SectionRef::getSize() const {
  295. return OwningObject->getSectionSize(SectionPimpl);
  296. }
  297. inline std::error_code SectionRef::getContents(StringRef &Result) const {
  298. return OwningObject->getSectionContents(SectionPimpl, Result);
  299. }
  300. inline uint64_t SectionRef::getAlignment() const {
  301. return OwningObject->getSectionAlignment(SectionPimpl);
  302. }
  303. inline bool SectionRef::isText() const {
  304. return OwningObject->isSectionText(SectionPimpl);
  305. }
  306. inline bool SectionRef::isData() const {
  307. return OwningObject->isSectionData(SectionPimpl);
  308. }
  309. inline bool SectionRef::isBSS() const {
  310. return OwningObject->isSectionBSS(SectionPimpl);
  311. }
  312. inline bool SectionRef::isVirtual() const {
  313. return OwningObject->isSectionVirtual(SectionPimpl);
  314. }
  315. inline relocation_iterator SectionRef::relocation_begin() const {
  316. return OwningObject->section_rel_begin(SectionPimpl);
  317. }
  318. inline relocation_iterator SectionRef::relocation_end() const {
  319. return OwningObject->section_rel_end(SectionPimpl);
  320. }
  321. inline section_iterator SectionRef::getRelocatedSection() const {
  322. return OwningObject->getRelocatedSection(SectionPimpl);
  323. }
  324. inline DataRefImpl SectionRef::getRawDataRefImpl() const {
  325. return SectionPimpl;
  326. }
  327. inline const ObjectFile *SectionRef::getObject() const {
  328. return OwningObject;
  329. }
  330. /// RelocationRef
  331. inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
  332. const ObjectFile *Owner)
  333. : RelocationPimpl(RelocationP)
  334. , OwningObject(Owner) {}
  335. inline bool RelocationRef::operator==(const RelocationRef &Other) const {
  336. return RelocationPimpl == Other.RelocationPimpl;
  337. }
  338. inline void RelocationRef::moveNext() {
  339. return OwningObject->moveRelocationNext(RelocationPimpl);
  340. }
  341. inline uint64_t RelocationRef::getOffset() const {
  342. return OwningObject->getRelocationOffset(RelocationPimpl);
  343. }
  344. inline symbol_iterator RelocationRef::getSymbol() const {
  345. return OwningObject->getRelocationSymbol(RelocationPimpl);
  346. }
  347. inline uint64_t RelocationRef::getType() const {
  348. return OwningObject->getRelocationType(RelocationPimpl);
  349. }
  350. inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
  351. return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
  352. }
  353. inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
  354. return RelocationPimpl;
  355. }
  356. inline const ObjectFile *RelocationRef::getObject() const {
  357. return OwningObject;
  358. }
  359. } // end namespace object
  360. } // end namespace llvm
  361. #endif