2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #ifndef SharedCacheBuilder_h
26 #define SharedCacheBuilder_h
28 #include "CacheBuilder.h"
29 #include "DyldSharedCache.h"
30 #include "ClosureFileSystem.h"
32 class SharedCacheBuilder
: public CacheBuilder
{
34 SharedCacheBuilder(const DyldSharedCache::CreateOptions
& options
, const dyld3::closure::FileSystem
& fileSystem
);
36 void build(std::vector
<InputFile
>& inputFiles
,
37 std::vector
<DyldSharedCache::FileAlias
>& aliases
);
38 void build(const std::vector
<LoadedMachO
>& dylibs
,
39 const std::vector
<LoadedMachO
>& otherOsDylibsInput
,
40 const std::vector
<LoadedMachO
>& osExecutables
,
41 std::vector
<DyldSharedCache::FileAlias
>& aliases
);
42 void build(const std::vector
<DyldSharedCache::MappedMachO
>& dylibsToCache
,
43 const std::vector
<DyldSharedCache::MappedMachO
>& otherOsDylibs
,
44 const std::vector
<DyldSharedCache::MappedMachO
>& osExecutables
,
45 std::vector
<DyldSharedCache::FileAlias
>& aliases
);
47 void writeFile(const std::string
& path
);
48 void writeBuffer(uint8_t*& buffer
, uint64_t& size
);
49 void writeMapFile(const std::string
& path
);
50 std::string
getMapFileBuffer(const std::string
& cacheDisposition
) const;
52 const std::set
<std::string
> warnings();
53 const std::set
<const dyld3::MachOAnalyzer
*> evictions();
54 const bool agileSignature();
55 const std::string
cdHashFirst();
56 const std::string
cdHashSecond();
57 const std::string
uuid() const;
59 void forEachCacheDylib(void (^callback
)(const std::string
& path
));
63 void writeSlideInfoV1();
65 template <typename P
> void writeSlideInfoV2(const bool bitmap
[], unsigned dataPageCount
);
66 template <typename P
> bool makeRebaseChainV2(uint8_t* pageContent
, uint16_t lastLocationOffset
, uint16_t newOffset
, const struct dyld_cache_slide_info2
* info
);
67 template <typename P
> void addPageStartsV2(uint8_t* pageContent
, const bool bitmap
[], const struct dyld_cache_slide_info2
* info
,
68 std::vector
<uint16_t>& pageStarts
, std::vector
<uint16_t>& pageExtras
);
70 void writeSlideInfoV3(const bool bitmap
[], unsigned dataPageCoun
);
71 uint16_t pageStartV3(uint8_t* pageContent
, uint32_t pageSize
, const bool bitmap
[]);
72 void setPointerContentV3(dyld3::MachOLoaded::ChainedFixupPointerOnDisk
* loc
, uint64_t targetVMAddr
, size_t next
);
74 template <typename P
> void writeSlideInfoV4(const bool bitmap
[], unsigned dataPageCount
);
75 template <typename P
> bool makeRebaseChainV4(uint8_t* pageContent
, uint16_t lastLocationOffset
, uint16_t newOffset
, const struct dyld_cache_slide_info4
* info
);
76 template <typename P
> void addPageStartsV4(uint8_t* pageContent
, const bool bitmap
[], const struct dyld_cache_slide_info4
* info
,
77 std::vector
<uint16_t>& pageStarts
, std::vector
<uint16_t>& pageExtras
);
81 uint64_t sharedMemoryStart
;
82 uint64_t sharedMemorySize
;
83 uint64_t textAndDataMaxSize
;
84 uint64_t sharedRegionPadding
;
85 uint64_t pointerDeltaMask
;
88 uint8_t sharedRegionAlignP2
;
89 uint8_t slideInfoBytesPerPage
;
90 bool sharedRegionsAreDiscontiguous
;
95 static const ArchLayout _s_archLayout
[];
96 static const char* const _s_neverStubEliminateDylibs
[];
97 static const char* const _s_neverStubEliminateSymbols
[];
99 void makeSortedDylibs(const std::vector
<LoadedMachO
>& dylibs
, const std::unordered_map
<std::string
, unsigned> sortOrder
);
100 void processSelectorStrings(const std::vector
<LoadedMachO
>& executables
);
101 void parseCoalescableSegments();
102 void assignSegmentAddresses();
104 uint64_t cacheOverflowAmount();
105 size_t evictLeafDylibs(uint64_t reductionTarget
, std::vector
<const LoadedMachO
*>& overflowDylibs
);
109 uint64_t pathHash(const char* path
);
110 void writeCacheHeader();
111 void findDylibAndSegment(const void* contentPtr
, std::string
& dylibName
, std::string
& segName
);
112 void addImageArray();
113 void buildImageArray(std::vector
<DyldSharedCache::FileAlias
>& aliases
);
114 void addOtherImageArray(const std::vector
<LoadedMachO
>&, std::vector
<const LoadedMachO
*>& overflowDylibs
);
115 void addClosures(const std::vector
<LoadedMachO
>&);
116 void markPaddingInaccessible();
118 bool writeCache(void (^cacheSizeCallback
)(uint64_t size
), bool (^copyCallback
)(const uint8_t* src
, uint64_t size
, uint64_t dstOffset
));
120 // implemented in OptimizerObjC.cpp
122 uint32_t computeReadOnlyObjC(uint32_t selRefCount
, uint32_t classDefCount
, uint32_t protocolDefCount
);
123 uint32_t computeReadWriteObjC(uint32_t imageCount
, uint32_t protocolDefCount
);
125 // implemented in OptimizerBranches.cpp
126 void optimizeAwayStubs();
128 typedef std::unordered_map
<std::string
, const dyld3::MachOAnalyzer
*> InstallNameToMA
;
130 typedef uint64_t CacheOffset
;
132 UnmappedRegion _codeSignatureRegion
;
133 std::set
<const dyld3::MachOAnalyzer
*> _evictions
;
134 const ArchLayout
* _archLayout
= nullptr;
135 uint32_t _aliasCount
= 0;
136 uint64_t _slideInfoFileOffset
= 0;
137 uint64_t _slideInfoBufferSizeAllocated
= 0;
138 uint8_t* _objcReadOnlyBuffer
= nullptr;
139 uint64_t _objcReadOnlyBufferSizeUsed
= 0;
140 uint64_t _objcReadOnlyBufferSizeAllocated
= 0;
141 uint8_t* _objcReadWriteBuffer
= nullptr;
142 uint64_t _objcReadWriteBufferSizeAllocated
= 0;
143 uint64_t _selectorStringsFromExecutables
= 0;
144 InstallNameToMA _installNameToCacheDylib
;
145 std::unordered_map
<std::string
, uint32_t> _dataDirtySegsOrder
;
146 std::map
<void*, std::string
> _missingWeakImports
;
147 const dyld3::closure::ImageArray
* _imageArray
= nullptr;
148 uint8_t _cdHashFirst
[20];
149 uint8_t _cdHashSecond
[20];
150 bool _someDylibsUsedChainedFixups
= false;
151 std::unordered_map
<const dyld3::MachOLoaded
*, std::set
<CacheOffset
>> _dylibToItsExports
;
152 std::set
<std::pair
<const dyld3::MachOLoaded
*, CacheOffset
>> _dylibWeakExports
;
153 std::unordered_map
<CacheOffset
, std::vector
<dyld_cache_patchable_location
>> _exportsToUses
;
154 std::unordered_map
<CacheOffset
, std::string
> _exportsToName
;
159 #endif /* SharedCacheBuilder_h */