macho_info.hpp 14 KB

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