dyld-851.27.tar.gz
[apple/dyld.git] / src / ImageLoaderMachOCompressed.h
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2008 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
25
26 #ifndef __IMAGELOADER_MACHO_COMPRESSED__
27 #define __IMAGELOADER_MACHO_COMPRESSED__
28
29 #include <stdint.h>
30
31 #include "ImageLoaderMachO.h"
32
33
34 //
35 // ImageLoaderMachOCompressed is the concrete subclass of ImageLoader which loads mach-o files
36 // that use the compressed LINKEDIT format.
37 //
38 class ImageLoaderMachOCompressed : public ImageLoaderMachO {
39 public:
40 static ImageLoaderMachOCompressed* instantiateMainExecutable(const macho_header* mh, uintptr_t slide, const char* path,
41 unsigned int segCount, unsigned int libCount, const LinkContext& context);
42 static ImageLoaderMachOCompressed* instantiateFromFile(const char* path, int fd, const uint8_t *fileData, size_t lenFileData,
43 uint64_t offsetInFat, uint64_t lenInFat, const struct stat& info,
44 unsigned int segCount, unsigned int libCount,
45 const struct linkedit_data_command* codeSigCmd,
46 const struct encryption_info_command* encryptCmd,
47 const LinkContext& context);
48 static ImageLoaderMachOCompressed* instantiateFromCache(const macho_header* mh, const char* path, long slide, const struct stat& info,
49 unsigned int segCount, unsigned int libCount, const LinkContext& context);
50 static ImageLoaderMachOCompressed* instantiateFromMemory(const char* moduleName, const macho_header* mh, uint64_t len,
51 unsigned int segCount, unsigned int libCount, const LinkContext& context);
52
53
54 virtual ~ImageLoaderMachOCompressed();
55
56 virtual ImageLoader* libImage(unsigned int) const;
57 virtual bool libReExported(unsigned int) const;
58 virtual bool libIsUpward(unsigned int) const;
59 virtual void setLibImage(unsigned int, ImageLoader*, bool, bool);
60 virtual void doBind(const LinkContext& context, bool forceLazysBound, const ImageLoader* reExportParent);
61 virtual void doBindJustLazies(const LinkContext& context, DyldSharedCache::DataConstLazyScopedWriter& patcher);
62 virtual uintptr_t doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context,
63 DyldSharedCache::DataConstLazyScopedWriter& patcher);
64 virtual uintptr_t doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context, void (*lock)(), void (*unlock)());
65 virtual const char* findClosestSymbol(const void* addr, const void** closestAddr) const;
66 virtual void initializeCoalIterator(CoalIterator&, unsigned int loadOrder, unsigned);
67 virtual bool incrementCoalIterator(CoalIterator&);
68 virtual uintptr_t getAddressCoalIterator(CoalIterator&, const LinkContext& contex);
69 virtual void updateUsesCoalIterator(CoalIterator&, uintptr_t newAddr, ImageLoader* target, unsigned targetIndex, const LinkContext& context);
70 virtual void registerInterposing(const LinkContext& context);
71 virtual bool usesChainedFixups() const;
72 virtual void makeDataReadOnly() const;
73
74 protected:
75 virtual void doInterpose(const LinkContext& context);
76 virtual void dynamicInterpose(const LinkContext& context);
77 virtual void setDyldInfo(const dyld_info_command* dyldInfo) { fDyldInfo = dyldInfo; }
78 virtual void setChainedFixups(const linkedit_data_command* fixups) { fChainedFixups = fixups; }
79 virtual void setExportsTrie(const linkedit_data_command* trie) { fExportsTrie = trie; }
80 virtual void setSymbolTableInfo(const macho_nlist*, const char*, const dysymtab_command*) {}
81 virtual bool isSubframeworkOf(const LinkContext& context, const ImageLoader* image) const { return false; }
82 virtual bool hasSubLibrary(const LinkContext& context, const ImageLoader* child) const { return false; }
83 virtual uint32_t* segmentCommandOffsets() const;
84 virtual void rebase(const LinkContext& context, uintptr_t slide);
85 virtual const ImageLoader::Symbol* findShallowExportedSymbol(const char* name, const ImageLoader** foundIn) const;
86 virtual bool containsSymbol(const void* addr) const;
87 virtual uintptr_t exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, const ImageLoader* requestor, bool runResolver) const;
88 virtual bool exportedSymbolIsWeakDefintion(const Symbol* symbol) const;
89 virtual const char* exportedSymbolName(const Symbol* symbol) const;
90 virtual unsigned int exportedSymbolCount() const;
91 virtual const ImageLoader::Symbol* exportedSymbolIndexed(unsigned int) const;
92 virtual unsigned int importedSymbolCount() const;
93 virtual const ImageLoader::Symbol* importedSymbolIndexed(unsigned int) const;
94 virtual const char* importedSymbolName(const Symbol* symbol) const;
95 #if PREBOUND_IMAGE_SUPPORT
96 virtual void resetPreboundLazyPointers(const LinkContext& context);
97 #endif
98 virtual uintptr_t resolveWeak(const LinkContext& context, const char* symbolName, bool weak_import, bool runResolver,
99 const ImageLoader** foundIn, DyldSharedCache::DataConstLazyScopedWriter& patcher);
100
101
102 private:
103 struct LastLookup { long ordinal; uint8_t flags; const char* name; uintptr_t result; const ImageLoader* foundIn; };
104
105
106 typedef uintptr_t (^bind_handler)(const LinkContext& context, ImageLoaderMachOCompressed* image, uintptr_t addr, uint8_t type,
107 const char* symbolName, uint8_t symboFlags, intptr_t addend, long libraryOrdinal,
108 ExtraBindData *extraBindData,
109 const char* msg, LastLookup* last, bool runResolver);
110
111 void eachLazyBind(const LinkContext& context, bind_handler);
112 void eachBind(const LinkContext& context, bind_handler);
113
114
115 ImageLoaderMachOCompressed(const macho_header* mh, const char* path, unsigned int segCount,
116 uint32_t segOffsets[], unsigned int libCount);
117 static ImageLoaderMachOCompressed* instantiateStart(const macho_header* mh, const char* path, unsigned int segCount, unsigned int libCount);
118 void instantiateFinish(const LinkContext& context);
119
120 void rebaseAt(const LinkContext& context, uintptr_t addr, uintptr_t slide, uint8_t type);
121 void throwBadRebaseAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex,
122 const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos);
123 static uintptr_t bindAt(const LinkContext& context, ImageLoaderMachOCompressed* image, uintptr_t addr, uint8_t type, const char* symbolName,
124 uint8_t symboFlags, intptr_t addend, long libraryOrdinal,
125 ExtraBindData *extraBindData,
126 const char* msg,
127 DyldSharedCache::DataConstLazyScopedWriter& patcher,
128 LastLookup* last, bool runResolver=false);
129 void bindCompressed(const LinkContext& context);
130 void throwBadBindingAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex,
131 const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos);
132 uintptr_t resolve(const LinkContext& context, const char* symbolName,
133 uint8_t symboFlags, long libraryOrdinal, const ImageLoader** targetImage,
134 DyldSharedCache::DataConstLazyScopedWriter& patcher,
135 LastLookup* last = NULL, bool runResolver=false);
136 uintptr_t resolveFlat(const LinkContext& context, const char* symbolName, bool weak_import, bool runResolver,
137 const ImageLoader** foundIn);
138 uintptr_t resolveCoalesced(const LinkContext& context, const char* symbolName, const ImageLoader** foundIn);
139 uintptr_t resolveTwolevel(const LinkContext& context, const char* symbolName, const ImageLoader* definedInImage,
140 const ImageLoader* requestorImage, unsigned requestorOrdinalOfDef, bool weak_import, bool runResolver,
141 const ImageLoader** foundInn);
142 static uintptr_t interposeAt(const LinkContext& context, ImageLoaderMachOCompressed* image, uintptr_t addr, uint8_t type, const char*,
143 uint8_t, intptr_t, long,
144 ExtraBindData *extraBindData,
145 const char*, LastLookup*, bool runResolver);
146 static uintptr_t dynamicInterposeAt(const LinkContext& context, ImageLoaderMachOCompressed* image, uintptr_t addr, uint8_t type, const char*,
147 uint8_t, intptr_t, long,
148 ExtraBindData *extraBindData,
149 const char*, LastLookup*, bool runResolver);
150 void updateOptimizedLazyPointers(const LinkContext& context);
151 void updateAlternateLazyPointer(uint8_t* stub, void** originalLazyPointerAddr, const LinkContext& context);
152 void registerEncryption(const struct encryption_info_command* encryptCmd, const LinkContext& context);
153 void doApplyFixups(const LinkContext& context, const dyld_chained_fixups_header* fixupsHeader,
154 DyldSharedCache::DataConstLazyScopedWriter& patcher);
155
156 const struct dyld_info_command* fDyldInfo;
157 const struct linkedit_data_command* fChainedFixups;
158 const struct linkedit_data_command* fExportsTrie;
159 };
160
161
162
163 #endif // __IMAGELOADER_MACHO_COMPRESSED__
164
165
166
167