2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <mach-o/loader.h>
31 #include <mach-o/nlist.h>
32 #include <mach-o/reloc.h>
33 #include <sys/types.h>
36 #include <libkern/kernel_mach_header.h>
37 #include <mach/vm_param.h>
38 #include <mach-o/fat.h>
40 #include <architecture/byte_order.h>
41 #include <mach/mach_init.h>
42 #include <mach-o/arch.h>
43 #include <mach-o/swap.h>
46 #define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
47 #include <AssertMacros.h>
49 #include "kxld_demangle.h"
50 #include "kxld_dict.h"
51 #include "kxld_reloc.h"
52 #include "kxld_sect.h"
54 #include "kxld_symtab.h"
55 #include "kxld_util.h"
56 #include "kxld_uuid.h"
57 #include "kxld_vtable.h"
59 #include "kxld_object.h"
61 /*******************************************************************************
63 *******************************************************************************/
71 cpu_subtype_t cpusubtype
;
76 KXLDRelocator relocator
;
79 struct dysymtab_command
*dysymtab_hdr
;
80 kxld_addr_t link_addr
;
82 boolean_t is_final_image
;
84 boolean_t got_is_created
;
85 #if KXLD_USER_OR_OBJECT
86 KXLDArray
*section_order
;
89 enum NXByteOrder host_order
;
90 enum NXByteOrder target_order
;
94 /*******************************************************************************
96 *******************************************************************************/
98 static kern_return_t
get_target_machine_info(KXLDObject
*object
,
99 cpu_type_t cputype
, cpu_subtype_t cpusubtype
);
100 static kern_return_t
get_macho_slice_for_arch(KXLDObject
*object
,
101 u_char
*file
, u_long size
);
103 static u_long
get_macho_header_size(const KXLDObject
*object
);
104 static u_long
get_macho_data_size(const KXLDObject
*object
) __unused
;
106 static kern_return_t
init_from_execute(KXLDObject
*object
);
107 static kern_return_t
init_from_final_linked_image(KXLDObject
*object
,
108 u_int
*filetype_out
, struct symtab_command
**symtab_hdr_out
);
110 static boolean_t
target_supports_protected_segments(const KXLDObject
*object
)
111 __attribute__((pure
));
112 static void set_is_object_linked(KXLDObject
*object
);
114 #if KXLD_USER_OR_BUNDLE
115 static boolean_t
target_supports_bundle(const KXLDObject
*object
)
117 static kern_return_t
init_from_bundle(KXLDObject
*object
);
118 static kern_return_t
process_relocs_from_tables(KXLDObject
*object
);
119 static KXLDSeg
*get_seg_by_base_addr(KXLDObject
*object
,
120 kxld_addr_t base_addr
);
121 static kern_return_t
process_symbol_pointers(KXLDObject
*object
);
122 static void add_to_ptr(u_char
*symptr
, kxld_addr_t val
, boolean_t is_32_bit
);
123 #endif /* KXLD_USER_OR_BUNDLE */
125 #if KXLD_USER_OR_OBJECT
126 static boolean_t
target_supports_object(const KXLDObject
*object
)
128 static kern_return_t
init_from_object(KXLDObject
*object
);
129 static kern_return_t
process_relocs_from_sections(KXLDObject
*object
);
130 #endif /* KXLD_USER_OR_OBJECT */
132 static kern_return_t
export_macho_header(const KXLDObject
*object
, u_char
*buf
,
133 u_int ncmds
, u_long
*header_offset
, u_long header_size
);
134 #if KXLD_USER_OR_ILP32
135 static u_long
get_macho_cmd_data_32(u_char
*file
, u_long offset
,
136 u_int
*filetype
, u_int
*ncmds
);
137 static kern_return_t
export_macho_header_32(const KXLDObject
*object
,
138 u_char
*buf
, u_int ncmds
, u_long
*header_offset
, u_long header_size
);
139 #endif /* KXLD_USER_OR_ILP32 */
140 #if KXLD_USER_OR_LP64
141 static u_long
get_macho_cmd_data_64(u_char
*file
, u_long offset
,
142 u_int
*filetype
, u_int
*ncmds
);
143 static kern_return_t
export_macho_header_64(const KXLDObject
*object
,
144 u_char
*buf
, u_int ncmds
, u_long
*header_offset
, u_long header_size
);
145 #endif /* KXLD_USER_OR_LP64 */
147 #if KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON
148 static kern_return_t
add_section(KXLDObject
*object
, KXLDSect
**sect
);
149 #endif /* KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON */
151 #if KXLD_USER_OR_COMMON
152 static kern_return_t
resolve_common_symbols(KXLDObject
*object
);
153 #endif /* KXLD_USER_OR_COMMON */
156 static boolean_t
target_has_got(const KXLDObject
*object
) __attribute__((pure
));
157 static kern_return_t
create_got(KXLDObject
*object
);
158 static kern_return_t
populate_got(KXLDObject
*object
);
159 #endif /* KXLD_USER_OR_GOT */
161 static KXLDSym
*get_mutable_sym(const KXLDObject
*object
, const KXLDSym
*sym
);
163 static kern_return_t
populate_kmod_info(KXLDObject
*object
);
165 /*******************************************************************************
166 * Prototypes that may need to be exported
167 *******************************************************************************/
168 static boolean_t
kxld_object_target_needs_swap(const KXLDObject
*object __unused
);
169 static KXLDSeg
* kxld_object_get_seg_by_name(const KXLDObject
*object
, const char *segname
);
170 static KXLDSect
* kxld_object_get_sect_by_name(const KXLDObject
*object
, const char *segname
,
171 const char *sectname
);
173 /*******************************************************************************
174 *******************************************************************************/
176 kxld_object_sizeof(void)
178 return sizeof(KXLDObject
);
181 /*******************************************************************************
182 *******************************************************************************/
184 kxld_object_init_from_macho(KXLDObject
*object
, u_char
*file
, u_long size
,
185 const char *name
, KXLDArray
*section_order __unused
,
186 cpu_type_t cputype
, cpu_subtype_t cpusubtype
)
188 kern_return_t rval
= KERN_FAILURE
;
189 KXLDSeg
* seg
= NULL
;
198 #if KXLD_USER_OR_OBJECT
199 object
->section_order
= section_order
;
201 /* Find the local architecture */
203 rval
= get_target_machine_info(object
, cputype
, cpusubtype
);
204 require_noerr(rval
, finish
);
206 /* Find the Mach-O slice for the target architecture */
208 rval
= get_macho_slice_for_arch(object
, file
, size
);
209 require_noerr(rval
, finish
);
211 /* Allocate the symbol table */
213 if (!object
->symtab
) {
214 object
->symtab
= kxld_alloc(kxld_symtab_sizeof());
215 require_action(object
->symtab
, finish
, rval
=KERN_RESOURCE_SHORTAGE
);
216 bzero(object
->symtab
, kxld_symtab_sizeof());
219 /* Build the relocator */
221 rval
= kxld_relocator_init(&object
->relocator
, object
->file
,
222 object
->symtab
, &object
->sects
, object
->cputype
,
223 object
->cpusubtype
, kxld_object_target_needs_swap(object
));
224 require_noerr(rval
, finish
);
226 /* There are four types of Mach-O files that we can support:
227 * 1) 32-bit MH_OBJECT - Snow Leopard and earlier
228 * 2) 32-bit MH_KEXT_BUNDLE - Lion and Later
229 * 3) 64-bit MH_OBJECT - Unsupported
230 * 4) 64-bit MH_KEXT_BUNDLE - Snow Leopard and Later
233 if (kxld_object_is_32_bit(object
)) {
234 struct mach_header
*mach_hdr
= (struct mach_header
*) object
->file
;
235 object
->filetype
= mach_hdr
->filetype
;
237 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) object
->file
;
238 object
->filetype
= mach_hdr
->filetype
;
241 switch (object
->filetype
) {
242 #if KXLD_USER_OR_BUNDLE
244 rval
= init_from_bundle(object
);
245 require_noerr(rval
, finish
);
247 #endif /* KXLD_USER_OR_BUNDLE */
248 #if KXLD_USER_OR_OBJECT
250 rval
= init_from_object(object
);
251 require_noerr(rval
, finish
);
253 #endif /* KXLD_USER_OR_OBJECT */
255 object
->is_kernel
= TRUE
;
256 rval
= init_from_execute(object
);
257 require_noerr(rval
, finish
);
261 kxld_log(kKxldLogLinking
, kKxldLogErr
,
262 kKxldLogFiletypeNotSupported
, object
->filetype
);
266 if (!kxld_object_is_kernel(object
)) {
267 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
268 seg
= kxld_array_get_item(&object
->segs
, i
);
269 kxld_seg_set_vm_protections(seg
,
270 target_supports_protected_segments(object
));
273 seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
275 (void) kxld_seg_populate_linkedit(seg
, object
->symtab
,
276 kxld_object_is_32_bit(object
));
280 (void) set_is_object_linked(object
);
287 /*******************************************************************************
288 *******************************************************************************/
290 get_target_machine_info(KXLDObject
*object
, cpu_type_t cputype __unused
,
291 cpu_subtype_t cpusubtype __unused
)
295 /* Because the kernel can only link for its own architecture, we know what
296 * the host and target architectures are at compile time, so we can use
297 * a vastly simplified version of this function.
302 #if defined(__i386__)
303 object
->cputype
= CPU_TYPE_I386
;
304 object
->cpusubtype
= CPU_SUBTYPE_I386_ALL
;
306 #elif defined(__x86_64__)
307 object
->cputype
= CPU_TYPE_X86_64
;
308 object
->cpusubtype
= CPU_SUBTYPE_X86_64_ALL
;
311 kxld_log(kKxldLogLinking
, kKxldLogErr
,
312 kKxldLogArchNotSupported
, _mh_execute_header
->cputype
);
313 return KERN_NOT_SUPPORTED
;
314 #endif /* Supported architecture defines */
319 /* User-space must look up the architecture it's running on and the target
320 * architecture at run-time.
323 kern_return_t rval
= KERN_FAILURE
;
324 const NXArchInfo
*host_arch
= NULL
;
328 host_arch
= NXGetLocalArchInfo();
329 require_action(host_arch
, finish
, rval
=KERN_FAILURE
);
331 object
->host_order
= host_arch
->byteorder
;
333 /* If the user did not specify a cputype, use the local architecture.
337 object
->cputype
= cputype
;
338 object
->cpusubtype
= cpusubtype
;
340 object
->cputype
= host_arch
->cputype
;
341 object
->target_order
= object
->host_order
;
343 switch (object
->cputype
) {
345 object
->cpusubtype
= CPU_SUBTYPE_I386_ALL
;
347 case CPU_TYPE_POWERPC
:
348 object
->cpusubtype
= CPU_SUBTYPE_POWERPC_ALL
;
350 case CPU_TYPE_X86_64
:
351 object
->cpusubtype
= CPU_SUBTYPE_X86_64_ALL
;
354 object
->cpusubtype
= CPU_SUBTYPE_ARM_ALL
;
357 object
->cpusubtype
= 0;
361 /* Validate that we support the target architecture and record its
365 switch(object
->cputype
) {
368 case CPU_TYPE_X86_64
:
369 object
->target_order
= NX_LittleEndian
;
371 case CPU_TYPE_POWERPC
:
372 object
->target_order
= NX_BigEndian
;
375 rval
= KERN_NOT_SUPPORTED
;
376 kxld_log(kKxldLogLinking
, kKxldLogErr
,
377 kKxldLogArchNotSupported
, object
->cputype
);
388 /*******************************************************************************
389 *******************************************************************************/
391 get_macho_slice_for_arch(KXLDObject
*object
, u_char
*file
, u_long size
)
393 kern_return_t rval
= KERN_FAILURE
;
394 struct mach_header
*mach_hdr
= NULL
;
396 struct fat_header
*fat
= (struct fat_header
*) file
;
397 struct fat_arch
*archs
= (struct fat_arch
*) &fat
[1];
398 boolean_t swap
= FALSE
;
408 /* We are assuming that we will never receive a fat file in the kernel */
411 require_action(size
>= sizeof(*fat
), finish
,
413 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
415 /* The fat header is always big endian, so swap if necessary */
416 if (fat
->magic
== FAT_CIGAM
) {
417 (void) swap_fat_header(fat
, object
->host_order
);
421 if (fat
->magic
== FAT_MAGIC
) {
422 struct fat_arch
*arch
= NULL
;
424 require_action(size
>= (sizeof(*fat
) + (fat
->nfat_arch
* sizeof(*archs
))),
427 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
429 /* Swap the fat_arch structures if necessary */
431 (void) swap_fat_arch(archs
, fat
->nfat_arch
, object
->host_order
);
434 /* Locate the Mach-O for the requested architecture */
436 arch
= NXFindBestFatArch(object
->cputype
, object
->cpusubtype
, archs
,
438 require_action(arch
, finish
, rval
=KERN_FAILURE
;
439 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogArchNotFound
));
440 require_action(size
>= arch
->offset
+ arch
->size
, finish
,
442 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
444 object
->file
= file
+ arch
->offset
;
445 object
->size
= arch
->size
;
449 /* Swap the Mach-O's headers to this architecture if necessary */
450 if (kxld_object_is_32_bit(object
)) {
451 rval
= validate_and_swap_macho_32(object
->file
, object
->size
457 rval
= validate_and_swap_macho_64(object
->file
, object
->size
463 require_noerr(rval
, finish
);
465 mach_hdr
= (struct mach_header
*) object
->file
;
466 require_action(object
->cputype
== mach_hdr
->cputype
, finish
,
468 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
475 /*******************************************************************************
476 *******************************************************************************/
478 init_from_final_linked_image(KXLDObject
*object
, u_int
*filetype_out
,
479 struct symtab_command
**symtab_hdr_out
)
481 kern_return_t rval
= KERN_FAILURE
;
483 KXLDSect
*sect
= NULL
;
484 struct load_command
*cmd_hdr
= NULL
;
485 struct symtab_command
*symtab_hdr
= NULL
;
486 struct uuid_command
*uuid_hdr
= NULL
;
487 u_long base_offset
= 0;
489 u_long sect_offset
= 0;
499 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), base_offset
,
500 get_macho_cmd_data_32
, get_macho_cmd_data_64
,
501 object
->file
, offset
, &filetype
, &ncmds
);
503 /* First pass to count segments and sections */
505 offset
= base_offset
;
506 for (i
= 0; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
507 cmd_hdr
= (struct load_command
*) (object
->file
+ offset
);
509 switch(cmd_hdr
->cmd
) {
510 #if KXLD_USER_OR_ILP32
513 struct segment_command
*seg_hdr
=
514 (struct segment_command
*) cmd_hdr
;
516 /* Ignore segments with no vm size */
517 if (!seg_hdr
->vmsize
) continue;
520 nsects
+= seg_hdr
->nsects
;
523 #endif /* KXLD_USER_OR_ILP32 */
524 #if KXLD_USER_OR_LP64
527 struct segment_command_64
*seg_hdr
=
528 (struct segment_command_64
*) cmd_hdr
;
530 /* Ignore segments with no vm size */
531 if (!seg_hdr
->vmsize
) continue;
534 nsects
+= seg_hdr
->nsects
;
537 #endif /* KXLD_USER_OR_LP64 */
543 /* Allocate the segments and sections */
546 rval
= kxld_array_init(&object
->segs
, sizeof(KXLDSeg
), nsegs
);
547 require_noerr(rval
, finish
);
549 rval
= kxld_array_init(&object
->sects
, sizeof(KXLDSect
), nsects
);
550 require_noerr(rval
, finish
);
553 /* Initialize the segments and sections */
555 offset
= base_offset
;
556 for (i
= 0; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
557 cmd_hdr
= (struct load_command
*) (object
->file
+ offset
);
560 switch(cmd_hdr
->cmd
) {
561 #if KXLD_USER_OR_ILP32
564 struct segment_command
*seg_hdr
=
565 (struct segment_command
*) cmd_hdr
;
567 /* Ignore segments with no vm size */
568 if (!seg_hdr
->vmsize
) continue;
570 seg
= kxld_array_get_item(&object
->segs
, segi
++);
572 rval
= kxld_seg_init_from_macho_32(seg
, seg_hdr
);
573 require_noerr(rval
, finish
);
575 sect_offset
= offset
+ sizeof(*seg_hdr
);
578 #endif /* KXLD_USER_OR_ILP32 */
579 #if KXLD_USER_OR_LP64
582 struct segment_command_64
*seg_hdr
=
583 (struct segment_command_64
*) cmd_hdr
;
585 /* Ignore segments with no vm size */
586 if (!seg_hdr
->vmsize
) continue;
588 seg
= kxld_array_get_item(&object
->segs
, segi
++);
590 rval
= kxld_seg_init_from_macho_64(seg
, seg_hdr
);
591 require_noerr(rval
, finish
);
593 sect_offset
= offset
+ sizeof(*seg_hdr
);
596 #endif /* KXLD_USER_OR_LP64 */
598 symtab_hdr
= (struct symtab_command
*) cmd_hdr
;
601 uuid_hdr
= (struct uuid_command
*) cmd_hdr
;
602 kxld_uuid_init_from_macho(&object
->uuid
, uuid_hdr
);
605 object
->dysymtab_hdr
= (struct dysymtab_command
*) cmd_hdr
;
607 rval
= kxld_reloc_create_macho(&object
->extrelocs
, &object
->relocator
,
608 (struct relocation_info
*) (object
->file
+ object
->dysymtab_hdr
->extreloff
),
609 object
->dysymtab_hdr
->nextrel
);
610 require_noerr(rval
, finish
);
612 rval
= kxld_reloc_create_macho(&object
->locrelocs
, &object
->relocator
,
613 (struct relocation_info
*) (object
->file
+ object
->dysymtab_hdr
->locreloff
),
614 object
->dysymtab_hdr
->nlocrel
);
615 require_noerr(rval
, finish
);
619 /* Don't need to do anything with UNIXTHREAD for the kernel */
620 require_action(kxld_object_is_kernel(object
),
621 finish
, rval
=KERN_FAILURE
;
622 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
623 "LC_UNIXTHREAD segment is not valid in a kext."));
627 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
628 "Invalid segment type in MH_KEXT_BUNDLE kext: %u.", cmd_hdr
->cmd
);
634 /* Initialize the sections */
635 for (j
= 0; j
< seg
->sects
.nitems
; ++j
, ++secti
) {
636 sect
= kxld_array_get_item(&object
->sects
, secti
);
637 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
638 kxld_sect_init_from_macho_32
, kxld_sect_init_from_macho_64
,
639 sect
, object
->file
, §_offset
, secti
, &object
->relocator
);
640 require_noerr(rval
, finish
);
642 /* Add the section to the segment. This will also make sure
643 * that the sections and segments have the same segname.
645 rval
= kxld_seg_add_section(seg
, sect
);
646 require_noerr(rval
, finish
);
648 rval
= kxld_seg_finish_init(seg
);
649 require_noerr(rval
, finish
);
653 if (filetype_out
) *filetype_out
= filetype
;
654 if (symtab_hdr_out
) *symtab_hdr_out
= symtab_hdr
;
655 object
->is_final_image
= TRUE
;
661 /*******************************************************************************
662 *******************************************************************************/
664 init_from_execute(KXLDObject
*object
)
666 kern_return_t rval
= KERN_FAILURE
;
667 struct symtab_command
*symtab_hdr
= NULL
;
669 KXLDSeg
* kernel_linkedit_seg
= NULL
; // used if running kernel
670 #if KXLD_USER_OR_OBJECT
672 KXLDSect
*sect
= NULL
;
673 KXLDSectionName
*sname
= NULL
;
674 u_int i
= 0, j
= 0, k
= 0;
675 #endif /* KXLD_USER_OR_OBJECT */
679 require_action(kxld_object_is_kernel(object
), finish
, rval
=KERN_FAILURE
);
681 rval
= init_from_final_linked_image(object
, &filetype
, &symtab_hdr
);
682 require_noerr(rval
, finish
);
684 require_action(filetype
== MH_EXECUTE
, finish
, rval
=KERN_FAILURE
;
685 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
686 "The kernel file is not of type MH_EXECUTE."));
688 /* Initialize the symbol table. If this is the running kernel
689 * we will work from the in-memory linkedit segment;
690 * otherwise we work from the whole mach-o image.
693 kernel_linkedit_seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
694 require_action(kernel_linkedit_seg
, finish
, rval
=KERN_FAILURE
;
695 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
));
698 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
699 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
700 object
->symtab
, symtab_hdr
, object
->file
, kernel_linkedit_seg
);
701 require_noerr(rval
, finish
);
703 #if KXLD_USER_OR_OBJECT
704 /* Save off the order of section names so that we can lay out kext
705 * sections for MH_OBJECT-based systems.
707 if (target_supports_object(object
)) {
709 rval
= kxld_array_init(object
->section_order
, sizeof(KXLDSectionName
),
710 object
->sects
.nitems
);
711 require_noerr(rval
, finish
);
713 /* Copy the section names into the section_order array for future kext
716 for (i
= 0, k
= 0; i
< object
->segs
.nitems
; ++i
) {
717 seg
= kxld_array_get_item(&object
->segs
, i
);
719 for (j
= 0; j
< seg
->sects
.nitems
; ++j
, ++k
) {
720 sect
= *(KXLDSect
**) kxld_array_get_item(&seg
->sects
, j
);
721 sname
= kxld_array_get_item(object
->section_order
, k
);
723 strlcpy(sname
->segname
, sect
->segname
, sizeof(sname
->segname
));
724 strlcpy(sname
->sectname
, sect
->sectname
, sizeof(sname
->sectname
));
728 #endif /* KXLD_USER_OR_OBJECT */
735 #if KXLD_USER_OR_BUNDLE
736 /*******************************************************************************
737 *******************************************************************************/
739 target_supports_bundle(const KXLDObject
*object
)
741 return (object
->cputype
== CPU_TYPE_I386
||
742 object
->cputype
== CPU_TYPE_X86_64
||
743 object
->cputype
== CPU_TYPE_ARM
);
746 /*******************************************************************************
747 *******************************************************************************/
749 init_from_bundle(KXLDObject
*object
)
751 kern_return_t rval
= KERN_FAILURE
;
752 struct symtab_command
*symtab_hdr
= NULL
;
757 require_action(target_supports_bundle(object
), finish
,
759 kxld_log(kKxldLogLinking
, kKxldLogErr
,
760 kKxldLogFiletypeNotSupported
, MH_KEXT_BUNDLE
));
762 rval
= init_from_final_linked_image(object
, &filetype
, &symtab_hdr
);
763 require_noerr(rval
, finish
);
765 require_action(filetype
== MH_KEXT_BUNDLE
, finish
,
768 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
769 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
770 object
->symtab
, symtab_hdr
, object
->file
,
771 /* kernel_linkedit_seg */ NULL
);
772 require_noerr(rval
, finish
);
778 #endif /* KXLD_USER_OR_BUNDLE */
780 #if KXLD_USER_OR_OBJECT
781 /*******************************************************************************
782 *******************************************************************************/
783 static boolean_t
target_supports_object(const KXLDObject
*object
)
785 return (object
->cputype
== CPU_TYPE_POWERPC
||
786 object
->cputype
== CPU_TYPE_I386
||
787 object
->cputype
== CPU_TYPE_ARM
);
790 /*******************************************************************************
791 *******************************************************************************/
793 init_from_object(KXLDObject
*object
)
795 kern_return_t rval
= KERN_FAILURE
;
796 struct load_command
*cmd_hdr
= NULL
;
797 struct symtab_command
*symtab_hdr
= NULL
;
798 struct uuid_command
*uuid_hdr
= NULL
;
799 KXLDSect
*sect
= NULL
;
801 u_long sect_offset
= 0;
806 boolean_t has_segment
= FALSE
;
810 require_action(target_supports_object(object
),
811 finish
, rval
=KERN_FAILURE
;
812 kxld_log(kKxldLogLinking
, kKxldLogErr
,
813 kKxldLogFiletypeNotSupported
, MH_OBJECT
));
815 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), offset
,
816 get_macho_cmd_data_32
, get_macho_cmd_data_64
,
817 object
->file
, offset
, &filetype
, &ncmds
);
819 require_action(filetype
== MH_OBJECT
, finish
, rval
=KERN_FAILURE
);
821 /* MH_OBJECTs use one unnamed segment to contain all of the sections. We
822 * loop over all of the load commands to initialize the structures we
823 * expect. Then, we'll use the unnamed segment to get to all of the
824 * sections, and then use those sections to create the actual segments.
827 for (; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
828 cmd_hdr
= (struct load_command
*) (object
->file
+ offset
);
830 switch(cmd_hdr
->cmd
) {
831 #if KXLD_USER_OR_ILP32
834 struct segment_command
*seg_hdr
=
835 (struct segment_command
*) cmd_hdr
;
837 /* Ignore segments with no vm size */
838 if (!seg_hdr
->vmsize
) continue;
840 /* Ignore LINKEDIT segments */
841 if (streq_safe(seg_hdr
->segname
, SEG_LINKEDIT
,
842 const_strlen(SEG_LINKEDIT
)))
847 require_action(kxld_object_is_32_bit(object
), finish
, rval
=KERN_FAILURE
;
848 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
849 "LC_SEGMENT in 64-bit kext."));
850 require_action(!has_segment
, finish
, rval
=KERN_FAILURE
;
851 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
852 "Multiple segments in an MH_OBJECT kext."));
854 nsects
= seg_hdr
->nsects
;
855 sect_offset
= offset
+ sizeof(*seg_hdr
);
859 #endif /* KXLD_USER_OR_ILP32 */
860 #if KXLD_USER_OR_LP64
863 struct segment_command_64
*seg_hdr
=
864 (struct segment_command_64
*) cmd_hdr
;
866 /* Ignore segments with no vm size */
867 if (!seg_hdr
->vmsize
) continue;
869 /* Ignore LINKEDIT segments */
870 if (streq_safe(seg_hdr
->segname
, SEG_LINKEDIT
,
871 const_strlen(SEG_LINKEDIT
)))
876 require_action(!kxld_object_is_32_bit(object
), finish
, rval
=KERN_FAILURE
;
877 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
878 "LC_SEGMENT_64 in a 32-bit kext."));
879 require_action(!has_segment
, finish
, rval
=KERN_FAILURE
;
880 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
881 "Multiple segments in an MH_OBJECT kext."));
883 nsects
= seg_hdr
->nsects
;
884 sect_offset
= offset
+ sizeof(*seg_hdr
);
888 #endif /* KXLD_USER_OR_LP64 */
890 symtab_hdr
= (struct symtab_command
*) cmd_hdr
;
892 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
893 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
894 object
->symtab
, symtab_hdr
, object
->file
,
895 /* kernel_linkedit_seg */ NULL
);
896 require_noerr(rval
, finish
);
899 uuid_hdr
= (struct uuid_command
*) cmd_hdr
;
900 kxld_uuid_init_from_macho(&object
->uuid
, uuid_hdr
);
903 /* Don't need to do anything with UNIXTHREAD */
907 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
908 "Invalid segment type in MH_OBJECT kext: %u.", cmd_hdr
->cmd
);
915 /* Get the number of sections from the segment and build the section index */
917 rval
= kxld_array_init(&object
->sects
, sizeof(KXLDSect
), nsects
);
918 require_noerr(rval
, finish
);
920 /* Loop over all of the sections to initialize the section index */
922 for (i
= 0; i
< nsects
; ++i
) {
923 sect
= kxld_array_get_item(&object
->sects
, i
);
924 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
925 kxld_sect_init_from_macho_32
, kxld_sect_init_from_macho_64
,
926 sect
, object
->file
, §_offset
, i
, &object
->relocator
);
927 require_noerr(rval
, finish
);
930 /* Create special sections */
933 rval
= create_got(object
);
934 require_noerr(rval
, finish
);
935 #endif /* KXLD_USER_OR_GOT */
937 #if KXLD_USER_OR_COMMON
938 rval
= resolve_common_symbols(object
);
939 require_noerr(rval
, finish
);
940 #endif /* KXLD_USER_OR_COMMON */
942 /* Create the segments from the section index */
944 rval
= kxld_seg_create_seg_from_sections(&object
->segs
, &object
->sects
);
945 require_noerr(rval
, finish
);
947 rval
= kxld_seg_finalize_object_segment(&object
->segs
,
948 object
->section_order
, get_macho_header_size(object
));
949 require_noerr(rval
, finish
);
951 rval
= kxld_seg_init_linkedit(&object
->segs
);
952 require_noerr(rval
, finish
);
959 #endif /* KXLD_USER_OR_OBJECT */
961 #if KXLD_USER_OR_ILP32
962 /*******************************************************************************
963 *******************************************************************************/
965 get_macho_cmd_data_32(u_char
*file
, u_long offset
, u_int
*filetype
, u_int
*ncmds
)
967 struct mach_header
*mach_hdr
= (struct mach_header
*) (file
+ offset
);
969 if (filetype
) *filetype
= mach_hdr
->filetype
;
970 if (ncmds
) *ncmds
= mach_hdr
->ncmds
;
972 return sizeof(*mach_hdr
);
975 #endif /* KXLD_USER_OR_ILP32 */
977 #if KXLD_USER_OR_LP64
978 /*******************************************************************************
979 *******************************************************************************/
981 get_macho_cmd_data_64(u_char
*file
, u_long offset
, u_int
*filetype
, u_int
*ncmds
)
983 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) (file
+ offset
);
985 if (filetype
) *filetype
= mach_hdr
->filetype
;
986 if (ncmds
) *ncmds
= mach_hdr
->ncmds
;
988 return sizeof(*mach_hdr
);
990 #endif /* KXLD_USER_OR_LP64 */
992 /*******************************************************************************
993 *******************************************************************************/
995 get_macho_header_size(const KXLDObject
*object
)
998 u_long header_size
= 0;
1003 /* Mach, segment, symtab, and UUID headers */
1005 if (kxld_object_is_32_bit(object
)) {
1006 header_size
+= sizeof(struct mach_header
);
1008 header_size
+= sizeof(struct mach_header_64
);
1011 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1012 seg
= kxld_array_get_item(&object
->segs
, i
);
1013 header_size
+= kxld_seg_get_macho_header_size(seg
, kxld_object_is_32_bit(object
));
1016 header_size
+= kxld_symtab_get_macho_header_size();
1018 if (object
->uuid
.has_uuid
) {
1019 header_size
+= kxld_uuid_get_macho_header_size();
1025 /*******************************************************************************
1026 *******************************************************************************/
1028 get_macho_data_size(const KXLDObject
*object
)
1030 KXLDSeg
*seg
= NULL
;
1031 u_long data_size
= 0;
1036 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1037 seg
= kxld_array_get_item(&object
->segs
, i
);
1038 data_size
+= (u_long
) kxld_seg_get_vmsize(seg
);
1044 /*******************************************************************************
1045 *******************************************************************************/
1047 kxld_object_target_needs_swap(const KXLDObject
*object __unused
)
1052 return (object
->target_order
!= object
->host_order
);
1056 /*******************************************************************************
1057 *******************************************************************************/
1059 kxld_object_get_seg_by_name(const KXLDObject
*object
, const char *segname
)
1061 KXLDSeg
*seg
= NULL
;
1064 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1065 seg
= kxld_array_get_item(&object
->segs
, i
);
1067 if (streq_safe(segname
, seg
->segname
, sizeof(seg
->segname
))) break;
1075 /*******************************************************************************
1076 *******************************************************************************/
1077 const KXLDRelocator
*
1078 kxld_object_get_relocator(const KXLDObject
* object
)
1082 return &object
->relocator
;
1085 /*******************************************************************************
1086 *******************************************************************************/
1088 kxld_object_get_sect_by_name(const KXLDObject
*object
, const char *segname
,
1089 const char *sectname
)
1091 KXLDSect
*sect
= NULL
;
1094 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1095 sect
= kxld_array_get_item(&object
->sects
, i
);
1097 if (streq_safe(segname
, sect
->segname
, sizeof(sect
->segname
)) &&
1098 streq_safe(sectname
, sect
->sectname
, sizeof(sect
->sectname
)))
1109 /*******************************************************************************
1110 *******************************************************************************/
1112 kxld_object_get_reloc_at_symbol(const KXLDObject
*object
, const KXLDSym
*sym
)
1114 const KXLDReloc
*reloc
= NULL
;
1115 const KXLDSect
*sect
= NULL
;
1116 uint32_t offset
= 0;
1121 sect
= kxld_object_get_section_by_index(object
, sym
->sectnum
);
1122 require(sect
, finish
);
1124 if (kxld_object_is_final_image(object
)) {
1125 reloc
= kxld_reloc_get_reloc_by_offset(&object
->extrelocs
,
1128 reloc
= kxld_reloc_get_reloc_by_offset(&object
->locrelocs
,
1132 offset
= kxld_sym_get_section_offset(sym
, sect
);
1133 reloc
= kxld_reloc_get_reloc_by_offset(§
->relocs
, offset
);
1140 /*******************************************************************************
1141 *******************************************************************************/
1143 kxld_object_get_symbol_of_reloc(const KXLDObject
*object
,
1144 const KXLDReloc
*reloc
, const KXLDSect
*sect
)
1146 const KXLDSym
*sym
= NULL
;
1148 if (kxld_object_is_final_image(object
)) {
1149 sym
= kxld_reloc_get_symbol(&object
->relocator
, reloc
, object
->file
);
1151 sym
= kxld_reloc_get_symbol(&object
->relocator
, reloc
, sect
->data
);
1157 /*******************************************************************************
1158 *******************************************************************************/
1160 kxld_object_get_section_by_index(const KXLDObject
*object
, u_int sectnum
)
1162 KXLDSect
*sect
= NULL
;
1166 if (sectnum
< object
->sects
.nitems
) {
1167 sect
= kxld_array_get_item(&object
->sects
, sectnum
);
1173 /*******************************************************************************
1174 *******************************************************************************/
1176 kxld_object_get_extrelocs(const KXLDObject
*object
)
1178 const KXLDArray
*rval
= NULL
;
1182 if (kxld_object_is_final_image(object
)) {
1183 rval
= &object
->extrelocs
;
1189 /*******************************************************************************
1190 *******************************************************************************/
1192 kxld_object_get_symtab(const KXLDObject
*object
)
1196 return object
->symtab
;
1199 #if KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON
1200 /*******************************************************************************
1201 *******************************************************************************/
1202 static kern_return_t
1203 add_section(KXLDObject
*object
, KXLDSect
**sect
)
1205 kern_return_t rval
= KERN_FAILURE
;
1206 u_int nsects
= object
->sects
.nitems
;
1208 rval
= kxld_array_resize(&object
->sects
, nsects
+ 1);
1209 require_noerr(rval
, finish
);
1211 *sect
= kxld_array_get_item(&object
->sects
, nsects
);
1213 rval
= KERN_SUCCESS
;
1218 #endif /* KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON */
1220 #if KXLD_USER_OR_COMMON
1221 /*******************************************************************************
1222 * If there are common symbols, calculate how much space they'll need
1223 * and create/grow the __DATA __common section to accommodate them.
1224 * Then, resolve them against that section.
1225 *******************************************************************************/
1226 static kern_return_t
1227 resolve_common_symbols(KXLDObject
*object
)
1229 kern_return_t rval
= KERN_FAILURE
;
1230 KXLDSymtabIterator iter
;
1231 KXLDSym
*sym
= NULL
;
1232 KXLDSect
*sect
= NULL
;
1233 kxld_addr_t base_addr
= 0;
1234 kxld_size_t size
= 0;
1235 kxld_size_t total_size
= 0;
1237 u_int max_align
= 0;
1240 if (!kxld_object_target_supports_common_symbols(object
)) {
1241 rval
= KERN_SUCCESS
;
1245 /* Iterate over the common symbols to calculate their total aligned size */
1246 kxld_symtab_iterator_init(&iter
, object
->symtab
, kxld_sym_is_common
, FALSE
);
1247 while ((sym
= kxld_symtab_iterator_get_next(&iter
))) {
1248 align
= kxld_sym_get_common_align(sym
);
1249 size
= kxld_sym_get_common_size(sym
);
1251 if (align
> max_align
) max_align
= align
;
1253 total_size
= kxld_align_address(total_size
, align
) + size
;
1256 /* If there are common symbols, grow or create the __DATA __common section
1260 sect
= kxld_object_get_sect_by_name(object
, SEG_DATA
, SECT_COMMON
);
1262 base_addr
= sect
->base_addr
+ sect
->size
;
1264 kxld_sect_grow(sect
, total_size
, max_align
);
1268 rval
= add_section(object
, §
);
1269 require_noerr(rval
, finish
);
1271 kxld_sect_init_zerofill(sect
, SEG_DATA
, SECT_COMMON
,
1272 total_size
, max_align
);
1275 /* Resolve the common symbols against the new section */
1276 rval
= kxld_array_get_index(&object
->sects
, sect
, §num
);
1277 require_noerr(rval
, finish
);
1279 kxld_symtab_iterator_reset(&iter
);
1280 while ((sym
= kxld_symtab_iterator_get_next(&iter
))) {
1281 align
= kxld_sym_get_common_align(sym
);
1282 size
= kxld_sym_get_common_size(sym
);
1284 base_addr
= kxld_align_address(base_addr
, align
);
1285 kxld_sym_resolve_common(sym
, sectnum
, base_addr
);
1291 rval
= KERN_SUCCESS
;
1296 #endif /* KXLD_USER_OR_COMMON */
1298 #if KXLD_USER_OR_GOT
1299 /*******************************************************************************
1300 *******************************************************************************/
1302 target_has_got(const KXLDObject
*object
)
1307 /*******************************************************************************
1308 * Create and initialize the Global Offset Table
1309 *******************************************************************************/
1310 static kern_return_t
1311 create_got(KXLDObject
*object
)
1313 kern_return_t rval
= KERN_FAILURE
;
1314 KXLDSect
*sect
= NULL
;
1318 if (!target_has_got(object
)) {
1319 rval
= KERN_SUCCESS
;
1323 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1324 sect
= kxld_array_get_item(&object
->sects
, i
);
1325 ngots
+= kxld_sect_get_ngots(sect
, &object
->relocator
,
1329 rval
= add_section(object
, §
);
1330 require_noerr(rval
, finish
);
1332 rval
= kxld_sect_init_got(sect
, ngots
);
1333 require_noerr(rval
, finish
);
1335 object
->got_is_created
= TRUE
;
1336 rval
= KERN_SUCCESS
;
1342 /*******************************************************************************
1343 *******************************************************************************/
1344 static kern_return_t
1345 populate_got(KXLDObject
*object
)
1347 kern_return_t rval
= KERN_FAILURE
;
1348 KXLDSect
*sect
= NULL
;
1351 if (!target_has_got(object
) || !object
->got_is_created
) {
1352 rval
= KERN_SUCCESS
;
1356 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1357 sect
= kxld_array_get_item(&object
->sects
, i
);
1358 if (streq_safe(sect
->segname
, KXLD_SEG_GOT
, sizeof(KXLD_SEG_GOT
)) &&
1359 streq_safe(sect
->sectname
, KXLD_SECT_GOT
, sizeof(KXLD_SECT_GOT
)))
1361 kxld_sect_populate_got(sect
, object
->symtab
,
1362 kxld_object_target_needs_swap(object
));
1367 require_action(i
< object
->sects
.nitems
, finish
, rval
=KXLD_MISSING_GOT
);
1369 rval
= KERN_SUCCESS
;
1374 #endif /* KXLD_USER_OR_GOT */
1376 /*******************************************************************************
1377 *******************************************************************************/
1379 target_supports_protected_segments(const KXLDObject
*object
)
1381 return (object
->is_final_image
&&
1382 object
->cputype
== CPU_TYPE_X86_64
);
1385 /*******************************************************************************
1386 *******************************************************************************/
1388 set_is_object_linked(KXLDObject
*object
)
1392 if (kxld_object_is_kernel(object
)) {
1393 object
->is_linked
= TRUE
;
1397 if (object
->is_final_image
) {
1398 object
->is_linked
= !object
->extrelocs
.nitems
&& !object
->locrelocs
.nitems
;
1402 object
->is_linked
= TRUE
;
1403 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1404 KXLDSect
*sect
= kxld_array_get_item(&object
->sects
, i
);
1405 if (sect
->relocs
.nitems
) {
1406 object
->is_linked
= FALSE
;
1413 /*******************************************************************************
1414 *******************************************************************************/
1415 void kxld_object_clear(KXLDObject
*object __unused
)
1417 KXLDSeg
*seg
= NULL
;
1418 KXLDSect
*sect
= NULL
;
1424 if (kxld_object_is_kernel(object
)) {
1425 unswap_macho(object
->file
, object
->host_order
, object
->target_order
);
1427 #endif /* !KERNEL */
1429 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1430 seg
= kxld_array_get_item(&object
->segs
, i
);
1431 kxld_seg_clear(seg
);
1433 kxld_array_reset(&object
->segs
);
1435 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1436 sect
= kxld_array_get_item(&object
->sects
, i
);
1437 kxld_sect_clear(sect
);
1439 kxld_array_reset(&object
->sects
);
1441 kxld_array_reset(&object
->extrelocs
);
1442 kxld_array_reset(&object
->locrelocs
);
1443 kxld_relocator_clear(&object
->relocator
);
1444 kxld_uuid_clear(&object
->uuid
);
1446 if (object
->symtab
) kxld_symtab_clear(object
->symtab
);
1448 object
->file
= NULL
;
1450 object
->filetype
= 0;
1451 object
->cputype
= 0;
1452 object
->cpusubtype
= 0;
1453 object
->is_kernel
= FALSE
;
1454 object
->is_final_image
= FALSE
;
1455 object
->is_linked
= FALSE
;
1456 object
->got_is_created
= FALSE
;
1458 #if KXLD_USER_OR_OBJECT
1459 object
->section_order
= NULL
;
1462 object
->host_order
= 0;
1463 object
->target_order
= 0;
1467 /*******************************************************************************
1468 *******************************************************************************/
1469 void kxld_object_deinit(KXLDObject
*object __unused
)
1471 KXLDSeg
*seg
= NULL
;
1472 KXLDSect
*sect
= NULL
;
1478 if (object
->file
&& kxld_object_is_kernel(object
)) {
1479 unswap_macho(object
->file
, object
->host_order
, object
->target_order
);
1481 #endif /* !KERNEL */
1483 for (i
= 0; i
< object
->segs
.maxitems
; ++i
) {
1484 seg
= kxld_array_get_slot(&object
->segs
, i
);
1485 kxld_seg_deinit(seg
);
1487 kxld_array_deinit(&object
->segs
);
1489 for (i
= 0; i
< object
->sects
.maxitems
; ++i
) {
1490 sect
= kxld_array_get_slot(&object
->sects
, i
);
1491 kxld_sect_deinit(sect
);
1493 kxld_array_deinit(&object
->sects
);
1495 kxld_array_deinit(&object
->extrelocs
);
1496 kxld_array_deinit(&object
->locrelocs
);
1498 if (object
->symtab
) {
1499 kxld_symtab_deinit(object
->symtab
);
1500 kxld_free(object
->symtab
, kxld_symtab_sizeof());
1503 bzero(object
, sizeof(*object
));
1506 /*******************************************************************************
1507 *******************************************************************************/
1509 kxld_object_get_file(const KXLDObject
*object
)
1513 return object
->file
;
1516 /*******************************************************************************
1517 *******************************************************************************/
1519 kxld_object_get_name(const KXLDObject
*object
)
1523 return object
->name
;
1526 /*******************************************************************************
1527 *******************************************************************************/
1529 kxld_object_is_32_bit(const KXLDObject
*object
)
1533 return kxld_is_32_bit(object
->cputype
);
1536 /*******************************************************************************
1537 *******************************************************************************/
1539 kxld_object_is_final_image(const KXLDObject
*object
)
1543 return object
->is_final_image
;
1546 /*******************************************************************************
1547 *******************************************************************************/
1549 kxld_object_is_kernel(const KXLDObject
*object
)
1553 return object
->is_kernel
;
1556 /*******************************************************************************
1557 *******************************************************************************/
1559 kxld_object_is_linked(const KXLDObject
*object
)
1563 return object
->is_linked
;
1566 /*******************************************************************************
1567 *******************************************************************************/
1569 kxld_object_target_supports_strict_patching(const KXLDObject
*object
)
1573 return (object
->cputype
!= CPU_TYPE_I386
&&
1574 object
->cputype
!= CPU_TYPE_POWERPC
);
1577 /*******************************************************************************
1578 *******************************************************************************/
1580 kxld_object_target_supports_common_symbols(const KXLDObject
*object
)
1584 return (object
->cputype
== CPU_TYPE_I386
||
1585 object
->cputype
== CPU_TYPE_POWERPC
);
1588 /*******************************************************************************
1589 *******************************************************************************/
1591 kxld_object_get_vmsize(const KXLDObject
*object
, u_long
*header_size
,
1600 /* vmsize is the padded header page(s) + segment vmsizes */
1602 *header_size
= (object
->is_final_image
) ?
1603 0 : round_page(get_macho_header_size(object
));
1604 *vmsize
= *header_size
+ get_macho_data_size(object
);
1608 /*******************************************************************************
1609 *******************************************************************************/
1611 kxld_object_export_linked_object(const KXLDObject
*object
,
1612 u_char
*linked_object
)
1614 kern_return_t rval
= KERN_FAILURE
;
1615 KXLDSeg
*seg
= NULL
;
1617 u_long header_size
= 0;
1618 u_long header_offset
= 0;
1619 u_long data_offset
= 0;
1624 check(linked_object
);
1626 /* Calculate the size of the headers and data */
1628 header_size
= get_macho_header_size(object
);
1629 data_offset
= (object
->is_final_image
) ? header_size
: round_page(header_size
);
1630 size
= data_offset
+ get_macho_data_size(object
);
1632 /* Copy data to the file */
1634 ncmds
= object
->segs
.nitems
+ (object
->uuid
.has_uuid
== TRUE
) + 1 /* linkedit */;
1636 rval
= export_macho_header(object
, linked_object
, ncmds
,
1637 &header_offset
, header_size
);
1638 require_noerr(rval
, finish
);
1640 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1641 seg
= kxld_array_get_item(&object
->segs
, i
);
1643 rval
= kxld_seg_export_macho_to_vm(seg
, linked_object
, &header_offset
,
1644 header_size
, size
, object
->link_addr
, kxld_object_is_32_bit(object
));
1645 require_noerr(rval
, finish
);
1648 seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
1649 data_offset
= (u_long
) (seg
->link_addr
- object
->link_addr
);
1650 rval
= kxld_symtab_export_macho(object
->symtab
, linked_object
, &header_offset
,
1651 header_size
, &data_offset
, size
, kxld_object_is_32_bit(object
));
1652 require_noerr(rval
, finish
);
1654 if (object
->uuid
.has_uuid
) {
1655 rval
= kxld_uuid_export_macho(&object
->uuid
, linked_object
,
1656 &header_offset
, header_size
);
1657 require_noerr(rval
, finish
);
1661 unswap_macho(linked_object
, object
->host_order
, object
->target_order
);
1664 rval
= KERN_SUCCESS
;
1670 /*******************************************************************************
1671 *******************************************************************************/
1672 static kern_return_t
1673 export_macho_header(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1674 u_long
*header_offset
, u_long header_size
)
1676 kern_return_t rval
= KERN_FAILURE
;
1680 check(header_offset
);
1682 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
1683 export_macho_header_32
, export_macho_header_64
,
1684 object
, buf
, ncmds
, header_offset
, header_size
);
1685 require_noerr(rval
, finish
);
1687 rval
= KERN_SUCCESS
;
1693 #if KXLD_USER_OR_ILP32
1694 /*******************************************************************************
1695 *******************************************************************************/
1696 static kern_return_t
1697 export_macho_header_32(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1698 u_long
*header_offset
, u_long header_size
)
1700 kern_return_t rval
= KERN_FAILURE
;
1701 struct mach_header
*mach
= NULL
;
1705 check(header_offset
);
1707 require_action(sizeof(*mach
) <= header_size
- *header_offset
, finish
,
1709 mach
= (struct mach_header
*) (buf
+ *header_offset
);
1711 mach
->magic
= MH_MAGIC
;
1712 mach
->cputype
= object
->cputype
;
1713 mach
->cpusubtype
= object
->cpusubtype
;
1714 mach
->filetype
= object
->filetype
;
1715 mach
->ncmds
= ncmds
;
1716 mach
->sizeofcmds
= (uint32_t) (header_size
- sizeof(*mach
));
1717 mach
->flags
= MH_NOUNDEFS
;
1719 *header_offset
+= sizeof(*mach
);
1721 rval
= KERN_SUCCESS
;
1726 #endif /* KXLD_USER_OR_ILP32 */
1728 #if KXLD_USER_OR_LP64
1729 /*******************************************************************************
1730 *******************************************************************************/
1731 static kern_return_t
1732 export_macho_header_64(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1733 u_long
*header_offset
, u_long header_size
)
1735 kern_return_t rval
= KERN_FAILURE
;
1736 struct mach_header_64
*mach
= NULL
;
1740 check(header_offset
);
1742 require_action(sizeof(*mach
) <= header_size
- *header_offset
, finish
,
1744 mach
= (struct mach_header_64
*) (buf
+ *header_offset
);
1746 mach
->magic
= MH_MAGIC_64
;
1747 mach
->cputype
= object
->cputype
;
1748 mach
->cpusubtype
= object
->cpusubtype
;
1749 mach
->filetype
= object
->filetype
;
1750 mach
->ncmds
= ncmds
;
1751 mach
->sizeofcmds
= (uint32_t) (header_size
- sizeof(*mach
));
1752 mach
->flags
= MH_NOUNDEFS
;
1754 *header_offset
+= sizeof(*mach
);
1756 rval
= KERN_SUCCESS
;
1761 #endif /* KXLD_USER_OR_LP64 */
1763 /*******************************************************************************
1764 *******************************************************************************/
1766 kxld_object_index_symbols_by_name(KXLDObject
*object
)
1768 return kxld_symtab_index_symbols_by_name(object
->symtab
);
1771 /*******************************************************************************
1772 *******************************************************************************/
1774 kxld_object_index_cxx_symbols_by_value(KXLDObject
*object
)
1776 return kxld_symtab_index_cxx_symbols_by_value(object
->symtab
);
1779 /*******************************************************************************
1780 *******************************************************************************/
1782 kxld_object_relocate(KXLDObject
*object
, kxld_addr_t link_address
)
1784 kern_return_t rval
= KERN_FAILURE
;
1785 KXLDSeg
*seg
= NULL
;
1790 object
->link_addr
= link_address
;
1792 /* Relocate segments (which relocates the sections) */
1793 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1794 seg
= kxld_array_get_item(&object
->segs
, i
);
1795 kxld_seg_relocate(seg
, link_address
);
1798 /* Relocate symbols */
1799 rval
= kxld_symtab_relocate(object
->symtab
, &object
->sects
);
1800 require_noerr(rval
, finish
);
1802 rval
= KERN_SUCCESS
;
1807 /*******************************************************************************
1808 *******************************************************************************/
1810 get_mutable_sym(const KXLDObject
*object
, const KXLDSym
*sym
)
1812 KXLDSym
*rval
= NULL
;
1813 kern_return_t result
= KERN_FAILURE
;
1816 result
= kxld_symtab_get_sym_index(object
->symtab
, sym
, &i
);
1817 require_noerr(result
, finish
);
1819 rval
= kxld_symtab_get_symbol_by_index(object
->symtab
, i
);
1820 require_action(rval
== sym
, finish
, rval
=NULL
);
1826 /*******************************************************************************
1827 *******************************************************************************/
1829 kxld_object_resolve_symbol(KXLDObject
*object
,
1830 const KXLDSym
*sym
, kxld_addr_t addr
)
1832 kern_return_t rval
= KERN_FAILURE
;
1833 KXLDSym
*resolved_sym
= NULL
;
1835 resolved_sym
= get_mutable_sym(object
, sym
);
1836 require_action(resolved_sym
, finish
, rval
=KERN_FAILURE
);
1838 rval
= kxld_sym_resolve(resolved_sym
, addr
);
1839 require_noerr(rval
, finish
);
1841 rval
= KERN_SUCCESS
;
1846 /*******************************************************************************
1847 *******************************************************************************/
1849 kxld_object_patch_symbol(KXLDObject
*object
, const struct kxld_sym
*sym
)
1851 kern_return_t rval
= KERN_FAILURE
;
1852 KXLDSym
*patched_sym
= NULL
;
1854 patched_sym
= get_mutable_sym(object
, sym
);
1855 require_action(patched_sym
, finish
, rval
=KERN_FAILURE
);
1857 (void) kxld_sym_patch(patched_sym
);
1858 rval
= KERN_SUCCESS
;
1863 /*******************************************************************************
1864 *******************************************************************************/
1866 kxld_object_add_symbol(KXLDObject
*object
, char *name
, kxld_addr_t link_addr
,
1867 const KXLDSym
**sym_out
)
1869 kern_return_t rval
= KERN_FAILURE
;
1870 KXLDSym
*sym
= NULL
;
1872 rval
= kxld_symtab_add_symbol(object
->symtab
, name
, link_addr
, &sym
);
1873 require_noerr(rval
, finish
);
1876 rval
= KERN_SUCCESS
;
1881 /*******************************************************************************
1882 *******************************************************************************/
1884 kxld_object_process_relocations(KXLDObject
*object
,
1885 const KXLDDict
*patched_vtables
)
1887 kern_return_t rval
= KERN_FAILURE
;
1889 (void) kxld_relocator_set_vtables(&object
->relocator
, patched_vtables
);
1891 /* Process relocation entries and populate the global offset table.
1893 * For final linked images: the relocation entries are contained in a couple
1894 * of tables hanging off the end of the symbol table. The GOT has its own
1895 * section created by the linker; we simply need to fill it.
1897 * For object files: the relocation entries are bound to each section.
1898 * The GOT, if it exists for the target architecture, is created by kxld,
1899 * and we must populate it according to our internal structures.
1901 if (object
->is_final_image
) {
1902 #if KXLD_USER_OR_BUNDLE
1903 rval
= process_symbol_pointers(object
);
1904 require_noerr(rval
, finish
);
1906 rval
= process_relocs_from_tables(object
);
1907 require_noerr(rval
, finish
);
1909 require_action(FALSE
, finish
, rval
=KERN_FAILURE
);
1910 #endif /* KXLD_USER_OR_BUNDLE */
1912 #if KXLD_USER_OR_GOT
1914 rval
= populate_got(object
);
1915 require_noerr(rval
, finish
);
1916 #endif /* KXLD_USER_OR_GOT */
1917 #if KXLD_USER_OR_OBJECT
1918 rval
= process_relocs_from_sections(object
);
1919 require_noerr(rval
, finish
);
1921 require_action(FALSE
, finish
, rval
=KERN_FAILURE
);
1922 #endif /* KXLD_USER_OR_OBJECT */
1925 /* Populate kmod info structure */
1926 rval
= populate_kmod_info(object
);
1927 require_noerr(rval
, finish
);
1929 rval
= KERN_SUCCESS
;
1934 #if KXLD_USER_OR_BUNDLE
1936 #define SECT_SYM_PTRS "__nl_symbol_ptr"
1938 /*******************************************************************************
1939 * Final linked images create an __nl_symbol_ptr section for the global offset
1940 * table and for symbol pointer lookups in general. Rather than use relocation
1941 * entries, the linker creates an "indirect symbol table" which stores indexes
1942 * into the symbol table corresponding to the entries of this section. This
1943 * function populates the section with the relocated addresses of those symbols.
1944 *******************************************************************************/
1945 static kern_return_t
1946 process_symbol_pointers(KXLDObject
*object
)
1948 kern_return_t rval
= KERN_FAILURE
;
1949 KXLDSect
*sect
= NULL
;
1950 KXLDSym
*sym
= NULL
;
1951 int32_t *symidx
= NULL
;
1952 u_char
*symptr
= NULL
;
1953 u_long symptrsize
= 0;
1960 require_action(object
->is_final_image
&& object
->dysymtab_hdr
,
1961 finish
, rval
=KERN_FAILURE
);
1963 /* Get the __DATA,__nl_symbol_ptr section. If it doesn't exist, we have
1967 sect
= kxld_object_get_sect_by_name(object
, SEG_DATA
, SECT_SYM_PTRS
);
1969 rval
= KERN_SUCCESS
;
1973 require_action(sect
->flags
& S_NON_LAZY_SYMBOL_POINTERS
,
1974 finish
, rval
=KERN_FAILURE
;
1975 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
1976 "Section %s,%s does not have S_NON_LAZY_SYMBOL_POINTERS flag.",
1977 SEG_DATA
, SECT_SYM_PTRS
));
1979 /* Calculate the table offset and number of entries in the section */
1981 if (kxld_object_is_32_bit(object
)) {
1982 symptrsize
= sizeof(uint32_t);
1984 symptrsize
= sizeof(uint64_t);
1987 nsyms
= (u_int
) (sect
->size
/ symptrsize
);
1988 firstsym
= sect
->reserved1
;
1990 require_action(firstsym
+ nsyms
<= object
->dysymtab_hdr
->nindirectsyms
,
1991 finish
, rval
=KERN_FAILURE
;
1992 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
));
1994 /* Iterate through the indirect symbol table and fill in the section of
1995 * symbol pointers. There are three cases:
1996 * 1) A normal symbol - put its value directly in the table
1997 * 2) An INDIRECT_SYMBOL_LOCAL - symbols that are local and already have
1998 * their offset from the start of the file in the section. Simply
1999 * add the file's link address to fill this entry.
2000 * 3) An INDIRECT_SYMBOL_ABS - prepopulated absolute symbols. No
2001 * action is required.
2004 symidx
= (int32_t *) (object
->file
+ object
->dysymtab_hdr
->indirectsymoff
);
2006 symptr
= sect
->data
;
2007 for (i
= 0; i
< nsyms
; ++i
, ++symidx
, symptr
+=symptrsize
) {
2008 if (*symidx
& INDIRECT_SYMBOL_LOCAL
) {
2009 if (*symidx
& INDIRECT_SYMBOL_ABS
) continue;
2011 add_to_ptr(symptr
, object
->link_addr
, kxld_object_is_32_bit(object
));
2013 sym
= kxld_symtab_get_symbol_by_index(object
->symtab
, *symidx
);
2014 require_action(sym
, finish
, rval
=KERN_FAILURE
);
2016 add_to_ptr(symptr
, sym
->link_addr
, kxld_object_is_32_bit(object
));
2020 rval
= KERN_SUCCESS
;
2025 /*******************************************************************************
2026 *******************************************************************************/
2028 get_seg_by_base_addr(KXLDObject
*object
, kxld_addr_t base_addr
)
2030 KXLDSeg
*seg
= NULL
;
2031 kxld_addr_t start
= 0;
2032 kxld_addr_t end
= 0;
2035 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
2036 seg
= kxld_array_get_item(&object
->segs
, i
);
2037 start
= seg
->base_addr
;
2038 end
= seg
->base_addr
+ seg
->vmsize
;
2040 if (start
<= base_addr
&& base_addr
< end
) return seg
;
2046 /*******************************************************************************
2047 *******************************************************************************/
2048 static kern_return_t
2049 process_relocs_from_tables(KXLDObject
*object
)
2051 kern_return_t rval
= KERN_FAILURE
;
2052 KXLDReloc
*reloc
= NULL
;
2053 KXLDSeg
*seg
= NULL
;
2056 /* Process external relocations */
2057 for (i
= 0; i
< object
->extrelocs
.nitems
; ++i
) {
2058 reloc
= kxld_array_get_item(&object
->extrelocs
, i
);
2060 seg
= get_seg_by_base_addr(object
, reloc
->address
);
2061 require_action(seg
, finish
, rval
=KERN_FAILURE
);
2063 rval
= kxld_relocator_process_table_reloc(&object
->relocator
, reloc
,
2064 seg
, object
->link_addr
);
2065 require_noerr(rval
, finish
);
2068 /* Process local relocations */
2069 for (i
= 0; i
< object
->locrelocs
.nitems
; ++i
) {
2070 reloc
= kxld_array_get_item(&object
->locrelocs
, i
);
2072 seg
= get_seg_by_base_addr(object
, reloc
->address
);
2073 require_action(seg
, finish
, rval
=KERN_FAILURE
);
2075 rval
= kxld_relocator_process_table_reloc(&object
->relocator
, reloc
,
2076 seg
, object
->link_addr
);
2077 require_noerr(rval
, finish
);
2080 rval
= KERN_SUCCESS
;
2085 /*******************************************************************************
2086 *******************************************************************************/
2088 add_to_ptr(u_char
*symptr
, kxld_addr_t val
, boolean_t is_32_bit
)
2091 uint32_t *ptr
= (uint32_t *) symptr
;
2092 *ptr
+= (uint32_t) val
;
2094 uint64_t *ptr
= (uint64_t *) symptr
;
2095 *ptr
+= (uint64_t) val
;
2098 #endif /* KXLD_USER_OR_BUNDLE */
2100 #if KXLD_USER_OR_OBJECT
2101 /*******************************************************************************
2102 *******************************************************************************/
2103 static kern_return_t
2104 process_relocs_from_sections(KXLDObject
*object
)
2106 kern_return_t rval
= KERN_FAILURE
;
2107 KXLDSect
*sect
= NULL
;
2110 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
2111 sect
= kxld_array_get_item(&object
->sects
, i
);
2112 rval
= kxld_sect_process_relocs(sect
, &object
->relocator
);
2113 require_noerr(rval
, finish
);
2116 rval
= KERN_SUCCESS
;
2120 #endif /* KXLD_USER_OR_OBJECT */
2122 /*******************************************************************************
2123 *******************************************************************************/
2124 static kern_return_t
2125 populate_kmod_info(KXLDObject
*object
)
2127 kern_return_t rval
= KERN_FAILURE
;
2128 KXLDSect
*kmodsect
= NULL
;
2129 KXLDSym
*kmodsym
= NULL
;
2130 kmod_info_t
*kmod_info
= NULL
;
2131 u_long kmod_offset
= 0;
2135 if (kxld_object_is_kernel(object
)) {
2136 rval
= KERN_SUCCESS
;
2140 kxld_object_get_vmsize(object
, &header_size
, &size
);
2142 kmodsym
= kxld_symtab_get_locally_defined_symbol_by_name(object
->symtab
,
2143 KXLD_KMOD_INFO_SYMBOL
);
2144 require_action(kmodsym
, finish
, rval
=KERN_FAILURE
;
2145 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogNoKmodInfo
));
2147 kmodsect
= kxld_array_get_item(&object
->sects
, kmodsym
->sectnum
);
2148 kmod_offset
= (u_long
) (kmodsym
->base_addr
- kmodsect
->base_addr
);
2149 kmod_info
= (kmod_info_t
*) (kmodsect
->data
+ kmod_offset
);
2151 if (kxld_object_is_32_bit(object
)) {
2152 kmod_info_32_v1_t
*kmod
= (kmod_info_32_v1_t
*) (kmod_info
);
2153 kmod
->address
= (uint32_t) object
->link_addr
;
2154 kmod
->size
= (uint32_t) size
;
2155 kmod
->hdr_size
= (uint32_t) header_size
;
2158 if (kxld_object_target_needs_swap(object
)) {
2159 kmod
->address
= OSSwapInt32(kmod
->address
);
2160 kmod
->size
= OSSwapInt32(kmod
->size
);
2161 kmod
->hdr_size
= OSSwapInt32(kmod
->hdr_size
);
2163 #endif /* !KERNEL */
2165 kmod_info_64_v1_t
*kmod
= (kmod_info_64_v1_t
*) (kmod_info
);
2166 kmod
->address
= object
->link_addr
;
2168 kmod
->hdr_size
= header_size
;
2171 if (kxld_object_target_needs_swap(object
)) {
2172 kmod
->address
= OSSwapInt64(kmod
->address
);
2173 kmod
->size
= OSSwapInt64(kmod
->size
);
2174 kmod
->hdr_size
= OSSwapInt64(kmod
->hdr_size
);
2176 #endif /* !KERNEL */
2180 rval
= KERN_SUCCESS
;