dyld-832.7.1.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);
59 virtual uintptr_t doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context);
60 virtual uintptr_t doBindFastLazySymbol(uint32_t lazyBindingInfoOffset, const LinkContext& context, void (*lock)(), void (*unlock)());
61 virtual const char* findClosestSymbol(const void* addr, const void** closestAddr) const;
62 virtual void initializeCoalIterator(CoalIterator&, unsigned int loadOrder, unsigned);
63 virtual bool incrementCoalIterator(CoalIterator&);
64 virtual uintptr_t getAddressCoalIterator(CoalIterator&, const LinkContext& contex);
65 virtual void updateUsesCoalIterator(CoalIterator&, uintptr_t newAddr, ImageLoader* target, unsigned targetIndex, const LinkContext& context);
66
67 protected:
68 virtual void doInterpose(const LinkContext& context);
69 virtual void dynamicInterpose(const LinkContext& context);
70 virtual void setDyldInfo(const dyld_info_command*) {}
71 virtual void setChainedFixups(const linkedit_data_command*) {}
72 virtual void setExportsTrie(const linkedit_data_command*) {}
73 virtual void setSymbolTableInfo(const macho_nlist*, const char*, const dysymtab_command*);
74 virtual bool isSubframeworkOf(const LinkContext& context, const ImageLoader* image) const;
75 virtual bool hasSubLibrary(const LinkContext& context, const ImageLoader* child) const;
76 virtual uint32_t* segmentCommandOffsets() const;
77 virtual void rebase(const LinkContext& context, uintptr_t slide);
78 virtual const ImageLoader::Symbol* findShallowExportedSymbol(const char* name, const ImageLoader** foundIn) const;
79 virtual bool containsSymbol(const void* addr) const;
80 virtual uintptr_t exportedSymbolAddress(const LinkContext& context, const Symbol* symbol, const ImageLoader* requestor, bool runResolver) const;
81 virtual bool exportedSymbolIsWeakDefintion(const Symbol* symbol) const;
82 virtual const char* exportedSymbolName(const Symbol* symbol) const;
83 virtual unsigned int exportedSymbolCount() const;
84 virtual const ImageLoader::Symbol* exportedSymbolIndexed(unsigned int) const;
85 virtual unsigned int importedSymbolCount() const;
86 virtual const ImageLoader::Symbol* importedSymbolIndexed(unsigned int) const;
87 virtual const char* importedSymbolName(const Symbol* symbol) const;
88 #if PREBOUND_IMAGE_SUPPORT
89 virtual void resetPreboundLazyPointers(const LinkContext& context);
90 #endif
91
92
93 private:
94 ImageLoaderMachOClassic(const macho_header* mh, const char* path, unsigned int segCount,
95 uint32_t segOffsets[], unsigned int libCount);
96 static ImageLoaderMachOClassic* instantiateStart(const macho_header* mh, const char* path, unsigned int segCount, unsigned int libCount);
97 void instantiateFinish(const LinkContext& context);
98 uintptr_t getRelocBase();
99 void mapSegmentsClassic(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
100 uintptr_t getFirstWritableSegmentAddress();
101 const struct macho_nlist* binarySearch(const char* key, const char stringPool[], const struct macho_nlist symbols[], uint32_t symbolCount) const;
102 const struct macho_nlist* binarySearchWithToc(const char* key, const char stringPool[], const struct macho_nlist symbols[],
103 const struct dylib_table_of_contents toc[], uint32_t symbolCount, uint32_t hintIndex) const;
104 static bool symbolIsWeakReference(const struct macho_nlist* symbol);
105 static bool symbolIsWeakDefinition(const struct macho_nlist* symbol);
106 uintptr_t resolveUndefined(const LinkContext& context, const struct macho_nlist* symbol, bool twoLevel,
107 bool dontCoalesce, bool runResolver, const ImageLoader **foundIn);
108 uintptr_t getSymbolAddress(const macho_nlist*, const LinkContext& context, bool runResolver) const;
109 bool isAddrInSection(uintptr_t addr, uint8_t sectionIndex);
110 void doBindExternalRelocations(const LinkContext& context);
111 uintptr_t bindIndirectSymbol(uintptr_t* ptrToBind, const struct macho_section* sect,
112 const char* symbolName, uintptr_t targetAddr,
113 const ImageLoader* targetImage, const LinkContext& context);
114 void bindIndirectSymbolPointers(const LinkContext& context, bool bindNonLazys, bool bindLazys);
115 void initializeLazyStubs(const LinkContext& context);
116 #if SPLIT_SEG_DYLIB_SUPPORT
117 unsigned int getExtraZeroFillEntriesCount();
118 void initMappingTable(uint64_t offsetInFat, shared_file_mapping_np *mappingTable);
119 #endif
120 int mapSplitSegDylibOutsideSharedRegion(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
121 #if SPLIT_SEG_SHARED_REGION_SUPPORT
122 int mapSplitSegDylibInfoSharedRegion(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
123 #endif
124
125
126 const char* fStrings;
127 const struct macho_nlist* fSymbolTable;
128 const struct dysymtab_command* fDynamicInfo;
129
130 };
131
132
133
134
135 #endif // __IMAGELOADER_MACHO_CLASSIC__
136
137
138
139