2 * Copyright (c) 2007-2008 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 const char *s_callback_name
= NULL
;
66 static void *s_callback_data
= NULL
;
68 /*******************************************************************************
69 *******************************************************************************/
71 kxld_set_logging_callback(KXLDLoggingCallback logging_callback
)
73 s_logging_callback
= logging_callback
;
76 /*******************************************************************************
77 *******************************************************************************/
79 kxld_set_logging_callback_data(const char *name
, void *user_data
)
81 s_callback_name
= name
;
82 s_callback_data
= user_data
;
85 /*******************************************************************************
86 *******************************************************************************/
88 kxld_log(KXLDLogSubsystem subsystem
, KXLDLogLevel level
,
89 const char *in_format
, ...)
91 char stack_buffer
[256];
92 char *alloc_buffer
= NULL
;
93 char *format
= stack_buffer
;
94 const char *name
= (s_callback_name
) ? s_callback_name
: "internal";
98 if (s_logging_callback
) {
100 length
= snprintf(stack_buffer
, sizeof(stack_buffer
), "kxld[%s]: %s",
103 if (length
>= sizeof(stack_buffer
)) {
105 alloc_buffer
= kxld_alloc(length
);
106 if (!alloc_buffer
) return;
108 snprintf(alloc_buffer
, sizeof(alloc_buffer
), "kxld[%s]: %s",
110 format
= alloc_buffer
;
113 va_start(ap
, in_format
);
114 s_logging_callback(subsystem
, level
, format
, ap
, s_callback_data
);
118 kxld_free(alloc_buffer
, length
);
123 /* We'll use kalloc for any page-based allocations under this threshold, and
124 * kmem_alloc otherwise.
126 #define KALLOC_MAX 16 * 1024
128 /*******************************************************************************
129 *******************************************************************************/
131 kxld_alloc(size_t size
)
144 bytes_allocated
+= size
;
151 /*******************************************************************************
152 *******************************************************************************/
154 kxld_page_alloc_untracked(size_t size
)
158 kern_return_t rval
= 0;
159 vm_offset_t addr
= 0;
162 size
= round_page(size
);
165 if (size
< KALLOC_MAX
) {
168 rval
= kmem_alloc(kernel_map
, &addr
, size
);
169 if (!rval
) ptr
= (void *) addr
;
178 /*******************************************************************************
179 *******************************************************************************/
181 kxld_page_alloc(size_t size
)
185 ptr
= kxld_page_alloc_untracked(size
);
189 bytes_allocated
+= round_page(size
);
196 /*******************************************************************************
197 *******************************************************************************/
199 kxld_alloc_pageable(size_t size
)
201 size
= round_page(size
);
204 kern_return_t rval
= 0;
207 rval
= kmem_alloc_pageable(kernel_map
, &ptr
, size
);
212 return kxld_page_alloc_untracked(size
);
216 /*******************************************************************************
217 *******************************************************************************/
219 kxld_free(void *ptr
, size_t size __unused
)
233 /*******************************************************************************
234 *******************************************************************************/
236 kxld_page_free_untracked(void *ptr
, size_t size __unused
)
239 size
= round_page(size
);
241 if (size
< KALLOC_MAX
) {
244 kmem_free(kernel_map
, (vm_offset_t
) ptr
, size
);
252 /*******************************************************************************
253 *******************************************************************************/
255 kxld_page_free(void *ptr
, size_t size
)
259 bytes_freed
+= round_page(size
);
261 kxld_page_free_untracked(ptr
, size
);
264 /*******************************************************************************
265 *******************************************************************************/
267 validate_and_swap_macho_32(u_char
*file
, u_long size
269 , enum NXByteOrder host_order
273 kern_return_t rval
= KERN_FAILURE
;
274 struct mach_header
*mach_hdr
= (struct mach_header
*) file
;
275 struct load_command
*load_hdr
= NULL
;
276 struct segment_command
*seg_hdr
= NULL
;
277 struct section
*sects
= NULL
;
278 struct relocation_info
*relocs
= NULL
;
279 struct symtab_command
*symtab_hdr
= NULL
;
280 struct nlist
*symtab
= NULL
;
287 boolean_t swap
= FALSE
;
293 /* Verify that the file is big enough for the mach header */
294 require_action(size
>= sizeof(*mach_hdr
), finish
,
296 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
297 offset
= sizeof(*mach_hdr
);
300 /* Swap the mach header if necessary */
301 if (mach_hdr
->magic
== MH_CIGAM
) {
303 (void) swap_mach_header(mach_hdr
, host_order
);
307 /* Validate the mach_header's magic number */
308 require_action(mach_hdr
->magic
== MH_MAGIC
, finish
,
310 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
311 "Invalid magic number: 0x%x.", mach_hdr
->magic
));
313 /* Validate and potentially swap the load commands */
314 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= cmdsize
) {
316 /* Get the load command and size */
317 load_hdr
= (struct load_command
*) (file
+ offset
);
319 cmdsize
= load_hdr
->cmdsize
;
323 cmd
= OSSwapInt32(load_hdr
->cmd
);
324 cmdsize
= OSSwapInt32(load_hdr
->cmdsize
);
328 /* Verify that the file is big enough to contain the load command */
329 require_action(size
>= offset
+ cmdsize
, finish
,
331 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
335 /* Get and swap the segment header */
336 seg_hdr
= (struct segment_command
*) load_hdr
;
338 if (swap
) swap_segment_command(seg_hdr
, host_order
);
341 /* Get and swap the section headers */
342 sects
= (struct section
*) &seg_hdr
[1];
344 if (swap
) swap_section(sects
, seg_hdr
->nsects
, host_order
);
347 /* Ignore segments with no vm size */
348 if (!seg_hdr
->vmsize
) continue;
350 /* Verify that the file is big enough for the segment data. */
351 require_action(size
>= seg_hdr
->fileoff
+ seg_hdr
->filesize
, finish
,
353 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
355 for (j
= 0; j
< seg_hdr
->nsects
; ++j
) {
357 /* Verify that, if the section is not to be zero filled on
358 * demand, that file is big enough for the section's data.
360 require_action((sects
[j
].flags
& S_ZEROFILL
) ||
361 (size
>= sects
[j
].offset
+ sects
[j
].size
), finish
,
363 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
365 /* Verify that the file is big enough for the section's
366 * relocation entries.
368 require_action(size
>=
369 sects
[j
].reloff
+ sects
[j
].nreloc
* sizeof(*relocs
), finish
,
371 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
373 /* Swap the relocation entries */
374 relocs
= (struct relocation_info
*) (file
+ sects
[j
].reloff
);
377 swap_relocation_info(relocs
, sects
[j
].nreloc
,
385 /* Get and swap the symtab header */
386 symtab_hdr
= (struct symtab_command
*) load_hdr
;
388 if (swap
) swap_symtab_command(symtab_hdr
, host_order
);
391 /* Verify that the file is big enough for the symbol table */
392 require_action(size
>=
393 symtab_hdr
->symoff
+ symtab_hdr
->nsyms
* sizeof(*symtab
), finish
,
395 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
397 /* Verify that the file is big enough for the string table */
398 require_action(size
>= symtab_hdr
->stroff
+ symtab_hdr
->strsize
, finish
,
400 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
403 /* Swap the symbol table entries */
404 symtab
= (struct nlist
*) (file
+ symtab_hdr
->symoff
);
405 if (swap
) swap_nlist(symtab
, symtab_hdr
->nsyms
, host_order
);
411 /* Swap the load command */
412 if (swap
) swap_load_command(load_hdr
, host_order
);
424 /*******************************************************************************
425 *******************************************************************************/
427 validate_and_swap_macho_64(u_char
*file
, u_long size
429 , enum NXByteOrder host_order
433 kern_return_t rval
= KERN_FAILURE
;
434 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) file
;
435 struct load_command
*load_hdr
= NULL
;
436 struct segment_command_64
*seg_hdr
= NULL
;
437 struct section_64
*sects
= NULL
;
438 struct relocation_info
*relocs
= NULL
;
439 struct symtab_command
*symtab_hdr
= NULL
;
440 struct nlist_64
*symtab
= NULL
;
447 boolean_t swap
= FALSE
;
453 /* Verify that the file is big enough for the mach header */
454 require_action(size
>= sizeof(*mach_hdr
), finish
,
456 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
457 offset
= sizeof(*mach_hdr
);
460 /* Swap the mach header if necessary */
461 if (mach_hdr
->magic
== MH_CIGAM_64
) {
463 (void) swap_mach_header_64(mach_hdr
, host_order
);
467 /* Validate the mach_header's magic number */
468 require_action(mach_hdr
->magic
== MH_MAGIC_64
, finish
,
470 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogMalformedMachO
471 "Invalid magic number: 0x%x.", mach_hdr
->magic
));
473 /* Validate and potentially swap the load commands */
474 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= cmdsize
) {
475 /* Get the load command and size */
476 load_hdr
= (struct load_command
*) (file
+ offset
);
478 cmdsize
= load_hdr
->cmdsize
;
482 cmd
= OSSwapInt32(load_hdr
->cmd
);
483 cmdsize
= OSSwapInt32(load_hdr
->cmdsize
);
487 /* Verify that the file is big enough to contain the load command */
488 require_action(size
>= offset
+ cmdsize
, finish
,
490 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
493 /* Get and swap the segment header */
494 seg_hdr
= (struct segment_command_64
*) load_hdr
;
496 if (swap
) swap_segment_command_64(seg_hdr
, host_order
);
499 /* Get and swap the section headers */
500 sects
= (struct section_64
*) &seg_hdr
[1];
502 if (swap
) swap_section_64(sects
, seg_hdr
->nsects
, host_order
);
505 /* If the segment has no vm footprint, skip it */
506 if (!seg_hdr
->vmsize
) continue;
508 /* Verify that the file is big enough for the segment data. */
509 require_action(size
>= seg_hdr
->fileoff
+ seg_hdr
->filesize
, finish
,
511 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
513 for (j
= 0; j
< seg_hdr
->nsects
; ++j
) {
515 /* Verify that, if the section is not to be zero filled on
516 * demand, that file is big enough for the section's data.
518 require_action((sects
[j
].flags
& S_ZEROFILL
) ||
519 (size
>= sects
[j
].offset
+ sects
[j
].size
), finish
,
521 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
523 /* Verify that the file is big enough for the section's
524 * relocation entries.
526 require_action(size
>=
527 sects
[j
].reloff
+ sects
[j
].nreloc
* sizeof(*relocs
), finish
,
529 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
531 /* Swap the relocation entries */
532 relocs
= (struct relocation_info
*) (file
+ sects
[j
].reloff
);
535 swap_relocation_info(relocs
, sects
[j
].nreloc
,
543 /* Get and swap the symtab header */
544 symtab_hdr
= (struct symtab_command
*) load_hdr
;
546 if (swap
) swap_symtab_command(symtab_hdr
, host_order
);
549 /* Verify that the file is big enough for the symbol table */
550 require_action(size
>=
551 symtab_hdr
->symoff
+ symtab_hdr
->nsyms
* sizeof(*symtab
), finish
,
553 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
555 /* Verify that the file is big enough for the string table */
556 require_action(size
>= symtab_hdr
->stroff
+ symtab_hdr
->strsize
, finish
,
558 kxld_log(kKxldLogLinking
, kKxldLogErr
, kKxldLogTruncatedMachO
));
561 /* Swap the symbol table entries */
562 symtab
= (struct nlist_64
*) (file
+ symtab_hdr
->symoff
);
563 if (swap
) swap_nlist_64(symtab
, symtab_hdr
->nsyms
, host_order
);
569 /* Swap the load command */
570 if (swap
) swap_load_command(load_hdr
, host_order
);
583 /*******************************************************************************
584 *******************************************************************************/
585 void unswap_macho(u_char
*file
, enum NXByteOrder host_order
,
586 enum NXByteOrder target_order
)
588 struct mach_header
*hdr
= (struct mach_header
*) file
;
592 if (hdr
->magic
== MH_MAGIC
) {
593 unswap_macho_32(file
, host_order
, target_order
);
594 } else if (hdr
->magic
== MH_MAGIC_64
) {
595 unswap_macho_64(file
, host_order
, target_order
);
599 /*******************************************************************************
600 *******************************************************************************/
602 unswap_macho_32(u_char
*file
, enum NXByteOrder host_order
,
603 enum NXByteOrder target_order
)
605 struct mach_header
*mach_hdr
= (struct mach_header
*) file
;
606 struct load_command
*load_hdr
= NULL
;
607 struct segment_command
*seg_hdr
= NULL
;
608 struct section
*sects
= NULL
;
609 struct symtab_command
*symtab_hdr
= NULL
;
610 struct nlist
*symtab
= NULL
;
618 if (target_order
== host_order
) return;
620 offset
= sizeof(*mach_hdr
);
621 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= size
) {
622 load_hdr
= (struct load_command
*) (file
+ offset
);
624 size
= load_hdr
->cmdsize
;
628 seg_hdr
= (struct segment_command
*) load_hdr
;
629 sects
= (struct section
*) &seg_hdr
[1];
631 /* We don't need to unswap relocations because this function is
632 * called when linking is completed (so there are no relocations).
635 swap_section(sects
, seg_hdr
->nsects
, target_order
);
636 swap_segment_command(seg_hdr
, target_order
);
639 symtab_hdr
= (struct symtab_command
*) load_hdr
;
640 symtab
= (struct nlist
*) (file
+ symtab_hdr
->symoff
);
642 swap_nlist(symtab
, symtab_hdr
->nsyms
, target_order
);
643 swap_symtab_command(symtab_hdr
, target_order
);
647 swap_load_command(load_hdr
, target_order
);
652 (void) swap_mach_header(mach_hdr
, target_order
);
655 /*******************************************************************************
656 *******************************************************************************/
658 unswap_macho_64(u_char
*file
, enum NXByteOrder host_order
,
659 enum NXByteOrder target_order
)
661 struct mach_header_64
*mach_hdr
= (struct mach_header_64
*) file
;
662 struct load_command
*load_hdr
= NULL
;
663 struct segment_command_64
*seg_hdr
= NULL
;
664 struct section_64
*sects
= NULL
;
665 struct symtab_command
*symtab_hdr
= NULL
;
666 struct nlist_64
*symtab
= NULL
;
674 if (target_order
== host_order
) return;
676 offset
= sizeof(*mach_hdr
);
677 for(i
= 0; i
< mach_hdr
->ncmds
; ++i
, offset
+= size
) {
678 load_hdr
= (struct load_command
*) (file
+ offset
);
680 size
= load_hdr
->cmdsize
;
684 seg_hdr
= (struct segment_command_64
*) load_hdr
;
685 sects
= (struct section_64
*) &seg_hdr
[1];
687 /* We don't need to unswap relocations because this function is
688 * called when linking is completed (so there are no relocations).
691 swap_section_64(sects
, seg_hdr
->nsects
, target_order
);
692 swap_segment_command_64(seg_hdr
, target_order
);
695 symtab_hdr
= (struct symtab_command
*) load_hdr
;
696 symtab
= (struct nlist_64
*) (file
+ symtab_hdr
->symoff
);
698 swap_nlist_64(symtab
, symtab_hdr
->nsyms
, target_order
);
699 swap_symtab_command(symtab_hdr
, target_order
);
703 swap_load_command(load_hdr
, target_order
);
708 (void) swap_mach_header_64(mach_hdr
, target_order
);
712 /*******************************************************************************
713 *******************************************************************************/
715 kxld_align_address(kxld_addr_t address
, u_int align
)
717 kxld_addr_t alignment
= (1 << align
);
718 kxld_addr_t low_bits
= 0;
720 low_bits
= (address
) & (alignment
- 1);
722 address
+= (alignment
- low_bits
);
728 /*******************************************************************************
729 *******************************************************************************/
731 kxld_is_32_bit(cpu_type_t cputype
)
733 return !(cputype
& CPU_ARCH_ABI64
);
736 /*******************************************************************************
737 * Borrowed (and slightly modified) the libc implementation for the kernel
738 * until the kernel has a supported strstr().
739 * Find the first occurrence of find in s.
740 *******************************************************************************/
743 const char *s
, *find
;
749 if ((c
= *find
++) != 0) {
753 if ((sc
= *s
++) == 0)
756 } while (strncmp(s
, find
, len
) != 0);
761 return strstr(s
, find
);
765 /*******************************************************************************
766 *******************************************************************************/
768 kxld_print_memory_report(void)
771 kxld_log(kKxldLogLinking
, kKxldLogExplicit
, "kxld memory usage report:\n"
772 "\tNumber of allocations: %8lu\n"
773 "\tNumber of frees: %8lu\n"
774 "\tAverage allocation size: %8lu\n"
775 "\tTotal bytes allocated: %8lu\n"
776 "\tTotal bytes freed: %8lu\n"
777 "\tTotal bytes leaked: %8lu",
778 num_allocations
, num_frees
, bytes_allocated
/ num_allocations
,
779 bytes_allocated
, bytes_freed
, bytes_allocated
- bytes_freed
);