forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kxld_object.c
2728 lines (2271 loc) · 86.7 KB
/
kxld_object.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) 2009-2014 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_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. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* 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_OSREFERENCE_LICENSE_HEADER_END@
*/
#include <string.h>
#include <sys/types.h>
#if KERNEL
#include <libkern/kernel_mach_header.h>
#include <mach/machine.h>
#include <mach/vm_param.h>
#include <mach-o/fat.h>
#else /* !KERNEL */
/* Get machine.h from the kernel source so we can support all platforms
* that the kernel supports. Otherwise we're at the mercy of the host.
*/
#include "../../osfmk/mach/machine.h"
#include <architecture/byte_order.h>
#include <mach/mach_init.h>
#include <mach-o/arch.h>
#include <mach-o/swap.h>
#endif /* KERNEL */
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <mach-o/reloc.h>
#include <os/overflow.h>
#define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
#include <AssertMacros.h>
#include "kxld_demangle.h"
#include "kxld_dict.h"
#include "kxld_reloc.h"
#include "kxld_sect.h"
#include "kxld_seg.h"
#include "kxld_srcversion.h"
#include "kxld_symtab.h"
#include "kxld_util.h"
#include "kxld_uuid.h"
#include "kxld_versionmin.h"
#include "kxld_vtable.h"
#include "kxld_splitinfolc.h"
#include "kxld_object.h"
extern boolean_t isSplitKext;
extern boolean_t isOldInterface;
/*******************************************************************************
* Data structures
*******************************************************************************/
struct kxld_object {
u_char *file; // used by old interface
u_long size; // used by old interface
const char *name;
uint32_t filetype;
cpu_type_t cputype;
cpu_subtype_t cpusubtype;
KXLDArray segs;
KXLDArray sects;
KXLDArray extrelocs;
KXLDArray locrelocs;
KXLDRelocator relocator;
KXLDuuid uuid;
KXLDversionmin versionmin;
KXLDsrcversion srcversion;
KXLDSymtab *symtab;
struct dysymtab_command *dysymtab_hdr;
KXLDsplitinfolc splitinfolc;
splitKextLinkInfo split_info;
kxld_addr_t link_addr;
u_long output_buffer_size;
boolean_t is_kernel;
boolean_t is_final_image;
boolean_t is_linked;
boolean_t got_is_created;
#if KXLD_USER_OR_OBJECT
KXLDArray *section_order;
#endif
#if KXLD_PIC_KEXTS
boolean_t include_kaslr_relocs;
#endif
#if !KERNEL
enum NXByteOrder host_order;
enum NXByteOrder target_order;
#endif
};
/*******************************************************************************
* Prototypes
*******************************************************************************/
static kern_return_t get_target_machine_info(KXLDObject *object,
cpu_type_t cputype, cpu_subtype_t cpusubtype);
static kern_return_t get_macho_slice_for_arch(KXLDObject *object,
u_char *file, u_long size);
static u_long get_macho_header_size(const KXLDObject *object);
static u_long get_macho_data_size(const KXLDObject *object) __unused;
static kern_return_t init_from_execute(KXLDObject *object);
static kern_return_t init_from_final_linked_image(KXLDObject *object,
u_int *filetype_out, struct symtab_command **symtab_hdr_out);
static boolean_t target_supports_protected_segments(const KXLDObject *object)
__attribute__((pure));
static void set_is_object_linked(KXLDObject *object);
#if KXLD_USER_OR_BUNDLE
static boolean_t target_supports_bundle(const KXLDObject *object)
__attribute((pure));
static kern_return_t init_from_bundle(KXLDObject *object);
static kern_return_t process_relocs_from_tables(KXLDObject *object);
static KXLDSeg *get_seg_by_base_addr(KXLDObject *object,
kxld_addr_t base_addr);
static kern_return_t process_symbol_pointers(KXLDObject *object);
static void add_to_ptr(u_char *symptr, kxld_addr_t val, boolean_t is_32_bit);
#endif /* KXLD_USER_OR_BUNDLE */
#if KXLD_USER_OR_OBJECT
static boolean_t target_supports_object(const KXLDObject *object)
__attribute((pure));
static kern_return_t init_from_object(KXLDObject *object);
static kern_return_t process_relocs_from_sections(KXLDObject *object);
#endif /* KXLD_USER_OR_OBJECT */
#if KXLD_PIC_KEXTS
static boolean_t target_supports_slideable_kexts(const KXLDObject *object);
#endif /* KXLD_PIC_KEXTS */
static kern_return_t export_macho_header(const KXLDObject *object, u_char *buf,
u_int ncmds, u_long *header_offset, u_long header_size);
#if KXLD_USER_OR_ILP32
static u_long get_macho_cmd_data_32(u_char *file, u_long offset,
u_int *filetype, u_int *ncmds);
static kern_return_t export_macho_header_32(const KXLDObject *object,
u_char *buf, u_int ncmds, u_long *header_offset, u_long header_size);
#endif /* KXLD_USER_OR_ILP32 */
#if KXLD_USER_OR_LP64
static u_long get_macho_cmd_data_64(u_char *file, u_long offset,
u_int *filetype, u_int *ncmds);
static kern_return_t export_macho_header_64(const KXLDObject *object,
u_char *buf, u_int ncmds, u_long *header_offset, u_long header_size);
#endif /* KXLD_USER_OR_LP64 */
#if KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON
static kern_return_t add_section(KXLDObject *object, KXLDSect **sect);
#endif /* KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON */
#if KXLD_USER_OR_COMMON
static kern_return_t resolve_common_symbols(KXLDObject *object);
#endif /* KXLD_USER_OR_COMMON */
#if KXLD_USER_OR_GOT
static boolean_t target_has_got(const KXLDObject *object) __attribute__((pure));
static kern_return_t create_got(KXLDObject *object);
static kern_return_t populate_got(KXLDObject *object);
#endif /* KXLD_USER_OR_GOT */
static KXLDSym *get_mutable_sym(const KXLDObject *object, const KXLDSym *sym);
static kern_return_t populate_kmod_info(KXLDObject *object);
/*******************************************************************************
* Prototypes that may need to be exported
*******************************************************************************/
static boolean_t kxld_object_target_needs_swap(const KXLDObject *object __unused);
static KXLDSeg * kxld_object_get_seg_by_name(const KXLDObject *object, const char *segname);
static KXLDSect * kxld_object_get_sect_by_name(const KXLDObject *object, const char *segname,
const char *sectname);
/*******************************************************************************
*******************************************************************************/
size_t
kxld_object_sizeof(void)
{
return sizeof(KXLDObject);
}
/*******************************************************************************
*******************************************************************************/
kern_return_t
kxld_object_init_from_macho(KXLDObject *object, u_char *file, u_long size,
const char *name, KXLDArray *section_order __unused,
cpu_type_t cputype, cpu_subtype_t cpusubtype, KXLDFlags flags __unused)
{
kern_return_t rval = KERN_FAILURE;
KXLDSeg * seg = NULL;
u_int i = 0;
u_char * my_file;
check(object);
check(file);
check(name);
object->name = name;
#if KXLD_USER_OR_OBJECT
object->section_order = section_order;
#endif
#if KXLD_PIC_KEXTS
object->include_kaslr_relocs = ((flags & kKXLDFlagIncludeRelocs) == kKXLDFlagIncludeRelocs);
#endif
/* Find the local architecture */
rval = get_target_machine_info(object, cputype, cpusubtype);
require_noerr(rval, finish);
/* Find the Mach-O slice for the target architecture */
rval = get_macho_slice_for_arch(object, file, size);
require_noerr(rval, finish);
if (isOldInterface) {
my_file = object->file;
}
else {
my_file = object->split_info.kextExecutable;
}
/* Allocate the symbol table */
if (!object->symtab) {
object->symtab = kxld_alloc(kxld_symtab_sizeof());
require_action(object->symtab, finish, rval=KERN_RESOURCE_SHORTAGE);
bzero(object->symtab, kxld_symtab_sizeof());
}
/* Build the relocator */
rval = kxld_relocator_init(&object->relocator,
my_file,
object->symtab, &object->sects,
object->cputype,
object->cpusubtype,
kxld_object_target_needs_swap(object));
require_noerr(rval, finish);
/* There are four types of Mach-O files that we can support:
* 1) 32-bit MH_OBJECT - Snow Leopard and earlier
* 2) 32-bit MH_KEXT_BUNDLE - Lion and Later
* 3) 64-bit MH_OBJECT - Unsupported
* 4) 64-bit MH_KEXT_BUNDLE - Snow Leopard and Later
*/
if (kxld_object_is_32_bit(object)) {
struct mach_header *mach_hdr = (struct mach_header *) ((void *) my_file);
object->filetype = mach_hdr->filetype;
} else {
struct mach_header_64 *mach_hdr = (struct mach_header_64 *) ((void *) my_file);
object->filetype = mach_hdr->filetype;
}
switch (object->filetype) {
#if KXLD_USER_OR_BUNDLE
case MH_KEXT_BUNDLE:
rval = init_from_bundle(object);
require_noerr(rval, finish);
break;
#endif /* KXLD_USER_OR_BUNDLE */
#if KXLD_USER_OR_OBJECT
case MH_OBJECT:
rval = init_from_object(object);
require_noerr(rval, finish);
break;
#endif /* KXLD_USER_OR_OBJECT */
case MH_EXECUTE:
object->is_kernel = TRUE;
rval = init_from_execute(object);
require_noerr(rval, finish);
break;
default:
rval = KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr,
kKxldLogFiletypeNotSupported, object->filetype);
goto finish;
}
if (!kxld_object_is_kernel(object)) {
for (i = 0; i < object->segs.nitems; ++i) {
seg = kxld_array_get_item(&object->segs, i);
kxld_seg_set_vm_protections(seg,
target_supports_protected_segments(object));
}
seg = kxld_object_get_seg_by_name(object, SEG_LINKEDIT);
if (seg) {
(void) kxld_seg_populate_linkedit(seg, object->symtab,
kxld_object_is_32_bit(object)
#if KXLD_PIC_KEXTS
, &object->locrelocs, &object->extrelocs,
target_supports_slideable_kexts(object)
#endif
, isOldInterface ? 0 : object->splitinfolc.datasize
);
}
}
(void) set_is_object_linked(object);
rval = KERN_SUCCESS;
finish:
return rval;
}
/*******************************************************************************
*******************************************************************************/
splitKextLinkInfo *
kxld_object_get_link_info(KXLDObject *object)
{
check(object);
return &object->split_info;
}
/*******************************************************************************
*******************************************************************************/
void
kxld_object_set_link_info(KXLDObject *object, splitKextLinkInfo *link_info)
{
check(object);
check(link_info);
object->split_info.vmaddr_TEXT = link_info->vmaddr_TEXT;
object->split_info.vmaddr_TEXT_EXEC = link_info->vmaddr_TEXT_EXEC;
object->split_info.vmaddr_DATA = link_info->vmaddr_DATA;
object->split_info.vmaddr_DATA_CONST = link_info->vmaddr_DATA_CONST;
object->split_info.vmaddr_LLVM_COV = link_info->vmaddr_LLVM_COV;
object->split_info.vmaddr_LINKEDIT = link_info->vmaddr_LINKEDIT;
return;
}
/*******************************************************************************
*******************************************************************************/
kern_return_t
get_target_machine_info(KXLDObject *object, cpu_type_t cputype __unused,
cpu_subtype_t cpusubtype __unused)
{
#if KERNEL
/* Because the kernel can only link for its own architecture, we know what
* the host and target architectures are at compile time, so we can use
* a vastly simplified version of this function.
*/
check(object);
#if defined(__x86_64__)
object->cputype = CPU_TYPE_X86_64;
/* FIXME: we need clang to provide a __x86_64h__ macro for the sub-type. Using
* __AVX2__ is a temporary solution until this is available. */
#if defined(__AVX2__)
object->cpusubtype = CPU_SUBTYPE_X86_64_H;
#else
object->cpusubtype = CPU_SUBTYPE_X86_64_ALL;
#endif
return KERN_SUCCESS;
#elif defined(__arm__)
object->cputype = CPU_TYPE_ARM;
object->cpusubtype = CPU_SUBTYPE_ARM_ALL;
return KERN_SUCCESS;
#elif defined(__arm64__)
object->cputype = CPU_TYPE_ARM64;
object->cpusubtype = CPU_SUBTYPE_ARM64_ALL;
return KERN_SUCCESS;
#else
kxld_log(kKxldLogLinking, kKxldLogErr,
kKxldLogArchNotSupported, _mh_execute_header->cputype);
return KERN_NOT_SUPPORTED;
#endif /* Supported architecture defines */
#else /* !KERNEL */
/* User-space must look up the architecture it's running on and the target
* architecture at run-time.
*/
kern_return_t rval = KERN_FAILURE;
const NXArchInfo *host_arch = NULL;
check(object);
host_arch = NXGetLocalArchInfo();
require_action(host_arch, finish, rval=KERN_FAILURE);
object->host_order = host_arch->byteorder;
/* If the user did not specify a cputype, use the local architecture.
*/
if (cputype) {
object->cputype = cputype;
object->cpusubtype = cpusubtype;
} else {
object->cputype = host_arch->cputype;
object->target_order = object->host_order;
switch (object->cputype) {
case CPU_TYPE_I386:
object->cpusubtype = CPU_SUBTYPE_I386_ALL;
break;
case CPU_TYPE_X86_64:
object->cpusubtype = CPU_SUBTYPE_X86_64_ALL;
break;
case CPU_TYPE_ARM:
object->cpusubtype = CPU_SUBTYPE_ARM_ALL;
break;
case CPU_TYPE_ARM64:
object->cpusubtype = CPU_SUBTYPE_ARM64_ALL;
break;
default:
object->cpusubtype = 0;
break;
}
}
/* Validate that we support the target architecture and record its
* endianness.
*/
switch(object->cputype) {
case CPU_TYPE_ARM:
case CPU_TYPE_ARM64:
case CPU_TYPE_I386:
case CPU_TYPE_X86_64:
object->target_order = NX_LittleEndian;
break;
default:
rval = KERN_NOT_SUPPORTED;
kxld_log(kKxldLogLinking, kKxldLogErr,
kKxldLogArchNotSupported, object->cputype);
goto finish;
}
rval = KERN_SUCCESS;
finish:
return rval;
#endif /* KERNEL */
}
/*******************************************************************************
*******************************************************************************/
static kern_return_t
get_macho_slice_for_arch(KXLDObject *object, u_char *file, u_long size)
{
kern_return_t rval = KERN_FAILURE;
struct mach_header *mach_hdr = NULL;
#if !KERNEL
struct fat_header *fat = (struct fat_header *) ((void *) file);
struct fat_arch *archs = (struct fat_arch *) &fat[1];
boolean_t swap = FALSE;
#endif /* KERNEL */
u_char *my_file = file;
u_long my_file_size = size;
check(object);
check(file);
check(size);
/* We are assuming that we will never receive a fat file in the kernel */
#if !KERNEL
require_action(size >= sizeof(*fat), finish,
rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogTruncatedMachO));
/* The fat header is always big endian, so swap if necessary */
if (fat->magic == FAT_CIGAM) {
(void) swap_fat_header(fat, object->host_order);
swap = TRUE;
}
if (fat->magic == FAT_MAGIC) {
struct fat_arch *arch = NULL;
u_long arch_size;
boolean_t ovr = os_mul_and_add_overflow(fat->nfat_arch, sizeof(*archs), sizeof(*fat), &arch_size);
require_action(!ovr && size >= arch_size,
finish,
rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogTruncatedMachO));
/* Swap the fat_arch structures if necessary */
if (swap) {
(void) swap_fat_arch(archs, fat->nfat_arch, object->host_order);
}
/* Locate the Mach-O for the requested architecture */
arch = NXFindBestFatArch(object->cputype, object->cpusubtype, archs, fat->nfat_arch);
require_action(arch, finish, rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogArchNotFound));
require_action(size >= arch->offset + arch->size, finish,
rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogTruncatedMachO));
my_file = my_file + arch->offset;
my_file_size = arch->size;
}
#endif /* !KERNEL */
/* Swap the Mach-O's headers to this architecture if necessary */
if (kxld_object_is_32_bit(object)) {
rval = validate_and_swap_macho_32(my_file, my_file_size
#if !KERNEL
, object->host_order
#endif /* !KERNEL */
);
} else {
rval = validate_and_swap_macho_64(my_file, my_file_size
#if !KERNEL
, object->host_order
#endif /* !KERNEL */
);
}
require_noerr(rval, finish);
mach_hdr = (struct mach_header *) ((void *) my_file);
require_action(object->cputype == mach_hdr->cputype, finish,
rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogTruncatedMachO));
object->cpusubtype = mach_hdr->cpusubtype; /* <rdar://problem/16008438> */
if (isOldInterface) {
object->file = my_file;
object->size = my_file_size;
}
else {
object->split_info.kextExecutable = my_file;
object->split_info.kextSize = my_file_size;
}
rval = KERN_SUCCESS;
finish:
return rval;
}
/*******************************************************************************
*******************************************************************************/
static kern_return_t
init_from_final_linked_image(KXLDObject *object, u_int *filetype_out,
struct symtab_command **symtab_hdr_out)
{
kern_return_t rval = KERN_FAILURE;
KXLDSeg *seg = NULL;
KXLDSect *sect = NULL;
struct load_command *cmd_hdr = NULL;
struct symtab_command *symtab_hdr = NULL;
struct uuid_command *uuid_hdr = NULL;
struct version_min_command *versionmin_hdr = NULL;
struct build_version_command *build_version_hdr = NULL;
struct source_version_command *source_version_hdr = NULL;
u_long base_offset = 0;
u_long offset = 0;
u_long sect_offset = 0;
u_int filetype = 0;
u_int i = 0;
u_int j = 0;
u_int segi = 0;
u_int secti = 0;
u_int nsegs = 0;
u_int nsects = 0;
u_int ncmds = 0;
u_char *my_file;
if (isOldInterface) {
my_file = object->file;
}
else {
my_file = object->split_info.kextExecutable;
}
KXLD_3264_FUNC(kxld_object_is_32_bit(object), base_offset,
get_macho_cmd_data_32, get_macho_cmd_data_64,
my_file, offset, &filetype, &ncmds);
/* First pass to count segments and sections */
offset = base_offset;
for (i = 0; i < ncmds; ++i, offset += cmd_hdr->cmdsize) {
cmd_hdr = (struct load_command *) ((void *) (my_file + offset));
switch(cmd_hdr->cmd) {
#if KXLD_USER_OR_ILP32
case LC_SEGMENT:
{
struct segment_command *seg_hdr =
(struct segment_command *) cmd_hdr;
/* Ignore segments with no vm size */
if (!seg_hdr->vmsize) continue;
++nsegs;
nsects += seg_hdr->nsects;
}
break;
#endif /* KXLD_USER_OR_ILP32 */
#if KXLD_USER_OR_LP64
case LC_SEGMENT_64:
{
struct segment_command_64 *seg_hdr =
(struct segment_command_64 *) ((void *) cmd_hdr);
/* Ignore segments with no vm size */
if (!seg_hdr->vmsize) continue;
++nsegs;
nsects += seg_hdr->nsects;
}
break;
#endif /* KXLD_USER_OR_LP64 */
default:
continue;
}
}
/* Allocate the segments and sections */
if (nsegs) {
rval = kxld_array_init(&object->segs, sizeof(KXLDSeg), nsegs);
require_noerr(rval, finish);
rval = kxld_array_init(&object->sects, sizeof(KXLDSect), nsects);
require_noerr(rval, finish);
}
/* Initialize the segments and sections */
offset = base_offset;
for (i = 0; i < ncmds; ++i, offset += cmd_hdr->cmdsize) {
cmd_hdr = (struct load_command *) ((void *) (my_file + offset));
seg = NULL;
switch(cmd_hdr->cmd) {
#if KXLD_USER_OR_ILP32
case LC_SEGMENT:
{
struct segment_command *seg_hdr =
(struct segment_command *) cmd_hdr;
/* Ignore segments with no vm size */
if (!seg_hdr->vmsize) continue;
seg = kxld_array_get_item(&object->segs, segi++);
rval = kxld_seg_init_from_macho_32(seg, seg_hdr);
require_noerr(rval, finish);
sect_offset = offset + sizeof(*seg_hdr);
}
break;
#endif /* KXLD_USER_OR_ILP32 */
#if KXLD_USER_OR_LP64
case LC_SEGMENT_64:
{
struct segment_command_64 *seg_hdr =
(struct segment_command_64 *) ((void *) cmd_hdr);
/* Ignore segments with no vm size */
if (!seg_hdr->vmsize) continue;
seg = kxld_array_get_item(&object->segs, segi++);
rval = kxld_seg_init_from_macho_64(seg, seg_hdr);
require_noerr(rval, finish);
sect_offset = offset + sizeof(*seg_hdr);
}
break;
#endif /* KXLD_USER_OR_LP64 */
case LC_SYMTAB:
symtab_hdr = (struct symtab_command *) cmd_hdr;
break;
case LC_UUID:
uuid_hdr = (struct uuid_command *) cmd_hdr;
kxld_uuid_init_from_macho(&object->uuid, uuid_hdr);
break;
case LC_VERSION_MIN_MACOSX:
case LC_VERSION_MIN_IPHONEOS:
case LC_VERSION_MIN_TVOS:
case LC_VERSION_MIN_WATCHOS:
versionmin_hdr = (struct version_min_command *) cmd_hdr;
kxld_versionmin_init_from_macho(&object->versionmin, versionmin_hdr);
break;
case LC_BUILD_VERSION:
build_version_hdr = (struct build_version_command *)cmd_hdr;
kxld_versionmin_init_from_build_cmd(&object->versionmin, build_version_hdr);
break;
case LC_SOURCE_VERSION:
source_version_hdr = (struct source_version_command *) (void *) cmd_hdr;
kxld_srcversion_init_from_macho(&object->srcversion, source_version_hdr);
break;
case LC_DYSYMTAB:
object->dysymtab_hdr = (struct dysymtab_command *) cmd_hdr;
rval = kxld_reloc_create_macho(&object->extrelocs, &object->relocator,
(struct relocation_info *) ((void *) (my_file + object->dysymtab_hdr->extreloff)),
object->dysymtab_hdr->nextrel);
require_noerr(rval, finish);
rval = kxld_reloc_create_macho(&object->locrelocs, &object->relocator,
(struct relocation_info *) ((void *) (my_file + object->dysymtab_hdr->locreloff)),
object->dysymtab_hdr->nlocrel);
require_noerr(rval, finish);
break;
case LC_UNIXTHREAD:
case LC_MAIN:
/* Don't need to do anything with UNIXTHREAD or MAIN for the kernel */
require_action(kxld_object_is_kernel(object),
finish, rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogMalformedMachO
"LC_UNIXTHREAD/LC_MAIN segment is not valid in a kext."));
break;
case LC_SEGMENT_SPLIT_INFO:
if (isSplitKext) {
struct linkedit_data_command *split_info_hdr = NULL;
split_info_hdr = (struct linkedit_data_command *) (void *) cmd_hdr;
kxld_splitinfolc_init_from_macho(&object->splitinfolc, split_info_hdr);
}
break;
case LC_NOTE:
/* binary blob of data */
break;
case LC_CODE_SIGNATURE:
case LC_DYLD_INFO:
case LC_DYLD_INFO_ONLY:
case LC_FUNCTION_STARTS:
case LC_DATA_IN_CODE:
case LC_DYLIB_CODE_SIGN_DRS:
/* Various metadata that might be stored in the linkedit segment */
break;
default:
rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogMalformedMachO
"Invalid load command type in MH_KEXT_BUNDLE kext: %u.", cmd_hdr->cmd);
goto finish;
}
if (seg) {
/* Initialize the sections */
for (j = 0; j < seg->sects.nitems; ++j, ++secti) {
sect = kxld_array_get_item(&object->sects, secti);
KXLD_3264_FUNC(kxld_object_is_32_bit(object), rval,
kxld_sect_init_from_macho_32, kxld_sect_init_from_macho_64,
sect, my_file, §_offset, secti, &object->relocator);
require_noerr(rval, finish);
/* Add the section to the segment. This will also make sure
* that the sections and segments have the same segname.
*/
rval = kxld_seg_add_section(seg, sect);
require_noerr(rval, finish);
}
rval = kxld_seg_finish_init(seg);
require_noerr(rval, finish);
}
}
if (filetype_out) *filetype_out = filetype;
if (symtab_hdr_out) *symtab_hdr_out = symtab_hdr;
object->is_final_image = TRUE;
rval = KERN_SUCCESS;
finish:
return rval;
}
/*******************************************************************************
*******************************************************************************/
static kern_return_t
init_from_execute(KXLDObject *object)
{
kern_return_t rval = KERN_FAILURE;
struct symtab_command *symtab_hdr = NULL;
u_int filetype = 0;
KXLDSeg * kernel_linkedit_seg = NULL; // used if running kernel
#if KXLD_USER_OR_OBJECT
KXLDSeg *seg = NULL;
KXLDSect *sect = NULL;
KXLDSectionName *sname = NULL;
u_int i = 0, j = 0, k = 0;
#endif /* KXLD_USER_OR_OBJECT */
u_char *my_file;
check(object);
if (isOldInterface) {
my_file = object->file;
}
else {
my_file = object->split_info.kextExecutable;
}
require_action(kxld_object_is_kernel(object), finish, rval=KERN_FAILURE);
rval = init_from_final_linked_image(object, &filetype, &symtab_hdr);
require_noerr(rval, finish);
require_action(filetype == MH_EXECUTE, finish, rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogMalformedMachO
"The kernel file is not of type MH_EXECUTE."));
/* Initialize the symbol table. If this is the running kernel
* we will work from the in-memory linkedit segment;
* otherwise we work from the whole mach-o image.
*/
#if KERNEL
kernel_linkedit_seg = kxld_object_get_seg_by_name(object, SEG_LINKEDIT);
require_action(kernel_linkedit_seg, finish, rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogMalformedMachO));
#endif
KXLD_3264_FUNC(kxld_object_is_32_bit(object), rval,
kxld_symtab_init_from_macho_32, kxld_symtab_init_from_macho_64,
object->symtab, symtab_hdr, my_file, kernel_linkedit_seg);
require_noerr(rval, finish);
#if KXLD_USER_OR_OBJECT
/* Save off the order of section names so that we can lay out kext
* sections for MH_OBJECT-based systems.
*/
if (target_supports_object(object)) {
rval = kxld_array_init(object->section_order, sizeof(KXLDSectionName),
object->sects.nitems);
require_noerr(rval, finish);
/* Copy the section names into the section_order array for future kext
* section ordering.
*/
for (i = 0, k = 0; i < object->segs.nitems; ++i) {
seg = kxld_array_get_item(&object->segs, i);
for (j = 0; j < seg->sects.nitems; ++j, ++k) {
sect = *(KXLDSect **) kxld_array_get_item(&seg->sects, j);
sname = kxld_array_get_item(object->section_order, k);
strlcpy(sname->segname, sect->segname, sizeof(sname->segname));
strlcpy(sname->sectname, sect->sectname, sizeof(sname->sectname));
}
}
}
#endif /* KXLD_USER_OR_OBJECT */
rval = KERN_SUCCESS;
finish:
return rval;
}
#if KXLD_USER_OR_BUNDLE
/*******************************************************************************
*******************************************************************************/
static boolean_t
target_supports_bundle(const KXLDObject *object __unused)
{
return TRUE;
}
/*******************************************************************************
*******************************************************************************/
static kern_return_t
init_from_bundle(KXLDObject *object)
{
kern_return_t rval = KERN_FAILURE;
struct symtab_command *symtab_hdr = NULL;
u_int filetype = 0;
u_char *my_file;
check(object);
if (isOldInterface) {
my_file = object->file;
}
else {
my_file = object->split_info.kextExecutable;
}
require_action(target_supports_bundle(object), finish,
rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr,
kKxldLogFiletypeNotSupported, MH_KEXT_BUNDLE));
rval = init_from_final_linked_image(object, &filetype, &symtab_hdr);
require_noerr(rval, finish);
require_action(filetype == MH_KEXT_BUNDLE, finish,
rval=KERN_FAILURE);
KXLD_3264_FUNC(kxld_object_is_32_bit(object), rval,
kxld_symtab_init_from_macho_32, kxld_symtab_init_from_macho_64,
object->symtab, symtab_hdr, my_file,
/* kernel_linkedit_seg */ NULL);
require_noerr(rval, finish);
rval = KERN_SUCCESS;
finish:
return rval;
}
#endif /* KXLD_USER_OR_BUNDLE */
#if KXLD_USER_OR_OBJECT
/*******************************************************************************
*******************************************************************************/
static boolean_t target_supports_object(const KXLDObject *object)
{
return (object->cputype == CPU_TYPE_I386);
}
/*******************************************************************************
*******************************************************************************/
static kern_return_t
init_from_object(KXLDObject *object)
{
kern_return_t rval = KERN_FAILURE;
struct load_command *cmd_hdr = NULL;
struct symtab_command *symtab_hdr = NULL;
struct uuid_command *uuid_hdr = NULL;
KXLDSect *sect = NULL;
u_long offset = 0;
u_long sect_offset = 0;
u_int filetype = 0;
u_int ncmds = 0;
u_int nsects = 0;
u_int i = 0;
boolean_t has_segment = FALSE;
u_char *my_file;
check(object);
if (isOldInterface) {
my_file = object->file;
}
else {
my_file = object->split_info.kextExecutable;
}
require_action(target_supports_object(object),
finish, rval=KERN_FAILURE;
kxld_log(kKxldLogLinking, kKxldLogErr,
kKxldLogFiletypeNotSupported, MH_OBJECT));
KXLD_3264_FUNC(kxld_object_is_32_bit(object), offset,
get_macho_cmd_data_32, get_macho_cmd_data_64,
my_file, offset, &filetype, &ncmds);
require_action(filetype == MH_OBJECT, finish, rval=KERN_FAILURE);
/* MH_OBJECTs use one unnamed segment to contain all of the sections. We
* loop over all of the load commands to initialize the structures we
* expect. Then, we'll use the unnamed segment to get to all of the
* sections, and then use those sections to create the actual segments.
*/
for (; i < ncmds; ++i, offset += cmd_hdr->cmdsize) {
cmd_hdr = (struct load_command *) ((void *) (my_file + offset));
switch(cmd_hdr->cmd) {
#if KXLD_USER_OR_ILP32
case LC_SEGMENT:
{
struct segment_command *seg_hdr =
(struct segment_command *) cmd_hdr;
/* Ignore segments with no vm size */
if (!seg_hdr->vmsize) continue;
/* Ignore LINKEDIT segments */