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