-
Notifications
You must be signed in to change notification settings - Fork 3
/
redo_prebinding.c
9984 lines (9408 loc) · 295 KB
/
redo_prebinding.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@
*/
#ifndef LIBRARY_API
/*
* The redo_prebinding(1) program. This redoes the prebinding of an executable
* or dynamic library.
*
* redo_prebinding [-c|-p|-d] [-i] [-z] [-u] [-r rootdir] [-e executable_path]
* [-seg_addr_table table_file_name]
* [-seg_addr_table_filename pathname ]
* [-seg1addr address]
* [-o output_file] [-s] input_file
* -c check only and return status
* -p check only for prebound files and return status
* -d check only for dylibs and return status
* -i ignore non-prebound files
* -z zero out the prebind check sum
* -r prepend the next argument to dependent libraries
* -e replace "@executable_path" in dependent libraries with the next
* argument
* -o write the output to the next argument instead of the input_file
* -s write the output to standard output
* -u unprebind, rather than reprebind (-c, -p, -d, -e ignored)
* -seg_addr_table the next argument is the file name of the table
* -seg_addr_table_filename the next argument is the pathname to use
* instead of the install name
* -seg1addr the next argument is a hex address at which to place the
* input_file dylib
* With no -c, -p or -d it exits 0 if sucessful and 2 means it could not be
* done for reasons like a dependent library is missing. An exit of 3 is for
* the specific case when the dependent libraries are out of date with respect
* to each other.
*
* If -c, check only, is specified a 0 exit means the file's prebinding is
* up to date, 1 means it needs to be redone and 2 means it could not be checked
* for reasons like a dependent library is missing.
*
* If -p, check only for prebound files, is specified 1 exit means the file is
* a Mach-O that could be prebound and is not otherwise the exit is 0.
*
* If -d, check only for dylib files, is specified a 0 exit means the file is a
* dylib, 1 means the file is not a dylib and 2 means there is some mix in
* the architectures.
*
* The option -seg_addr_table is used when the input a dynamic library and if
* specified the table entry for the install_name (or -seg_addr_table_filename
* pathname agrument) of the dynamic library is used for checking and the
* address to slide the library to.
*
* The -seg1addr option is followed by a valid hexadecimal address at which to
* place the input dynamic library.
*
* If -u, prebinding-specific information is removed from the binary or reset.
* The -c, -p, -d, and -e arguments are ignored (as are -r, -seg_addr_table,
* -seg_addr_table_filename, and -seg1addr which are irrelevant to unprebinding).
* dylibs are slid to zero.
*/
#else /* defined(LIBRARY_API) */
/*
* The library API for redo_prebinding is defined in <mach-o/redo_prebinding.h>
* and below in the comments before each routine.
*/
#include <mach-o/redo_prebinding.h>
#endif /* defined(LIBRARY_API) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <libc.h>
#include <malloc/malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <mach-o/stab.h>
#include <mach-o/loader.h>
#include <mach-o/reloc.h>
#include <mach-o/hppa/reloc.h>
#include <mach-o/sparc/reloc.h>
#include <mach-o/ppc/reloc.h>
#include <mach-o/arm/reloc.h>
#include <stuff/breakout.h>
#include <stuff/best_arch.h>
#include <stuff/allocate.h>
#include <stuff/errors.h>
#include <stuff/rnd.h>
#include <stuff/hppa.h>
#include <stuff/execute.h>
#include <stuff/guess_short_name.h>
#include <stuff/seg_addr_table.h>
#include <stuff/macosx_deployment_target.h>
#include <mach-o/dyld.h>
#define U_ABS(l) (((int32_t)(l))<0 ? (uint32_t)(-(l)) : (l))
/* name of the program for error messages (argv[0]) */
__private_extern__ char *progname = NULL;
/* -c option, only check and return status */
static enum bool check_only = FALSE;
static enum bool seen_a_non_64_bit = FALSE;
/* -i option, ignore non-prebound files */
static enum bool ignore_non_prebound = FALSE;
/* -z option, zero out prebind checksum */
static enum bool zero_out_prebind_checksum = FALSE;
/* -p option, check for non-prebound files */
static enum bool check_for_non_prebound = FALSE;
/* -d option, check for dynamic library files */
static enum bool check_for_dylibs = FALSE;
static enum bool seen_a_dylib = FALSE;
static enum bool seen_a_non_dylib = FALSE;
/* -r option's argument, root directory to prepend to dependent libraries */
static char *root_dir = NULL;
/*
* -e option's argument, executable_path is used to replace "@executable_path
* for dependent libraries.
*/
static char *executable_path = NULL;
#ifndef LIBRARY_API
/*
* -seg_addr_table option's argument, the file name of the segment address
* table. And the parsed seg_addr_table and its size.
*/
char *seg_addr_table_name = NULL;
struct seg_addr_table *seg_addr_table = NULL;
uint32_t table_size = 0;
/*
* -seg_addr_table_filename option's argument, the pathame to use instead of the
* install name.
*/
char *seg_addr_table_filename = NULL;
#endif /* !defined(LIBRARY_API) */
/* the address the input dylib is to have or be moved to if not zero */
static uint32_t new_dylib_address = 0;
/* the address the input dylib started out at */
static uint32_t old_dylib_address = 0;
/*
* The amount to add to the old dylib address to get it to the new dylib
* address. This will remain at zero if the address is not specified to be
* changed.
*/
static uint32_t dylib_vmslide = 0;
/* -debug turn on debugging printf()'s */
static enum bool debug = FALSE;
/* -u enables the 'unprebind' operation, as opposed to redoing prebinding */
static enum bool unprebinding = FALSE;
/*
* If some architecture was processed then the output file needs to be built
* otherwise no output file is written.
*/
static enum bool arch_processed = FALSE;
/* the link state of each module */
enum link_state {
UNLINKED,
LINKED
};
/*
* These are set to the current arch's symbolic info.
*/
static struct arch *arch = NULL;
static struct arch_flag arch_flag = { 0 };
static enum bool arch_swapped = FALSE;
static char *arch_name = NULL;
static struct nlist *arch_symbols = NULL;
static struct nlist_64 *arch_symbols64 = NULL;
static uint32_t arch_nsyms = 0;
static char *arch_strings = NULL;
static uint32_t arch_strsize = 0;
static struct dylib_table_of_contents *arch_tocs = NULL;
static uint32_t arch_ntoc = 0;
static struct dylib_module *arch_mods = NULL;
static struct dylib_module_64 *arch_mods64 = NULL;
static uint32_t arch_nmodtab = 0;
static struct dylib_reference *arch_refs = NULL;
static uint32_t arch_nextrefsyms = 0;
static struct twolevel_hint *arch_hints = NULL;
static uint32_t arch_nhints = 0;
static enum link_state arch_state = LINKED;
static uint32_t arch_seg1addr = 0;
static uint32_t arch_segs_read_write_addr = 0;
static enum bool arch_split_segs = FALSE;
static struct relocation_info *arch_extrelocs = NULL;
static struct relocation_info *arch_locrelocs = NULL;
static uint32_t arch_nextrel = 0;
static uint32_t arch_nlocrel = 0;
static uint32_t *arch_indirect_symtab = NULL;
static uint32_t arch_nindirectsyms = 0;
static enum bool arch_force_flat_namespace = FALSE;
static cpu_type_t arch_cant_be_missing = 0;
/*
* These hold the dependent libraries for the arch currently being processed.
* Their link edit information is used to update the arch currently being
* processed.
*/
struct lib {
char *dylib_name;
char *file_name;
struct ofile *ofile;
dev_t dev;
ino_t ino;
struct symtab_command *st;
struct dysymtab_command *dyst;
struct routines_command *rc;
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;
enum link_state *module_states;
enum bool LC_PREBOUND_DYLIB_found;
uint32_t LC_PREBOUND_DYLIB_size;
/*
* For two-level namespace images this is the array of pointers to the
* dependent images (indexes into the libs[] array) and the count of them.
*/
uint32_t *dependent_images;
uint32_t ndependent_images;
/*
* If this is a library image which has a framework name or library name
* then this is the part that would be the umbrella name or library name
* and the size of the name. This points into the name and since framework
* and library names may have suffixes the size is needed to exclude it.
* This is only needed for two-level namespace images. umbrella_name and
* or library_name will be NULL and name_size will be 0 if there is no
* umbrella name.
*/
char *umbrella_name;
char *library_name;
uint32_t name_size;
/*
* array of pointers (indexes into the libs[] array) to sub-frameworks and
* sub-umbrellas and count
*/
enum bool sub_images_setup;
uint32_t *sub_images;
uint32_t nsub_images;
enum bool two_level_debug_printed;
};
static struct lib *libs = NULL;
static uint32_t nlibs = 0;
/*
* A fake lib struct for the arch being processed which is used if the arch
* being processed is a two-level namespace image.
*/
static struct lib arch_lib;
/*
* A fake lib struct for the missing weak libraries which is used if for
* two-level namespace images.
*/
static struct lib weak_lib;
/*
* The weak symbol used as the value for missing weak symbols.
*/
static struct nlist weak_symbol = {
{ 0 }, /* n_un.strx */
N_ABS | N_EXT, /* n_type */
NO_SECT, /* n_sect */
0, /* n_desc */
0x0, /* n_value */
};
/*
* The module used for missing weak symbols.
*/
static enum link_state weak_module = LINKED;
/*
* This is used by check_for_overlapping_segments() to create a list of segment
* for overlap checking.
*/
struct segment {
char *file_name;
struct segment_command sg;
};
#ifndef LIBRARY_API
static void usage(
void);
static char * get_install_name(
struct arch *archs,
uint32_t narchs);
#endif /* !defined(LIBRARY_API) */
static enum bool has_resource_fork(
char *filename);
static void process_archs(
struct arch *archs,
uint32_t narchs,
enum bool has_resource_fork);
static uint32_t get_dylib_address(
void);
static void process_arch(void);
static void unprebind_arch(void);
static enum bool load_archs_libraries(void);
static enum bool load_library(
char *file_name,
struct dylib_command *dl_load,
enum bool time_stamps_must_match,
uint32_t *image_pointer);
static enum bool load_dependent_libraries(void);
static void print_two_level_info(
struct lib *lib);
static enum bool setup_sub_images(
struct lib *lib,
struct mach_header *lib_mh,
struct mach_header_64 *lib_mh64);
static void check_for_overlapping_segments(
uint32_t vmslide);
static void check_overlap(
struct segment *s1,
struct segment *s2);
static void setup_symbolic_info(enum bool missing_arch);
static void swap_arch_for_output(void);
static void check_symbolic_info_tables(
char *file_name,
struct mach_header *mh,
struct mach_header_64 *mh64,
uint32_t nlibrefs,
struct symtab_command *st,
struct dysymtab_command *dyst,
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);
static void check_for_dylib_override_symbols(void);
static void check_dylibs_for_definition(
char *file_name,
char *symbol_name);
static enum bool check_dylibs_for_reference(
char *symbol_name);
/* these two variables are used by the bsearch routines */
static char *bsearch_strings = NULL;
static struct nlist *bsearch_symbols = NULL;
static int dylib_bsearch(
const char *symbol_name,
const struct dylib_table_of_contents *toc);
static int nlist_bsearch(
const char *symbol_name,
const struct nlist *symbol);
static void setup_initial_undefined_list(void);
static void link_in_need_modules(void);
/* fake index into the libs[] array to refer to the arch being processed */
#define ARCH_LIB 0xffffffff
/* fake index into the libs[] array to refer to the a missing weak library */
#define WEAK_LIB 0xfffffffe
/*
* The structure of an element in a symbol list.
*/
struct symbol_list {
char *name; /* name of the symbol */
/* for two-level references then next two fields are used */
struct nlist *symbol; /* the symbol, NULL for flat references */
uint32_t ilib; /* the library the symbol is from (index into
the libs[] array, or ARCH_LIB) */
struct symbol_list *prev; /* previous in the chain */
struct symbol_list *next; /* next in the chain */
};
/*
* The head of the undefined list. This is a circular list so it can be
* searched from start to end and so new items can be put on the end. This
* structure never has its name filled in but only serves as the head and tail
* of the list.
*/
static struct symbol_list undefined_list = {
NULL, NULL, 0, &undefined_list, &undefined_list
};
static void add_to_undefined_list(
char *name,
struct nlist *symbol,
uint32_t ilib);
static void link_library_module(
enum link_state *module_state,
struct lib *lib);
struct indr_loop_list {
struct nlist *symbol;
struct indr_loop_list *next;
};
#define NO_INDR_LOOP ((struct indr_loop_list *)1)
static struct lib *get_primary_lib(
uint32_t ilib,
struct nlist *symbol);
static struct lib *get_indr_lib(
char *symbol_name,
struct lib *lib);
static enum bool get_weak(
struct nlist *symbol);
static void lookup_symbol(
char *name,
struct lib *primary_lib,
enum bool weak,
struct nlist **symbol,
enum link_state **module_state,
struct lib **lib,
uint32_t *isub_image,
uint32_t *itoc,
struct indr_loop_list *indr_loop);
static enum bool lookup_symbol_in_arch(
char *name,
struct nlist **symbol,
enum link_state **module_state,
struct lib **lib,
uint32_t *isub_image,
uint32_t *itoc,
struct indr_loop_list *indr_loop);
static enum bool lookup_symbol_in_lib(
char *name,
struct lib *primary_lib,
struct nlist **symbol,
enum link_state **module_state,
struct lib **lib,
uint32_t *isub_image,
uint32_t *itoc,
struct indr_loop_list *indr_loop);
static void build_new_symbol_table(
uint32_t vmslide,
enum bool missing_arch);
static void setup_r_address_base(
void);
static void update_local_relocs(
uint32_t vmslide);
static void update_generic_local_relocs(
uint32_t vmslide);
static void update_hppa_local_relocs(
uint32_t vmslide);
static void update_sparc_local_relocs(
uint32_t vmslide);
static void update_ppc_local_relocs(
uint32_t vmslide);
static void update_arm_local_relocs(
uint32_t vmslide);
static void update_external_relocs(
uint32_t vmslide);
static void update_generic_external_relocs(
uint32_t vmslide);
static void update_hppa_external_relocs(
uint32_t vmslide);
static void update_sparc_external_relocs(
uint32_t vmslide);
static void update_ppc_external_relocs(
uint32_t vmslide);
static void update_arm_external_relocs(
uint32_t vmslide);
static char *contents_pointer_for_vmaddr(
uint32_t vmaddr,
uint32_t size);
static void update_symbol_pointers(
uint32_t vmslide);
static void update_self_modifying_stubs(
uint32_t vmslide);
static void reset_symbol_pointers(
uint32_t vmslide);
static void reset_self_modifying_stubs(
void);
static enum bool check_pb_la_ptr_reloc_cputype(
unsigned int reloc_type);
static void update_load_commands(
uint32_t vmslide);
static void message(
const char *format, ...)
#ifdef __GNUC__
__attribute__ ((format (printf, 1, 2)))
#endif
;
/*
* These routines are used to get/set values that might not be aligned correctly
* which are being relocated.
*/
static
inline
uint32_t
get_arch_long(
void *addr)
{
int32_t l;
memcpy(&l, addr, sizeof(uint32_t));
if(arch_swapped == TRUE)
return(SWAP_INT(l));
else
return(l);
}
static
inline
short
get_arch_short(
void *addr)
{
short s;
memcpy(&s, addr, sizeof(short));
if(arch_swapped == TRUE)
return(SWAP_SHORT(s));
else
return(s);
}
static
inline
char
get_arch_byte(
char *addr)
{
return(*addr);
}
static
inline
void
set_arch_long(
void *addr,
uint32_t value)
{
if(arch_swapped == TRUE)
value = SWAP_INT(value);
memcpy(addr, &value, sizeof(uint32_t));
}
static
inline
void
set_arch_short(
void *addr,
short value)
{
if(arch_swapped == TRUE)
value = SWAP_SHORT(value);
memcpy(addr, &value, sizeof(short));
}
static
inline
void
set_arch_byte(
char *addr,
char value)
{
*addr = value;
}
/*
* cleanup_libs() unmaps the ofiles for all the libraries in the libs[] array.
*/
static
void
cleanup_libs()
{
uint32_t i;
for(i = 0; i < nlibs; i++){
if(libs[i].ofile != NULL)
ofile_unmap(libs[i].ofile);
}
}
#ifndef LIBRARY_API
/* apple_version is created by the libstuff/Makefile */
extern char apple_version[];
char *version = apple_version;
/*
* main() see top of file for program's description and options.
*/
int
main(
int argc,
char *argv[],
char *envp[])
{
int i;
char *input_file, *output_file, *objcunique;
struct arch *archs;
uint32_t narchs;
struct stat stat_buf;
enum bool verbose, calculate_input_prebind_cksum, write_to_stdout;
unsigned short mode;
uid_t uid;
gid_t gid;
struct seg_addr_table *entry;
char *install_name, *seg1addr_str, *endp;
input_file = NULL;
output_file = NULL;
archs = NULL;
narchs = 0;
errors = 0;
verbose = FALSE;
seg1addr_str = NULL;
write_to_stdout = FALSE;
progname = argv[0];
for(i = 1; i < argc; i++){
if(argv[i][0] == '-'){
if(strcmp(argv[i], "-o") == 0){
if(write_to_stdout)
fatal("-o cannot be used with the -s option");
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(output_file != NULL)
fatal("-s cannot be used with the -o option");
write_to_stdout = TRUE;
}
else if(strcmp(argv[i], "-r") == 0){
if(i + 1 >= argc)
fatal("-r requires an argument");
if(root_dir != NULL)
fatal("only one -r option allowed");
root_dir = argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-e") == 0){
if(i + 1 >= argc)
fatal("-e requires an argument");
if(executable_path != NULL)
fatal("only one -e option allowed");
executable_path = argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-seg_addr_table") == 0){
if(i + 1 >= argc)
fatal("-seg_addr_table requires an argument");
if(seg_addr_table_name != NULL)
fatal("only one -seg_addr_table option allowed");
if(seg1addr_str != NULL)
fatal("-seg_addr_table can't be used with "
"-seg1addr");
seg_addr_table_name = argv[i + 1];
seg_addr_table = parse_seg_addr_table(argv[i+1],
argv[i], argv[i+1], &table_size);
i++;
}
else if(strcmp(argv[i], "-seg_addr_table_filename") == 0){
if(i + 1 >= argc)
fatal("-seg_addr_table_filename requires an argument");
if(seg_addr_table_filename != NULL)
fatal("only one -seg_addr_table_filename option "
"allowed");
if(seg1addr_str != NULL)
fatal("-seg_addr_table_filename can't be used with "
"-seg1addr");
seg_addr_table_filename = argv[i + 1];
i++;
}
else if(strcmp(argv[i], "-seg1addr") == 0){
if(i + 1 >= argc)
fatal("-seg1addr requires an argument");
if(seg1addr_str != NULL)
fatal("only one -seg1addr option allowed");
if(seg_addr_table_filename != NULL ||
seg_addr_table_name != NULL)
fatal("-seg1addr can't be used with -seg_addr_table"
" or -seg_addr_table_filename");
seg1addr_str = argv[i + 1];
new_dylib_address = strtoul(argv[i + 1], &endp, 16);
if(*endp != '\0')
fatal("address for %s %s not a proper "
"hexadecimal number", argv[i], argv[i + 1]);
i++;
}
else if(strcmp(argv[i], "-c") == 0){
check_only = TRUE;
}
else if(strcmp(argv[i], "-i") == 0){
ignore_non_prebound = TRUE;
}
else if(strcmp(argv[i], "-z") == 0){
zero_out_prebind_checksum = TRUE;
}
else if(strcmp(argv[i], "-p") == 0){
check_for_non_prebound = TRUE;
}
else if(strcmp(argv[i], "-d") == 0){
check_for_dylibs = TRUE;
}
else if(strcmp(argv[i], "-debug") == 0){
debug = TRUE;
}
else if(strcmp(argv[i], "-v") == 0){
verbose = TRUE;
}
else if(strcmp(argv[i], "-u") == 0){
unprebinding = TRUE;
}
else{
fprintf(stderr, "%s: unknown option: %s\n", progname,
argv[i]);
usage();
}
}
else{
if(input_file != NULL)
fatal("only one input file allowed");
input_file = argv[i];
}
}
if(input_file == NULL){
fprintf(stderr, "%s no input file specified\n", progname);
usage();
}
if(check_only + check_for_non_prebound + check_for_dylibs > 1){
fprintf(stderr, "%s only one of -c, -p or -d can be specified\n",
progname);
usage();
}
/* breakout the file for processing */
if(zero_out_prebind_checksum == TRUE)
calculate_input_prebind_cksum = FALSE;
else
calculate_input_prebind_cksum = TRUE;
breakout(input_file, &archs, &narchs, calculate_input_prebind_cksum);
if(errors)
exit(2);
/* checkout the file for processing */
checkout(archs, narchs);
/*
* If the -seg_addr_table option was specified then get the
* install_name of this binary if it is a dynamic library. If it is
* then get the entry in the table for it. There must be an entry in
* the table for it when the -seg_addr_table option is specified and
* the entry must have a non-zero address.
*/
if(seg_addr_table != NULL && unprebinding == FALSE){
install_name = get_install_name(archs, narchs);
if(install_name != NULL || seg_addr_table_filename != NULL){
if(seg_addr_table_filename != NULL)
entry = search_seg_addr_table(seg_addr_table,
seg_addr_table_filename);
else
entry = search_seg_addr_table(seg_addr_table, install_name);
if(entry == NULL){
fprintf(stderr, "%s: no entry in -seg_addr_table %s for "
"input file's (%s) %s %s\n", progname,
seg_addr_table_name, input_file,
seg_addr_table_filename != NULL ?
"-seg_addr_table_filename" : "install name:",
seg_addr_table_filename != NULL ?
seg_addr_table_filename : install_name);
exit(2);
}
if(entry->split == TRUE)
new_dylib_address = entry->segs_read_only_addr;
else
new_dylib_address = entry->seg1addr;
if(new_dylib_address == 0){
fprintf(stderr, "%s: entry in -seg_addr_table %s for "
"input file's (%s) %s %s on line %u "
"has an address of zero\n", progname,
seg_addr_table_name, input_file,
seg_addr_table_filename != NULL ?
"-seg_addr_table_filename" : "install name:",
seg_addr_table_filename != NULL ?
seg_addr_table_filename : install_name,
entry->line);
exit(2);
}
}
}
/* process the input file */
process_archs(archs, narchs, has_resource_fork(input_file));
if(errors)
exit(2);
/*
* If we are checking for dylibs and get back from process_archs() we
* either have all dylibs or all non-dylibs. So exit with 0 for dylibs
* an 1 for non-dylibs.
*/
if(check_for_dylibs == TRUE){
if(seen_a_dylib == TRUE)
exit(0);
exit(1);
}
/*
* If we are checking for non-prebound files and get back from
* process_archs() we don't have any Mach-O's that were not prebound
* so indicate this with an exit status of 0.
*/
if(check_for_non_prebound == TRUE)
exit(0);
/*
* Create an output file if we processed any of the archs and we are
* not doing checking only.
*/
if(arch_processed == TRUE){
if(check_only == TRUE)
exit(1);
if(stat(input_file, &stat_buf) == -1)
system_error("can't stat input file: %s", input_file);
mode = stat_buf.st_mode & 07777;
uid = stat_buf.st_uid;
gid = stat_buf.st_gid;
if(output_file != NULL || write_to_stdout){
if(write_to_stdout)
output_file = NULL;
writeout(archs, narchs, output_file, mode, TRUE, FALSE, FALSE,
FALSE, NULL);
if(errors){
if(write_to_stdout == FALSE)
unlink(output_file);
return(2);
}
}
else{
output_file = makestr(input_file, ".redo_prebinding", NULL);
writeout(archs, narchs, output_file, mode, TRUE, FALSE, FALSE,
FALSE, NULL);
if(errors){
unlink(output_file);
return(2);
}
if(rename(output_file, input_file) == 1)
system_error("can't move temporary file: %s to input "
"file: %s\n", output_file, input_file);
free(output_file);
output_file = NULL;
}
/*
* Run objcunique on the output.
*/
objcunique = cmd_with_prefix("objcunique");
if(stat(objcunique, &stat_buf) != -1){
reset_execute_list();
add_execute_list(objcunique);
if(output_file != NULL)
add_execute_list(output_file);
else
add_execute_list(input_file);
add_execute_list("-prebind");
if(ignore_non_prebound == TRUE)
add_execute_list("-i");
if(root_dir != NULL){
add_execute_list("-r");
add_execute_list(root_dir);
}
if(execute_list(verbose) == 0)
fatal("internal objcunique command failed");
}
/*
* Call chmod(2) to insure set-uid, set-gid and sticky bits get set.
* Then call chown to insure the file has the same owner and group
* as the original file.
*/
if(output_file != NULL){
if(chmod(output_file, mode) == -1)
system_error("can't set permissions on file: %s",
output_file);
if(chown(output_file, uid, gid) == -1)
system_error("can't set owner and group on file: %s",
output_file);
}
else if(write_to_stdout == FALSE){
if(chmod(input_file, mode) == -1)
system_error("can't set permissions on file: %s",
input_file);
if(chown(input_file, uid, gid) == -1)
system_error("can't set owner and group on file: %s",
input_file);
}
}
else{
if(check_only == TRUE)
exit(0);
}
/* clean-up data structures */
free_archs(archs, narchs);
if(errors)
return(2);
else
return(0);
}
/*
* usage() prints the current usage message.