-
Notifications
You must be signed in to change notification settings - Fork 11
/
strip.c
5561 lines (5329 loc) · 158 KB
/
strip.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
/*
* Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* The strip(1) and nmedit(l) program. This understands only Mach-O format
* files (with the restriction the symbol table is at the end of the file) and
* fat files with Mach-O files in them.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <libc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <mach-o/loader.h>
#include <mach-o/reloc.h>
#include <mach-o/nlist.h>
#include <mach-o/stab.h>
#include "stuff/breakout.h"
#include "stuff/allocate.h"
#include "stuff/errors.h"
#include "stuff/rnd.h"
#include "stuff/reloc.h"
#include "stuff/reloc.h"
#include "stuff/symbol_list.h"
#include "stuff/unix_standard_mode.h"
#include "stuff/execute.h"
#ifdef TRIE_SUPPORT
#include <mach-o/prune_trie.h>
#endif /* TRIE_SUPPORT */
/* These are set from the command line arguments */
__private_extern__
char *progname = NULL; /* name of the program for error messages (argv[0]) */
static char *output_file;/* name of the output file */
static char *sfile; /* filename of global symbol names to keep */
static char *Rfile; /* filename of global symbol names to remove */
static uint32_t Aflag; /* save only absolute symbols with non-zero value and
.objc_class_name_* symbols */
static uint32_t iflag; /* -i ignore symbols in -s file not in object */
#ifdef NMEDIT
static uint32_t pflag; /* make all defined global symbols private extern */
#else /* !defined(NMEDIT) */
static char *dfile; /* filename of filenames of debugger symbols to keep */
static uint32_t uflag; /* save undefined symbols */
static uint32_t rflag; /* save symbols referenced dynamically */
static uint32_t nflag; /* save N_SECT global symbols */
static uint32_t Sflag; /* -S strip only debugger symbols N_STAB */
static uint32_t xflag; /* -x strip non-globals */
static uint32_t Xflag; /* -X strip local symbols with 'L' names */
static uint32_t cflag; /* -c strip section contents from dynamic libraries
files to create stub libraries */
static uint32_t no_uuid;/* -no_uuid strip LC_UUID load commands */
static uint32_t vflag; /* -v for verbose debugging ld -r executions */
static uint32_t lflag; /* -l do ld -r executions even if it has bugs */
static uint32_t strip_all = 1;
/*
* This is set on an object by object basis if the strip_all flag is still set
* and the object is an executable that is for use with the dynamic linker.
* This has the same effect as -r and -u.
*/
static enum bool default_dyld_executable = FALSE;
#endif /* NMEDIT */
/*
* Data structures to perform selective stripping of symbol table entries.
* save_symbols is the names of the symbols from the -s <file> argument.
* remove_symbols is the names of the symbols from the -R <file> argument.
*/
static struct symbol_list *save_symbols = NULL;
static uint32_t nsave_symbols = 0;
static struct symbol_list *remove_symbols = NULL;
static uint32_t nremove_symbols = 0;
/*
* saves points to an array of uint32_t's that is allocated. This array is a
* map of old symbol indexes to new symbol indexes. The new symbol indexes are
* plus 1 and zero value means that old symbol is not in the new symbol table.
* ref_saves is used in the same way but for the reference table.
* nmedits is an array and indexed by the symbol index the value indicates if
* the symbol was edited and turned into a non-global.
*/
static int32_t *saves = NULL;
#ifndef NMEDIT
static int32_t *ref_saves = NULL;
#else
static enum bool *nmedits = NULL;
#endif
/*
* These hold pointers to the symbol, string and indirect tables being worked on
* by strip_object and strip_symtab() from an input object file or possiblity
* changed to an ld -r (-S or -x) file by make_ld_r_object().
*/
static struct nlist *symbols = NULL;
static struct nlist_64 *symbols64 = NULL;
static uint32_t nsyms = 0;
static char *strings = NULL;
static uint32_t strsize = 0;
static uint32_t *indirectsyms = NULL;
static uint32_t nindirectsyms = 0;
/*
* These hold the new symbol and string table created by strip_symtab()
* and the new counts of local, defined external and undefined symbols.
*/
static struct nlist *new_symbols = NULL;
static struct nlist_64 *new_symbols64 = NULL;
static uint32_t new_nsyms = 0;
static char *new_strings = NULL;
static uint32_t new_strsize = 0;
static uint32_t new_nlocalsym = 0;
static uint32_t new_nextdefsym = 0;
static uint32_t new_nundefsym = 0;
#if defined(TRIE_SUPPORT) && !defined(NMEDIT)
/*
* The index into the new symbols where the defined external start.
*/
static uint32_t inew_nextdefsym = 0;
#endif
/*
* These hold the new table of contents, reference table and module table for
* dylibs.
*/
static struct dylib_table_of_contents *new_tocs = NULL;
static uint32_t new_ntoc = 0;
static struct dylib_reference *new_refs = NULL;
static uint32_t new_nextrefsyms = 0;
#ifdef NMEDIT
static struct dylib_module *new_mods = NULL;
static struct dylib_module_64 *new_mods64 = NULL;
static uint32_t new_nmodtab = 0;
#endif
#ifndef NMEDIT
/*
* The list of file names to save debugging symbols from.
*/
static char **debug_filenames = NULL;
static uint32_t ndebug_filenames = 0;
struct undef_map {
uint32_t index;
struct nlist symbol;
};
struct undef_map64 {
uint32_t index;
struct nlist_64 symbol64;
};
static char *qsort_strings = NULL;
#endif /* !defined(NMEDIT) */
/* Internal routines */
static void usage(
void);
static void strip_file(
char *input_file,
struct arch_flag *arch_flags,
uint32_t narch_flags,
enum bool all_archs);
static void strip_arch(
struct arch *archs,
uint32_t narchs,
struct arch_flag *arch_flags,
uint32_t narch_flags,
enum bool all_archs);
static void strip_object(
struct arch *arch,
struct member *member,
struct object *object);
static uint32_t get_starting_syminfo_offset(
struct object *object);
static void check_object_relocs(
struct arch *arch,
struct member *member,
struct object *object,
char *segname,
char *sectname,
uint64_t sectsize,
char *contents,
struct relocation_info *relocs,
uint32_t nreloc,
struct nlist *symbols,
struct nlist_64 *symbols64,
uint32_t nsyms,
char *strings,
int32_t *missing_reloc_symbols,
enum byte_sex host_byte_sex);
static void check_indirect_symtab(
struct arch *arch,
struct member *member,
struct object *object,
uint32_t nitems,
uint32_t reserved1,
uint32_t section_type,
char *contents,
struct nlist *symbols,
struct nlist_64 *symbols64,
uint32_t nsyms,
char *strings,
int32_t *missing_reloc_symbols,
enum byte_sex host_byte_sex);
#ifndef NMEDIT
static enum bool strip_symtab(
struct arch *arch,
struct member *member,
struct object *object,
struct dylib_table_of_contents *tocs,
uint32_t ntoc,
struct dylib_module *mods,
struct dylib_module_64 *mods64,
uint32_t nmodtab,
struct dylib_reference *refs,
uint32_t nextrefsyms);
#ifdef TRIE_SUPPORT
static int prune(
const char *name);
#endif /* TRIE_SUPPORT */
static void make_ld_r_object(
struct arch *arch,
struct member *member,
struct object *object);
static void strip_LC_UUID_commands(
struct arch *arch,
struct member *member,
struct object *object);
#ifndef NMEDIT
static void strip_LC_CODE_SIGNATURE_commands(
struct arch *arch,
struct member *member,
struct object *object);
#endif /* !(NMEDIT) */
static enum bool private_extern_reference_by_module(
uint32_t symbol_index,
struct dylib_reference *refs,
uint32_t nextrefsyms);
static enum bool symbol_pointer_used(
uint32_t symbol_index,
uint32_t *indirectsyms,
uint32_t nindirectsyms);
static int cmp_qsort_undef_map(
const struct undef_map *sym1,
const struct undef_map *sym2);
static int cmp_qsort_undef_map_64(
const struct undef_map64 *sym1,
const struct undef_map64 *sym2);
#endif /* !defined(NMEDIT) */
#ifdef NMEDIT
static enum bool edit_symtab(
struct arch *arch,
struct member *member,
struct object *object,
struct nlist *symbols,
struct nlist_64 *symbols64,
uint32_t nsyms,
char *strings,
uint32_t strsize,
struct dylib_table_of_contents *tocs,
uint32_t ntoc,
struct dylib_module *mods,
struct dylib_module_64 *mods64,
uint32_t nmodtab,
struct dylib_reference *refs,
uint32_t nextrefsyms);
#endif /* NMEDIT */
#ifndef NMEDIT
static void setup_debug_filenames(
char *dfile);
static int cmp_qsort_filename(
const char **name1,
const char **name2);
static int cmp_bsearch_filename(
const char *name1,
const char **name2);
#endif /* NMEDIT */
#ifdef NMEDIT
/*
* This variable and routines are used for nmedit(1) only.
*/
static char *global_strings = NULL;
static int cmp_qsort_global(
const struct nlist **sym1,
const struct nlist **sym2);
static int cmp_qsort_global_64(
const struct nlist_64 **sym1,
const struct nlist_64 **sym2);
static int cmp_bsearch_global_stab(
const char *name,
const struct nlist **sym);
static int cmp_bsearch_global_stab_64(
const char *name,
const struct nlist_64 **sym);
static int cmp_bsearch_global(
const char *name,
const struct nlist **sym);
static int cmp_bsearch_global_64(
const char *name,
const struct nlist_64 **sym);
#endif /* NMEDIT */
/* apple_version is created by the libstuff/Makefile */
extern char apple_version[];
char *version = apple_version;
int
main(
int argc,
char *argv[],
char *envp[])
{
int i;
uint32_t j, args_left, files_specified;
struct arch_flag *arch_flags;
uint32_t narch_flags;
enum bool all_archs;
struct symbol_list *sp;
progname = argv[0];
arch_flags = NULL;
narch_flags = 0;
all_archs = FALSE;
files_specified = 0;
args_left = 1;
for (i = 1; i < argc; i++){
if(argv[i][0] == '-'){
if(argv[i][1] == '\0'){
args_left = 0;
break;
}
if(strcmp(argv[i], "-o") == 0){
if(i + 1 >= argc)
fatal("-o requires an argument");
if(output_file != NULL)
fatal("only one -o option allowed");
output_file = argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-s") == 0){
if(i + 1 >= argc)
fatal("-s requires an argument");
if(sfile != NULL)
fatal("only one -s option allowed");
sfile = argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-R") == 0){
if(i + 1 >= argc)
fatal("-R requires an argument");
if(Rfile != NULL)
fatal("only one -R option allowed");
Rfile = argv[i + 1];
i++;
}
#ifndef NMEDIT
else if(strcmp(argv[i], "-d") == 0){
if(i + 1 >= argc)
fatal("-d requires an argument");
if(dfile != NULL)
fatal("only one -d option allowed");
dfile = argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-no_uuid") == 0){
no_uuid = 1;
}
#endif /* !defined(NMEDIT) */
else if(strcmp(argv[i], "-arch") == 0){
if(i + 1 == argc){
error("missing argument(s) to %s option", argv[i]);
usage();
}
if(strcmp("all", argv[i+1]) == 0){
all_archs = TRUE;
}
else{
arch_flags = reallocate(arch_flags,
(narch_flags + 1) * sizeof(struct arch_flag));
if(get_arch_from_flag(argv[i+1],
arch_flags + narch_flags) == 0){
error("unknown architecture specification flag: "
"%s %s", argv[i], argv[i+1]);
arch_usage();
usage();
}
for(j = 0; j < narch_flags; j++){
if(arch_flags[j].cputype ==
arch_flags[narch_flags].cputype &&
(arch_flags[j].cpusubtype & ~CPU_SUBTYPE_MASK) ==
(arch_flags[narch_flags].cpusubtype &
~CPU_SUBTYPE_MASK) &&
strcmp(arch_flags[j].name,
arch_flags[narch_flags].name) == 0)
break;
}
if(j == narch_flags)
narch_flags++;
}
i++;
}
else{
for(j = 1; argv[i][j] != '\0'; j++){
switch(argv[i][j]){
#ifdef NMEDIT
case 'p':
pflag = 1;
break;
#else /* !defined(NMEDIT) */
case 'S':
Sflag = 1;
strip_all = 0;
break;
case 'X':
Xflag = 1;
strip_all = 0;
break;
case 'x':
xflag = 1;
strip_all = 0;
break;
case 'i':
iflag = 1;
break;
case 'u':
uflag = 1;
strip_all = 0;
break;
case 'r':
rflag = 1;
strip_all = 0;
break;
case 'n':
nflag = 1;
strip_all = 0;
break;
#endif /* !defined(NMEDIT) */
case 'A':
Aflag = 1;
#ifndef NMEDIT
strip_all = 0;
#endif /* !defined(NMEDIT) */
break;
#ifndef NMEDIT
case 'c':
cflag = 1;
strip_all = 0;
break;
case 'v':
vflag = 1;
break;
case 'l':
lflag = 1;
break;
#endif /* NMEDIT */
default:
error("unrecognized option: %s", argv[i]);
usage();
}
}
}
}
else
files_specified++;
}
if(args_left == 0)
files_specified += argc - (i + 1);
if(files_specified > 1 && output_file != NULL){
error("-o <filename> can only be used when one file is specified");
usage();
}
if(sfile){
setup_symbol_list(sfile, &save_symbols, &nsave_symbols);
}
#ifdef NMEDIT
else{
if(Rfile == NULL && pflag == 0){
error("-s <filename>, -R <filename> or -p argument required");
usage();
}
}
#endif /* NMEDIT */
if(Rfile){
setup_symbol_list(Rfile, &remove_symbols, &nremove_symbols);
if(sfile){
for(j = 0; j < nremove_symbols ; j++){
sp = bsearch(remove_symbols[j].name,
save_symbols, nsave_symbols,
sizeof(struct symbol_list),
(int (*)(const void *, const void *))
symbol_list_bsearch);
if(sp != NULL){
error("symbol name: %s is listed in both -s %s and -R "
"%s files (can't be both saved and removed)",
remove_symbols[j].name, sfile, Rfile);
}
}
if(errors)
exit(EXIT_FAILURE);
}
}
/* the default when no -arch flags is present is to strip all archs */
if(narch_flags == 0)
all_archs = TRUE;
#ifndef NMEDIT
if(dfile){
setup_debug_filenames(dfile);
}
#endif /* !defined(NMEDIT) */
files_specified = 0;
args_left = 1;
for (i = 1; i < argc; i++) {
if(args_left && argv[i][0] == '-'){
if(argv[i][1] == '\0')
args_left = 0;
else if(strcmp(argv[i], "-o") == 0 ||
strcmp(argv[i], "-s") == 0 ||
strcmp(argv[i], "-R") == 0 ||
#ifndef NMEDIT
strcmp(argv[i], "-d") == 0 ||
#endif /* !defined(NMEDIT) */
strcmp(argv[i], "-arch") == 0)
i++;
}
else{
char resolved_path[PATH_MAX + 1];
if(realpath(argv[i], resolved_path) == NULL)
strip_file(argv[i], arch_flags, narch_flags, all_archs);
else
strip_file(resolved_path, arch_flags,narch_flags,all_archs);
files_specified++;
}
}
if(files_specified == 0)
fatal("no files specified");
if(errors)
return(EXIT_FAILURE);
else
return(EXIT_SUCCESS);
}
static
void
usage(
void)
{
#ifndef NMEDIT
fprintf(stderr, "Usage: %s [-AnuSXx] [-] [-d filename] [-s filename] "
"[-R filename] [-o output] file [...] \n", progname);
#else /* defined(NMEDIT) */
fprintf(stderr, "Usage: %s -s filename [-R filename] [-p] [-A] [-] "
"[-o output] file [...] \n",
progname);
#endif /* NMEDIT */
exit(EXIT_FAILURE);
}
static
void
strip_file(
char *input_file,
struct arch_flag *arch_flags,
uint32_t narch_flags,
enum bool all_archs)
{
struct ofile *ofile;
struct arch *archs;
uint32_t narchs;
struct stat stat_buf;
uint32_t previous_errors;
enum bool unix_standard_mode;
int cwd_fd;
char *rename_file;
#ifndef NMEDIT
char *p;
#endif
archs = NULL;
narchs = 0;
previous_errors = errors;
errors = 0;
/* breakout the file for processing */
ofile = breakout(input_file, &archs, &narchs, FALSE);
if(errors)
return;
/* checkout the file for symbol table replacement processing */
checkout(archs, narchs);
/* process the symbols in the input file */
strip_arch(archs, narchs, arch_flags, narch_flags, all_archs);
if(errors){
free_archs(archs, narchs);
ofile_unmap(ofile);
return;
}
/* create the output file */
if(stat(input_file, &stat_buf) == -1)
system_error("can't stat input file: %s", input_file);
if(output_file != NULL){
writeout(archs, narchs, output_file, stat_buf.st_mode & 0777,
TRUE, FALSE, FALSE, NULL);
}
else{
unix_standard_mode = get_unix_standard_mode();
rename_file = NULL;
cwd_fd = -1;
#ifdef NMEDIT
output_file = makestr(input_file, ".nmedit", NULL);
#else /* !defined(NMEDIT) */
/*
* In UNIX standard conformance mode we are not allowed to replace
* a file that is not writeable.
*/
if(unix_standard_mode == TRUE &&
access(input_file, W_OK) == -1){
system_error("file: %s is not writable", input_file);
goto strip_file_return;
}
output_file = makestr(input_file, ".strip", NULL);
/*
* The UNIX standard conformance test suite expects files of
* MAXPATHLEN to work.
*/
if(strlen(output_file) >= MAXPATHLEN){
/*
* If there is a directory path in the name try to change
* the current working directory to that path.
*/
if((p = rindex(output_file, '/')) != NULL){
if((cwd_fd = open(".", O_RDONLY, 0)) == -1){
system_error("can't open current working directory");
goto strip_file_return;
}
*p = '\0';
if(chdir(output_file) == -1){
system_error("can't change current working directory "
"to: %s", output_file);
goto strip_file_return;
}
p = rindex(input_file, '/');
rename_file = makestr(p + 1, NULL);
}
/*
* Create what might be a short enough name.
*/
free(output_file);
output_file = makestr("strip.XXXXXX", NULL);
output_file = mktemp(output_file);
}
#endif /* NMEDIT */
writeout(archs, narchs, output_file, stat_buf.st_mode & 0777,
TRUE, FALSE, FALSE, NULL);
if(rename_file != NULL){
if(rename(output_file, rename_file) == -1)
system_error("can't move temporary file: %s to file: %s",
output_file, rename_file);
free(rename_file);
}
else{
if(rename(output_file, input_file) == -1)
system_error("can't move temporary file: %s to input "
"file: %s", output_file, input_file);
}
free(output_file);
output_file = NULL;
/*
* If we changed the current working directory change back to
* the previous working directory.
*/
if(cwd_fd != -1){
if(fchdir(cwd_fd) == -1)
system_error("can't change back to previous working "
"directory");
if(close(cwd_fd) == -1)
system_error("can't close previous working directory");
}
}
#ifndef NMEDIT
strip_file_return:
#endif /* !defined(NMEDIT) */
/* clean-up data structures */
free_archs(archs, narchs);
ofile_unmap(ofile);
errors += previous_errors;
}
static
void
strip_arch(
struct arch *archs,
uint32_t narchs,
struct arch_flag *arch_flags,
uint32_t narch_flags,
enum bool all_archs)
{
uint32_t i, j, k, offset, size, missing_syms;
cpu_type_t cputype;
cpu_subtype_t cpusubtype;
struct arch_flag host_arch_flag;
enum bool arch_process, any_processing, *arch_flag_processed, family;
const struct arch_flag *family_arch_flag;
/*
* Using the specified arch_flags process specified objects for those
* architecures.
*/
any_processing = FALSE;
arch_flag_processed = NULL;
if(narch_flags != 0)
arch_flag_processed = allocate(narch_flags * sizeof(enum bool));
memset(arch_flag_processed, '\0', narch_flags * sizeof(enum bool));
for(i = 0; i < narchs; i++){
/*
* Determine the architecture (cputype and cpusubtype) of arch[i]
*/
cputype = 0;
cpusubtype = 0;
if(archs[i].type == OFILE_ARCHIVE){
for(j = 0; j < archs[i].nmembers; j++){
if(archs[i].members[j].type == OFILE_Mach_O){
cputype = archs[i].members[j].object->mh_cputype;
cpusubtype = archs[i].members[j].object->mh_cpusubtype;
break;
}
}
}
else if(archs[i].type == OFILE_Mach_O){
cputype = archs[i].object->mh_cputype;
cpusubtype = archs[i].object->mh_cpusubtype;
}
else if(archs[i].fat_arch != NULL){
cputype = archs[i].fat_arch->cputype;
cpusubtype = archs[i].fat_arch->cpusubtype;
}
arch_process = FALSE;
if(all_archs == TRUE){
arch_process = TRUE;
}
else if(narch_flags != 0){
family = FALSE;
if(narch_flags == 1){
family_arch_flag =
get_arch_family_from_cputype(arch_flags[0].cputype);
if(family_arch_flag != NULL)
family = (enum bool)
((family_arch_flag->cpusubtype & ~CPU_SUBTYPE_MASK) ==
(arch_flags[0].cpusubtype & ~CPU_SUBTYPE_MASK));
}
for(j = 0; j < narch_flags; j++){
if(arch_flags[j].cputype == cputype &&
((arch_flags[j].cpusubtype & ~CPU_SUBTYPE_MASK) ==
(cpusubtype & ~CPU_SUBTYPE_MASK) ||
family == TRUE)){
arch_process = TRUE;
arch_flag_processed[j] = TRUE;
break;
}
}
}
else{
(void)get_arch_from_host(&host_arch_flag, NULL);
if(host_arch_flag.cputype == cputype &&
(host_arch_flag.cpusubtype & ~CPU_SUBTYPE_MASK) ==
(cpusubtype & ~CPU_SUBTYPE_MASK))
arch_process = TRUE;
}
if(narchs != 1 && arch_process == FALSE)
continue;
any_processing = TRUE;
/*
* Now this arch[i] has been selected to be processed so process it
* according to its type.
*/
if(archs[i].type == OFILE_ARCHIVE){
for(j = 0; j < archs[i].nmembers; j++){
if(archs[i].members[j].type == OFILE_Mach_O){
strip_object(archs + i, archs[i].members + j,
archs[i].members[j].object);
}
}
missing_syms = 0;
if(iflag == 0){
for(k = 0; k < nsave_symbols; k++){
if(save_symbols[k].seen == FALSE){
if(missing_syms == 0){
error_arch(archs + i, NULL, "symbols names "
"listed in: %s not in: ", sfile);
missing_syms = 1;
}
fprintf(stderr, "%s\n", save_symbols[k].name);
}
}
}
for(k = 0; k < nsave_symbols; k++){
save_symbols[k].seen = FALSE;
}
missing_syms = 0;
if(iflag == 0){
for(k = 0; k < nremove_symbols; k++){
if(remove_symbols[k].seen == FALSE){
if(missing_syms == 0){
error_arch(archs + i, NULL, "symbols names "
"listed in: %s not defined in: ",
Rfile);
missing_syms = 1;
}
fprintf(stderr, "%s\n", remove_symbols[k].name);
}
}
}
for(k = 0; k < nremove_symbols; k++){
remove_symbols[k].seen = FALSE;
}
/*
* Reset the library offsets and size.
*/
offset = 0;
for(j = 0; j < archs[i].nmembers; j++){
archs[i].members[j].offset = offset;
size = 0;
if(archs[i].members[j].member_long_name == TRUE){
size = rnd(archs[i].members[j].member_name_size, 8) +
(rnd(sizeof(struct ar_hdr), 8) -
sizeof(struct ar_hdr));
archs[i].toc_long_name = TRUE;
}
if(archs[i].members[j].object != NULL){
size +=
rnd(archs[i].members[j].object->object_size -
archs[i].members[j].object->input_sym_info_size +
archs[i].members[j].object->output_sym_info_size,
8);
sprintf(archs[i].members[j].ar_hdr->ar_size, "%-*ld",
(int)sizeof(archs[i].members[j].ar_hdr->ar_size),
(long)(size));
/*
* This has to be done by hand because sprintf puts a
* null at the end of the buffer.
*/
memcpy(archs[i].members[j].ar_hdr->ar_fmag, ARFMAG,
(int)sizeof(archs[i].members[j].ar_hdr->ar_fmag));
}
else{
size += archs[i].members[j].unknown_size;
}
offset += sizeof(struct ar_hdr) + size;
}
archs[i].library_size = offset;
}
else if(archs[i].type == OFILE_Mach_O){
strip_object(archs + i, NULL, archs[i].object);
}
else {
warning_arch(archs + i, NULL, "can't process non-object and "
"non-archive file: ");
return;
}
}
if(all_archs == FALSE && narch_flags != 0){
for(i = 0; i < narch_flags; i++){
if(arch_flag_processed[i] == FALSE)
error("file: %s does not contain architecture: %s",
archs[0].file_name, arch_flags[i].name);
}
free(arch_flag_processed);
}
if(any_processing == FALSE)
fatal("no processing done on input file: %s (specify a -arch flag)",
archs[0].file_name);
}
static
void
strip_object(
struct arch *arch,
struct member *member,
struct object *object)
{
enum byte_sex host_byte_sex;
uint32_t offset;
struct dylib_table_of_contents *tocs;
uint32_t ntoc;
struct dylib_module *mods;
struct dylib_module_64 *mods64;
uint32_t nmodtab;
struct dylib_reference *refs;
uint32_t nextrefsyms;
uint32_t i, j;
struct load_command *lc;
struct segment_command *sg;
struct segment_command_64 *sg64;
struct section *s;
struct section_64 *s64;
struct relocation_info *relocs;
struct scattered_relocation_info *sreloc;
int32_t missing_reloc_symbols;
uint32_t stride, section_type, nitems;
char *contents;
uint32_t dyld_info_start;
uint32_t dyld_info_end;
#ifndef NMEDIT
uint32_t flags;
uint32_t k;
#endif
uint32_t ncmds;
host_byte_sex = get_host_byte_sex();
/* Don't do anything to stub dylibs which have no load commands. */
if(object->mh_filetype == MH_DYLIB_STUB){
if((object->mh != NULL && object->mh->ncmds == 0) ||
(object->mh64 != NULL && object->mh64->ncmds == 0)){
return;
}
}
if(object->mh_filetype == MH_DSYM)
fatal_arch(arch, member, "can't process dSYM companion file: ");
if(object->st == NULL || object->st->nsyms == 0){
warning_arch(arch, member, "input object file stripped: ");
return;
}
nsyms = object->st->nsyms;
if(object->mh != NULL){
symbols = (struct nlist *)
(object->object_addr + object->st->symoff);
if(object->object_byte_sex != host_byte_sex)
swap_nlist(symbols, nsyms, host_byte_sex);
symbols64 = NULL;
}
else{
symbols = NULL;
symbols64 = (struct nlist_64 *)
(object->object_addr + object->st->symoff);
if(object->object_byte_sex != host_byte_sex)
swap_nlist_64(symbols64, nsyms, host_byte_sex);