2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (C) 1988, 1989, NeXT, Inc.
26 * File: kern/mach_loader.c
27 * Author: Avadis Tevanian, Jr.
29 * Mach object file loader (kernel version, for now).
31 * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
35 #include <sys/param.h>
36 #include <sys/vnode_internal.h>
38 #include <sys/namei.h>
39 #include <sys/proc_internal.h>
40 #include <sys/kauth.h>
42 #include <sys/malloc.h>
43 #include <sys/mount_internal.h>
44 #include <sys/fcntl.h>
45 #include <sys/ubc_internal.h>
46 #include <sys/imgact.h>
48 #include <mach/mach_types.h>
49 #include <mach/vm_map.h> /* vm_allocate() */
50 #include <mach/mach_vm.h> /* mach_vm_allocate() */
51 #include <mach/vm_statistics.h>
52 #include <mach/shared_memory_server.h>
53 #include <mach/task.h>
54 #include <mach/thread_act.h>
56 #include <machine/vmparam.h>
58 #include <kern/kern_types.h>
59 #include <kern/cpu_number.h>
60 #include <kern/mach_loader.h>
61 #include <kern/kalloc.h>
62 #include <kern/task.h>
63 #include <kern/thread.h>
65 #include <mach-o/fat.h>
66 #include <mach-o/loader.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_pager.h>
72 #include <vm/vnode_pager.h>
73 #include <vm/vm_shared_memory_server.h>
74 #include <vm/vm_protos.h>
77 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
78 * when KERNEL is defined.
80 extern pmap_t
pmap_create(vm_map_size_t size
);
81 extern void pmap_switch(pmap_t
);
82 extern void pmap_map_sharedpage(task_t task
, pmap_t pmap
);
85 * XXX kern/thread.h should not treat these prototypes as MACH_KERNEL_PRIVATE
86 * when KERNEL is defined.
88 extern kern_return_t
thread_setstatus(thread_t thread
, int flavor
,
89 thread_state_t tstate
,
90 mach_msg_type_number_t count
);
92 extern kern_return_t
thread_state_initialize(thread_t thread
);
95 /* XXX should have prototypes in a shared header file */
96 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
97 extern int get_map_nentries(vm_map_t
);
98 extern kern_return_t
thread_userstack(thread_t
, int, thread_state_t
,
99 unsigned int, mach_vm_offset_t
*, int *);
100 extern kern_return_t
thread_entrypoint(thread_t
, int, thread_state_t
,
101 unsigned int, mach_vm_offset_t
*);
104 /* An empty load_result_t */
105 static load_result_t load_result_null
= {
116 * Prototypes of static functions.
123 struct mach_header
*header
,
126 boolean_t shared_regions
,
127 boolean_t clean_regions
,
129 load_result_t
*result
134 struct segment_command
*scp
,
140 load_result_t
*result
145 struct segment_command_64
*scp64
,
151 load_result_t
*result
156 struct thread_command
*tcp
,
158 load_result_t
*result
163 struct thread_command
*tcp
,
165 load_result_t
*result
172 unsigned long total_size
179 unsigned long total_size
,
180 mach_vm_offset_t
*user_stack
,
188 unsigned long total_size
,
189 mach_vm_offset_t
*entry_point
194 struct dylinker_command
*lcp
,
199 load_result_t
*result
,
200 boolean_t clean_regions
207 struct mach_header
*mach_header
,
215 struct image_params
*imgp
,
216 struct mach_header
*header
,
219 boolean_t clean_regions
,
220 load_result_t
*result
223 struct vnode
*vp
= imgp
->ip_vp
;
224 off_t file_offset
= imgp
->ip_arch_offset
;
225 off_t macho_size
= imgp
->ip_arch_size
;
227 pmap_t pmap
= 0; /* protected by create_map */
230 load_result_t myresult
;
232 boolean_t create_map
= TRUE
;
234 if (new_map
!= VM_MAP_NULL
) {
239 old_map
= current_map();
241 pmap
= get_task_pmap(current_task());
242 pmap_reference(pmap
);
244 pmap
= pmap_create((vm_map_size_t
) 0);
246 map
= vm_map_create(pmap
,
247 get_map_min(old_map
),
248 get_map_max(old_map
),
249 TRUE
); /**** FIXME ****/
256 *result
= load_result_null
;
258 lret
= parse_machfile(vp
, map
, thr_act
, header
, file_offset
, macho_size
,
259 ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0), /* shared regions? */
260 clean_regions
, 0, result
);
262 if (lret
!= LOAD_SUCCESS
) {
264 vm_map_deallocate(map
); /* will lose pmap reference too */
270 * Commit to new map. First make sure that the current
271 * users of the task get done with it, and that we clean
272 * up the old contents of IPC and memory. The task is
273 * guaranteed to be single threaded upon return (us).
275 * Swap the new map for the old, which consumes our new map
276 * reference but each leaves us responsible for the old_map reference.
277 * That lets us get off the pmap associated with it, and
278 * then we can release it.
281 task_halt(current_task());
283 old_map
= swap_task_map(current_task(), map
);
285 pmap_switch(pmap
); /* Make sure we are using the new pmap */
287 vm_map_deallocate(old_map
);
289 return(LOAD_SUCCESS
);
295 * The file size of a mach-o file is limited to 32 bits; this is because
296 * this is the limit on the kalloc() of enough bytes for a mach_header and
297 * the contents of its sizeofcmds, which is currently constrained to 32
298 * bits in the file format itself. We read into the kernel buffer the
299 * commands section, and then parse it in order to parse the mach-o file
300 * format load_command segment(s). We are only interested in a subset of
301 * the total set of possible commands.
309 struct mach_header
*header
,
312 boolean_t shared_regions
,
313 boolean_t clean_regions
,
315 load_result_t
*result
319 struct load_command
*lcp
;
320 struct dylinker_command
*dlp
= 0;
321 integer_t dlarchbits
= 0;
323 load_return_t ret
= LOAD_SUCCESS
;
326 vm_size_t size
,kl_size
;
328 size_t oldoffset
; /* for overflow check */
330 struct proc
*p
= current_proc(); /* XXXX */
334 size_t mach_header_sz
= sizeof(struct mach_header
);
337 if (header
->magic
== MH_MAGIC_64
||
338 header
->magic
== MH_CIGAM_64
) {
339 mach_header_sz
= sizeof(struct mach_header_64
);
343 * Break infinite recursion
346 return(LOAD_FAILURE
);
348 task
= (task_t
)get_threadtask(thr_act
);
353 * Check to see if right machine type.
355 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != cpu_type()) ||
356 !grade_binary(header
->cputype
, header
->cpusubtype
))
357 return(LOAD_BADARCH
);
359 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
361 switch (header
->filetype
) {
367 return (LOAD_FAILURE
);
373 return (LOAD_FAILURE
);
378 return (LOAD_FAILURE
);
382 return (LOAD_FAILURE
);
386 * Get the pager for the file.
388 UBCINFOCHECK("parse_machfile", vp
);
389 pager
= (void *) ubc_getpager(vp
);
392 * Map portion that must be accessible directly into
395 if ((mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
396 return(LOAD_BADMACHO
);
399 * Round size of Mach-O commands up to page boundry.
401 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
403 return(LOAD_BADMACHO
);
406 * Map the load commands into kernel memory.
410 kl_addr
= kalloc(size
);
411 addr
= (caddr_t
)kl_addr
;
413 return(LOAD_NOSPACE
);
415 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
416 UIO_SYSSPACE32
, 0, kauth_cred_get(), &resid
, p
);
419 kfree(kl_addr
, kl_size
);
420 return(LOAD_IOERROR
);
422 /* (void)ubc_map(vp, PROT_EXEC); */ /* NOT HERE */
425 * Scan through the commands, processing each one as necessary.
427 for (pass
= 1; pass
<= 2; pass
++) {
429 * Loop through each of the load_commands indicated by the
430 * Mach-O header; if an absurd value is provided, we just
431 * run off the end of the reserved section by incrementing
432 * the offset too far, so we are implicitly fail-safe.
434 offset
= mach_header_sz
;
435 ncmds
= header
->ncmds
;
438 * Get a pointer to the command.
440 lcp
= (struct load_command
*)(addr
+ offset
);
442 offset
+= lcp
->cmdsize
;
445 * Perform prevalidation of the struct load_command
446 * before we attempt to use its contents. Invalid
447 * values are ones which result in an overflow, or
448 * which can not possibly be valid commands, or which
449 * straddle or exist past the reserved section at the
450 * start of the image.
452 if (oldoffset
> offset
||
453 lcp
->cmdsize
< sizeof(struct load_command
) ||
454 offset
> header
->sizeofcmds
+ mach_header_sz
) {
460 * Act on struct load_command's for which kernel
461 * intervention is required.
467 ret
= load_segment_64(
468 (struct segment_command_64
*)lcp
,
480 (struct segment_command
*) lcp
,
491 ret
= load_thread((struct thread_command
*)lcp
,
498 ret
= load_unixthread(
499 (struct thread_command
*) lcp
,
503 case LC_LOAD_DYLINKER
:
506 if ((depth
== 1) && (dlp
== 0)) {
507 dlp
= (struct dylinker_command
*)lcp
;
508 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
514 /* Other commands are ignored by the kernel */
518 if (ret
!= LOAD_SUCCESS
)
521 if (ret
!= LOAD_SUCCESS
)
524 if (ret
== LOAD_SUCCESS
) {
526 if (shared_regions
) {
528 shared_region_mapping_t shared_region
;
529 struct shared_region_task_mappings map_info
;
530 shared_region_mapping_t next
;
533 vm_get_shared_region(task
, &shared_region
);
534 map_info
.self
= (vm_offset_t
)shared_region
;
535 shared_region_mapping_info(shared_region
,
536 &(map_info
.text_region
),
537 &(map_info
.text_size
),
538 &(map_info
.data_region
),
539 &(map_info
.data_size
),
540 &(map_info
.region_mappings
),
541 &(map_info
.client_base
),
542 &(map_info
.alternate_base
),
543 &(map_info
.alternate_next
),
546 &(map_info
.flags
), &next
);
548 if((map_info
.flags
& SHARED_REGION_FULL
) ||
549 (map_info
.flags
& SHARED_REGION_STALE
)) {
550 shared_region_mapping_t system_region
;
551 system_region
= lookup_default_shared_region(
552 map_info
.fs_base
, map_info
.system
);
553 if((map_info
.self
!= (vm_offset_t
)system_region
) &&
554 (map_info
.flags
& SHARED_REGION_SYSTEM
)) {
555 if(system_region
== NULL
) {
556 shared_file_boot_time_init(
557 map_info
.fs_base
, map_info
.system
);
559 vm_set_shared_region(task
, system_region
);
561 shared_region_mapping_dealloc(
562 (shared_region_mapping_t
)map_info
.self
);
564 } else if (map_info
.flags
& SHARED_REGION_SYSTEM
) {
565 shared_region_mapping_dealloc(system_region
);
566 shared_file_boot_time_init(
567 map_info
.fs_base
, map_info
.system
);
568 shared_region_mapping_dealloc(
569 (shared_region_mapping_t
)map_info
.self
);
571 shared_region_mapping_dealloc(system_region
);
576 p
->p_flag
|= P_NOSHLIB
; /* no shlibs in use */
577 vmaddr
= map_info
.client_base
;
579 vm_map(map
, &vmaddr
, map_info
.text_size
,
580 0, SHARED_LIB_ALIAS
|VM_FLAGS_FIXED
,
581 map_info
.text_region
, 0, FALSE
,
582 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
584 vm_map(map
, &vmaddr
, map_info
.text_size
, 0,
585 (VM_MEMORY_SHARED_PMAP
<< 24)
586 | SHARED_LIB_ALIAS
| VM_FLAGS_FIXED
,
587 map_info
.text_region
, 0, FALSE
,
588 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
590 vmaddr
= map_info
.client_base
+ map_info
.text_size
;
591 vm_map(map
, &vmaddr
, map_info
.data_size
,
592 0, SHARED_LIB_ALIAS
| VM_FLAGS_FIXED
,
593 map_info
.data_region
, 0, TRUE
,
594 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
597 /* this should be fleshed out for the general case */
598 /* but this is not necessary for now. Indeed we */
599 /* are handling the com page inside of the */
600 /* shared_region mapping create calls for now for */
601 /* simplicities sake. If more general support is */
602 /* needed the code to manipulate the shared range */
603 /* chain can be pulled out and moved to the callers*/
604 shared_region_mapping_info(next
,
605 &(map_info
.text_region
),
606 &(map_info
.text_size
),
607 &(map_info
.data_region
),
608 &(map_info
.data_size
),
609 &(map_info
.region_mappings
),
610 &(map_info
.client_base
),
611 &(map_info
.alternate_base
),
612 &(map_info
.alternate_next
),
615 &(map_info
.flags
), &next
);
617 vmaddr
= map_info
.client_base
;
618 vm_map(map
, &vmaddr
, map_info
.text_size
,
619 0, SHARED_LIB_ALIAS
| VM_FLAGS_FIXED
,
620 map_info
.text_region
, 0, FALSE
,
621 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
626 ret
= load_dylinker(dlp
, dlarchbits
, map
, thr_act
, depth
, result
, clean_regions
);
629 if (result
->thread_count
== 0)
633 /* Map in 64-bit commpage */
634 /* LP64todo - make this clean */
635 pmap_map_sharedpage(current_task(), get_map_pmap(map
));
636 vm_map_commpage64(map
);
643 kfree(kl_addr
, kl_size
);
645 if (ret
== LOAD_SUCCESS
)
646 (void)ubc_map(vp
, PROT_EXEC
);
654 struct segment_command
*scp
,
658 __unused off_t end_of_file
,
660 load_result_t
*result
664 vm_offset_t map_addr
, map_offset
;
665 vm_size_t map_size
, seg_size
, delta_size
;
670 * Make sure what we get from the file is really ours (as specified
673 if (scp
->fileoff
+ scp
->filesize
> macho_size
)
674 return (LOAD_BADMACHO
);
676 seg_size
= round_page(scp
->vmsize
);
678 return(KERN_SUCCESS
);
681 * Round sizes to page size.
683 map_size
= round_page(scp
->filesize
);
684 map_addr
= trunc_page(scp
->vmaddr
);
686 map_offset
= pager_offset
+ scp
->fileoff
;
689 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
690 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
692 * Map a copy of the file into the address space.
695 &map_addr
, map_size
, (vm_offset_t
)0,
696 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
699 if (ret
!= KERN_SUCCESS
)
700 return(LOAD_NOSPACE
);
703 * If the file didn't end on a page boundary,
704 * we need to zero the leftover.
706 delta_size
= map_size
- scp
->filesize
;
708 if (delta_size
> 0) {
711 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
712 if (ret
!= KERN_SUCCESS
)
713 return(LOAD_RESOURCE
);
715 if (copyout(tmp
, map_addr
+ scp
->filesize
,
717 (void) vm_deallocate(
718 kernel_map
, tmp
, delta_size
);
719 return(LOAD_FAILURE
);
722 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
728 * If the virtual size of the segment is greater
729 * than the size from the file, we need to allocate
730 * zero fill memory for the rest.
732 delta_size
= seg_size
- map_size
;
733 if (delta_size
> 0) {
734 vm_offset_t tmp
= map_addr
+ map_size
;
736 ret
= vm_allocate(map
, &tmp
, delta_size
, VM_FLAGS_FIXED
);
737 if (ret
!= KERN_SUCCESS
)
738 return(LOAD_NOSPACE
);
742 * Set protection values. (Note: ignore errors!)
745 if (scp
->maxprot
!= VM_PROT_DEFAULT
) {
746 (void) vm_protect(map
,
750 if (scp
->initprot
!= VM_PROT_DEFAULT
) {
751 (void) vm_protect(map
,
753 FALSE
, scp
->initprot
);
755 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
756 result
->mach_header
= map_addr
;
757 return(LOAD_SUCCESS
);
763 struct segment_command_64
*scp64
,
767 __unused off_t end_of_file
,
769 load_result_t
*result
773 mach_vm_offset_t map_addr
, map_offset
;
774 mach_vm_size_t map_size
, seg_size
, delta_size
;
779 * Make sure what we get from the file is really ours (as specified
782 if (scp64
->fileoff
+ scp64
->filesize
> (uint64_t)macho_size
)
783 return (LOAD_BADMACHO
);
785 seg_size
= round_page_64(scp64
->vmsize
);
787 return(KERN_SUCCESS
);
790 * Round sizes to page size.
792 map_size
= round_page_64(scp64
->filesize
); /* limited to 32 bits */
793 map_addr
= round_page_64(scp64
->vmaddr
);
795 map_offset
= pager_offset
+ scp64
->fileoff
; /* limited to 32 bits */
798 initprot
= (scp64
->initprot
) & VM_PROT_ALL
;
799 maxprot
= (scp64
->maxprot
) & VM_PROT_ALL
;
801 * Map a copy of the file into the address space.
803 ret
= mach_vm_map(map
,
804 &map_addr
, map_size
, (mach_vm_offset_t
)0,
805 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
808 if (ret
!= KERN_SUCCESS
)
809 return(LOAD_NOSPACE
);
812 * If the file didn't end on a page boundary,
813 * we need to zero the leftover.
815 delta_size
= map_size
- scp64
->filesize
;
817 if (delta_size
> 0) {
818 mach_vm_offset_t tmp
;
820 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
821 if (ret
!= KERN_SUCCESS
)
822 return(LOAD_RESOURCE
);
824 if (copyout(tmp
, map_addr
+ scp64
->filesize
,
826 (void) vm_deallocate(
827 kernel_map
, tmp
, delta_size
);
828 return (LOAD_FAILURE
);
831 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
837 * If the virtual size of the segment is greater
838 * than the size from the file, we need to allocate
839 * zero fill memory for the rest.
841 delta_size
= seg_size
- map_size
;
842 if (delta_size
> 0) {
843 mach_vm_offset_t tmp
= map_addr
+ map_size
;
845 ret
= mach_vm_allocate(map
, &tmp
, delta_size
, VM_FLAGS_FIXED
);
846 if (ret
!= KERN_SUCCESS
)
847 return(LOAD_NOSPACE
);
851 * Set protection values. (Note: ignore errors!)
854 if (scp64
->maxprot
!= VM_PROT_DEFAULT
) {
855 (void) mach_vm_protect(map
,
857 TRUE
, scp64
->maxprot
);
859 if (scp64
->initprot
!= VM_PROT_DEFAULT
) {
860 (void) mach_vm_protect(map
,
862 FALSE
, scp64
->initprot
);
864 if ( (scp64
->fileoff
== 0) && (scp64
->filesize
!= 0) )
865 result
->mach_header
= map_addr
;
866 return(LOAD_SUCCESS
);
872 struct thread_command
*tcp
,
874 load_result_t
*result
882 task
= get_threadtask(thread
);
884 /* if count is 0; same as thr_act */
885 if (result
->thread_count
!= 0) {
886 kret
= thread_create(task
, &thread
);
887 if (kret
!= KERN_SUCCESS
)
888 return(LOAD_RESOURCE
);
889 thread_deallocate(thread
);
892 lret
= load_threadstate(thread
,
893 (unsigned long *)(((vm_offset_t
)tcp
) +
894 sizeof(struct thread_command
)),
895 tcp
->cmdsize
- sizeof(struct thread_command
));
896 if (lret
!= LOAD_SUCCESS
)
899 if (result
->thread_count
== 0) {
900 lret
= load_threadstack(thread
,
901 (unsigned long *)(((vm_offset_t
)tcp
) +
902 sizeof(struct thread_command
)),
903 tcp
->cmdsize
- sizeof(struct thread_command
),
907 result
->customstack
= 1;
909 result
->customstack
= 0;
911 if (lret
!= LOAD_SUCCESS
)
914 lret
= load_threadentry(thread
,
915 (unsigned long *)(((vm_offset_t
)tcp
) +
916 sizeof(struct thread_command
)),
917 tcp
->cmdsize
- sizeof(struct thread_command
),
918 &result
->entry_point
);
919 if (lret
!= LOAD_SUCCESS
)
923 * Resume thread now, note that this means that the thread
924 * commands should appear after all the load commands to
925 * be sure they don't reference anything not yet mapped.
928 thread_resume(thread
);
930 result
->thread_count
++;
932 return(LOAD_SUCCESS
);
938 struct thread_command
*tcp
,
940 load_result_t
*result
946 if (result
->thread_count
!= 0)
947 return (LOAD_FAILURE
);
949 ret
= load_threadstack(thread
,
950 (unsigned long *)(((vm_offset_t
)tcp
) +
951 sizeof(struct thread_command
)),
952 tcp
->cmdsize
- sizeof(struct thread_command
),
955 if (ret
!= LOAD_SUCCESS
)
959 result
->customstack
= 1;
961 result
->customstack
= 0;
962 ret
= load_threadentry(thread
,
963 (unsigned long *)(((vm_offset_t
)tcp
) +
964 sizeof(struct thread_command
)),
965 tcp
->cmdsize
- sizeof(struct thread_command
),
966 &result
->entry_point
);
967 if (ret
!= LOAD_SUCCESS
)
970 ret
= load_threadstate(thread
,
971 (unsigned long *)(((vm_offset_t
)tcp
) +
972 sizeof(struct thread_command
)),
973 tcp
->cmdsize
- sizeof(struct thread_command
));
974 if (ret
!= LOAD_SUCCESS
)
977 result
->unixproc
= TRUE
;
978 result
->thread_count
++;
980 return(LOAD_SUCCESS
);
988 unsigned long total_size
994 unsigned long thread_size
;
996 ret
= thread_state_initialize( thread
);
997 if (ret
!= KERN_SUCCESS
)
998 return(LOAD_FAILURE
);
1001 * Set the new thread state; iterate through the state flavors in
1004 while (total_size
> 0) {
1007 thread_size
= (size
+2)*sizeof(unsigned long);
1008 if (thread_size
> total_size
)
1009 return(LOAD_BADMACHO
);
1010 total_size
-= thread_size
;
1012 * Third argument is a kernel space pointer; it gets cast
1013 * to the appropriate type in machine_thread_set_state()
1014 * based on the value of flavor.
1016 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1017 if (ret
!= KERN_SUCCESS
)
1018 return(LOAD_FAILURE
);
1019 ts
+= size
; /* ts is a (unsigned long *) */
1021 return(LOAD_SUCCESS
);
1029 unsigned long total_size
,
1030 user_addr_t
*user_stack
,
1037 unsigned long stack_size
;
1039 while (total_size
> 0) {
1042 stack_size
= (size
+2)*sizeof(unsigned long);
1043 if (stack_size
> total_size
)
1044 return(LOAD_BADMACHO
);
1045 total_size
-= stack_size
;
1048 * Third argument is a kernel space pointer; it gets cast
1049 * to the appropriate type in thread_userstack() based on
1050 * the value of flavor.
1052 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1053 if (ret
!= KERN_SUCCESS
)
1054 return(LOAD_FAILURE
);
1055 ts
+= size
; /* ts is a (unsigned long *) */
1057 return(LOAD_SUCCESS
);
1065 unsigned long total_size
,
1066 mach_vm_offset_t
*entry_point
1072 unsigned long entry_size
;
1075 * Set the thread state.
1077 *entry_point
= MACH_VM_MIN_ADDRESS
;
1078 while (total_size
> 0) {
1081 entry_size
= (size
+2)*sizeof(unsigned long);
1082 if (entry_size
> total_size
)
1083 return(LOAD_BADMACHO
);
1084 total_size
-= entry_size
;
1086 * Third argument is a kernel space pointer; it gets cast
1087 * to the appropriate type in thread_entrypoint() based on
1088 * the value of flavor.
1090 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1091 if (ret
!= KERN_SUCCESS
)
1092 return(LOAD_FAILURE
);
1093 ts
+= size
; /* ts is a (unsigned long *) */
1095 return(LOAD_SUCCESS
);
1102 struct dylinker_command
*lcp
,
1107 load_result_t
*result
,
1108 boolean_t clean_regions
1114 struct mach_header header
;
1118 load_result_t myresult
;
1121 mach_vm_offset_t dyl_start
, map_addr
;
1122 mach_vm_size_t dyl_length
;
1124 name
= (char *)lcp
+ lcp
->name
.offset
;
1126 * Check for a proper null terminated string.
1130 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1131 return(LOAD_BADMACHO
);
1134 ret
= get_macho_vnode(name
, archbits
, &header
, &file_offset
, &macho_size
, &vp
);
1140 * Use a temporary map to do the work.
1142 copy_map
= vm_map_create(pmap_create(vm_map_round_page(macho_size
)),
1143 get_map_min(map
), get_map_max(map
), TRUE
);
1144 if (VM_MAP_NULL
== copy_map
) {
1145 ret
= LOAD_RESOURCE
;
1149 myresult
= load_result_null
;
1151 ret
= parse_machfile(vp
, copy_map
, thr_act
, &header
,
1152 file_offset
, macho_size
,
1153 FALSE
, clean_regions
, depth
, &myresult
);
1158 if (get_map_nentries(copy_map
) > 0) {
1160 dyl_start
= mach_get_vm_start(copy_map
);
1161 dyl_length
= mach_get_vm_end(copy_map
) - dyl_start
;
1163 map_addr
= dyl_start
;
1164 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_FIXED
);
1165 if (ret
!= KERN_SUCCESS
) {
1166 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1169 if (ret
!= KERN_SUCCESS
) {
1174 ret
= vm_map_copyin(copy_map
,
1175 (vm_map_address_t
)dyl_start
,
1176 (vm_map_size_t
)dyl_length
,
1178 if (ret
!= KERN_SUCCESS
) {
1179 (void) vm_map_remove(map
,
1180 vm_map_trunc_page(map_addr
),
1181 vm_map_round_page(map_addr
+ dyl_length
),
1186 ret
= vm_map_copy_overwrite(map
,
1187 (vm_map_address_t
)map_addr
,
1189 if (ret
!= KERN_SUCCESS
) {
1190 vm_map_copy_discard(tmp
);
1191 (void) vm_map_remove(map
,
1192 vm_map_trunc_page(map_addr
),
1193 vm_map_round_page(map_addr
+ dyl_length
),
1198 if (map_addr
!= dyl_start
)
1199 myresult
.entry_point
+= (map_addr
- dyl_start
);
1203 if (ret
== LOAD_SUCCESS
) {
1204 result
->dynlinker
= TRUE
;
1205 result
->entry_point
= myresult
.entry_point
;
1206 (void)ubc_map(vp
, PROT_EXEC
);
1209 vm_map_deallocate(copy_map
);
1217 * This routine exists to support the load_dylinker().
1219 * This routine has its own, separate, understanding of the FAT file format,
1220 * which is terrifically unfortunate.
1227 struct mach_header
*mach_header
,
1234 struct vfs_context context
;
1235 struct nameidata nid
, *ndp
;
1236 struct proc
*p
= current_proc(); /* XXXX */
1238 struct fat_arch fat_arch
;
1239 int error
= LOAD_SUCCESS
;
1242 struct mach_header mach_header
;
1243 struct fat_header fat_header
;
1246 off_t fsize
= (off_t
)0;
1247 struct ucred
*cred
= kauth_cred_get();
1250 context
.vc_proc
= p
;
1251 context
.vc_ucred
= cred
;
1255 /* init the namei data to point the file user's program name */
1256 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(path
), &context
);
1258 if ((error
= namei(ndp
)) != 0) {
1259 if (error
== ENOENT
)
1260 error
= LOAD_ENOENT
;
1262 error
= LOAD_FAILURE
;
1268 /* check for regular file */
1269 if (vp
->v_type
!= VREG
) {
1270 error
= LOAD_PROTECT
;
1275 if ((error
= vnode_size(vp
, &fsize
, &context
)) != 0) {
1276 error
= LOAD_FAILURE
;
1280 /* Check mount point */
1281 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1282 error
= LOAD_PROTECT
;
1287 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, &context
)) != 0) {
1288 error
= LOAD_PROTECT
;
1292 /* try to open it */
1293 if ((error
= VNOP_OPEN(vp
, FREAD
, &context
)) != 0) {
1294 error
= LOAD_PROTECT
;
1298 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
, sizeof(header
), 0,
1299 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &resid
, p
)) != 0) {
1300 error
= LOAD_IOERROR
;
1304 if (header
.mach_header
.magic
== MH_MAGIC
||
1305 header
.mach_header
.magic
== MH_MAGIC_64
)
1307 else if (header
.fat_header
.magic
== FAT_MAGIC
||
1308 header
.fat_header
.magic
== FAT_CIGAM
)
1311 error
= LOAD_BADMACHO
;
1316 /* Look up our architecture in the fat file. */
1317 error
= fatfile_getarch_with_bits(vp
, archbits
, (vm_offset_t
)(&header
.fat_header
), &fat_arch
);
1318 if (error
!= LOAD_SUCCESS
)
1321 /* Read the Mach-O header out of it */
1322 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
.mach_header
,
1323 sizeof(header
.mach_header
), fat_arch
.offset
,
1324 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &resid
, p
);
1326 error
= LOAD_IOERROR
;
1330 /* Is this really a Mach-O? */
1331 if (header
.mach_header
.magic
!= MH_MAGIC
&&
1332 header
.mach_header
.magic
!= MH_MAGIC_64
) {
1333 error
= LOAD_BADMACHO
;
1337 *file_offset
= fat_arch
.offset
;
1338 *macho_size
= fsize
= fat_arch
.size
;
1341 * Force get_macho_vnode() to fail if the architecture bits
1342 * do not match the expected architecture bits. This in
1343 * turn causes load_dylinker() to fail for the same reason,
1344 * so it ensures the dynamic linker and the binary are in
1345 * lock-step. This is potentially bad, if we ever add to
1346 * the CPU_ARCH_* bits any bits that are desirable but not
1347 * required, since the dynamic linker might work, but we will
1348 * refuse to load it because of this check.
1350 if ((cpu_type_t
)(header
.mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
)
1351 return(LOAD_BADARCH
);
1354 *macho_size
= fsize
;
1357 *mach_header
= header
.mach_header
;
1360 ubc_setsize(vp
, fsize
);
1365 err2
= VNOP_CLOSE(vp
, FREAD
, &context
);