195-6
[darwin-xtools.git] / dyld / src / ImageLoaderMachOCompressed.h
blob99c55294d0be209d058374f6070aa12e88b11ce1
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008 Apple Inc. All rights reserved.
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.
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_MACHO_COMPRESSED__
27 #define __IMAGELOADER_MACHO_COMPRESSED__
29 #include <stdint.h>
31 #include "ImageLoaderMachO.h"
35 // ImageLoaderMachOCompressed is the concrete subclass of ImageLoader which loads mach-o files
36 // that use the compressed LINKEDIT format.
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,
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, const LinkContext& context);
46 static ImageLoaderMachOCompressed* instantiateFromCache(const macho_header* mh, const char* path, long slide, const struct stat& info,
47 unsigned int segCount, unsigned int libCount, const LinkContext& context);
48 static ImageLoaderMachOCompressed* instantiateFromMemory(const char* moduleName, const macho_header* mh, uint64_t len,
49 unsigned int segCount, unsigned int libCount, const LinkContext& context);
52 virtual ~ImageLoaderMachOCompressed();
54 virtual ImageLoader* libImage(unsigned int) const;
55 virtual bool libReExported(unsigned int) const;
56 virtual bool libIsUpward(unsigned int) const;
57 virtual void setLibImage(unsigned int, ImageLoader*, bool, bool);
58 virtual void doBind(const LinkContext& context, bool forceLazysBound);
59 virtual void doBindJustLazies(const LinkContext& context);
60 virtual uintptr_t doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context);
61 virtual uintptr_t doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context, void (*lock)(), void (*unlock)());
62 virtual const char* findClosestSymbol(const void* addr, const void** closestAddr) const;
63 virtual void initializeCoalIterator(CoalIterator&, unsigned int loadOrder);
64 virtual bool incrementCoalIterator(CoalIterator&);
65 virtual uintptr_t getAddressCoalIterator(CoalIterator&, const LinkContext& contex);
66 virtual void updateUsesCoalIterator(CoalIterator&, uintptr_t newAddr, ImageLoader* target, const LinkContext& context);
69 protected:
70 virtual void doInterpose(const LinkContext& context);
71 virtual void setDyldInfo(const dyld_info_command* dyldInfo) { fDyldInfo = dyldInfo; }
72 virtual void setSymbolTableInfo(const macho_nlist*, const char*, const dysymtab_command*) {}
73 virtual bool isSubframeworkOf(const LinkContext& context, const ImageLoader* image) const { return false; }
74 virtual bool hasSubLibrary(const LinkContext& context, const ImageLoader* child) const { return false; }
75 virtual uint32_t* segmentCommandOffsets() const;
76 virtual void rebase(const LinkContext& context);
77 virtual const ImageLoader::Symbol* findExportedSymbol(const char* name, const ImageLoader** foundIn) const;
78 virtual bool containsSymbol(const void* addr) const;
79 virtual uintptr_t exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, bool runResolver) const;
80 virtual bool exportedSymbolIsWeakDefintion(const Symbol* symbol) const;
81 virtual const char* exportedSymbolName(const Symbol* symbol) const;
82 virtual unsigned int exportedSymbolCount() const;
83 virtual const ImageLoader::Symbol* exportedSymbolIndexed(unsigned int) const;
84 virtual unsigned int importedSymbolCount() const;
85 virtual const ImageLoader::Symbol* importedSymbolIndexed(unsigned int) const;
86 virtual const char* importedSymbolName(const Symbol* symbol) const;
87 #if PREBOUND_IMAGE_SUPPORT
88 virtual void resetPreboundLazyPointers(const LinkContext& context);
89 #endif
92 private:
93 struct LastLookup { int ordinal; uint8_t flags; const char* name; uintptr_t result; const ImageLoader* foundIn; };
96 typedef uintptr_t (ImageLoaderMachOCompressed::*bind_handler)(const LinkContext& context, uintptr_t addr, uint8_t type,
97 const char* symbolName, uint8_t symboFlags, intptr_t addend, int libraryOrdinal,
98 const char* msg, LastLookup* last, bool runResolver);
100 void eachLazyBind(const LinkContext& context, bind_handler);
101 void eachBind(const LinkContext& context, bind_handler);
104 ImageLoaderMachOCompressed(const macho_header* mh, const char* path, unsigned int segCount,
105 uint32_t segOffsets[], unsigned int libCount);
106 static ImageLoaderMachOCompressed* instantiateStart(const macho_header* mh, const char* path, unsigned int segCount, unsigned int libCount);
107 void instantiateFinish(const LinkContext& context);
108 void markSequentialLINKEDIT(const LinkContext& context);
109 void markFreeLINKEDIT(const LinkContext& context);
110 void markLINKEDIT(const LinkContext& context, int advise);
112 void rebaseAt(const LinkContext& context, uintptr_t addr, uintptr_t slide, uint8_t type);
113 void throwBadRebaseAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex,
114 const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos);
115 uintptr_t bindAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char* symbolName,
116 uint8_t symboFlags, intptr_t addend, int libraryOrdinal, const char* msg,
117 LastLookup* last, bool runResolver=false);
118 void bindCompressed(const LinkContext& context);
119 void throwBadBindingAddress(uintptr_t address, uintptr_t segmentEndAddress, int segmentIndex,
120 const uint8_t* startOpcodes, const uint8_t* endOpcodes, const uint8_t* pos);
121 uintptr_t resolve(const LinkContext& context, const char* symbolName,
122 uint8_t symboFlags, int libraryOrdinal, const ImageLoader** targetImage,
123 LastLookup* last = NULL, bool runResolver=false);
124 uintptr_t resolveFlat(const LinkContext& context, const char* symbolName, bool weak_import, bool runResolver,
125 const ImageLoader** foundIn);
126 uintptr_t resolveCoalesced(const LinkContext& context, const char* symbolName, const ImageLoader** foundIn);
127 uintptr_t resolveTwolevel(const LinkContext& context, const ImageLoader* targetImage, bool weak_import,
128 const char* symbolName, bool runResolver, const ImageLoader** foundIn);
129 uintptr_t interposeAt(const LinkContext& context, uintptr_t addr, uint8_t type, const char*,
130 uint8_t, intptr_t, int, const char*, LastLookup*, bool runResolver);
131 static const uint8_t* trieWalk(const uint8_t* start, const uint8_t* end, const char* s);
132 void updateOptimizedLazyPointers(const LinkContext& context);
133 void updateAlternateLazyPointer(uint8_t* stub, void** originalLazyPointerAddr);
135 const struct dyld_info_command* fDyldInfo;
140 #endif // __IMAGELOADER_MACHO_COMPRESSED__