1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008 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_MACHO_COMPRESSED__
27 #define __IMAGELOADER_MACHO_COMPRESSED__
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
{
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
);
54 virtual ~ImageLoaderMachOCompressed();
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
);
61 virtual void doBindJustLazies(const LinkContext
& context
);
62 virtual uintptr_t doBindLazySymbol(uintptr_t* lazyPointer
, const LinkContext
& context
);
63 virtual uintptr_t doBindFastLazySymbol(uint32_t lazyBindingInfoOffset
, const LinkContext
& context
, void (*lock
)(), void (*unlock
)());
64 virtual const char* findClosestSymbol(const void* addr
, const void** closestAddr
) const;
65 virtual void initializeCoalIterator(CoalIterator
&, unsigned int loadOrder
);
66 virtual bool incrementCoalIterator(CoalIterator
&);
67 virtual uintptr_t getAddressCoalIterator(CoalIterator
&, const LinkContext
& contex
);
68 virtual void updateUsesCoalIterator(CoalIterator
&, uintptr_t newAddr
, ImageLoader
* target
, const LinkContext
& context
);
72 virtual void doInterpose(const LinkContext
& context
);
73 virtual void dynamicInterpose(const LinkContext
& context
);
74 virtual void setDyldInfo(const dyld_info_command
* dyldInfo
) { fDyldInfo
= dyldInfo
; }
75 virtual void setSymbolTableInfo(const macho_nlist
*, const char*, const dysymtab_command
*) {}
76 virtual bool isSubframeworkOf(const LinkContext
& context
, const ImageLoader
* image
) const { return false; }
77 virtual bool hasSubLibrary(const LinkContext
& context
, const ImageLoader
* child
) const { return false; }
78 virtual uint32_t* segmentCommandOffsets() const;
79 virtual void rebase(const LinkContext
& context
);
80 virtual const ImageLoader::Symbol
* findExportedSymbol(const char* name
, const ImageLoader
** foundIn
) const;
81 virtual bool containsSymbol(const void* addr
) const;
82 virtual uintptr_t exportedSymbolAddress(const LinkContext
& context
, const Symbol
* symbol
, const ImageLoader
* requestor
, bool runResolver
) const;
83 virtual bool exportedSymbolIsWeakDefintion(const Symbol
* symbol
) const;
84 virtual const char* exportedSymbolName(const Symbol
* symbol
) const;
85 virtual unsigned int exportedSymbolCount() const;
86 virtual const ImageLoader::Symbol
* exportedSymbolIndexed(unsigned int) const;
87 virtual unsigned int importedSymbolCount() const;
88 virtual const ImageLoader::Symbol
* importedSymbolIndexed(unsigned int) const;
89 virtual const char* importedSymbolName(const Symbol
* symbol
) const;
90 #if PREBOUND_IMAGE_SUPPORT
91 virtual void resetPreboundLazyPointers(const LinkContext
& context
);
96 struct LastLookup
{ long ordinal
; uint8_t flags
; const char* name
; uintptr_t result
; const ImageLoader
* foundIn
; };
99 typedef uintptr_t (ImageLoaderMachOCompressed::*bind_handler
)(const LinkContext
& context
, uintptr_t addr
, uint8_t type
,
100 const char* symbolName
, uint8_t symboFlags
, intptr_t addend
, long libraryOrdinal
,
101 const char* msg
, LastLookup
* last
, bool runResolver
);
103 void eachLazyBind(const LinkContext
& context
, bind_handler
);
104 void eachBind(const LinkContext
& context
, bind_handler
);
107 ImageLoaderMachOCompressed(const macho_header
* mh
, const char* path
, unsigned int segCount
,
108 uint32_t segOffsets
[], unsigned int libCount
);
109 static ImageLoaderMachOCompressed
* instantiateStart(const macho_header
* mh
, const char* path
, unsigned int segCount
, unsigned int libCount
);
110 void instantiateFinish(const LinkContext
& context
);
111 void markSequentialLINKEDIT(const LinkContext
& context
);
112 void markFreeLINKEDIT(const LinkContext
& context
);
113 void markLINKEDIT(const LinkContext
& context
, int advise
);
115 void rebaseAt(const LinkContext
& context
, uintptr_t addr
, uintptr_t slide
, uint8_t type
);
116 void throwBadRebaseAddress(uintptr_t address
, uintptr_t segmentEndAddress
, int segmentIndex
,
117 const uint8_t* startOpcodes
, const uint8_t* endOpcodes
, const uint8_t* pos
);
118 uintptr_t bindAt(const LinkContext
& context
, uintptr_t addr
, uint8_t type
, const char* symbolName
,
119 uint8_t symboFlags
, intptr_t addend
, long libraryOrdinal
, const char* msg
,
120 LastLookup
* last
, bool runResolver
=false);
121 void bindCompressed(const LinkContext
& context
);
122 void throwBadBindingAddress(uintptr_t address
, uintptr_t segmentEndAddress
, int segmentIndex
,
123 const uint8_t* startOpcodes
, const uint8_t* endOpcodes
, const uint8_t* pos
);
124 uintptr_t resolve(const LinkContext
& context
, const char* symbolName
,
125 uint8_t symboFlags
, long libraryOrdinal
, const ImageLoader
** targetImage
,
126 LastLookup
* last
= NULL
, bool runResolver
=false);
127 uintptr_t resolveFlat(const LinkContext
& context
, const char* symbolName
, bool weak_import
, bool runResolver
,
128 const ImageLoader
** foundIn
);
129 uintptr_t resolveCoalesced(const LinkContext
& context
, const char* symbolName
, const ImageLoader
** foundIn
);
130 uintptr_t resolveTwolevel(const LinkContext
& context
, const ImageLoader
* targetImage
, bool weak_import
,
131 const char* symbolName
, bool runResolver
, const ImageLoader
** foundIn
);
132 uintptr_t interposeAt(const LinkContext
& context
, uintptr_t addr
, uint8_t type
, const char*,
133 uint8_t, intptr_t, long, const char*, LastLookup
*, bool runResolver
);
134 uintptr_t dynamicInterposeAt(const LinkContext
& context
, uintptr_t addr
, uint8_t type
, const char*,
135 uint8_t, intptr_t, long, const char*, LastLookup
*, bool runResolver
);
136 static const uint8_t* trieWalk(const uint8_t* start
, const uint8_t* end
, const char* s
);
137 void updateOptimizedLazyPointers(const LinkContext
& context
);
138 void updateAlternateLazyPointer(uint8_t* stub
, void** originalLazyPointerAddr
, const LinkContext
& context
);
139 void registerEncryption(const struct encryption_info_command
* encryptCmd
, const LinkContext
& context
);
141 const struct dyld_info_command
* fDyldInfo
;
146 #endif // __IMAGELOADER_MACHO_COMPRESSED__