2 * Copyright (c) 2009, 2011-2012 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(__i386__)
331 object
->cputype
= CPU_TYPE_I386
;
332 object
->cpusubtype
= CPU_SUBTYPE_I386_ALL
;
334 #elif defined(__x86_64__)
335 object
->cputype
= CPU_TYPE_X86_64
;
336 object
->cpusubtype
= CPU_SUBTYPE_X86_64_ALL
;
339 kxld_log(kKxldLogLinking
, kKxldLogErr
,
340 kKxldLogArchNotSupported
, _mh_execute_header
->cputype
);
341 return KERN_NOT_SUPPORTED
;
342 #endif /* Supported architecture defines */
347 /* User-space must look up the architecture it's running on and the target
348 * architecture at run-time.
351 kern_return_t rval
= KERN_FAILURE
;
352 const NXArchInfo
*host_arch
= NULL
;
356 host_arch
= NXGetLocalArchInfo();
357 require_action(host_arch
, finish
, rval
=KERN_FAILURE
);
359 object
->host_order
= host_arch
->byteorder
;
361 /* If the user did not specify a cputype, use the local architecture.
365 object
->cputype
= cputype
;
366 object
->cpusubtype
= cpusubtype
;
368 object
->cputype
= host_arch
->cputype
;
369 object
->target_order
= object
->host_order
;
371 switch (object
->cputype
) {
373 object
->cpusubtype
= CPU_SUBTYPE_I386_ALL
;
375 case CPU_TYPE_X86_64
:
376 object
->cpusubtype
= CPU_SUBTYPE_X86_64_ALL
;
379 object
->cpusubtype
= CPU_SUBTYPE_ARM_ALL
;
382 object
->cpusubtype
= 0;
386 /* Validate that we support the target architecture and record its
390 switch(object
->cputype
) {
393 case CPU_TYPE_X86_64
:
394 object
->target_order
= NX_LittleEndian
;
397 rval
= KERN_NOT_SUPPORTED
;
398 kxld_log(kKxldLogLinking
, kKxldLogErr
,
399 kKxldLogArchNotSupported
, object
->cputype
);
410 /*******************************************************************************
411 *******************************************************************************/
413 get_macho_slice_for_arch(KXLDObject
*object
, u_char
*file
, u_long size
)
415 kern_return_t rval
= KERN_FAILURE
;
416 struct mach_header
*mach_hdr
= NULL
;
418 struct fat_header
*fat
= (struct fat_header
*) ((void *) file
);
419 struct fat_arch
*archs
= (struct fat_arch
*) &fat
[1];
420 boolean_t swap
= FALSE
;
430 /* We are assuming that we will never receive a fat file in the kernel */
433 require_action(size
>= sizeof(*fat
), finish
,
435 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
437 /* The fat header is always big endian, so swap if necessary */
438 if (fat
->magic
== FAT_CIGAM
) {
439 (void) swap_fat_header(fat
, object
->host_order
);
443 if (fat
->magic
== FAT_MAGIC
) {
444 struct fat_arch
*arch
= NULL
;
446 require_action(size
>= (sizeof(*fat
) + (fat
->nfat_arch
* sizeof(*archs
))),
449 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
451 /* Swap the fat_arch structures if necessary */
453 (void) swap_fat_arch(archs
, fat
->nfat_arch
, object
->host_order
);
456 /* Locate the Mach-O for the requested architecture */
458 arch
= NXFindBestFatArch(object
->cputype
, object
->cpusubtype
, archs
,
460 require_action(arch
, finish
, rval
=KERN_FAILURE
;
461 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogArchNotFound
));
462 require_action(size
>= arch
->offset
+ arch
->size
, finish
,
464 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
466 object
->file
= file
+ arch
->offset
;
467 object
->size
= arch
->size
;
471 /* Swap the Mach-O's headers to this architecture if necessary */
472 if (kxld_object_is_32_bit(object
)) {
473 rval
= validate_and_swap_macho_32(object
->file
, object
->size
479 rval
= validate_and_swap_macho_64(object
->file
, object
->size
485 require_noerr(rval
, finish
);
487 mach_hdr
= (struct mach_header
*) ((void *) object
->file
);
488 require_action(object
->cputype
== mach_hdr
->cputype
, finish
,
490 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
497 /*******************************************************************************
498 *******************************************************************************/
500 init_from_final_linked_image(KXLDObject
*object
, u_int
*filetype_out
,
501 struct symtab_command
**symtab_hdr_out
)
503 kern_return_t rval
= KERN_FAILURE
;
505 KXLDSect
*sect
= NULL
;
506 struct load_command
*cmd_hdr
= NULL
;
507 struct symtab_command
*symtab_hdr
= NULL
;
508 struct uuid_command
*uuid_hdr
= NULL
;
509 struct version_min_command
*versionmin_hdr
= NULL
;
510 struct source_version_command
*source_version_hdr
= NULL
;
511 u_long base_offset
= 0;
513 u_long sect_offset
= 0;
523 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), base_offset
,
524 get_macho_cmd_data_32
, get_macho_cmd_data_64
,
525 object
->file
, offset
, &filetype
, &ncmds
);
527 /* First pass to count segments and sections */
529 offset
= base_offset
;
530 for (i
= 0; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
531 cmd_hdr
= (struct load_command
*) ((void *) (object
->file
+ offset
));
533 switch(cmd_hdr
->cmd
) {
534 #if KXLD_USER_OR_ILP32
537 struct segment_command
*seg_hdr
=
538 (struct segment_command
*) cmd_hdr
;
540 /* Ignore segments with no vm size */
541 if (!seg_hdr
->vmsize
) continue;
544 nsects
+= seg_hdr
->nsects
;
547 #endif /* KXLD_USER_OR_ILP32 */
548 #if KXLD_USER_OR_LP64
551 struct segment_command_64
*seg_hdr
=
552 (struct segment_command_64
*) ((void *) cmd_hdr
);
554 /* Ignore segments with no vm size */
555 if (!seg_hdr
->vmsize
) continue;
558 nsects
+= seg_hdr
->nsects
;
561 #endif /* KXLD_USER_OR_LP64 */
567 /* Allocate the segments and sections */
570 rval
= kxld_array_init(&object
->segs
, sizeof(KXLDSeg
), nsegs
);
571 require_noerr(rval
, finish
);
573 rval
= kxld_array_init(&object
->sects
, sizeof(KXLDSect
), nsects
);
574 require_noerr(rval
, finish
);
577 /* Initialize the segments and sections */
579 offset
= base_offset
;
580 for (i
= 0; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
581 cmd_hdr
= (struct load_command
*) ((void *) (object
->file
+ offset
));
584 switch(cmd_hdr
->cmd
) {
585 #if KXLD_USER_OR_ILP32
588 struct segment_command
*seg_hdr
=
589 (struct segment_command
*) cmd_hdr
;
591 /* Ignore segments with no vm size */
592 if (!seg_hdr
->vmsize
) continue;
594 seg
= kxld_array_get_item(&object
->segs
, segi
++);
596 rval
= kxld_seg_init_from_macho_32(seg
, seg_hdr
);
597 require_noerr(rval
, finish
);
599 sect_offset
= offset
+ sizeof(*seg_hdr
);
602 #endif /* KXLD_USER_OR_ILP32 */
603 #if KXLD_USER_OR_LP64
606 struct segment_command_64
*seg_hdr
=
607 (struct segment_command_64
*) ((void *) cmd_hdr
);
609 /* Ignore segments with no vm size */
610 if (!seg_hdr
->vmsize
) continue;
612 seg
= kxld_array_get_item(&object
->segs
, segi
++);
614 rval
= kxld_seg_init_from_macho_64(seg
, seg_hdr
);
615 require_noerr(rval
, finish
);
617 sect_offset
= offset
+ sizeof(*seg_hdr
);
620 #endif /* KXLD_USER_OR_LP64 */
622 symtab_hdr
= (struct symtab_command
*) cmd_hdr
;
625 uuid_hdr
= (struct uuid_command
*) cmd_hdr
;
626 kxld_uuid_init_from_macho(&object
->uuid
, uuid_hdr
);
628 case LC_VERSION_MIN_MACOSX
:
629 case LC_VERSION_MIN_IPHONEOS
:
630 versionmin_hdr
= (struct version_min_command
*) cmd_hdr
;
631 kxld_versionmin_init_from_macho(&object
->versionmin
, versionmin_hdr
);
633 case LC_SOURCE_VERSION
:
634 source_version_hdr
= (struct source_version_command
*) (void *) cmd_hdr
;
635 kxld_srcversion_init_from_macho(&object
->srcversion
, source_version_hdr
);
638 object
->dysymtab_hdr
= (struct dysymtab_command
*) cmd_hdr
;
640 rval
= kxld_reloc_create_macho(&object
->extrelocs
, &object
->relocator
,
641 (struct relocation_info
*) ((void *) (object
->file
+ object
->dysymtab_hdr
->extreloff
)),
642 object
->dysymtab_hdr
->nextrel
);
643 require_noerr(rval
, finish
);
645 rval
= kxld_reloc_create_macho(&object
->locrelocs
, &object
->relocator
,
646 (struct relocation_info
*) ((void *) (object
->file
+ object
->dysymtab_hdr
->locreloff
)),
647 object
->dysymtab_hdr
->nlocrel
);
648 require_noerr(rval
, finish
);
653 /* Don't need to do anything with UNIXTHREAD or MAIN for the kernel */
654 require_action(kxld_object_is_kernel(object
),
655 finish
, rval
=KERN_FAILURE
;
656 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
657 "LC_UNIXTHREAD/LC_MAIN segment is not valid in a kext."));
659 case LC_CODE_SIGNATURE
:
661 case LC_DYLD_INFO_ONLY
:
662 case LC_FUNCTION_STARTS
:
663 case LC_DATA_IN_CODE
:
664 case LC_DYLIB_CODE_SIGN_DRS
:
665 /* Various metadata that might be stored in the linkedit segment */
669 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
670 "Invalid segment type in MH_KEXT_BUNDLE kext: %u.", cmd_hdr
->cmd
);
676 /* Initialize the sections */
677 for (j
= 0; j
< seg
->sects
.nitems
; ++j
, ++secti
) {
678 sect
= kxld_array_get_item(&object
->sects
, secti
);
679 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
680 kxld_sect_init_from_macho_32
, kxld_sect_init_from_macho_64
,
681 sect
, object
->file
, §_offset
, secti
, &object
->relocator
);
682 require_noerr(rval
, finish
);
684 /* Add the section to the segment. This will also make sure
685 * that the sections and segments have the same segname.
687 rval
= kxld_seg_add_section(seg
, sect
);
688 require_noerr(rval
, finish
);
690 rval
= kxld_seg_finish_init(seg
);
691 require_noerr(rval
, finish
);
695 if (filetype_out
) *filetype_out
= filetype
;
696 if (symtab_hdr_out
) *symtab_hdr_out
= symtab_hdr
;
697 object
->is_final_image
= TRUE
;
703 /*******************************************************************************
704 *******************************************************************************/
706 init_from_execute(KXLDObject
*object
)
708 kern_return_t rval
= KERN_FAILURE
;
709 struct symtab_command
*symtab_hdr
= NULL
;
711 KXLDSeg
* kernel_linkedit_seg
= NULL
; // used if running kernel
712 #if KXLD_USER_OR_OBJECT
714 KXLDSect
*sect
= NULL
;
715 KXLDSectionName
*sname
= NULL
;
716 u_int i
= 0, j
= 0, k
= 0;
717 #endif /* KXLD_USER_OR_OBJECT */
721 require_action(kxld_object_is_kernel(object
), finish
, rval
=KERN_FAILURE
);
723 rval
= init_from_final_linked_image(object
, &filetype
, &symtab_hdr
);
724 require_noerr(rval
, finish
);
726 require_action(filetype
== MH_EXECUTE
, finish
, rval
=KERN_FAILURE
;
727 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
728 "The kernel file is not of type MH_EXECUTE."));
730 /* Initialize the symbol table. If this is the running kernel
731 * we will work from the in-memory linkedit segment;
732 * otherwise we work from the whole mach-o image.
735 kernel_linkedit_seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
736 require_action(kernel_linkedit_seg
, finish
, rval
=KERN_FAILURE
;
737 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
));
740 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
741 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
742 object
->symtab
, symtab_hdr
, object
->file
, kernel_linkedit_seg
);
743 require_noerr(rval
, finish
);
745 #if KXLD_USER_OR_OBJECT
746 /* Save off the order of section names so that we can lay out kext
747 * sections for MH_OBJECT-based systems.
749 if (target_supports_object(object
)) {
751 rval
= kxld_array_init(object
->section_order
, sizeof(KXLDSectionName
),
752 object
->sects
.nitems
);
753 require_noerr(rval
, finish
);
755 /* Copy the section names into the section_order array for future kext
758 for (i
= 0, k
= 0; i
< object
->segs
.nitems
; ++i
) {
759 seg
= kxld_array_get_item(&object
->segs
, i
);
761 for (j
= 0; j
< seg
->sects
.nitems
; ++j
, ++k
) {
762 sect
= *(KXLDSect
**) kxld_array_get_item(&seg
->sects
, j
);
763 sname
= kxld_array_get_item(object
->section_order
, k
);
765 strlcpy(sname
->segname
, sect
->segname
, sizeof(sname
->segname
));
766 strlcpy(sname
->sectname
, sect
->sectname
, sizeof(sname
->sectname
));
770 #endif /* KXLD_USER_OR_OBJECT */
777 #if KXLD_USER_OR_BUNDLE
778 /*******************************************************************************
779 *******************************************************************************/
781 target_supports_bundle(const KXLDObject
*object __unused
)
786 /*******************************************************************************
787 *******************************************************************************/
789 init_from_bundle(KXLDObject
*object
)
791 kern_return_t rval
= KERN_FAILURE
;
792 struct symtab_command
*symtab_hdr
= NULL
;
797 require_action(target_supports_bundle(object
), finish
,
799 kxld_log(kKxldLogLinking
, kKxldLogErr
,
800 kKxldLogFiletypeNotSupported
, MH_KEXT_BUNDLE
));
802 rval
= init_from_final_linked_image(object
, &filetype
, &symtab_hdr
);
803 require_noerr(rval
, finish
);
805 require_action(filetype
== MH_KEXT_BUNDLE
, finish
,
808 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
809 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
810 object
->symtab
, symtab_hdr
, object
->file
,
811 /* kernel_linkedit_seg */ NULL
);
812 require_noerr(rval
, finish
);
818 #endif /* KXLD_USER_OR_BUNDLE */
820 #if KXLD_USER_OR_OBJECT
821 /*******************************************************************************
822 *******************************************************************************/
823 static boolean_t
target_supports_object(const KXLDObject
*object
)
825 return (object
->cputype
== CPU_TYPE_I386
);
828 /*******************************************************************************
829 *******************************************************************************/
831 init_from_object(KXLDObject
*object
)
833 kern_return_t rval
= KERN_FAILURE
;
834 struct load_command
*cmd_hdr
= NULL
;
835 struct symtab_command
*symtab_hdr
= NULL
;
836 struct uuid_command
*uuid_hdr
= NULL
;
837 KXLDSect
*sect
= NULL
;
839 u_long sect_offset
= 0;
844 boolean_t has_segment
= FALSE
;
848 require_action(target_supports_object(object
),
849 finish
, rval
=KERN_FAILURE
;
850 kxld_log(kKxldLogLinking
, kKxldLogErr
,
851 kKxldLogFiletypeNotSupported
, MH_OBJECT
));
853 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), offset
,
854 get_macho_cmd_data_32
, get_macho_cmd_data_64
,
855 object
->file
, offset
, &filetype
, &ncmds
);
857 require_action(filetype
== MH_OBJECT
, finish
, rval
=KERN_FAILURE
);
859 /* MH_OBJECTs use one unnamed segment to contain all of the sections. We
860 * loop over all of the load commands to initialize the structures we
861 * expect. Then, we'll use the unnamed segment to get to all of the
862 * sections, and then use those sections to create the actual segments.
865 for (; i
< ncmds
; ++i
, offset
+= cmd_hdr
->cmdsize
) {
866 cmd_hdr
= (struct load_command
*) ((void *) (object
->file
+ offset
));
868 switch(cmd_hdr
->cmd
) {
869 #if KXLD_USER_OR_ILP32
872 struct segment_command
*seg_hdr
=
873 (struct segment_command
*) cmd_hdr
;
875 /* Ignore segments with no vm size */
876 if (!seg_hdr
->vmsize
) continue;
878 /* Ignore LINKEDIT segments */
879 if (streq_safe(seg_hdr
->segname
, SEG_LINKEDIT
,
880 const_strlen(SEG_LINKEDIT
)))
885 require_action(kxld_object_is_32_bit(object
), finish
, rval
=KERN_FAILURE
;
886 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
887 "LC_SEGMENT in 64-bit kext."));
888 require_action(!has_segment
, finish
, rval
=KERN_FAILURE
;
889 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
890 "Multiple segments in an MH_OBJECT kext."));
892 nsects
= seg_hdr
->nsects
;
893 sect_offset
= offset
+ sizeof(*seg_hdr
);
897 #endif /* KXLD_USER_OR_ILP32 */
898 #if KXLD_USER_OR_LP64
901 struct segment_command_64
*seg_hdr
=
902 (struct segment_command_64
*) ((void *) cmd_hdr
);
904 /* Ignore segments with no vm size */
905 if (!seg_hdr
->vmsize
) continue;
907 /* Ignore LINKEDIT segments */
908 if (streq_safe(seg_hdr
->segname
, SEG_LINKEDIT
,
909 const_strlen(SEG_LINKEDIT
)))
914 require_action(!kxld_object_is_32_bit(object
), finish
, rval
=KERN_FAILURE
;
915 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
916 "LC_SEGMENT_64 in a 32-bit kext."));
917 require_action(!has_segment
, finish
, rval
=KERN_FAILURE
;
918 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
919 "Multiple segments in an MH_OBJECT kext."));
921 nsects
= seg_hdr
->nsects
;
922 sect_offset
= offset
+ sizeof(*seg_hdr
);
926 #endif /* KXLD_USER_OR_LP64 */
928 symtab_hdr
= (struct symtab_command
*) cmd_hdr
;
930 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
931 kxld_symtab_init_from_macho_32
, kxld_symtab_init_from_macho_64
,
932 object
->symtab
, symtab_hdr
, object
->file
,
933 /* kernel_linkedit_seg */ NULL
);
934 require_noerr(rval
, finish
);
937 uuid_hdr
= (struct uuid_command
*) cmd_hdr
;
938 kxld_uuid_init_from_macho(&object
->uuid
, uuid_hdr
);
942 /* Don't need to do anything with UNIXTHREAD or MAIN */
944 case LC_CODE_SIGNATURE
:
946 case LC_DYLD_INFO_ONLY
:
947 case LC_FUNCTION_STARTS
:
948 case LC_DATA_IN_CODE
:
949 case LC_DYLIB_CODE_SIGN_DRS
:
950 /* Various metadata that might be stored in the linkedit segment */
952 case LC_VERSION_MIN_MACOSX
:
953 case LC_VERSION_MIN_IPHONEOS
:
954 case LC_SOURCE_VERSION
:
955 /* Not supported for object files, fall through */
958 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
959 "Invalid segment type in MH_OBJECT kext: %u.", cmd_hdr
->cmd
);
966 /* Get the number of sections from the segment and build the section index */
968 rval
= kxld_array_init(&object
->sects
, sizeof(KXLDSect
), nsects
);
969 require_noerr(rval
, finish
);
971 /* Loop over all of the sections to initialize the section index */
973 for (i
= 0; i
< nsects
; ++i
) {
974 sect
= kxld_array_get_item(&object
->sects
, i
);
975 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
976 kxld_sect_init_from_macho_32
, kxld_sect_init_from_macho_64
,
977 sect
, object
->file
, §_offset
, i
, &object
->relocator
);
978 require_noerr(rval
, finish
);
981 /* Create special sections */
984 rval
= create_got(object
);
985 require_noerr(rval
, finish
);
986 #endif /* KXLD_USER_OR_GOT */
988 #if KXLD_USER_OR_COMMON
989 rval
= resolve_common_symbols(object
);
990 require_noerr(rval
, finish
);
991 #endif /* KXLD_USER_OR_COMMON */
993 /* Create the segments from the section index */
995 rval
= kxld_seg_create_seg_from_sections(&object
->segs
, &object
->sects
);
996 require_noerr(rval
, finish
);
998 rval
= kxld_seg_finalize_object_segment(&object
->segs
,
999 object
->section_order
, get_macho_header_size(object
));
1000 require_noerr(rval
, finish
);
1002 rval
= kxld_seg_init_linkedit(&object
->segs
);
1003 require_noerr(rval
, finish
);
1006 rval
= KERN_SUCCESS
;
1010 #endif /* KXLD_USER_OR_OBJECT */
1012 #if KXLD_USER_OR_ILP32
1013 /*******************************************************************************
1014 *******************************************************************************/
1016 get_macho_cmd_data_32(u_char
*file
, u_long offset
, u_int
*filetype
, u_int
*ncmds
)
1018 struct mach_header
*mach_hdr
= (struct mach_header
*) ((void *) (file
+ offset
));
1020 if (filetype
) *filetype
= mach_hdr
->filetype
;
1021 if (ncmds
) *ncmds
= mach_hdr
->ncmds
;
1023 return sizeof(*mach_hdr
);
1026 #endif /* KXLD_USER_OR_ILP32 */
1028 #if KXLD_USER_OR_LP64
1029 /*******************************************************************************
1030 *******************************************************************************/
1032 get_macho_cmd_data_64(u_char
*file
, u_long offset
, u_int
*filetype
, u_int
*ncmds
)
1034 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) ((void *) (file
+ offset
));
1036 if (filetype
) *filetype
= mach_hdr
->filetype
;
1037 if (ncmds
) *ncmds
= mach_hdr
->ncmds
;
1039 return sizeof(*mach_hdr
);
1041 #endif /* KXLD_USER_OR_LP64 */
1043 /*******************************************************************************
1044 *******************************************************************************/
1046 get_macho_header_size(const KXLDObject
*object
)
1048 KXLDSeg
*seg
= NULL
;
1049 u_long header_size
= 0;
1051 boolean_t object_is_32_bit
= kxld_object_is_32_bit(object
);
1055 /* Mach, segment, symtab, and UUID headers */
1057 header_size
+= object_is_32_bit
? sizeof(struct mach_header
) : sizeof(struct mach_header_64
);
1059 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1060 seg
= kxld_array_get_item(&object
->segs
, i
);
1061 header_size
+= kxld_seg_get_macho_header_size(seg
, object_is_32_bit
);
1064 header_size
+= kxld_symtab_get_macho_header_size();
1067 if (target_supports_slideable_kexts(object
)) {
1068 header_size
+= kxld_reloc_get_macho_header_size();
1070 #endif /* KXLD_PIC_KEXTS */
1072 if (object
->uuid
.has_uuid
) {
1073 header_size
+= kxld_uuid_get_macho_header_size();
1076 if (object
->versionmin
.has_versionmin
) {
1077 header_size
+= kxld_versionmin_get_macho_header_size();
1080 if (object
->srcversion
.has_srcversion
) {
1081 header_size
+= kxld_srcversion_get_macho_header_size();
1087 /*******************************************************************************
1088 *******************************************************************************/
1090 get_macho_data_size(const KXLDObject
*object
)
1092 KXLDSeg
*seg
= NULL
;
1093 u_long data_size
= 0;
1098 /* total all segment vmsize values */
1099 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1100 seg
= kxld_array_get_item(&object
->segs
, i
);
1101 data_size
+= (u_long
) kxld_seg_get_vmsize(seg
);
1106 /* ensure that when we eventually emit the final linked object,
1107 * appending the __DYSYMTAB data after the __LINKEDIT data will
1108 * not overflow the space allocated for the __LINKEDIT segment
1111 u_long seg_vmsize
= 0;
1112 u_long symtab_size
= 0;
1113 u_long reloc_size
= 0;
1115 /* get current __LINKEDIT sizes */
1116 seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
1117 seg_vmsize
= (u_long
) kxld_seg_get_vmsize(seg
);
1119 /* get size of symbol table data that will eventually be dumped
1120 * into the __LINKEDIT segment
1122 symtab_size
= kxld_symtab_get_macho_data_size(object
->symtab
, kxld_object_is_32_bit(object
));
1124 if (target_supports_slideable_kexts(object
)) {
1125 /* get size of __DYSYMTAB relocation entries */
1126 reloc_size
= kxld_reloc_get_macho_data_size(&object
->locrelocs
, &object
->extrelocs
);
1129 /* combine, and ensure they'll both fit within the page(s)
1130 * allocated for the __LINKEDIT segment. If they'd overflow,
1131 * increase the vmsize appropriately so no overflow will occur
1133 if ((symtab_size
+ reloc_size
) > seg_vmsize
) {
1134 u_long overflow
= (symtab_size
+ reloc_size
) - seg_vmsize
;
1135 data_size
+= round_page(overflow
);
1138 #endif // KXLD_PIC_KEXTS
1143 /*******************************************************************************
1144 *******************************************************************************/
1146 kxld_object_target_needs_swap(const KXLDObject
*object __unused
)
1151 return (object
->target_order
!= object
->host_order
);
1155 /*******************************************************************************
1156 *******************************************************************************/
1158 kxld_object_get_seg_by_name(const KXLDObject
*object
, const char *segname
)
1160 KXLDSeg
*seg
= NULL
;
1163 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1164 seg
= kxld_array_get_item(&object
->segs
, i
);
1166 if (streq_safe(segname
, seg
->segname
, sizeof(seg
->segname
))) break;
1174 /*******************************************************************************
1175 *******************************************************************************/
1176 const KXLDRelocator
*
1177 kxld_object_get_relocator(const KXLDObject
* object
)
1181 return &object
->relocator
;
1184 /*******************************************************************************
1185 *******************************************************************************/
1187 kxld_object_get_sect_by_name(const KXLDObject
*object
, const char *segname
,
1188 const char *sectname
)
1190 KXLDSect
*sect
= NULL
;
1193 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1194 sect
= kxld_array_get_item(&object
->sects
, i
);
1196 if (streq_safe(segname
, sect
->segname
, sizeof(sect
->segname
)) &&
1197 streq_safe(sectname
, sect
->sectname
, sizeof(sect
->sectname
)))
1208 /*******************************************************************************
1209 *******************************************************************************/
1211 kxld_object_get_reloc_at_symbol(const KXLDObject
*object
, const KXLDSym
*sym
)
1213 const KXLDReloc
*reloc
= NULL
;
1214 const KXLDSect
*sect
= NULL
;
1215 uint32_t offset
= 0;
1220 sect
= kxld_object_get_section_by_index(object
, sym
->sectnum
);
1221 require(sect
, finish
);
1223 if (kxld_object_is_final_image(object
)) {
1224 reloc
= kxld_reloc_get_reloc_by_offset(&object
->extrelocs
,
1227 reloc
= kxld_reloc_get_reloc_by_offset(&object
->locrelocs
,
1231 offset
= kxld_sym_get_section_offset(sym
, sect
);
1232 reloc
= kxld_reloc_get_reloc_by_offset(§
->relocs
, offset
);
1239 /*******************************************************************************
1240 *******************************************************************************/
1242 kxld_object_get_symbol_of_reloc(const KXLDObject
*object
,
1243 const KXLDReloc
*reloc
, const KXLDSect
*sect
)
1245 const KXLDSym
*sym
= NULL
;
1247 if (kxld_object_is_final_image(object
)) {
1248 sym
= kxld_reloc_get_symbol(&object
->relocator
, reloc
, object
->file
);
1250 sym
= kxld_reloc_get_symbol(&object
->relocator
, reloc
, sect
->data
);
1256 /*******************************************************************************
1257 *******************************************************************************/
1259 kxld_object_get_section_by_index(const KXLDObject
*object
, u_int sectnum
)
1261 KXLDSect
*sect
= NULL
;
1265 if (sectnum
< object
->sects
.nitems
) {
1266 sect
= kxld_array_get_item(&object
->sects
, sectnum
);
1272 /*******************************************************************************
1273 *******************************************************************************/
1275 kxld_object_get_extrelocs(const KXLDObject
*object
)
1277 const KXLDArray
*rval
= NULL
;
1281 if (kxld_object_is_final_image(object
)) {
1282 rval
= &object
->extrelocs
;
1288 /*******************************************************************************
1289 *******************************************************************************/
1291 kxld_object_get_symtab(const KXLDObject
*object
)
1295 return object
->symtab
;
1298 #if KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON
1299 /*******************************************************************************
1300 *******************************************************************************/
1301 static kern_return_t
1302 add_section(KXLDObject
*object
, KXLDSect
**sect
)
1304 kern_return_t rval
= KERN_FAILURE
;
1305 u_int nsects
= object
->sects
.nitems
;
1307 rval
= kxld_array_resize(&object
->sects
, nsects
+ 1);
1308 require_noerr(rval
, finish
);
1310 *sect
= kxld_array_get_item(&object
->sects
, nsects
);
1312 rval
= KERN_SUCCESS
;
1317 #endif /* KXLD_USER_OR_GOT || KXLD_USER_OR_COMMON */
1319 #if KXLD_USER_OR_COMMON
1320 /*******************************************************************************
1321 * If there are common symbols, calculate how much space they'll need
1322 * and create/grow the __DATA __common section to accommodate them.
1323 * Then, resolve them against that section.
1324 *******************************************************************************/
1325 static kern_return_t
1326 resolve_common_symbols(KXLDObject
*object
)
1328 kern_return_t rval
= KERN_FAILURE
;
1329 KXLDSymtabIterator iter
;
1330 KXLDSym
*sym
= NULL
;
1331 KXLDSect
*sect
= NULL
;
1332 kxld_addr_t base_addr
= 0;
1333 kxld_size_t size
= 0;
1334 kxld_size_t total_size
= 0;
1336 u_int max_align
= 0;
1339 if (!kxld_object_target_supports_common_symbols(object
)) {
1340 rval
= KERN_SUCCESS
;
1344 /* Iterate over the common symbols to calculate their total aligned size */
1345 kxld_symtab_iterator_init(&iter
, object
->symtab
, kxld_sym_is_common
, FALSE
);
1346 while ((sym
= kxld_symtab_iterator_get_next(&iter
))) {
1347 align
= kxld_sym_get_common_align(sym
);
1348 size
= kxld_sym_get_common_size(sym
);
1350 if (align
> max_align
) max_align
= align
;
1352 total_size
= kxld_align_address(total_size
, align
) + size
;
1355 /* If there are common symbols, grow or create the __DATA __common section
1359 sect
= kxld_object_get_sect_by_name(object
, SEG_DATA
, SECT_COMMON
);
1361 base_addr
= sect
->base_addr
+ sect
->size
;
1363 kxld_sect_grow(sect
, total_size
, max_align
);
1367 rval
= add_section(object
, §
);
1368 require_noerr(rval
, finish
);
1370 kxld_sect_init_zerofill(sect
, SEG_DATA
, SECT_COMMON
,
1371 total_size
, max_align
);
1374 /* Resolve the common symbols against the new section */
1375 rval
= kxld_array_get_index(&object
->sects
, sect
, §num
);
1376 require_noerr(rval
, finish
);
1378 kxld_symtab_iterator_reset(&iter
);
1379 while ((sym
= kxld_symtab_iterator_get_next(&iter
))) {
1380 align
= kxld_sym_get_common_align(sym
);
1381 size
= kxld_sym_get_common_size(sym
);
1383 base_addr
= kxld_align_address(base_addr
, align
);
1384 kxld_sym_resolve_common(sym
, sectnum
, base_addr
);
1390 rval
= KERN_SUCCESS
;
1395 #endif /* KXLD_USER_OR_COMMON */
1397 #if KXLD_USER_OR_GOT
1398 /*******************************************************************************
1399 *******************************************************************************/
1401 target_has_got(const KXLDObject
*object
)
1406 /*******************************************************************************
1407 * Create and initialize the Global Offset Table
1408 *******************************************************************************/
1409 static kern_return_t
1410 create_got(KXLDObject
*object
)
1412 kern_return_t rval
= KERN_FAILURE
;
1413 KXLDSect
*sect
= NULL
;
1417 if (!target_has_got(object
)) {
1418 rval
= KERN_SUCCESS
;
1422 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1423 sect
= kxld_array_get_item(&object
->sects
, i
);
1424 ngots
+= kxld_sect_get_ngots(sect
, &object
->relocator
,
1428 rval
= add_section(object
, §
);
1429 require_noerr(rval
, finish
);
1431 rval
= kxld_sect_init_got(sect
, ngots
);
1432 require_noerr(rval
, finish
);
1434 object
->got_is_created
= TRUE
;
1435 rval
= KERN_SUCCESS
;
1441 /*******************************************************************************
1442 *******************************************************************************/
1443 static kern_return_t
1444 populate_got(KXLDObject
*object
)
1446 kern_return_t rval
= KERN_FAILURE
;
1447 KXLDSect
*sect
= NULL
;
1450 if (!target_has_got(object
) || !object
->got_is_created
) {
1451 rval
= KERN_SUCCESS
;
1455 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1456 sect
= kxld_array_get_item(&object
->sects
, i
);
1457 if (streq_safe(sect
->segname
, KXLD_SEG_GOT
, sizeof(KXLD_SEG_GOT
)) &&
1458 streq_safe(sect
->sectname
, KXLD_SECT_GOT
, sizeof(KXLD_SECT_GOT
)))
1460 kxld_sect_populate_got(sect
, object
->symtab
,
1461 kxld_object_target_needs_swap(object
));
1466 require_action(i
< object
->sects
.nitems
, finish
, rval
=KXLD_MISSING_GOT
);
1468 rval
= KERN_SUCCESS
;
1473 #endif /* KXLD_USER_OR_GOT */
1475 /*******************************************************************************
1476 *******************************************************************************/
1478 target_supports_protected_segments(const KXLDObject
*object
)
1480 return (object
->is_final_image
&&
1481 object
->cputype
== CPU_TYPE_X86_64
);
1484 /*******************************************************************************
1485 *******************************************************************************/
1487 set_is_object_linked(KXLDObject
*object
)
1491 if (kxld_object_is_kernel(object
)) {
1492 object
->is_linked
= TRUE
;
1496 if (object
->is_final_image
) {
1497 object
->is_linked
= !object
->extrelocs
.nitems
;
1501 object
->is_linked
= TRUE
;
1502 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1503 KXLDSect
*sect
= kxld_array_get_item(&object
->sects
, i
);
1504 if (sect
->relocs
.nitems
) {
1505 object
->is_linked
= FALSE
;
1512 /*******************************************************************************
1513 *******************************************************************************/
1514 void kxld_object_clear(KXLDObject
*object __unused
)
1516 KXLDSeg
*seg
= NULL
;
1517 KXLDSect
*sect
= NULL
;
1523 if (kxld_object_is_kernel(object
)) {
1524 unswap_macho(object
->file
, object
->host_order
, object
->target_order
);
1526 #endif /* !KERNEL */
1528 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1529 seg
= kxld_array_get_item(&object
->segs
, i
);
1530 kxld_seg_clear(seg
);
1532 kxld_array_reset(&object
->segs
);
1534 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
1535 sect
= kxld_array_get_item(&object
->sects
, i
);
1536 kxld_sect_clear(sect
);
1538 kxld_array_reset(&object
->sects
);
1540 kxld_array_reset(&object
->extrelocs
);
1541 kxld_array_reset(&object
->locrelocs
);
1542 kxld_relocator_clear(&object
->relocator
);
1543 kxld_uuid_clear(&object
->uuid
);
1544 kxld_versionmin_clear(&object
->versionmin
);
1545 kxld_srcversion_clear(&object
->srcversion
);
1547 if (object
->symtab
) kxld_symtab_clear(object
->symtab
);
1549 object
->file
= NULL
;
1551 object
->filetype
= 0;
1552 object
->cputype
= 0;
1553 object
->cpusubtype
= 0;
1554 object
->is_kernel
= FALSE
;
1555 object
->is_final_image
= FALSE
;
1556 object
->is_linked
= FALSE
;
1557 object
->got_is_created
= FALSE
;
1559 #if KXLD_USER_OR_OBJECT
1560 object
->section_order
= NULL
;
1563 object
->host_order
= 0;
1564 object
->target_order
= 0;
1568 /*******************************************************************************
1569 *******************************************************************************/
1570 void kxld_object_deinit(KXLDObject
*object __unused
)
1572 KXLDSeg
*seg
= NULL
;
1573 KXLDSect
*sect
= NULL
;
1579 if (object
->file
&& kxld_object_is_kernel(object
)) {
1580 unswap_macho(object
->file
, object
->host_order
, object
->target_order
);
1582 #endif /* !KERNEL */
1584 for (i
= 0; i
< object
->segs
.maxitems
; ++i
) {
1585 seg
= kxld_array_get_slot(&object
->segs
, i
);
1586 kxld_seg_deinit(seg
);
1588 kxld_array_deinit(&object
->segs
);
1590 for (i
= 0; i
< object
->sects
.maxitems
; ++i
) {
1591 sect
= kxld_array_get_slot(&object
->sects
, i
);
1592 kxld_sect_deinit(sect
);
1594 kxld_array_deinit(&object
->sects
);
1596 kxld_array_deinit(&object
->extrelocs
);
1597 kxld_array_deinit(&object
->locrelocs
);
1599 if (object
->symtab
) {
1600 kxld_symtab_deinit(object
->symtab
);
1601 kxld_free(object
->symtab
, kxld_symtab_sizeof());
1604 bzero(object
, sizeof(*object
));
1607 /*******************************************************************************
1608 *******************************************************************************/
1610 kxld_object_get_file(const KXLDObject
*object
)
1614 return object
->file
;
1617 /*******************************************************************************
1618 *******************************************************************************/
1620 kxld_object_get_name(const KXLDObject
*object
)
1624 return object
->name
;
1627 /*******************************************************************************
1628 *******************************************************************************/
1630 kxld_object_is_32_bit(const KXLDObject
*object
)
1634 return kxld_is_32_bit(object
->cputype
);
1637 /*******************************************************************************
1638 *******************************************************************************/
1640 kxld_object_is_final_image(const KXLDObject
*object
)
1644 return object
->is_final_image
;
1647 /*******************************************************************************
1648 *******************************************************************************/
1650 kxld_object_is_kernel(const KXLDObject
*object
)
1654 return object
->is_kernel
;
1657 /*******************************************************************************
1658 *******************************************************************************/
1660 kxld_object_is_linked(const KXLDObject
*object
)
1664 return object
->is_linked
;
1667 /*******************************************************************************
1668 *******************************************************************************/
1670 kxld_object_target_supports_strict_patching(const KXLDObject
*object
)
1674 return (object
->cputype
!= CPU_TYPE_I386
);
1677 /*******************************************************************************
1678 *******************************************************************************/
1680 kxld_object_target_supports_common_symbols(const KXLDObject
*object
)
1684 return (object
->cputype
== CPU_TYPE_I386
);
1687 /*******************************************************************************
1688 *******************************************************************************/
1690 kxld_object_get_vmsize(const KXLDObject
*object
, u_long
*header_size
,
1699 /* vmsize is the padded header page(s) + segment vmsizes */
1701 *header_size
= (object
->is_final_image
) ?
1702 0 : round_page(get_macho_header_size(object
));
1703 *vmsize
= *header_size
+ get_macho_data_size(object
);
1707 /*******************************************************************************
1708 *******************************************************************************/
1710 kxld_object_set_linked_object_size(KXLDObject
*object
, u_long vmsize
)
1712 object
->output_buffer_size
= vmsize
; /* cache this for use later */
1716 /*******************************************************************************
1717 *******************************************************************************/
1719 kxld_object_export_linked_object(const KXLDObject
*object
,
1720 u_char
*linked_object
)
1722 kern_return_t rval
= KERN_FAILURE
;
1723 KXLDSeg
*seg
= NULL
;
1725 u_long header_size
= 0;
1726 u_long header_offset
= 0;
1727 u_long data_offset
= 0;
1730 boolean_t is_32bit_object
= kxld_object_is_32_bit(object
);
1733 check(linked_object
);
1735 /* Calculate the size of the headers and data */
1737 header_size
= get_macho_header_size(object
);
1738 data_offset
= (object
->is_final_image
) ? header_size
: round_page(header_size
);
1739 size
= object
->output_buffer_size
;
1741 /* Copy data to the file */
1743 ncmds
= object
->segs
.nitems
+ 1 /* LC_SYMTAB */;
1746 /* don't write out a DYSYMTAB segment for targets that can't digest it
1748 if (target_supports_slideable_kexts(object
)) {
1749 ncmds
++; /* dysymtab */
1751 #endif /* KXLD_PIC_KEXTS */
1753 if (object
->uuid
.has_uuid
== TRUE
) {
1757 if (object
->versionmin
.has_versionmin
== TRUE
) {
1761 if (object
->srcversion
.has_srcversion
== TRUE
) {
1765 rval
= export_macho_header(object
, linked_object
, ncmds
, &header_offset
, header_size
);
1766 require_noerr(rval
, finish
);
1768 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1769 seg
= kxld_array_get_item(&object
->segs
, i
);
1771 rval
= kxld_seg_export_macho_to_vm(seg
, linked_object
, &header_offset
,
1772 header_size
, size
, object
->link_addr
, is_32bit_object
);
1773 require_noerr(rval
, finish
);
1776 seg
= kxld_object_get_seg_by_name(object
, SEG_LINKEDIT
);
1777 data_offset
= (u_long
) (seg
->link_addr
- object
->link_addr
);
1779 rval
= kxld_symtab_export_macho(object
->symtab
, linked_object
, &header_offset
,
1780 header_size
, &data_offset
, size
, is_32bit_object
);
1781 require_noerr(rval
, finish
);
1784 if (target_supports_slideable_kexts(object
)) {
1785 rval
= kxld_reloc_export_macho(&object
->relocator
, &object
->locrelocs
,
1786 &object
->extrelocs
, linked_object
, &header_offset
, header_size
,
1787 &data_offset
, size
);
1788 require_noerr(rval
, finish
);
1790 #endif /* KXLD_PIC_KEXTS */
1792 if (object
->uuid
.has_uuid
) {
1793 rval
= kxld_uuid_export_macho(&object
->uuid
, linked_object
, &header_offset
, header_size
);
1794 require_noerr(rval
, finish
);
1797 if (object
->versionmin
.has_versionmin
) {
1798 rval
= kxld_versionmin_export_macho(&object
->versionmin
, linked_object
, &header_offset
, header_size
);
1799 require_noerr(rval
, finish
);
1802 if (object
->srcversion
.has_srcversion
) {
1803 rval
= kxld_srcversion_export_macho(&object
->srcversion
, linked_object
, &header_offset
, header_size
);
1804 require_noerr(rval
, finish
);
1808 unswap_macho(linked_object
, object
->host_order
, object
->target_order
);
1811 rval
= KERN_SUCCESS
;
1817 /*******************************************************************************
1818 *******************************************************************************/
1819 static kern_return_t
1820 export_macho_header(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1821 u_long
*header_offset
, u_long header_size
)
1823 kern_return_t rval
= KERN_FAILURE
;
1827 check(header_offset
);
1829 KXLD_3264_FUNC(kxld_object_is_32_bit(object
), rval
,
1830 export_macho_header_32
, export_macho_header_64
,
1831 object
, buf
, ncmds
, header_offset
, header_size
);
1832 require_noerr(rval
, finish
);
1834 rval
= KERN_SUCCESS
;
1840 #if KXLD_USER_OR_ILP32
1841 /*******************************************************************************
1842 *******************************************************************************/
1843 static kern_return_t
1844 export_macho_header_32(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1845 u_long
*header_offset
, u_long header_size
)
1847 kern_return_t rval
= KERN_FAILURE
;
1848 struct mach_header
*mach
= NULL
;
1852 check(header_offset
);
1854 require_action(sizeof(*mach
) <= header_size
- *header_offset
, finish
,
1856 mach
= (struct mach_header
*) ((void *) (buf
+ *header_offset
));
1858 mach
->magic
= MH_MAGIC
;
1859 mach
->cputype
= object
->cputype
;
1860 mach
->cpusubtype
= object
->cpusubtype
;
1861 mach
->filetype
= object
->filetype
;
1862 mach
->ncmds
= ncmds
;
1863 mach
->sizeofcmds
= (uint32_t) (header_size
- sizeof(*mach
));
1864 mach
->flags
= MH_NOUNDEFS
;
1866 *header_offset
+= sizeof(*mach
);
1868 rval
= KERN_SUCCESS
;
1873 #endif /* KXLD_USER_OR_ILP32 */
1875 #if KXLD_USER_OR_LP64
1876 /*******************************************************************************
1877 *******************************************************************************/
1878 static kern_return_t
1879 export_macho_header_64(const KXLDObject
*object
, u_char
*buf
, u_int ncmds
,
1880 u_long
*header_offset
, u_long header_size
)
1882 kern_return_t rval
= KERN_FAILURE
;
1883 struct mach_header_64
*mach
= NULL
;
1887 check(header_offset
);
1889 require_action(sizeof(*mach
) <= header_size
- *header_offset
, finish
,
1891 mach
= (struct mach_header_64
*) ((void *) (buf
+ *header_offset
));
1893 mach
->magic
= MH_MAGIC_64
;
1894 mach
->cputype
= object
->cputype
;
1895 mach
->cpusubtype
= object
->cpusubtype
;
1896 mach
->filetype
= object
->filetype
;
1897 mach
->ncmds
= ncmds
;
1898 mach
->sizeofcmds
= (uint32_t) (header_size
- sizeof(*mach
));
1899 mach
->flags
= MH_NOUNDEFS
;
1901 *header_offset
+= sizeof(*mach
);
1903 rval
= KERN_SUCCESS
;
1908 #endif /* KXLD_USER_OR_LP64 */
1910 /*******************************************************************************
1911 *******************************************************************************/
1913 kxld_object_index_symbols_by_name(KXLDObject
*object
)
1915 return kxld_symtab_index_symbols_by_name(object
->symtab
);
1918 /*******************************************************************************
1919 *******************************************************************************/
1921 kxld_object_index_cxx_symbols_by_value(KXLDObject
*object
)
1923 return kxld_symtab_index_cxx_symbols_by_value(object
->symtab
);
1926 /*******************************************************************************
1927 *******************************************************************************/
1929 kxld_object_relocate(KXLDObject
*object
, kxld_addr_t link_address
)
1931 kern_return_t rval
= KERN_FAILURE
;
1932 KXLDSeg
*seg
= NULL
;
1937 object
->link_addr
= link_address
;
1939 /* Relocate segments (which relocates the sections) */
1940 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
1941 seg
= kxld_array_get_item(&object
->segs
, i
);
1942 kxld_seg_relocate(seg
, link_address
);
1945 /* Relocate symbols */
1946 rval
= kxld_symtab_relocate(object
->symtab
, &object
->sects
);
1947 require_noerr(rval
, finish
);
1949 rval
= KERN_SUCCESS
;
1954 /*******************************************************************************
1955 *******************************************************************************/
1957 get_mutable_sym(const KXLDObject
*object
, const KXLDSym
*sym
)
1959 KXLDSym
*rval
= NULL
;
1960 kern_return_t result
= KERN_FAILURE
;
1963 result
= kxld_symtab_get_sym_index(object
->symtab
, sym
, &i
);
1964 require_noerr(result
, finish
);
1966 rval
= kxld_symtab_get_symbol_by_index(object
->symtab
, i
);
1967 require_action(rval
== sym
, finish
, rval
=NULL
);
1973 /*******************************************************************************
1974 *******************************************************************************/
1976 kxld_object_resolve_symbol(KXLDObject
*object
,
1977 const KXLDSym
*sym
, kxld_addr_t addr
)
1979 kern_return_t rval
= KERN_FAILURE
;
1980 KXLDSym
*resolved_sym
= NULL
;
1982 resolved_sym
= get_mutable_sym(object
, sym
);
1983 require_action(resolved_sym
, finish
, rval
=KERN_FAILURE
);
1985 rval
= kxld_sym_resolve(resolved_sym
, addr
);
1986 require_noerr(rval
, finish
);
1988 rval
= KERN_SUCCESS
;
1993 /*******************************************************************************
1994 *******************************************************************************/
1996 kxld_object_patch_symbol(KXLDObject
*object
, const struct kxld_sym
*sym
)
1998 kern_return_t rval
= KERN_FAILURE
;
1999 KXLDSym
*patched_sym
= NULL
;
2001 patched_sym
= get_mutable_sym(object
, sym
);
2002 require_action(patched_sym
, finish
, rval
=KERN_FAILURE
);
2004 (void) kxld_sym_patch(patched_sym
);
2005 rval
= KERN_SUCCESS
;
2010 /*******************************************************************************
2011 *******************************************************************************/
2013 kxld_object_add_symbol(KXLDObject
*object
, char *name
, kxld_addr_t link_addr
,
2014 const KXLDSym
**sym_out
)
2016 kern_return_t rval
= KERN_FAILURE
;
2017 KXLDSym
*sym
= NULL
;
2019 rval
= kxld_symtab_add_symbol(object
->symtab
, name
, link_addr
, &sym
);
2020 require_noerr(rval
, finish
);
2023 rval
= KERN_SUCCESS
;
2028 /*******************************************************************************
2029 *******************************************************************************/
2031 kxld_object_process_relocations(KXLDObject
*object
,
2032 const KXLDDict
*patched_vtables
)
2034 kern_return_t rval
= KERN_FAILURE
;
2036 (void) kxld_relocator_set_vtables(&object
->relocator
, patched_vtables
);
2038 /* Process relocation entries and populate the global offset table.
2040 * For final linked images: the relocation entries are contained in a couple
2041 * of tables hanging off the end of the symbol table. The GOT has its own
2042 * section created by the linker; we simply need to fill it.
2044 * For object files: the relocation entries are bound to each section.
2045 * The GOT, if it exists for the target architecture, is created by kxld,
2046 * and we must populate it according to our internal structures.
2048 if (object
->is_final_image
) {
2049 #if KXLD_USER_OR_BUNDLE
2050 rval
= process_symbol_pointers(object
);
2051 require_noerr(rval
, finish
);
2053 rval
= process_relocs_from_tables(object
);
2054 require_noerr(rval
, finish
);
2056 require_action(FALSE
, finish
, rval
=KERN_FAILURE
);
2057 #endif /* KXLD_USER_OR_BUNDLE */
2059 #if KXLD_USER_OR_GOT
2061 rval
= populate_got(object
);
2062 require_noerr(rval
, finish
);
2063 #endif /* KXLD_USER_OR_GOT */
2064 #if KXLD_USER_OR_OBJECT
2065 rval
= process_relocs_from_sections(object
);
2066 require_noerr(rval
, finish
);
2068 require_action(FALSE
, finish
, rval
=KERN_FAILURE
);
2069 #endif /* KXLD_USER_OR_OBJECT */
2072 /* Populate kmod info structure */
2073 rval
= populate_kmod_info(object
);
2074 require_noerr(rval
, finish
);
2076 rval
= KERN_SUCCESS
;
2081 #if KXLD_USER_OR_BUNDLE
2083 #define SECT_SYM_PTRS "__nl_symbol_ptr"
2085 /*******************************************************************************
2086 * Final linked images create an __nl_symbol_ptr section for the global offset
2087 * table and for symbol pointer lookups in general. Rather than use relocation
2088 * entries, the linker creates an "indirect symbol table" which stores indexes
2089 * into the symbol table corresponding to the entries of this section. This
2090 * function populates the section with the relocated addresses of those symbols.
2091 *******************************************************************************/
2092 static kern_return_t
2093 process_symbol_pointers(KXLDObject
*object
)
2095 kern_return_t rval
= KERN_FAILURE
;
2096 KXLDSect
*sect
= NULL
;
2097 KXLDSym
*sym
= NULL
;
2098 int32_t *symidx
= NULL
;
2099 u_char
*symptr
= NULL
;
2100 u_long symptrsize
= 0;
2107 require_action(object
->is_final_image
&& object
->dysymtab_hdr
,
2108 finish
, rval
=KERN_FAILURE
);
2110 /* Get the __DATA,__nl_symbol_ptr section. If it doesn't exist, we have
2114 sect
= kxld_object_get_sect_by_name(object
, SEG_DATA
, SECT_SYM_PTRS
);
2115 if (!sect
|| !(sect
->flags
& S_NON_LAZY_SYMBOL_POINTERS
)) {
2116 rval
= KERN_SUCCESS
;
2120 /* Calculate the table offset and number of entries in the section */
2122 if (kxld_object_is_32_bit(object
)) {
2123 symptrsize
= sizeof(uint32_t);
2125 symptrsize
= sizeof(uint64_t);
2128 nsyms
= (u_int
) (sect
->size
/ symptrsize
);
2129 firstsym
= sect
->reserved1
;
2131 require_action(firstsym
+ nsyms
<= object
->dysymtab_hdr
->nindirectsyms
,
2132 finish
, rval
=KERN_FAILURE
;
2133 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
2134 "firstsym + nsyms > object->dysymtab_hdr->nindirectsyms"));
2136 /* Iterate through the indirect symbol table and fill in the section of
2137 * symbol pointers. There are three cases:
2138 * 1) A normal symbol - put its value directly in the table
2139 * 2) An INDIRECT_SYMBOL_LOCAL - symbols that are local and already have
2140 * their offset from the start of the file in the section. Simply
2141 * add the file's link address to fill this entry.
2142 * 3) An INDIRECT_SYMBOL_ABS - prepopulated absolute symbols. No
2143 * action is required.
2146 symidx
= (int32_t *) ((void *) (object
->file
+ object
->dysymtab_hdr
->indirectsymoff
));
2148 symptr
= sect
->data
;
2149 for (i
= 0; i
< nsyms
; ++i
, ++symidx
, symptr
+=symptrsize
) {
2150 if (*symidx
& INDIRECT_SYMBOL_LOCAL
) {
2151 if (*symidx
& INDIRECT_SYMBOL_ABS
) continue;
2153 add_to_ptr(symptr
, object
->link_addr
, kxld_object_is_32_bit(object
));
2155 sym
= kxld_symtab_get_symbol_by_index(object
->symtab
, *symidx
);
2156 require_action(sym
, finish
, rval
=KERN_FAILURE
);
2158 add_to_ptr(symptr
, sym
->link_addr
, kxld_object_is_32_bit(object
));
2162 rval
= KERN_SUCCESS
;
2167 /*******************************************************************************
2168 *******************************************************************************/
2170 get_seg_by_base_addr(KXLDObject
*object
, kxld_addr_t base_addr
)
2172 KXLDSeg
*seg
= NULL
;
2173 kxld_addr_t start
= 0;
2174 kxld_addr_t end
= 0;
2177 for (i
= 0; i
< object
->segs
.nitems
; ++i
) {
2178 seg
= kxld_array_get_item(&object
->segs
, i
);
2179 start
= seg
->base_addr
;
2180 end
= seg
->base_addr
+ seg
->vmsize
;
2182 if (start
<= base_addr
&& base_addr
< end
) return seg
;
2188 /*******************************************************************************
2189 *******************************************************************************/
2190 static kern_return_t
2191 process_relocs_from_tables(KXLDObject
*object
)
2193 kern_return_t rval
= KERN_FAILURE
;
2194 KXLDReloc
*reloc
= NULL
;
2195 KXLDSeg
*seg
= NULL
;
2198 /* Process external relocations */
2199 for (i
= 0; i
< object
->extrelocs
.nitems
; ++i
) {
2200 reloc
= kxld_array_get_item(&object
->extrelocs
, i
);
2202 seg
= get_seg_by_base_addr(object
, reloc
->address
);
2203 require_action(seg
, finish
, rval
=KERN_FAILURE
);
2205 rval
= kxld_relocator_process_table_reloc(&object
->relocator
, reloc
,
2206 seg
, object
->link_addr
);
2207 require_noerr(rval
, finish
);
2210 /* Process local relocations */
2211 for (i
= 0; i
< object
->locrelocs
.nitems
; ++i
) {
2212 reloc
= kxld_array_get_item(&object
->locrelocs
, i
);
2214 seg
= get_seg_by_base_addr(object
, reloc
->address
);
2215 require_action(seg
, finish
, rval
=KERN_FAILURE
);
2217 rval
= kxld_relocator_process_table_reloc(&object
->relocator
, reloc
,
2218 seg
, object
->link_addr
);
2219 require_noerr(rval
, finish
);
2222 rval
= KERN_SUCCESS
;
2227 /*******************************************************************************
2228 *******************************************************************************/
2230 add_to_ptr(u_char
*symptr
, kxld_addr_t val
, boolean_t is_32_bit
)
2233 uint32_t *ptr
= (uint32_t *) ((void *) symptr
);
2234 *ptr
+= (uint32_t) val
;
2236 uint64_t *ptr
= (uint64_t *) ((void *) symptr
);
2237 *ptr
+= (uint64_t) val
;
2240 #endif /* KXLD_USER_OR_BUNDLE */
2242 #if KXLD_USER_OR_OBJECT
2243 /*******************************************************************************
2244 *******************************************************************************/
2245 static kern_return_t
2246 process_relocs_from_sections(KXLDObject
*object
)
2248 kern_return_t rval
= KERN_FAILURE
;
2249 KXLDSect
*sect
= NULL
;
2252 for (i
= 0; i
< object
->sects
.nitems
; ++i
) {
2253 sect
= kxld_array_get_item(&object
->sects
, i
);
2254 rval
= kxld_sect_process_relocs(sect
, &object
->relocator
);
2255 require_noerr(rval
, finish
);
2258 rval
= KERN_SUCCESS
;
2262 #endif /* KXLD_USER_OR_OBJECT */
2264 /*******************************************************************************
2265 *******************************************************************************/
2266 static kern_return_t
2267 populate_kmod_info(KXLDObject
*object
)
2269 kern_return_t rval
= KERN_FAILURE
;
2270 KXLDSect
*kmodsect
= NULL
;
2271 KXLDSym
*kmodsym
= NULL
;
2272 kmod_info_t
*kmod_info
= NULL
;
2273 u_long kmod_offset
= 0;
2277 if (kxld_object_is_kernel(object
)) {
2278 rval
= KERN_SUCCESS
;
2282 kxld_object_get_vmsize(object
, &header_size
, &size
);
2284 kmodsym
= kxld_symtab_get_locally_defined_symbol_by_name(object
->symtab
,
2285 KXLD_KMOD_INFO_SYMBOL
);
2286 require_action(kmodsym
, finish
, rval
=KERN_FAILURE
;
2287 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogNoKmodInfo
));
2289 kmodsect
= kxld_array_get_item(&object
->sects
, kmodsym
->sectnum
);
2290 kmod_offset
= (u_long
) (kmodsym
->base_addr
- kmodsect
->base_addr
);
2291 kmod_info
= (kmod_info_t
*) ((void *) (kmodsect
->data
+ kmod_offset
));
2293 if (kxld_object_is_32_bit(object
)) {
2294 kmod_info_32_v1_t
*kmod
= (kmod_info_32_v1_t
*) (kmod_info
);
2295 kmod
->address
= (uint32_t) object
->link_addr
;
2296 kmod
->size
= (uint32_t) size
;
2297 kmod
->hdr_size
= (uint32_t) header_size
;
2300 if (kxld_object_target_needs_swap(object
)) {
2301 kmod
->address
= OSSwapInt32(kmod
->address
);
2302 kmod
->size
= OSSwapInt32(kmod
->size
);
2303 kmod
->hdr_size
= OSSwapInt32(kmod
->hdr_size
);
2305 #endif /* !KERNEL */
2307 kmod_info_64_v1_t
*kmod
= (kmod_info_64_v1_t
*) (kmod_info
);
2308 kmod
->address
= object
->link_addr
;
2310 kmod
->hdr_size
= header_size
;
2313 if (kxld_object_target_needs_swap(object
)) {
2314 kmod
->address
= OSSwapInt64(kmod
->address
);
2315 kmod
->size
= OSSwapInt64(kmod
->size
);
2316 kmod
->hdr_size
= OSSwapInt64(kmod
->hdr_size
);
2318 #endif /* !KERNEL */
2322 rval
= KERN_SUCCESS
;
2329 /*******************************************************************************
2330 *******************************************************************************/
2332 target_supports_slideable_kexts(const KXLDObject
*object
)
2336 return ( object
->cputype
!= CPU_TYPE_I386
2337 && object
->include_kaslr_relocs
2340 #endif /* KXLD_PIC_KEXTS */