dyld-655.1.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 setIs64(bool);
74 void setHasObjC(bool);
75 void setHasPlusLoads(bool);
76 void setIsBundle(bool);
77 void setIsDylib(bool);
78 void setIsExecutable(bool);
79 void setIsRestricted(bool);
80 void setHasWeakDefs(bool);
81 void setUses16KPages(bool);
82 void setOverridableDylib(bool);
83 void setNeverUnload(bool);
84 void setUUID(const uuid_t uuid);
85 void setCDHash(const uint8_t cdHash[20]);
86 void setDependents(const Array<Image::LinkedImage>& deps);
87 void setDofOffsets(const Array<uint32_t>& dofSectionOffsets);
88 void setInitOffsets(const uint32_t initOffsets[], uint32_t count);
89 void setDiskSegments(const Image::DiskSegment segs[], uint32_t count);
90 void setCachedSegments(const Image::DyldCacheSegment segs[], uint32_t count);
91 void setCodeSignatureLocation(uint32_t fileOffset, uint32_t size);
92 void setFairPlayEncryptionRange(uint32_t fileOffset, uint32_t size);
93 void setMappingInfo(uint64_t sliceOffset, uint64_t vmSize);
94 void setFileInfo(uint64_t inode, uint64_t modTime);
95 void setRebaseInfo(const Array<Image::RebasePattern>&);
96 void setTextRebaseInfo(const Array<Image::TextFixupPattern>&);
97 void setBindInfo(const Array<Image::BindPattern>&);
98 void setAsOverrideOf(ImageNum);
99 void addExportPatchInfo(uint32_t implOff, const char* name, uint32_t locCount, const Image::PatchableExport::PatchLocation* locs);
100 void setInitsOrder(const ImageNum images[], uint32_t count);
101 void setChainedFixups(const Array<uint64_t>& starts, const Array<Image::ResolvedSymbolTarget>& targets);
102
103 const Image* currentImage();
104
105 const Image* finalize();
106
107 private:
108 Image::Flags& getFlags();
109
110 int _flagsOffset = -1;
111 };
112
113
114 class VIS_HIDDEN ImageArrayWriter : public ContainerTypedBytesWriter
115 {
116 public:
117 ImageArrayWriter(ImageNum startImageNum, unsigned count);
118
119 void appendImage(const Image*);
120 const ImageArray* finalize();
121 private:
122 unsigned _index;
123 };
124
125 class VIS_HIDDEN ClosureWriter : public ContainerTypedBytesWriter
126 {
127 public:
128 void setTopImageNum(ImageNum imageNum);
129 void addCachePatches(const Array<Closure::PatchEntry>&);
130 };
131
132 class VIS_HIDDEN LaunchClosureWriter : public ClosureWriter
133 {
134 public:
135 LaunchClosureWriter(const ImageArray* images);
136
137 const LaunchClosure* finalize();
138 void setLibSystemImageNum(ImageNum imageNum);
139 void setInitImageCount(uint32_t count);
140 void setLibDyldEntry(Image::ResolvedSymbolTarget dyldEntry);
141 void setMainEntry(Image::ResolvedSymbolTarget main);
142 void setStartEntry(Image::ResolvedSymbolTarget start);
143 void setUsedFallbackPaths(bool);
144 void setUsedAtPaths(bool);
145 void setMustBeMissingFiles(const Array<const char*>& paths);
146 void addInterposingTuples(const Array<InterposingTuple>& tuples);
147 void setDyldCacheUUID(const uuid_t);
148 void setBootUUID(const char* uuid);
149 void applyInterposing();
150 void addEnvVar(const char* envVar);
151
152 private:
153 LaunchClosure::Flags& getFlags();
154
155 int _flagsOffset = -1;
156 };
157
158
159 class VIS_HIDDEN DlopenClosureWriter : public ClosureWriter
160 {
161 public:
162 DlopenClosureWriter(const ImageArray* images);
163
164 const DlopenClosure* finalize();
165
166 };
167
168
169 } // namespace closure
170 } // namespace dyld3
171
172
173 #endif // ClosureWriter_h
174