MachO.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //===- MachO.h - MachO 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 MachOObjectFile class, which implement the ObjectFile
  11. // interface for MachO files.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_OBJECT_MACHO_H
  15. #define LLVM_OBJECT_MACHO_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/Object/ObjectFile.h"
  20. #include "llvm/Support/MachO.h"
  21. namespace llvm {
  22. namespace object {
  23. /// DiceRef - This is a value type class that represents a single
  24. /// data in code entry in the table in a Mach-O object file.
  25. class DiceRef {
  26. DataRefImpl DicePimpl;
  27. const ObjectFile *OwningObject;
  28. public:
  29. DiceRef() : OwningObject(nullptr) { }
  30. DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
  31. bool operator==(const DiceRef &Other) const;
  32. bool operator<(const DiceRef &Other) const;
  33. void moveNext();
  34. std::error_code getOffset(uint32_t &Result) const;
  35. std::error_code getLength(uint16_t &Result) const;
  36. std::error_code getKind(uint16_t &Result) const;
  37. DataRefImpl getRawDataRefImpl() const;
  38. const ObjectFile *getObjectFile() const;
  39. };
  40. typedef content_iterator<DiceRef> dice_iterator;
  41. /// ExportEntry encapsulates the current-state-of-the-walk used when doing a
  42. /// non-recursive walk of the trie data structure. This allows you to iterate
  43. /// across all exported symbols using:
  44. /// for (const llvm::object::ExportEntry &AnExport : Obj->exports()) {
  45. /// }
  46. class ExportEntry {
  47. public:
  48. ExportEntry(ArrayRef<uint8_t> Trie);
  49. StringRef name() const;
  50. uint64_t flags() const;
  51. uint64_t address() const;
  52. uint64_t other() const;
  53. StringRef otherName() const;
  54. uint32_t nodeOffset() const;
  55. bool operator==(const ExportEntry &) const;
  56. void moveNext();
  57. private:
  58. friend class MachOObjectFile;
  59. void moveToFirst();
  60. void moveToEnd();
  61. uint64_t readULEB128(const uint8_t *&p);
  62. void pushDownUntilBottom();
  63. void pushNode(uint64_t Offset);
  64. // Represents a node in the mach-o exports trie.
  65. struct NodeState {
  66. NodeState(const uint8_t *Ptr);
  67. const uint8_t *Start;
  68. const uint8_t *Current;
  69. uint64_t Flags;
  70. uint64_t Address;
  71. uint64_t Other;
  72. const char *ImportName;
  73. unsigned ChildCount;
  74. unsigned NextChildIndex;
  75. unsigned ParentStringLength;
  76. bool IsExportNode;
  77. };
  78. ArrayRef<uint8_t> Trie;
  79. SmallString<256> CumulativeString;
  80. SmallVector<NodeState, 16> Stack;
  81. bool Malformed;
  82. bool Done;
  83. };
  84. typedef content_iterator<ExportEntry> export_iterator;
  85. /// MachORebaseEntry encapsulates the current state in the decompression of
  86. /// rebasing opcodes. This allows you to iterate through the compressed table of
  87. /// rebasing using:
  88. /// for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
  89. /// }
  90. class MachORebaseEntry {
  91. public:
  92. MachORebaseEntry(ArrayRef<uint8_t> opcodes, bool is64Bit);
  93. uint32_t segmentIndex() const;
  94. uint64_t segmentOffset() const;
  95. StringRef typeName() const;
  96. bool operator==(const MachORebaseEntry &) const;
  97. void moveNext();
  98. private:
  99. friend class MachOObjectFile;
  100. void moveToFirst();
  101. void moveToEnd();
  102. uint64_t readULEB128();
  103. ArrayRef<uint8_t> Opcodes;
  104. const uint8_t *Ptr;
  105. uint64_t SegmentOffset;
  106. uint32_t SegmentIndex;
  107. uint64_t RemainingLoopCount;
  108. uint64_t AdvanceAmount;
  109. uint8_t RebaseType;
  110. uint8_t PointerSize;
  111. bool Malformed;
  112. bool Done;
  113. };
  114. typedef content_iterator<MachORebaseEntry> rebase_iterator;
  115. /// MachOBindEntry encapsulates the current state in the decompression of
  116. /// binding opcodes. This allows you to iterate through the compressed table of
  117. /// bindings using:
  118. /// for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
  119. /// }
  120. class MachOBindEntry {
  121. public:
  122. enum class Kind { Regular, Lazy, Weak };
  123. MachOBindEntry(ArrayRef<uint8_t> Opcodes, bool is64Bit, MachOBindEntry::Kind);
  124. uint32_t segmentIndex() const;
  125. uint64_t segmentOffset() const;
  126. StringRef typeName() const;
  127. StringRef symbolName() const;
  128. uint32_t flags() const;
  129. int64_t addend() const;
  130. int ordinal() const;
  131. bool operator==(const MachOBindEntry &) const;
  132. void moveNext();
  133. private:
  134. friend class MachOObjectFile;
  135. void moveToFirst();
  136. void moveToEnd();
  137. uint64_t readULEB128();
  138. int64_t readSLEB128();
  139. ArrayRef<uint8_t> Opcodes;
  140. const uint8_t *Ptr;
  141. uint64_t SegmentOffset;
  142. uint32_t SegmentIndex;
  143. StringRef SymbolName;
  144. int Ordinal;
  145. uint32_t Flags;
  146. int64_t Addend;
  147. uint64_t RemainingLoopCount;
  148. uint64_t AdvanceAmount;
  149. uint8_t BindType;
  150. uint8_t PointerSize;
  151. Kind TableKind;
  152. bool Malformed;
  153. bool Done;
  154. };
  155. typedef content_iterator<MachOBindEntry> bind_iterator;
  156. class MachOObjectFile : public ObjectFile {
  157. public:
  158. struct LoadCommandInfo {
  159. const char *Ptr; // Where in memory the load command is.
  160. MachO::load_command C; // The command itself.
  161. };
  162. typedef SmallVector<LoadCommandInfo, 4> LoadCommandList;
  163. typedef LoadCommandList::const_iterator load_command_iterator;
  164. MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
  165. std::error_code &EC);
  166. void moveSymbolNext(DataRefImpl &Symb) const override;
  167. uint64_t getNValue(DataRefImpl Sym) const;
  168. ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
  169. // MachO specific.
  170. std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
  171. unsigned getSectionType(SectionRef Sec) const;
  172. ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
  173. uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
  174. uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
  175. SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
  176. uint32_t getSymbolFlags(DataRefImpl Symb) const override;
  177. std::error_code getSymbolSection(DataRefImpl Symb,
  178. section_iterator &Res) const override;
  179. unsigned getSymbolSectionID(SymbolRef Symb) const;
  180. unsigned getSectionID(SectionRef Sec) const;
  181. void moveSectionNext(DataRefImpl &Sec) const override;
  182. std::error_code getSectionName(DataRefImpl Sec,
  183. StringRef &Res) const override;
  184. uint64_t getSectionAddress(DataRefImpl Sec) const override;
  185. uint64_t getSectionSize(DataRefImpl Sec) const override;
  186. std::error_code getSectionContents(DataRefImpl Sec,
  187. StringRef &Res) const override;
  188. uint64_t getSectionAlignment(DataRefImpl Sec) const override;
  189. bool isSectionText(DataRefImpl Sec) const override;
  190. bool isSectionData(DataRefImpl Sec) const override;
  191. bool isSectionBSS(DataRefImpl Sec) const override;
  192. bool isSectionVirtual(DataRefImpl Sec) const override;
  193. relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
  194. relocation_iterator section_rel_end(DataRefImpl Sec) const override;
  195. void moveRelocationNext(DataRefImpl &Rel) const override;
  196. uint64_t getRelocationOffset(DataRefImpl Rel) const override;
  197. symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
  198. section_iterator getRelocationSection(DataRefImpl Rel) const;
  199. uint64_t getRelocationType(DataRefImpl Rel) const override;
  200. void getRelocationTypeName(DataRefImpl Rel,
  201. SmallVectorImpl<char> &Result) const override;
  202. uint8_t getRelocationLength(DataRefImpl Rel) const;
  203. // MachO specific.
  204. std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
  205. section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
  206. // TODO: Would be useful to have an iterator based version
  207. // of the load command interface too.
  208. basic_symbol_iterator symbol_begin_impl() const override;
  209. basic_symbol_iterator symbol_end_impl() const override;
  210. // MachO specific.
  211. basic_symbol_iterator getSymbolByIndex(unsigned Index) const;
  212. section_iterator section_begin() const override;
  213. section_iterator section_end() const override;
  214. uint8_t getBytesInAddress() const override;
  215. StringRef getFileFormatName() const override;
  216. unsigned getArch() const override;
  217. Triple getArch(const char **McpuDefault, Triple *ThumbTriple) const;
  218. relocation_iterator section_rel_begin(unsigned Index) const;
  219. relocation_iterator section_rel_end(unsigned Index) const;
  220. dice_iterator begin_dices() const;
  221. dice_iterator end_dices() const;
  222. load_command_iterator begin_load_commands() const;
  223. load_command_iterator end_load_commands() const;
  224. iterator_range<load_command_iterator> load_commands() const;
  225. /// For use iterating over all exported symbols.
  226. iterator_range<export_iterator> exports() const;
  227. /// For use examining a trie not in a MachOObjectFile.
  228. static iterator_range<export_iterator> exports(ArrayRef<uint8_t> Trie);
  229. /// For use iterating over all rebase table entries.
  230. iterator_range<rebase_iterator> rebaseTable() const;
  231. /// For use examining rebase opcodes not in a MachOObjectFile.
  232. static iterator_range<rebase_iterator> rebaseTable(ArrayRef<uint8_t> Opcodes,
  233. bool is64);
  234. /// For use iterating over all bind table entries.
  235. iterator_range<bind_iterator> bindTable() const;
  236. /// For use iterating over all lazy bind table entries.
  237. iterator_range<bind_iterator> lazyBindTable() const;
  238. /// For use iterating over all lazy bind table entries.
  239. iterator_range<bind_iterator> weakBindTable() const;
  240. /// For use examining bind opcodes not in a MachOObjectFile.
  241. static iterator_range<bind_iterator> bindTable(ArrayRef<uint8_t> Opcodes,
  242. bool is64,
  243. MachOBindEntry::Kind);
  244. // In a MachO file, sections have a segment name. This is used in the .o
  245. // files. They have a single segment, but this field specifies which segment
  246. // a section should be put in in the final object.
  247. StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
  248. // Names are stored as 16 bytes. These returns the raw 16 bytes without
  249. // interpreting them as a C string.
  250. ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
  251. ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
  252. // MachO specific Info about relocations.
  253. bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
  254. unsigned getPlainRelocationSymbolNum(
  255. const MachO::any_relocation_info &RE) const;
  256. bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
  257. bool getScatteredRelocationScattered(
  258. const MachO::any_relocation_info &RE) const;
  259. uint32_t getScatteredRelocationValue(
  260. const MachO::any_relocation_info &RE) const;
  261. uint32_t getScatteredRelocationType(
  262. const MachO::any_relocation_info &RE) const;
  263. unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
  264. unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
  265. unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
  266. unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
  267. SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
  268. // MachO specific structures.
  269. MachO::section getSection(DataRefImpl DRI) const;
  270. MachO::section_64 getSection64(DataRefImpl DRI) const;
  271. MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
  272. MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
  273. MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
  274. MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
  275. MachO::linkedit_data_command
  276. getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
  277. MachO::segment_command
  278. getSegmentLoadCommand(const LoadCommandInfo &L) const;
  279. MachO::segment_command_64
  280. getSegment64LoadCommand(const LoadCommandInfo &L) const;
  281. MachO::linker_option_command
  282. getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
  283. MachO::version_min_command
  284. getVersionMinLoadCommand(const LoadCommandInfo &L) const;
  285. MachO::dylib_command
  286. getDylibIDLoadCommand(const LoadCommandInfo &L) const;
  287. MachO::dyld_info_command
  288. getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
  289. MachO::dylinker_command
  290. getDylinkerCommand(const LoadCommandInfo &L) const;
  291. MachO::uuid_command
  292. getUuidCommand(const LoadCommandInfo &L) const;
  293. MachO::rpath_command
  294. getRpathCommand(const LoadCommandInfo &L) const;
  295. MachO::source_version_command
  296. getSourceVersionCommand(const LoadCommandInfo &L) const;
  297. MachO::entry_point_command
  298. getEntryPointCommand(const LoadCommandInfo &L) const;
  299. MachO::encryption_info_command
  300. getEncryptionInfoCommand(const LoadCommandInfo &L) const;
  301. MachO::encryption_info_command_64
  302. getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
  303. MachO::sub_framework_command
  304. getSubFrameworkCommand(const LoadCommandInfo &L) const;
  305. MachO::sub_umbrella_command
  306. getSubUmbrellaCommand(const LoadCommandInfo &L) const;
  307. MachO::sub_library_command
  308. getSubLibraryCommand(const LoadCommandInfo &L) const;
  309. MachO::sub_client_command
  310. getSubClientCommand(const LoadCommandInfo &L) const;
  311. MachO::routines_command
  312. getRoutinesCommand(const LoadCommandInfo &L) const;
  313. MachO::routines_command_64
  314. getRoutinesCommand64(const LoadCommandInfo &L) const;
  315. MachO::thread_command
  316. getThreadCommand(const LoadCommandInfo &L) const;
  317. MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
  318. MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
  319. const MachO::mach_header &getHeader() const;
  320. const MachO::mach_header_64 &getHeader64() const;
  321. uint32_t
  322. getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
  323. unsigned Index) const;
  324. MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
  325. unsigned Index) const;
  326. MachO::symtab_command getSymtabLoadCommand() const;
  327. MachO::dysymtab_command getDysymtabLoadCommand() const;
  328. MachO::linkedit_data_command getDataInCodeLoadCommand() const;
  329. MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
  330. ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
  331. ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
  332. ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
  333. ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
  334. ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
  335. ArrayRef<uint8_t> getUuid() const;
  336. StringRef getStringTableData() const;
  337. bool is64Bit() const;
  338. void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
  339. static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
  340. StringRef &Suffix);
  341. static Triple::ArchType getArch(uint32_t CPUType);
  342. static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
  343. const char **McpuDefault = nullptr);
  344. static Triple getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
  345. const char **McpuDefault = nullptr);
  346. static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
  347. const char **McpuDefault, Triple *ThumbTriple);
  348. static bool isValidArch(StringRef ArchFlag);
  349. static Triple getHostArch();
  350. bool isRelocatableObject() const override;
  351. bool hasPageZeroSegment() const { return HasPageZeroSegment; }
  352. static bool classof(const Binary *v) {
  353. return v->isMachO();
  354. }
  355. private:
  356. uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
  357. union {
  358. MachO::mach_header_64 Header64;
  359. MachO::mach_header Header;
  360. };
  361. typedef SmallVector<const char*, 1> SectionList;
  362. SectionList Sections;
  363. typedef SmallVector<const char*, 1> LibraryList;
  364. LibraryList Libraries;
  365. LoadCommandList LoadCommands;
  366. typedef SmallVector<StringRef, 1> LibraryShortName;
  367. mutable LibraryShortName LibrariesShortNames;
  368. const char *SymtabLoadCmd;
  369. const char *DysymtabLoadCmd;
  370. const char *DataInCodeLoadCmd;
  371. const char *LinkOptHintsLoadCmd;
  372. const char *DyldInfoLoadCmd;
  373. const char *UuidLoadCmd;
  374. bool HasPageZeroSegment;
  375. };
  376. /// DiceRef
  377. inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
  378. : DicePimpl(DiceP) , OwningObject(Owner) {}
  379. inline bool DiceRef::operator==(const DiceRef &Other) const {
  380. return DicePimpl == Other.DicePimpl;
  381. }
  382. inline bool DiceRef::operator<(const DiceRef &Other) const {
  383. return DicePimpl < Other.DicePimpl;
  384. }
  385. inline void DiceRef::moveNext() {
  386. const MachO::data_in_code_entry *P =
  387. reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
  388. DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
  389. }
  390. // Since a Mach-O data in code reference, a DiceRef, can only be created when
  391. // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
  392. // the methods that get the values of the fields of the reference.
  393. inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
  394. const MachOObjectFile *MachOOF =
  395. static_cast<const MachOObjectFile *>(OwningObject);
  396. MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
  397. Result = Dice.offset;
  398. return std::error_code();
  399. }
  400. inline std::error_code DiceRef::getLength(uint16_t &Result) const {
  401. const MachOObjectFile *MachOOF =
  402. static_cast<const MachOObjectFile *>(OwningObject);
  403. MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
  404. Result = Dice.length;
  405. return std::error_code();
  406. }
  407. inline std::error_code DiceRef::getKind(uint16_t &Result) const {
  408. const MachOObjectFile *MachOOF =
  409. static_cast<const MachOObjectFile *>(OwningObject);
  410. MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
  411. Result = Dice.kind;
  412. return std::error_code();
  413. }
  414. inline DataRefImpl DiceRef::getRawDataRefImpl() const {
  415. return DicePimpl;
  416. }
  417. inline const ObjectFile *DiceRef::getObjectFile() const {
  418. return OwningObject;
  419. }
  420. }
  421. }
  422. #endif