dyld-851.27.tar.gz
[apple/dyld.git] / src / ImageLoaderMachOClassic.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_CLASSIC__
27 #define __IMAGELOADER_MACHO_CLASSIC__
28
29 #include <stdint.h>
30
31 #include "ImageLoaderMachO.h"
32
33
34 //
35 // ImageLoaderMachOClassic is the concrete subclass of ImageLoader which loads mach-o files
36 // that use the traditional LINKEDIT format.
37 //
38 class ImageLoaderMachOClassic : public ImageLoaderMachO {
39 public:
40 static ImageLoaderMachOClassic* instantiateMainExecutable(const macho_header* mh, uintptr_t slide, const char* path,
41 unsigned int segCount, unsigned int libCount, const LinkContext& context);
42 static ImageLoaderMachOClassic* 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, const LinkContext& context);
46 static ImageLoaderMachOClassic* 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 ImageLoaderMachOClassic* instantiateFromMemory(const char* moduleName, const macho_header* mh, uint64_t len,
49 unsigned int segCount, unsigned int libCount, const LinkContext& context);
50
51 virtual ~ImageLoaderMachOClassic();
52
53 virtual ImageLoader* libImage(unsigned int) const;
54 virtual bool libReExported(unsigned int) const;
55 virtual bool libIsUpward(unsigned int) const;
56 virtual void setLibImage(unsigned int, ImageLoader*, bool, bool);
57 virtual void doBind(const LinkContext& context, bool forceLazysBound, const ImageLoader* reExportParent);
58 virtual void doBindJustLazies(const LinkContext& context, DyldSharedCache::DataConstLazyScopedWriter& patcher);
59 virtual uintptr_t doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context,
60 DyldSharedCache::DataConstLazyScopedWriter& patcher);
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, unsigned);
64 virtual bool incrementCoalIterator(CoalIterator&);
65 virtual uintptr_t getAddressCoalIterator(CoalIterator&, const LinkContext& contex);
66 virtual void updateUsesCoalIterator(CoalIterator&, uintptr_t newAddr, ImageLoader* target, unsigned targetIndex, const LinkContext& context);
67
68 protected:
69 virtual void doInterpose(const LinkContext& context);
70 virtual void dynamicInterpose(const LinkContext& context);
71 virtual void setDyldInfo(const dyld_info_command*) {}
72 virtual void setChainedFixups(const linkedit_data_command*) {}
73 virtual void setExportsTrie(const linkedit_data_command*) {}
74 virtual void setSymbolTableInfo(const macho_nlist*, const char*, const dysymtab_command*);
75 virtual bool isSubframeworkOf(const LinkContext& context, const ImageLoader* image) const;
76 virtual bool hasSubLibrary(const LinkContext& context, const ImageLoader* child) const;
77 virtual uint32_t* segmentCommandOffsets() const;
78 virtual void rebase(const LinkContext& context, uintptr_t slide);
79 virtual const ImageLoader::Symbol* findShallowExportedSymbol(const char* name, const ImageLoader** foundIn) const;
80 virtual bool containsSymbol(const void* addr) const;
81 virtual uintptr_t exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, const ImageLoader* requestor, bool runResolver) const;
82 virtual bool exportedSymbolIsWeakDefintion(const Symbol* symbol) const;
83 virtual const char* exportedSymbolName(const Symbol* symbol) const;
84 virtual unsigned int exportedSymbolCount() const;
85 virtual const ImageLoader::Symbol* exportedSymbolIndexed(unsigned int) const;
86 virtual unsigned int importedSymbolCount() const;
87 virtual const ImageLoader::Symbol* importedSymbolIndexed(unsigned int) const;
88 virtual const char* importedSymbolName(const Symbol* symbol) const;
89 #if PREBOUND_IMAGE_SUPPORT
90 virtual void resetPreboundLazyPointers(const LinkContext& context);
91 #endif
92
93
94 private:
95 ImageLoaderMachOClassic(const macho_header* mh, const char* path, unsigned int segCount,
96 uint32_t segOffsets[], unsigned int libCount);
97 static ImageLoaderMachOClassic* instantiateStart(const macho_header* mh, const char* path, unsigned int segCount, unsigned int libCount);
98 void instantiateFinish(const LinkContext& context);
99 uintptr_t getRelocBase();
100 void mapSegmentsClassic(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
101 uintptr_t getFirstWritableSegmentAddress();
102 const struct macho_nlist* binarySearch(const char* key, const char stringPool[], const struct macho_nlist symbols[], uint32_t symbolCount) const;
103 const struct macho_nlist* binarySearchWithToc(const char* key, const char stringPool[], const struct macho_nlist symbols[],
104 const struct dylib_table_of_contents toc[], uint32_t symbolCount, uint32_t hintIndex) const;
105 static bool symbolIsWeakReference(const struct macho_nlist* symbol);
106 static bool symbolIsWeakDefinition(const struct macho_nlist* symbol);
107 uintptr_t resolveUndefined(const LinkContext& context, const struct macho_nlist* symbol, bool twoLevel,
108 bool dontCoalesce, bool runResolver, const ImageLoader **foundIn);
109 uintptr_t getSymbolAddress(const macho_nlist*, const LinkContext& context, bool runResolver) const;
110 bool isAddrInSection(uintptr_t addr, uint8_t sectionIndex);
111 void doBindExternalRelocations(const LinkContext& context);
112 uintptr_t bindIndirectSymbol(uintptr_t* ptrToBind, const struct macho_section* sect,
113 const char* symbolName, uintptr_t targetAddr,
114 const ImageLoader* targetImage, const LinkContext& context);
115 void bindIndirectSymbolPointers(const LinkContext& context, bool bindNonLazys, bool bindLazys);
116 void initializeLazyStubs(const LinkContext& context);
117 #if SPLIT_SEG_DYLIB_SUPPORT
118 unsigned int getExtraZeroFillEntriesCount();
119 void initMappingTable(uint64_t offsetInFat, shared_file_mapping_np *mappingTable);
120 #endif
121 int mapSplitSegDylibOutsideSharedRegion(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
122 #if SPLIT_SEG_SHARED_REGION_SUPPORT
123 int mapSplitSegDylibInfoSharedRegion(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
124 #endif
125
126
127 const char* fStrings;
128 const struct macho_nlist* fSymbolTable;
129 const struct dysymtab_command* fDynamicInfo;
130
131 };
132
133
134
135
136 #endif // __IMAGELOADER_MACHO_CLASSIC__
137
138
139
140