dyld-43.tar.gz
[apple/dyld.git] / src / ImageLoaderMachO.h
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004-2005 Apple Computer, 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 __IMAGELOADERMACHO__
27 #define __IMAGELOADERMACHO__
28
29 #include <stdint.h>
30
31 #include "ImageLoader.h"
32
33 struct sf_mapping;
34 struct _shared_region_mapping_np;
35
36 //
37 // ImageLoaderMachO is the concrete subclass of ImageLoader which loads mach-o format files.
38 // The class is written to be 64-bit clean and support both 32-bit and 64-bit executables in
39 // mach-o.
40 //
41 //
42 class ImageLoaderMachO : public ImageLoader {
43 public:
44 ImageLoaderMachO(const char* path, int fd, const uint8_t firstPage[4096], uint64_t offset, uint64_t len, const struct stat& info, const LinkContext& context);
45 ImageLoaderMachO(const char* moduleName, const struct mach_header* mh, uint64_t len, const LinkContext& context);
46 virtual ~ImageLoaderMachO() {}
47
48 const char* getInstallPath() const;
49 virtual void* getMain() const;
50 virtual const struct mach_header* machHeader() const;
51 virtual uintptr_t getSlide() const;
52 virtual const void* getBaseAddress() const;
53 virtual bool hasCoalescedExports() const;
54 virtual const Symbol* findExportedSymbol(const char* name, const void* hint, bool searchReExports, ImageLoader** foundIn) const;
55 virtual uintptr_t getExportedSymbolAddress(const Symbol* sym) const;
56 virtual DefinitionFlags getExportedSymbolInfo(const Symbol* sym) const;
57 virtual const char* getExportedSymbolName(const Symbol* sym) const;
58 virtual uint32_t getExportedSymbolCount() const;
59 virtual const Symbol* getIndexedExportedSymbol(uint32_t index) const;
60 virtual uint32_t getImportedSymbolCount() const;
61 virtual const Symbol* getIndexedImportedSymbol(uint32_t index) const;
62 virtual ReferenceFlags geImportedSymbolInfo(const Symbol* sym) const;
63 virtual const char* getImportedSymbolName(const Symbol* sym) const;
64 virtual bool isBundle() const;
65 virtual bool isDylib() const;
66 virtual bool forceFlat() const;
67 virtual uintptr_t doBindLazySymbol(uintptr_t* lazyPointer, const LinkContext& context);
68 virtual void doTermination(const LinkContext& context);
69 virtual void doNotification(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[]);
70 virtual bool needsInitialization();
71 virtual bool hasImageNotification();
72 virtual bool getSectionContent(const char* segmentName, const char* sectionName, void** start, size_t* length);
73 virtual bool findSection(const void* imageInterior, const char** segmentName, const char** sectionName, size_t* sectionOffset);
74 virtual bool usablePrebinding(const LinkContext& context) const;
75
76
77 static void printStatistics(unsigned int imageCount);
78
79 protected:
80 ImageLoaderMachO(const ImageLoaderMachO&);
81 void operator=(const ImageLoaderMachO&);
82
83 virtual uint32_t doGetDependentLibraryCount();
84 virtual void doGetDependentLibraries(DependentLibrary libs[]);
85 virtual LibraryInfo doGetLibraryInfo();
86 virtual void doRebase(const LinkContext& context);
87 virtual void doBind(const LinkContext& context, BindingLaziness bindness);
88 virtual void doInitialization(const LinkContext& context);
89 virtual void doPrebinding(const LinkContext& context, time_t timestamp, uint8_t* fileToPrebind);
90 virtual bool needsTermination();
91 virtual void instantiateSegments(const uint8_t* fileData);
92 virtual bool segmentsMustSlideTogether() const;
93 virtual bool segmentsCanSlide() const;
94 virtual void setSlide(intptr_t slide);
95 virtual bool usesTwoLevelNameSpace() const;
96 virtual bool isSubframeworkOf(const LinkContext& context, const ImageLoader* image) const;
97 virtual bool hasSubLibrary(const LinkContext& context, const ImageLoader* child) const;
98 virtual bool isPrebindable() const;
99
100 #if !__LP64__ // split segs not supported for 64-bits
101 virtual void mapSegments(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
102 #endif
103
104 private:
105 friend class SegmentMachO;
106
107 void init();
108 void parseLoadCmds();
109 void doBindIndirectSymbolPointers(const LinkContext& context, BindingLaziness bindness, bool onlyCoalescedSymbols);
110 void doBindExternalRelocations(const LinkContext& context, bool onlyCoalescedSymbols);
111 uintptr_t resolveUndefined(const LinkContext& context, const struct macho_nlist* symbol, bool twoLevel, ImageLoader **foundIn);
112 uintptr_t getRelocBase();
113 void doImageInit(const LinkContext& context);
114 void doModInitFunctions(const LinkContext& context);
115 void setupLazyPointerHandler();
116 void applyPrebindingToDATA(uint8_t* fileToPrebind);
117 void applyPrebindingToLoadCommands(const LinkContext& context, uint8_t* fileToPrebind, time_t timestamp);
118 void applyPrebindingToLinkEdit(const LinkContext& context, uint8_t* fileToPrebind);
119 #if !__LP64__ // split segs not supported for 64-bits
120 unsigned int getExtraZeroFillEntriesCount();
121 int sharedRegionLoadFile(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
122 int sharedRegionMapFile(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
123 int sharedRegionMakePrivate(const LinkContext& context);
124 int sharedRegionMapFilePrivate(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
125 int sharedRegionMapFilePrivateOutside(int fd, uint64_t offsetInFat, uint64_t lenInFat, uint64_t fileLen, const LinkContext& context);
126 void initMappingTable(uint64_t offsetInFat, sf_mapping *mappingTable, uintptr_t baseAddress);
127 void initMappingTable(uint64_t offsetInFat, _shared_region_mapping_np *mappingTable);
128 #endif
129 bool needsCoalescing() const;
130
131 static bool symbolRequiresCoalescing(const struct macho_nlist* symbol);
132 static uintptr_t bindLazySymbol(const mach_header*, uintptr_t* lazyPointer);
133 static const struct macho_nlist* binarySearch(const char* key, const char stringPool[], const struct macho_nlist symbols[], uint32_t symbolCount);
134 static const struct macho_nlist* binarySearchWithToc(const char* key, const char stringPool[], const struct macho_nlist symbols[],
135 const struct dylib_table_of_contents toc[], uint32_t symbolCount, uint32_t hintIndex);
136
137 const uint8_t* fMachOData;
138 const uint8_t* fLinkEditBase; // add any internal "offset" to this to get actual address
139 const struct macho_nlist* fSymbolTable;
140 const char* fStrings;
141 const struct dysymtab_command* fDynamicInfo;
142 uintptr_t fSlide;
143 bool fIsSplitSeg;
144 bool fHasSubLibraries;
145 bool fHasSubUmbrella;
146 bool fTextSegmentHasFixups;
147 const struct macho_routines_command* fDashInit;
148 const struct macho_section* fModInitSection;
149 const struct macho_section* fModTermSection;
150 const struct macho_section* fDATAdyld;
151 const struct macho_section* fImageNotifySection;
152 const struct twolevel_hints_command* fTwoLevelHints;
153 const struct dylib_command* fDylibID;
154 const char* fReExportThruFramework;
155 SegmentMachO* fTextSegmentWithFixups; // NULL unless __TEXT segment has fixups
156
157 static uint32_t fgHintedBinaryTreeSearchs;
158 static uint32_t fgUnhintedBinaryTreeSearchs;
159 };
160
161
162 class SegmentMachO : public Segment
163 {
164 public:
165 SegmentMachO(const struct macho_segment_command* cmd, ImageLoaderMachO*, const uint8_t* fileData);
166 virtual ~SegmentMachO();
167
168 virtual const ImageLoader* getImage();
169 virtual const char* getName();
170 virtual uintptr_t getSize();
171 virtual uintptr_t getFileSize();
172 virtual uintptr_t getFileOffset();
173 virtual bool readable();
174 virtual bool writeable();
175 virtual bool executable();
176 virtual bool unaccessible();
177 virtual bool hasFixUps();
178 virtual uintptr_t getActualLoadAddress();
179 virtual uintptr_t getPreferredLoadAddress();
180 virtual void setUnMapWhenDestructed(bool unmap);
181 virtual uint32_t crc32();
182
183 protected:
184 virtual bool hasPreferredLoadAddress();
185
186 private:
187 SegmentMachO(const SegmentMachO&);
188 void operator=(const SegmentMachO&);
189
190 friend class ImageLoaderMachO;
191
192
193 ImageLoaderMachO* const fImage;
194 char fName[18];
195 const uintptr_t fSize;
196 const uintptr_t fFileSize;
197 const uintptr_t fFileOffset;
198 const uintptr_t fPreferredLoadAddress;
199 const uint16_t fVMProtection;
200 bool fHasFixUps;
201 bool fUnMapOnDestruction;
202 };
203
204
205 #endif // __IMAGELOADERMACHO__
206
207
208
209