2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #ifndef __ALL_IMAGES_H__
26 #define __ALL_IMAGES_H__
29 #include <mach-o/loader.h>
31 #include "dyld_priv.h"
33 #include "LaunchCache.h"
38 #if __MAC_OS_X_VERSION_MIN_REQUIRED
39 // only in macOS and deprecated
40 struct VIS_HIDDEN __NSObjectFileImage
42 const char* path
= nullptr;
43 const void* memSource
= nullptr;
45 const mach_header
* loadAddress
= nullptr;
46 const dyld3::launch_cache::binary_format::Image
* binImage
= nullptr;
52 class VIS_HIDDEN AllImages
55 typedef launch_cache::binary_format::Closure BinaryClosure
;
56 typedef launch_cache::binary_format::ImageGroup BinaryImageGroup
;
57 typedef launch_cache::binary_format::Image BinaryImage
;
58 typedef launch_cache::ImageGroupList ImageGroupList
;
59 typedef void (*NotifyFunc
)(const mach_header
* mh
, intptr_t slide
);
61 void init(const BinaryClosure
* closure
, const void* dyldCacheLoadAddress
, const char* dyldCachePath
,
62 const dyld3::launch_cache::DynArray
<loader::ImageInfo
>& initialImages
);
63 void setMainPath(const char* path
);
64 void applyInitialImages();
66 void addImages(const dyld3::launch_cache::DynArray
<loader::ImageInfo
>& newImages
);
67 void removeImages(const launch_cache::DynArray
<loader::ImageInfo
>& unloadImages
);
68 void setNeverUnload(const loader::ImageInfo
& existingImage
);
69 void applyInterposingToDyldCache(const launch_cache::binary_format::Closure
* closure
, const dyld3::launch_cache::DynArray
<loader::ImageInfo
>& initialImages
);
70 void runInitialzersBottomUp(const mach_header
* imageLoadAddress
);
71 void setInitialGroups();
73 uint32_t count() const;
74 const BinaryImageGroup
* cachedDylibsGroup();
75 const BinaryImageGroup
* otherDylibsGroup();
76 const BinaryImageGroup
* mainClosureGroup();
77 const BinaryClosure
* mainClosure() { return _mainClosure
; }
78 uint32_t currentGroupsCount() const;
79 void copyCurrentGroups(ImageGroupList
& groups
) const;
81 const BinaryImage
* messageClosured(const char* path
, const char* apiName
, const char* closuredErrorMessages
[3], int& closuredErrorMessagesCount
);
83 launch_cache::Image
findByLoadOrder(uint32_t index
, const mach_header
** loadAddress
) const;
84 launch_cache::Image
findByLoadAddress(const mach_header
* loadAddress
) const;
85 launch_cache::Image
findByOwnedAddress(const void* addr
, const mach_header
** loadAddress
, uint8_t* permissions
=nullptr) const;
86 const mach_header
* findLoadAddressByImage(const BinaryImage
*) const;
87 bool findIndexForLoadAddress(const mach_header
* loadAddress
, uint32_t& index
);
88 void forEachImage(void (^handler
)(uint32_t imageIndex
, const mach_header
* loadAddress
, const launch_cache::Image image
, bool& stop
)) const;
90 const mach_header
* mainExecutable() const;
91 launch_cache::Image
mainExecutableImage() const;
92 const void* cacheLoadAddress() const { return _dyldCacheAddress
; }
93 const char* dyldCachePath() const { return _dyldCachePath
; }
94 const char* imagePath(const BinaryImage
*) const;
96 const mach_header
* alreadyLoaded(const char* path
, bool bumpRefCount
);
97 const mach_header
* alreadyLoaded(const BinaryImage
*, bool bumpRefCount
);
98 const mach_header
* alreadyLoaded(uint64_t inode
, uint64_t mtime
, bool bumpRefCount
);
99 const BinaryImage
* findImageInKnownGroups(const char* path
);
101 bool imageUnloadable(const launch_cache::Image
& image
, const mach_header
* loadAddress
) const;
102 void incRefCount(const mach_header
* loadAddress
);
103 void decRefCount(const mach_header
* loadAddress
);
105 void addLoadNotifier(NotifyFunc
);
106 void addUnloadNotifier(NotifyFunc
);
107 void setObjCNotifiers(_dyld_objc_notify_mapped
, _dyld_objc_notify_init
, _dyld_objc_notify_unmapped
);
108 void notifyObjCUnmap(const char* path
, const struct mach_header
* mh
);
110 void runLibSystemInitializer(const mach_header
* imageLoadAddress
, const launch_cache::binary_format::Image
* binImage
);
112 void setOldAllImageInfo(dyld_all_image_infos
* old
) { _oldAllImageInfos
= old
; }
113 dyld_all_image_infos
* oldAllImageInfo() const { return _oldAllImageInfos
;}
115 void notifyMonitorMain();
116 void notifyMonitorLoads(const launch_cache::DynArray
<loader::ImageInfo
>& newImages
);
117 void notifyMonitorUnloads(const launch_cache::DynArray
<loader::ImageInfo
>& unloadingImages
);
119 #if __MAC_OS_X_VERSION_MIN_REQUIRED
120 __NSObjectFileImage
* addNSObjectFileImage();
121 bool hasNSObjectFileImage(__NSObjectFileImage
*);
122 void removeNSObjectFileImage(__NSObjectFileImage
*);
129 const char*** NXArgvPtr
;
130 const char*** environPtr
;
131 const char** __prognamePtr
;
133 void setProgramVars(ProgramVars
* vars
);
137 typedef void (*Initializer
)(int argc
, const char* argv
[], char* envp
[], const char* apple
[], const ProgramVars
* vars
);
138 typedef const launch_cache::DynArray
<loader::ImageInfo
> StartImageArray
;
140 void runInitialzersInImage(const mach_header
* imageLoadAddress
, const launch_cache::binary_format::Image
* binImage
);
141 void mirrorToOldAllImageInfos();
142 void garbageCollectImages();
143 void vmAccountingSetSuspended(bool suspend
);
144 void copyCurrentGroupsNoLock(ImageGroupList
& groups
) const;
146 const BinaryClosure
* _mainClosure
= nullptr;
147 const void* _dyldCacheAddress
= nullptr;
148 const char* _dyldCachePath
= nullptr;
149 StartImageArray
* _initialImages
= nullptr;
150 const char* _mainExeOverridePath
= nullptr;
151 _dyld_objc_notify_mapped _objcNotifyMapped
= nullptr;
152 _dyld_objc_notify_init _objcNotifyInit
= nullptr;
153 _dyld_objc_notify_unmapped _objcNotifyUnmapped
= nullptr;
154 ProgramVars
* _programVars
= nullptr;
155 dyld_all_image_infos
* _oldAllImageInfos
= nullptr;
156 dyld_image_info
* _oldAllImageArray
= nullptr;
157 uint32_t _oldArrayAllocCount
= 0;
158 pthread_mutex_t _initializerLock
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER
;
159 pthread_cond_t _initializerCondition
= PTHREAD_COND_INITIALIZER
;
160 int32_t _gcCount
= 0;
163 extern AllImages gAllImages
;
169 #endif // __ALL_IMAGES_H__