forked from emacs-mirror/emacs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unexmacosx.c
1467 lines (1268 loc) · 43.6 KB
/
unexmacosx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* unexmacosx.c -*- C -*-
* (this is the top-level one; use the one in ./src instead)
* Dump Emacs in Mach-O format for use on Mac OS X.
* Copyright (C) 2001, 2002, 2003, 2004, 2005,
*************** 2006, 2007 Free Software Foundation, Inc. */
/*
This file is part of GNU Emacs.
GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* Contributed by Andrew Choi <[email protected]>. */
/* Documentation note.
Consult the following documents/files for a description of the
Mach-O format: the file loader.h, man pages for Mach-O and ld, old
NEXTSTEP documents of the Mach-O format. The tool otool dumps the
mach header (-h option) and the load commands (-l option) in a
Mach-O file. The tool nm on Mac OS X displays the symbol table in
a Mach-O file. For examples of unexec for the Mach-O format, see
the file unexnext.c in the GNU Emacs distribution, the file
unexdyld.c in the Darwin port of GNU Emacs 20.7, and unexdyld.c in
the Darwin port of XEmacs 21.1. Also the Darwin Libc source
contains the source code for malloc_freezedry and malloc_jumpstart.
Read that to see what they do. This file was written completely
from scratch, making use of information from the above sources. */
/* The Mac OS X implementation of unexec makes use of Darwin's `zone'
memory allocator. All calls to malloc, realloc, and free in Emacs
are redirected to unexec_malloc, unexec_realloc, and unexec_free in
this file. When temacs is run, all memory requests are handled in
the zone EmacsZone. The Darwin memory allocator library calls
maintain the data structures to manage this zone. Dumping writes
its contents to data segments of the executable file. When emacs
is run, the loader recreates the contents of the zone in memory.
However since the initialization routine of the zone memory
allocator is run again, this `zone' can no longer be used as a
heap. That is why emacs uses the ordinary malloc system call to
allocate memory. Also, when a block of memory needs to be
reallocated and the new size is larger than the old one, a new
block must be obtained by malloc and the old contents copied to
it. */
/* Peculiarity of the Mach-O files generated by ld in Mac OS X
(possible causes of future bugs if changed).
The file offset of the start of the __TEXT segment is zero. Since
the Mach header and load commands are located at the beginning of a
Mach-O file, copying the contents of the __TEXT segment from the
input file overwrites them in the output file. Despite this,
unexec works fine as written below because the segment load command
for __TEXT appears, and is therefore processed, before all other
load commands except the segment load command for __PAGEZERO, which
remains unchanged.
Although the file offset of the start of the __TEXT segment is
zero, none of the sections it contains actually start there. In
fact, the earliest one starts a few hundred bytes beyond the end of
the last load command. The linker option -headerpad controls the
minimum size of this padding. Its setting can be changed in
s/darwin.h. A value of 0x690, e.g., leaves room for 30 additional
load commands for the newly created __DATA segments (at 56 bytes
each). Unexec fails if there is not enough room for these new
segments.
The __TEXT segment contains the sections __text, __cstring,
__picsymbol_stub, and __const and the __DATA segment contains the
sections __data, __la_symbol_ptr, __nl_symbol_ptr, __dyld, __bss,
and __common. The other segments do not contain any sections.
These sections are copied from the input file to the output file,
except for __data, __bss, and __common, which are dumped from
memory. The types of the sections __bss and __common are changed
from S_ZEROFILL to S_REGULAR. Note that the number of sections and
their relative order in the input and output files remain
unchanged. Otherwise all n_sect fields in the nlist records in the
symbol table (specified by the LC_SYMTAB load command) will have to
be changed accordingly.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <mach/mach.h>
#include <mach-o/loader.h>
#include <mach-o/reloc.h>
#if defined (__ppc__)
#include <mach-o/ppc/reloc.h>
#endif
#include <config.h>
#undef malloc
#undef realloc
#undef free
#ifdef HAVE_MALLOC_MALLOC_H
#include <malloc/malloc.h>
#else
#include <objc/malloc.h>
#endif
#include <assert.h>
#include "version.h"
#if _LP64
#define mach_header mach_header_64
#define segment_command segment_command_64
#define vm_region vm_region_64
#define section section_64
#define target_VM_REGION_BASIC_INFO_COUNT VM_REGION_BASIC_INFO_COUNT_64
#define target_VM_REGION_BASIC_INFO VM_REGION_BASIC_INFO_64
#define target_LC_SEGMENT LC_SEGMENT_64
#define target_MH_MAGIC MH_MAGIC_64
#else
#define target_VM_REGION_BASIC_INFO_COUNT VM_REGION_BASIC_INFO_COUNT
#define target_VM_REGION_BASIC_INFO VM_REGION_BASIC_INFO
#define target_LC_SEGMENT LC_SEGMENT
#define target_MH_MAGIC MH_MAGIC
#endif
#define VERBOSE 1
/* Size of buffer used to copy data from the input file to the output
file in function unexec_copy. */
#define UNEXEC_COPY_BUFSZ 1024
/* Regions with memory addresses above this value are assumed to be
mapped to dynamically loaded libraries and will not be dumped. */
#define VM_DATA_TOP (20 * 1024 * 1024)
/* Type of an element on the list of regions to be dumped. */
struct region_t {
vm_address_t address;
vm_size_t size;
vm_prot_t protection;
vm_prot_t max_protection;
struct region_t *next;
};
/* Head and tail of the list of regions to be dumped. */
static struct region_t *region_list_head = 0;
static struct region_t *region_list_tail = 0;
/* Pointer to array of load commands. */
static struct load_command **lca;
/* Number of load commands. */
static int nlc;
/* The highest VM address of segments loaded by the input file.
Regions with addresses beyond this are assumed to be allocated
dynamically and thus require dumping. */
static vm_address_t infile_lc_highest_addr = 0;
static vm_address_t last_vmaddr = 0; /* for __LINKEDIT */
/* The lowest file offset used by the all sections in the __TEXT
segments. This leaves room at the beginning of the file to store
the Mach-O header. Check this value against header size to ensure
the added load commands for the new __DATA segments did not
overwrite any of the sections in the __TEXT segment. */
static unsigned long text_seg_lowest_offset = 0x10000000;
/* Mach header. */
static struct mach_header mh;
/* Offset at which the next load command should be written. */
static unsigned long curr_header_offset = sizeof (struct mach_header);
/* Offset at which the next segment should be written. */
static unsigned long curr_file_offset = 0;
static unsigned long pagesize;
#define ROUNDUP_TO_PAGE_BOUNDARY(x) (((x) + pagesize - 1) & ~(pagesize - 1))
static int infd, outfd;
static int in_dumped_exec = 0;
static malloc_zone_t *emacs_zone;
/* file offset of input file's data segment */
static off_t data_segment_old_fileoff = 0;
static struct segment_command *data_segment_scp;
/* Read N bytes from infd into memory starting at address DEST.
Return true if successful, false otherwise. */
static int
unexec_read (void *dest, size_t n)
{
return n == read (infd, dest, n);
}
/* Write COUNT bytes from memory starting at address SRC to outfd
starting at offset DEST. Return true if successful, false
otherwise. */
static int
unexec_write (off_t dest, const void *src, size_t count)
{
if (lseek (outfd, dest, SEEK_SET) != dest)
return 0;
return (count == write(outfd, src, count));
}
/* Write COUNT bytes of zeros to outfd starting at offset DEST.
Return true if successful, false otherwise. */
static int
unexec_write_zero (off_t dest, size_t count)
{
char buf[UNEXEC_COPY_BUFSZ];
ssize_t bytes;
bzero (buf, UNEXEC_COPY_BUFSZ);
if (lseek (outfd, dest, SEEK_SET) != dest)
return 0;
while (count > 0)
{
bytes = count > UNEXEC_COPY_BUFSZ ? UNEXEC_COPY_BUFSZ : count;
if (write (outfd, buf, bytes) != bytes)
return 0;
count -= bytes;
}
return 1;
}
/* Copy COUNT bytes from starting offset SRC in infd to starting
offset DEST in outfd. Return true if successful, false
otherwise. */
static int
unexec_copy (off_t dest, off_t src, ssize_t count)
{
ssize_t bytes_read;
ssize_t bytes_to_read;
char buf[UNEXEC_COPY_BUFSZ];
if (lseek (infd, src, SEEK_SET) != src)
return 0;
if (lseek (outfd, dest, SEEK_SET) != dest)
return 0;
while (count > 0)
{
bytes_to_read = count > UNEXEC_COPY_BUFSZ ? UNEXEC_COPY_BUFSZ : count;
bytes_read = read (infd, buf, bytes_to_read);
if (bytes_read <= 0)
return 0;
if (write (outfd, buf, bytes_read) != bytes_read)
return 0;
count -= bytes_read;
}
return 1;
}
/* Debugging and informational messages routines: */
static _Noreturn void unexec_error(char *format, ...)
{
va_list ap;
va_start (ap, format);
fprintf (stderr, "unexec: ");
vfprintf (stderr, format, ap);
fprintf (stderr, "\n");
va_end (ap);
exit (1);
}
static void
print_prot (vm_prot_t prot)
{
if (prot == VM_PROT_NONE)
printf ("none");
else
{
putchar (prot & VM_PROT_READ ? 'r' : ' ');
putchar (prot & VM_PROT_WRITE ? 'w' : ' ');
putchar (prot & VM_PROT_EXECUTE ? 'x' : ' ');
putchar (' ');
}
}
static void
print_region (vm_address_t address, vm_size_t size, vm_prot_t prot,
vm_prot_t max_prot)
{
printf ("%#10lx %#8lx ", (long) address, (long) size);
print_prot (prot);
putchar (' ');
print_prot (max_prot);
putchar ('\n');
}
static void
print_region_list ()
{
struct region_t *r;
printf (" address size prot maxp\n");
for (r = region_list_head; r; r = r->next)
print_region (r->address, r->size, r->protection, r->max_protection);
}
static void
print_regions ()
{
task_t target_task = mach_task_self ();
vm_address_t address = (vm_address_t) 0;
vm_size_t size;
struct vm_region_basic_info info;
mach_msg_type_number_t info_count = target_VM_REGION_BASIC_INFO_COUNT;
mach_port_t object_name;
printf (" address size prot maxp\n");
while (vm_region (target_task, &address, &size, VM_REGION_BASIC_INFO,
(vm_region_info_t) &info, &info_count, &object_name)
== KERN_SUCCESS && info_count == target_VM_REGION_BASIC_INFO_COUNT)
{
print_region (address, size, info.protection, info.max_protection);
if (object_name != MACH_PORT_NULL)
mach_port_deallocate (target_task, object_name);
address += size;
}
}
/* Build the list of regions that need to be dumped. Regions with
addresses above VM_DATA_TOP are omitted. Adjacent regions with
identical protection are merged. Note that non-writable regions
cannot be omitted because they some regions created at run time are
read-only. */
static void
build_region_list ()
{
task_t target_task = mach_task_self ();
vm_address_t address = (vm_address_t) 0;
vm_size_t size;
struct vm_region_basic_info info;
mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT;
mach_port_t object_name;
struct region_t *r;
#if VERBOSE
printf ("--- List of All Regions ---\n");
printf (" address size prot maxp\n");
#endif
while (vm_region (target_task, &address, &size, VM_REGION_BASIC_INFO,
(vm_region_info_t) &info, &info_count, &object_name)
== KERN_SUCCESS && info_count == VM_REGION_BASIC_INFO_COUNT)
{
/* Done when we reach addresses of shared libraries, which are
loaded in high memory. */
if (address >= VM_DATA_TOP)
break;
#if VERBOSE
print_region (address, size, info.protection, info.max_protection);
#endif
/* If a region immediately follows the previous one (the one
most recently added to the list) and has identical
protection, merge it with the latter. Otherwise create a
new list element for it. */
if (region_list_tail
&& info.protection == region_list_tail->protection
&& info.max_protection == region_list_tail->max_protection
&& region_list_tail->address + region_list_tail->size == address)
{
region_list_tail->size += size;
}
else
{
r = (struct region_t *) malloc (sizeof (struct region_t));
if (!r)
unexec_error ("cannot allocate region structure");
r->address = address;
r->size = size;
r->protection = info.protection;
r->max_protection = info.max_protection;
r->next = 0;
if (region_list_head == 0)
{
region_list_head = r;
region_list_tail = r;
}
else
{
if (region_list_tail != NULL)
region_list_tail->next = r;
region_list_tail = r;
}
/* Deallocate (unused) object name returned by
vm_region. */
if (object_name != MACH_PORT_NULL)
mach_port_deallocate (target_task, object_name);
}
address += size;
}
printf ("--- List of Regions to be Dumped ---\n");
print_region_list ();
}
#define MAX_UNEXEC_REGIONS 400
static int num_unexec_regions;
typedef struct {
vm_range_t range;
vm_size_t filesize;
} unexec_region_info;
static unexec_region_info unexec_regions[MAX_UNEXEC_REGIONS];
static void
unexec_regions_recorder (task_t task, void *rr, unsigned type,
vm_range_t *ranges, unsigned num)
{
while (num && num_unexec_regions < MAX_UNEXEC_REGIONS)
{
unexec_regions[num_unexec_regions].filesize = ranges->size;
unexec_regions[num_unexec_regions++].range = *ranges;
printf ("%#10lx (sz: %#8lx)\n", (long) (ranges->address),
(long) (ranges->size));
ranges++; num--;
}
}
static kern_return_t
unexec_reader (task_t task, vm_address_t address, vm_size_t size, void **ptr)
{
*ptr = (void *) address;
return KERN_SUCCESS;
}
static void
find_emacs_zone_regions ()
{
num_unexec_regions = 0;
emacs_zone->introspect->enumerator (mach_task_self(), 0,
MALLOC_PTR_REGION_RANGE_TYPE,
(vm_address_t) emacs_zone,
unexec_reader,
unexec_regions_recorder);
if (num_unexec_regions == MAX_UNEXEC_REGIONS)
unexec_error ("find_emacs_zone_regions: too many regions");
}
static int
unexec_regions_sort_compare (const void *a, const void *b)
{
vm_address_t aa = ((unexec_region_info *) a)->range.address;
vm_address_t bb = ((unexec_region_info *) b)->range.address;
if (aa < bb)
return -1;
else if (aa > bb)
return 1;
else
return 0;
}
static void
unexec_regions_merge ()
{
int i, n;
vm_address_t begin, end;
unexec_region_info r;
long total = 0;
void *zeropage = calloc(1, pagesize);
long zerodBytes = 0L;
qsort (unexec_regions, num_unexec_regions, sizeof (unexec_regions[0]),
&unexec_regions_sort_compare);
n = 0;
r = unexec_regions[0];
if (r.range.address & (pagesize-1L)) {
begin = r.range.address;
r.range.address = r.range.address & ~(pagesize-1L);
r.range.size += begin - r.range.address;
r.filesize += begin - r.range.address;
}
for (i = 1; i < num_unexec_regions; i++) {
if ((r.range.address + r.range.size) == unexec_regions[i].range.address) {
r.filesize += unexec_regions[i].filesize;
r.range.size += unexec_regions[i].range.size;
} else { /* All segments must be a multiple of pagesize */
end = r.range.address + r.range.size;
if (end & (pagesize-1L)) {
end = ROUNDUP_TO_PAGE_BOUNDARY(end);
printf("Page (%#8lx) aligning region @%#8lx size from %#8lx to %#8lx\n",
(long)pagesize, (long)r.range.address, (long)r.range.size, (long)(end - r.range.address));
r.range.size = end - r.range.address;
r.filesize = r.range.size;
if (end == unexec_regions[i].range.address) {
r.filesize += unexec_regions[i].filesize;
r.range.size += unexec_regions[i].range.size;
continue;
}
}
/* Truncate zerod pages */
long zerodBytes = 0L;
while (r.filesize > 0) {
vm_address_t p = r.range.address + r.filesize - pagesize;
if (memcmp(p, zeropage, pagesize) == 0) {
r.filesize -= pagesize;
zerodBytes += pagesize;
} else {
break;
}
}
if (r.filesize != r.range.size) {
printf("Removed %lx zerod bytes from filesize\n", r.range.size - r.filesize);
} else if (zerodBytes) {
printf("Removed %lx zerod bytes from filesize\n", zerodBytes);
}
unexec_regions[n++] = r;
r = unexec_regions[i];
if (r.range.address & (pagesize-1L)) { /* Align beginning of unmerged region */
begin = r.range.address;
r.range.address = r.range.address & ~(pagesize-1L);
r.range.size += begin - r.range.address;
r.filesize += begin - r.range.address;
}
}
}
end = r.range.address + r.range.size;
if (end & (pagesize-1L)) {
end = ROUNDUP_TO_PAGE_BOUNDARY(end);
printf("Page (%#8lx) aligning region @%#8lx size from %#8lx to %#8lx\n",
pagesize, (long)r.range.address, (long)r.range.size, (long)(end - r.range.address));
r.range.size = end - r.range.address;
r.filesize = r.range.size;
}
/* Truncate zerod pages */
zerodBytes = 0L;
while (r.filesize > 0) {
vm_address_t p = r.range.address + r.filesize - pagesize;
if (memcmp(p, zeropage, pagesize) == 0) {
r.filesize -= pagesize;
zerodBytes += pagesize;
} else {
break;
}
}
free(zeropage);
if (r.filesize != r.range.size) {
printf("Removed %lx zerod bytes from filesize\n", r.range.size - r.filesize);
} else if (zerodBytes) {
printf("Removed %lx zerod bytes from filesize\n", zerodBytes);
}
unexec_regions[n++] = r;
num_unexec_regions = n;
}
/* More informational messages routines. */
static void
print_load_command_name (int lc)
{
switch (lc)
{
case LC_SEGMENT:
printf("LC_SEGMENT ");
break;
case LC_SYMTAB:
printf("LC_SYMTAB ");
break;
case LC_SYMSEG:
printf("LC_SYMSEG ");
break;
case LC_UNIXTHREAD:
printf("LC_UNIXTHREAD ");
break;
case LC_DYSYMTAB:
printf("LC_DYSYMTAB ");
break;
case LC_LOAD_DYLIB:
printf("LC_LOAD_DYLIB ");
break;
case LC_ID_DYLIB:
printf("LC_ID_DYLIB ");
break;
case LC_LOAD_DYLINKER:
printf("LC_LOAD_DYLINKER ");
break;
case LC_PREBOUND_DYLIB:
printf("LC_PREBOUND_DYLIB ");
break;
case LC_ROUTINES:
printf("LC_ROUTINES ");
break;
case LC_SUB_FRAMEWORK:
printf("LC_SUBFRAMEWORK ");
break;
case LC_SUB_UMBRELLA:
printf("LC_SUB_UMBRELLA ");
break;
case LC_SUB_CLIENT:
printf("LC_SUB_CLIENT ");
break;
case LC_SUB_LIBRARY:
printf("LC_SUB_LIBRARY ");
break;
case LC_TWOLEVEL_HINTS:
printf("LC_TWOLEVEL_HINTS ");
break;
case LC_PREBIND_CKSUM:
printf("LC_PREBIND_CKSUM ");
break;
case LC_LOAD_WEAK_DYLIB:
printf("LC_LOAD_WEAK_DYLIB ");
break;
case LC_SEGMENT_64:
printf("LC_SEGMENT_64 ");
break;
case LC_ROUTINES_64:
printf("LC_ROUTINES_64 ");
break;
case LC_UUID:
printf("LC_UUID ");
break;
case LC_RPATH:
printf("LC_RPATH ");
break;
case LC_CODE_SIGNATURE:
printf("LC_CODE_SIGNATURE ");
break;
case LC_SEGMENT_SPLIT_INFO:
printf("LC_SEGMENT_SPLIT_INFO ");
break;
case LC_REEXPORT_DYLIB:
printf("LC_REEXPORT_DYLIB ");
break;
case LC_LAZY_LOAD_DYLIB:
printf("LC_LAZY_LOAD_DYLIB ");
break;
case LC_ENCRYPTION_INFO:
printf("LC_ENCRYPTION_INFO ");
break;
case LC_DYLD_INFO:
printf("LC_DYLD_INFO ");
break;
case LC_DYLD_INFO_ONLY:
printf("LC_DYLD_INFO_ONLY ");
break;
#ifdef LC_LOAD_UPWARD_DYLIB
case LC_LOAD_UPWARD_DYLIB:
printf("LC_LOAD_UPWARD_DYLIB ");
break;
#endif /* LC_LOAD_UPWARD_DYLIB */
#ifdef LC_VERSION_MIN_MACOSX
case LC_VERSION_MIN_MACOSX:
printf("LC_VERSION_MIN_MACOSX ");
break;
#endif /* LC_VERSION_MIN_MACOSX */
#ifdef LC_VERSION_MIN_IPHONEOS
case LC_VERSION_MIN_IPHONEOS:
printf("LC_VERSION_MIN_IPHONEOS");
break;
#endif /* LC_VERSION_MIN_IPHONEOS */
#ifdef LC_FUNCTION_STARTS
case LC_FUNCTION_STARTS:
printf("LC_FUNCTION_STARTS ");
break;
#endif /* LC_FUNCTION_STARTS */
#ifdef LC_DYLD_ENVIRONMENT
case LC_DYLD_ENVIRONMENT:
printf("LC_DYLD_ENVIRONMENT ");
break;
#endif /* LC_DYLD_ENVIRONMENT */
#ifdef LC_SOURCE_VERSION
case LC_SOURCE_VERSION:
printf("LC_SOURCE_VERSION ");
break;
#endif /* LC_SOURCE_VERSION */
#ifdef LC_DYLIB_CODE_SIGN_DRS
case LC_DYLIB_CODE_SIGN_DRS:
printf("LC_DYLIB_CODE_SIGN_DRS ");
break;
#endif /* LC_DYLIB_CODE_SIGN_DRS */
#ifdef LC_MAIN
case LC_MAIN:
printf("LC_MAIN ");
break;
#endif /* LC_MAIN */
#ifdef LC_DATA_IN_CODE
case LC_DATA_IN_CODE:
printf("LC_DATA_IN_CODE ");
break;
#endif /* LC_DATA_IN_CODE */
default:
printf("unknown(%08x)", lc);
break;
}
}
static void
print_load_command (struct load_command *lc)
{
print_load_command_name (lc->cmd);
printf ("%8d", lc->cmdsize);
if (lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
{
struct segment_command *scp;
struct section *sectp;
int j;
scp = (struct segment_command *) lc;
printf (" %-16.16s %#10lx %#8lx\n",
scp->segname, (long) (scp->vmaddr), (long) (scp->vmsize));
sectp = (struct section *) (scp + 1);
for (j = 0; j < scp->nsects; j++)
{
printf (" %-16.16s %#10lx %#8lx (flags: %#8lx)\n",
sectp->sectname, (long) (sectp->addr), (long) (sectp->size), (long) (sectp->flags));
sectp++;
}
}
else
printf ("\n");
}
/* Read header and load commands from input file. Store the latter in
the global array lca. Store the total number of load commands in
global variable nlc. */
static void
read_load_commands ()
{
int i;
if (!unexec_read (&mh, sizeof (struct mach_header)))
unexec_error ("cannot read mach-o header");
if (mh.magic != target_MH_MAGIC)
unexec_error ("input file not in correct Mach-O format");
if (mh.filetype != MH_EXECUTE)
unexec_error ("input Mach-O file is not an executable object file");
#if VERBOSE
printf ("--- Header Information ---\n");
printf ("Magic = 0x%08x\n", mh.magic);
printf ("CPUType = %d\n", mh.cputype);
printf ("CPUSubType = %d\n", mh.cpusubtype);
printf ("FileType = 0x%x\n", mh.filetype);
printf ("NCmds = %d\n", mh.ncmds);
printf ("SizeOfCmds = %d\n", mh.sizeofcmds);
printf ("Flags = 0x%08x\n", mh.flags);
#endif
nlc = mh.ncmds;
lca = (struct load_command **) malloc (nlc * sizeof (struct load_command *));
for (i = 0; i < nlc; i++)
{
struct load_command lc;
/* Load commands are variable-size: so read the command type and
size first and then read the rest. */
if (!unexec_read (&lc, sizeof (struct load_command)))
unexec_error ("cannot read load command");
lca[i] = (struct load_command *) malloc (lc.cmdsize);
memcpy (lca[i], &lc, sizeof (struct load_command));
if (!unexec_read (lca[i] + 1, lc.cmdsize - sizeof (struct load_command)))
unexec_error ("cannot read content of load command");
if (lc.cmd == LC_SEGMENT || lc.cmd == LC_SEGMENT_64)
{
struct segment_command *scp = (struct segment_command *) lca[i];
if (scp->vmaddr + scp->vmsize > infile_lc_highest_addr)
infile_lc_highest_addr = scp->vmaddr + scp->vmsize;
if (strncmp (scp->segname, SEG_TEXT, 16) == 0)
{
struct section *sectp = (struct section *) (scp + 1);
int j;
for (j = 0; j < scp->nsects; j++)
if (sectp->offset < text_seg_lowest_offset)
text_seg_lowest_offset = sectp->offset;
}
}
}
printf ("Highest address of load commands in input file: %#8lx\n",
(long)infile_lc_highest_addr);
printf ("Lowest offset of all sections in __TEXT segment: %#8lx\n",
text_seg_lowest_offset);
printf ("--- List of Load Commands in Input File ---\n");
printf ("# cmd cmdsize name address size\n");
for (i = 0; i < nlc; i++)
{
printf ("%1d ", i);
print_load_command (lca[i]);
}
}
/* Copy a LC_SEGMENT load command other than the __DATA segment from
the input file to the output file, adjusting the file offset of the
segment and the file offsets of sections contained in it. */
static void
copy_segment (struct load_command *lc)
{
struct segment_command *scp = (struct segment_command *) lc;
unsigned long old_fileoff = scp->fileoff;
struct section *sectp;
int j;
scp->fileoff = curr_file_offset;
sectp = (struct section *) (scp + 1);
for (j = 0; j < scp->nsects; j++)
{
sectp->offset += curr_file_offset - old_fileoff;
sectp++;
}
printf ("Writing segment %-16.16s @ %#8lx (%#8lx/%#8lx @ %#10lx)\n",
scp->segname, (long) (scp->fileoff), (long) (scp->filesize),
(long) (scp->vmsize), (long) (scp->vmaddr));
if (!unexec_copy (scp->fileoff, old_fileoff, scp->filesize))
unexec_error ("cannot copy segment from input to output file");
curr_file_offset += ROUNDUP_TO_PAGE_BOUNDARY (scp->filesize);
if (!unexec_write (curr_header_offset, lc, lc->cmdsize))
unexec_error ("cannot write load command to header");
curr_header_offset += lc->cmdsize;
}
/* Copy a LC_SEGMENT load command for the __DATA segment in the input
file to the output file. We assume that only one such segment load
command exists in the input file and it contains the sections
__data, __bss, __common, __la_symbol_ptr, __nl_symbol_ptr, and
__dyld. The first three of these should be dumped from memory and
the rest should be copied from the input file. Note that the
sections __bss and __common contain no data in the input file
because their flag fields have the value S_ZEROFILL. Dumping these
from memory makes it necessary to adjust file offset fields in
subsequently dumped load commands. Then, create new __DATA segment
load commands for regions on the region list other than the one
corresponding to the __DATA segment in the input file. */
static void
copy_data_segment (struct load_command *lc)
{
struct segment_command *scp = (struct segment_command *) lc;
struct section *sectp;
int j;
unsigned long header_offset, old_file_offset;
/* The new filesize of the segment is set to its vmsize because data
blocks for segments must start at region boundaries. Note that
this may leave unused locations at the end of the segment data
block because the total of the sizes of all sections in the
segment is generally smaller than vmsize. */
scp->filesize = scp->vmsize;
printf ("Writing segment %-16.16s @ %#8lx (%#8lx/%#8lx @ %#10lx)\n",
scp->segname, curr_file_offset, (long)(scp->filesize),
(long)(scp->vmsize), (long) (scp->vmaddr));
/* Offsets in the output file for writing the next section structure
and segment data block, respectively. */
header_offset = curr_header_offset + sizeof (struct segment_command);
sectp = (struct section *) (scp + 1);
for (j = 0; j < scp->nsects; j++)
{
old_file_offset = sectp->offset;
sectp->offset = sectp->addr - scp->vmaddr + curr_file_offset;
/* The __data section is dumped from memory. The __bss and
__common sections are also dumped from memory but their flag
fields require changing (from S_ZEROFILL to S_REGULAR). The
other three kinds of sections are just copied from the input
file. */
unsigned char sect_type = sectp->flags & SECTION_TYPE;
switch (sect_type) {
case S_LAZY_SYMBOL_POINTERS:
case S_NON_LAZY_SYMBOL_POINTERS:
case S_CSTRING_LITERALS:
if (!unexec_copy (sectp->offset, old_file_offset, sectp->size))
unexec_error ("cannot copy section %s", sectp->sectname);
if (!unexec_write (header_offset, sectp, sizeof (struct section)))
unexec_error ("cannot write section %s's header", sectp->sectname);
break;
case S_REGULAR:
if (strncmp (sectp->sectname, "__const", 16) == 0
|| strncmp(sectp->sectname, "__program_vars", 16) == 0) {
if (!unexec_copy (sectp->offset, old_file_offset, sectp->size))
unexec_error ("cannot copy section %s", sectp->sectname);
} else {
if (!unexec_write (sectp->offset, (void *)sectp->addr, sectp->size))
unexec_error ("cannot write section %s", sectp->sectname);
}
if (!unexec_write (header_offset, sectp, sizeof (struct section)))
unexec_error ("cannot write section %s's header", sectp->sectname);
break;
case S_ZEROFILL:
sectp->flags = S_REGULAR;
if (strncmp (sectp->sectname, SECT_BSS, 16) == 0) {
extern char *my_endbss_static;
unsigned long my_size;
/* Clear uninitialized local variables in statically linked
libraries. In particular, function pointers stored by
libSystemStub.a, which is introduced in Mac OS X 10.4 for
binary compatibility with respect to long double, are
cleared so that they will be reinitialized when the
dumped binary is executed on other versions of OS. */
my_size = (unsigned long)my_endbss_static - sectp->addr;
if (!(sectp->addr <= (unsigned long)my_endbss_static
&& my_size <= sectp->size))
unexec_error ("my_endbss_static is not in section %s",
sectp->sectname);
if (!unexec_write (sectp->offset, (void *) sectp->addr, my_size))
unexec_error ("cannot write section %s", sectp->sectname);
if (!unexec_write_zero (sectp->offset + my_size,
sectp->size - my_size))
unexec_error ("cannot write section %s", sectp->sectname);
if (!unexec_write (header_offset, sectp, sizeof (struct section)))
unexec_error ("cannot write section %s's header", sectp->sectname);
printf("copy SECT_BSS\n");
} else {
if (!unexec_write (sectp->offset, (void *) sectp->addr, sectp->size))
unexec_error ("cannot write section %s", sectp->sectname);
if (!unexec_write (header_offset, sectp, sizeof (struct section)))
unexec_error ("cannot write section %s's header", sectp->sectname);
printf("copy %s\n", sectp->sectname);
}
break;
default:
unexec_error("unrecognized section type '0x%x' '%s' in __DATA segment", sect_type, sectp->sectname);
}
printf (" section %-16.16s at %#8lx - %#8lx (sz: %#8lx)\n",
sectp->sectname, (long) (sectp->offset),
(long) (sectp->offset + sectp->size), (long) (sectp->size));
header_offset += sizeof (struct section);
sectp++;
}
curr_file_offset += ROUNDUP_TO_PAGE_BOUNDARY (scp->filesize);
if (!unexec_write (curr_header_offset, scp, sizeof (struct segment_command)))
unexec_error ("cannot write header of __DATA segment");
curr_header_offset += lc->cmdsize;
/* Create new __DATA segment load commands for regions on the region
list that do not corresponding to any segment load commands in
the input file.
*/
long total = 0;
for (j = 0; j < num_unexec_regions; j++)
{
struct segment_command sc;
sc.cmd = target_LC_SEGMENT;
sc.cmdsize = sizeof (struct segment_command);
strncpy (sc.segname, SEG_DATA, 16);