2 * Copyright (c) 2009-2014 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@
29 #include <sys/types.h>
32 #include <libkern/kernel_mach_header.h>
33 #include <mach/machine.h>
34 #include <mach/vm_param.h>
35 #include <mach-o/fat.h>
37 /* Get machine.h from the kernel source so we can support all platforms
38 * that the kernel supports. Otherwise we're at the mercy of the host.
40 #include "../../osfmk/mach/machine.h"
42 #include <architecture/byte_order.h>
43 #include <mach/mach_init.h>
44 #include <mach-o/arch.h>
45 #include <mach-o/swap.h>
48 #include <mach-o/loader.h>
49 #include <mach-o/nlist.h>
50 #include <mach-o/reloc.h>
52 #define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
53 #include <AssertMacros.h>
55 #include "kxld_demangle.h"
56 #include "kxld_dict.h"
57 #include "kxld_reloc.h"
58 #include "kxld_sect.h"
60 #include "kxld_srcversion.h"
61 #include "kxld_symtab.h"
62 #include "kxld_util.h"
63 #include "kxld_uuid.h"
64 #include "kxld_versionmin.h"
65 #include "kxld_vtable.h"
67 #include "kxld_object.h"
69 /*******************************************************************************
71 *******************************************************************************/
79 cpu_subtype_t cpusubtype
;
84 KXLDRelocator relocator
;
86 KXLDversionmin versionmin
;
87 KXLDsrcversion srcversion
;
89 struct dysymtab_command
*dysymtab_hdr
;
90 kxld_addr_t link_addr
;
91 u_long output_buffer_size
;
93 boolean_t is_final_image
;
95 boolean_t got_is_created
;
96 #if KXLD_USER_OR_OBJECT
97 KXLDArray
*section_order
;
100 boolean_t include_kaslr_relocs
;
103 enum NXByteOrder host_order
;
104 enum NXByteOrder target_order
;
108 /*******************************************************************************
110 *******************************************************************************/
112 static kern_return_t
get_target_machine_info(KXLDObject
*object
,
113 cpu_type_t cputype
, cpu_subtype_t cpusubtype
);
114 static kern_return_t
get_macho_slice_for_arch(KXLDObject
*object
,
115 u_char
*file
, u_long size
);
117 static u_long
get_macho_header_size(const KXLDObject
*object
);
118 static u_long
get_macho_data_size(const KXLDObject
*object
) __unused
;
120 static kern_return_t
init_from_execute(KXLDObject
*object
);
121 static kern_return_t
init_from_final_linked_image(KXLDObject
*object
,
122 u_int
*filetype_out
, struct symtab_command
**symtab_hdr_out
);
124 static boolean_t
target_supports_protected_segments(const KXLDObject
*object
)
125 __attribute__((pure
));
126 static void set_is_object_linked(KXLDObject
*object
);
128 #if KXLD_USER_OR_BUNDLE
129 static boolean_t
target_supports_bundle(const KXLDObject
*object
)
131 static kern_return_t
init_from_bundle(KXLDObject
*object
);
132 static kern_return_t
process_relocs_from_tables(KXLDObject
*object
);
133 static KXLDSeg
*get_seg_by_base_addr(KXLDObject
*object
,
134 kxld_addr_t base_addr
);
135 static kern_return_t
process_symbol_pointers(KXLDObject
*object
);
136 static void add_to_ptr(u_char
*symptr
, kxld_addr_t val
, boolean_t is_32_bit
);
137 #endif /* KXLD_USER_OR_BUNDLE */
139 #if KXLD_USER_OR_OBJECT
140 static boolean_t
target_supports_object(const KXLDObject
*object
)
142 static kern_return_t
init_from_object(KXLDObject
*object
);
143 static kern_return_t
process_relocs_from_sections(KXLDObject
*object
);
144 #endif /* KXLD_USER_OR_OBJECT */
147 static boolean_t
target_supports_slideable_kexts(const KXLDObject
*object
);
148 #endif /* KXLD_PIC_KEXTS */
151 static kern_return_t
export_macho_header(const KXLDObject
*object
, u_char
*buf
,
152 u_int ncmds
, u_long
*header_offset
, u_long header_size
);
153 #if KXLD_USER_OR_ILP32
154 static u_long
get_macho_cmd_data_32(u_char
*file
, u_long offset
,
155 u_int
*filetype
, u_int
*ncmds
);
156 static kern_return_t
export_macho_header_32(const KXLDObject
*object
,
157 u_char
*buf
, u_int ncmds
, u_long
*header_offset
, u_long header_size
);
158 #endif /* KXLD_USER_OR_ILP32 */
159 #if KXLD_USER_OR_LP64
160 static u_long
get_macho_cmd_data_64(u_char
*file
, u_long offset
,
161 u_int
*filetype
, u_int
*ncmds
);
162 static kern_return_t
export_macho_header_64(const KXLDObject
*object
,
163 u_char
*buf
, u_int ncmds
, u_long
*header_offset
, u_long header_size
);
164 #endif /* KXLD_USER_OR_LP64 */
166 #if KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON
167 static kern_return_t
add_section(KXLDObject
*object
, KXLDSect
**sect
);
168 #endif /* KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON */
170 #if KXLD_USER_OR_COMMON
171 static kern_return_t
resolve_common_symbols(KXLDObject
*object
);
172 #endif /* KXLD_USER_OR_COMMON */
175 static boolean_t
target_has_got(const KXLDObject
*object
) __attribute__((pure
));
176 static kern_return_t
create_got(KXLDObject
*object
);
177 static kern_return_t
populate_got(KXLDObject
*object
);
178 #endif /* KXLD_USER_OR_GOT */
180 static KXLDSym
*get_mutable_sym(const KXLDObject
*object
, const KXLDSym
*sym
);
182 static kern_return_t
populate_kmod_info(KXLDObject
*object
);
184 /*******************************************************************************
185 * Prototypes that may need to be exported
186 *******************************************************************************/
187 static boolean_t
kxld_object_target_needs_swap(const KXLDObject
*object __unused
);
188 static KXLDSeg
* kxld_object_get_seg_by_name(const KXLDObject
*object
, const char *segname
);
189 static KXLDSect
* kxld_object_get_sect_by_name(const KXLDObject
*object
, const char *segname
,
190 const char *sectname
);
192 /*******************************************************************************
193 *******************************************************************************/
195 kxld_object_sizeof(void)
197 return sizeof(KXLDObject
);
200 /*******************************************************************************
201 *******************************************************************************/
203 kxld_object_init_from_macho(KXLDObject
*object
, u_char
*file
, u_long size
,
204 const char *name
, KXLDArray
*section_order __unused
,
205 cpu_type_t cputype
, cpu_subtype_t cpusubtype
, KXLDFlags flags __unused
)
207 kern_return_t rval
= KERN_FAILURE
;
208 KXLDSeg
* seg
= NULL
;
217 #if KXLD_USER_OR_OBJECT
218 object
->section_order
= section_order
;
221 object
->include_kaslr_relocs
= ((flags
& kKXLDFlagIncludeRelocs
) == kKXLDFlagIncludeRelocs
);
224 /* Find the local architecture */
226 rval
= get_target_machine_info(object
, cputype
, cpusubtype
);
227 require_noerr(rval
, finish
);
229 /* Find the Mach-O slice for the target architecture */
231 rval
= get_macho_slice_for_arch(object
, file
, size
);
232 require_noerr(rval
, finish
);
234 /* Allocate the symbol table */
236 if (!object
->symtab
) {
237 object
->symtab
= kxld_alloc(kxld_symtab_sizeof());
238 require_action(object
->symtab
, finish
, rval
=KERN_RESOURCE_SHORTAGE
);
239 bzero(object
->symtab
, kxld_symtab_sizeof());
242 /* Build the relocator */
244 rval
= kxld_relocator_init(&object
->relocator
, object
->file
,
245 object
->symtab
, &object
->sects
, object
->cputype
,
246 object
->cpusubtype
, kxld_object_target_needs_swap(object
));
247 require_noerr(rval
, finish
);
249 /* There are four types of Mach-O files that we can support:
250 * 1) 32-bit MH_OBJECT - Snow Leopard and earlier
251 * 2) 32-bit MH_KEXT_BUNDLE - Lion and Later
252 * 3) 64-bit MH_OBJECT - Unsupported
253 * 4) 64-bit MH_KEXT_BUNDLE - Snow Leopard and Later
256 if (kxld_object_is_32_bit(object
)) {
257 struct mach_header
*mach_hdr
= (struct mach_header
*) ((void *) object
->file
);
258 object
->filetype
= mach_hdr
->filetype
;
260 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) ((void *) object
->file
);
261 object
->filetype
= mach_hdr
->filetype
;
264 switch (object
->filetype
) {
265 #if KXLD_USER_OR_BUNDLE
267 rval
= init_from_bundle(object
);
268 require_noerr(rval
, finish
);
270 #endif /* KXLD_USER_OR_BUNDLE */
271 #if KXLD_USER_OR_OBJECT
273 rval
= init_from_object(object
);
274 require_noerr(rval
, finish
);
276 #endif /* KXLD_USER_OR_OBJECT */
278 object
->is_kernel
= TRUE
;
279 rval
= init_from_execute(object
);
280 require_noerr(rval
, finish
);
284 kxld_log(kKxldLogLinking
, kKxldLogErr
,
285 kKxldLogFiletypeNotSupported
, object
->filetype
);
289 if (!kxld_object_is_kernel(object
)) {
290 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
291 seg
= kxld_array_get_item(&object
->segs
, i
);
292 kxld_seg_set_vm_protections(seg
,
293 target_supports_protected_segments(object
));
296 seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
298 (void) kxld_seg_populate_linkedit(seg
, object
->symtab
,
299 kxld_object_is_32_bit(object
)
301 , &object
->locrelocs
, &object
->extrelocs
,
302 target_supports_slideable_kexts(object
)
308 (void) set_is_object_linked(object
);
315 /*******************************************************************************
316 *******************************************************************************/
318 get_target_machine_info(KXLDObject
*object
, cpu_type_t cputype __unused
,
319 cpu_subtype_t cpusubtype __unused
)
323 /* Because the kernel can only link for its own architecture, we know what
324 * the host and target architectures are at compile time, so we can use
325 * a vastly simplified version of this function.
330 #if defined(__x86_64__)
331 object
->cputype
= CPU_TYPE_X86_64
;
332 /* FIXME: we need clang to provide a __x86_64h__ macro for the sub-type. Using
333 * __AVX2__ is a temporary solution until this is available. */
334 #if defined(__AVX2__)
335 object
->cpusubtype
= CPU_SUBTYPE_X86_64_H
;
337 object
->cpusubtype
= CPU_SUBTYPE_X86_64_ALL
;
341 kxld_log(kKxldLogLinking
, kKxldLogErr
,
342 kKxldLogArchNotSupported
, _mh_execute_header
->cputype
);
343 return KERN_NOT_SUPPORTED
;
344 #endif /* Supported architecture defines */
349 /* User-space must look up the architecture it's running on and the target
350 * architecture at run-time.
353 kern_return_t rval
= KERN_FAILURE
;
354 const NXArchInfo
*host_arch
= NULL
;
358 host_arch
= NXGetLocalArchInfo();
359 require_action(host_arch
, finish
, rval
=KERN_FAILURE
);
361 object
->host_order
= host_arch
->byteorder
;
363 /* If the user did not specify a cputype, use the local architecture.
367 object
->cputype
= cputype
;
368 object
->cpusubtype
= cpusubtype
;
370 object
->cputype
= host_arch
->cputype
;
371 object
->target_order
= object
->host_order
;
373 switch (object
->cputype
) {
375 object
->cpusubtype
= CPU_SUBTYPE_I386_ALL
;
377 case CPU_TYPE_X86_64
:
378 object
->cpusubtype
= CPU_SUBTYPE_X86_64_ALL
;
381 object
->cpusubtype
= CPU_SUBTYPE_ARM_ALL
;
384 object
->cpusubtype
= CPU_SUBTYPE_ARM64_ALL
;
387 object
->cpusubtype
= 0;
392 /* Validate that we support the target architecture and record its
396 switch(object
->cputype
) {
400 case CPU_TYPE_X86_64
:
401 object
->target_order
= NX_LittleEndian
;
404 rval
= KERN_NOT_SUPPORTED
;
405 kxld_log(kKxldLogLinking
, kKxldLogErr
,
406 kKxldLogArchNotSupported
, object
->cputype
);
417 /*******************************************************************************
418 *******************************************************************************/
420 get_macho_slice_for_arch(KXLDObject
*object
, u_char
*file
, u_long size
)
422 kern_return_t rval
= KERN_FAILURE
;
423 struct mach_header
*mach_hdr
= NULL
;
425 struct fat_header
*fat
= (struct fat_header
*) ((void *) file
);
426 struct fat_arch
*archs
= (struct fat_arch
*) &fat
[1];
427 boolean_t swap
= FALSE
;
437 /* We are assuming that we will never receive a fat file in the kernel */
440 require_action(size
>= sizeof(*fat
), finish
,
442 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
444 /* The fat header is always big endian, so swap if necessary */
445 if (fat
->magic
== FAT_CIGAM
) {
446 (void) swap_fat_header(fat
, object
->host_order
);
450 if (fat
->magic
== FAT_MAGIC
) {
451 struct fat_arch
*arch
= NULL
;
453 require_action(size
>= (sizeof(*fat
) + (fat
->nfat_arch
* sizeof(*archs
))),
456 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
458 /* Swap the fat_arch structures if necessary */
460 (void) swap_fat_arch(archs
, fat
->nfat_arch
, object
->host_order
);
463 /* Locate the Mach-O for the requested architecture */
465 arch
= NXFindBestFatArch(object
->cputype
, object
->cpusubtype
, archs
, fat
->nfat_arch
);
466 require_action(arch
, finish
, rval
=KERN_FAILURE
;
467 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogArchNotFound
));
468 require_action(size
>= arch
->offset
+ arch
->size
, finish
,
470 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
472 object
->file
= file
+ arch
->offset
;
473 object
->size
= arch
->size
;
477 /* Swap the Mach-O's headers to this architecture if necessary */
478 if (kxld_object_is_32_bit(object
)) {
479 rval
= validate_and_swap_macho_32(object
->file
, object
->size
485 rval
= validate_and_swap_macho_64(object
->file
, object
->size
491 require_noerr(rval
, finish
);
493 mach_hdr
= (struct mach_header
*) ((void *) object
->file
);
494 require_action(object
->cputype
== mach_hdr
->cputype
, finish
,
496 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
497 object
->cpusubtype
= mach_hdr
->cpusubtype
; /* <rdar://problem/16008438> */
504 /*******************************************************************************
505 *******************************************************************************/
507 init_from_final_linked_image(KXLDObject
*object
, u_int
*filetype_out
,
508 struct symtab_command
**symtab_hdr_out
)
510 kern_return_t rval
= KERN_FAILURE
;
512 KXLDSect
*sect
= NULL
;
513 struct load_command
*cmd_hdr
= NULL
;
514 struct symtab_command
*symtab_hdr
= NULL
;
515 struct uuid_command
*uuid_hdr
= NULL
;
516 struct version_min_command
*versionmin_hdr
= NULL
;
517 struct source_version_command
*source_version_hdr
= NULL
;
518 u_long base_offset
= 0;
520 u_long sect_offset
= 0;
530 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), base_offset
,
531 get_macho_cmd_data_32
, get_macho_cmd_data_64
,
532 object
->file
, offset
, &filetype
, &ncmds
);
534 /* First pass to count segments and sections */
536 offset
= base_offset
;
537 for (i
= 0; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
538 cmd_hdr
= (struct load_command
*) ((void *) (object
->file
+ offset
));
540 switch(cmd_hdr
->cmd
) {
541 #if KXLD_USER_OR_ILP32
544 struct segment_command
*seg_hdr
=
545 (struct segment_command
*) cmd_hdr
;
547 /* Ignore segments with no vm size */
548 if (!seg_hdr
->vmsize
) continue;
551 nsects
+= seg_hdr
->nsects
;
554 #endif /* KXLD_USER_OR_ILP32 */
555 #if KXLD_USER_OR_LP64
558 struct segment_command_64
*seg_hdr
=
559 (struct segment_command_64
*) ((void *) cmd_hdr
);
561 /* Ignore segments with no vm size */
562 if (!seg_hdr
->vmsize
) continue;
565 nsects
+= seg_hdr
->nsects
;
568 #endif /* KXLD_USER_OR_LP64 */
574 /* Allocate the segments and sections */
577 rval
= kxld_array_init(&object
->segs
, sizeof(KXLDSeg
), nsegs
);
578 require_noerr(rval
, finish
);
580 rval
= kxld_array_init(&object
->sects
, sizeof(KXLDSect
), nsects
);
581 require_noerr(rval
, finish
);
584 /* Initialize the segments and sections */
586 offset
= base_offset
;
587 for (i
= 0; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
588 cmd_hdr
= (struct load_command
*) ((void *) (object
->file
+ offset
));
591 switch(cmd_hdr
->cmd
) {
592 #if KXLD_USER_OR_ILP32
595 struct segment_command
*seg_hdr
=
596 (struct segment_command
*) cmd_hdr
;
598 /* Ignore segments with no vm size */
599 if (!seg_hdr
->vmsize
) continue;
601 seg
= kxld_array_get_item(&object
->segs
, segi
++);
603 rval
= kxld_seg_init_from_macho_32(seg
, seg_hdr
);
604 require_noerr(rval
, finish
);
606 sect_offset
= offset
+ sizeof(*seg_hdr
);
609 #endif /* KXLD_USER_OR_ILP32 */
610 #if KXLD_USER_OR_LP64
613 struct segment_command_64
*seg_hdr
=
614 (struct segment_command_64
*) ((void *) cmd_hdr
);
616 /* Ignore segments with no vm size */
617 if (!seg_hdr
->vmsize
) continue;
619 seg
= kxld_array_get_item(&object
->segs
, segi
++);
621 rval
= kxld_seg_init_from_macho_64(seg
, seg_hdr
);
622 require_noerr(rval
, finish
);
624 sect_offset
= offset
+ sizeof(*seg_hdr
);
627 #endif /* KXLD_USER_OR_LP64 */
629 symtab_hdr
= (struct symtab_command
*) cmd_hdr
;
632 uuid_hdr
= (struct uuid_command
*) cmd_hdr
;
633 kxld_uuid_init_from_macho(&object
->uuid
, uuid_hdr
);
635 case LC_VERSION_MIN_MACOSX
:
636 case LC_VERSION_MIN_IPHONEOS
:
637 versionmin_hdr
= (struct version_min_command
*) cmd_hdr
;
638 kxld_versionmin_init_from_macho(&object
->versionmin
, versionmin_hdr
);
640 case LC_SOURCE_VERSION
:
641 source_version_hdr
= (struct source_version_command
*) (void *) cmd_hdr
;
642 kxld_srcversion_init_from_macho(&object
->srcversion
, source_version_hdr
);
645 object
->dysymtab_hdr
= (struct dysymtab_command
*) cmd_hdr
;
647 rval
= kxld_reloc_create_macho(&object
->extrelocs
, &object
->relocator
,
648 (struct relocation_info
*) ((void *) (object
->file
+ object
->dysymtab_hdr
->extreloff
)),
649 object
->dysymtab_hdr
->nextrel
);
650 require_noerr(rval
, finish
);
652 rval
= kxld_reloc_create_macho(&object
->locrelocs
, &object
->relocator
,
653 (struct relocation_info
*) ((void *) (object
->file
+ object
->dysymtab_hdr
->locreloff
)),
654 object
->dysymtab_hdr
->nlocrel
);
655 require_noerr(rval
, finish
);
660 /* Don't need to do anything with UNIXTHREAD or MAIN for the kernel */
661 require_action(kxld_object_is_kernel(object
),
662 finish
, rval
=KERN_FAILURE
;
663 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
664 "LC_UNIXTHREAD/LC_MAIN segment is not valid in a kext."));
666 case LC_SEGMENT_SPLIT_INFO
:
667 /* To be implemented later; treat as uninteresting for now */
668 case LC_CODE_SIGNATURE
:
670 case LC_DYLD_INFO_ONLY
:
671 case LC_FUNCTION_STARTS
:
672 case LC_DATA_IN_CODE
:
673 case LC_DYLIB_CODE_SIGN_DRS
:
674 /* Various metadata that might be stored in the linkedit segment */
678 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
679 "Invalid segment type in MH_KEXT_BUNDLE kext: %u.", cmd_hdr
->cmd
);
685 /* Initialize the sections */
686 for (j
= 0; j
< seg
->sects
.nitems
; ++j
, ++secti
) {
687 sect
= kxld_array_get_item(&object
->sects
, secti
);
688 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
689 kxld_sect_init_from_macho_32
, kxld_sect_init_from_macho_64
,
690 sect
, object
->file
, §_offset
, secti
, &object
->relocator
);
691 require_noerr(rval
, finish
);
693 /* Add the section to the segment. This will also make sure
694 * that the sections and segments have the same segname.
696 rval
= kxld_seg_add_section(seg
, sect
);
697 require_noerr(rval
, finish
);
699 rval
= kxld_seg_finish_init(seg
);
700 require_noerr(rval
, finish
);
704 if (filetype_out
) *filetype_out
= filetype
;
705 if (symtab_hdr_out
) *symtab_hdr_out
= symtab_hdr
;
706 object
->is_final_image
= TRUE
;
712 /*******************************************************************************
713 *******************************************************************************/
715 init_from_execute(KXLDObject
*object
)
717 kern_return_t rval
= KERN_FAILURE
;
718 struct symtab_command
*symtab_hdr
= NULL
;
720 KXLDSeg
* kernel_linkedit_seg
= NULL
; // used if running kernel
721 #if KXLD_USER_OR_OBJECT
723 KXLDSect
*sect
= NULL
;
724 KXLDSectionName
*sname
= NULL
;
725 u_int i
= 0, j
= 0, k
= 0;
726 #endif /* KXLD_USER_OR_OBJECT */
730 require_action(kxld_object_is_kernel(object
), finish
, rval
=KERN_FAILURE
);
732 rval
= init_from_final_linked_image(object
, &filetype
, &symtab_hdr
);
733 require_noerr(rval
, finish
);
735 require_action(filetype
== MH_EXECUTE
, finish
, rval
=KERN_FAILURE
;
736 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
737 "The kernel file is not of type MH_EXECUTE."));
739 /* Initialize the symbol table. If this is the running kernel
740 * we will work from the in-memory linkedit segment;
741 * otherwise we work from the whole mach-o image.
744 kernel_linkedit_seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
745 require_action(kernel_linkedit_seg
, finish
, rval
=KERN_FAILURE
;
746 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
));
749 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
750 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
751 object
->symtab
, symtab_hdr
, object
->file
, kernel_linkedit_seg
);
752 require_noerr(rval
, finish
);
754 #if KXLD_USER_OR_OBJECT
755 /* Save off the order of section names so that we can lay out kext
756 * sections for MH_OBJECT-based systems.
758 if (target_supports_object(object
)) {
760 rval
= kxld_array_init(object
->section_order
, sizeof(KXLDSectionName
),
761 object
->sects
.nitems
);
762 require_noerr(rval
, finish
);
764 /* Copy the section names into the section_order array for future kext
767 for (i
= 0, k
= 0; i
< object
->segs
.nitems
; ++i
) {
768 seg
= kxld_array_get_item(&object
->segs
, i
);
770 for (j
= 0; j
< seg
->sects
.nitems
; ++j
, ++k
) {
771 sect
= *(KXLDSect
**) kxld_array_get_item(&seg
->sects
, j
);
772 sname
= kxld_array_get_item(object
->section_order
, k
);
774 strlcpy(sname
->segname
, sect
->segname
, sizeof(sname
->segname
));
775 strlcpy(sname
->sectname
, sect
->sectname
, sizeof(sname
->sectname
));
779 #endif /* KXLD_USER_OR_OBJECT */
786 #if KXLD_USER_OR_BUNDLE
787 /*******************************************************************************
788 *******************************************************************************/
790 target_supports_bundle(const KXLDObject
*object __unused
)
795 /*******************************************************************************
796 *******************************************************************************/
798 init_from_bundle(KXLDObject
*object
)
800 kern_return_t rval
= KERN_FAILURE
;
801 struct symtab_command
*symtab_hdr
= NULL
;
806 require_action(target_supports_bundle(object
), finish
,
808 kxld_log(kKxldLogLinking
, kKxldLogErr
,
809 kKxldLogFiletypeNotSupported
, MH_KEXT_BUNDLE
));
811 rval
= init_from_final_linked_image(object
, &filetype
, &symtab_hdr
);
812 require_noerr(rval
, finish
);
814 require_action(filetype
== MH_KEXT_BUNDLE
, finish
,
817 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
818 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
819 object
->symtab
, symtab_hdr
, object
->file
,
820 /* kernel_linkedit_seg */ NULL
);
821 require_noerr(rval
, finish
);
827 #endif /* KXLD_USER_OR_BUNDLE */
829 #if KXLD_USER_OR_OBJECT
830 /*******************************************************************************
831 *******************************************************************************/
832 static boolean_t
target_supports_object(const KXLDObject
*object
)
834 return (object
->cputype
== CPU_TYPE_I386
);
837 /*******************************************************************************
838 *******************************************************************************/
840 init_from_object(KXLDObject
*object
)
842 kern_return_t rval
= KERN_FAILURE
;
843 struct load_command
*cmd_hdr
= NULL
;
844 struct symtab_command
*symtab_hdr
= NULL
;
845 struct uuid_command
*uuid_hdr
= NULL
;
846 KXLDSect
*sect
= NULL
;
848 u_long sect_offset
= 0;
853 boolean_t has_segment
= FALSE
;
857 require_action(target_supports_object(object
),
858 finish
, rval
=KERN_FAILURE
;
859 kxld_log(kKxldLogLinking
, kKxldLogErr
,
860 kKxldLogFiletypeNotSupported
, MH_OBJECT
));
862 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), offset
,
863 get_macho_cmd_data_32
, get_macho_cmd_data_64
,
864 object
->file
, offset
, &filetype
, &ncmds
);
866 require_action(filetype
== MH_OBJECT
, finish
, rval
=KERN_FAILURE
);
868 /* MH_OBJECTs use one unnamed segment to contain all of the sections. We
869 * loop over all of the load commands to initialize the structures we
870 * expect. Then, we'll use the unnamed segment to get to all of the
871 * sections, and then use those sections to create the actual segments.
874 for (; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
875 cmd_hdr
= (struct load_command
*) ((void *) (object
->file
+ offset
));
877 switch(cmd_hdr
->cmd
) {
878 #if KXLD_USER_OR_ILP32
881 struct segment_command
*seg_hdr
=
882 (struct segment_command
*) cmd_hdr
;
884 /* Ignore segments with no vm size */
885 if (!seg_hdr
->vmsize
) continue;
887 /* Ignore LINKEDIT segments */
888 if (streq_safe(seg_hdr
->segname
, SEG_LINKEDIT
,
889 const_strlen(SEG_LINKEDIT
)))
894 require_action(kxld_object_is_32_bit(object
), finish
, rval
=KERN_FAILURE
;
895 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
896 "LC_SEGMENT in 64-bit kext."));
897 require_action(!has_segment
, finish
, rval
=KERN_FAILURE
;
898 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
899 "Multiple segments in an MH_OBJECT kext."));
901 nsects
= seg_hdr
->nsects
;
902 sect_offset
= offset
+ sizeof(*seg_hdr
);
906 #endif /* KXLD_USER_OR_ILP32 */
907 #if KXLD_USER_OR_LP64
910 struct segment_command_64
*seg_hdr
=
911 (struct segment_command_64
*) ((void *) cmd_hdr
);
913 /* Ignore segments with no vm size */
914 if (!seg_hdr
->vmsize
) continue;
916 /* Ignore LINKEDIT segments */
917 if (streq_safe(seg_hdr
->segname
, SEG_LINKEDIT
,
918 const_strlen(SEG_LINKEDIT
)))
923 require_action(!kxld_object_is_32_bit(object
), finish
, rval
=KERN_FAILURE
;
924 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
925 "LC_SEGMENT_64 in a 32-bit kext."));
926 require_action(!has_segment
, finish
, rval
=KERN_FAILURE
;
927 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
928 "Multiple segments in an MH_OBJECT kext."));
930 nsects
= seg_hdr
->nsects
;
931 sect_offset
= offset
+ sizeof(*seg_hdr
);
935 #endif /* KXLD_USER_OR_LP64 */
937 symtab_hdr
= (struct symtab_command
*) cmd_hdr
;
939 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
940 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
941 object
->symtab
, symtab_hdr
, object
->file
,
942 /* kernel_linkedit_seg */ NULL
);
943 require_noerr(rval
, finish
);
946 uuid_hdr
= (struct uuid_command
*) cmd_hdr
;
947 kxld_uuid_init_from_macho(&object
->uuid
, uuid_hdr
);
951 /* Don't need to do anything with UNIXTHREAD or MAIN */
953 case LC_CODE_SIGNATURE
:
955 case LC_DYLD_INFO_ONLY
:
956 case LC_FUNCTION_STARTS
:
957 case LC_DATA_IN_CODE
:
958 case LC_DYLIB_CODE_SIGN_DRS
:
959 /* Various metadata that might be stored in the linkedit segment */
961 case LC_VERSION_MIN_MACOSX
:
962 case LC_VERSION_MIN_IPHONEOS
:
963 case LC_SOURCE_VERSION
:
964 /* Not supported for object files, fall through */
967 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
968 "Invalid segment type in MH_OBJECT kext: %u.", cmd_hdr
->cmd
);
975 /* Get the number of sections from the segment and build the section index */
977 rval
= kxld_array_init(&object
->sects
, sizeof(KXLDSect
), nsects
);
978 require_noerr(rval
, finish
);
980 /* Loop over all of the sections to initialize the section index */
982 for (i
= 0; i
< nsects
; ++i
) {
983 sect
= kxld_array_get_item(&object
->sects
, i
);
984 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
985 kxld_sect_init_from_macho_32
, kxld_sect_init_from_macho_64
,
986 sect
, object
->file
, §_offset
, i
, &object
->relocator
);
987 require_noerr(rval
, finish
);
990 /* Create special sections */
993 rval
= create_got(object
);
994 require_noerr(rval
, finish
);
995 #endif /* KXLD_USER_OR_GOT */
997 #if KXLD_USER_OR_COMMON
998 rval
= resolve_common_symbols(object
);
999 require_noerr(rval
, finish
);
1000 #endif /* KXLD_USER_OR_COMMON */
1002 /* Create the segments from the section index */
1004 rval
= kxld_seg_create_seg_from_sections(&object
->segs
, &object
->sects
);
1005 require_noerr(rval
, finish
);
1007 rval
= kxld_seg_finalize_object_segment(&object
->segs
,
1008 object
->section_order
, get_macho_header_size(object
));
1009 require_noerr(rval
, finish
);
1011 rval
= kxld_seg_init_linkedit(&object
->segs
);
1012 require_noerr(rval
, finish
);
1015 rval
= KERN_SUCCESS
;
1019 #endif /* KXLD_USER_OR_OBJECT */
1021 #if KXLD_USER_OR_ILP32
1022 /*******************************************************************************
1023 *******************************************************************************/
1025 get_macho_cmd_data_32(u_char
*file
, u_long offset
, u_int
*filetype
, u_int
*ncmds
)
1027 struct mach_header
*mach_hdr
= (struct mach_header
*) ((void *) (file
+ offset
));
1029 if (filetype
) *filetype
= mach_hdr
->filetype
;
1030 if (ncmds
) *ncmds
= mach_hdr
->ncmds
;
1032 return sizeof(*mach_hdr
);
1035 #endif /* KXLD_USER_OR_ILP32 */
1037 #if KXLD_USER_OR_LP64
1038 /*******************************************************************************
1039 *******************************************************************************/
1041 get_macho_cmd_data_64(u_char
*file
, u_long offset
, u_int
*filetype
, u_int
*ncmds
)
1043 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) ((void *) (file
+ offset
));
1045 if (filetype
) *filetype
= mach_hdr
->filetype
;
1046 if (ncmds
) *ncmds
= mach_hdr
->ncmds
;
1048 return sizeof(*mach_hdr
);
1050 #endif /* KXLD_USER_OR_LP64 */
1052 /*******************************************************************************
1053 *******************************************************************************/
1055 get_macho_header_size(const KXLDObject
*object
)
1057 KXLDSeg
*seg
= NULL
;
1058 u_long header_size
= 0;
1060 boolean_t object_is_32_bit
= kxld_object_is_32_bit(object
);
1064 /* Mach, segment, symtab, and UUID headers */
1066 header_size
+= object_is_32_bit
? sizeof(struct mach_header
) : sizeof(struct mach_header_64
);
1068 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1069 seg
= kxld_array_get_item(&object
->segs
, i
);
1070 header_size
+= kxld_seg_get_macho_header_size(seg
, object_is_32_bit
);
1073 header_size
+= kxld_symtab_get_macho_header_size();
1076 if (target_supports_slideable_kexts(object
)) {
1077 header_size
+= kxld_reloc_get_macho_header_size();
1079 #endif /* KXLD_PIC_KEXTS */
1081 if (object
->uuid
.has_uuid
) {
1082 header_size
+= kxld_uuid_get_macho_header_size();
1085 if (object
->versionmin
.has_versionmin
) {
1086 header_size
+= kxld_versionmin_get_macho_header_size();
1089 if (object
->srcversion
.has_srcversion
) {
1090 header_size
+= kxld_srcversion_get_macho_header_size();
1096 /*******************************************************************************
1097 *******************************************************************************/
1099 get_macho_data_size(const KXLDObject
*object
)
1101 KXLDSeg
*seg
= NULL
;
1102 u_long data_size
= 0;
1107 /* total all segment vmsize values */
1108 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1109 seg
= kxld_array_get_item(&object
->segs
, i
);
1110 data_size
+= (u_long
) kxld_seg_get_vmsize(seg
);
1115 /* ensure that when we eventually emit the final linked object,
1116 * appending the __DYSYMTAB data after the __LINKEDIT data will
1117 * not overflow the space allocated for the __LINKEDIT segment
1120 u_long seg_vmsize
= 0;
1121 u_long symtab_size
= 0;
1122 u_long reloc_size
= 0;
1124 /* get current __LINKEDIT sizes */
1125 seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
1126 seg_vmsize
= (u_long
) kxld_seg_get_vmsize(seg
);
1128 /* get size of symbol table data that will eventually be dumped
1129 * into the __LINKEDIT segment
1131 symtab_size
= kxld_symtab_get_macho_data_size(object
->symtab
, kxld_object_is_32_bit(object
));
1133 if (target_supports_slideable_kexts(object
)) {
1134 /* get size of __DYSYMTAB relocation entries */
1135 reloc_size
= kxld_reloc_get_macho_data_size(&object
->locrelocs
, &object
->extrelocs
);
1138 /* combine, and ensure they'll both fit within the page(s)
1139 * allocated for the __LINKEDIT segment. If they'd overflow,
1140 * increase the vmsize appropriately so no overflow will occur
1142 if ((symtab_size
+ reloc_size
) > seg_vmsize
) {
1143 u_long overflow
= (symtab_size
+ reloc_size
) - seg_vmsize
;
1144 data_size
+= round_page(overflow
);
1147 #endif // KXLD_PIC_KEXTS
1152 /*******************************************************************************
1153 *******************************************************************************/
1155 kxld_object_target_needs_swap(const KXLDObject
*object __unused
)
1160 return (object
->target_order
!= object
->host_order
);
1164 /*******************************************************************************
1165 *******************************************************************************/
1167 kxld_object_get_seg_by_name(const KXLDObject
*object
, const char *segname
)
1169 KXLDSeg
*seg
= NULL
;
1172 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1173 seg
= kxld_array_get_item(&object
->segs
, i
);
1175 if (streq_safe(segname
, seg
->segname
, sizeof(seg
->segname
))) break;
1183 /*******************************************************************************
1184 *******************************************************************************/
1185 const KXLDRelocator
*
1186 kxld_object_get_relocator(const KXLDObject
* object
)
1190 return &object
->relocator
;
1193 /*******************************************************************************
1194 *******************************************************************************/
1196 kxld_object_get_sect_by_name(const KXLDObject
*object
, const char *segname
,
1197 const char *sectname
)
1199 KXLDSect
*sect
= NULL
;
1202 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1203 sect
= kxld_array_get_item(&object
->sects
, i
);
1205 if (streq_safe(segname
, sect
->segname
, sizeof(sect
->segname
)) &&
1206 streq_safe(sectname
, sect
->sectname
, sizeof(sect
->sectname
)))
1217 /*******************************************************************************
1218 *******************************************************************************/
1220 kxld_object_get_reloc_at_symbol(const KXLDObject
*object
, const KXLDSym
*sym
)
1222 const KXLDReloc
*reloc
= NULL
;
1223 const KXLDSect
*sect
= NULL
;
1224 uint32_t offset
= 0;
1229 sect
= kxld_object_get_section_by_index(object
, sym
->sectnum
);
1230 require(sect
, finish
);
1232 if (kxld_object_is_final_image(object
)) {
1233 reloc
= kxld_reloc_get_reloc_by_offset(&object
->extrelocs
,
1236 reloc
= kxld_reloc_get_reloc_by_offset(&object
->locrelocs
,
1240 offset
= kxld_sym_get_section_offset(sym
, sect
);
1241 reloc
= kxld_reloc_get_reloc_by_offset(§
->relocs
, offset
);
1248 /*******************************************************************************
1249 *******************************************************************************/
1251 kxld_object_get_symbol_of_reloc(const KXLDObject
*object
,
1252 const KXLDReloc
*reloc
, const KXLDSect
*sect
)
1254 const KXLDSym
*sym
= NULL
;
1256 if (kxld_object_is_final_image(object
)) {
1257 sym
= kxld_reloc_get_symbol(&object
->relocator
, reloc
, object
->file
);
1259 sym
= kxld_reloc_get_symbol(&object
->relocator
, reloc
, sect
->data
);
1265 /*******************************************************************************
1266 *******************************************************************************/
1268 kxld_object_get_section_by_index(const KXLDObject
*object
, u_int sectnum
)
1270 KXLDSect
*sect
= NULL
;
1274 if (sectnum
< object
->sects
.nitems
) {
1275 sect
= kxld_array_get_item(&object
->sects
, sectnum
);
1281 /*******************************************************************************
1282 *******************************************************************************/
1284 kxld_object_get_extrelocs(const KXLDObject
*object
)
1286 const KXLDArray
*rval
= NULL
;
1290 if (kxld_object_is_final_image(object
)) {
1291 rval
= &object
->extrelocs
;
1297 /*******************************************************************************
1298 *******************************************************************************/
1300 kxld_object_get_symtab(const KXLDObject
*object
)
1304 return object
->symtab
;
1307 #if KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON
1308 /*******************************************************************************
1309 *******************************************************************************/
1310 static kern_return_t
1311 add_section(KXLDObject
*object
, KXLDSect
**sect
)
1313 kern_return_t rval
= KERN_FAILURE
;
1314 u_int nsects
= object
->sects
.nitems
;
1316 rval
= kxld_array_resize(&object
->sects
, nsects
+ 1);
1317 require_noerr(rval
, finish
);
1319 *sect
= kxld_array_get_item(&object
->sects
, nsects
);
1321 rval
= KERN_SUCCESS
;
1326 #endif /* KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON */
1328 #if KXLD_USER_OR_COMMON
1329 /*******************************************************************************
1330 * If there are common symbols, calculate how much space they'll need
1331 * and create/grow the __DATA __common section to accommodate them.
1332 * Then, resolve them against that section.
1333 *******************************************************************************/
1334 static kern_return_t
1335 resolve_common_symbols(KXLDObject
*object
)
1337 kern_return_t rval
= KERN_FAILURE
;
1338 KXLDSymtabIterator iter
;
1339 KXLDSym
*sym
= NULL
;
1340 KXLDSect
*sect
= NULL
;
1341 kxld_addr_t base_addr
= 0;
1342 kxld_size_t size
= 0;
1343 kxld_size_t total_size
= 0;
1345 u_int max_align
= 0;
1348 if (!kxld_object_target_supports_common_symbols(object
)) {
1349 rval
= KERN_SUCCESS
;
1353 /* Iterate over the common symbols to calculate their total aligned size */
1354 kxld_symtab_iterator_init(&iter
, object
->symtab
, kxld_sym_is_common
, FALSE
);
1355 while ((sym
= kxld_symtab_iterator_get_next(&iter
))) {
1356 align
= kxld_sym_get_common_align(sym
);
1357 size
= kxld_sym_get_common_size(sym
);
1359 if (align
> max_align
) max_align
= align
;
1361 total_size
= kxld_align_address(total_size
, align
) + size
;
1364 /* If there are common symbols, grow or create the __DATA __common section
1368 sect
= kxld_object_get_sect_by_name(object
, SEG_DATA
, SECT_COMMON
);
1370 base_addr
= sect
->base_addr
+ sect
->size
;
1372 kxld_sect_grow(sect
, total_size
, max_align
);
1376 rval
= add_section(object
, §
);
1377 require_noerr(rval
, finish
);
1379 kxld_sect_init_zerofill(sect
, SEG_DATA
, SECT_COMMON
,
1380 total_size
, max_align
);
1383 /* Resolve the common symbols against the new section */
1384 rval
= kxld_array_get_index(&object
->sects
, sect
, §num
);
1385 require_noerr(rval
, finish
);
1387 kxld_symtab_iterator_reset(&iter
);
1388 while ((sym
= kxld_symtab_iterator_get_next(&iter
))) {
1389 align
= kxld_sym_get_common_align(sym
);
1390 size
= kxld_sym_get_common_size(sym
);
1392 base_addr
= kxld_align_address(base_addr
, align
);
1393 kxld_sym_resolve_common(sym
, sectnum
, base_addr
);
1399 rval
= KERN_SUCCESS
;
1404 #endif /* KXLD_USER_OR_COMMON */
1406 #if KXLD_USER_OR_GOT
1407 /*******************************************************************************
1408 *******************************************************************************/
1410 target_has_got(const KXLDObject
*object
)
1415 /*******************************************************************************
1416 * Create and initialize the Global Offset Table
1417 *******************************************************************************/
1418 static kern_return_t
1419 create_got(KXLDObject
*object
)
1421 kern_return_t rval
= KERN_FAILURE
;
1422 KXLDSect
*sect
= NULL
;
1426 if (!target_has_got(object
)) {
1427 rval
= KERN_SUCCESS
;
1431 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1432 sect
= kxld_array_get_item(&object
->sects
, i
);
1433 ngots
+= kxld_sect_get_ngots(sect
, &object
->relocator
,
1437 rval
= add_section(object
, §
);
1438 require_noerr(rval
, finish
);
1440 rval
= kxld_sect_init_got(sect
, ngots
);
1441 require_noerr(rval
, finish
);
1443 object
->got_is_created
= TRUE
;
1444 rval
= KERN_SUCCESS
;
1450 /*******************************************************************************
1451 *******************************************************************************/
1452 static kern_return_t
1453 populate_got(KXLDObject
*object
)
1455 kern_return_t rval
= KERN_FAILURE
;
1456 KXLDSect
*sect
= NULL
;
1459 if (!target_has_got(object
) || !object
->got_is_created
) {
1460 rval
= KERN_SUCCESS
;
1464 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1465 sect
= kxld_array_get_item(&object
->sects
, i
);
1466 if (streq_safe(sect
->segname
, KXLD_SEG_GOT
, sizeof(KXLD_SEG_GOT
)) &&
1467 streq_safe(sect
->sectname
, KXLD_SECT_GOT
, sizeof(KXLD_SECT_GOT
)))
1469 kxld_sect_populate_got(sect
, object
->symtab
,
1470 kxld_object_target_needs_swap(object
));
1475 require_action(i
< object
->sects
.nitems
, finish
, rval
=KXLD_MISSING_GOT
);
1477 rval
= KERN_SUCCESS
;
1482 #endif /* KXLD_USER_OR_GOT */
1484 /*******************************************************************************
1485 *******************************************************************************/
1487 target_supports_protected_segments(const KXLDObject
*object
)
1489 return (object
->is_final_image
&&
1490 (object
->cputype
== CPU_TYPE_X86_64
||
1491 object
->cputype
== CPU_TYPE_ARM
||
1492 object
->cputype
== CPU_TYPE_ARM64
));
1495 /*******************************************************************************
1496 *******************************************************************************/
1498 set_is_object_linked(KXLDObject
*object
)
1502 if (kxld_object_is_kernel(object
)) {
1503 object
->is_linked
= TRUE
;
1507 if (object
->is_final_image
) {
1508 object
->is_linked
= !object
->extrelocs
.nitems
;
1512 object
->is_linked
= TRUE
;
1513 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1514 KXLDSect
*sect
= kxld_array_get_item(&object
->sects
, i
);
1515 if (sect
->relocs
.nitems
) {
1516 object
->is_linked
= FALSE
;
1523 /*******************************************************************************
1524 *******************************************************************************/
1525 void kxld_object_clear(KXLDObject
*object __unused
)
1527 KXLDSeg
*seg
= NULL
;
1528 KXLDSect
*sect
= NULL
;
1534 if (kxld_object_is_kernel(object
)) {
1535 unswap_macho(object
->file
, object
->host_order
, object
->target_order
);
1537 #endif /* !KERNEL */
1539 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1540 seg
= kxld_array_get_item(&object
->segs
, i
);
1541 kxld_seg_clear(seg
);
1543 kxld_array_reset(&object
->segs
);
1545 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1546 sect
= kxld_array_get_item(&object
->sects
, i
);
1547 kxld_sect_clear(sect
);
1549 kxld_array_reset(&object
->sects
);
1551 kxld_array_reset(&object
->extrelocs
);
1552 kxld_array_reset(&object
->locrelocs
);
1553 kxld_relocator_clear(&object
->relocator
);
1554 kxld_uuid_clear(&object
->uuid
);
1555 kxld_versionmin_clear(&object
->versionmin
);
1556 kxld_srcversion_clear(&object
->srcversion
);
1558 if (object
->symtab
) kxld_symtab_clear(object
->symtab
);
1560 object
->file
= NULL
;
1562 object
->filetype
= 0;
1563 object
->cputype
= 0;
1564 object
->cpusubtype
= 0;
1565 object
->is_kernel
= FALSE
;
1566 object
->is_final_image
= FALSE
;
1567 object
->is_linked
= FALSE
;
1568 object
->got_is_created
= FALSE
;
1570 #if KXLD_USER_OR_OBJECT
1571 object
->section_order
= NULL
;
1574 object
->host_order
= 0;
1575 object
->target_order
= 0;
1579 /*******************************************************************************
1580 *******************************************************************************/
1581 void kxld_object_deinit(KXLDObject
*object __unused
)
1583 KXLDSeg
*seg
= NULL
;
1584 KXLDSect
*sect
= NULL
;
1590 if (object
->file
&& kxld_object_is_kernel(object
)) {
1591 unswap_macho(object
->file
, object
->host_order
, object
->target_order
);
1593 #endif /* !KERNEL */
1595 for (i
= 0; i
< object
->segs
.maxitems
; ++i
) {
1596 seg
= kxld_array_get_slot(&object
->segs
, i
);
1597 kxld_seg_deinit(seg
);
1599 kxld_array_deinit(&object
->segs
);
1601 for (i
= 0; i
< object
->sects
.maxitems
; ++i
) {
1602 sect
= kxld_array_get_slot(&object
->sects
, i
);
1603 kxld_sect_deinit(sect
);
1605 kxld_array_deinit(&object
->sects
);
1607 kxld_array_deinit(&object
->extrelocs
);
1608 kxld_array_deinit(&object
->locrelocs
);
1610 if (object
->symtab
) {
1611 kxld_symtab_deinit(object
->symtab
);
1612 kxld_free(object
->symtab
, kxld_symtab_sizeof());
1615 bzero(object
, sizeof(*object
));
1618 /*******************************************************************************
1619 *******************************************************************************/
1621 kxld_object_get_file(const KXLDObject
*object
)
1625 return object
->file
;
1628 /*******************************************************************************
1629 *******************************************************************************/
1631 kxld_object_get_name(const KXLDObject
*object
)
1635 return object
->name
;
1638 /*******************************************************************************
1639 *******************************************************************************/
1641 kxld_object_is_32_bit(const KXLDObject
*object
)
1645 return kxld_is_32_bit(object
->cputype
);
1648 /*******************************************************************************
1649 *******************************************************************************/
1651 kxld_object_is_final_image(const KXLDObject
*object
)
1655 return object
->is_final_image
;
1658 /*******************************************************************************
1659 *******************************************************************************/
1661 kxld_object_is_kernel(const KXLDObject
*object
)
1665 return object
->is_kernel
;
1668 /*******************************************************************************
1669 *******************************************************************************/
1671 kxld_object_is_linked(const KXLDObject
*object
)
1675 return object
->is_linked
;
1678 /*******************************************************************************
1679 *******************************************************************************/
1681 kxld_object_target_supports_strict_patching(const KXLDObject
*object
)
1685 return (object
->cputype
!= CPU_TYPE_I386
);
1688 /*******************************************************************************
1689 *******************************************************************************/
1691 kxld_object_target_supports_common_symbols(const KXLDObject
*object
)
1695 return (object
->cputype
== CPU_TYPE_I386
);
1698 /*******************************************************************************
1699 *******************************************************************************/
1701 kxld_object_get_vmsize(const KXLDObject
*object
, u_long
*header_size
,
1710 /* vmsize is the padded header page(s) + segment vmsizes */
1712 *header_size
= (object
->is_final_image
) ?
1713 0 : round_page(get_macho_header_size(object
));
1714 *vmsize
= *header_size
+ get_macho_data_size(object
);
1718 /*******************************************************************************
1719 *******************************************************************************/
1721 kxld_object_set_linked_object_size(KXLDObject
*object
, u_long vmsize
)
1723 object
->output_buffer_size
= vmsize
; /* cache this for use later */
1727 /*******************************************************************************
1728 *******************************************************************************/
1730 kxld_object_export_linked_object(const KXLDObject
*object
,
1731 u_char
*linked_object
)
1733 kern_return_t rval
= KERN_FAILURE
;
1734 KXLDSeg
*seg
= NULL
;
1736 u_long header_size
= 0;
1737 u_long header_offset
= 0;
1738 u_long data_offset
= 0;
1741 boolean_t is_32bit_object
= kxld_object_is_32_bit(object
);
1744 check(linked_object
);
1746 /* Calculate the size of the headers and data */
1748 header_size
= get_macho_header_size(object
);
1749 data_offset
= (object
->is_final_image
) ? header_size
: round_page(header_size
);
1750 size
= object
->output_buffer_size
;
1752 /* Copy data to the file */
1754 ncmds
= object
->segs
.nitems
+ 1 /* LC_SYMTAB */;
1757 /* don't write out a DYSYMTAB segment for targets that can't digest it
1759 if (target_supports_slideable_kexts(object
)) {
1760 ncmds
++; /* dysymtab */
1762 #endif /* KXLD_PIC_KEXTS */
1764 if (object
->uuid
.has_uuid
== TRUE
) {
1768 if (object
->versionmin
.has_versionmin
== TRUE
) {
1772 if (object
->srcversion
.has_srcversion
== TRUE
) {
1776 rval
= export_macho_header(object
, linked_object
, ncmds
, &header_offset
, header_size
);
1777 require_noerr(rval
, finish
);
1779 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1780 seg
= kxld_array_get_item(&object
->segs
, i
);
1782 rval
= kxld_seg_export_macho_to_vm(seg
, linked_object
, &header_offset
,
1783 header_size
, size
, object
->link_addr
, is_32bit_object
);
1784 require_noerr(rval
, finish
);
1787 seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
1788 data_offset
= (u_long
) (seg
->link_addr
- object
->link_addr
);
1790 rval
= kxld_symtab_export_macho(object
->symtab
, linked_object
, &header_offset
,
1791 header_size
, &data_offset
, size
, is_32bit_object
);
1792 require_noerr(rval
, finish
);
1795 if (target_supports_slideable_kexts(object
)) {
1796 rval
= kxld_reloc_export_macho(&object
->relocator
, &object
->locrelocs
,
1797 &object
->extrelocs
, linked_object
, &header_offset
, header_size
,
1798 &data_offset
, size
);
1799 require_noerr(rval
, finish
);
1801 #endif /* KXLD_PIC_KEXTS */
1803 if (object
->uuid
.has_uuid
) {
1804 rval
= kxld_uuid_export_macho(&object
->uuid
, linked_object
, &header_offset
, header_size
);
1805 require_noerr(rval
, finish
);
1808 if (object
->versionmin
.has_versionmin
) {
1809 rval
= kxld_versionmin_export_macho(&object
->versionmin
, linked_object
, &header_offset
, header_size
);
1810 require_noerr(rval
, finish
);
1813 if (object
->srcversion
.has_srcversion
) {
1814 rval
= kxld_srcversion_export_macho(&object
->srcversion
, linked_object
, &header_offset
, header_size
);
1815 require_noerr(rval
, finish
);
1819 unswap_macho(linked_object
, object
->host_order
, object
->target_order
);
1822 rval
= KERN_SUCCESS
;
1828 /*******************************************************************************
1829 *******************************************************************************/
1830 static kern_return_t
1831 export_macho_header(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1832 u_long
*header_offset
, u_long header_size
)
1834 kern_return_t rval
= KERN_FAILURE
;
1838 check(header_offset
);
1840 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
1841 export_macho_header_32
, export_macho_header_64
,
1842 object
, buf
, ncmds
, header_offset
, header_size
);
1843 require_noerr(rval
, finish
);
1845 rval
= KERN_SUCCESS
;
1851 #if KXLD_USER_OR_ILP32
1852 /*******************************************************************************
1853 *******************************************************************************/
1854 static kern_return_t
1855 export_macho_header_32(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1856 u_long
*header_offset
, u_long header_size
)
1858 kern_return_t rval
= KERN_FAILURE
;
1859 struct mach_header
*mach
= NULL
;
1863 check(header_offset
);
1865 require_action(sizeof(*mach
) <= header_size
- *header_offset
, finish
,
1867 mach
= (struct mach_header
*) ((void *) (buf
+ *header_offset
));
1869 mach
->magic
= MH_MAGIC
;
1870 mach
->cputype
= object
->cputype
;
1871 mach
->cpusubtype
= object
->cpusubtype
;
1872 mach
->filetype
= object
->filetype
;
1873 mach
->ncmds
= ncmds
;
1874 mach
->sizeofcmds
= (uint32_t) (header_size
- sizeof(*mach
));
1875 mach
->flags
= MH_NOUNDEFS
;
1877 *header_offset
+= sizeof(*mach
);
1879 rval
= KERN_SUCCESS
;
1884 #endif /* KXLD_USER_OR_ILP32 */
1886 #if KXLD_USER_OR_LP64
1887 /*******************************************************************************
1888 *******************************************************************************/
1889 static kern_return_t
1890 export_macho_header_64(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1891 u_long
*header_offset
, u_long header_size
)
1893 kern_return_t rval
= KERN_FAILURE
;
1894 struct mach_header_64
*mach
= NULL
;
1898 check(header_offset
);
1900 require_action(sizeof(*mach
) <= header_size
- *header_offset
, finish
,
1902 mach
= (struct mach_header_64
*) ((void *) (buf
+ *header_offset
));
1904 mach
->magic
= MH_MAGIC_64
;
1905 mach
->cputype
= object
->cputype
;
1906 mach
->cpusubtype
= object
->cpusubtype
;
1907 mach
->filetype
= object
->filetype
;
1908 mach
->ncmds
= ncmds
;
1909 mach
->sizeofcmds
= (uint32_t) (header_size
- sizeof(*mach
));
1910 mach
->flags
= MH_NOUNDEFS
;
1912 *header_offset
+= sizeof(*mach
);
1914 rval
= KERN_SUCCESS
;
1919 #endif /* KXLD_USER_OR_LP64 */
1921 /*******************************************************************************
1922 *******************************************************************************/
1924 kxld_object_index_symbols_by_name(KXLDObject
*object
)
1926 return kxld_symtab_index_symbols_by_name(object
->symtab
);
1929 /*******************************************************************************
1930 *******************************************************************************/
1932 kxld_object_index_cxx_symbols_by_value(KXLDObject
*object
)
1934 return kxld_symtab_index_cxx_symbols_by_value(object
->symtab
);
1937 /*******************************************************************************
1938 *******************************************************************************/
1940 kxld_object_relocate(KXLDObject
*object
, kxld_addr_t link_address
)
1942 kern_return_t rval
= KERN_FAILURE
;
1943 KXLDSeg
*seg
= NULL
;
1948 object
->link_addr
= link_address
;
1950 /* Relocate segments (which relocates the sections) */
1951 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1952 seg
= kxld_array_get_item(&object
->segs
, i
);
1953 kxld_seg_relocate(seg
, link_address
);
1956 /* Relocate symbols */
1957 rval
= kxld_symtab_relocate(object
->symtab
, &object
->sects
);
1958 require_noerr(rval
, finish
);
1960 rval
= KERN_SUCCESS
;
1965 /*******************************************************************************
1966 *******************************************************************************/
1968 get_mutable_sym(const KXLDObject
*object
, const KXLDSym
*sym
)
1970 KXLDSym
*rval
= NULL
;
1971 kern_return_t result
= KERN_FAILURE
;
1974 result
= kxld_symtab_get_sym_index(object
->symtab
, sym
, &i
);
1975 require_noerr(result
, finish
);
1977 rval
= kxld_symtab_get_symbol_by_index(object
->symtab
, i
);
1978 require_action(rval
== sym
, finish
, rval
=NULL
);
1984 /*******************************************************************************
1985 *******************************************************************************/
1987 kxld_object_resolve_symbol(KXLDObject
*object
,
1988 const KXLDSym
*sym
, kxld_addr_t addr
)
1990 kern_return_t rval
= KERN_FAILURE
;
1991 KXLDSym
*resolved_sym
= NULL
;
1993 resolved_sym
= get_mutable_sym(object
, sym
);
1994 require_action(resolved_sym
, finish
, rval
=KERN_FAILURE
);
1996 rval
= kxld_sym_resolve(resolved_sym
, addr
);
1997 require_noerr(rval
, finish
);
1999 rval
= KERN_SUCCESS
;
2004 /*******************************************************************************
2005 *******************************************************************************/
2007 kxld_object_patch_symbol(KXLDObject
*object
, const struct kxld_sym
*sym
)
2009 kern_return_t rval
= KERN_FAILURE
;
2010 KXLDSym
*patched_sym
= NULL
;
2012 patched_sym
= get_mutable_sym(object
, sym
);
2013 require_action(patched_sym
, finish
, rval
=KERN_FAILURE
);
2015 (void) kxld_sym_patch(patched_sym
);
2016 rval
= KERN_SUCCESS
;
2021 /*******************************************************************************
2022 *******************************************************************************/
2024 kxld_object_add_symbol(KXLDObject
*object
, char *name
, kxld_addr_t link_addr
,
2025 const KXLDSym
**sym_out
)
2027 kern_return_t rval
= KERN_FAILURE
;
2028 KXLDSym
*sym
= NULL
;
2030 rval
= kxld_symtab_add_symbol(object
->symtab
, name
, link_addr
, &sym
);
2031 require_noerr(rval
, finish
);
2034 rval
= KERN_SUCCESS
;
2039 /*******************************************************************************
2040 *******************************************************************************/
2042 kxld_object_process_relocations(KXLDObject
*object
,
2043 const KXLDDict
*patched_vtables
)
2045 kern_return_t rval
= KERN_FAILURE
;
2047 (void) kxld_relocator_set_vtables(&object
->relocator
, patched_vtables
);
2049 /* Process relocation entries and populate the global offset table.
2051 * For final linked images: the relocation entries are contained in a couple
2052 * of tables hanging off the end of the symbol table. The GOT has its own
2053 * section created by the linker; we simply need to fill it.
2055 * For object files: the relocation entries are bound to each section.
2056 * The GOT, if it exists for the target architecture, is created by kxld,
2057 * and we must populate it according to our internal structures.
2059 if (object
->is_final_image
) {
2060 #if KXLD_USER_OR_BUNDLE
2061 rval
= process_symbol_pointers(object
);
2062 require_noerr(rval
, finish
);
2064 rval
= process_relocs_from_tables(object
);
2065 require_noerr(rval
, finish
);
2067 require_action(FALSE
, finish
, rval
=KERN_FAILURE
);
2068 #endif /* KXLD_USER_OR_BUNDLE */
2070 #if KXLD_USER_OR_GOT
2072 rval
= populate_got(object
);
2073 require_noerr(rval
, finish
);
2074 #endif /* KXLD_USER_OR_GOT */
2075 #if KXLD_USER_OR_OBJECT
2076 rval
= process_relocs_from_sections(object
);
2077 require_noerr(rval
, finish
);
2079 require_action(FALSE
, finish
, rval
=KERN_FAILURE
);
2080 #endif /* KXLD_USER_OR_OBJECT */
2083 /* Populate kmod info structure */
2084 rval
= populate_kmod_info(object
);
2085 require_noerr(rval
, finish
);
2087 rval
= KERN_SUCCESS
;
2092 #if KXLD_USER_OR_BUNDLE
2094 #define SECT_SYM_PTRS "__nl_symbol_ptr"
2096 /*******************************************************************************
2097 * Final linked images create an __nl_symbol_ptr section for the global offset
2098 * table and for symbol pointer lookups in general. Rather than use relocation
2099 * entries, the linker creates an "indirect symbol table" which stores indexes
2100 * into the symbol table corresponding to the entries of this section. This
2101 * function populates the section with the relocated addresses of those symbols.
2102 *******************************************************************************/
2103 static kern_return_t
2104 process_symbol_pointers(KXLDObject
*object
)
2106 kern_return_t rval
= KERN_FAILURE
;
2107 KXLDSect
*sect
= NULL
;
2108 KXLDSym
*sym
= NULL
;
2109 int32_t *symidx
= NULL
;
2110 u_char
*symptr
= NULL
;
2111 u_long symptrsize
= 0;
2118 require_action(object
->is_final_image
&& object
->dysymtab_hdr
,
2119 finish
, rval
=KERN_FAILURE
);
2121 /* Get the __DATA,__nl_symbol_ptr section. If it doesn't exist, we have
2125 sect
= kxld_object_get_sect_by_name(object
, SEG_DATA
, SECT_SYM_PTRS
);
2126 if (!sect
|| !(sect
->flags
& S_NON_LAZY_SYMBOL_POINTERS
)) {
2127 rval
= KERN_SUCCESS
;
2131 /* Calculate the table offset and number of entries in the section */
2133 if (kxld_object_is_32_bit(object
)) {
2134 symptrsize
= sizeof(uint32_t);
2136 symptrsize
= sizeof(uint64_t);
2139 nsyms
= (u_int
) (sect
->size
/ symptrsize
);
2140 firstsym
= sect
->reserved1
;
2142 require_action(firstsym
+ nsyms
<= object
->dysymtab_hdr
->nindirectsyms
,
2143 finish
, rval
=KERN_FAILURE
;
2144 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
2145 "firstsym + nsyms > object->dysymtab_hdr->nindirectsyms"));
2147 /* Iterate through the indirect symbol table and fill in the section of
2148 * symbol pointers. There are three cases:
2149 * 1) A normal symbol - put its value directly in the table
2150 * 2) An INDIRECT_SYMBOL_LOCAL - symbols that are local and already have
2151 * their offset from the start of the file in the section. Simply
2152 * add the file's link address to fill this entry.
2153 * 3) An INDIRECT_SYMBOL_ABS - prepopulated absolute symbols. No
2154 * action is required.
2157 symidx
= (int32_t *) ((void *) (object
->file
+ object
->dysymtab_hdr
->indirectsymoff
));
2159 symptr
= sect
->data
;
2160 for (i
= 0; i
< nsyms
; ++i
, ++symidx
, symptr
+=symptrsize
) {
2161 if (*symidx
& INDIRECT_SYMBOL_LOCAL
) {
2162 if (*symidx
& INDIRECT_SYMBOL_ABS
) continue;
2164 add_to_ptr(symptr
, object
->link_addr
, kxld_object_is_32_bit(object
));
2166 sym
= kxld_symtab_get_symbol_by_index(object
->symtab
, *symidx
);
2167 require_action(sym
, finish
, rval
=KERN_FAILURE
);
2169 add_to_ptr(symptr
, sym
->link_addr
, kxld_object_is_32_bit(object
));
2173 rval
= KERN_SUCCESS
;
2178 /*******************************************************************************
2179 *******************************************************************************/
2181 get_seg_by_base_addr(KXLDObject
*object
, kxld_addr_t base_addr
)
2183 KXLDSeg
*seg
= NULL
;
2184 kxld_addr_t start
= 0;
2185 kxld_addr_t end
= 0;
2188 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
2189 seg
= kxld_array_get_item(&object
->segs
, i
);
2190 start
= seg
->base_addr
;
2191 end
= seg
->base_addr
+ seg
->vmsize
;
2193 if (start
<= base_addr
&& base_addr
< end
) return seg
;
2199 /*******************************************************************************
2200 *******************************************************************************/
2201 static kern_return_t
2202 process_relocs_from_tables(KXLDObject
*object
)
2204 kern_return_t rval
= KERN_FAILURE
;
2205 KXLDReloc
*reloc
= NULL
;
2206 KXLDSeg
*seg
= NULL
;
2209 /* Process external relocations */
2210 for (i
= 0; i
< object
->extrelocs
.nitems
; ++i
) {
2211 reloc
= kxld_array_get_item(&object
->extrelocs
, i
);
2213 seg
= get_seg_by_base_addr(object
, reloc
->address
);
2214 require_action(seg
, finish
, rval
=KERN_FAILURE
);
2216 rval
= kxld_relocator_process_table_reloc(&object
->relocator
, reloc
,
2217 seg
, object
->link_addr
);
2218 require_noerr(rval
, finish
);
2221 /* Process local relocations */
2222 for (i
= 0; i
< object
->locrelocs
.nitems
; ++i
) {
2223 reloc
= kxld_array_get_item(&object
->locrelocs
, i
);
2225 seg
= get_seg_by_base_addr(object
, reloc
->address
);
2226 require_action(seg
, finish
, rval
=KERN_FAILURE
);
2228 rval
= kxld_relocator_process_table_reloc(&object
->relocator
, reloc
,
2229 seg
, object
->link_addr
);
2230 require_noerr(rval
, finish
);
2233 rval
= KERN_SUCCESS
;
2238 /*******************************************************************************
2239 *******************************************************************************/
2241 add_to_ptr(u_char
*symptr
, kxld_addr_t val
, boolean_t is_32_bit
)
2244 uint32_t *ptr
= (uint32_t *) ((void *) symptr
);
2245 *ptr
+= (uint32_t) val
;
2247 uint64_t *ptr
= (uint64_t *) ((void *) symptr
);
2248 *ptr
+= (uint64_t) val
;
2251 #endif /* KXLD_USER_OR_BUNDLE */
2253 #if KXLD_USER_OR_OBJECT
2254 /*******************************************************************************
2255 *******************************************************************************/
2256 static kern_return_t
2257 process_relocs_from_sections(KXLDObject
*object
)
2259 kern_return_t rval
= KERN_FAILURE
;
2260 KXLDSect
*sect
= NULL
;
2263 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
2264 sect
= kxld_array_get_item(&object
->sects
, i
);
2265 rval
= kxld_sect_process_relocs(sect
, &object
->relocator
);
2266 require_noerr(rval
, finish
);
2269 rval
= KERN_SUCCESS
;
2273 #endif /* KXLD_USER_OR_OBJECT */
2275 /*******************************************************************************
2276 *******************************************************************************/
2277 static kern_return_t
2278 populate_kmod_info(KXLDObject
*object
)
2280 kern_return_t rval
= KERN_FAILURE
;
2281 KXLDSect
*kmodsect
= NULL
;
2282 KXLDSym
*kmodsym
= NULL
;
2283 kmod_info_t
*kmod_info
= NULL
;
2284 u_long kmod_offset
= 0;
2288 if (kxld_object_is_kernel(object
)) {
2289 rval
= KERN_SUCCESS
;
2293 kxld_object_get_vmsize(object
, &header_size
, &size
);
2295 kmodsym
= kxld_symtab_get_locally_defined_symbol_by_name(object
->symtab
,
2296 KXLD_KMOD_INFO_SYMBOL
);
2297 require_action(kmodsym
, finish
, rval
=KERN_FAILURE
;
2298 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogNoKmodInfo
));
2300 kmodsect
= kxld_array_get_item(&object
->sects
, kmodsym
->sectnum
);
2301 kmod_offset
= (u_long
) (kmodsym
->base_addr
- kmodsect
->base_addr
);
2302 kmod_info
= (kmod_info_t
*) ((void *) (kmodsect
->data
+ kmod_offset
));
2304 if (kxld_object_is_32_bit(object
)) {
2305 kmod_info_32_v1_t
*kmod
= (kmod_info_32_v1_t
*) (kmod_info
);
2306 kmod
->address
= (uint32_t) object
->link_addr
;
2307 kmod
->size
= (uint32_t) size
;
2308 kmod
->hdr_size
= (uint32_t) header_size
;
2311 if (kxld_object_target_needs_swap(object
)) {
2312 kmod
->address
= OSSwapInt32(kmod
->address
);
2313 kmod
->size
= OSSwapInt32(kmod
->size
);
2314 kmod
->hdr_size
= OSSwapInt32(kmod
->hdr_size
);
2316 #endif /* !KERNEL */
2318 kmod_info_64_v1_t
*kmod
= (kmod_info_64_v1_t
*) (kmod_info
);
2319 kmod
->address
= object
->link_addr
;
2321 kmod
->hdr_size
= header_size
;
2324 if (kxld_object_target_needs_swap(object
)) {
2325 kmod
->address
= OSSwapInt64(kmod
->address
);
2326 kmod
->size
= OSSwapInt64(kmod
->size
);
2327 kmod
->hdr_size
= OSSwapInt64(kmod
->hdr_size
);
2329 #endif /* !KERNEL */
2333 rval
= KERN_SUCCESS
;
2340 /*******************************************************************************
2341 *******************************************************************************/
2343 target_supports_slideable_kexts(const KXLDObject
*object
)
2347 return (object
->cputype
!= CPU_TYPE_I386
&& object
->include_kaslr_relocs
);
2349 #endif /* KXLD_PIC_KEXTS */