macho_info.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2021 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_DETAIL_MACHO_INFO_HPP
  8. #define BOOST_DLL_DETAIL_MACHO_INFO_HPP
  9. #include <boost/dll/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #include <algorithm>
  14. #include <fstream>
  15. #include <string> // for std::getline
  16. #include <boost/cstdint.hpp>
  17. namespace boost { namespace dll { namespace detail {
  18. typedef int integer_t;
  19. typedef int vm_prot_t;
  20. typedef integer_t cpu_type_t;
  21. typedef integer_t cpu_subtype_t;
  22. template <class AddressOffsetT>
  23. struct mach_header_template {
  24. boost::uint32_t magic;
  25. cpu_type_t cputype;
  26. cpu_subtype_t cpusubtype;
  27. boost::uint32_t filetype;
  28. boost::uint32_t ncmds;
  29. boost::uint32_t sizeofcmds;
  30. boost::uint32_t flags[sizeof(AddressOffsetT) / sizeof(uint32_t)]; // Flags and reserved
  31. };
  32. typedef mach_header_template<boost::uint32_t> mach_header_32_;
  33. typedef mach_header_template<boost::uint64_t> mach_header_64_;
  34. struct load_command_ {
  35. boost::uint32_t cmd; /* type of command */
  36. boost::uint32_t cmdsize;
  37. };
  38. struct load_command_types {
  39. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SEGMENT_ = 0x1); /* segment of this file to be mapped */
  40. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SYMTAB_ = 0x2); /* link-edit stab symbol table info */
  41. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SYMSEG_ = 0x3); /* link-edit gdb symbol table info (obsolete) */
  42. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_THREAD_ = 0x4); /* thread */
  43. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_UNIXTHREAD_ = 0x5); /* unix thread (includes a stack) */
  44. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_LOADFVMLIB_ = 0x6); /* load a specified fixed VM shared library */
  45. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_IDFVMLIB_ = 0x7); /* fixed VM shared library identification */
  46. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_IDENT_ = 0x8); /* object identification info (obsolete) */
  47. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_FVMFILE_ = 0x9); /* fixed VM file inclusion (internal use) */
  48. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_PREPAGE_ = 0xa); /* prepage command (internal use) */
  49. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_DYSYMTAB_ = 0xb); /* dynamic link-edit symbol table info */
  50. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_LOAD_DYLIB_ = 0xc); /* load a dynamically linked shared library */
  51. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_ID_DYLIB_ = 0xd); /* dynamically linked shared lib ident */
  52. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_LOAD_DYLINKER_ = 0xe); /* load a dynamic linker */
  53. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_ID_DYLINKER_ = 0xf); /* dynamic linker identification */
  54. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_PREBOUND_DYLIB_ = 0x10); /* modules prebound for a dynamically linked shared library */
  55. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_ROUTINES_ = 0x11); /* image routines */
  56. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SUB_FRAMEWORK_ = 0x12); /* sub framework */
  57. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SUB_UMBRELLA_ = 0x13); /* sub umbrella */
  58. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SUB_CLIENT_ = 0x14); /* sub client */
  59. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SUB_LIBRARY_ = 0x15); /* sub library */
  60. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_TWOLEVEL_HINTS_ = 0x16); /* two-level namespace lookup hints */
  61. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_PREBIND_CKSUM_ = 0x17); /* prebind checksum */
  62. /*
  63. * After MacOS X 10.1 when a new load command is added that is required to be
  64. * understood by the dynamic linker for the image to execute properly the
  65. * LC_REQ_DYLD bit will be or'ed into the load command constant. If the dynamic
  66. * linker sees such a load command it it does not understand will issue a
  67. * "unknown load command required for execution" error and refuse to use the
  68. * image. Other load commands without this bit that are not understood will
  69. * simply be ignored.
  70. */
  71. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_REQ_DYLD_ = 0x80000000);
  72. /*
  73. * load a dynamically linked shared library that is allowed to be missing
  74. * (all symbols are weak imported).
  75. */
  76. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_LOAD_WEAK_DYLIB_ = (0x18 | LC_REQ_DYLD_));
  77. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SEGMENT_64_ = 0x19); /* 64-bit segment of this file to be mapped */
  78. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_ROUTINES_64_ = 0x1a); /* 64-bit image routines */
  79. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_UUID_ = 0x1b); /* the uuid */
  80. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_RPATH_ = (0x1c | LC_REQ_DYLD_)); /* runpath additions */
  81. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_CODE_SIGNATURE_ = 0x1d); /* local of code signature */
  82. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_SEGMENT_SPLIT_INFO_= 0x1e); /* local of info to split segments */
  83. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_REEXPORT_DYLIB_ = (0x1f | LC_REQ_DYLD_)); /* load and re-export dylib */
  84. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_LAZY_LOAD_DYLIB_ = 0x20); /* delay load of dylib until first use */
  85. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_ENCRYPTION_INFO_ = 0x21); /* encrypted segment information */
  86. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_DYLD_INFO_ = 0x22); /* compressed dyld information */
  87. BOOST_STATIC_CONSTANT(boost::uint32_t, LC_DYLD_INFO_ONLY_ = (0x22|LC_REQ_DYLD_)); /* compressed dyld information only */
  88. };
  89. template <class AddressOffsetT>
  90. struct segment_command_template {
  91. boost::uint32_t cmd; /* LC_SEGMENT_ */
  92. boost::uint32_t cmdsize; /* includes sizeof section structs */
  93. char segname[16]; /* segment name */
  94. AddressOffsetT vmaddr; /* memory address of this segment */
  95. AddressOffsetT vmsize; /* memory size of this segment */
  96. AddressOffsetT fileoff; /* file offset of this segment */
  97. AddressOffsetT filesize; /* amount to map from the file */
  98. vm_prot_t maxprot; /* maximum VM protection */
  99. vm_prot_t initprot; /* initial VM protection */
  100. boost::uint32_t nsects; /* number of sections in segment */
  101. boost::uint32_t flags; /* flags */
  102. };
  103. typedef segment_command_template<boost::uint32_t> segment_command_32_;
  104. typedef segment_command_template<boost::uint64_t> segment_command_64_;
  105. template <class AddressOffsetT>
  106. struct section_template {
  107. char sectname[16]; /* name of this section */
  108. char segname[16]; /* segment this section goes in */
  109. AddressOffsetT addr; /* memory address of this section */
  110. AddressOffsetT size; /* size in bytes of this section */
  111. boost::uint32_t offset; /* file offset of this section */
  112. boost::uint32_t align; /* section alignment (power of 2) */
  113. boost::uint32_t reloff; /* file offset of relocation entries */
  114. boost::uint32_t nreloc; /* number of relocation entries */
  115. boost::uint32_t flags; /* flags (section type and attributes)*/
  116. boost::uint32_t reserved[1 + sizeof(AddressOffsetT) / sizeof(uint32_t)];
  117. };
  118. typedef section_template<boost::uint32_t> section_32_;
  119. typedef section_template<boost::uint64_t> section_64_;
  120. struct symtab_command_ {
  121. boost::uint32_t cmd; /* LC_SYMTAB_ */
  122. boost::uint32_t cmdsize; /* sizeof(struct symtab_command) */
  123. boost::uint32_t symoff; /* symbol table offset */
  124. boost::uint32_t nsyms; /* number of symbol table entries */
  125. boost::uint32_t stroff; /* string table offset */
  126. boost::uint32_t strsize; /* string table size in bytes */
  127. };
  128. template <class AddressOffsetT>
  129. struct nlist_template {
  130. boost::uint32_t n_strx;
  131. boost::uint8_t n_type;
  132. boost::uint8_t n_sect;
  133. boost::uint16_t n_desc;
  134. AddressOffsetT n_value;
  135. };
  136. typedef nlist_template<boost::uint32_t> nlist_32_;
  137. typedef nlist_template<boost::uint64_t> nlist_64_;
  138. template <class AddressOffsetT>
  139. class macho_info {
  140. typedef boost::dll::detail::mach_header_template<AddressOffsetT> header_t;
  141. typedef boost::dll::detail::load_command_ load_command_t;
  142. typedef boost::dll::detail::segment_command_template<AddressOffsetT> segment_t;
  143. typedef boost::dll::detail::section_template<AddressOffsetT> section_t;
  144. typedef boost::dll::detail::symtab_command_ symbol_header_t;
  145. typedef boost::dll::detail::nlist_template<AddressOffsetT> nlist_t;
  146. BOOST_STATIC_CONSTANT(boost::uint32_t, SEGMENT_CMD_NUMBER = (sizeof(AddressOffsetT) > 4 ? load_command_types::LC_SEGMENT_64_ : load_command_types::LC_SEGMENT_));
  147. public:
  148. static bool parsing_supported(std::ifstream& fs) {
  149. static const uint32_t magic_bytes = (sizeof(AddressOffsetT) <= sizeof(uint32_t) ? 0xfeedface : 0xfeedfacf);
  150. uint32_t magic;
  151. fs.seekg(0);
  152. fs.read(reinterpret_cast<char*>(&magic), sizeof(magic));
  153. return (magic_bytes == magic);
  154. }
  155. private:
  156. template <class T>
  157. static void read_raw(std::ifstream& fs, T& value, std::size_t size = sizeof(T)) {
  158. fs.read(reinterpret_cast<char*>(&value), size);
  159. }
  160. template <class F>
  161. static void command_finder(std::ifstream& fs, uint32_t cmd_num, F callback_f) {
  162. const header_t h = header(fs);
  163. load_command_t command;
  164. fs.seekg(sizeof(header_t));
  165. for (std::size_t i = 0; i < h.ncmds; ++i) {
  166. const std::ifstream::pos_type pos = fs.tellg();
  167. read_raw(fs, command);
  168. if (command.cmd != cmd_num) {
  169. fs.seekg(pos + static_cast<std::ifstream::pos_type>(command.cmdsize));
  170. continue;
  171. }
  172. fs.seekg(pos);
  173. callback_f(fs);
  174. fs.seekg(pos + static_cast<std::ifstream::pos_type>(command.cmdsize));
  175. }
  176. }
  177. struct section_names_gather {
  178. std::vector<std::string>& ret;
  179. void operator()(std::ifstream& fs) const {
  180. segment_t segment;
  181. read_raw(fs, segment);
  182. section_t section;
  183. ret.reserve(ret.size() + segment.nsects);
  184. for (std::size_t j = 0; j < segment.nsects; ++j) {
  185. read_raw(fs, section);
  186. // `segname` goes right after the `sectname`.
  187. // Forcing `sectname` to end on '\0'
  188. section.segname[0] = '\0';
  189. ret.push_back(section.sectname);
  190. if (ret.back().empty()) {
  191. ret.pop_back(); // Do not show empty names
  192. }
  193. }
  194. }
  195. };
  196. struct symbol_names_gather {
  197. std::vector<std::string>& ret;
  198. std::size_t section_index;
  199. void operator()(std::ifstream& fs) const {
  200. symbol_header_t symbh;
  201. read_raw(fs, symbh);
  202. ret.reserve(ret.size() + symbh.nsyms);
  203. nlist_t symbol;
  204. std::string symbol_name;
  205. for (std::size_t j = 0; j < symbh.nsyms; ++j) {
  206. fs.seekg(symbh.symoff + j * sizeof(nlist_t));
  207. read_raw(fs, symbol);
  208. if (!symbol.n_strx) {
  209. continue; // Symbol has no name
  210. }
  211. if ((symbol.n_type & 0x0e) != 0xe || !symbol.n_sect) {
  212. continue; // Symbol has no section
  213. }
  214. if (section_index && section_index != symbol.n_sect) {
  215. continue; // Not in the required section
  216. }
  217. fs.seekg(symbh.stroff + symbol.n_strx);
  218. std::getline(fs, symbol_name, '\0');
  219. if (symbol_name.empty()) {
  220. continue;
  221. }
  222. if (symbol_name[0] == '_') {
  223. // Linker adds additional '_' symbol. Could not find official docs for that case.
  224. ret.push_back(symbol_name.c_str() + 1);
  225. } else {
  226. ret.push_back(symbol_name);
  227. }
  228. }
  229. }
  230. };
  231. public:
  232. static std::vector<std::string> sections(std::ifstream& fs) {
  233. std::vector<std::string> ret;
  234. section_names_gather f = { ret };
  235. command_finder(fs, SEGMENT_CMD_NUMBER, f);
  236. return ret;
  237. }
  238. private:
  239. static header_t header(std::ifstream& fs) {
  240. header_t h;
  241. fs.seekg(0);
  242. read_raw(fs, h);
  243. return h;
  244. }
  245. public:
  246. static std::vector<std::string> symbols(std::ifstream& fs) {
  247. std::vector<std::string> ret;
  248. symbol_names_gather f = { ret, 0 };
  249. command_finder(fs, load_command_types::LC_SYMTAB_, f);
  250. return ret;
  251. }
  252. static std::vector<std::string> symbols(std::ifstream& fs, const char* section_name) {
  253. // Not very optimal solution
  254. std::vector<std::string> ret = sections(fs);
  255. std::vector<std::string>::iterator it = std::find(ret.begin(), ret.end(), section_name);
  256. if (it == ret.end()) {
  257. // No section with such name
  258. ret.clear();
  259. return ret;
  260. }
  261. // section indexes start from 1
  262. symbol_names_gather f = { ret, static_cast<std::size_t>(1 + (it - ret.begin())) };
  263. ret.clear();
  264. command_finder(fs, load_command_types::LC_SYMTAB_, f);
  265. return ret;
  266. }
  267. };
  268. typedef macho_info<boost::uint32_t> macho_info32;
  269. typedef macho_info<boost::uint64_t> macho_info64;
  270. }}} // namespace boost::dll::detail
  271. #endif // BOOST_DLL_DETAIL_MACHO_INFO_HPP