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