-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheckout.c
822 lines (798 loc) · 27.9 KB
/
checkout.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
/*
* 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 RLD
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xar/xar.h>
#include "stuff/ofile.h"
#include "stuff/breakout.h"
#include "stuff/rnd.h"
static void check_object(
struct arch *arch,
struct member *member,
struct object *object);
static void symbol_string_at_end(
struct arch *arch,
struct member *member,
struct object *object);
static void dyld_order(
struct arch *arch,
struct member *member,
struct object *object);
static void order_error(
struct arch *arch,
struct member *member,
char *reason);
__private_extern__
void
checkout(
struct arch *archs,
uint32_t narchs)
{
uint32_t i, j;
for(i = 0; i < narchs; i++){
if(archs[i].type == OFILE_ARCHIVE){
for(j = 0; j < archs[i].nmembers; j++){
if(archs[i].members[j].type == OFILE_Mach_O){
check_object(archs + i, archs[i].members + j,
archs[i].members[j].object);
}
}
}
else if(archs[i].type == OFILE_Mach_O){
check_object(archs + i, NULL, archs[i].object);
}
}
}
static
void
check_object(
struct arch *arch,
struct member *member,
struct object *object)
{
uint32_t i, ncmds, flags;
struct load_command *lc;
struct segment_command *sg;
struct segment_command_64 *sg64;
struct dylib_command *dl_id;
/*
* Set up the symtab load command field and link edit segment fields in
* the object structure.
*/
object->st = NULL;
object->dyst = NULL;
object->hints_cmd = NULL;
object->code_sig_cmd = NULL;
object->split_info_cmd = NULL;
object->func_starts_info_cmd = NULL;
object->data_in_code_cmd = NULL;
object->code_sign_drs_cmd = NULL;
object->link_opt_hint_cmd = NULL;
object->dyld_info = NULL;
object->dyld_exports_trie = NULL;
object->dyld_chained_fixups = NULL;
object->encryption_info_command = NULL;
object->encryption_info_command64 = NULL;
object->seg_bitcode = NULL;
object->seg_bitcode64 = NULL;
object->seg_linkedit = NULL;
object->seg_linkedit64 = NULL;
object->notes = NULL;
object->nnote = 0;
dl_id = NULL;
lc = object->load_commands;
if(object->mh != NULL){
ncmds = object->mh->ncmds;
flags = object->mh->flags;
}
else{
ncmds = object->mh64->ncmds;
flags = object->mh64->flags;
}
for(i = 0; i < ncmds; i++){
if(lc->cmd == LC_SYMTAB){
if(object->st != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_SYMTAB load command): ");
object->st = (struct symtab_command *)lc;
}
else if(lc->cmd == LC_DYSYMTAB){
if(object->dyst != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_DYSYMTAB load command): ");
object->dyst = (struct dysymtab_command *)lc;
}
else if(lc->cmd == LC_TWOLEVEL_HINTS){
if(object->hints_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_TWOLEVEL_HINTS load command): ");
object->hints_cmd = (struct twolevel_hints_command *)lc;
}
else if(lc->cmd == LC_CODE_SIGNATURE){
if(object->code_sig_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_CODE_SIGNATURE load command): ");
object->code_sig_cmd = (struct linkedit_data_command *)lc;
}
else if(lc->cmd == LC_SEGMENT_SPLIT_INFO){
if(object->split_info_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_SEGMENT_SPLIT_INFO load command): ");
object->split_info_cmd = (struct linkedit_data_command *)lc;
}
else if(lc->cmd == LC_FUNCTION_STARTS){
if(object->func_starts_info_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_FUNCTION_STARTS load command): ");
object->func_starts_info_cmd =
(struct linkedit_data_command *)lc;
}
else if(lc->cmd == LC_DATA_IN_CODE){
if(object->data_in_code_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_DATA_IN_CODE load command): ");
object->data_in_code_cmd =
(struct linkedit_data_command *)lc;
}
else if(lc->cmd == LC_DYLIB_CODE_SIGN_DRS){
if(object->code_sign_drs_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_DYLIB_CODE_SIGN_DRS load command): ");
object->code_sign_drs_cmd =
(struct linkedit_data_command *)lc;
}
else if(lc->cmd == LC_LINKER_OPTIMIZATION_HINT){
if(object->link_opt_hint_cmd != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_LINKER_OPTIMIZATION_HINT load command): ");
object->link_opt_hint_cmd =
(struct linkedit_data_command *)lc;
}
else if((lc->cmd == LC_DYLD_INFO) ||(lc->cmd == LC_DYLD_INFO_ONLY)){
if(object->dyld_info != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_DYLD_INFO load command): ");
object->dyld_info = (struct dyld_info_command *)lc;
}
else if(lc->cmd == LC_DYLD_EXPORTS_TRIE){
if(object->dyld_exports_trie != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_DYLD_EXPORTS_TRIE load command): ");
object->dyld_exports_trie =
(struct linkedit_data_command *)lc;
}
else if(lc->cmd == LC_DYLD_CHAINED_FIXUPS){
if(object->dyld_chained_fixups != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_DYLD_CHAINED_FIXUPS load command): ");
object->dyld_chained_fixups =
(struct linkedit_data_command *)lc;
}
else if (lc->cmd == LC_ENCRYPTION_INFO) {
if (object->encryption_info_command != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_ENCRYPTION_INFO load command): ");
object->encryption_info_command =
(struct encryption_info_command*)lc;
}
else if (lc->cmd == LC_ENCRYPTION_INFO_64) {
if (object->encryption_info_command64 != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_ENCRYPTION_INFO_64 load command): ");
object->encryption_info_command64 =
(struct encryption_info_command_64*)lc;
}
else if(lc->cmd == LC_SEGMENT){
sg = (struct segment_command *)lc;
if(strcmp(sg->segname, SEG_LINKEDIT) == 0){
if(object->seg_linkedit != NULL)
fatal_arch(arch, member, "malformed file (more than "
"one " SEG_LINKEDIT "segment): ");
object->seg_linkedit = sg;
}
else if(strcmp(sg->segname, "__LLVM") == 0){
if(object->seg_bitcode != NULL)
fatal_arch(arch, member, "malformed file (more than "
"one __LLVM segment): ");
object->seg_bitcode = sg;
}
}
else if(lc->cmd == LC_SEGMENT_64){
sg64 = (struct segment_command_64 *)lc;
if(strcmp(sg64->segname, SEG_LINKEDIT) == 0){
if(object->seg_linkedit64 != NULL)
fatal_arch(arch, member, "malformed file (more than "
"one " SEG_LINKEDIT "segment): ");
object->seg_linkedit64 = sg64;
}
else if(strcmp(sg64->segname, "__LLVM") == 0){
if(object->seg_bitcode64 != NULL)
fatal_arch(arch, member, "malformed file (more than "
"one __LLVM segment): ");
object->seg_bitcode64 = sg64;
}
}
else if(lc->cmd == LC_ID_DYLIB){
if(dl_id != NULL)
fatal_arch(arch, member, "malformed file (more than one "
"LC_ID_DYLIB load command): ");
dl_id = (struct dylib_command *)lc;
if(dl_id->dylib.name.offset >= dl_id->cmdsize)
fatal_arch(arch, member, "malformed file (name.offset of "
"load command %u extends past the end of the load "
"command): ", i);
}
else if (lc->cmd == LC_NOTE) {
object->notes =
(struct note_command**)
realloc(object->notes, sizeof(struct note_command*) *
(object->nnote + 1));
object->notes[object->nnote++] = (struct note_command*)lc;
}
lc = (struct load_command *)((char *)lc + lc->cmdsize);
}
if((object->mh_filetype == MH_DYLIB ||
(object->mh_filetype == MH_DYLIB_STUB && ncmds > 0)) &&
dl_id == NULL)
fatal_arch(arch, member, "malformed file (no LC_ID_DYLIB load "
"command in %s file): ", object->mh_filetype == MH_DYLIB ?
"MH_DYLIB" : "MH_DYLIB_STUB");
if(object->hints_cmd != NULL){
if(object->dyst == NULL && object->hints_cmd->nhints != 0)
fatal_arch(arch, member, "malformed file (LC_TWOLEVEL_HINTS "
"load command present without an LC_DYSYMTAB load command):");
if(object->hints_cmd->nhints != 0 &&
object->hints_cmd->nhints != object->dyst->nundefsym)
fatal_arch(arch, member, "malformed file (LC_TWOLEVEL_HINTS "
"load command's nhints does not match LC_DYSYMTAB load "
"command's nundefsym):");
}
/*
* For objects without a dynamic symbol table check to see that the
* string table is at the end of the file and that the symbol table is
* just before it.
*/
if(object->dyst == NULL){
symbol_string_at_end(arch, member, object);
}
else{
/*
* This file has a dynamic symbol table command. We handle three
* cases, a dynamic shared library, a file for the dynamic linker,
* and a relocatable object file. Since it has a dynamic symbol
* table command it could have an indirect symbol table.
*/
if(object->mh_filetype == MH_DYLIB /* ||
object->mh_filetype == MH_DYLIB_STUB */ ){
/*
* This is a dynamic shared library.
* The order of the symbolic info is:
* local relocation entries
* symbol table
* local symbols
* defined external symbols
* undefined symbols
* two-level namespace hints
* external relocation entries
* indirect symbol table
* table of contents
* module table
* reference table
* string table
* strings for external symbols
* strings for local symbols
* code signature data (16 byte aligned)
*/
dyld_order(arch, member, object);
}
else if(flags & MH_DYLDLINK ||
object->mh_filetype == MH_KEXT_BUNDLE){
/*
* This is a file for the dynamic linker (output of ld(1) with
* -output_for_dyld . That is the relocation entries are split
* into local and external and hanging off the dysymtab not off
* the sections.
* The order of the symbolic info is:
* local relocation entries
* symbol table
* local symbols (in order as appeared in stabs)
* defined external symbols (sorted by name)
* undefined symbols (sorted by name)
* external relocation entries
* indirect symbol table
* string table
* strings for external symbols
* strings for local symbols
* code signature data (16 byte aligned)
*/
dyld_order(arch, member, object);
}
else{
/*
* This is a relocatable object file either the output of the
* assembler or output of ld(1) with -r. For the output of
* the assembler:
* The order of the symbolic info is:
* relocation entries (by section)
* indirect symbol table
* symbol table
* local symbols (in order as appeared in stabs)
* defined external symbols (sorted by name)
* undefined symbols (sorted by name)
* string table
* strings for external symbols
* strings for local symbols
* With this order the symbol table can be replaced and the
* relocation entries and the indirect symbol table entries
* can be updated in the file and not moved.
* For the output of ld -r:
* The order of the symbolic info is:
* relocation entries (by section)
* symbol table
* local symbols (in order as appeared in stabs)
* defined external symbols (sorted by name)
* undefined symbols (sorted by name)
* indirect symbol table
* string table
* strings for external symbols
* strings for local symbols
* code signature
*/
symbol_string_at_end(arch, member, object);
}
}
}
static
void
dyld_order(
struct arch *arch,
struct member *member,
struct object *object)
{
uint64_t offset, rounded_offset;
uint32_t isym;
if(object->mh != NULL){
if(object->seg_linkedit == NULL)
fatal_arch(arch, member, "malformed file (no " SEG_LINKEDIT
" segment): ");
if(object->seg_linkedit->filesize != 0 &&
object->seg_linkedit->fileoff +
object->seg_linkedit->filesize != object->object_size)
fatal_arch(arch, member, "the " SEG_LINKEDIT " segment "
"does not cover the end of the file (can't "
"be processed) in: ");
offset = object->seg_linkedit->fileoff;
if(object->seg_bitcode != NULL){
if(object->seg_bitcode->filesize < sizeof(struct xar_header))
fatal_arch(arch, member, "the __LLVM segment too small "
"(less than sizeof(struct xar_header)) in: ");
if(object->seg_bitcode->fileoff +
object->seg_bitcode->filesize != offset)
fatal_arch(arch, member, "the __LLVM segment not directly "
"before the " SEG_LINKEDIT " segment in: ");
if(object->seg_bitcode->vmaddr +
object->seg_bitcode->vmsize != object->seg_linkedit->vmaddr)
fatal_arch(arch, member, "the __LLVM segment's vmaddr plus "
"vmsize not directly before the vmaddr of the "
SEG_LINKEDIT " segment in: ");
}
}
else{
if(object->seg_linkedit64 == NULL)
fatal_arch(arch, member, "malformed file (no " SEG_LINKEDIT
" segment): ");
if(object->seg_linkedit64->filesize != 0 &&
object->seg_linkedit64->fileoff +
object->seg_linkedit64->filesize != object->object_size)
fatal_arch(arch, member, "the " SEG_LINKEDIT " segment "
"does not cover the end of the file (can't "
"be processed) in: ");
offset = object->seg_linkedit64->fileoff;
if(object->seg_bitcode64 != NULL){
if(object->seg_bitcode64->filesize < sizeof(struct xar_header))
fatal_arch(arch, member, "the __LLVM segment too small "
"(less than sizeof(struct xar_header)) in: ");
if(object->seg_bitcode64->fileoff +
object->seg_bitcode64->filesize != offset)
fatal_arch(arch, member, "the __LLVM segment not directly "
"before the " SEG_LINKEDIT " segment in: ");
if(object->seg_bitcode64->vmaddr +
object->seg_bitcode64->vmsize !=
object->seg_linkedit64->vmaddr)
fatal_arch(arch, member, "the __LLVM segment's vmaddr plus "
"vmsize not directly before the vmaddr of the "
SEG_LINKEDIT " segment in: ");
}
}
if(object->dyld_info != NULL){
/* dyld_info starts at beginning of __LINKEDIT */
if (object->dyld_info->rebase_off != 0){
if (object->dyld_info->rebase_off != offset)
order_error(arch, member, "dyld_info "
"out of place");
}
else if (object->dyld_info->bind_off != 0){
if (object->dyld_info->bind_off != offset)
order_error(arch, member, "dyld_info "
"out of place");
}
else if(object->dyld_info->export_off != 0){
if(object->dyld_info->export_off != offset &&
object->dyld_info->weak_bind_size != 0 &&
object->dyld_info->lazy_bind_size != 0)
order_error(arch, member, "dyld_info "
"out of place");
}
/* update offset to end of dyld_info contents */
if (object->dyld_info->export_size != 0)
offset = object->dyld_info->export_off +
object->dyld_info->export_size;
else if (object->dyld_info->lazy_bind_size != 0)
offset = object->dyld_info->lazy_bind_off +
object->dyld_info->lazy_bind_size;
else if (object->dyld_info->weak_bind_size != 0)
offset = object->dyld_info->weak_bind_off +
object->dyld_info->weak_bind_size;
else if (object->dyld_info->bind_size != 0)
offset = object->dyld_info->bind_off +
object->dyld_info->bind_size;
else if (object->dyld_info->rebase_size != 0)
offset = object->dyld_info->rebase_off +
object->dyld_info->rebase_size;
}
if(object->dyld_chained_fixups != NULL){
/* dyld_chained_fixups starts at beginning of __LINKEDIT */
if (object->dyld_chained_fixups->dataoff != 0) {
if (object->dyld_chained_fixups->dataoff != offset)
order_error(arch, member, "dyld chained fixups "
"out of place");
offset = object->dyld_chained_fixups->dataoff +
object->dyld_chained_fixups->datasize;
}
}
if(object->dyld_exports_trie != NULL){
if (object->dyld_exports_trie->dataoff != 0) {
if (object->dyld_exports_trie->dataoff != offset)
order_error(arch, member, "dyld exports trie "
"out of place");
offset = object->dyld_exports_trie->dataoff +
object->dyld_exports_trie->datasize;
}
}
if(object->dyst->nlocrel != 0){
if(object->dyst->locreloff != offset)
order_error(arch, member, "local relocation entries "
"out of place");
offset += object->dyst->nlocrel *
sizeof(struct relocation_info);
}
if(object->split_info_cmd != NULL){
if(object->split_info_cmd->dataoff != 0 &&
object->split_info_cmd->dataoff != offset)
order_error(arch, member, "split info data out of place");
offset += object->split_info_cmd->datasize;
}
if(object->func_starts_info_cmd != NULL){
if(object->func_starts_info_cmd->dataoff != 0 &&
object->func_starts_info_cmd->dataoff != offset)
order_error(arch, member, "function starts data out of place");
offset += object->func_starts_info_cmd->datasize;
}
if(object->data_in_code_cmd != NULL){
if(object->data_in_code_cmd->dataoff != 0 &&
object->data_in_code_cmd->dataoff != offset)
order_error(arch, member, "data in code info out of place");
offset += object->data_in_code_cmd->datasize;
}
if(object->code_sign_drs_cmd != NULL){
if(object->code_sign_drs_cmd->dataoff != 0 &&
object->code_sign_drs_cmd->dataoff != offset)
order_error(arch, member, "code signing DRs info out of place");
offset += object->code_sign_drs_cmd->datasize;
}
if(object->link_opt_hint_cmd != NULL){
if(object->link_opt_hint_cmd->dataoff != 0 &&
object->link_opt_hint_cmd->dataoff != offset)
order_error(arch, member, "linker optimization hint info out "
"of place");
offset += object->link_opt_hint_cmd->datasize;
}
if(object->st->nsyms != 0){
if(object->st->symoff != offset)
order_error(arch, member, "symbol table out of place");
if(object->mh != NULL)
offset += object->st->nsyms * sizeof(struct nlist);
else
offset += object->st->nsyms * sizeof(struct nlist_64);
}
isym = 0;
if(object->dyst->nlocalsym != 0){
if(object->dyst->ilocalsym != isym)
order_error(arch, member, "local symbols out of place");
isym += object->dyst->nlocalsym;
}
if(object->dyst->nextdefsym != 0){
if(object->dyst->iextdefsym != isym)
order_error(arch, member, "externally defined symbols out of "
"place");
isym += object->dyst->nextdefsym;
}
if(object->dyst->nundefsym != 0){
if(object->dyst->iundefsym != isym)
order_error(arch, member, "undefined symbols out of place");
isym += object->dyst->nundefsym;
}
if(object->hints_cmd != NULL && object->hints_cmd->nhints != 0){
if(object->hints_cmd->offset != offset)
order_error(arch, member, "hints table out of place");
offset += object->hints_cmd->nhints * sizeof(struct twolevel_hint);
}
if(object->dyst->nextrel != 0){
if(object->dyst->extreloff != offset)
order_error(arch, member, "external relocation entries"
" out of place");
offset += object->dyst->nextrel *
sizeof(struct relocation_info);
}
if(object->dyst->nindirectsyms != 0){
if(object->dyst->indirectsymoff != offset)
order_error(arch, member, "indirect symbol table "
"out of place");
offset += object->dyst->nindirectsyms *
sizeof(uint32_t);
}
/*
* If this is a 64-bit Mach-O file and has an odd number of indirect
* symbol table entries the next offset MAYBE rounded to a multiple of
* 8 or MAY NOT BE. This should done to keep all the tables aligned but
* was not done for 64-bit Mach-O in Mac OS X 10.4.
*/
object->input_indirectsym_pad = 0;
if(object->mh64 != NULL &&
(object->dyst->nindirectsyms % 2) != 0){
rounded_offset = rnd(offset, 8);
}
else{
rounded_offset = offset;
}
if(object->dyst->ntoc != 0){
if(object->dyst->tocoff != offset &&
object->dyst->tocoff != rounded_offset)
order_error(arch, member, "table of contents out of place");
if(object->dyst->tocoff == offset){
offset += object->dyst->ntoc *
sizeof(struct dylib_table_of_contents);
rounded_offset = offset;
}
else if(object->dyst->tocoff == rounded_offset){
object->input_indirectsym_pad = (uint32_t)(rounded_offset -
offset);
rounded_offset += object->dyst->ntoc *
sizeof(struct dylib_table_of_contents);
offset = rounded_offset;
}
}
if(object->dyst->nmodtab != 0){
if(object->dyst->modtaboff != offset &&
object->dyst->modtaboff != rounded_offset)
order_error(arch, member, "module table out of place");
if(object->mh != NULL){
offset += object->dyst->nmodtab *
sizeof(struct dylib_module);
rounded_offset = offset;
}
else{
if(object->dyst->modtaboff == offset){
offset += object->dyst->nmodtab *
sizeof(struct dylib_module_64);
rounded_offset = offset;
}
else if(object->dyst->modtaboff == rounded_offset){
object->input_indirectsym_pad = (uint32_t)(rounded_offset -
offset);
rounded_offset += object->dyst->nmodtab *
sizeof(struct dylib_module_64);
offset = rounded_offset;
}
}
}
if(object->dyst->nextrefsyms != 0){
if(object->dyst->extrefsymoff != offset &&
object->dyst->extrefsymoff != rounded_offset)
order_error(arch, member, "reference table out of place");
if(object->dyst->extrefsymoff == offset){
offset += object->dyst->nextrefsyms *
sizeof(struct dylib_reference);
rounded_offset = offset;
}
else if(object->dyst->extrefsymoff == rounded_offset){
object->input_indirectsym_pad = (uint32_t)(rounded_offset -
offset);
rounded_offset += object->dyst->nextrefsyms *
sizeof(struct dylib_reference);
offset = rounded_offset;
}
}
if(object->st->strsize != 0){
if(object->st->stroff != offset &&
object->st->stroff != rounded_offset)
order_error(arch, member, "string table out of place");
if(object->st->stroff == offset){
offset += object->st->strsize;
rounded_offset = offset;
}
else if(object->st->stroff == rounded_offset){
object->input_indirectsym_pad = (uint32_t)(rounded_offset -
offset);
rounded_offset += object->st->strsize;
offset = rounded_offset;
}
}
if(object->code_sig_cmd != NULL){
rounded_offset = rnd(rounded_offset, 16);
if(object->code_sig_cmd->dataoff != rounded_offset)
order_error(arch, member, "code signature data out of place");
rounded_offset += object->code_sig_cmd->datasize;
offset = rounded_offset;
}
if(offset != object->object_size &&
rounded_offset != object->object_size)
order_error(arch, member, "link edit information does not fill the "
SEG_LINKEDIT " segment");
}
static
void
order_error(
struct arch *arch,
struct member *member,
char *reason)
{
fatal_arch(arch, member, "file not in an order that can be processed "
"(%s): ", reason);
}
static
void
symbol_string_at_end(
struct arch *arch,
struct member *member,
struct object *object)
{
uint32_t end, sigend, strend, rounded_strend;
uint32_t indirectend, rounded_indirectend;
if(object->st != NULL && object->st->nsyms != 0){
end = object->object_size;
if(object->code_sig_cmd != NULL){
sigend = object->code_sig_cmd->dataoff +
object->code_sig_cmd->datasize;
if(sigend != end)
fatal_arch(arch, member, "code signature not at the end "
"of the file (can't be processed) in file: ");
/*
* The code signature starts at a 16 byte offset. So if the
* string table end rouned to 16 bytes is the offset where the
* code signature starts then just back up the current "end" to
* the end of the string table.
*/
end = object->code_sig_cmd->dataoff;
if(object->st->strsize != 0){
strend = object->st->stroff + object->st->strsize;
rounded_strend = (uint32_t)rnd(strend, 16);
if(object->code_sig_cmd->dataoff == rounded_strend)
end = strend;
}
}
if(object->st->strsize != 0){
strend = object->st->stroff + object->st->strsize;
/*
* Since archive member sizes are now rounded to 8 bytes the
* string table may not be exactly at the end of the
* object_size due to rounding.
*/
rounded_strend = (uint32_t)rnd(strend, 8);
if(strend != end && rounded_strend != end)
fatal_arch(arch, member, "string table not at the end "
"of the file (can't be processed) in file: ");
/*
* To make the code work that assumes the end of string table is
* at the end of the object file change the object_size to be
* the end of the string table here. This could be done at the
* end of this routine but since all the later checks are fatal
* we'll just do this here. If there is a code signature after
* string table don't do this.
*/
if(rounded_strend != strend && object->code_sig_cmd == NULL)
object->object_size = strend;
end = object->st->stroff;
}
if(object->dyst != NULL &&
object->dyst->nindirectsyms != 0 &&
object->st->nsyms != 0 &&
object->dyst->indirectsymoff > object->st->symoff){
indirectend = object->dyst->indirectsymoff +
object->dyst->nindirectsyms * sizeof(uint32_t);
/*
* If this is a 64-bit Mach-O file and has an odd number of
* indirect symbol table entries the next offset MAYBE rounded
* to a multiple of 8 or MAY NOT BE. This should done to keep
* all the tables aligned but was not done for 64-bit Mach-O in
* Mac OS X 10.4.
*/
if(object->mh64 != NULL &&
(object->dyst->nindirectsyms % 2) != 0){
rounded_indirectend = (uint32_t)rnd(indirectend, 8);
}
else{
rounded_indirectend = indirectend;
}
if(indirectend != end && rounded_indirectend != end){
fatal_arch(arch, member, "indirect symbol table does not "
"directly preceed the string table (can't be "
"processed) in file: ");
}
object->input_indirectsym_pad = end - indirectend;
end = object->dyst->indirectsymoff;
if(object->mh != NULL){
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist) != end)
fatal_arch(arch, member, "symbol table does not "
"directly preceed the indirect symbol table (can't "
"be processed) in file: ");
}
else{
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist_64) != end)
fatal_arch(arch, member, "symbol table does not "
"directly preceed the indirect symbol table (can't "
"be processed) in file: ");
}
}
else{
if(object->mh != NULL){
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist) != end)
fatal_arch(arch, member, "symbol table and string "
"table not at the end of the file (can't be "
"processed) in file: ");
}
else{
if(object->st->symoff +
object->st->nsyms * sizeof(struct nlist_64) != end)
fatal_arch(arch, member, "symbol table and string "
"table not at the end of the file (can't be "
"processed) in file: ");
}
}
if(object->seg_linkedit != NULL &&
(object->seg_linkedit->flags & SG_FVMLIB) != SG_FVMLIB &&
object->seg_linkedit->filesize != 0){
if(object->seg_linkedit->fileoff +
object->seg_linkedit->filesize != object->object_size)
fatal_arch(arch, member, "the " SEG_LINKEDIT " segment "
"does not cover the symbol and string table (can't "
"be processed) in file: ");
}
}
}
#endif /* !defined(RLD) */