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 ClosureWriter_h
26 #define ClosureWriter_h
32 #include <uuid/uuid.h>
33 #include <mach/mach.h>
34 #include <mach-o/loader.h>
35 #include <uuid/uuid.h>
46 class VIS_HIDDEN ContainerTypedBytesWriter
52 void setContainerType(TypedBytes::Type containerType
);
53 void* append(TypedBytes::Type t
, const void* payload
, uint32_t payloadSize
);
55 const void* currentTypedBytes();
56 const void* finalizeContainer();
58 void* _vmAllocationStart
= nullptr;
59 size_t _vmAllocationSize
= 0;
60 TypedBytes
* _containerTypedBytes
= nullptr;
65 class VIS_HIDDEN ImageWriter
: public ContainerTypedBytesWriter
69 void setImageNum(ImageNum num
);
70 void addPath(const char* path
); // first is canonical, others are aliases
72 void setInDyldCache(bool);
73 void setHasPrecomputedObjC(bool);
75 void setHasObjC(bool);
76 void setHasPlusLoads(bool);
77 void setIsBundle(bool);
78 void setIsDylib(bool);
79 void setIsExecutable(bool);
80 void setIsRestricted(bool);
81 void setHasWeakDefs(bool);
82 void setUses16KPages(bool);
83 void setOverridableDylib(bool);
84 void setNeverUnload(bool);
85 void setHasTerminators(bool);
86 void setUUID(const uuid_t uuid
);
87 void addCDHash(const uint8_t cdHash
[20]);
88 void setDependents(const Array
<Image::LinkedImage
>& deps
);
89 void setDofOffsets(const Array
<uint32_t>& dofSectionOffsets
);
90 void setInitOffsets(const uint32_t initOffsets
[], uint32_t count
);
91 void setInitSectRange(uint32_t sectionOffset
, uint32_t sectionSize
);
92 void setTermOffsets(const uint32_t termOffsets
[], uint32_t count
);
93 void setDiskSegments(const Image::DiskSegment segs
[], uint32_t count
);
94 void setCachedSegments(const Image::DyldCacheSegment segs
[], uint32_t count
);
95 void setCodeSignatureLocation(uint32_t fileOffset
, uint32_t size
);
96 void setFairPlayEncryptionRange(uint32_t fileOffset
, uint32_t size
);
97 void setMappingInfo(uint64_t sliceOffset
, uint64_t vmSize
);
98 void setFileInfo(uint64_t inode
, uint64_t modTime
);
99 void setRebaseInfo(const Array
<Image::RebasePattern
>&);
100 void setRebasesNotEncoded();
101 void setTextRebaseInfo(const Array
<Image::TextFixupPattern
>&);
102 void setBindInfo(const Array
<Image::BindPattern
>&);
103 void setObjCFixupInfo(const Image::ResolvedSymbolTarget
& objcProtocolClassTarget
,
104 uint64_t objcImageInfoVMOffset
,
105 const Array
<Image::ProtocolISAFixup
>& protocolISAFixups
,
106 const Array
<Image::SelectorReferenceFixup
>& selRefFixups
,
107 const Array
<Image::ClassStableSwiftFixup
>& classStableSwiftFixups
,
108 const Array
<Image::MethodListFixup
>& methodListFixups
);
109 void setAsOverrideOf(ImageNum
);
110 void setInitsOrder(const ImageNum images
[], uint32_t count
);
111 void setChainedFixups(uint64_t runtimeStartsStructOffset
, const Array
<Image::ResolvedSymbolTarget
>& targets
);
112 void setFixupsNotEncoded();
114 const Image
* currentImage();
116 const Image
* finalize();
119 Image::Flags
& getFlags();
121 int _flagsOffset
= -1;
125 class VIS_HIDDEN ImageArrayWriter
: public ContainerTypedBytesWriter
128 ImageArrayWriter(ImageNum startImageNum
, unsigned count
, bool hasRoots
);
130 void appendImage(const Image
*);
131 const ImageArray
* finalize();
136 class VIS_HIDDEN ClosureWriter
: public ContainerTypedBytesWriter
139 void setTopImageNum(ImageNum imageNum
);
140 void addCachePatches(const Array
<Closure::PatchEntry
>&);
141 void applyInterposing(const LaunchClosure
* launchClosure
);
142 void addWarning(Closure::Warning::Type type
, const char* warning
);
145 class VIS_HIDDEN LaunchClosureWriter
: public ClosureWriter
147 friend class ClosureBuilder
;
149 LaunchClosureWriter(const ImageArray
* images
);
151 const LaunchClosure
* finalize();
152 void setLibSystemImageNum(ImageNum imageNum
);
153 void setInitImageCount(uint32_t count
);
154 void setLibDyldEntry(Image::ResolvedSymbolTarget dyldEntry
);
155 void setMainEntry(Image::ResolvedSymbolTarget main
);
156 void setStartEntry(Image::ResolvedSymbolTarget start
);
157 void setUsedFallbackPaths(bool);
158 void setUsedAtPaths(bool);
159 void setUsedInterposing(bool);
160 void setHasInsertedLibraries(bool);
161 void setMustBeMissingFiles(const Array
<const char*>& paths
);
162 void setMustExistFiles(const Array
<LaunchClosure::SkippedFile
>& files
);
163 void addInterposingTuples(const Array
<InterposingTuple
>& tuples
);
164 void setDyldCacheUUID(const uuid_t
);
165 void addEnvVar(const char* envVar
);
166 void setObjCSelectorInfo(const Array
<uint8_t>& hashTable
, const Array
<Image::ObjCSelectorImage
>& hashTableImages
);
167 void setObjCClassAndProtocolInfo(const Array
<uint8_t>& classHashTable
, const Array
<uint8_t>& protocolHashTable
,
168 const Array
<Image::ObjCClassImage
>& hashTableImages
);
169 void setObjCDuplicateClassesInfo(const Array
<uint8_t>& hashTable
);
170 void setHasProgramVars(uint32_t offset
);
173 LaunchClosure::Flags
& getFlags();
175 int _flagsOffset
= -1;
179 class VIS_HIDDEN DlopenClosureWriter
: public ClosureWriter
182 DlopenClosureWriter(const ImageArray
* images
);
184 const DlopenClosure
* finalize();
189 } // namespace closure
193 #endif // ClosureWriter_h