]> git.saurik.com Git - apple/dyld.git/blob - dyld3/ClosureWriter.h
277431896daa46d8787f5565e5f6169cd2208fb2
[apple/dyld.git] / dyld3 / ClosureWriter.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #ifndef ClosureWriter_h
26 #define ClosureWriter_h
27
28
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <uuid/uuid.h>
33 #include <mach/mach.h>
34 #include <mach-o/loader.h>
35 #include <uuid/uuid.h>
36
37 #include "Closure.h"
38
39
40
41 namespace dyld3 {
42
43 namespace closure {
44
45
46 class VIS_HIDDEN ContainerTypedBytesWriter
47 {
48 public:
49 void deallocate();
50
51 protected:
52 void setContainerType(TypedBytes::Type containerType);
53 void* append(TypedBytes::Type t, const void* payload, uint32_t payloadSize);
54
55 const void* currentTypedBytes();
56 const void* finalizeContainer();
57
58 void* _vmAllocationStart = nullptr;
59 size_t _vmAllocationSize = 0;
60 TypedBytes* _containerTypedBytes = nullptr;
61 void* _end = nullptr;
62 };
63
64
65 class VIS_HIDDEN ImageWriter : public ContainerTypedBytesWriter
66 {
67 public:
68
69 void setImageNum(ImageNum num);
70 void addPath(const char* path); // first is canonical, others are aliases
71 void setInvalid();
72 void setInDyldCache(bool);
73 void setHasPrecomputedObjC(bool);
74 void setIs64(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 setTextRebaseInfo(const Array<Image::TextFixupPattern>&);
101 void setBindInfo(const Array<Image::BindPattern>&);
102 void setObjCFixupInfo(const Image::ResolvedSymbolTarget& objcProtocolClassTarget,
103 uint64_t objcImageInfoVMOffset,
104 const Array<Image::ProtocolISAFixup>& protocolISAFixups,
105 const Array<Image::SelectorReferenceFixup>& selRefFixups,
106 const Array<Image::ClassStableSwiftFixup>& classStableSwiftFixups,
107 const Array<Image::MethodListFixup>& methodListFixups);
108 void setAsOverrideOf(ImageNum);
109 void setInitsOrder(const ImageNum images[], uint32_t count);
110 void setChainedFixups(uint64_t runtimeStartsStructOffset, const Array<Image::ResolvedSymbolTarget>& targets);
111
112 const Image* currentImage();
113
114 const Image* finalize();
115
116 private:
117 Image::Flags& getFlags();
118
119 int _flagsOffset = -1;
120 };
121
122
123 class VIS_HIDDEN ImageArrayWriter : public ContainerTypedBytesWriter
124 {
125 public:
126 ImageArrayWriter(ImageNum startImageNum, unsigned count, bool hasRoots);
127
128 void appendImage(const Image*);
129 const ImageArray* finalize();
130 private:
131 unsigned _index;
132 };
133
134 class VIS_HIDDEN ClosureWriter : public ContainerTypedBytesWriter
135 {
136 public:
137 void setTopImageNum(ImageNum imageNum);
138 void addCachePatches(const Array<Closure::PatchEntry>&);
139 void applyInterposing(const LaunchClosure* launchClosure);
140 void addWarning(Closure::Warning::Type type, const char* warning);
141 };
142
143 class VIS_HIDDEN LaunchClosureWriter : public ClosureWriter
144 {
145 friend class ClosureBuilder;
146 public:
147 LaunchClosureWriter(const ImageArray* images);
148
149 const LaunchClosure* finalize();
150 void setLibSystemImageNum(ImageNum imageNum);
151 void setInitImageCount(uint32_t count);
152 void setLibDyldEntry(Image::ResolvedSymbolTarget dyldEntry);
153 void setMainEntry(Image::ResolvedSymbolTarget main);
154 void setStartEntry(Image::ResolvedSymbolTarget start);
155 void setUsedFallbackPaths(bool);
156 void setUsedAtPaths(bool);
157 void setUsedInterposing(bool);
158 void setHasInsertedLibraries(bool);
159 void setMustBeMissingFiles(const Array<const char*>& paths);
160 void setMustExistFiles(const Array<LaunchClosure::SkippedFile>& files);
161 void addInterposingTuples(const Array<InterposingTuple>& tuples);
162 void setDyldCacheUUID(const uuid_t);
163 void setBootUUID(const char* uuid);
164 void addEnvVar(const char* envVar);
165 void setObjCSelectorInfo(const Array<uint8_t>& hashTable, const Array<Image::ObjCSelectorImage>& hashTableImages);
166 void setObjCClassAndProtocolInfo(const Array<uint8_t>& classHashTable, const Array<uint8_t>& protocolHashTable,
167 const Array<Image::ObjCClassImage>& hashTableImages);
168 void setObjCDuplicateClassesInfo(const Array<uint8_t>& hashTable);
169
170 private:
171 LaunchClosure::Flags& getFlags();
172
173 int _flagsOffset = -1;
174 };
175
176
177 class VIS_HIDDEN DlopenClosureWriter : public ClosureWriter
178 {
179 public:
180 DlopenClosureWriter(const ImageArray* images);
181
182 const DlopenClosure* finalize();
183
184 };
185
186
187 } // namespace closure
188 } // namespace dyld3
189
190
191 #endif // ClosureWriter_h
192