dyld-832.7.1.tar.gz
[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 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();
113
114 const Image* currentImage();
115
116 const Image* finalize();
117
118 private:
119 Image::Flags& getFlags();
120
121 int _flagsOffset = -1;
122 };
123
124
125 class VIS_HIDDEN ImageArrayWriter : public ContainerTypedBytesWriter
126 {
127 public:
128 ImageArrayWriter(ImageNum startImageNum, unsigned count, bool hasRoots);
129
130 void appendImage(const Image*);
131 const ImageArray* finalize();
132 private:
133 unsigned _index;
134 };
135
136 class VIS_HIDDEN ClosureWriter : public ContainerTypedBytesWriter
137 {
138 public:
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);
143 };
144
145 class VIS_HIDDEN LaunchClosureWriter : public ClosureWriter
146 {
147 friend class ClosureBuilder;
148 public:
149 LaunchClosureWriter(const ImageArray* images);
150
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);
171
172 private:
173 LaunchClosure::Flags& getFlags();
174
175 int _flagsOffset = -1;
176 };
177
178
179 class VIS_HIDDEN DlopenClosureWriter : public ClosureWriter
180 {
181 public:
182 DlopenClosureWriter(const ImageArray* images);
183
184 const DlopenClosure* finalize();
185
186 };
187
188
189 } // namespace closure
190 } // namespace dyld3
191
192
193 #endif // ClosureWriter_h
194