]> git.saurik.com Git - ldid.git/blame - ldid.cpp
Drastic, sweeping modifications to support iPhone 1.2.0/2.0.
[ldid.git] / ldid.cpp
CommitLineData
a362a82f
JF
1/* JocStrap - Java/Objective-C Bootstrap
2 * Copyright (C) 2007 Jay Freeman (saurik)
3*/
4
5/*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36*/
37
38#include "minimal/stdlib.h"
39#include "minimal/mapping.h"
40
41#include <cstring>
42#include <string>
43#include <vector>
44
45struct fat_header {
46 uint32_t magic;
47 uint32_t nfat_arch;
48};
49
50#define FAT_MAGIC 0xcafebabe
51#define FAT_CIGAM 0xbebafeca
52
53struct fat_arch {
54 uint32_t cputype;
55 uint32_t cpusubtype;
56 uint32_t offset;
57 uint32_t size;
58 uint32_t align;
59};
60
61struct mach_header {
62 uint32_t magic;
63 uint32_t cputype;
64 uint32_t cpusubtype;
65 uint32_t filetype;
66 uint32_t ncmds;
67 uint32_t sizeofcmds;
68 uint32_t flags;
69};
70
71#define MH_MAGIC 0xfeedface
72#define MH_CIGAM 0xcefaedfe
73
74#define MH_EXECUTE 0x2
75#define MH_DYLIB 0x6
76#define MH_BUNDLE 0x8
77
78struct load_command {
79 uint32_t cmd;
80 uint32_t cmdsize;
81};
82
83#define LC_REQ_DYLD 0x80000000
84
85#define LC_LOAD_DYLIB 0x0c
86#define LC_ID_DYLIB 0x0d
87#define LC_UUID 0x1b
88#define LC_REEXPORT_DYLIB (0x1f | LC_REQ_DYLD)
89
90struct dylib {
91 uint32_t name;
92 uint32_t timestamp;
93 uint32_t current_version;
94 uint32_t compatibility_version;
95};
96
97struct dylib_command {
98 uint32_t cmd;
99 uint32_t cmdsize;
100 struct dylib dylib;
101};
102
103struct uuid_command {
104 uint32_t cmd;
105 uint32_t cmdsize;
106 uint8_t uuid[16];
107};
108
109class Framework {
110 private:
111 void *base_;
112 size_t size_;
113 mach_header *mach_header_;
114 bool swapped_;
115
116 public:
117 int16_t Swap(int16_t value) const {
118 return Swap(static_cast<uint16_t>(value));
119 }
120
121 int32_t Swap(int32_t value) const {
122 return Swap(static_cast<uint32_t>(value));
123 }
124
125 uint16_t Swap(uint16_t value) const {
126 return !swapped_ ? value :
127 ((value >> 8) & 0x00ff) |
128 ((value << 8) & 0xff00);
129 }
130
131 uint32_t Swap(uint32_t value) const {
132 if (!swapped_)
133 return value;
134 else {
135 value = ((value >> 8) & 0x00ff00ff) |
136 ((value << 8) & 0xff00ff00);
137 value = ((value >> 16) & 0x0000ffff) |
138 ((value << 16) & 0xffff0000);
139 return value;
140 }
141 }
142
143 Framework(const char *framework_path) :
144 swapped_(false)
145 {
146 base_ = map(framework_path, 0, _not(size_t), &size_, false);
147 fat_header *fat_header = reinterpret_cast<struct fat_header *>(base_);
148
149 if (Swap(fat_header->magic) == FAT_CIGAM) {
150 swapped_ = !swapped_;
151 goto fat;
152 } else if (Swap(fat_header->magic) != FAT_MAGIC)
153 mach_header_ = (mach_header *) base_;
154 else fat: {
155 size_t fat_narch = Swap(fat_header->nfat_arch);
156 fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header + 1);
157 size_t arch;
158 for (arch = 0; arch != fat_narch; ++arch) {
159 uint32_t arch_offset = Swap(fat_arch->offset);
160 mach_header_ = (mach_header *) ((uint8_t *) base_ + arch_offset);
161 goto found;
162 ++fat_arch;
163 }
164
165 _assert(false);
166 }
167
168 found:
169 if (Swap(mach_header_->magic) == MH_CIGAM)
170 swapped_ = !swapped_;
171 else _assert(Swap(mach_header_->magic) == MH_MAGIC);
172
173 _assert(
174 Swap(mach_header_->filetype) == MH_EXECUTE ||
175 Swap(mach_header_->filetype) == MH_DYLIB ||
176 Swap(mach_header_->filetype) == MH_BUNDLE
177 );
178 }
179
180 void *GetBase() {
181 return base_;
182 }
183
184 size_t GetSize() {
185 return size_;
186 }
187
188 std::vector<struct load_command *> GetLoadCommands() {
189 std::vector<struct load_command *> load_commands;
190
191 struct load_command *load_command = reinterpret_cast<struct load_command *>(mach_header_ + 1);
192 for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) {
193 load_commands.push_back(load_command);
194 load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize));
195 }
196
197 return load_commands;
198 }
199};
200
201extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval);
202
203int main(int argc, const char *argv[]) {
204 bool flag_R(false);
205 bool flag_t(false);
206 bool flag_p(false);
207 bool flag_u(false);
208
209 bool flag_T(false);
210
211 bool timeh(false);
212 uint32_t timev(0);
213
214 std::vector<std::string> files;
215
216 _assert(argc != 0);
217 for (int argi(1); argi != argc; ++argi)
218 if (argv[argi][0] != '-')
219 files.push_back(argv[argi]);
220 else switch (argv[argi][1]) {
221 case 'R': flag_R = true; break;
222 case 't': flag_t = true; break;
223 case 'u': flag_u = true; break;
224 case 'p': flag_p = true; break;
225
226 case 'T': {
227 flag_T = true;
228 if (argv[argi][2] == '-')
229 timeh = true;
230 else {
231 char *arge;
232 timev = strtoul(argv[argi] + 2, &arge, 0);
233 _assert(arge == argv[argi] + strlen(argv[argi]));
234 }
235 } break;
236
237 default:
238 goto usage;
239 break;
240 }
241
242 if (files.empty()) usage: {
243 exit(0);
244 }
245
246 size_t filei(0), filee(0);
247 _foreach (file, files) try {
248 Framework framework(file->c_str());
249
250 if (flag_p)
251 printf("path%zu='%s'\n", filei, file->c_str());
252
253 _foreach (load_command, framework.GetLoadCommands()) {
254 uint32_t cmd(framework.Swap((*load_command)->cmd));
255
256 if (flag_R && cmd == LC_REEXPORT_DYLIB)
257 (*load_command)->cmd = framework.Swap(LC_LOAD_DYLIB);
258 else if (cmd == LC_UUID) {
259 volatile struct uuid_command *uuid_command(reinterpret_cast<struct uuid_command *>(*load_command));
260
261 if (flag_u) {
262 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei,
263 uuid_command->uuid[ 0], uuid_command->uuid[ 1], uuid_command->uuid[ 2], uuid_command->uuid[ 3],
264 uuid_command->uuid[ 4], uuid_command->uuid[ 5], uuid_command->uuid[ 6], uuid_command->uuid[ 7],
265 uuid_command->uuid[ 8], uuid_command->uuid[ 9], uuid_command->uuid[10], uuid_command->uuid[11],
266 uuid_command->uuid[12], uuid_command->uuid[13], uuid_command->uuid[14], uuid_command->uuid[15]
267 );
268 }
269 } else if (cmd == LC_ID_DYLIB) {
270 volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(*load_command));
271
272 if (flag_t)
273 printf("time%zu=0x%.8x\n", filei, framework.Swap(dylib_command->dylib.timestamp));
274
275 if (flag_T) {
276 uint32_t timed;
277
278 if (!timeh)
279 timed = timev;
280 else {
281 dylib_command->dylib.timestamp = 0;
282 timed = hash(reinterpret_cast<uint8_t *>(framework.GetBase()), framework.GetSize(), timev);
283 }
284
285 dylib_command->dylib.timestamp = framework.Swap(timed);
286 }
287 }
288 }
289
290 ++filei;
291 } catch (const char *) {
292 ++filee;
293 ++filei;
294 }
295
296 return filee;
297}