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