2 * Copyright (c) 2007-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <mach-o/loader.h>
31 #include <mach-o/nlist.h>
32 #include <mach-o/reloc.h>
34 #include <kern/kalloc.h>
35 #include <libkern/libkern.h>
36 #include <mach/vm_param.h>
37 #include <vm/vm_kern.h>
41 #include <mach/mach_init.h>
42 #include <mach-o/swap.h>
45 #define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
46 #include <AssertMacros.h>
48 #include "kxld_util.h"
51 static void unswap_macho_32(u_char
*file
, enum NXByteOrder host_order
,
52 enum NXByteOrder target_order
);
53 static void unswap_macho_64(u_char
*file
, enum NXByteOrder host_order
,
54 enum NXByteOrder target_order
);
58 static unsigned long num_allocations
= 0;
59 static unsigned long num_frees
= 0;
60 static unsigned long bytes_allocated
= 0;
61 static unsigned long bytes_freed
= 0;
64 static KXLDLoggingCallback s_logging_callback
= NULL
;
65 static char s_callback_name
[64] = "internal";
66 static void *s_callback_data
= NULL
;
69 static boolean_t s_cross_link_enabled
= FALSE
;
70 static kxld_size_t s_cross_link_page_size
= PAGE_SIZE
;
74 /*******************************************************************************
75 *******************************************************************************/
77 kxld_set_logging_callback(KXLDLoggingCallback logging_callback
)
79 s_logging_callback
= logging_callback
;
82 /*******************************************************************************
83 *******************************************************************************/
85 kxld_set_logging_callback_data(const char *name
, void *user_data
)
88 (void)strlcpy(s_callback_name
, name
, sizeof(s_callback_name
));
89 /* disallow format strings in the kxld logging callback name */
90 for (size_t i
= 0; i
< sizeof(s_callback_name
); i
++) {
91 if (s_callback_name
[i
] == '%') {
92 s_callback_name
[i
] = '.';
96 (void)strlcpy(s_callback_name
, "internal", sizeof(s_callback_name
));
99 s_callback_data
= user_data
;
102 /*******************************************************************************
103 *******************************************************************************/
105 kxld_log(KXLDLogSubsystem subsystem
, KXLDLogLevel level
,
106 const char *in_format
, ...)
108 char stack_buffer
[256];
109 char *alloc_buffer
= NULL
;
110 char *format
= stack_buffer
;
114 if (s_logging_callback
) {
116 length
= snprintf(stack_buffer
, sizeof(stack_buffer
), "kxld[%s]: %s",
117 s_callback_name
, in_format
);
119 if (length
>= sizeof(stack_buffer
)) {
121 alloc_buffer
= kxld_alloc(length
);
122 if (!alloc_buffer
) return;
124 snprintf(alloc_buffer
, length
, "kxld[%s]: %s",
125 s_callback_name
, in_format
);
126 format
= alloc_buffer
;
129 va_start(ap
, in_format
);
130 s_logging_callback(subsystem
, level
, format
, ap
, s_callback_data
);
134 kxld_free(alloc_buffer
, length
);
139 /* We'll use kalloc for any page-based allocations under this threshold, and
140 * kmem_alloc otherwise.
142 #define KALLOC_MAX 16 * 1024
144 /*******************************************************************************
145 *******************************************************************************/
147 kxld_alloc(size_t size
)
160 bytes_allocated
+= size
;
167 /*******************************************************************************
168 *******************************************************************************/
170 kxld_page_alloc_untracked(size_t size
)
174 kern_return_t rval
= 0;
175 vm_offset_t addr
= 0;
178 size
= round_page(size
);
181 if (size
< KALLOC_MAX
) {
184 rval
= kmem_alloc(kernel_map
, &addr
, size
, VM_KERN_MEMORY_OSKEXT
);
185 if (!rval
) ptr
= (void *) addr
;
194 /*******************************************************************************
195 *******************************************************************************/
197 kxld_page_alloc(size_t size
)
201 ptr
= kxld_page_alloc_untracked(size
);
205 bytes_allocated
+= round_page(size
);
212 /*******************************************************************************
213 *******************************************************************************/
215 kxld_alloc_pageable(size_t size
)
217 size
= round_page(size
);
220 kern_return_t rval
= 0;
223 rval
= kmem_alloc_pageable(kernel_map
, &ptr
, size
, VM_KERN_MEMORY_OSKEXT
);
228 return kxld_page_alloc_untracked(size
);
232 /*******************************************************************************
233 *******************************************************************************/
235 kxld_free(void *ptr
, size_t size __unused
)
249 /*******************************************************************************
250 *******************************************************************************/
252 kxld_page_free_untracked(void *ptr
, size_t size __unused
)
255 size
= round_page(size
);
257 if (size
< KALLOC_MAX
) {
260 kmem_free(kernel_map
, (vm_offset_t
) ptr
, size
);
268 /*******************************************************************************
269 *******************************************************************************/
271 kxld_page_free(void *ptr
, size_t size
)
275 bytes_freed
+= round_page(size
);
277 kxld_page_free_untracked(ptr
, size
);
280 /*******************************************************************************
281 *******************************************************************************/
283 validate_and_swap_macho_32(u_char
*file
, u_long size
285 , enum NXByteOrder host_order
289 kern_return_t rval
= KERN_FAILURE
;
290 struct mach_header
*mach_hdr
= (struct mach_header
*) ((void *) file
);
291 struct load_command
*load_hdr
= NULL
;
292 struct segment_command
*seg_hdr
= NULL
;
293 struct section
*sects
= NULL
;
294 struct relocation_info
*relocs
= NULL
;
295 struct symtab_command
*symtab_hdr
= NULL
;
296 struct nlist
*symtab
= NULL
;
303 boolean_t swap
= FALSE
;
309 /* Verify that the file is big enough for the mach header */
310 require_action(size
>= sizeof(*mach_hdr
), finish
,
312 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
313 offset
= sizeof(*mach_hdr
);
316 /* Swap the mach header if necessary */
317 if (mach_hdr
->magic
== MH_CIGAM
) {
319 (void) swap_mach_header(mach_hdr
, host_order
);
323 /* Validate the mach_header's magic number */
324 require_action(mach_hdr
->magic
== MH_MAGIC
, finish
,
326 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
327 "Invalid magic number: 0x%x.", mach_hdr
->magic
));
329 /* If in the running kernel, and asked to validate the kernel
330 * (which is the only file of type MH_EXECUTE we should ever see),
331 * then just assume it's ok or we wouldn't be running to begin with.
334 if (mach_hdr
->filetype
== MH_EXECUTE
) {
340 /* Validate and potentially swap the load commands */
341 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= cmdsize
) {
343 /* Get the load command and size */
344 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
346 cmdsize
= load_hdr
->cmdsize
;
350 cmd
= OSSwapInt32(load_hdr
->cmd
);
351 cmdsize
= OSSwapInt32(load_hdr
->cmdsize
);
355 /* Verify that the file is big enough to contain the load command */
356 require_action(size
>= offset
+ cmdsize
, finish
,
358 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
362 /* Get and swap the segment header */
363 seg_hdr
= (struct segment_command
*) load_hdr
;
365 if (swap
) swap_segment_command(seg_hdr
, host_order
);
368 /* Get and swap the section headers */
369 sects
= (struct section
*) &seg_hdr
[1];
371 if (swap
) swap_section(sects
, seg_hdr
->nsects
, host_order
);
374 /* Ignore segments with no vm size */
375 if (!seg_hdr
->vmsize
) continue;
377 /* Verify that the file is big enough for the segment data. */
378 require_action(size
>= seg_hdr
->fileoff
+ seg_hdr
->filesize
, finish
,
380 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
382 for (j
= 0; j
< seg_hdr
->nsects
; ++j
) {
384 /* Verify that, if the section is not to be zero filled on
385 * demand, that file is big enough for the section's data.
387 require_action((sects
[j
].flags
& S_ZEROFILL
) ||
388 (size
>= sects
[j
].offset
+ sects
[j
].size
), finish
,
390 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
392 /* Verify that the file is big enough for the section's
393 * relocation entries.
395 require_action(size
>=
396 sects
[j
].reloff
+ sects
[j
].nreloc
* sizeof(*relocs
), finish
,
398 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
400 /* Swap the relocation entries */
401 relocs
= (struct relocation_info
*) ((void *) (file
+ sects
[j
].reloff
));
404 swap_relocation_info(relocs
, sects
[j
].nreloc
,
412 /* Get and swap the symtab header */
413 symtab_hdr
= (struct symtab_command
*) load_hdr
;
415 if (swap
) swap_symtab_command(symtab_hdr
, host_order
);
418 /* Verify that the file is big enough for the symbol table */
419 require_action(size
>=
420 symtab_hdr
->symoff
+ symtab_hdr
->nsyms
* sizeof(*symtab
), finish
,
422 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
424 /* Verify that the file is big enough for the string table */
425 require_action(size
>= symtab_hdr
->stroff
+ symtab_hdr
->strsize
, finish
,
427 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
430 /* Swap the symbol table entries */
431 symtab
= (struct nlist
*) ((void *) (file
+ symtab_hdr
->symoff
));
432 if (swap
) swap_nlist(symtab
, symtab_hdr
->nsyms
, host_order
);
438 /* Swap the load command */
439 if (swap
) swap_load_command(load_hdr
, host_order
);
451 /*******************************************************************************
452 *******************************************************************************/
454 validate_and_swap_macho_64(u_char
*file
, u_long size
456 , enum NXByteOrder host_order
460 kern_return_t rval
= KERN_FAILURE
;
461 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) ((void *) file
);
462 struct load_command
*load_hdr
= NULL
;
463 struct segment_command_64
*seg_hdr
= NULL
;
464 struct section_64
*sects
= NULL
;
465 struct relocation_info
*relocs
= NULL
;
466 struct symtab_command
*symtab_hdr
= NULL
;
467 struct nlist_64
*symtab
= NULL
;
474 boolean_t swap
= FALSE
;
480 /* Verify that the file is big enough for the mach header */
481 require_action(size
>= sizeof(*mach_hdr
), finish
,
483 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
484 offset
= sizeof(*mach_hdr
);
487 /* Swap the mach header if necessary */
488 if (mach_hdr
->magic
== MH_CIGAM_64
) {
490 (void) swap_mach_header_64(mach_hdr
, host_order
);
494 /* Validate the mach_header's magic number */
495 require_action(mach_hdr
->magic
== MH_MAGIC_64
, finish
,
497 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
498 "Invalid magic number: 0x%x.", mach_hdr
->magic
));
500 /* If in the running kernel, and asked to validate the kernel
501 * (which is the only file of type MH_EXECUTE we should ever see),
502 * then just assume it's ok or we wouldn't be running to begin with.
505 if (mach_hdr
->filetype
== MH_EXECUTE
) {
511 /* Validate and potentially swap the load commands */
512 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= cmdsize
) {
513 /* Get the load command and size */
514 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
516 cmdsize
= load_hdr
->cmdsize
;
520 cmd
= OSSwapInt32(load_hdr
->cmd
);
521 cmdsize
= OSSwapInt32(load_hdr
->cmdsize
);
525 /* Verify that the file is big enough to contain the load command */
526 require_action(size
>= offset
+ cmdsize
, finish
,
528 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
531 /* Get and swap the segment header */
532 seg_hdr
= (struct segment_command_64
*) ((void *) load_hdr
);
534 if (swap
) swap_segment_command_64(seg_hdr
, host_order
);
537 /* Get and swap the section headers */
538 sects
= (struct section_64
*) &seg_hdr
[1];
540 if (swap
) swap_section_64(sects
, seg_hdr
->nsects
, host_order
);
543 /* If the segment has no vm footprint, skip it */
544 if (!seg_hdr
->vmsize
) continue;
546 /* Verify that the file is big enough for the segment data. */
547 require_action(size
>= seg_hdr
->fileoff
+ seg_hdr
->filesize
, finish
,
549 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
551 for (j
= 0; j
< seg_hdr
->nsects
; ++j
) {
553 /* Verify that, if the section is not to be zero filled on
554 * demand, that file is big enough for the section's data.
556 require_action((sects
[j
].flags
& S_ZEROFILL
) ||
557 (size
>= sects
[j
].offset
+ sects
[j
].size
), finish
,
559 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
561 /* Verify that the file is big enough for the section's
562 * relocation entries.
564 require_action(size
>=
565 sects
[j
].reloff
+ sects
[j
].nreloc
* sizeof(*relocs
), finish
,
567 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
569 /* Swap the relocation entries */
570 relocs
= (struct relocation_info
*) ((void *) (file
+ sects
[j
].reloff
));
573 swap_relocation_info(relocs
, sects
[j
].nreloc
,
581 /* Get and swap the symtab header */
582 symtab_hdr
= (struct symtab_command
*) load_hdr
;
584 if (swap
) swap_symtab_command(symtab_hdr
, host_order
);
587 /* Verify that the file is big enough for the symbol table */
588 require_action(size
>=
589 symtab_hdr
->symoff
+ symtab_hdr
->nsyms
* sizeof(*symtab
), finish
,
591 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
593 /* Verify that the file is big enough for the string table */
594 require_action(size
>= symtab_hdr
->stroff
+ symtab_hdr
->strsize
, finish
,
596 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
599 /* Swap the symbol table entries */
600 symtab
= (struct nlist_64
*) ((void *) (file
+ symtab_hdr
->symoff
));
601 if (swap
) swap_nlist_64(symtab
, symtab_hdr
->nsyms
, host_order
);
607 /* Swap the load command */
608 if (swap
) swap_load_command(load_hdr
, host_order
);
621 /*******************************************************************************
622 *******************************************************************************/
623 void unswap_macho(u_char
*file
, enum NXByteOrder host_order
,
624 enum NXByteOrder target_order
)
626 struct mach_header
*hdr
= (struct mach_header
*) ((void *) file
);
630 if (hdr
->magic
== MH_MAGIC
) {
631 unswap_macho_32(file
, host_order
, target_order
);
632 } else if (hdr
->magic
== MH_MAGIC_64
) {
633 unswap_macho_64(file
, host_order
, target_order
);
637 /*******************************************************************************
638 *******************************************************************************/
640 unswap_macho_32(u_char
*file
, enum NXByteOrder host_order
,
641 enum NXByteOrder target_order
)
643 struct mach_header
*mach_hdr
= (struct mach_header
*) ((void *) file
);
644 struct load_command
*load_hdr
= NULL
;
645 struct segment_command
*seg_hdr
= NULL
;
646 struct section
*sects
= NULL
;
647 struct symtab_command
*symtab_hdr
= NULL
;
648 struct nlist
*symtab
= NULL
;
656 if (target_order
== host_order
) return;
658 offset
= sizeof(*mach_hdr
);
659 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= size
) {
660 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
662 size
= load_hdr
->cmdsize
;
666 seg_hdr
= (struct segment_command
*) load_hdr
;
667 sects
= (struct section
*) &seg_hdr
[1];
669 /* We don't need to unswap relocations because this function is
670 * called when linking is completed (so there are no relocations).
673 swap_section(sects
, seg_hdr
->nsects
, target_order
);
674 swap_segment_command(seg_hdr
, target_order
);
677 symtab_hdr
= (struct symtab_command
*) load_hdr
;
678 symtab
= (struct nlist
*) ((void *) (file
+ symtab_hdr
->symoff
));
680 swap_nlist(symtab
, symtab_hdr
->nsyms
, target_order
);
681 swap_symtab_command(symtab_hdr
, target_order
);
685 swap_load_command(load_hdr
, target_order
);
690 (void) swap_mach_header(mach_hdr
, target_order
);
693 /*******************************************************************************
694 *******************************************************************************/
696 unswap_macho_64(u_char
*file
, enum NXByteOrder host_order
,
697 enum NXByteOrder target_order
)
699 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) ((void *) file
);
700 struct load_command
*load_hdr
= NULL
;
701 struct segment_command_64
*seg_hdr
= NULL
;
702 struct section_64
*sects
= NULL
;
703 struct symtab_command
*symtab_hdr
= NULL
;
704 struct nlist_64
*symtab
= NULL
;
712 if (target_order
== host_order
) return;
714 offset
= sizeof(*mach_hdr
);
715 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= size
) {
716 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
718 size
= load_hdr
->cmdsize
;
722 seg_hdr
= (struct segment_command_64
*) ((void *) load_hdr
);
723 sects
= (struct section_64
*) &seg_hdr
[1];
725 /* We don't need to unswap relocations because this function is
726 * called when linking is completed (so there are no relocations).
729 swap_section_64(sects
, seg_hdr
->nsects
, target_order
);
730 swap_segment_command_64(seg_hdr
, target_order
);
733 symtab_hdr
= (struct symtab_command
*) load_hdr
;
734 symtab
= (struct nlist_64
*) ((void *) (file
+ symtab_hdr
->symoff
));
736 swap_nlist_64(symtab
, symtab_hdr
->nsyms
, target_order
);
737 swap_symtab_command(symtab_hdr
, target_order
);
741 swap_load_command(load_hdr
, target_order
);
746 (void) swap_mach_header_64(mach_hdr
, target_order
);
750 /*******************************************************************************
751 *******************************************************************************/
753 kxld_align_address(kxld_addr_t address
, u_int align
)
755 kxld_addr_t alignment
= (1 << align
);
756 kxld_addr_t low_bits
= 0;
758 if (!align
) return address
;
760 low_bits
= (address
) & (alignment
- 1);
762 address
+= (alignment
- low_bits
);
768 /*******************************************************************************
769 *******************************************************************************/
771 kxld_is_32_bit(cpu_type_t cputype
)
773 return !(cputype
& CPU_ARCH_ABI64
);
776 /*******************************************************************************
777 * Borrowed (and slightly modified) the libc implementation for the kernel
778 * until the kernel has a supported strstr().
779 * Find the first occurrence of find in s.
780 *******************************************************************************/
782 kxld_strstr(const char *s
, const char *find
)
788 if ((c
= *find
++) != 0) {
792 if ((sc
= *s
++) == 0)
795 } while (strncmp(s
, find
, len
) != 0);
800 return strstr(s
, find
);
804 /*******************************************************************************
805 *******************************************************************************/
807 kxld_print_memory_report(void)
810 kxld_log(kKxldLogLinking
, kKxldLogExplicit
, "kxld memory usage report:\n"
811 "\tNumber of allocations: %8lu\n"
812 "\tNumber of frees: %8lu\n"
813 "\tAverage allocation size: %8lu\n"
814 "\tTotal bytes allocated: %8lu\n"
815 "\tTotal bytes freed: %8lu\n"
816 "\tTotal bytes leaked: %8lu",
817 num_allocations
, num_frees
, bytes_allocated
/ num_allocations
,
818 bytes_allocated
, bytes_freed
, bytes_allocated
- bytes_freed
);
822 /*********************************************************************
823 *********************************************************************/
825 boolean_t
kxld_set_cross_link_page_size(kxld_size_t target_page_size
)
828 if ((target_page_size
!= 0) &&
829 ((target_page_size
& (target_page_size
- 1)) == 0)) {
831 s_cross_link_enabled
= TRUE
;
832 s_cross_link_page_size
= target_page_size
;
841 /*********************************************************************
842 *********************************************************************/
843 kxld_size_t
kxld_get_effective_page_size(void)
848 if (s_cross_link_enabled
) {
849 return s_cross_link_page_size
;
856 /*********************************************************************
857 *********************************************************************/
858 kxld_addr_t
kxld_round_page_cross_safe(kxld_addr_t offset
)
861 return round_page(offset
);
863 // assume s_cross_link_page_size is power of 2
864 if (s_cross_link_enabled
) {
865 return (offset
+ (s_cross_link_page_size
- 1)) &
866 (~(s_cross_link_page_size
- 1));
868 return round_page(offset
);
873 #if SPLIT_KEXTS_DEBUG
875 void kxld_show_split_info(splitKextLinkInfo
*info
)
877 kxld_log(kKxldLogLinking
, kKxldLogErr
,
878 "splitKextLinkInfo: \n"
879 "kextExecutable %p to %p kextSize %lu \n"
880 "linkedKext %p to %p linkedKextSize %lu \n"
881 "vmaddr_TEXT %p vmaddr_TEXT_EXEC %p "
882 "vmaddr_DATA %p vmaddr_DATA_CONST %p "
883 "vmaddr_LLVM_COV %p vmaddr_LINKEDIT %p",
884 (void *) info
->kextExecutable
,
885 (void *) (info
->kextExecutable
+ info
->kextSize
),
887 (void*) info
->linkedKext
,
888 (void*) (info
->linkedKext
+ info
->linkedKextSize
),
889 info
->linkedKextSize
,
890 (void *) info
->vmaddr_TEXT
,
891 (void *) info
->vmaddr_TEXT_EXEC
,
892 (void *) info
->vmaddr_DATA
,
893 (void *) info
->vmaddr_DATA_CONST
,
894 (void *) info
->vmaddr_LLVM_COV
,
895 (void *) info
->vmaddr_LINKEDIT
);
898 boolean_t
isTargetKextName(const char * the_name
)
900 if (the_name
&& 0 == strcmp(the_name
, KXLD_TARGET_KEXT
)) {