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 /* Can't use PAGE_SIZE here because it is not a compile-time constant.
71 * However from inspection below, s_cross_link_page_size is not used
72 * unless s_cross_link_enabled is TRUE, and s_cross_link_enabled is
73 * only set to TRUE when a client specifies the value. So the
74 * default should never be used in practice,
76 static kxld_size_t s_cross_link_page_size
;
80 /*******************************************************************************
81 *******************************************************************************/
83 kxld_set_logging_callback(KXLDLoggingCallback logging_callback
)
85 s_logging_callback
= logging_callback
;
88 /*******************************************************************************
89 *******************************************************************************/
91 kxld_set_logging_callback_data(const char *name
, void *user_data
)
94 (void)strlcpy(s_callback_name
, name
, sizeof(s_callback_name
));
95 /* disallow format strings in the kxld logging callback name */
96 for (size_t i
= 0; i
< sizeof(s_callback_name
); i
++) {
97 if (s_callback_name
[i
] == '%') {
98 s_callback_name
[i
] = '.';
102 (void)strlcpy(s_callback_name
, "internal", sizeof(s_callback_name
));
105 s_callback_data
= user_data
;
108 /*******************************************************************************
109 *******************************************************************************/
111 kxld_log(KXLDLogSubsystem subsystem
, KXLDLogLevel level
,
112 const char *in_format
, ...)
114 char stack_buffer
[256];
115 char *alloc_buffer
= NULL
;
116 char *format
= stack_buffer
;
120 if (s_logging_callback
) {
121 length
= snprintf(stack_buffer
, sizeof(stack_buffer
), "kxld[%s]: %s",
122 s_callback_name
, in_format
);
124 if (length
>= sizeof(stack_buffer
)) {
126 alloc_buffer
= kxld_alloc(length
);
131 snprintf(alloc_buffer
, length
, "kxld[%s]: %s",
132 s_callback_name
, in_format
);
133 format
= alloc_buffer
;
136 va_start(ap
, in_format
);
137 s_logging_callback(subsystem
, level
, format
, ap
, s_callback_data
);
141 kxld_free(alloc_buffer
, length
);
146 /* We'll use kalloc for any page-based allocations under this threshold, and
147 * kmem_alloc otherwise.
149 #define KALLOC_MAX 16 * 1024
151 /*******************************************************************************
152 *******************************************************************************/
154 kxld_calloc(size_t size
)
164 ptr
= calloc(1, size
);
170 bytes_allocated
+= size
;
178 kxld_alloc(size_t size
)
191 bytes_allocated
+= size
;
198 /*******************************************************************************
199 *******************************************************************************/
201 kxld_page_alloc_untracked(size_t size
)
205 kern_return_t rval
= 0;
206 vm_offset_t addr
= 0;
209 size
= round_page(size
);
212 if (size
< KALLOC_MAX
) {
215 rval
= kmem_alloc(kernel_map
, &addr
, size
, VM_KERN_MEMORY_OSKEXT
);
224 ptr
= calloc(1, size
);
230 /*******************************************************************************
231 *******************************************************************************/
233 kxld_page_alloc(size_t size
)
237 ptr
= kxld_page_alloc_untracked(size
);
241 bytes_allocated
+= round_page(size
);
248 /*******************************************************************************
249 *******************************************************************************/
251 kxld_alloc_pageable(size_t size
)
253 size
= round_page(size
);
256 kern_return_t rval
= 0;
259 rval
= kmem_alloc_pageable(kernel_map
, &ptr
, size
, VM_KERN_MEMORY_OSKEXT
);
266 return kxld_page_alloc_untracked(size
);
270 /*******************************************************************************
271 *******************************************************************************/
273 kxld_free(void *ptr
, size_t size __unused
)
287 /*******************************************************************************
288 *******************************************************************************/
290 kxld_page_free_untracked(void *ptr
, size_t size __unused
)
293 size
= round_page(size
);
295 if (size
< KALLOC_MAX
) {
298 kmem_free(kernel_map
, (vm_offset_t
) ptr
, size
);
306 /*******************************************************************************
307 *******************************************************************************/
309 kxld_page_free(void *ptr
, size_t size
)
313 bytes_freed
+= round_page(size
);
315 kxld_page_free_untracked(ptr
, size
);
318 /*******************************************************************************
319 *******************************************************************************/
321 validate_and_swap_macho_32(u_char
*file
, u_long size
323 , enum NXByteOrder host_order
327 kern_return_t rval
= KERN_FAILURE
;
328 struct mach_header
*mach_hdr
= (struct mach_header
*) ((void *) file
);
329 struct load_command
*load_hdr
= NULL
;
330 struct segment_command
*seg_hdr
= NULL
;
331 struct section
*sects
= NULL
;
332 struct relocation_info
*relocs
= NULL
;
333 struct symtab_command
*symtab_hdr
= NULL
;
334 struct nlist
*symtab
= NULL
;
341 boolean_t swap
= FALSE
;
347 /* Verify that the file is big enough for the mach header */
348 require_action(size
>= sizeof(*mach_hdr
), finish
,
350 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
351 offset
= sizeof(*mach_hdr
);
354 /* Swap the mach header if necessary */
355 if (mach_hdr
->magic
== MH_CIGAM
) {
357 (void) swap_mach_header(mach_hdr
, host_order
);
361 /* Validate the mach_header's magic number */
362 require_action(mach_hdr
->magic
== MH_MAGIC
, finish
,
364 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
365 "Invalid magic number: 0x%x.", mach_hdr
->magic
));
367 /* If in the running kernel, and asked to validate the kernel
368 * (which is the only file of type MH_EXECUTE we should ever see),
369 * then just assume it's ok or we wouldn't be running to begin with.
372 if (mach_hdr
->filetype
== MH_EXECUTE
) {
378 /* Validate and potentially swap the load commands */
379 for (i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= cmdsize
) {
380 /* Get the load command and size */
381 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
383 cmdsize
= load_hdr
->cmdsize
;
387 cmd
= OSSwapInt32(load_hdr
->cmd
);
388 cmdsize
= OSSwapInt32(load_hdr
->cmdsize
);
392 /* Verify that the file is big enough to contain the load command */
393 require_action(size
>= offset
+ cmdsize
, finish
,
395 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
399 /* Get and swap the segment header */
400 seg_hdr
= (struct segment_command
*) load_hdr
;
403 swap_segment_command(seg_hdr
, host_order
);
407 /* Get and swap the section headers */
408 sects
= (struct section
*) &seg_hdr
[1];
411 swap_section(sects
, seg_hdr
->nsects
, host_order
);
415 /* Ignore segments with no vm size */
416 if (!seg_hdr
->vmsize
) {
420 /* Verify that the file is big enough for the segment data. */
421 require_action(size
>= seg_hdr
->fileoff
+ seg_hdr
->filesize
, finish
,
423 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
425 for (j
= 0; j
< seg_hdr
->nsects
; ++j
) {
426 /* Verify that, if the section is not to be zero filled on
427 * demand, that file is big enough for the section's data.
429 require_action((sects
[j
].flags
& S_ZEROFILL
) ||
430 (size
>= sects
[j
].offset
+ sects
[j
].size
), finish
,
432 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
434 /* Verify that the file is big enough for the section's
435 * relocation entries.
437 require_action(size
>=
438 sects
[j
].reloff
+ sects
[j
].nreloc
* sizeof(*relocs
), finish
,
440 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
442 /* Swap the relocation entries */
443 relocs
= (struct relocation_info
*) ((void *) (file
+ sects
[j
].reloff
));
446 swap_relocation_info(relocs
, sects
[j
].nreloc
,
454 /* Get and swap the symtab header */
455 symtab_hdr
= (struct symtab_command
*) load_hdr
;
458 swap_symtab_command(symtab_hdr
, host_order
);
462 /* Verify that the file is big enough for the symbol table */
463 require_action(size
>=
464 symtab_hdr
->symoff
+ symtab_hdr
->nsyms
* sizeof(*symtab
), finish
,
466 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
468 /* Verify that the file is big enough for the string table */
469 require_action(size
>= symtab_hdr
->stroff
+ symtab_hdr
->strsize
, finish
,
471 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
474 /* Swap the symbol table entries */
475 symtab
= (struct nlist
*) ((void *) (file
+ symtab_hdr
->symoff
));
477 swap_nlist(symtab
, symtab_hdr
->nsyms
, host_order
);
484 /* Swap the load command */
486 swap_load_command(load_hdr
, host_order
);
499 /*******************************************************************************
500 *******************************************************************************/
502 validate_and_swap_macho_64(u_char
*file
, u_long size
504 , enum NXByteOrder host_order
508 kern_return_t rval
= KERN_FAILURE
;
509 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) ((void *) file
);
510 struct load_command
*load_hdr
= NULL
;
511 struct segment_command_64
*seg_hdr
= NULL
;
512 struct section_64
*sects
= NULL
;
513 struct relocation_info
*relocs
= NULL
;
514 struct symtab_command
*symtab_hdr
= NULL
;
515 struct nlist_64
*symtab
= NULL
;
522 boolean_t swap
= FALSE
;
528 /* Verify that the file is big enough for the mach header */
529 require_action(size
>= sizeof(*mach_hdr
), finish
,
531 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
532 offset
= sizeof(*mach_hdr
);
535 /* Swap the mach header if necessary */
536 if (mach_hdr
->magic
== MH_CIGAM_64
) {
538 (void) swap_mach_header_64(mach_hdr
, host_order
);
542 /* Validate the mach_header's magic number */
543 require_action(mach_hdr
->magic
== MH_MAGIC_64
, finish
,
545 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
546 "Invalid magic number: 0x%x.", mach_hdr
->magic
));
548 /* If in the running kernel, and asked to validate the kernel
549 * (which is the only file of type MH_EXECUTE we should ever see),
550 * then just assume it's ok or we wouldn't be running to begin with.
553 if (mach_hdr
->filetype
== MH_EXECUTE
) {
559 /* Validate and potentially swap the load commands */
560 for (i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= cmdsize
) {
561 /* Get the load command and size */
562 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
564 cmdsize
= load_hdr
->cmdsize
;
568 cmd
= OSSwapInt32(load_hdr
->cmd
);
569 cmdsize
= OSSwapInt32(load_hdr
->cmdsize
);
573 /* Verify that the file is big enough to contain the load command */
574 require_action(size
>= offset
+ cmdsize
, finish
,
576 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
579 /* Get and swap the segment header */
580 seg_hdr
= (struct segment_command_64
*) ((void *) load_hdr
);
583 swap_segment_command_64(seg_hdr
, host_order
);
587 /* Get and swap the section headers */
588 sects
= (struct section_64
*) &seg_hdr
[1];
591 swap_section_64(sects
, seg_hdr
->nsects
, host_order
);
595 /* If the segment has no vm footprint, skip it */
596 if (!seg_hdr
->vmsize
) {
600 /* Verify that the file is big enough for the segment data. */
601 require_action(size
>= seg_hdr
->fileoff
+ seg_hdr
->filesize
, finish
,
603 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
605 for (j
= 0; j
< seg_hdr
->nsects
; ++j
) {
606 /* Verify that, if the section is not to be zero filled on
607 * demand, that file is big enough for the section's data.
609 require_action((sects
[j
].flags
& S_ZEROFILL
) ||
610 (size
>= sects
[j
].offset
+ sects
[j
].size
), finish
,
612 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
614 /* Verify that the file is big enough for the section's
615 * relocation entries.
617 require_action(size
>=
618 sects
[j
].reloff
+ sects
[j
].nreloc
* sizeof(*relocs
), finish
,
620 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
622 /* Swap the relocation entries */
623 relocs
= (struct relocation_info
*) ((void *) (file
+ sects
[j
].reloff
));
626 swap_relocation_info(relocs
, sects
[j
].nreloc
,
634 /* Get and swap the symtab header */
635 symtab_hdr
= (struct symtab_command
*) load_hdr
;
638 swap_symtab_command(symtab_hdr
, host_order
);
642 /* Verify that the file is big enough for the symbol table */
643 require_action(size
>=
644 symtab_hdr
->symoff
+ symtab_hdr
->nsyms
* sizeof(*symtab
), finish
,
646 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
648 /* Verify that the file is big enough for the string table */
649 require_action(size
>= symtab_hdr
->stroff
+ symtab_hdr
->strsize
, finish
,
651 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
654 /* Swap the symbol table entries */
655 symtab
= (struct nlist_64
*) ((void *) (file
+ symtab_hdr
->symoff
));
657 swap_nlist_64(symtab
, symtab_hdr
->nsyms
, host_order
);
664 /* Swap the load command */
666 swap_load_command(load_hdr
, host_order
);
680 /*******************************************************************************
681 *******************************************************************************/
683 unswap_macho(u_char
*file
, enum NXByteOrder host_order
,
684 enum NXByteOrder target_order
)
686 struct mach_header
*hdr
= (struct mach_header
*) ((void *) file
);
692 if (hdr
->magic
== MH_MAGIC
) {
693 unswap_macho_32(file
, host_order
, target_order
);
694 } else if (hdr
->magic
== MH_MAGIC_64
) {
695 unswap_macho_64(file
, host_order
, target_order
);
699 /*******************************************************************************
700 *******************************************************************************/
702 unswap_macho_32(u_char
*file
, enum NXByteOrder host_order
,
703 enum NXByteOrder target_order
)
705 struct mach_header
*mach_hdr
= (struct mach_header
*) ((void *) file
);
706 struct load_command
*load_hdr
= NULL
;
707 struct segment_command
*seg_hdr
= NULL
;
708 struct section
*sects
= NULL
;
709 struct symtab_command
*symtab_hdr
= NULL
;
710 struct nlist
*symtab
= NULL
;
718 if (target_order
== host_order
) {
722 offset
= sizeof(*mach_hdr
);
723 for (i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= size
) {
724 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
726 size
= load_hdr
->cmdsize
;
730 seg_hdr
= (struct segment_command
*) load_hdr
;
731 sects
= (struct section
*) &seg_hdr
[1];
733 /* We don't need to unswap relocations because this function is
734 * called when linking is completed (so there are no relocations).
737 swap_section(sects
, seg_hdr
->nsects
, target_order
);
738 swap_segment_command(seg_hdr
, target_order
);
741 symtab_hdr
= (struct symtab_command
*) load_hdr
;
742 symtab
= (struct nlist
*) ((void *) (file
+ symtab_hdr
->symoff
));
744 swap_nlist(symtab
, symtab_hdr
->nsyms
, target_order
);
745 swap_symtab_command(symtab_hdr
, target_order
);
749 swap_load_command(load_hdr
, target_order
);
754 (void) swap_mach_header(mach_hdr
, target_order
);
757 /*******************************************************************************
758 *******************************************************************************/
760 unswap_macho_64(u_char
*file
, enum NXByteOrder host_order
,
761 enum NXByteOrder target_order
)
763 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) ((void *) file
);
764 struct load_command
*load_hdr
= NULL
;
765 struct segment_command_64
*seg_hdr
= NULL
;
766 struct section_64
*sects
= NULL
;
767 struct symtab_command
*symtab_hdr
= NULL
;
768 struct nlist_64
*symtab
= NULL
;
776 if (target_order
== host_order
) {
780 offset
= sizeof(*mach_hdr
);
781 for (i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= size
) {
782 load_hdr
= (struct load_command
*) ((void *) (file
+ offset
));
784 size
= load_hdr
->cmdsize
;
788 seg_hdr
= (struct segment_command_64
*) ((void *) load_hdr
);
789 sects
= (struct section_64
*) &seg_hdr
[1];
791 /* We don't need to unswap relocations because this function is
792 * called when linking is completed (so there are no relocations).
795 swap_section_64(sects
, seg_hdr
->nsects
, target_order
);
796 swap_segment_command_64(seg_hdr
, target_order
);
799 symtab_hdr
= (struct symtab_command
*) load_hdr
;
800 symtab
= (struct nlist_64
*) ((void *) (file
+ symtab_hdr
->symoff
));
802 swap_nlist_64(symtab
, symtab_hdr
->nsyms
, target_order
);
803 swap_symtab_command(symtab_hdr
, target_order
);
807 swap_load_command(load_hdr
, target_order
);
812 (void) swap_mach_header_64(mach_hdr
, target_order
);
816 /*******************************************************************************
817 *******************************************************************************/
819 kxld_align_address(kxld_addr_t address
, u_int align
)
821 kxld_addr_t alignment
= (1 << align
);
822 kxld_addr_t low_bits
= 0;
828 low_bits
= (address
) & (alignment
- 1);
830 address
+= (alignment
- low_bits
);
836 /*******************************************************************************
837 *******************************************************************************/
839 kxld_is_32_bit(cpu_type_t cputype
)
841 return !(cputype
& CPU_ARCH_ABI64
);
844 /*******************************************************************************
845 *******************************************************************************/
847 kxld_print_memory_report(void)
850 kxld_log(kKxldLogLinking
, kKxldLogExplicit
, "kxld memory usage report:\n"
851 "\tNumber of allocations: %8lu\n"
852 "\tNumber of frees: %8lu\n"
853 "\tAverage allocation size: %8lu\n"
854 "\tTotal bytes allocated: %8lu\n"
855 "\tTotal bytes freed: %8lu\n"
856 "\tTotal bytes leaked: %8lu",
857 num_allocations
, num_frees
, bytes_allocated
/ num_allocations
,
858 bytes_allocated
, bytes_freed
, bytes_allocated
- bytes_freed
);
862 /*********************************************************************
863 *********************************************************************/
866 kxld_set_cross_link_page_size(kxld_size_t target_page_size
)
869 if ((target_page_size
!= 0) &&
870 ((target_page_size
& (target_page_size
- 1)) == 0)) {
871 s_cross_link_enabled
= TRUE
;
872 s_cross_link_page_size
= target_page_size
;
881 /*********************************************************************
882 *********************************************************************/
884 kxld_get_effective_page_size(void)
889 if (s_cross_link_enabled
) {
890 return s_cross_link_page_size
;
897 /*********************************************************************
898 *********************************************************************/
900 kxld_round_page_cross_safe(kxld_addr_t offset
)
903 return round_page(offset
);
905 // assume s_cross_link_page_size is power of 2
906 if (s_cross_link_enabled
) {
907 return (offset
+ (s_cross_link_page_size
- 1)) &
908 (~(s_cross_link_page_size
- 1));
910 return round_page(offset
);
915 #if SPLIT_KEXTS_DEBUG
918 kxld_show_split_info(splitKextLinkInfo
*info
)
920 kxld_log(kKxldLogLinking
, kKxldLogErr
,
921 "splitKextLinkInfo: \n"
922 "kextExecutable %p to %p kextSize %lu \n"
923 "linkedKext %p to %p linkedKextSize %lu \n"
924 "vmaddr_TEXT %p vmaddr_TEXT_EXEC %p "
925 "vmaddr_DATA %p vmaddr_DATA_CONST %p "
926 "vmaddr_LLVM_COV %p vmaddr_LINKEDIT %p",
927 (void *) info
->kextExecutable
,
928 (void *) (info
->kextExecutable
+ info
->kextSize
),
930 (void*) info
->linkedKext
,
931 (void*) (info
->linkedKext
+ info
->linkedKextSize
),
932 info
->linkedKextSize
,
933 (void *) info
->vmaddr_TEXT
,
934 (void *) info
->vmaddr_TEXT_EXEC
,
935 (void *) info
->vmaddr_DATA
,
936 (void *) info
->vmaddr_DATA_CONST
,
937 (void *) info
->vmaddr_LLVM_COV
,
938 (void *) info
->vmaddr_LINKEDIT
);
942 isTargetKextName(const char * the_name
)
944 if (the_name
&& 0 == strcmp(the_name
, KXLD_TARGET_KEXT
)) {