macho_info.hpp 14 KB

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