1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2004-2005 Apple Computer, 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 __IMAGELOADERMACHO__
27 #define __IMAGELOADERMACHO__
31 #include "ImageLoader.h"
34 struct _shared_region_mapping_np
;
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
42 class ImageLoaderMachO
: public ImageLoader
{
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();
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;
77 static void printStatistics(unsigned int imageCount
);
80 ImageLoaderMachO(const ImageLoaderMachO
&);
81 void operator=(const ImageLoaderMachO
&);
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 virtual void prebindUnmap(const LinkContext
& context
);
101 #if !__LP64__ // split segs not supported for 64-bits
102 virtual void mapSegments(int fd
, uint64_t offsetInFat
, uint64_t lenInFat
, uint64_t fileLen
, const LinkContext
& context
);
106 friend class SegmentMachO
;
109 void parseLoadCmds();
110 uintptr_t bindIndirectSymbol(uintptr_t* ptrToBind
, const struct macho_section
* sect
, const char* symbolName
, uintptr_t targetAddr
, ImageLoader
* targetImage
, const LinkContext
& context
);
111 void doBindIndirectSymbolPointers(const LinkContext
& context
, BindingLaziness bindness
, bool onlyCoalescedSymbols
);
112 void doBindExternalRelocations(const LinkContext
& context
, bool onlyCoalescedSymbols
);
113 uintptr_t resolveUndefined(const LinkContext
& context
, const struct macho_nlist
* symbol
, bool twoLevel
, ImageLoader
**foundIn
);
114 uintptr_t getRelocBase();
115 uintptr_t getFirstWritableSegmentAddress();
116 void resetPreboundLazyPointers(const LinkContext
& context
, uintptr_t relocBase
);
117 void doImageInit(const LinkContext
& context
);
118 void doModInitFunctions(const LinkContext
& context
);
119 void setupLazyPointerHandler(const LinkContext
& context
);
120 void applyPrebindingToDATA(uint8_t* fileToPrebind
);
121 void applyPrebindingToLoadCommands(const LinkContext
& context
, uint8_t* fileToPrebind
, time_t timestamp
);
122 void applyPrebindingToLinkEdit(const LinkContext
& context
, uint8_t* fileToPrebind
);
123 #if !__LP64__ // split segs not supported for 64-bits
124 unsigned int getExtraZeroFillEntriesCount();
125 int sharedRegionLoadFile(int fd
, uint64_t offsetInFat
, uint64_t lenInFat
, uint64_t fileLen
, const LinkContext
& context
);
126 int sharedRegionMapFile(int fd
, uint64_t offsetInFat
, uint64_t lenInFat
, uint64_t fileLen
, const LinkContext
& context
);
127 int sharedRegionMakePrivate(const LinkContext
& context
);
128 int sharedRegionMapFilePrivate(int fd
, uint64_t offsetInFat
, uint64_t lenInFat
, uint64_t fileLen
, const LinkContext
& context
, bool usemmap
);
129 int sharedRegionMapFilePrivateOutside(int fd
, uint64_t offsetInFat
, uint64_t lenInFat
, uint64_t fileLen
, const LinkContext
& context
);
130 void initMappingTable(uint64_t offsetInFat
, sf_mapping
*mappingTable
, uintptr_t baseAddress
);
131 void initMappingTable(uint64_t offsetInFat
, _shared_region_mapping_np
*mappingTable
);
133 bool needsCoalescing() const;
134 bool isAddrInSection(uintptr_t addr
, uint8_t sectionIndex
);
136 static bool symbolRequiresCoalescing(const struct macho_nlist
* symbol
);
137 static uintptr_t bindLazySymbol(const mach_header
*, uintptr_t* lazyPointer
);
138 static const struct macho_nlist
* binarySearch(const char* key
, const char stringPool
[], const struct macho_nlist symbols
[], uint32_t symbolCount
);
139 static const struct macho_nlist
* binarySearchWithToc(const char* key
, const char stringPool
[], const struct macho_nlist symbols
[],
140 const struct dylib_table_of_contents toc
[], uint32_t symbolCount
, uint32_t hintIndex
);
142 const uint8_t* fMachOData
;
143 const uint8_t* fLinkEditBase
; // add any internal "offset" to this to get actual address
144 const struct macho_nlist
* fSymbolTable
;
145 const char* fStrings
;
146 const struct dysymtab_command
* fDynamicInfo
;
149 bool fHasSubLibraries
;
150 bool fHasSubUmbrella
;
151 bool fTextSegmentHasFixups
;
152 const struct macho_routines_command
* fDashInit
;
153 const struct macho_section
* fModInitSection
;
154 const struct macho_section
* fModTermSection
;
155 const struct macho_section
* fDATAdyld
;
156 const struct macho_section
* fImageNotifySection
;
157 const struct twolevel_hints_command
* fTwoLevelHints
;
158 const struct dylib_command
* fDylibID
;
159 const char* fReExportThruFramework
;
160 class SegmentMachO
* fTextSegmentWithFixups
; // NULL unless __TEXT segment has fixups
162 static uint32_t fgHintedBinaryTreeSearchs
;
163 static uint32_t fgUnhintedBinaryTreeSearchs
;
164 static uint32_t fgCountOfImagesWithWeakExports
;
168 class SegmentMachO
: public Segment
171 SegmentMachO(const struct macho_segment_command
* cmd
, ImageLoaderMachO
*, const uint8_t* fileData
);
172 virtual ~SegmentMachO();
174 virtual const ImageLoader
* getImage();
175 virtual const char* getName();
176 virtual uintptr_t getSize();
177 virtual uintptr_t getFileSize();
178 virtual uintptr_t getFileOffset();
179 virtual bool readable();
180 virtual bool writeable();
181 virtual bool executable();
182 virtual bool unaccessible();
183 virtual bool hasFixUps();
184 virtual uintptr_t getActualLoadAddress();
185 virtual uintptr_t getPreferredLoadAddress();
186 virtual void setUnMapWhenDestructed(bool unmap
);
187 virtual uint32_t crc32();
190 virtual bool hasPreferredLoadAddress();
193 SegmentMachO(const SegmentMachO
&);
194 void operator=(const SegmentMachO
&);
196 friend class ImageLoaderMachO
;
199 ImageLoaderMachO
* const fImage
;
201 const uintptr_t fSize
;
202 const uintptr_t fFileSize
;
203 const uintptr_t fFileOffset
;
204 const uintptr_t fPreferredLoadAddress
;
205 const uint16_t fVMProtection
;
207 bool fUnMapOnDestruction
;
211 #endif // __IMAGELOADERMACHO__