2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (C) 1988, 1989, NeXT, Inc.
25 * File: kern/mach_loader.c
26 * Author: Avadis Tevanian, Jr.
28 * Mach object file loader (kernel version, for now).
30 * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
34 #include <sys/param.h>
35 #include <sys/vnode_internal.h>
37 #include <sys/namei.h>
38 #include <sys/proc_internal.h>
39 #include <sys/kauth.h>
41 #include <sys/malloc.h>
42 #include <sys/mount_internal.h>
43 #include <sys/fcntl.h>
44 #include <sys/ubc_internal.h>
45 #include <sys/imgact.h>
47 #include <mach/mach_types.h>
48 #include <mach/vm_map.h> /* vm_allocate() */
49 #include <mach/mach_vm.h> /* mach_vm_allocate() */
50 #include <mach/vm_statistics.h>
51 #include <mach/shared_memory_server.h>
52 #include <mach/task.h>
53 #include <mach/thread_act.h>
55 #include <machine/vmparam.h>
57 #include <kern/kern_types.h>
58 #include <kern/cpu_number.h>
59 #include <kern/mach_loader.h>
60 #include <kern/kalloc.h>
61 #include <kern/task.h>
62 #include <kern/thread.h>
64 #include <mach-o/fat.h>
65 #include <mach-o/loader.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vnode_pager.h>
72 #include <vm/vm_shared_memory_server.h>
73 #include <vm/vm_protos.h>
76 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
77 * when KERNEL is defined.
79 extern pmap_t
pmap_create(vm_map_size_t size
);
80 extern void pmap_switch(pmap_t
);
81 extern void pmap_map_sharedpage(task_t task
, pmap_t pmap
);
84 * XXX kern/thread.h should not treat these prototypes as MACH_KERNEL_PRIVATE
85 * when KERNEL is defined.
87 extern kern_return_t
thread_setstatus(thread_t thread
, int flavor
,
88 thread_state_t tstate
,
89 mach_msg_type_number_t count
);
91 extern kern_return_t
thread_state_initialize(thread_t thread
);
94 /* XXX should have prototypes in a shared header file */
95 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
96 extern int get_map_nentries(vm_map_t
);
97 extern kern_return_t
thread_userstack(thread_t
, int, thread_state_t
,
98 unsigned int, mach_vm_offset_t
*, int *);
99 extern kern_return_t
thread_entrypoint(thread_t
, int, thread_state_t
,
100 unsigned int, mach_vm_offset_t
*);
103 /* An empty load_result_t */
104 static load_result_t load_result_null
= {
115 * Prototypes of static functions.
122 struct mach_header
*header
,
125 boolean_t shared_regions
,
126 boolean_t clean_regions
,
128 load_result_t
*result
133 struct segment_command
*scp
,
139 load_result_t
*result
144 struct segment_command_64
*scp64
,
150 load_result_t
*result
155 struct thread_command
*tcp
,
157 load_result_t
*result
162 struct thread_command
*tcp
,
164 load_result_t
*result
171 unsigned long total_size
178 unsigned long total_size
,
179 mach_vm_offset_t
*user_stack
,
187 unsigned long total_size
,
188 mach_vm_offset_t
*entry_point
193 struct dylinker_command
*lcp
,
198 load_result_t
*result
,
199 boolean_t clean_regions
206 struct mach_header
*mach_header
,
214 struct image_params
*imgp
,
215 struct mach_header
*header
,
218 boolean_t clean_regions
,
219 load_result_t
*result
222 struct vnode
*vp
= imgp
->ip_vp
;
223 off_t file_offset
= imgp
->ip_arch_offset
;
224 off_t macho_size
= imgp
->ip_arch_size
;
226 pmap_t pmap
= 0; /* protected by create_map */
229 load_result_t myresult
;
231 boolean_t create_map
= TRUE
;
233 if (new_map
!= VM_MAP_NULL
) {
238 old_map
= current_map();
240 pmap
= get_task_pmap(current_task());
241 pmap_reference(pmap
);
243 pmap
= pmap_create((vm_map_size_t
) 0);
245 map
= vm_map_create(pmap
,
246 get_map_min(old_map
),
247 get_map_max(old_map
),
248 TRUE
); /**** FIXME ****/
255 *result
= load_result_null
;
257 lret
= parse_machfile(vp
, map
, thr_act
, header
, file_offset
, macho_size
,
258 ((imgp
->ip_flags
& IMGPF_IS_64BIT
) == 0), /* shared regions? */
259 clean_regions
, 0, result
);
261 if (lret
!= LOAD_SUCCESS
) {
263 vm_map_deallocate(map
); /* will lose pmap reference too */
269 * Commit to new map. First make sure that the current
270 * users of the task get done with it, and that we clean
271 * up the old contents of IPC and memory. The task is
272 * guaranteed to be single threaded upon return (us).
274 * Swap the new map for the old, which consumes our new map
275 * reference but each leaves us responsible for the old_map reference.
276 * That lets us get off the pmap associated with it, and
277 * then we can release it.
280 task_halt(current_task());
282 old_map
= swap_task_map(current_task(), map
);
284 pmap_switch(pmap
); /* Make sure we are using the new pmap */
286 vm_map_deallocate(old_map
);
288 return(LOAD_SUCCESS
);
294 * The file size of a mach-o file is limited to 32 bits; this is because
295 * this is the limit on the kalloc() of enough bytes for a mach_header and
296 * the contents of its sizeofcmds, which is currently constrained to 32
297 * bits in the file format itself. We read into the kernel buffer the
298 * commands section, and then parse it in order to parse the mach-o file
299 * format load_command segment(s). We are only interested in a subset of
300 * the total set of possible commands.
308 struct mach_header
*header
,
311 boolean_t shared_regions
,
312 boolean_t clean_regions
,
314 load_result_t
*result
318 struct load_command
*lcp
;
319 struct dylinker_command
*dlp
= 0;
320 integer_t dlarchbits
= 0;
322 load_return_t ret
= LOAD_SUCCESS
;
325 vm_size_t size
,kl_size
;
327 size_t oldoffset
; /* for overflow check */
329 struct proc
*p
= current_proc(); /* XXXX */
333 size_t mach_header_sz
= sizeof(struct mach_header
);
336 if (header
->magic
== MH_MAGIC_64
||
337 header
->magic
== MH_CIGAM_64
) {
338 mach_header_sz
= sizeof(struct mach_header_64
);
342 * Break infinite recursion
345 return(LOAD_FAILURE
);
347 task
= (task_t
)get_threadtask(thr_act
);
352 * Check to see if right machine type.
354 if (((cpu_type_t
)(header
->cputype
& ~CPU_ARCH_MASK
) != cpu_type()) ||
355 !grade_binary(header
->cputype
, header
->cpusubtype
))
356 return(LOAD_BADARCH
);
358 abi64
= ((header
->cputype
& CPU_ARCH_ABI64
) == CPU_ARCH_ABI64
);
360 switch (header
->filetype
) {
366 return (LOAD_FAILURE
);
372 return (LOAD_FAILURE
);
377 return (LOAD_FAILURE
);
381 return (LOAD_FAILURE
);
385 * Get the pager for the file.
387 UBCINFOCHECK("parse_machfile", vp
);
388 pager
= (void *) ubc_getpager(vp
);
391 * Map portion that must be accessible directly into
394 if ((mach_header_sz
+ header
->sizeofcmds
) > macho_size
)
395 return(LOAD_BADMACHO
);
398 * Round size of Mach-O commands up to page boundry.
400 size
= round_page(mach_header_sz
+ header
->sizeofcmds
);
402 return(LOAD_BADMACHO
);
405 * Map the load commands into kernel memory.
409 kl_addr
= kalloc(size
);
410 addr
= (caddr_t
)kl_addr
;
412 return(LOAD_NOSPACE
);
414 error
= vn_rdwr(UIO_READ
, vp
, addr
, size
, file_offset
,
415 UIO_SYSSPACE32
, 0, kauth_cred_get(), &resid
, p
);
418 kfree(kl_addr
, kl_size
);
419 return(LOAD_IOERROR
);
421 /* (void)ubc_map(vp, PROT_EXEC); */ /* NOT HERE */
424 * Scan through the commands, processing each one as necessary.
426 for (pass
= 1; pass
<= 2; pass
++) {
428 * Loop through each of the load_commands indicated by the
429 * Mach-O header; if an absurd value is provided, we just
430 * run off the end of the reserved section by incrementing
431 * the offset too far, so we are implicitly fail-safe.
433 offset
= mach_header_sz
;
434 ncmds
= header
->ncmds
;
437 * Get a pointer to the command.
439 lcp
= (struct load_command
*)(addr
+ offset
);
441 offset
+= lcp
->cmdsize
;
444 * Perform prevalidation of the struct load_command
445 * before we attempt to use its contents. Invalid
446 * values are ones which result in an overflow, or
447 * which can not possibly be valid commands, or which
448 * straddle or exist past the reserved section at the
449 * start of the image.
451 if (oldoffset
> offset
||
452 lcp
->cmdsize
< sizeof(struct load_command
) ||
453 offset
> header
->sizeofcmds
+ mach_header_sz
) {
459 * Act on struct load_command's for which kernel
460 * intervention is required.
466 ret
= load_segment_64(
467 (struct segment_command_64
*)lcp
,
479 (struct segment_command
*) lcp
,
490 ret
= load_thread((struct thread_command
*)lcp
,
497 ret
= load_unixthread(
498 (struct thread_command
*) lcp
,
502 case LC_LOAD_DYLINKER
:
505 if ((depth
== 1) && (dlp
== 0)) {
506 dlp
= (struct dylinker_command
*)lcp
;
507 dlarchbits
= (header
->cputype
& CPU_ARCH_MASK
);
513 /* Other commands are ignored by the kernel */
517 if (ret
!= LOAD_SUCCESS
)
520 if (ret
!= LOAD_SUCCESS
)
523 if (ret
== LOAD_SUCCESS
) {
525 if (shared_regions
) {
527 shared_region_mapping_t shared_region
;
528 struct shared_region_task_mappings map_info
;
529 shared_region_mapping_t next
;
532 vm_get_shared_region(task
, &shared_region
);
533 map_info
.self
= (vm_offset_t
)shared_region
;
534 shared_region_mapping_info(shared_region
,
535 &(map_info
.text_region
),
536 &(map_info
.text_size
),
537 &(map_info
.data_region
),
538 &(map_info
.data_size
),
539 &(map_info
.region_mappings
),
540 &(map_info
.client_base
),
541 &(map_info
.alternate_base
),
542 &(map_info
.alternate_next
),
545 &(map_info
.flags
), &next
);
547 if((map_info
.flags
& SHARED_REGION_FULL
) ||
548 (map_info
.flags
& SHARED_REGION_STALE
)) {
549 shared_region_mapping_t system_region
;
550 system_region
= lookup_default_shared_region(
551 map_info
.fs_base
, map_info
.system
);
552 if((map_info
.self
!= (vm_offset_t
)system_region
) &&
553 (map_info
.flags
& SHARED_REGION_SYSTEM
)) {
554 if(system_region
== NULL
) {
555 shared_file_boot_time_init(
556 map_info
.fs_base
, map_info
.system
);
558 vm_set_shared_region(task
, system_region
);
560 shared_region_mapping_dealloc(
561 (shared_region_mapping_t
)map_info
.self
);
563 } else if (map_info
.flags
& SHARED_REGION_SYSTEM
) {
564 shared_region_mapping_dealloc(system_region
);
565 shared_file_boot_time_init(
566 map_info
.fs_base
, map_info
.system
);
567 shared_region_mapping_dealloc(
568 (shared_region_mapping_t
)map_info
.self
);
570 shared_region_mapping_dealloc(system_region
);
575 p
->p_flag
|= P_NOSHLIB
; /* no shlibs in use */
576 vmaddr
= map_info
.client_base
;
578 vm_map(map
, &vmaddr
, map_info
.text_size
,
579 0, SHARED_LIB_ALIAS
|VM_FLAGS_FIXED
,
580 map_info
.text_region
, 0, FALSE
,
581 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
583 vm_map(map
, &vmaddr
, map_info
.text_size
, 0,
584 (VM_MEMORY_SHARED_PMAP
<< 24)
585 | SHARED_LIB_ALIAS
| VM_FLAGS_FIXED
,
586 map_info
.text_region
, 0, FALSE
,
587 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
589 vmaddr
= map_info
.client_base
+ map_info
.text_size
;
590 vm_map(map
, &vmaddr
, map_info
.data_size
,
591 0, SHARED_LIB_ALIAS
| VM_FLAGS_FIXED
,
592 map_info
.data_region
, 0, TRUE
,
593 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
596 /* this should be fleshed out for the general case */
597 /* but this is not necessary for now. Indeed we */
598 /* are handling the com page inside of the */
599 /* shared_region mapping create calls for now for */
600 /* simplicities sake. If more general support is */
601 /* needed the code to manipulate the shared range */
602 /* chain can be pulled out and moved to the callers*/
603 shared_region_mapping_info(next
,
604 &(map_info
.text_region
),
605 &(map_info
.text_size
),
606 &(map_info
.data_region
),
607 &(map_info
.data_size
),
608 &(map_info
.region_mappings
),
609 &(map_info
.client_base
),
610 &(map_info
.alternate_base
),
611 &(map_info
.alternate_next
),
614 &(map_info
.flags
), &next
);
616 vmaddr
= map_info
.client_base
;
617 vm_map(map
, &vmaddr
, map_info
.text_size
,
618 0, SHARED_LIB_ALIAS
| VM_FLAGS_FIXED
,
619 map_info
.text_region
, 0, FALSE
,
620 VM_PROT_READ
, VM_PROT_READ
, VM_INHERIT_SHARE
);
625 ret
= load_dylinker(dlp
, dlarchbits
, map
, thr_act
, depth
, result
, clean_regions
);
628 if (result
->thread_count
== 0)
632 /* Map in 64-bit commpage */
633 /* LP64todo - make this clean */
634 pmap_map_sharedpage(current_task(), get_map_pmap(map
));
635 vm_map_commpage64(map
);
642 kfree(kl_addr
, kl_size
);
644 if (ret
== LOAD_SUCCESS
)
645 (void)ubc_map(vp
, PROT_EXEC
);
653 struct segment_command
*scp
,
657 __unused off_t end_of_file
,
659 load_result_t
*result
663 vm_offset_t map_addr
, map_offset
;
664 vm_size_t map_size
, seg_size
, delta_size
;
669 * Make sure what we get from the file is really ours (as specified
672 if (scp
->fileoff
+ scp
->filesize
> macho_size
)
673 return (LOAD_BADMACHO
);
675 seg_size
= round_page(scp
->vmsize
);
677 return(KERN_SUCCESS
);
680 * Round sizes to page size.
682 map_size
= round_page(scp
->filesize
);
683 map_addr
= trunc_page(scp
->vmaddr
);
685 map_offset
= pager_offset
+ scp
->fileoff
;
688 initprot
= (scp
->initprot
) & VM_PROT_ALL
;
689 maxprot
= (scp
->maxprot
) & VM_PROT_ALL
;
691 * Map a copy of the file into the address space.
694 &map_addr
, map_size
, (vm_offset_t
)0,
695 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
698 if (ret
!= KERN_SUCCESS
)
699 return(LOAD_NOSPACE
);
702 * If the file didn't end on a page boundary,
703 * we need to zero the leftover.
705 delta_size
= map_size
- scp
->filesize
;
707 if (delta_size
> 0) {
710 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
711 if (ret
!= KERN_SUCCESS
)
712 return(LOAD_RESOURCE
);
714 if (copyout(tmp
, map_addr
+ scp
->filesize
,
716 (void) vm_deallocate(
717 kernel_map
, tmp
, delta_size
);
718 return(LOAD_FAILURE
);
721 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
727 * If the virtual size of the segment is greater
728 * than the size from the file, we need to allocate
729 * zero fill memory for the rest.
731 delta_size
= seg_size
- map_size
;
732 if (delta_size
> 0) {
733 vm_offset_t tmp
= map_addr
+ map_size
;
735 ret
= vm_allocate(map
, &tmp
, delta_size
, VM_FLAGS_FIXED
);
736 if (ret
!= KERN_SUCCESS
)
737 return(LOAD_NOSPACE
);
741 * Set protection values. (Note: ignore errors!)
744 if (scp
->maxprot
!= VM_PROT_DEFAULT
) {
745 (void) vm_protect(map
,
749 if (scp
->initprot
!= VM_PROT_DEFAULT
) {
750 (void) vm_protect(map
,
752 FALSE
, scp
->initprot
);
754 if ( (scp
->fileoff
== 0) && (scp
->filesize
!= 0) )
755 result
->mach_header
= map_addr
;
756 return(LOAD_SUCCESS
);
762 struct segment_command_64
*scp64
,
766 __unused off_t end_of_file
,
768 load_result_t
*result
772 mach_vm_offset_t map_addr
, map_offset
;
773 mach_vm_size_t map_size
, seg_size
, delta_size
;
778 * Make sure what we get from the file is really ours (as specified
781 if (scp64
->fileoff
+ scp64
->filesize
> (uint64_t)macho_size
)
782 return (LOAD_BADMACHO
);
784 seg_size
= round_page_64(scp64
->vmsize
);
786 return(KERN_SUCCESS
);
789 * Round sizes to page size.
791 map_size
= round_page_64(scp64
->filesize
); /* limited to 32 bits */
792 map_addr
= round_page_64(scp64
->vmaddr
);
794 map_offset
= pager_offset
+ scp64
->fileoff
; /* limited to 32 bits */
797 initprot
= (scp64
->initprot
) & VM_PROT_ALL
;
798 maxprot
= (scp64
->maxprot
) & VM_PROT_ALL
;
800 * Map a copy of the file into the address space.
802 ret
= mach_vm_map(map
,
803 &map_addr
, map_size
, (mach_vm_offset_t
)0,
804 VM_FLAGS_FIXED
, pager
, map_offset
, TRUE
,
807 if (ret
!= KERN_SUCCESS
)
808 return(LOAD_NOSPACE
);
811 * If the file didn't end on a page boundary,
812 * we need to zero the leftover.
814 delta_size
= map_size
- scp64
->filesize
;
816 if (delta_size
> 0) {
817 mach_vm_offset_t tmp
;
819 ret
= vm_allocate(kernel_map
, &tmp
, delta_size
, VM_FLAGS_ANYWHERE
);
820 if (ret
!= KERN_SUCCESS
)
821 return(LOAD_RESOURCE
);
823 if (copyout(tmp
, map_addr
+ scp64
->filesize
,
825 (void) vm_deallocate(
826 kernel_map
, tmp
, delta_size
);
827 return (LOAD_FAILURE
);
830 (void) vm_deallocate(kernel_map
, tmp
, delta_size
);
836 * If the virtual size of the segment is greater
837 * than the size from the file, we need to allocate
838 * zero fill memory for the rest.
840 delta_size
= seg_size
- map_size
;
841 if (delta_size
> 0) {
842 mach_vm_offset_t tmp
= map_addr
+ map_size
;
844 ret
= mach_vm_allocate(map
, &tmp
, delta_size
, VM_FLAGS_FIXED
);
845 if (ret
!= KERN_SUCCESS
)
846 return(LOAD_NOSPACE
);
850 * Set protection values. (Note: ignore errors!)
853 if (scp64
->maxprot
!= VM_PROT_DEFAULT
) {
854 (void) mach_vm_protect(map
,
856 TRUE
, scp64
->maxprot
);
858 if (scp64
->initprot
!= VM_PROT_DEFAULT
) {
859 (void) mach_vm_protect(map
,
861 FALSE
, scp64
->initprot
);
863 if ( (scp64
->fileoff
== 0) && (scp64
->filesize
!= 0) )
864 result
->mach_header
= map_addr
;
865 return(LOAD_SUCCESS
);
871 struct thread_command
*tcp
,
873 load_result_t
*result
881 task
= get_threadtask(thread
);
883 /* if count is 0; same as thr_act */
884 if (result
->thread_count
!= 0) {
885 kret
= thread_create(task
, &thread
);
886 if (kret
!= KERN_SUCCESS
)
887 return(LOAD_RESOURCE
);
888 thread_deallocate(thread
);
891 lret
= load_threadstate(thread
,
892 (unsigned long *)(((vm_offset_t
)tcp
) +
893 sizeof(struct thread_command
)),
894 tcp
->cmdsize
- sizeof(struct thread_command
));
895 if (lret
!= LOAD_SUCCESS
)
898 if (result
->thread_count
== 0) {
899 lret
= load_threadstack(thread
,
900 (unsigned long *)(((vm_offset_t
)tcp
) +
901 sizeof(struct thread_command
)),
902 tcp
->cmdsize
- sizeof(struct thread_command
),
906 result
->customstack
= 1;
908 result
->customstack
= 0;
910 if (lret
!= LOAD_SUCCESS
)
913 lret
= load_threadentry(thread
,
914 (unsigned long *)(((vm_offset_t
)tcp
) +
915 sizeof(struct thread_command
)),
916 tcp
->cmdsize
- sizeof(struct thread_command
),
917 &result
->entry_point
);
918 if (lret
!= LOAD_SUCCESS
)
922 * Resume thread now, note that this means that the thread
923 * commands should appear after all the load commands to
924 * be sure they don't reference anything not yet mapped.
927 thread_resume(thread
);
929 result
->thread_count
++;
931 return(LOAD_SUCCESS
);
937 struct thread_command
*tcp
,
939 load_result_t
*result
945 if (result
->thread_count
!= 0)
946 return (LOAD_FAILURE
);
948 ret
= load_threadstack(thread
,
949 (unsigned long *)(((vm_offset_t
)tcp
) +
950 sizeof(struct thread_command
)),
951 tcp
->cmdsize
- sizeof(struct thread_command
),
954 if (ret
!= LOAD_SUCCESS
)
958 result
->customstack
= 1;
960 result
->customstack
= 0;
961 ret
= load_threadentry(thread
,
962 (unsigned long *)(((vm_offset_t
)tcp
) +
963 sizeof(struct thread_command
)),
964 tcp
->cmdsize
- sizeof(struct thread_command
),
965 &result
->entry_point
);
966 if (ret
!= LOAD_SUCCESS
)
969 ret
= load_threadstate(thread
,
970 (unsigned long *)(((vm_offset_t
)tcp
) +
971 sizeof(struct thread_command
)),
972 tcp
->cmdsize
- sizeof(struct thread_command
));
973 if (ret
!= LOAD_SUCCESS
)
976 result
->unixproc
= TRUE
;
977 result
->thread_count
++;
979 return(LOAD_SUCCESS
);
987 unsigned long total_size
993 unsigned long thread_size
;
995 ret
= thread_state_initialize( thread
);
996 if (ret
!= KERN_SUCCESS
)
997 return(LOAD_FAILURE
);
1000 * Set the new thread state; iterate through the state flavors in
1003 while (total_size
> 0) {
1006 thread_size
= (size
+2)*sizeof(unsigned long);
1007 if (thread_size
> total_size
)
1008 return(LOAD_BADMACHO
);
1009 total_size
-= thread_size
;
1011 * Third argument is a kernel space pointer; it gets cast
1012 * to the appropriate type in machine_thread_set_state()
1013 * based on the value of flavor.
1015 ret
= thread_setstatus(thread
, flavor
, (thread_state_t
)ts
, size
);
1016 if (ret
!= KERN_SUCCESS
)
1017 return(LOAD_FAILURE
);
1018 ts
+= size
; /* ts is a (unsigned long *) */
1020 return(LOAD_SUCCESS
);
1028 unsigned long total_size
,
1029 user_addr_t
*user_stack
,
1036 unsigned long stack_size
;
1038 while (total_size
> 0) {
1041 stack_size
= (size
+2)*sizeof(unsigned long);
1042 if (stack_size
> total_size
)
1043 return(LOAD_BADMACHO
);
1044 total_size
-= stack_size
;
1047 * Third argument is a kernel space pointer; it gets cast
1048 * to the appropriate type in thread_userstack() based on
1049 * the value of flavor.
1051 ret
= thread_userstack(thread
, flavor
, (thread_state_t
)ts
, size
, user_stack
, customstack
);
1052 if (ret
!= KERN_SUCCESS
)
1053 return(LOAD_FAILURE
);
1054 ts
+= size
; /* ts is a (unsigned long *) */
1056 return(LOAD_SUCCESS
);
1064 unsigned long total_size
,
1065 mach_vm_offset_t
*entry_point
1071 unsigned long entry_size
;
1074 * Set the thread state.
1076 *entry_point
= MACH_VM_MIN_ADDRESS
;
1077 while (total_size
> 0) {
1080 entry_size
= (size
+2)*sizeof(unsigned long);
1081 if (entry_size
> total_size
)
1082 return(LOAD_BADMACHO
);
1083 total_size
-= entry_size
;
1085 * Third argument is a kernel space pointer; it gets cast
1086 * to the appropriate type in thread_entrypoint() based on
1087 * the value of flavor.
1089 ret
= thread_entrypoint(thread
, flavor
, (thread_state_t
)ts
, size
, entry_point
);
1090 if (ret
!= KERN_SUCCESS
)
1091 return(LOAD_FAILURE
);
1092 ts
+= size
; /* ts is a (unsigned long *) */
1094 return(LOAD_SUCCESS
);
1101 struct dylinker_command
*lcp
,
1106 load_result_t
*result
,
1107 boolean_t clean_regions
1113 struct mach_header header
;
1117 load_result_t myresult
;
1120 mach_vm_offset_t dyl_start
, map_addr
;
1121 mach_vm_size_t dyl_length
;
1123 name
= (char *)lcp
+ lcp
->name
.offset
;
1125 * Check for a proper null terminated string.
1129 if (p
>= (char *)lcp
+ lcp
->cmdsize
)
1130 return(LOAD_BADMACHO
);
1133 ret
= get_macho_vnode(name
, archbits
, &header
, &file_offset
, &macho_size
, &vp
);
1139 * Use a temporary map to do the work.
1141 copy_map
= vm_map_create(pmap_create(vm_map_round_page(macho_size
)),
1142 get_map_min(map
), get_map_max(map
), TRUE
);
1143 if (VM_MAP_NULL
== copy_map
) {
1144 ret
= LOAD_RESOURCE
;
1148 myresult
= load_result_null
;
1150 ret
= parse_machfile(vp
, copy_map
, thr_act
, &header
,
1151 file_offset
, macho_size
,
1152 FALSE
, clean_regions
, depth
, &myresult
);
1157 if (get_map_nentries(copy_map
) > 0) {
1159 dyl_start
= mach_get_vm_start(copy_map
);
1160 dyl_length
= mach_get_vm_end(copy_map
) - dyl_start
;
1162 map_addr
= dyl_start
;
1163 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_FIXED
);
1164 if (ret
!= KERN_SUCCESS
) {
1165 ret
= mach_vm_allocate(map
, &map_addr
, dyl_length
, VM_FLAGS_ANYWHERE
);
1168 if (ret
!= KERN_SUCCESS
) {
1173 ret
= vm_map_copyin(copy_map
,
1174 (vm_map_address_t
)dyl_start
,
1175 (vm_map_size_t
)dyl_length
,
1177 if (ret
!= KERN_SUCCESS
) {
1178 (void) vm_map_remove(map
,
1179 vm_map_trunc_page(map_addr
),
1180 vm_map_round_page(map_addr
+ dyl_length
),
1185 ret
= vm_map_copy_overwrite(map
,
1186 (vm_map_address_t
)map_addr
,
1188 if (ret
!= KERN_SUCCESS
) {
1189 vm_map_copy_discard(tmp
);
1190 (void) vm_map_remove(map
,
1191 vm_map_trunc_page(map_addr
),
1192 vm_map_round_page(map_addr
+ dyl_length
),
1197 if (map_addr
!= dyl_start
)
1198 myresult
.entry_point
+= (map_addr
- dyl_start
);
1202 if (ret
== LOAD_SUCCESS
) {
1203 result
->dynlinker
= TRUE
;
1204 result
->entry_point
= myresult
.entry_point
;
1205 (void)ubc_map(vp
, PROT_EXEC
);
1208 vm_map_deallocate(copy_map
);
1216 * This routine exists to support the load_dylinker().
1218 * This routine has its own, separate, understanding of the FAT file format,
1219 * which is terrifically unfortunate.
1226 struct mach_header
*mach_header
,
1233 struct vfs_context context
;
1234 struct nameidata nid
, *ndp
;
1235 struct proc
*p
= current_proc(); /* XXXX */
1237 struct fat_arch fat_arch
;
1238 int error
= LOAD_SUCCESS
;
1241 struct mach_header mach_header
;
1242 struct fat_header fat_header
;
1245 off_t fsize
= (off_t
)0;
1246 struct ucred
*cred
= kauth_cred_get();
1249 context
.vc_proc
= p
;
1250 context
.vc_ucred
= cred
;
1254 /* init the namei data to point the file user's program name */
1255 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
, UIO_SYSSPACE32
, CAST_USER_ADDR_T(path
), &context
);
1257 if ((error
= namei(ndp
)) != 0) {
1258 if (error
== ENOENT
)
1259 error
= LOAD_ENOENT
;
1261 error
= LOAD_FAILURE
;
1267 /* check for regular file */
1268 if (vp
->v_type
!= VREG
) {
1269 error
= LOAD_PROTECT
;
1274 if ((error
= vnode_size(vp
, &fsize
, &context
)) != 0) {
1275 error
= LOAD_FAILURE
;
1279 /* Check mount point */
1280 if (vp
->v_mount
->mnt_flag
& MNT_NOEXEC
) {
1281 error
= LOAD_PROTECT
;
1286 if ((error
= vnode_authorize(vp
, NULL
, KAUTH_VNODE_EXECUTE
, &context
)) != 0) {
1287 error
= LOAD_PROTECT
;
1291 /* try to open it */
1292 if ((error
= VNOP_OPEN(vp
, FREAD
, &context
)) != 0) {
1293 error
= LOAD_PROTECT
;
1297 if ((error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
, sizeof(header
), 0,
1298 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &resid
, p
)) != 0) {
1299 error
= LOAD_IOERROR
;
1303 if (header
.mach_header
.magic
== MH_MAGIC
||
1304 header
.mach_header
.magic
== MH_MAGIC_64
)
1306 else if (header
.fat_header
.magic
== FAT_MAGIC
||
1307 header
.fat_header
.magic
== FAT_CIGAM
)
1310 error
= LOAD_BADMACHO
;
1315 /* Look up our architecture in the fat file. */
1316 error
= fatfile_getarch_with_bits(vp
, archbits
, (vm_offset_t
)(&header
.fat_header
), &fat_arch
);
1317 if (error
!= LOAD_SUCCESS
)
1320 /* Read the Mach-O header out of it */
1321 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&header
.mach_header
,
1322 sizeof(header
.mach_header
), fat_arch
.offset
,
1323 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &resid
, p
);
1325 error
= LOAD_IOERROR
;
1329 /* Is this really a Mach-O? */
1330 if (header
.mach_header
.magic
!= MH_MAGIC
&&
1331 header
.mach_header
.magic
!= MH_MAGIC_64
) {
1332 error
= LOAD_BADMACHO
;
1336 *file_offset
= fat_arch
.offset
;
1337 *macho_size
= fsize
= fat_arch
.size
;
1340 * Force get_macho_vnode() to fail if the architecture bits
1341 * do not match the expected architecture bits. This in
1342 * turn causes load_dylinker() to fail for the same reason,
1343 * so it ensures the dynamic linker and the binary are in
1344 * lock-step. This is potentially bad, if we ever add to
1345 * the CPU_ARCH_* bits any bits that are desirable but not
1346 * required, since the dynamic linker might work, but we will
1347 * refuse to load it because of this check.
1349 if ((cpu_type_t
)(header
.mach_header
.cputype
& CPU_ARCH_MASK
) != archbits
)
1350 return(LOAD_BADARCH
);
1353 *macho_size
= fsize
;
1356 *mach_header
= header
.mach_header
;
1359 ubc_setsize(vp
, fsize
);
1364 err2
= VNOP_CLOSE(vp
, FREAD
, &context
);