]>
Commit | Line | Data |
---|---|---|
ab113d22 | 1 | /* ldid - (Mach-O) Link-Loader Identity Editor |
e0524446 | 2 | * Copyright (C) 2007-2012 Jay Freeman (saurik) |
a362a82f JF |
3 | */ |
4 | ||
dd6c06ed JF |
5 | /* GNU Affero General Public License, Version 3 {{{ */ |
6 | /* | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Affero General Public License as published by | |
e0524446 JF |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. | |
dd6c06ed | 11 | |
e0524446 JF |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
dd6c06ed JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
e0524446 JF |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
20 | /* }}} */ | |
a362a82f JF |
21 | |
22 | #include "minimal/stdlib.h" | |
b8187ac7 | 23 | #include "minimal/string.h" |
a362a82f JF |
24 | #include "minimal/mapping.h" |
25 | ||
604cc486 | 26 | extern "C" { |
fdb119ef | 27 | #include "sha1.h" |
604cc486 | 28 | } |
fdb119ef | 29 | |
a362a82f JF |
30 | #include <cstring> |
31 | #include <string> | |
32 | #include <vector> | |
33 | ||
34 | struct fat_header { | |
35 | uint32_t magic; | |
36 | uint32_t nfat_arch; | |
c05d9758 | 37 | } _packed; |
a362a82f JF |
38 | |
39 | #define FAT_MAGIC 0xcafebabe | |
40 | #define FAT_CIGAM 0xbebafeca | |
41 | ||
42 | struct fat_arch { | |
43 | uint32_t cputype; | |
44 | uint32_t cpusubtype; | |
45 | uint32_t offset; | |
46 | uint32_t size; | |
47 | uint32_t align; | |
c05d9758 | 48 | } _packed; |
a362a82f JF |
49 | |
50 | struct mach_header { | |
51 | uint32_t magic; | |
52 | uint32_t cputype; | |
04802ab1 | 53 | uint32_t cpusubtype; |
a362a82f JF |
54 | uint32_t filetype; |
55 | uint32_t ncmds; | |
56 | uint32_t sizeofcmds; | |
57 | uint32_t flags; | |
c05d9758 | 58 | } _packed; |
a362a82f | 59 | |
fdb119ef | 60 | #define MH_MAGIC 0xfeedface |
a362a82f JF |
61 | #define MH_CIGAM 0xcefaedfe |
62 | ||
3cbc6463 JF |
63 | #define MH_MAGIC_64 0xfeedfacf |
64 | #define MH_CIGAM_64 0xcffaedfe | |
65 | ||
82813bde JF |
66 | #define MH_DYLDLINK 0x4 |
67 | ||
4a864785 | 68 | #define MH_OBJECT 0x1 |
efd6cd4a JF |
69 | #define MH_EXECUTE 0x2 |
70 | #define MH_DYLIB 0x6 | |
71 | #define MH_BUNDLE 0x8 | |
72 | #define MH_DYLIB_STUB 0x9 | |
a362a82f JF |
73 | |
74 | struct load_command { | |
75 | uint32_t cmd; | |
76 | uint32_t cmdsize; | |
c05d9758 | 77 | } _packed; |
a362a82f | 78 | |
4a57e66c JF |
79 | #define LC_REQ_DYLD uint32_t(0x80000000) |
80 | ||
81 | #define LC_SEGMENT uint32_t(0x01) | |
82 | #define LC_SYMTAB uint32_t(0x02) | |
83 | #define LC_DYSYMTAB uint32_t(0x0b) | |
84 | #define LC_LOAD_DYLIB uint32_t(0x0c) | |
85 | #define LC_ID_DYLIB uint32_t(0x0d) | |
289ccbbc | 86 | #define LC_SEGMENT_64 uint32_t(0x19) |
4a57e66c JF |
87 | #define LC_UUID uint32_t(0x1b) |
88 | #define LC_CODE_SIGNATURE uint32_t(0x1d) | |
89 | #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e) | |
90 | #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD) | |
c0cc1574 | 91 | #define LC_ENCRYPTION_INFO uint32_t(0x21) |
4a57e66c JF |
92 | #define LC_DYLD_INFO uint32_t(0x22) |
93 | #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD) | |
a362a82f JF |
94 | |
95 | struct dylib { | |
96 | uint32_t name; | |
97 | uint32_t timestamp; | |
98 | uint32_t current_version; | |
99 | uint32_t compatibility_version; | |
c05d9758 | 100 | } _packed; |
a362a82f JF |
101 | |
102 | struct dylib_command { | |
103 | uint32_t cmd; | |
104 | uint32_t cmdsize; | |
105 | struct dylib dylib; | |
c05d9758 | 106 | } _packed; |
a362a82f JF |
107 | |
108 | struct uuid_command { | |
109 | uint32_t cmd; | |
110 | uint32_t cmdsize; | |
111 | uint8_t uuid[16]; | |
c05d9758 | 112 | } _packed; |
a362a82f | 113 | |
20e7eb29 JF |
114 | struct symtab_command { |
115 | uint32_t cmd; | |
116 | uint32_t cmdsize; | |
117 | uint32_t symoff; | |
118 | uint32_t nsyms; | |
119 | uint32_t stroff; | |
120 | uint32_t strsize; | |
121 | } _packed; | |
122 | ||
4a57e66c JF |
123 | struct dyld_info_command { |
124 | uint32_t cmd; | |
125 | uint32_t cmdsize; | |
126 | uint32_t rebase_off; | |
127 | uint32_t rebase_size; | |
128 | uint32_t bind_off; | |
129 | uint32_t bind_size; | |
130 | uint32_t weak_bind_off; | |
131 | uint32_t weak_bind_size; | |
132 | uint32_t lazy_bind_off; | |
133 | uint32_t lazy_bind_size; | |
134 | uint32_t export_off; | |
135 | uint32_t export_size; | |
136 | } _packed; | |
137 | ||
138 | struct dysymtab_command { | |
139 | uint32_t cmd; | |
140 | uint32_t cmdsize; | |
141 | uint32_t ilocalsym; | |
142 | uint32_t nlocalsym; | |
143 | uint32_t iextdefsym; | |
144 | uint32_t nextdefsym; | |
145 | uint32_t iundefsym; | |
146 | uint32_t nundefsym; | |
147 | uint32_t tocoff; | |
148 | uint32_t ntoc; | |
149 | uint32_t modtaboff; | |
150 | uint32_t nmodtab; | |
151 | uint32_t extrefsymoff; | |
152 | uint32_t nextrefsyms; | |
153 | uint32_t indirectsymoff; | |
154 | uint32_t nindirectsyms; | |
155 | uint32_t extreloff; | |
156 | uint32_t nextrel; | |
157 | uint32_t locreloff; | |
158 | uint32_t nlocrel; | |
159 | } _packed; | |
160 | ||
161 | struct dylib_table_of_contents { | |
162 | uint32_t symbol_index; | |
163 | uint32_t module_index; | |
164 | } _packed; | |
165 | ||
166 | struct dylib_module { | |
167 | uint32_t module_name; | |
168 | uint32_t iextdefsym; | |
169 | uint32_t nextdefsym; | |
170 | uint32_t irefsym; | |
171 | uint32_t nrefsym; | |
172 | uint32_t ilocalsym; | |
173 | uint32_t nlocalsym; | |
174 | uint32_t iextrel; | |
175 | uint32_t nextrel; | |
176 | uint32_t iinit_iterm; | |
177 | uint32_t ninit_nterm; | |
178 | uint32_t objc_module_info_addr; | |
179 | uint32_t objc_module_info_size; | |
180 | } _packed; | |
181 | ||
182 | struct dylib_reference { | |
183 | uint32_t isym:24; | |
184 | uint32_t flags:8; | |
185 | } _packed; | |
186 | ||
187 | struct relocation_info { | |
188 | int32_t r_address; | |
189 | uint32_t r_symbolnum:24; | |
190 | uint32_t r_pcrel:1; | |
191 | uint32_t r_length:2; | |
192 | uint32_t r_extern:1; | |
193 | uint32_t r_type:4; | |
194 | } _packed; | |
195 | ||
196 | struct nlist { | |
197 | union { | |
198 | char *n_name; | |
199 | int32_t n_strx; | |
200 | } n_un; | |
201 | ||
202 | uint8_t n_type; | |
203 | uint8_t n_sect; | |
204 | uint8_t n_desc; | |
205 | uint32_t n_value; | |
206 | } _packed; | |
207 | ||
6e83315b JF |
208 | struct segment_command { |
209 | uint32_t cmd; | |
210 | uint32_t cmdsize; | |
211 | char segname[16]; | |
212 | uint32_t vmaddr; | |
213 | uint32_t vmsize; | |
214 | uint32_t fileoff; | |
215 | uint32_t filesize; | |
216 | uint32_t maxprot; | |
217 | uint32_t initprot; | |
218 | uint32_t nsects; | |
219 | uint32_t flags; | |
429e34a5 | 220 | } _packed; |
6e83315b | 221 | |
289ccbbc JF |
222 | struct segment_command_64 { |
223 | uint32_t cmd; | |
224 | uint32_t cmdsize; | |
225 | char segname[16]; | |
226 | uint64_t vmaddr; | |
227 | uint64_t vmsize; | |
228 | uint64_t fileoff; | |
229 | uint64_t filesize; | |
230 | uint32_t maxprot; | |
231 | uint32_t initprot; | |
232 | uint32_t nsects; | |
233 | uint32_t flags; | |
429e34a5 | 234 | } _packed; |
289ccbbc | 235 | |
6e83315b JF |
236 | struct section { |
237 | char sectname[16]; | |
238 | char segname[16]; | |
239 | uint32_t addr; | |
240 | uint32_t size; | |
241 | uint32_t offset; | |
242 | uint32_t align; | |
243 | uint32_t reloff; | |
244 | uint32_t nreloc; | |
245 | uint32_t flags; | |
246 | uint32_t reserved1; | |
247 | uint32_t reserved2; | |
429e34a5 | 248 | } _packed; |
6e83315b | 249 | |
289ccbbc JF |
250 | struct section_64 { |
251 | char sectname[16]; | |
252 | char segname[16]; | |
253 | uint64_t addr; | |
254 | uint64_t size; | |
255 | uint32_t offset; | |
256 | uint32_t align; | |
257 | uint32_t reloff; | |
258 | uint32_t nreloc; | |
259 | uint32_t flags; | |
260 | uint32_t reserved1; | |
261 | uint32_t reserved2; | |
429e34a5 | 262 | } _packed; |
289ccbbc | 263 | |
fdb119ef JF |
264 | struct linkedit_data_command { |
265 | uint32_t cmd; | |
266 | uint32_t cmdsize; | |
267 | uint32_t dataoff; | |
268 | uint32_t datasize; | |
c05d9758 | 269 | } _packed; |
fdb119ef | 270 | |
c0cc1574 JF |
271 | struct encryption_info_command { |
272 | uint32_t cmd; | |
273 | uint32_t cmdsize; | |
274 | uint32_t cryptoff; | |
275 | uint32_t cryptsize; | |
276 | uint32_t cryptid; | |
277 | } _packed; | |
278 | ||
4a864785 JF |
279 | #define BIND_OPCODE_MASK 0xf0 |
280 | #define BIND_IMMEDIATE_MASK 0x0f | |
281 | #define BIND_OPCODE_DONE 0x00 | |
282 | #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10 | |
283 | #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20 | |
284 | #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30 | |
285 | #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40 | |
286 | #define BIND_OPCODE_SET_TYPE_IMM 0x50 | |
287 | #define BIND_OPCODE_SET_ADDEND_SLEB 0x60 | |
288 | #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70 | |
289 | #define BIND_OPCODE_ADD_ADDR_ULEB 0x80 | |
290 | #define BIND_OPCODE_DO_BIND 0x90 | |
291 | #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0 | |
292 | #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0 | |
293 | #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0 | |
294 | ||
4374152f JF |
295 | template <typename Type_> |
296 | Type_ Align(Type_ value, size_t align) { | |
297 | value += align - 1; | |
298 | value /= align; | |
299 | value *= align; | |
300 | return value; | |
301 | } | |
302 | ||
fdb119ef JF |
303 | uint16_t Swap_(uint16_t value) { |
304 | return | |
305 | ((value >> 8) & 0x00ff) | | |
306 | ((value << 8) & 0xff00); | |
307 | } | |
308 | ||
309 | uint32_t Swap_(uint32_t value) { | |
310 | value = ((value >> 8) & 0x00ff00ff) | | |
311 | ((value << 8) & 0xff00ff00); | |
312 | value = ((value >> 16) & 0x0000ffff) | | |
313 | ((value << 16) & 0xffff0000); | |
314 | return value; | |
315 | } | |
316 | ||
4374152f JF |
317 | uint64_t Swap_(uint64_t value) { |
318 | value = (value & 0x00000000ffffffff) << 32 | (value & 0xffffffff00000000) >> 32; | |
319 | value = (value & 0x0000ffff0000ffff) << 16 | (value & 0xffff0000ffff0000) >> 16; | |
320 | value = (value & 0x00ff00ff00ff00ff) << 8 | (value & 0xff00ff00ff00ff00) >> 8; | |
321 | return value; | |
322 | } | |
323 | ||
fdb119ef JF |
324 | int16_t Swap_(int16_t value) { |
325 | return Swap_(static_cast<uint16_t>(value)); | |
326 | } | |
327 | ||
328 | int32_t Swap_(int32_t value) { | |
329 | return Swap_(static_cast<uint32_t>(value)); | |
330 | } | |
331 | ||
4374152f JF |
332 | int64_t Swap_(int64_t value) { |
333 | return Swap_(static_cast<uint64_t>(value)); | |
334 | } | |
335 | ||
5525a5a7 JF |
336 | bool little_(true); |
337 | ||
fdb119ef | 338 | uint16_t Swap(uint16_t value) { |
5525a5a7 | 339 | return little_ ? Swap_(value) : value; |
fdb119ef JF |
340 | } |
341 | ||
342 | uint32_t Swap(uint32_t value) { | |
5525a5a7 | 343 | return little_ ? Swap_(value) : value; |
fdb119ef JF |
344 | } |
345 | ||
4374152f JF |
346 | uint64_t Swap(uint64_t value) { |
347 | return little_ ? Swap_(value) : value; | |
348 | } | |
349 | ||
fdb119ef JF |
350 | int16_t Swap(int16_t value) { |
351 | return Swap(static_cast<uint16_t>(value)); | |
352 | } | |
353 | ||
354 | int32_t Swap(int32_t value) { | |
355 | return Swap(static_cast<uint32_t>(value)); | |
356 | } | |
357 | ||
4374152f JF |
358 | int64_t Swap(int64_t value) { |
359 | return Swap(static_cast<uint64_t>(value)); | |
360 | } | |
361 | ||
6e83315b JF |
362 | template <typename Target_> |
363 | class Pointer; | |
364 | ||
0a033524 | 365 | class Data { |
a362a82f JF |
366 | private: |
367 | void *base_; | |
368 | size_t size_; | |
3cbc6463 | 369 | |
0a033524 | 370 | protected: |
a362a82f JF |
371 | bool swapped_; |
372 | ||
373 | public: | |
0a033524 JF |
374 | Data(void *base, size_t size) : |
375 | base_(base), | |
376 | size_(size), | |
377 | swapped_(false) | |
378 | { | |
379 | } | |
380 | ||
fdb119ef JF |
381 | uint16_t Swap(uint16_t value) const { |
382 | return swapped_ ? Swap_(value) : value; | |
a362a82f JF |
383 | } |
384 | ||
fdb119ef JF |
385 | uint32_t Swap(uint32_t value) const { |
386 | return swapped_ ? Swap_(value) : value; | |
a362a82f JF |
387 | } |
388 | ||
4374152f JF |
389 | uint64_t Swap(uint64_t value) const { |
390 | return swapped_ ? Swap_(value) : value; | |
391 | } | |
392 | ||
fdb119ef JF |
393 | int16_t Swap(int16_t value) const { |
394 | return Swap(static_cast<uint16_t>(value)); | |
a362a82f JF |
395 | } |
396 | ||
fdb119ef JF |
397 | int32_t Swap(int32_t value) const { |
398 | return Swap(static_cast<uint32_t>(value)); | |
a362a82f JF |
399 | } |
400 | ||
4374152f JF |
401 | int64_t Swap(int64_t value) const { |
402 | return Swap(static_cast<uint64_t>(value)); | |
403 | } | |
404 | ||
0a033524 JF |
405 | void *GetBase() const { |
406 | return base_; | |
407 | } | |
a362a82f | 408 | |
0a033524 JF |
409 | size_t GetSize() const { |
410 | return size_; | |
411 | } | |
412 | }; | |
a362a82f | 413 | |
0a033524 JF |
414 | class MachHeader : |
415 | public Data | |
416 | { | |
417 | private: | |
418 | bool bits64_; | |
419 | ||
420 | struct mach_header *mach_header_; | |
421 | struct load_command *load_command_; | |
422 | ||
423 | public: | |
424 | MachHeader(void *base, size_t size) : | |
425 | Data(base, size) | |
426 | { | |
427 | mach_header_ = (mach_header *) base; | |
a362a82f | 428 | |
3cbc6463 JF |
429 | switch (Swap(mach_header_->magic)) { |
430 | case MH_CIGAM: | |
431 | swapped_ = !swapped_; | |
432 | case MH_MAGIC: | |
433 | bits64_ = false; | |
434 | break; | |
435 | ||
436 | case MH_CIGAM_64: | |
437 | swapped_ = !swapped_; | |
438 | case MH_MAGIC_64: | |
439 | bits64_ = true; | |
440 | break; | |
441 | ||
442 | default: | |
443 | _assert(false); | |
444 | } | |
445 | ||
446 | void *post = mach_header_ + 1; | |
447 | if (bits64_) | |
448 | post = (uint32_t *) post + 1; | |
449 | load_command_ = (struct load_command *) post; | |
a362a82f JF |
450 | |
451 | _assert( | |
452 | Swap(mach_header_->filetype) == MH_EXECUTE || | |
453 | Swap(mach_header_->filetype) == MH_DYLIB || | |
454 | Swap(mach_header_->filetype) == MH_BUNDLE | |
455 | ); | |
456 | } | |
457 | ||
afbb7c8e JF |
458 | struct mach_header *operator ->() const { |
459 | return mach_header_; | |
460 | } | |
461 | ||
4374152f JF |
462 | operator struct mach_header *() const { |
463 | return mach_header_; | |
464 | } | |
465 | ||
04802ab1 JF |
466 | uint32_t GetCPUType() const { |
467 | return Swap(mach_header_->cputype); | |
468 | } | |
469 | ||
4374152f | 470 | uint32_t GetCPUSubtype() const { |
04802ab1 JF |
471 | return Swap(mach_header_->cpusubtype) & 0xff; |
472 | } | |
473 | ||
4374152f JF |
474 | struct load_command *GetLoadCommand() const { |
475 | return load_command_; | |
476 | } | |
477 | ||
0a033524 | 478 | std::vector<struct load_command *> GetLoadCommands() const { |
a362a82f JF |
479 | std::vector<struct load_command *> load_commands; |
480 | ||
3cbc6463 | 481 | struct load_command *load_command = load_command_; |
a362a82f JF |
482 | for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { |
483 | load_commands.push_back(load_command); | |
484 | load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize)); | |
485 | } | |
486 | ||
487 | return load_commands; | |
488 | } | |
6e83315b | 489 | |
6f340f51 | 490 | std::vector<segment_command *> GetSegments(const char *segment_name) const { |
6e83315b JF |
491 | std::vector<struct segment_command *> segment_commands; |
492 | ||
289ccbbc | 493 | _foreach (load_command, GetLoadCommands()) { |
0a033524 JF |
494 | if (Swap(load_command->cmd) == LC_SEGMENT) { |
495 | segment_command *segment_command = reinterpret_cast<struct segment_command *>(load_command); | |
6e83315b JF |
496 | if (strncmp(segment_command->segname, segment_name, 16) == 0) |
497 | segment_commands.push_back(segment_command); | |
498 | } | |
289ccbbc JF |
499 | } |
500 | ||
501 | return segment_commands; | |
502 | } | |
503 | ||
4374152f | 504 | std::vector<segment_command_64 *> GetSegments64(const char *segment_name) const { |
289ccbbc JF |
505 | std::vector<struct segment_command_64 *> segment_commands; |
506 | ||
507 | _foreach (load_command, GetLoadCommands()) { | |
508 | if (Swap(load_command->cmd) == LC_SEGMENT_64) { | |
509 | segment_command_64 *segment_command = reinterpret_cast<struct segment_command_64 *>(load_command); | |
510 | if (strncmp(segment_command->segname, segment_name, 16) == 0) | |
511 | segment_commands.push_back(segment_command); | |
512 | } | |
513 | } | |
6e83315b JF |
514 | |
515 | return segment_commands; | |
516 | } | |
517 | ||
6f340f51 | 518 | std::vector<section *> GetSections(const char *segment_name, const char *section_name) const { |
6e83315b JF |
519 | std::vector<section *> sections; |
520 | ||
521 | _foreach (segment, GetSegments(segment_name)) { | |
0a033524 | 522 | section *section = (struct section *) (segment + 1); |
6e83315b JF |
523 | |
524 | uint32_t sect; | |
0a033524 | 525 | for (sect = 0; sect != Swap(segment->nsects); ++sect) { |
6e83315b JF |
526 | if (strncmp(section->sectname, section_name, 16) == 0) |
527 | sections.push_back(section); | |
528 | ++section; | |
529 | } | |
530 | } | |
531 | ||
532 | return sections; | |
533 | } | |
534 | ||
535 | template <typename Target_> | |
0a033524 | 536 | Pointer<Target_> GetPointer(uint32_t address, const char *segment_name = NULL) const { |
6e83315b JF |
537 | load_command *load_command = (struct load_command *) (mach_header_ + 1); |
538 | uint32_t cmd; | |
539 | ||
540 | for (cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { | |
541 | if (Swap(load_command->cmd) == LC_SEGMENT) { | |
542 | segment_command *segment_command = (struct segment_command *) load_command; | |
543 | if (segment_name != NULL && strncmp(segment_command->segname, segment_name, 16) != 0) | |
544 | goto next_command; | |
545 | ||
546 | section *sections = (struct section *) (segment_command + 1); | |
547 | ||
548 | uint32_t sect; | |
549 | for (sect = 0; sect != Swap(segment_command->nsects); ++sect) { | |
550 | section *section = §ions[sect]; | |
551 | //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size); | |
552 | if (address >= Swap(section->addr) && address < Swap(section->addr) + Swap(section->size)) { | |
553 | //printf("0x%.8x %s\n", address, segment_command->segname); | |
554 | return Pointer<Target_>(this, reinterpret_cast<Target_ *>(address - Swap(section->addr) + Swap(section->offset) + (char *) mach_header_)); | |
555 | } | |
556 | } | |
557 | } | |
558 | ||
559 | next_command: | |
560 | load_command = (struct load_command *) ((char *) load_command + Swap(load_command->cmdsize)); | |
561 | } | |
562 | ||
563 | return Pointer<Target_>(this); | |
564 | } | |
565 | ||
566 | template <typename Target_> | |
567 | Pointer<Target_> GetOffset(uint32_t offset) { | |
568 | return Pointer<Target_>(this, reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_)); | |
569 | } | |
570 | }; | |
571 | ||
289ccbbc JF |
572 | class FatMachHeader : |
573 | public MachHeader | |
574 | { | |
575 | private: | |
576 | fat_arch *fat_arch_; | |
577 | ||
578 | public: | |
579 | FatMachHeader(void *base, size_t size, fat_arch *fat_arch) : | |
580 | MachHeader(base, size), | |
581 | fat_arch_(fat_arch) | |
582 | { | |
583 | } | |
584 | ||
585 | fat_arch *GetFatArch() const { | |
586 | return fat_arch_; | |
587 | } | |
588 | }; | |
589 | ||
0a033524 JF |
590 | class FatHeader : |
591 | public Data | |
592 | { | |
593 | private: | |
594 | fat_header *fat_header_; | |
289ccbbc | 595 | std::vector<FatMachHeader> mach_headers_; |
0a033524 JF |
596 | |
597 | public: | |
598 | FatHeader(void *base, size_t size) : | |
599 | Data(base, size) | |
600 | { | |
601 | fat_header_ = reinterpret_cast<struct fat_header *>(base); | |
602 | ||
603 | if (Swap(fat_header_->magic) == FAT_CIGAM) { | |
604 | swapped_ = !swapped_; | |
605 | goto fat; | |
606 | } else if (Swap(fat_header_->magic) != FAT_MAGIC) { | |
607 | fat_header_ = NULL; | |
289ccbbc | 608 | mach_headers_.push_back(FatMachHeader(base, size, NULL)); |
0a033524 JF |
609 | } else fat: { |
610 | size_t fat_narch = Swap(fat_header_->nfat_arch); | |
611 | fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header_ + 1); | |
612 | size_t arch; | |
613 | for (arch = 0; arch != fat_narch; ++arch) { | |
614 | uint32_t arch_offset = Swap(fat_arch->offset); | |
615 | uint32_t arch_size = Swap(fat_arch->size); | |
289ccbbc | 616 | mach_headers_.push_back(FatMachHeader((uint8_t *) base + arch_offset, arch_size, fat_arch)); |
0a033524 JF |
617 | ++fat_arch; |
618 | } | |
619 | } | |
620 | } | |
621 | ||
289ccbbc | 622 | std::vector<FatMachHeader> &GetMachHeaders() { |
0a033524 JF |
623 | return mach_headers_; |
624 | } | |
20e7eb29 JF |
625 | |
626 | bool IsFat() const { | |
627 | return fat_header_ != NULL; | |
628 | } | |
6b38c173 JF |
629 | |
630 | struct fat_header *operator ->() const { | |
631 | return fat_header_; | |
632 | } | |
4374152f JF |
633 | |
634 | operator struct fat_header *() const { | |
635 | return fat_header_; | |
636 | } | |
0a033524 JF |
637 | }; |
638 | ||
80fb9fad | 639 | FatHeader Map(const char *path, bool ro = false) { |
0a033524 | 640 | size_t size; |
80fb9fad | 641 | void *base(map(path, 0, _not(size_t), &size, ro)); |
0a033524 JF |
642 | return FatHeader(base, size); |
643 | } | |
644 | ||
6e83315b JF |
645 | template <typename Target_> |
646 | class Pointer { | |
647 | private: | |
0a033524 | 648 | const MachHeader *framework_; |
6e83315b JF |
649 | const Target_ *pointer_; |
650 | ||
651 | public: | |
0a033524 | 652 | Pointer(const MachHeader *framework = NULL, const Target_ *pointer = NULL) : |
6e83315b JF |
653 | framework_(framework), |
654 | pointer_(pointer) | |
655 | { | |
656 | } | |
657 | ||
658 | operator const Target_ *() const { | |
659 | return pointer_; | |
660 | } | |
661 | ||
662 | const Target_ *operator ->() const { | |
663 | return pointer_; | |
664 | } | |
665 | ||
666 | Pointer<Target_> &operator ++() { | |
667 | ++pointer_; | |
668 | return *this; | |
669 | } | |
670 | ||
671 | template <typename Value_> | |
672 | Value_ Swap(Value_ value) { | |
673 | return framework_->Swap(value); | |
674 | } | |
a362a82f JF |
675 | }; |
676 | ||
645f8f12 JF |
677 | #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02) |
678 | #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0) | |
679 | #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171) | |
c05d9758 | 680 | |
645f8f12 JF |
681 | #define CSSLOT_CODEDIRECTORY uint32_t(0) |
682 | #define CSSLOT_REQUIREMENTS uint32_t(2) | |
683 | #define CSSLOT_ENTITLEMENTS uint32_t(5) | |
fdb119ef JF |
684 | |
685 | struct BlobIndex { | |
686 | uint32_t type; | |
687 | uint32_t offset; | |
c05d9758 | 688 | } _packed; |
fdb119ef | 689 | |
c05d9758 | 690 | struct Blob { |
fdb119ef JF |
691 | uint32_t magic; |
692 | uint32_t length; | |
c05d9758 JF |
693 | } _packed; |
694 | ||
695 | struct SuperBlob { | |
696 | struct Blob blob; | |
fdb119ef JF |
697 | uint32_t count; |
698 | struct BlobIndex index[]; | |
c05d9758 | 699 | } _packed; |
fdb119ef JF |
700 | |
701 | struct CodeDirectory { | |
c05d9758 | 702 | struct Blob blob; |
fdb119ef JF |
703 | uint32_t version; |
704 | uint32_t flags; | |
705 | uint32_t hashOffset; | |
706 | uint32_t identOffset; | |
707 | uint32_t nSpecialSlots; | |
708 | uint32_t nCodeSlots; | |
709 | uint32_t codeLimit; | |
710 | uint8_t hashSize; | |
711 | uint8_t hashType; | |
712 | uint8_t spare1; | |
713 | uint8_t pageSize; | |
714 | uint32_t spare2; | |
c05d9758 | 715 | } _packed; |
fdb119ef | 716 | |
a362a82f JF |
717 | extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval); |
718 | ||
fdb119ef JF |
719 | void sha1(uint8_t *hash, uint8_t *data, size_t size) { |
720 | SHA1Context context; | |
721 | SHA1Reset(&context); | |
722 | SHA1Input(&context, data, size); | |
723 | SHA1Result(&context, hash); | |
724 | } | |
725 | ||
0a033524 | 726 | struct CodesignAllocation { |
4374152f JF |
727 | FatMachHeader mach_header_; |
728 | uint32_t offset_; | |
729 | uint32_t size_; | |
730 | uint32_t alloc_; | |
731 | ||
732 | CodesignAllocation(FatMachHeader mach_header, size_t offset, size_t size, size_t alloc) : | |
733 | mach_header_(mach_header), | |
734 | offset_(offset), | |
735 | size_(size), | |
736 | alloc_(alloc) | |
0a033524 JF |
737 | { |
738 | } | |
739 | }; | |
740 | ||
a362a82f | 741 | int main(int argc, const char *argv[]) { |
5525a5a7 JF |
742 | union { |
743 | uint16_t word; | |
744 | uint8_t byte[2]; | |
745 | } endian = {1}; | |
746 | ||
747 | little_ = endian.byte[0]; | |
748 | ||
a362a82f | 749 | bool flag_R(false); |
20e7eb29 JF |
750 | bool flag_r(false); |
751 | ||
a362a82f JF |
752 | bool flag_t(false); |
753 | bool flag_p(false); | |
754 | bool flag_u(false); | |
9c83be90 | 755 | bool flag_e(false); |
a362a82f JF |
756 | |
757 | bool flag_T(false); | |
20c5f1e8 | 758 | |
fdb119ef | 759 | bool flag_S(false); |
20c5f1e8 | 760 | bool flag_s(false); |
a362a82f | 761 | |
6ab830a5 JF |
762 | bool flag_O(false); |
763 | ||
c0cc1574 JF |
764 | bool flag_D(false); |
765 | bool flag_d(false); | |
766 | ||
8c417842 JF |
767 | bool flag_A(false); |
768 | bool flag_a(false); | |
769 | ||
c0cc1574 JF |
770 | uint32_t flag_CPUType(_not(uint32_t)); |
771 | uint32_t flag_CPUSubtype(_not(uint32_t)); | |
772 | ||
5d7ceb5d JF |
773 | const char *flag_I(NULL); |
774 | ||
a362a82f JF |
775 | bool timeh(false); |
776 | uint32_t timev(0); | |
777 | ||
c05d9758 JF |
778 | const void *xmld(NULL); |
779 | size_t xmls(0); | |
780 | ||
6e83315b JF |
781 | uintptr_t noffset(_not(uintptr_t)); |
782 | uintptr_t woffset(_not(uintptr_t)); | |
783 | ||
a362a82f JF |
784 | std::vector<std::string> files; |
785 | ||
a960f392 JF |
786 | if (argc == 1) { |
787 | fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv[0]); | |
9c83be90 | 788 | fprintf(stderr, " %s -e MobileSafari\n", argv[0]); |
a960f392 JF |
789 | fprintf(stderr, " %s -S cat\n", argv[0]); |
790 | fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]); | |
791 | exit(0); | |
792 | } | |
793 | ||
a362a82f JF |
794 | for (int argi(1); argi != argc; ++argi) |
795 | if (argv[argi][0] != '-') | |
796 | files.push_back(argv[argi]); | |
797 | else switch (argv[argi][1]) { | |
798 | case 'R': flag_R = true; break; | |
20e7eb29 JF |
799 | case 'r': flag_r = true; break; |
800 | ||
a362a82f JF |
801 | case 't': flag_t = true; break; |
802 | case 'u': flag_u = true; break; | |
803 | case 'p': flag_p = true; break; | |
9c83be90 | 804 | case 'e': flag_e = true; break; |
6ab830a5 | 805 | case 'O': flag_O = true; break; |
c05d9758 | 806 | |
c0cc1574 JF |
807 | case 'D': flag_D = true; break; |
808 | case 'd': flag_d = true; break; | |
809 | ||
8c417842 JF |
810 | case 'a': flag_a = true; break; |
811 | ||
812 | case 'A': | |
813 | flag_A = true; | |
814 | if (argv[argi][2] != '\0') { | |
815 | const char *cpu = argv[argi] + 2; | |
816 | const char *colon = strchr(cpu, ':'); | |
817 | _assert(colon != NULL); | |
818 | char *arge; | |
819 | flag_CPUType = strtoul(cpu, &arge, 0); | |
820 | _assert(arge == colon); | |
821 | flag_CPUSubtype = strtoul(colon + 1, &arge, 0); | |
822 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
823 | } | |
824 | break; | |
825 | ||
20c5f1e8 JF |
826 | case 's': |
827 | _assert(!flag_S); | |
828 | flag_s = true; | |
829 | break; | |
830 | ||
c05d9758 | 831 | case 'S': |
20c5f1e8 | 832 | _assert(!flag_s); |
c05d9758 JF |
833 | flag_S = true; |
834 | if (argv[argi][2] != '\0') { | |
835 | const char *xml = argv[argi] + 2; | |
836 | xmld = map(xml, 0, _not(size_t), &xmls, true); | |
837 | } | |
838 | break; | |
a362a82f JF |
839 | |
840 | case 'T': { | |
841 | flag_T = true; | |
842 | if (argv[argi][2] == '-') | |
843 | timeh = true; | |
844 | else { | |
845 | char *arge; | |
846 | timev = strtoul(argv[argi] + 2, &arge, 0); | |
847 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
848 | } | |
849 | } break; | |
850 | ||
5d7ceb5d JF |
851 | case 'I': { |
852 | flag_I = argv[argi] + 2; | |
853 | } break; | |
854 | ||
6e83315b JF |
855 | case 'n': { |
856 | char *arge; | |
857 | noffset = strtoul(argv[argi] + 2, &arge, 0); | |
858 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
859 | } break; | |
860 | ||
861 | case 'w': { | |
862 | char *arge; | |
863 | woffset = strtoul(argv[argi] + 2, &arge, 0); | |
864 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
865 | } break; | |
866 | ||
a362a82f JF |
867 | default: |
868 | goto usage; | |
869 | break; | |
870 | } | |
871 | ||
872 | if (files.empty()) usage: { | |
873 | exit(0); | |
874 | } | |
875 | ||
876 | size_t filei(0), filee(0); | |
877 | _foreach (file, files) try { | |
0a033524 | 878 | const char *path(file.c_str()); |
fdb119ef JF |
879 | const char *base = strrchr(path, '/'); |
880 | char *temp(NULL), *dir; | |
881 | ||
882 | if (base != NULL) | |
b8187ac7 | 883 | dir = strndup_(path, base++ - path + 1); |
fdb119ef JF |
884 | else { |
885 | dir = strdup(""); | |
886 | base = path; | |
887 | } | |
888 | ||
5d7ceb5d JF |
889 | const char *name(flag_I ?: base); |
890 | ||
20e7eb29 | 891 | if (flag_r) { |
289ccbbc | 892 | uint32_t clip(0); { |
20e7eb29 JF |
893 | FatHeader fat_header(Map(path)); |
894 | _foreach (mach_header, fat_header.GetMachHeaders()) { | |
4f4aa9c3 JF |
895 | if (flag_A) { |
896 | if (mach_header.GetCPUType() != flag_CPUType) | |
897 | continue; | |
898 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) | |
899 | continue; | |
900 | } | |
901 | ||
20e7eb29 JF |
902 | mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK); |
903 | ||
289ccbbc | 904 | uint32_t size(_not(uint32_t)); { |
20e7eb29 JF |
905 | _foreach (load_command, mach_header.GetLoadCommands()) { |
906 | switch (mach_header.Swap(load_command->cmd)) { | |
907 | case LC_CODE_SIGNATURE: { | |
908 | struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
289ccbbc | 909 | memset(reinterpret_cast<uint8_t *>(mach_header.GetBase()) + mach_header.Swap(signature->dataoff), 0, mach_header.Swap(signature->datasize)); |
20e7eb29 JF |
910 | memset(signature, 0, sizeof(struct linkedit_data_command)); |
911 | ||
289ccbbc JF |
912 | mach_header->ncmds = mach_header.Swap(mach_header.Swap(mach_header->ncmds) - 1); |
913 | mach_header->sizeofcmds = mach_header.Swap(uint32_t(mach_header.Swap(mach_header->sizeofcmds) - sizeof(struct linkedit_data_command))); | |
20e7eb29 JF |
914 | } break; |
915 | ||
916 | case LC_SYMTAB: { | |
917 | struct symtab_command *symtab = reinterpret_cast<struct symtab_command *>(load_command); | |
289ccbbc | 918 | size = mach_header.Swap(symtab->stroff) + mach_header.Swap(symtab->strsize); |
20e7eb29 JF |
919 | } break; |
920 | } | |
921 | } | |
289ccbbc | 922 | } |
20e7eb29 | 923 | |
289ccbbc | 924 | _assert(size != _not(uint32_t)); |
20e7eb29 | 925 | |
4374152f | 926 | _foreach (segment, mach_header.GetSegments("__LINKEDIT")) { |
289ccbbc JF |
927 | segment->filesize -= mach_header.GetSize() - size; |
928 | ||
929 | if (fat_arch *fat_arch = mach_header.GetFatArch()) { | |
930 | fat_arch->size = fat_header.Swap(size); | |
931 | clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size); | |
932 | } else | |
933 | clip = std::max(clip, size); | |
934 | } | |
935 | ||
4374152f | 936 | _foreach (segment, mach_header.GetSegments64("__LINKEDIT")) { |
289ccbbc JF |
937 | segment->filesize -= mach_header.GetSize() - size; |
938 | ||
939 | if (fat_arch *fat_arch = mach_header.GetFatArch()) { | |
940 | fat_arch->size = fat_header.Swap(size); | |
941 | clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size); | |
942 | } else | |
943 | clip = std::max(clip, size); | |
20e7eb29 JF |
944 | } |
945 | } | |
946 | } | |
947 | ||
4f4aa9c3 JF |
948 | if (clip != 0) |
949 | _syscall(truncate(path, clip)); | |
20e7eb29 JF |
950 | } |
951 | ||
fdb119ef | 952 | if (flag_S) { |
4374152f JF |
953 | FatHeader source(Map(path)); |
954 | ||
955 | size_t offset(0); | |
956 | ||
957 | if (source.IsFat()) | |
958 | offset += sizeof(fat_header) + sizeof(fat_arch) * source.Swap(source->nfat_arch); | |
fdb119ef | 959 | |
0a033524 | 960 | std::vector<CodesignAllocation> allocations; { |
4374152f | 961 | _foreach (mach_header, source.GetMachHeaders()) { |
4f4aa9c3 JF |
962 | if (flag_A) { |
963 | if (mach_header.GetCPUType() != flag_CPUType) | |
964 | continue; | |
965 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) | |
966 | continue; | |
967 | } | |
968 | ||
0a033524 JF |
969 | mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK); |
970 | ||
971 | size_t size(_not(size_t)); { | |
972 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
973 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
974 | if (cmd == LC_CODE_SIGNATURE) { | |
975 | struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
976 | size = mach_header.Swap(signature->dataoff); | |
977 | _assert(size < mach_header.GetSize()); | |
978 | break; | |
979 | } | |
980 | } | |
981 | ||
982 | if (size == _not(size_t)) | |
983 | size = mach_header.GetSize(); | |
afbb7c8e | 984 | } |
afbb7c8e | 985 | |
0a033524 JF |
986 | size_t alloc(0); |
987 | alloc += sizeof(struct SuperBlob); | |
988 | uint32_t special(0); | |
989 | ||
990 | special = std::max(special, CSSLOT_CODEDIRECTORY); | |
991 | alloc += sizeof(struct BlobIndex); | |
992 | alloc += sizeof(struct CodeDirectory); | |
5d7ceb5d | 993 | alloc += strlen(name) + 1; |
0a033524 JF |
994 | |
995 | special = std::max(special, CSSLOT_REQUIREMENTS); | |
996 | alloc += sizeof(struct BlobIndex); | |
997 | alloc += 0xc; | |
998 | ||
999 | if (xmld != NULL) { | |
1000 | special = std::max(special, CSSLOT_ENTITLEMENTS); | |
1001 | alloc += sizeof(struct BlobIndex); | |
1002 | alloc += sizeof(struct Blob); | |
1003 | alloc += xmls; | |
1004 | } | |
1005 | ||
4374152f JF |
1006 | size_t normal((size + 0x1000 - 1) / 0x1000); |
1007 | alloc = Align(alloc + (special + normal) * 0x14, 16); | |
0a033524 | 1008 | |
4374152f JF |
1009 | offset = Align(offset, 4096); |
1010 | allocations.push_back(CodesignAllocation(mach_header, offset, size, alloc)); | |
1011 | offset += size + alloc; | |
1b7da752 | 1012 | offset = Align(offset, 16); |
0a033524 | 1013 | } |
4374152f | 1014 | } |
0a033524 | 1015 | |
4374152f JF |
1016 | asprintf(&temp, "%s.%s.cs", dir, base); |
1017 | fclose(fopen(temp, "w+")); | |
c7063c2b | 1018 | _syscall(truncate(temp, offset)); |
4374152f JF |
1019 | |
1020 | void *file(map(temp, 0, offset, NULL, false)); | |
1021 | memset(file, 0, offset); | |
1022 | ||
1023 | fat_arch *fat_arch; | |
1024 | if (!source.IsFat()) | |
1025 | fat_arch = NULL; | |
1026 | else { | |
1027 | auto *fat_header(reinterpret_cast<struct fat_header *>(file)); | |
1028 | fat_header->magic = Swap(FAT_MAGIC); | |
1029 | fat_header->nfat_arch = Swap(source.Swap(source->nfat_arch)); | |
1030 | fat_arch = reinterpret_cast<struct fat_arch *>(fat_header + 1); | |
1031 | } | |
1032 | ||
1033 | _foreach (allocation, allocations) { | |
1034 | auto &source(allocation.mach_header_); | |
0a033524 | 1035 | |
4374152f JF |
1036 | uint32_t align(allocation.size_); |
1037 | align = Align(align, 0x10); | |
0a033524 | 1038 | |
4374152f JF |
1039 | if (fat_arch != NULL) { |
1040 | fat_arch->cputype = Swap(source->cputype); | |
1041 | fat_arch->cpusubtype = Swap(source->cpusubtype); | |
1042 | fat_arch->offset = Swap(allocation.offset_); | |
1043 | fat_arch->size = Swap(align + allocation.alloc_); | |
1044 | fat_arch->align = Swap(0xc); | |
1045 | ++fat_arch; | |
0a033524 JF |
1046 | } |
1047 | ||
4374152f JF |
1048 | void *target(reinterpret_cast<uint8_t *>(file) + allocation.offset_); |
1049 | memcpy(target, source, allocation.size_); | |
1050 | MachHeader mach_header(target, align + allocation.alloc_); | |
1051 | ||
1052 | struct linkedit_data_command *signature(NULL); | |
1053 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
1054 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
1055 | if (cmd != LC_CODE_SIGNATURE) | |
1056 | continue; | |
1057 | signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
1058 | break; | |
1059 | } | |
1060 | ||
1061 | if (signature == NULL) { | |
1062 | mach_header->ncmds = mach_header.Swap(mach_header.Swap(mach_header->ncmds) + 1); | |
1063 | signature = reinterpret_cast<struct linkedit_data_command *>(reinterpret_cast<uint8_t *>(mach_header.GetLoadCommand()) + mach_header.Swap(mach_header->sizeofcmds)); | |
1064 | mach_header->sizeofcmds = mach_header.Swap(mach_header.Swap(mach_header->sizeofcmds) + uint32_t(sizeof(*signature))); | |
1065 | signature->cmd = mach_header.Swap(LC_CODE_SIGNATURE); | |
1066 | signature->cmdsize = mach_header.Swap(uint32_t(sizeof(*signature))); | |
1067 | } | |
fdb119ef | 1068 | |
4374152f JF |
1069 | signature->dataoff = mach_header.Swap(align); |
1070 | signature->datasize = mach_header.Swap(allocation.alloc_); | |
4f4aa9c3 | 1071 | |
2036c29d JF |
1072 | _foreach (segment, mach_header.GetSegments("__LINKEDIT")) { |
1073 | size_t size(mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff))); | |
1074 | segment->filesize = size; | |
1075 | segment->vmsize = Align(size, 0x1000); | |
1076 | } | |
1077 | ||
1078 | _foreach (segment, mach_header.GetSegments64("__LINKEDIT")) { | |
1079 | size_t size(mach_header.Swap(align + allocation.alloc_ - mach_header.Swap(segment->fileoff))); | |
1080 | segment->filesize = size; | |
1081 | segment->vmsize = Align(size, 0x1000); | |
1082 | } | |
4f4aa9c3 | 1083 | } |
fdb119ef JF |
1084 | } |
1085 | ||
0a033524 JF |
1086 | if (flag_p) |
1087 | printf("path%zu='%s'\n", filei, file.c_str()); | |
1088 | ||
ad77e976 | 1089 | FatHeader fat_header(Map(temp == NULL ? path : temp, !(flag_R || flag_T || flag_s || flag_S || flag_O || flag_D))); |
fdb119ef | 1090 | struct linkedit_data_command *signature(NULL); |
a362a82f | 1091 | |
0a033524 | 1092 | _foreach (mach_header, fat_header.GetMachHeaders()) { |
8c417842 JF |
1093 | if (flag_A) { |
1094 | if (mach_header.GetCPUType() != flag_CPUType) | |
1095 | continue; | |
1096 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) | |
1097 | continue; | |
1098 | } | |
1099 | ||
1100 | if (flag_a) | |
1101 | printf("cpu=0x%x:0x%x\n", mach_header.GetCPUType(), mach_header.GetCPUSubtype()); | |
1102 | ||
c0cc1574 JF |
1103 | if (flag_d) { |
1104 | if (struct fat_arch *fat_arch = mach_header.GetFatArch()) | |
1105 | printf("offset=0x%x\n", Swap(fat_arch->offset)); | |
1106 | else | |
1107 | printf("offset=0x0\n"); | |
1108 | } | |
1109 | ||
0a033524 JF |
1110 | if (woffset != _not(uintptr_t)) { |
1111 | Pointer<uint32_t> wvalue(mach_header.GetPointer<uint32_t>(woffset)); | |
1112 | if (wvalue == NULL) | |
1113 | printf("(null) %p\n", reinterpret_cast<void *>(woffset)); | |
1114 | else | |
1115 | printf("0x%.08x\n", *wvalue); | |
1116 | } | |
6e83315b | 1117 | |
0a033524 JF |
1118 | if (noffset != _not(uintptr_t)) |
1119 | printf("%s\n", &*mach_header.GetPointer<char>(noffset)); | |
1120 | ||
c0cc1574 JF |
1121 | if (flag_d) |
1122 | _foreach(segment, mach_header.GetSegments("__TEXT")) { | |
1123 | printf("vmaddr=0x%x\n", mach_header.Swap(segment->vmaddr)); | |
1124 | printf("fileoff=0x%x\n", mach_header.Swap(segment->fileoff)); | |
1125 | } | |
1126 | ||
6ab830a5 JF |
1127 | if (flag_O) { |
1128 | _foreach(section, mach_header.GetSections("__TEXT", "__text")) | |
1129 | section->addr = mach_header.Swap(0); | |
1130 | } | |
1131 | ||
0a033524 JF |
1132 | _foreach (load_command, mach_header.GetLoadCommands()) { |
1133 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
1134 | ||
1135 | if (flag_R && cmd == LC_REEXPORT_DYLIB) | |
1136 | load_command->cmd = mach_header.Swap(LC_LOAD_DYLIB); | |
1137 | else if (cmd == LC_CODE_SIGNATURE) | |
1138 | signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
1139 | else if (cmd == LC_UUID) { | |
1140 | volatile struct uuid_command *uuid_command(reinterpret_cast<struct uuid_command *>(load_command)); | |
1141 | ||
1142 | if (flag_u) { | |
1143 | printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei, | |
1144 | uuid_command->uuid[ 0], uuid_command->uuid[ 1], uuid_command->uuid[ 2], uuid_command->uuid[ 3], | |
1145 | uuid_command->uuid[ 4], uuid_command->uuid[ 5], uuid_command->uuid[ 6], uuid_command->uuid[ 7], | |
1146 | uuid_command->uuid[ 8], uuid_command->uuid[ 9], uuid_command->uuid[10], uuid_command->uuid[11], | |
1147 | uuid_command->uuid[12], uuid_command->uuid[13], uuid_command->uuid[14], uuid_command->uuid[15] | |
1148 | ); | |
1149 | } | |
1150 | } else if (cmd == LC_ID_DYLIB) { | |
1151 | volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command)); | |
a362a82f | 1152 | |
0a033524 JF |
1153 | if (flag_t) |
1154 | printf("time%zu=0x%.8x\n", filei, mach_header.Swap(dylib_command->dylib.timestamp)); | |
a362a82f | 1155 | |
0a033524 JF |
1156 | if (flag_T) { |
1157 | uint32_t timed; | |
a362a82f | 1158 | |
0a033524 JF |
1159 | if (!timeh) |
1160 | timed = timev; | |
1161 | else { | |
1162 | dylib_command->dylib.timestamp = 0; | |
1163 | timed = hash(reinterpret_cast<uint8_t *>(mach_header.GetBase()), mach_header.GetSize(), timev); | |
1164 | } | |
a362a82f | 1165 | |
0a033524 JF |
1166 | dylib_command->dylib.timestamp = mach_header.Swap(timed); |
1167 | } | |
c0cc1574 JF |
1168 | } else if (cmd == LC_ENCRYPTION_INFO) { |
1169 | volatile struct encryption_info_command *encryption_info_command(reinterpret_cast<struct encryption_info_command *>(load_command)); | |
1170 | ||
1171 | if (flag_D) | |
1172 | encryption_info_command->cryptid = mach_header.Swap(0); | |
1173 | ||
1174 | if (flag_d) { | |
1175 | printf("cryptoff=0x%x\n", mach_header.Swap(encryption_info_command->cryptoff)); | |
1176 | printf("cryptsize=0x%x\n", mach_header.Swap(encryption_info_command->cryptsize)); | |
1a982df5 | 1177 | printf("cryptid=0x%x\n", mach_header.Swap(encryption_info_command->cryptid)); |
c0cc1574 | 1178 | } |
a362a82f JF |
1179 | } |
1180 | } | |
a362a82f | 1181 | |
0a033524 JF |
1182 | if (flag_e) { |
1183 | _assert(signature != NULL); | |
9c83be90 | 1184 | |
0a033524 | 1185 | uint32_t data = mach_header.Swap(signature->dataoff); |
9c83be90 | 1186 | |
0a033524 JF |
1187 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
1188 | uint8_t *blob = top + data; | |
1189 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
9c83be90 | 1190 | |
0a033524 JF |
1191 | for (size_t index(0); index != Swap(super->count); ++index) |
1192 | if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) { | |
1193 | uint32_t begin = Swap(super->index[index].offset); | |
1194 | struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin); | |
1195 | fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(struct Blob), stdout); | |
1196 | } | |
1197 | } | |
9c83be90 | 1198 | |
0a033524 JF |
1199 | if (flag_s) { |
1200 | _assert(signature != NULL); | |
20c5f1e8 | 1201 | |
0a033524 | 1202 | uint32_t data = mach_header.Swap(signature->dataoff); |
20c5f1e8 | 1203 | |
0a033524 JF |
1204 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
1205 | uint8_t *blob = top + data; | |
1206 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
20c5f1e8 | 1207 | |
0a033524 JF |
1208 | for (size_t index(0); index != Swap(super->count); ++index) |
1209 | if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) { | |
1210 | uint32_t begin = Swap(super->index[index].offset); | |
1211 | struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin); | |
20c5f1e8 | 1212 | |
0a033524 JF |
1213 | uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + begin + Swap(directory->hashOffset)); |
1214 | uint32_t pages = Swap(directory->nCodeSlots); | |
20c5f1e8 | 1215 | |
0a033524 JF |
1216 | if (pages != 1) |
1217 | for (size_t i = 0; i != pages - 1; ++i) | |
1218 | sha1(hashes[i], top + 0x1000 * i, 0x1000); | |
1219 | if (pages != 0) | |
1220 | sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1); | |
1221 | } | |
1222 | } | |
20c5f1e8 | 1223 | |
0a033524 JF |
1224 | if (flag_S) { |
1225 | _assert(signature != NULL); | |
fdb119ef | 1226 | |
0a033524 JF |
1227 | uint32_t data = mach_header.Swap(signature->dataoff); |
1228 | uint32_t size = mach_header.Swap(signature->datasize); | |
fdb119ef | 1229 | |
0a033524 JF |
1230 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
1231 | uint8_t *blob = top + data; | |
1232 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
1233 | super->blob.magic = Swap(CSMAGIC_EMBEDDED_SIGNATURE); | |
fdb119ef | 1234 | |
0a033524 JF |
1235 | uint32_t count = xmld == NULL ? 2 : 3; |
1236 | uint32_t offset = sizeof(struct SuperBlob) + count * sizeof(struct BlobIndex); | |
fdb119ef | 1237 | |
0a033524 JF |
1238 | super->index[0].type = Swap(CSSLOT_CODEDIRECTORY); |
1239 | super->index[0].offset = Swap(offset); | |
fdb119ef | 1240 | |
0a033524 JF |
1241 | uint32_t begin = offset; |
1242 | struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin); | |
1243 | offset += sizeof(struct CodeDirectory); | |
fdb119ef | 1244 | |
0a033524 JF |
1245 | directory->blob.magic = Swap(CSMAGIC_CODEDIRECTORY); |
1246 | directory->version = Swap(uint32_t(0x00020001)); | |
1247 | directory->flags = Swap(uint32_t(0)); | |
1248 | directory->codeLimit = Swap(data); | |
1249 | directory->hashSize = 0x14; | |
1250 | directory->hashType = 0x01; | |
1251 | directory->spare1 = 0x00; | |
1252 | directory->pageSize = 0x0c; | |
1253 | directory->spare2 = Swap(uint32_t(0)); | |
fdb119ef | 1254 | |
0a033524 | 1255 | directory->identOffset = Swap(offset - begin); |
5d7ceb5d JF |
1256 | strcpy(reinterpret_cast<char *>(blob + offset), name); |
1257 | offset += strlen(name) + 1; | |
fdb119ef | 1258 | |
0a033524 JF |
1259 | uint32_t special = xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS; |
1260 | directory->nSpecialSlots = Swap(special); | |
20c5f1e8 | 1261 | |
0a033524 JF |
1262 | uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + offset); |
1263 | memset(hashes, 0, sizeof(*hashes) * special); | |
20c5f1e8 | 1264 | |
0a033524 JF |
1265 | offset += sizeof(*hashes) * special; |
1266 | hashes += special; | |
fdb119ef | 1267 | |
0a033524 JF |
1268 | uint32_t pages = (data + 0x1000 - 1) / 0x1000; |
1269 | directory->nCodeSlots = Swap(pages); | |
fdb119ef | 1270 | |
0a033524 JF |
1271 | if (pages != 1) |
1272 | for (size_t i = 0; i != pages - 1; ++i) | |
1273 | sha1(hashes[i], top + 0x1000 * i, 0x1000); | |
1274 | if (pages != 0) | |
1275 | sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1); | |
fdb119ef | 1276 | |
0a033524 JF |
1277 | directory->hashOffset = Swap(offset - begin); |
1278 | offset += sizeof(*hashes) * pages; | |
1279 | directory->blob.length = Swap(offset - begin); | |
fdb119ef | 1280 | |
0a033524 JF |
1281 | super->index[1].type = Swap(CSSLOT_REQUIREMENTS); |
1282 | super->index[1].offset = Swap(offset); | |
afbb7c8e | 1283 | |
0a033524 JF |
1284 | memcpy(blob + offset, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc); |
1285 | offset += 0xc; | |
afbb7c8e | 1286 | |
0a033524 JF |
1287 | if (xmld != NULL) { |
1288 | super->index[2].type = Swap(CSSLOT_ENTITLEMENTS); | |
1289 | super->index[2].offset = Swap(offset); | |
c05d9758 | 1290 | |
0a033524 JF |
1291 | uint32_t begin = offset; |
1292 | struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin); | |
1293 | offset += sizeof(struct Blob); | |
c05d9758 | 1294 | |
0a033524 JF |
1295 | memcpy(blob + offset, xmld, xmls); |
1296 | offset += xmls; | |
c05d9758 | 1297 | |
0a033524 JF |
1298 | entitlements->magic = Swap(CSMAGIC_ENTITLEMENTS); |
1299 | entitlements->length = Swap(offset - begin); | |
1300 | } | |
c05d9758 | 1301 | |
0a033524 JF |
1302 | for (size_t index(0); index != count; ++index) { |
1303 | uint32_t type = Swap(super->index[index].type); | |
1304 | if (type != 0 && type <= special) { | |
1305 | uint32_t offset = Swap(super->index[index].offset); | |
1306 | struct Blob *local = (struct Blob *) (blob + offset); | |
1307 | sha1((uint8_t *) (hashes - type), (uint8_t *) local, Swap(local->length)); | |
1308 | } | |
20c5f1e8 | 1309 | } |
20c5f1e8 | 1310 | |
0a033524 JF |
1311 | super->count = Swap(count); |
1312 | super->blob.length = Swap(offset); | |
c05d9758 | 1313 | |
0a033524 JF |
1314 | if (offset > size) { |
1315 | fprintf(stderr, "offset (%u) > size (%u)\n", offset, size); | |
1316 | _assert(false); | |
1317 | } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size); | |
fdb119ef | 1318 | |
0a033524 JF |
1319 | memset(blob + offset, 0, size - offset); |
1320 | } | |
fdb119ef JF |
1321 | } |
1322 | ||
122621c5 | 1323 | if (flag_S) { |
0a033524 JF |
1324 | uint8_t *top = reinterpret_cast<uint8_t *>(fat_header.GetBase()); |
1325 | size_t size = fat_header.GetSize(); | |
122621c5 JF |
1326 | |
1327 | char *copy; | |
1328 | asprintf(©, "%s.%s.cp", dir, base); | |
1329 | FILE *file = fopen(copy, "w+"); | |
1330 | size_t writ = fwrite(top, 1, size, file); | |
1331 | _assert(writ == size); | |
1332 | fclose(file); | |
1333 | ||
1334 | _syscall(unlink(temp)); | |
1335 | free(temp); | |
1336 | temp = copy; | |
1337 | } | |
1338 | ||
0a033524 | 1339 | if (temp != NULL) { |
31ad673b JF |
1340 | struct stat info; |
1341 | _syscall(stat(path, &info)); | |
1342 | _syscall(chown(temp, info.st_uid, info.st_gid)); | |
1343 | _syscall(chmod(temp, info.st_mode)); | |
fdb119ef JF |
1344 | _syscall(unlink(path)); |
1345 | _syscall(rename(temp, path)); | |
1346 | free(temp); | |
1347 | } | |
1348 | ||
1349 | free(dir); | |
a362a82f JF |
1350 | ++filei; |
1351 | } catch (const char *) { | |
1352 | ++filee; | |
1353 | ++filei; | |
1354 | } | |
1355 | ||
1356 | return filee; | |
1357 | } |