1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2015 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
26 #ifndef __IMAGELOADER_MEGADYLIB__
27 #define __IMAGELOADER_MEGADYLIB__
32 #include "ImageLoaderMachO.h"
33 #include "dyld_cache_format.h"
37 // ImageLoaderMegaDylib is the concrete subclass of ImageLoader which represents
38 // all dylibs in the shared cache.
40 class ImageLoaderMegaDylib
: public ImageLoader
{
42 static ImageLoaderMegaDylib
* makeImageLoaderMegaDylib(const dyld_cache_header
*, long slide
, const macho_header
* mainMH
, const LinkContext
&);
45 virtual ~ImageLoaderMegaDylib();
47 void appendImagesNeedingCoalescing(ImageLoader
* images
[], unsigned imageIndex
[], unsigned& count
);
48 virtual void initializeCoalIterator(CoalIterator
&, unsigned int loadOrder
, unsigned imageIndex
);
49 virtual bool incrementCoalIterator(CoalIterator
&);
50 virtual uintptr_t getAddressCoalIterator(CoalIterator
&, const LinkContext
& contex
);
51 virtual void updateUsesCoalIterator(CoalIterator
&, uintptr_t newAddr
, ImageLoader
* target
, unsigned imageIndex
, const LinkContext
& context
);
53 virtual const char* getIndexedPath(unsigned index
) const;
54 virtual const char* getIndexedShortName(unsigned) const;
55 virtual const char* getInstallPath() const;
56 virtual bool inSharedCache() const { return true; }
57 virtual bool containsSymbol(const void* addr
) const { unreachable(); }
58 virtual void* getThreadPC() const { unreachable(); }
59 virtual void* getMain() const { unreachable(); }
60 virtual const struct mach_header
* machHeader() const { unreachable(); }
61 virtual uintptr_t getSlide() const { return _slide
; }
62 virtual const void* getEnd() const { unreachable(); }
63 virtual bool hasCoalescedExports() const { unreachable(); }
64 virtual bool findExportedSymbolAddress(const LinkContext
& context
, const char* symbolName
,
65 const ImageLoader
* requestorImage
, int requestorOrdinalOfDef
,
66 bool runResolver
, const ImageLoader
** foundIn
, uintptr_t* address
) const;
67 virtual const Symbol
* findExportedSymbol(const char* name
, bool searchReExports
, const char* thisPath
, const ImageLoader
** foundIn
) const;
68 virtual uintptr_t getExportedSymbolAddress(const Symbol
* sym
, const LinkContext
& context
,
69 const ImageLoader
* requestor
, bool runResolver
, const char* symbolName
) const;
70 virtual DefinitionFlags
getExportedSymbolInfo(const Symbol
* sym
) const { unreachable(); }
71 virtual const char* getExportedSymbolName(const Symbol
* sym
) const { unreachable(); }
72 virtual uint32_t getExportedSymbolCount() const { unreachable(); }
73 virtual const Symbol
* getIndexedExportedSymbol(uint32_t index
) const { unreachable(); }
75 virtual uint32_t getImportedSymbolCount() const { unreachable(); }
76 virtual const Symbol
* getIndexedImportedSymbol(uint32_t index
) const { unreachable(); }
77 virtual ReferenceFlags
getImportedSymbolInfo(const Symbol
* sym
) const { unreachable(); }
78 virtual const char* getImportedSymbolName(const Symbol
* sym
) const { unreachable(); }
79 virtual const char* findClosestSymbol(const void* addr
, const void** closestAddr
) const { unreachable(); }
80 virtual bool isBundle() const { return false; }
81 virtual bool isDylib() const { return true; }
82 virtual bool isExecutable() const { unreachable(); }
83 virtual bool isPositionIndependentExecutable() const { unreachable(); }
84 virtual bool forceFlat() const { unreachable(); }
85 virtual uintptr_t doBindLazySymbol(uintptr_t* lazyPointer
, const LinkContext
& context
) { unreachable(); }
86 virtual uintptr_t doBindFastLazySymbol(uint32_t lazyBindingInfoOffset
, const LinkContext
& context
,
87 void (*lock
)(), void (*unlock
)()) { unreachable(); }
88 virtual void doTermination(const LinkContext
& context
) { unreachable(); }
89 virtual bool needsInitialization() { unreachable(); }
90 virtual bool getSectionContent(const char* segmentName
, const char* sectionName
, void** start
, size_t* length
) { unreachable(); }
91 virtual void getUnwindInfo(dyld_unwind_sections
* info
) { unreachable(); }
92 virtual bool findSection(const void* imageInterior
, const char** segmentName
, const char** sectionName
, size_t* sectionOffset
) { unreachable(); }
93 virtual bool isPrebindable() const { unreachable(); }
94 virtual bool usablePrebinding(const LinkContext
& context
) const { unreachable(); }
95 virtual void getRPaths(const LinkContext
& context
, std::vector
<const char*>&) const { }
96 virtual bool participatesInCoalescing() const { unreachable(); }
97 virtual bool getUUID(uuid_t
) const { unreachable(); }
98 virtual void dynamicInterpose(const LinkContext
& context
) { unreachable(); }
99 void addDynamicInterposingTuples(const struct dyld_interpose_tuple array
[], size_t count
) { unreachable(); }
100 virtual unsigned int segmentCount() const { unreachable(); }
101 virtual const char* segName(unsigned int) const { unreachable(); }
102 virtual uintptr_t segSize(unsigned int) const { unreachable(); }
103 virtual uintptr_t segFileSize(unsigned int) const { unreachable(); }
104 virtual bool segHasTrailingZeroFill(unsigned int) { unreachable(); }
105 virtual uintptr_t segFileOffset(unsigned int) const { unreachable(); }
106 virtual bool segReadable(unsigned int) const { unreachable(); }
107 virtual bool segWriteable(unsigned int) const { unreachable(); }
108 virtual bool segExecutable(unsigned int) const { unreachable(); }
109 virtual bool segUnaccessible(unsigned int) const { unreachable(); }
110 virtual bool segHasPreferredLoadAddress(unsigned int) const { unreachable(); }
111 virtual uintptr_t segPreferredLoadAddress(unsigned int) const { unreachable(); }
112 virtual uintptr_t segActualLoadAddress(unsigned int) const { unreachable(); }
113 virtual uintptr_t segActualEndAddress(unsigned int) const { unreachable(); }
116 // info from LC_VERSION_MIN_MACOSX or LC_VERSION_MIN_IPHONEOS
117 virtual uint32_t sdkVersion() const { unreachable(); }
118 virtual uint32_t minOSVersion() const { unreachable(); }
120 // if the image contains interposing functions, register them
121 virtual void registerInterposing() { unreachable(); }
123 virtual ImageLoader
* libImage(unsigned int) const { unreachable(); }
124 virtual bool libReExported(unsigned int) const { unreachable(); }
125 virtual bool libIsUpward(unsigned int) const { unreachable(); }
126 virtual void setLibImage(unsigned int, ImageLoader
*, bool, bool) { unreachable(); }
127 virtual const char* libPath(unsigned int) const { unreachable(); }
129 unsigned appendImagesToNotify(dyld_image_states state
, bool orLater
, dyld_image_info
* infos
);
130 const char* notify(dyld_image_states state
, bool orLater
, dyld_image_state_change_handler
);
131 bool dlopenFromCache(const LinkContext
& context
, const char* path
, int mode
, void** handle
);
132 bool makeCacheHandle(const LinkContext
& context
, unsigned cacheIndex
, int mode
, void** result
);
133 void* dlsymFromCache(const LinkContext
& context
, void* handle
, const char* symName
, unsigned index
);
134 bool isCacheHandle(void* handle
, unsigned* index
, uint8_t* flags
);
135 bool hasDylib(const char* path
, unsigned* index
) const;
136 bool addressInCache(const void* address
, const mach_header
** mh
, const char** path
, unsigned* index
);
137 bool findUnwindSections(const void* addr
, dyld_unwind_sections
* info
);
138 bool dladdrFromCache(const void* address
, Dl_info
* info
);
139 uintptr_t bindLazy(uintptr_t lazyBindingInfoOffset
, const LinkContext
& context
, const mach_header
* mh
, unsigned index
);
140 bool flatFindSymbol(const char* name
, bool onlyInCoalesced
, const ImageLoader::Symbol
** sym
, const ImageLoader
** image
);
141 void getDylibUUID(unsigned int index
, uuid_t
) const;
144 virtual void setDyldInfo(const dyld_info_command
* dyldInfo
) { unreachable(); }
145 virtual void setSymbolTableInfo(const macho_nlist
*, const char*, const dysymtab_command
*) { unreachable(); }
146 virtual uint32_t* segmentCommandOffsets() const { unreachable(); }
147 virtual void rebase(const LinkContext
& context
, uintptr_t slide
) { unreachable(); }
148 virtual uintptr_t exportedSymbolAddress(const LinkContext
& context
, const Symbol
* symbol
, const ImageLoader
* requestor
, bool runResolver
) const { unreachable(); }
149 virtual bool exportedSymbolIsWeakDefintion(const Symbol
* symbol
) const { unreachable(); }
150 virtual const char* exportedSymbolName(const Symbol
* symbol
) const { unreachable(); }
151 virtual unsigned int exportedSymbolCount() const { unreachable(); }
152 virtual const ImageLoader::Symbol
* exportedSymbolIndexed(unsigned int) const { unreachable(); }
153 virtual unsigned int importedSymbolCount() const { unreachable(); }
154 virtual const ImageLoader::Symbol
* importedSymbolIndexed(unsigned int) const { unreachable(); }
155 virtual const char* importedSymbolName(const Symbol
* symbol
) const { unreachable(); }
156 #if PREBOUND_IMAGE_SUPPORT
157 virtual void resetPreboundLazyPointers(const LinkContext
& context
) { unreachable(); }
160 virtual void recursiveLoadLibraries(const LinkContext
& context
, bool preflightOnly
, const RPathChain
& loaderRPaths
, const char* loadPath
);
161 virtual unsigned recursiveUpdateDepth(unsigned int maxDepth
);
162 virtual void recursiveRebase(const LinkContext
& context
) { }
163 virtual void recursiveBind(const LinkContext
& context
, bool forceLazysBound
, bool neverUnload
);
164 virtual void recursiveApplyInterposing(const LinkContext
& context
);
165 virtual void recursiveGetDOFSections(const LinkContext
& context
, std::vector
<DOFInfo
>& dofs
) { }
166 virtual void recursiveInitialization(const LinkContext
& context
, mach_port_t this_thread
, const char* pathToInitialize
,
167 ImageLoader::InitializerTimingList
&, ImageLoader::UninitedUpwards
&);
169 virtual void doGetDependentLibraries(DependentLibraryInfo libs
[]) { unreachable(); }
170 virtual LibraryInfo
doGetLibraryInfo(const LibraryInfo
& requestorInfo
) { return requestorInfo
; }
171 virtual void doRebase(const LinkContext
& context
) { unreachable(); }
172 virtual void doBind(const LinkContext
& context
, bool forceLazysBound
) { unreachable(); }
173 virtual void doBindJustLazies(const LinkContext
& context
) { unreachable(); }
174 virtual void doGetDOFSections(const LinkContext
& context
, std::vector
<DOFInfo
>& dofs
) { unreachable(); }
175 virtual void doInterpose(const LinkContext
& context
) { unreachable(); }
176 virtual bool doInitialization(const LinkContext
& context
) { unreachable(); }
177 virtual bool needsTermination() { unreachable(); }
178 virtual bool segmentsMustSlideTogether() const { unreachable(); }
179 virtual bool segmentsCanSlide() const { unreachable(); }
180 virtual void setSlide(intptr_t slide
) { unreachable(); }
181 bool allDependentLibrariesAsWhenPreBound() const { unreachable(); }
182 virtual bool isSubframeworkOf(const LinkContext
& context
, const ImageLoader
* image
) const { return false; }
183 virtual bool hasSubLibrary(const LinkContext
& context
, const ImageLoader
* child
) const { return false; }
184 virtual bool weakSymbolsBound(unsigned index
);
185 virtual void setWeakSymbolsBound(unsigned index
);
188 ImageLoaderMegaDylib(const dyld_cache_header
*, long slide
, const macho_header
* mainMH
, const LinkContext
&);
196 const macho_header
* getIndexedMachHeader(unsigned index
) const;
197 const uint8_t* getIndexedTrie(unsigned index
, uint32_t& trieSize
) const;
198 unsigned findImageIndex(const LinkContext
& context
, const char* path
) const;
199 void recursiveMarkLoaded(const LinkContext
& context
, unsigned imageIndex
);
200 void markAllbound(const LinkContext
& context
);
201 bool findInChainedTries(const LinkContext
& context
, const char* symbolName
, unsigned definedImageIndex
,
202 const ImageLoader
* requestorImage
, bool runResolver
, uintptr_t* address
) const;
203 bool findInChainedTriesAndDependents(const LinkContext
& context
, const char* symbolName
, unsigned definedImageIndex
,
204 const ImageLoader
* requestorImage
, bool runResolver
, uintptr_t* address
) const;
205 bool findInChainedTriesAndDependentsExcept(const LinkContext
& context
, const char* symbolName
, unsigned imageIndex
,
206 const ImageLoader
* requestorImage
, bool runResolver
, bool alreadyVisited
[], uintptr_t* address
) const;
207 bool exportTrieHasNodeRecursive(const char* symbolName
, unsigned index
,
208 const uint8_t** exportNode
, const uint8_t** exportTrieEnd
,
209 unsigned* foundinIndex
) const;
210 bool exportTrieHasNode(const char* symbolName
, unsigned index
,
211 const uint8_t** exportNode
, const uint8_t** exportTrieEnd
) const;
213 void initAllLoaded(const LinkContext
& context
, InitializerTimingList
& timingInfo
);
214 void printSegments(const macho_header
* mh
) const;
215 void* makeCacheHandle(unsigned index
, int mode
);
216 uint8_t flagsFromCacheHandle(void* handle
);
217 void processExportNode(const LinkContext
& context
, const char* symbolName
, unsigned definedImageIndex
,
218 const uint8_t* exportNode
, const uint8_t* exportTrieEnd
,
219 const ImageLoader
* requestorImage
, bool runResolver
, uintptr_t* address
) const;
220 static uint8_t dyldStateToCacheState(dyld_image_states state
);
221 void recursiveInitialization(const LinkContext
& context
, mach_port_t this_thread
, unsigned int imageIndex
,
222 InitializerTimingList
& timingInfo
, UpwardIndexes
&);
223 void recursiveSpinLockAcquire(unsigned int imageIndex
, mach_port_t thisThread
);
224 void recursiveSpinLockRelease(unsigned int imageIndex
, mach_port_t thisThread
);
226 __attribute__((noreturn
))
227 void unreachable() const;
229 enum { kStateUnused
=0, kStateLoaded
=1, kStateFlagBound
=2, kStateFlagWeakBound
=3, kStateFlagInitialized
=4 };
232 const dyld_cache_header
* _header
;
233 const void* _endOfCacheInMemory
;
234 const uint8_t* _linkEditBias
;
235 const dyld_cache_image_info
* _images
;
236 const dyld_cache_image_info_extra
* _imageExtras
;
238 const dyld_cache_accelerator_initializer
* _initializers
;
239 const dyld_cache_range_entry
* _rangeTable
;
240 const uint16_t* _reExportsArray
;
241 const uint16_t* _dependenciesArray
;
242 const uint16_t* _bottomUpArray
;
243 const uint8_t* _dylibsTrieStart
;
244 const uint8_t* _dylibsTrieEnd
;
245 const dyld_cache_image_text_info
* _imageTextInfo
;
246 uint8_t* _stateFlags
;
247 uint32_t _imageCount
;
248 uint32_t _initializerCount
;
249 uint32_t _rangeTableCount
;
250 pthread_mutex_t _lockArrayGuard
;
251 ImageLoader::recursive_lock
* _lockArray
;
252 unsigned int _lockArrayInUseCount
;
257 #endif // __IMAGELOADER_MEGADYLIB__