dyld_priv.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
  2. *
  3. * Copyright (c) 2003-2010 Apple Inc. All rights reserved.
  4. *
  5. * @APPLE_LICENSE_HEADER_START@
  6. *
  7. * This file contains Original Code and/or Modifications of Original Code
  8. * as defined in and that are subject to the Apple Public Source License
  9. * Version 2.0 (the 'License'). You may not use this file except in
  10. * compliance with the License. Please obtain a copy of the License at
  11. * http://www.opensource.apple.com/apsl/ and read it before using this
  12. * file.
  13. *
  14. * The Original Code and all software distributed under the License are
  15. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  16. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  17. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  19. * Please see the License for the specific language governing rights and
  20. * limitations under the License.
  21. *
  22. * @APPLE_LICENSE_HEADER_END@
  23. */
  24. #define DYLD_MACOSX_VERSION_10_11 0x000A0B00
  25. #define DYLD_MACOSX_VERSION_10_12 0x000A0C00
  26. #define DYLD_MACOSX_VERSION_10_13 0x000A0D00
  27. #define DYLD_MACOSX_VERSION_10_14 0x000A0E00
  28. #ifndef _MACH_O_DYLD_PRIV_H_
  29. #define _MACH_O_DYLD_PRIV_H_
  30. #include <assert.h>
  31. #include <stdbool.h>
  32. #include <Availability.h>
  33. #include <TargetConditionals.h>
  34. #include <mach-o/dyld.h>
  35. #include <mach-o/dyld_images.h>
  36. #include <uuid/uuid.h>
  37. #if __cplusplus
  38. extern "C" {
  39. #endif /* __cplusplus */
  40. //
  41. // private interface between libSystem.dylib and dyld
  42. //
  43. extern void _dyld_fork_child(void);
  44. // DEPRECATED
  45. enum dyld_image_states
  46. {
  47. dyld_image_state_mapped = 10, // No batch notification for this
  48. dyld_image_state_dependents_mapped = 20, // Only batch notification for this
  49. dyld_image_state_rebased = 30,
  50. dyld_image_state_bound = 40,
  51. dyld_image_state_dependents_initialized = 45, // Only single notification for this
  52. dyld_image_state_initialized = 50,
  53. dyld_image_state_terminated = 60 // Only single notification for this
  54. };
  55. // DEPRECATED
  56. typedef const char* (*dyld_image_state_change_handler)(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[]);
  57. typedef void (*_dyld_objc_notify_mapped)(unsigned count, const char* const paths[], const struct mach_header* const mh[]);
  58. typedef void (*_dyld_objc_notify_init)(const char* path, const struct mach_header* mh);
  59. typedef void (*_dyld_objc_notify_unmapped)(const char* path, const struct mach_header* mh);
  60. //
  61. // Note: only for use by objc runtime
  62. // Register handlers to be called when objc images are mapped, unmapped, and initialized.
  63. // Dyld will call back the "mapped" function with an array of images that contain an objc-image-info section.
  64. // Those images that are dylibs will have the ref-counts automatically bumped, so objc will no longer need to
  65. // call dlopen() on them to keep them from being unloaded. During the call to _dyld_objc_notify_register(),
  66. // dyld will call the "mapped" function with already loaded objc images. During any later dlopen() call,
  67. // dyld will also call the "mapped" function. Dyld will call the "init" function when dyld would be called
  68. // initializers in that image. This is when objc calls any +load methods in that image.
  69. //
  70. void _dyld_objc_notify_register(_dyld_objc_notify_mapped mapped,
  71. _dyld_objc_notify_init init,
  72. _dyld_objc_notify_unmapped unmapped);
  73. //
  74. // Possible thread-local variable state changes for which you can register to be notified
  75. //
  76. enum dyld_tlv_states {
  77. dyld_tlv_state_allocated = 10, // TLV range newly allocated
  78. dyld_tlv_state_deallocated = 20 // TLV range about to be deallocated
  79. };
  80. //
  81. // Info about thread-local variable storage.
  82. //
  83. typedef struct {
  84. size_t info_size; // sizeof(dyld_tlv_info)
  85. void * tlv_addr; // Base address of TLV storage
  86. size_t tlv_size; // Byte size of TLV storage
  87. } dyld_tlv_info;
  88. #if __BLOCKS__
  89. //
  90. // Callback that notes changes to thread-local variable storage.
  91. //
  92. typedef void (^dyld_tlv_state_change_handler)(enum dyld_tlv_states state, const dyld_tlv_info *info);
  93. //
  94. // Register a handler to be called when a thread adds or removes storage for thread-local variables.
  95. // The registered handler will only be called from and on behalf of the thread that owns the storage.
  96. // The registered handler will NOT be called for any storage that was
  97. // already allocated before dyld_register_tlv_state_change_handler() was
  98. // called. Use dyld_enumerate_tlv_storage() to get that information.
  99. // Exists in Mac OS X 10.7 and later
  100. //
  101. extern void
  102. dyld_register_tlv_state_change_handler(enum dyld_tlv_states state, dyld_tlv_state_change_handler handler);
  103. //
  104. // Enumerate the current thread-local variable storage allocated for the current thread.
  105. // Exists in Mac OS X 10.7 and later
  106. //
  107. extern void
  108. dyld_enumerate_tlv_storage(dyld_tlv_state_change_handler handler);
  109. #endif
  110. //
  111. // get slide for a given loaded mach_header
  112. // Mac OS X 10.6 and later
  113. //
  114. extern intptr_t _dyld_get_image_slide(const struct mach_header* mh);
  115. struct dyld_unwind_sections
  116. {
  117. const struct mach_header* mh;
  118. const void* dwarf_section;
  119. uintptr_t dwarf_section_length;
  120. const void* compact_unwind_section;
  121. uintptr_t compact_unwind_section_length;
  122. };
  123. //
  124. // Returns true iff some loaded mach-o image contains "addr".
  125. // info->mh mach header of image containing addr
  126. // info->dwarf_section pointer to start of __TEXT/__eh_frame section
  127. // info->dwarf_section_length length of __TEXT/__eh_frame section
  128. // info->compact_unwind_section pointer to start of __TEXT/__unwind_info section
  129. // info->compact_unwind_section_length length of __TEXT/__unwind_info section
  130. //
  131. // Exists in Mac OS X 10.6 and later
  132. #if !__USING_SJLJ_EXCEPTIONS__
  133. extern bool _dyld_find_unwind_sections(void* addr, struct dyld_unwind_sections* info);
  134. #endif
  135. //
  136. // This is an optimized form of dladdr() that only returns the dli_fname field.
  137. //
  138. // Exists in Mac OS X 10.6 and later
  139. extern const char* dyld_image_path_containing_address(const void* addr);
  140. //
  141. // This is an optimized form of dladdr() that only returns the dli_fbase field.
  142. // Return NULL, if address is not in any image tracked by dyld.
  143. //
  144. // Exists in Mac OS X 10.11 and later
  145. extern const struct mach_header* dyld_image_header_containing_address(const void* addr);
  146. typedef uint32_t dyld_platform_t;
  147. typedef struct {
  148. dyld_platform_t platform;
  149. uint32_t version;
  150. } dyld_build_version_t;
  151. // Returns the active platform of the process
  152. // extern dyld_platform_t dyld_get_active_platform(void) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  153. // Base platforms are platforms that have version numbers (macOS, iOS, watchos, tvOS, bridgeOS)
  154. // All other platforms are mapped to a base platform for version checks
  155. // extern dyld_platform_t dyld_get_base_platform(dyld_platform_t platform) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  156. // SPI to ask if a platform is a simulation platform
  157. // extern bool dyld_is_simulator_platform(dyld_platform_t platform) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  158. // Takes a version and returns if the image was built againt that SDK or newer
  159. // In the case of multi_plaform mach-o's it tests against the active platform
  160. // extern bool dyld_sdk_at_least(const struct mach_header* mh, dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  161. // Takes a version and returns if the image was built with that minos version or newer
  162. // In the case of multi_plaform mach-o's it tests against the active platform
  163. // extern bool dyld_minos_at_least(const struct mach_header* mh, dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  164. // Convenience versions of the previous two functions that run against the the main executable
  165. // extern bool dyld_program_sdk_at_least(dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  166. // extern bool dyld_program_minos_at_least(dyld_build_version_t version) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  167. // Function that walks through the load commands and calls the internal block for every version found
  168. // Intended as a fallback for very complex (and rare) version checks, or for tools that need to
  169. // print our everything for diagnostic reasons
  170. // extern void dyld_get_image_versions(const struct mach_header* mh, void (^callback)(dyld_platform_t platform, uint32_t sdk_version, uint32_t min_version)) __API_AVAILABLE(macos(10.14), ios(12.0), watchos(5.0), tvos(12.0), bridgeos(3.0));
  171. // Convienence constants for dyld version SPIs.
  172. //@VERSION_SET_DEFS@
  173. //@MACOS_PLATFORM_VERSION_DEFS@
  174. //@IOS_PLATFORM_VERSION_DEFS@
  175. //@WATCHOS_PLATFORM_VERSION_DEFS@
  176. //@TVOS_PLATFORM_VERSION_DEFS@
  177. //@BRIDGEOS_PLATFORM_VERSION_DEFS@
  178. // Convienence constants for return values from dyld_get_sdk_version() and friends.
  179. //@MAC_VERSION_DEFS@
  180. //@IOS_VERSION_DEFS@
  181. //@WATCHOS_VERSION_DEFS@
  182. //
  183. // This finds the SDK version a binary was built against.
  184. // Returns zero on error, or if SDK version could not be determined.
  185. //
  186. // Exists in Mac OS X 10.8 and later
  187. // Exists in iOS 6.0 and later
  188. extern uint32_t dyld_get_sdk_version(const struct mach_header* mh);
  189. //
  190. // This finds the SDK version that the main executable was built against.
  191. // Returns zero on error, or if SDK version could not be determined.
  192. //
  193. // Note on watchOS, this returns the equivalent iOS SDK version number
  194. // (i.e an app built against watchOS 2.0 SDK returne 9.0). To see the
  195. // platform specific sdk version use dyld_get_program_sdk_watch_os_version().
  196. //
  197. // Exists in Mac OS X 10.8 and later
  198. // Exists in iOS 6.0 and later
  199. extern uint32_t dyld_get_program_sdk_version(void);
  200. #if TARGET_OS_WATCH
  201. // watchOS only.
  202. // This finds the Watch OS SDK version that the main executable was built against.
  203. // Exists in Watch OS 2.0 and later
  204. extern uint32_t dyld_get_program_sdk_watch_os_version(void) __API_AVAILABLE(watchos(2.0));
  205. // watchOS only.
  206. // This finds the Watch min OS version that the main executable was built to run on.
  207. // Note: dyld_get_program_min_os_version() returns the iOS equivalent (e.g. 9.0)
  208. // whereas this returns the raw watchOS version (e.g. 2.0).
  209. // Exists in Watch OS 3.0 and later
  210. extern uint32_t dyld_get_program_min_watch_os_version(void) __API_AVAILABLE(watchos(3.0));
  211. #endif
  212. #if TARGET_OS_BRIDGE
  213. // bridgeOS only.
  214. // This finds the bridgeOS SDK version that the main executable was built against.
  215. // Exists in bridgeOSOS 2.0 and later
  216. extern uint32_t dyld_get_program_sdk_bridge_os_version(void) __API_AVAILABLE(bridgeos(2.0));
  217. // bridgeOS only.
  218. // This finds the Watch min OS version that the main executable was built to run on.
  219. // Note: dyld_get_program_min_os_version() returns the iOS equivalent (e.g. 9.0)
  220. // whereas this returns the raw bridgeOS version (e.g. 2.0).
  221. // Exists in bridgeOS 2.0 and later
  222. extern uint32_t dyld_get_program_min_bridge_os_version(void) __API_AVAILABLE(bridgeos(2.0));
  223. #endif
  224. //
  225. // This finds the min OS version a binary was built to run on.
  226. // Returns zero on error, or if no min OS recorded in binary.
  227. //
  228. // Exists in Mac OS X 10.8 and later
  229. // Exists in iOS 6.0 and later
  230. extern uint32_t dyld_get_min_os_version(const struct mach_header* mh);
  231. //
  232. // This finds the min OS version the main executable was built to run on.
  233. // Returns zero on error, or if no min OS recorded in binary.
  234. //
  235. // Exists in Mac OS X 10.8 and later
  236. // Exists in iOS 6.0 and later
  237. extern uint32_t dyld_get_program_min_os_version(void);
  238. //
  239. // Returns if any OS dylib has overridden its copy in the shared cache
  240. //
  241. // Exists in iPhoneOS 3.1 and later
  242. // Exists in Mac OS X 10.10 and later
  243. extern bool dyld_shared_cache_some_image_overridden(void);
  244. //
  245. // Returns if the process is setuid or is code signed with entitlements.
  246. //
  247. // Exists in Mac OS X 10.9 and later
  248. extern bool dyld_process_is_restricted(void);
  249. //
  250. // Returns path used by dyld for standard dyld shared cache file for the current arch.
  251. //
  252. // Exists in Mac OS X 10.11 and later
  253. extern const char* dyld_shared_cache_file_path(void);
  254. //
  255. // <rdar://problem/13820686> for OpenGL to tell dyld it is ok to deallocate a memory based image when done.
  256. //
  257. // Exists in Mac OS X 10.9 and later
  258. #define NSLINKMODULE_OPTION_CAN_UNLOAD 0x20
  259. //
  260. // Update all bindings on specified image.
  261. // Looks for uses of 'replacement' and changes it to 'replacee'.
  262. // NOTE: this is less safe than using static interposing via DYLD_INSERT_LIBRARIES
  263. // because the running program may have already copy the pointer values to other
  264. // locations that dyld does not know about.
  265. //
  266. struct dyld_interpose_tuple {
  267. const void* replacement;
  268. const void* replacee;
  269. };
  270. extern void dyld_dynamic_interpose(const struct mach_header* mh, const struct dyld_interpose_tuple array[], size_t count);
  271. struct dyld_shared_cache_dylib_text_info {
  272. uint64_t version; // current version 1
  273. // following fields all exist in version 1
  274. uint64_t loadAddressUnslid;
  275. uint64_t textSegmentSize;
  276. uuid_t dylibUuid;
  277. const char* path; // pointer invalid at end of iterations
  278. // following fields all exist in version 2
  279. uint64_t textSegmentOffset; // offset from start of cache
  280. };
  281. typedef struct dyld_shared_cache_dylib_text_info dyld_shared_cache_dylib_text_info;
  282. #ifdef __BLOCKS__
  283. //
  284. // Given the UUID of a dyld shared cache file, this function will attempt to locate the cache
  285. // file and if found iterate all images, returning info about each one. Returns 0 on success.
  286. //
  287. // Exists in Mac OS X 10.11 and later
  288. // iOS 9.0 and later
  289. extern int dyld_shared_cache_iterate_text(const uuid_t cacheUuid, void (^callback)(const dyld_shared_cache_dylib_text_info* info));
  290. //
  291. // Given the UUID of a dyld shared cache file, and a NULL terminated array of extra directory paths to search,
  292. // this function will scan the standard and extra directories looking for a cache file that matches the UUID
  293. // and if found iterate all images, returning info about each one. Returns 0 on success.
  294. //
  295. // Exists in Mac OS X 10.12 and later
  296. // iOS 10.0 and later
  297. extern int dyld_shared_cache_find_iterate_text(const uuid_t cacheUuid, const char* extraSearchDirs[], void (^callback)(const dyld_shared_cache_dylib_text_info* info));
  298. #endif /* __BLOCKS */
  299. //
  300. // Returns if the specified address range is in a dyld owned memory
  301. // that is mapped read-only and will never be unloaded.
  302. //
  303. // Exists in Mac OS X 10.12 and later
  304. // iOS 10.0 and later
  305. extern bool _dyld_is_memory_immutable(const void* addr, size_t length);
  306. //
  307. // Finds the UUID (from LC_UUID load command) of given image.
  308. // Returns false if LC_UUID is missing or mach_header is malformed.
  309. //
  310. // Exists in Mac OS X 10.12 and later
  311. // Exists in iOS 10.0 and later
  312. extern bool _dyld_get_image_uuid(const struct mach_header* mh, uuid_t uuid);
  313. //
  314. // Gets the UUID of the dyld shared cache in the current process.
  315. // Returns false if there is no dyld shared cache in use by the processes.
  316. //
  317. // Exists in Mac OS X 10.12 and later
  318. // Exists in iOS 10.0 and later
  319. extern bool _dyld_get_shared_cache_uuid(uuid_t uuid);
  320. //
  321. // Returns the start address of the dyld cache in the process and sets length to the size of the cache.
  322. // Returns NULL if the process is not using a dyld shared cache
  323. //
  324. // Exists in Mac OS X 10.13 and later
  325. // Exists in iOS 11.0 and later
  326. extern const void* _dyld_get_shared_cache_range(size_t* length);
  327. struct dyld_image_uuid_offset {
  328. uuid_t uuid;
  329. uint64_t offsetInImage;
  330. const struct mach_header* image;
  331. };
  332. //
  333. // Given an array of addresses, returns info about each address.
  334. // Common usage is the array or addresses was produced by a stack backtrace.
  335. // For each address, returns the where that image was loaded, the offset
  336. // of the address in the image, and the image's uuid. If a specified
  337. // address is unknown to dyld, all fields will be returned a zeros.
  338. //
  339. // Exists in macOS 10.14 and later
  340. // Exists in iOS 12.0 and later
  341. extern void _dyld_images_for_addresses(unsigned count, const void* addresses[], struct dyld_image_uuid_offset infos[]);
  342. //
  343. // Lets you register a callback which is called each time an image is loaded and provides the mach_header*, path, and
  344. // whether the image may be unloaded later. During the call to _dyld_register_for_image_loads(), the callback is called
  345. // once for each image currently loaded.
  346. //
  347. // Exists in macOS 10.14 and later
  348. // Exists in iOS 12.0 and later
  349. extern void _dyld_register_for_image_loads(void (*func)(const struct mach_header* mh, const char* path, bool unloadable));
  350. //
  351. // When dyld must terminate a process because of a required dependent dylib
  352. // could not be loaded or a symbol is missing, dyld calls abort_with_reason()
  353. // using one of the following error codes.
  354. //
  355. #define DYLD_EXIT_REASON_DYLIB_MISSING 1
  356. #define DYLD_EXIT_REASON_DYLIB_WRONG_ARCH 2
  357. #define DYLD_EXIT_REASON_DYLIB_WRONG_VERSION 3
  358. #define DYLD_EXIT_REASON_SYMBOL_MISSING 4
  359. #define DYLD_EXIT_REASON_CODE_SIGNATURE 5
  360. #define DYLD_EXIT_REASON_FILE_SYSTEM_SANDBOX 6
  361. #define DYLD_EXIT_REASON_MALFORMED_MACHO 7
  362. #define DYLD_EXIT_REASON_OTHER 9
  363. //
  364. // When it has more information about the termination, dyld will use abort_with_payload().
  365. // The payload is a dyld_abort_payload structure. The fixed fields are offsets into the
  366. // payload for the corresponding string. If the offset is zero, that string is not available.
  367. //
  368. struct dyld_abort_payload {
  369. uint32_t version; // first version is 1
  370. uint32_t flags; // 0x00000001 means dyld terminated at launch, backtrace not useful
  371. uint32_t targetDylibPathOffset; // offset in payload of path string to dylib that could not be loaded
  372. uint32_t clientPathOffset; // offset in payload of path string to image requesting dylib
  373. uint32_t symbolOffset; // offset in payload of symbol string that could not be found
  374. // string data
  375. };
  376. typedef struct dyld_abort_payload dyld_abort_payload;
  377. // These global variables are implemented in libdyld.dylib
  378. // Old programs that used crt1.o also defined these globals.
  379. // The ones in dyld are not used when an old program is run.
  380. extern int NXArgc;
  381. extern const char** NXArgv;
  382. extern char** environ; // POSIX says this not const, because it pre-dates const
  383. extern const char* __progname;
  384. // called by libSystem_initializer only
  385. extern void _dyld_initializer(void);
  386. // never called from source code. Used by static linker to implement lazy binding
  387. extern void dyld_stub_binder(void) __asm__("dyld_stub_binder");
  388. // called by exit() before it calls cxa_finalize() so that thread_local
  389. // objects are destroyed before global objects.
  390. extern void _tlv_exit(void);
  391. // temp exports to keep tapi happy, until ASan stops using dyldVersionNumber
  392. extern double dyldVersionNumber;
  393. extern const char* dyldVersionString;
  394. #if __cplusplus
  395. }
  396. #endif /* __cplusplus */
  397. #endif /* _MACH_O_DYLD_PRIV_H_ */