]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/mach_loader.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / kern / mach_loader.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (C) 1988, 1989, NeXT, Inc.
25 *
26 * File: kern/mach_loader.c
27 * Author: Avadis Tevanian, Jr.
28 *
29 * Mach object file loader (kernel version, for now).
30 *
31 * 21-Jul-88 Avadis Tevanian, Jr. (avie) at NeXT
32 * Started.
33 */
34
35 #include <sys/param.h>
36 #include <sys/vnode_internal.h>
37 #include <sys/uio.h>
38 #include <sys/namei.h>
39 #include <sys/proc_internal.h>
40 #include <sys/kauth.h>
41 #include <sys/stat.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>
47
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>
55
56 #include <machine/vmparam.h>
57
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>
64
65 #include <mach-o/fat.h>
66 #include <mach-o/loader.h>
67
68 #include <vm/pmap.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>
75
76 /*
77 * XXX vm/pmap.h should not treat these prototypes as MACH_KERNEL_PRIVATE
78 * when KERNEL is defined.
79 */
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);
83
84 /*
85 * XXX kern/thread.h should not treat these prototypes as MACH_KERNEL_PRIVATE
86 * when KERNEL is defined.
87 */
88 extern kern_return_t thread_setstatus(thread_t thread, int flavor,
89 thread_state_t tstate,
90 mach_msg_type_number_t count);
91
92 extern kern_return_t thread_state_initialize(thread_t thread);
93
94
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 *);
102
103
104 /* An empty load_result_t */
105 static load_result_t load_result_null = {
106 MACH_VM_MIN_ADDRESS,
107 MACH_VM_MIN_ADDRESS,
108 MACH_VM_MIN_ADDRESS,
109 0,
110 0,
111 0,
112 0
113 };
114
115 /*
116 * Prototypes of static functions.
117 */
118 static load_return_t
119 parse_machfile(
120 struct vnode *vp,
121 vm_map_t map,
122 thread_t thr_act,
123 struct mach_header *header,
124 off_t file_offset,
125 off_t macho_size,
126 boolean_t shared_regions,
127 boolean_t clean_regions,
128 int depth,
129 load_result_t *result
130 );
131
132 static load_return_t
133 load_segment(
134 struct segment_command *scp,
135 void * pager,
136 off_t pager_offset,
137 off_t macho_size,
138 off_t end_of_file,
139 vm_map_t map,
140 load_result_t *result
141 );
142
143 static load_return_t
144 load_segment_64(
145 struct segment_command_64 *scp64,
146 void *pager,
147 off_t pager_offset,
148 off_t macho_size,
149 off_t end_of_file,
150 vm_map_t map,
151 load_result_t *result
152 );
153
154 static load_return_t
155 load_unixthread(
156 struct thread_command *tcp,
157 thread_t thr_act,
158 load_result_t *result
159 );
160
161 static load_return_t
162 load_thread(
163 struct thread_command *tcp,
164 thread_t thr_act,
165 load_result_t *result
166 );
167
168 static load_return_t
169 load_threadstate(
170 thread_t thread,
171 unsigned long *ts,
172 unsigned long total_size
173 );
174
175 static load_return_t
176 load_threadstack(
177 thread_t thread,
178 unsigned long *ts,
179 unsigned long total_size,
180 mach_vm_offset_t *user_stack,
181 int *customstack
182 );
183
184 static load_return_t
185 load_threadentry(
186 thread_t thread,
187 unsigned long *ts,
188 unsigned long total_size,
189 mach_vm_offset_t *entry_point
190 );
191
192 static load_return_t
193 load_dylinker(
194 struct dylinker_command *lcp,
195 integer_t archbits,
196 vm_map_t map,
197 thread_t thr_act,
198 int depth,
199 load_result_t *result,
200 boolean_t clean_regions
201 );
202
203 static load_return_t
204 get_macho_vnode(
205 char *path,
206 integer_t archbits,
207 struct mach_header *mach_header,
208 off_t *file_offset,
209 off_t *macho_size,
210 struct vnode **vpp
211 );
212
213 load_return_t
214 load_machfile(
215 struct image_params *imgp,
216 struct mach_header *header,
217 thread_t thr_act,
218 vm_map_t new_map,
219 boolean_t clean_regions,
220 load_result_t *result
221 )
222 {
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;
226
227 pmap_t pmap = 0; /* protected by create_map */
228 vm_map_t map;
229 vm_map_t old_map;
230 load_result_t myresult;
231 load_return_t lret;
232 boolean_t create_map = TRUE;
233
234 if (new_map != VM_MAP_NULL) {
235 create_map = FALSE;
236 }
237
238 if (create_map) {
239 old_map = current_map();
240 #ifdef i386
241 pmap = get_task_pmap(current_task());
242 pmap_reference(pmap);
243 #else
244 pmap = pmap_create((vm_map_size_t) 0);
245 #endif
246 map = vm_map_create(pmap,
247 get_map_min(old_map),
248 get_map_max(old_map),
249 TRUE); /**** FIXME ****/
250 } else
251 map = new_map;
252
253 if (!result)
254 result = &myresult;
255
256 *result = load_result_null;
257
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);
261
262 if (lret != LOAD_SUCCESS) {
263 if (create_map) {
264 vm_map_deallocate(map); /* will lose pmap reference too */
265 }
266 return(lret);
267 }
268
269 /*
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).
274 *
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.
279 */
280 if (create_map) {
281 task_halt(current_task());
282
283 old_map = swap_task_map(current_task(), map);
284 #ifndef i386
285 pmap_switch(pmap); /* Make sure we are using the new pmap */
286 #endif
287 vm_map_deallocate(old_map);
288 }
289 return(LOAD_SUCCESS);
290 }
291
292 int dylink_test = 1;
293
294 /*
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.
302 */
303 static
304 load_return_t
305 parse_machfile(
306 struct vnode *vp,
307 vm_map_t map,
308 thread_t thr_act,
309 struct mach_header *header,
310 off_t file_offset,
311 off_t macho_size,
312 boolean_t shared_regions,
313 boolean_t clean_regions,
314 int depth,
315 load_result_t *result
316 )
317 {
318 uint32_t ncmds;
319 struct load_command *lcp;
320 struct dylinker_command *dlp = 0;
321 integer_t dlarchbits = 0;
322 void * pager;
323 load_return_t ret = LOAD_SUCCESS;
324 caddr_t addr;
325 void * kl_addr;
326 vm_size_t size,kl_size;
327 size_t offset;
328 size_t oldoffset; /* for overflow check */
329 int pass;
330 struct proc *p = current_proc(); /* XXXX */
331 int error;
332 int resid=0;
333 task_t task;
334 size_t mach_header_sz = sizeof(struct mach_header);
335 boolean_t abi64;
336
337 if (header->magic == MH_MAGIC_64 ||
338 header->magic == MH_CIGAM_64) {
339 mach_header_sz = sizeof(struct mach_header_64);
340 }
341
342 /*
343 * Break infinite recursion
344 */
345 if (depth > 6)
346 return(LOAD_FAILURE);
347
348 task = (task_t)get_threadtask(thr_act);
349
350 depth++;
351
352 /*
353 * Check to see if right machine type.
354 */
355 if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != cpu_type()) ||
356 !grade_binary(header->cputype, header->cpusubtype))
357 return(LOAD_BADARCH);
358
359 abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64);
360
361 switch (header->filetype) {
362
363 case MH_OBJECT:
364 case MH_EXECUTE:
365 case MH_PRELOAD:
366 if (depth != 1)
367 return (LOAD_FAILURE);
368 break;
369
370 case MH_FVMLIB:
371 case MH_DYLIB:
372 if (depth == 1)
373 return (LOAD_FAILURE);
374 break;
375
376 case MH_DYLINKER:
377 if (depth != 2)
378 return (LOAD_FAILURE);
379 break;
380
381 default:
382 return (LOAD_FAILURE);
383 }
384
385 /*
386 * Get the pager for the file.
387 */
388 UBCINFOCHECK("parse_machfile", vp);
389 pager = (void *) ubc_getpager(vp);
390
391 /*
392 * Map portion that must be accessible directly into
393 * kernel's map.
394 */
395 if ((mach_header_sz + header->sizeofcmds) > macho_size)
396 return(LOAD_BADMACHO);
397
398 /*
399 * Round size of Mach-O commands up to page boundry.
400 */
401 size = round_page(mach_header_sz + header->sizeofcmds);
402 if (size <= 0)
403 return(LOAD_BADMACHO);
404
405 /*
406 * Map the load commands into kernel memory.
407 */
408 addr = 0;
409 kl_size = size;
410 kl_addr = kalloc(size);
411 addr = (caddr_t)kl_addr;
412 if (addr == NULL)
413 return(LOAD_NOSPACE);
414
415 error = vn_rdwr(UIO_READ, vp, addr, size, file_offset,
416 UIO_SYSSPACE32, 0, kauth_cred_get(), &resid, p);
417 if (error) {
418 if (kl_addr )
419 kfree(kl_addr, kl_size);
420 return(LOAD_IOERROR);
421 }
422 /* (void)ubc_map(vp, PROT_EXEC); */ /* NOT HERE */
423
424 /*
425 * Scan through the commands, processing each one as necessary.
426 */
427 for (pass = 1; pass <= 2; pass++) {
428 /*
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.
433 */
434 offset = mach_header_sz;
435 ncmds = header->ncmds;
436 while (ncmds--) {
437 /*
438 * Get a pointer to the command.
439 */
440 lcp = (struct load_command *)(addr + offset);
441 oldoffset = offset;
442 offset += lcp->cmdsize;
443
444 /*
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.
451 */
452 if (oldoffset > offset ||
453 lcp->cmdsize < sizeof(struct load_command) ||
454 offset > header->sizeofcmds + mach_header_sz) {
455 ret = LOAD_BADMACHO;
456 break;
457 }
458
459 /*
460 * Act on struct load_command's for which kernel
461 * intervention is required.
462 */
463 switch(lcp->cmd) {
464 case LC_SEGMENT_64:
465 if (pass != 1)
466 break;
467 ret = load_segment_64(
468 (struct segment_command_64 *)lcp,
469 pager,
470 file_offset,
471 macho_size,
472 ubc_getsize(vp),
473 map,
474 result);
475 break;
476 case LC_SEGMENT:
477 if (pass != 1)
478 break;
479 ret = load_segment(
480 (struct segment_command *) lcp,
481 pager,
482 file_offset,
483 macho_size,
484 ubc_getsize(vp),
485 map,
486 result);
487 break;
488 case LC_THREAD:
489 if (pass != 2)
490 break;
491 ret = load_thread((struct thread_command *)lcp,
492 thr_act,
493 result);
494 break;
495 case LC_UNIXTHREAD:
496 if (pass != 2)
497 break;
498 ret = load_unixthread(
499 (struct thread_command *) lcp,
500 thr_act,
501 result);
502 break;
503 case LC_LOAD_DYLINKER:
504 if (pass != 2)
505 break;
506 if ((depth == 1) && (dlp == 0)) {
507 dlp = (struct dylinker_command *)lcp;
508 dlarchbits = (header->cputype & CPU_ARCH_MASK);
509 } else {
510 ret = LOAD_FAILURE;
511 }
512 break;
513 default:
514 /* Other commands are ignored by the kernel */
515 ret = LOAD_SUCCESS;
516 break;
517 }
518 if (ret != LOAD_SUCCESS)
519 break;
520 }
521 if (ret != LOAD_SUCCESS)
522 break;
523 }
524 if (ret == LOAD_SUCCESS) {
525
526 if (shared_regions) {
527 vm_offset_t vmaddr;
528 shared_region_mapping_t shared_region;
529 struct shared_region_task_mappings map_info;
530 shared_region_mapping_t next;
531
532 RedoLookup:
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),
544 &(map_info.fs_base),
545 &(map_info.system),
546 &(map_info.flags), &next);
547
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);
558 } else {
559 vm_set_shared_region(task, system_region);
560 }
561 shared_region_mapping_dealloc(
562 (shared_region_mapping_t)map_info.self);
563 goto RedoLookup;
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);
570 } else {
571 shared_region_mapping_dealloc(system_region);
572 }
573 }
574
575 if (dylink_test) {
576 p->p_flag |= P_NOSHLIB; /* no shlibs in use */
577 vmaddr = map_info.client_base;
578 if(clean_regions) {
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);
583 } else {
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);
589 }
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);
595
596 while (next) {
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),
613 &(map_info.fs_base),
614 &(map_info.system),
615 &(map_info.flags), &next);
616
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);
622 }
623 }
624 }
625 if (dlp != 0)
626 ret = load_dylinker(dlp, dlarchbits, map, thr_act, depth, result, clean_regions);
627
628 if(depth == 1) {
629 if (result->thread_count == 0)
630 ret = LOAD_FAILURE;
631 #ifdef __ppc__
632 else if ( abi64 ) {
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);
637 }
638 #endif
639 }
640 }
641
642 if (kl_addr )
643 kfree(kl_addr, kl_size);
644
645 if (ret == LOAD_SUCCESS)
646 (void)ubc_map(vp, PROT_EXEC);
647
648 return(ret);
649 }
650
651 static
652 load_return_t
653 load_segment(
654 struct segment_command *scp,
655 void * pager,
656 off_t pager_offset,
657 off_t macho_size,
658 __unused off_t end_of_file,
659 vm_map_t map,
660 load_result_t *result
661 )
662 {
663 kern_return_t ret;
664 vm_offset_t map_addr, map_offset;
665 vm_size_t map_size, seg_size, delta_size;
666 vm_prot_t initprot;
667 vm_prot_t maxprot;
668
669 /*
670 * Make sure what we get from the file is really ours (as specified
671 * by macho_size).
672 */
673 if (scp->fileoff + scp->filesize > macho_size)
674 return (LOAD_BADMACHO);
675
676 seg_size = round_page(scp->vmsize);
677 if (seg_size == 0)
678 return(KERN_SUCCESS);
679
680 /*
681 * Round sizes to page size.
682 */
683 map_size = round_page(scp->filesize);
684 map_addr = trunc_page(scp->vmaddr);
685
686 map_offset = pager_offset + scp->fileoff;
687
688 if (map_size > 0) {
689 initprot = (scp->initprot) & VM_PROT_ALL;
690 maxprot = (scp->maxprot) & VM_PROT_ALL;
691 /*
692 * Map a copy of the file into the address space.
693 */
694 ret = vm_map(map,
695 &map_addr, map_size, (vm_offset_t)0,
696 VM_FLAGS_FIXED, pager, map_offset, TRUE,
697 initprot, maxprot,
698 VM_INHERIT_DEFAULT);
699 if (ret != KERN_SUCCESS)
700 return(LOAD_NOSPACE);
701
702 /*
703 * If the file didn't end on a page boundary,
704 * we need to zero the leftover.
705 */
706 delta_size = map_size - scp->filesize;
707 #if FIXME
708 if (delta_size > 0) {
709 vm_offset_t tmp;
710
711 ret = vm_allocate(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE);
712 if (ret != KERN_SUCCESS)
713 return(LOAD_RESOURCE);
714
715 if (copyout(tmp, map_addr + scp->filesize,
716 delta_size)) {
717 (void) vm_deallocate(
718 kernel_map, tmp, delta_size);
719 return(LOAD_FAILURE);
720 }
721
722 (void) vm_deallocate(kernel_map, tmp, delta_size);
723 }
724 #endif /* FIXME */
725 }
726
727 /*
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.
731 */
732 delta_size = seg_size - map_size;
733 if (delta_size > 0) {
734 vm_offset_t tmp = map_addr + map_size;
735
736 ret = vm_allocate(map, &tmp, delta_size, VM_FLAGS_FIXED);
737 if (ret != KERN_SUCCESS)
738 return(LOAD_NOSPACE);
739 }
740
741 /*
742 * Set protection values. (Note: ignore errors!)
743 */
744
745 if (scp->maxprot != VM_PROT_DEFAULT) {
746 (void) vm_protect(map,
747 map_addr, seg_size,
748 TRUE, scp->maxprot);
749 }
750 if (scp->initprot != VM_PROT_DEFAULT) {
751 (void) vm_protect(map,
752 map_addr, seg_size,
753 FALSE, scp->initprot);
754 }
755 if ( (scp->fileoff == 0) && (scp->filesize != 0) )
756 result->mach_header = map_addr;
757 return(LOAD_SUCCESS);
758 }
759
760 static
761 load_return_t
762 load_segment_64(
763 struct segment_command_64 *scp64,
764 void * pager,
765 off_t pager_offset,
766 off_t macho_size,
767 __unused off_t end_of_file,
768 vm_map_t map,
769 load_result_t *result
770 )
771 {
772 kern_return_t ret;
773 mach_vm_offset_t map_addr, map_offset;
774 mach_vm_size_t map_size, seg_size, delta_size;
775 vm_prot_t initprot;
776 vm_prot_t maxprot;
777
778 /*
779 * Make sure what we get from the file is really ours (as specified
780 * by macho_size).
781 */
782 if (scp64->fileoff + scp64->filesize > (uint64_t)macho_size)
783 return (LOAD_BADMACHO);
784
785 seg_size = round_page_64(scp64->vmsize);
786 if (seg_size == 0)
787 return(KERN_SUCCESS);
788
789 /*
790 * Round sizes to page size.
791 */
792 map_size = round_page_64(scp64->filesize); /* limited to 32 bits */
793 map_addr = round_page_64(scp64->vmaddr);
794
795 map_offset = pager_offset + scp64->fileoff; /* limited to 32 bits */
796
797 if (map_size > 0) {
798 initprot = (scp64->initprot) & VM_PROT_ALL;
799 maxprot = (scp64->maxprot) & VM_PROT_ALL;
800 /*
801 * Map a copy of the file into the address space.
802 */
803 ret = mach_vm_map(map,
804 &map_addr, map_size, (mach_vm_offset_t)0,
805 VM_FLAGS_FIXED, pager, map_offset, TRUE,
806 initprot, maxprot,
807 VM_INHERIT_DEFAULT);
808 if (ret != KERN_SUCCESS)
809 return(LOAD_NOSPACE);
810
811 /*
812 * If the file didn't end on a page boundary,
813 * we need to zero the leftover.
814 */
815 delta_size = map_size - scp64->filesize;
816 #if FIXME
817 if (delta_size > 0) {
818 mach_vm_offset_t tmp;
819
820 ret = vm_allocate(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE);
821 if (ret != KERN_SUCCESS)
822 return(LOAD_RESOURCE);
823
824 if (copyout(tmp, map_addr + scp64->filesize,
825 delta_size)) {
826 (void) vm_deallocate(
827 kernel_map, tmp, delta_size);
828 return (LOAD_FAILURE);
829 }
830
831 (void) vm_deallocate(kernel_map, tmp, delta_size);
832 }
833 #endif /* FIXME */
834 }
835
836 /*
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.
840 */
841 delta_size = seg_size - map_size;
842 if (delta_size > 0) {
843 mach_vm_offset_t tmp = map_addr + map_size;
844
845 ret = mach_vm_allocate(map, &tmp, delta_size, VM_FLAGS_FIXED);
846 if (ret != KERN_SUCCESS)
847 return(LOAD_NOSPACE);
848 }
849
850 /*
851 * Set protection values. (Note: ignore errors!)
852 */
853
854 if (scp64->maxprot != VM_PROT_DEFAULT) {
855 (void) mach_vm_protect(map,
856 map_addr, seg_size,
857 TRUE, scp64->maxprot);
858 }
859 if (scp64->initprot != VM_PROT_DEFAULT) {
860 (void) mach_vm_protect(map,
861 map_addr, seg_size,
862 FALSE, scp64->initprot);
863 }
864 if ( (scp64->fileoff == 0) && (scp64->filesize != 0) )
865 result->mach_header = map_addr;
866 return(LOAD_SUCCESS);
867 }
868
869 static
870 load_return_t
871 load_thread(
872 struct thread_command *tcp,
873 thread_t thread,
874 load_result_t *result
875 )
876 {
877 kern_return_t kret;
878 load_return_t lret;
879 task_t task;
880 int customstack=0;
881
882 task = get_threadtask(thread);
883
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);
890 }
891
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)
897 return (lret);
898
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),
904 &result->user_stack,
905 &customstack);
906 if (customstack)
907 result->customstack = 1;
908 else
909 result->customstack = 0;
910
911 if (lret != LOAD_SUCCESS)
912 return(lret);
913
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)
920 return(lret);
921 }
922 /*
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.
926 */
927 else
928 thread_resume(thread);
929
930 result->thread_count++;
931
932 return(LOAD_SUCCESS);
933 }
934
935 static
936 load_return_t
937 load_unixthread(
938 struct thread_command *tcp,
939 thread_t thread,
940 load_result_t *result
941 )
942 {
943 load_return_t ret;
944 int customstack =0;
945
946 if (result->thread_count != 0)
947 return (LOAD_FAILURE);
948
949 ret = load_threadstack(thread,
950 (unsigned long *)(((vm_offset_t)tcp) +
951 sizeof(struct thread_command)),
952 tcp->cmdsize - sizeof(struct thread_command),
953 &result->user_stack,
954 &customstack);
955 if (ret != LOAD_SUCCESS)
956 return(ret);
957
958 if (customstack)
959 result->customstack = 1;
960 else
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)
968 return(ret);
969
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)
975 return (ret);
976
977 result->unixproc = TRUE;
978 result->thread_count++;
979
980 return(LOAD_SUCCESS);
981 }
982
983 static
984 load_return_t
985 load_threadstate(
986 thread_t thread,
987 unsigned long *ts,
988 unsigned long total_size
989 )
990 {
991 kern_return_t ret;
992 unsigned long size;
993 int flavor;
994 unsigned long thread_size;
995
996 ret = thread_state_initialize( thread );
997 if (ret != KERN_SUCCESS)
998 return(LOAD_FAILURE);
999
1000 /*
1001 * Set the new thread state; iterate through the state flavors in
1002 * the mach-o file.
1003 */
1004 while (total_size > 0) {
1005 flavor = *ts++;
1006 size = *ts++;
1007 thread_size = (size+2)*sizeof(unsigned long);
1008 if (thread_size > total_size)
1009 return(LOAD_BADMACHO);
1010 total_size -= thread_size;
1011 /*
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.
1015 */
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 *) */
1020 }
1021 return(LOAD_SUCCESS);
1022 }
1023
1024 static
1025 load_return_t
1026 load_threadstack(
1027 thread_t thread,
1028 unsigned long *ts,
1029 unsigned long total_size,
1030 user_addr_t *user_stack,
1031 int *customstack
1032 )
1033 {
1034 kern_return_t ret;
1035 unsigned long size;
1036 int flavor;
1037 unsigned long stack_size;
1038
1039 while (total_size > 0) {
1040 flavor = *ts++;
1041 size = *ts++;
1042 stack_size = (size+2)*sizeof(unsigned long);
1043 if (stack_size > total_size)
1044 return(LOAD_BADMACHO);
1045 total_size -= stack_size;
1046
1047 /*
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.
1051 */
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 *) */
1056 }
1057 return(LOAD_SUCCESS);
1058 }
1059
1060 static
1061 load_return_t
1062 load_threadentry(
1063 thread_t thread,
1064 unsigned long *ts,
1065 unsigned long total_size,
1066 mach_vm_offset_t *entry_point
1067 )
1068 {
1069 kern_return_t ret;
1070 unsigned long size;
1071 int flavor;
1072 unsigned long entry_size;
1073
1074 /*
1075 * Set the thread state.
1076 */
1077 *entry_point = MACH_VM_MIN_ADDRESS;
1078 while (total_size > 0) {
1079 flavor = *ts++;
1080 size = *ts++;
1081 entry_size = (size+2)*sizeof(unsigned long);
1082 if (entry_size > total_size)
1083 return(LOAD_BADMACHO);
1084 total_size -= entry_size;
1085 /*
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.
1089 */
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 *) */
1094 }
1095 return(LOAD_SUCCESS);
1096 }
1097
1098
1099 static
1100 load_return_t
1101 load_dylinker(
1102 struct dylinker_command *lcp,
1103 integer_t archbits,
1104 vm_map_t map,
1105 thread_t thr_act,
1106 int depth,
1107 load_result_t *result,
1108 boolean_t clean_regions
1109 )
1110 {
1111 char *name;
1112 char *p;
1113 struct vnode *vp;
1114 struct mach_header header;
1115 off_t file_offset;
1116 off_t macho_size;
1117 vm_map_t copy_map;
1118 load_result_t myresult;
1119 kern_return_t ret;
1120 vm_map_copy_t tmp;
1121 mach_vm_offset_t dyl_start, map_addr;
1122 mach_vm_size_t dyl_length;
1123
1124 name = (char *)lcp + lcp->name.offset;
1125 /*
1126 * Check for a proper null terminated string.
1127 */
1128 p = name;
1129 do {
1130 if (p >= (char *)lcp + lcp->cmdsize)
1131 return(LOAD_BADMACHO);
1132 } while (*p++);
1133
1134 ret = get_macho_vnode(name, archbits, &header, &file_offset, &macho_size, &vp);
1135 if (ret)
1136 return (ret);
1137
1138 /*
1139 * Load the Mach-O.
1140 * Use a temporary map to do the work.
1141 */
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;
1146 goto out;
1147 }
1148
1149 myresult = load_result_null;
1150
1151 ret = parse_machfile(vp, copy_map, thr_act, &header,
1152 file_offset, macho_size,
1153 FALSE, clean_regions, depth, &myresult);
1154
1155 if (ret)
1156 goto out;
1157
1158 if (get_map_nentries(copy_map) > 0) {
1159
1160 dyl_start = mach_get_vm_start(copy_map);
1161 dyl_length = mach_get_vm_end(copy_map) - dyl_start;
1162
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);
1167 }
1168
1169 if (ret != KERN_SUCCESS) {
1170 ret = LOAD_NOSPACE;
1171 goto out;
1172
1173 }
1174 ret = vm_map_copyin(copy_map,
1175 (vm_map_address_t)dyl_start,
1176 (vm_map_size_t)dyl_length,
1177 TRUE, &tmp);
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),
1182 VM_MAP_NO_FLAGS);
1183 goto out;
1184 }
1185
1186 ret = vm_map_copy_overwrite(map,
1187 (vm_map_address_t)map_addr,
1188 tmp, FALSE);
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),
1194 VM_MAP_NO_FLAGS);
1195 goto out;
1196 }
1197
1198 if (map_addr != dyl_start)
1199 myresult.entry_point += (map_addr - dyl_start);
1200 } else
1201 ret = LOAD_FAILURE;
1202
1203 if (ret == LOAD_SUCCESS) {
1204 result->dynlinker = TRUE;
1205 result->entry_point = myresult.entry_point;
1206 (void)ubc_map(vp, PROT_EXEC);
1207 }
1208 out:
1209 vm_map_deallocate(copy_map);
1210
1211 vnode_put(vp);
1212 return (ret);
1213
1214 }
1215
1216 /*
1217 * This routine exists to support the load_dylinker().
1218 *
1219 * This routine has its own, separate, understanding of the FAT file format,
1220 * which is terrifically unfortunate.
1221 */
1222 static
1223 load_return_t
1224 get_macho_vnode(
1225 char *path,
1226 integer_t archbits,
1227 struct mach_header *mach_header,
1228 off_t *file_offset,
1229 off_t *macho_size,
1230 struct vnode **vpp
1231 )
1232 {
1233 struct vnode *vp;
1234 struct vfs_context context;
1235 struct nameidata nid, *ndp;
1236 struct proc *p = current_proc(); /* XXXX */
1237 boolean_t is_fat;
1238 struct fat_arch fat_arch;
1239 int error = LOAD_SUCCESS;
1240 int resid;
1241 union {
1242 struct mach_header mach_header;
1243 struct fat_header fat_header;
1244 char pad[512];
1245 } header;
1246 off_t fsize = (off_t)0;
1247 struct ucred *cred = kauth_cred_get();
1248 int err2;
1249
1250 context.vc_proc = p;
1251 context.vc_ucred = cred;
1252
1253 ndp = &nid;
1254
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);
1257
1258 if ((error = namei(ndp)) != 0) {
1259 if (error == ENOENT)
1260 error = LOAD_ENOENT;
1261 else
1262 error = LOAD_FAILURE;
1263 return(error);
1264 }
1265 nameidone(ndp);
1266 vp = ndp->ni_vp;
1267
1268 /* check for regular file */
1269 if (vp->v_type != VREG) {
1270 error = LOAD_PROTECT;
1271 goto bad1;
1272 }
1273
1274 /* get size */
1275 if ((error = vnode_size(vp, &fsize, &context)) != 0) {
1276 error = LOAD_FAILURE;
1277 goto bad1;
1278 }
1279
1280 /* Check mount point */
1281 if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
1282 error = LOAD_PROTECT;
1283 goto bad1;
1284 }
1285
1286 /* check access */
1287 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE, &context)) != 0) {
1288 error = LOAD_PROTECT;
1289 goto bad1;
1290 }
1291
1292 /* try to open it */
1293 if ((error = VNOP_OPEN(vp, FREAD, &context)) != 0) {
1294 error = LOAD_PROTECT;
1295 goto bad1;
1296 }
1297
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;
1301 goto bad2;
1302 }
1303
1304 if (header.mach_header.magic == MH_MAGIC ||
1305 header.mach_header.magic == MH_MAGIC_64)
1306 is_fat = FALSE;
1307 else if (header.fat_header.magic == FAT_MAGIC ||
1308 header.fat_header.magic == FAT_CIGAM)
1309 is_fat = TRUE;
1310 else {
1311 error = LOAD_BADMACHO;
1312 goto bad2;
1313 }
1314
1315 if (is_fat) {
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)
1319 goto bad2;
1320
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);
1325 if (error) {
1326 error = LOAD_IOERROR;
1327 goto bad2;
1328 }
1329
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;
1334 goto bad2;
1335 }
1336
1337 *file_offset = fat_arch.offset;
1338 *macho_size = fsize = fat_arch.size;
1339 } else {
1340 /*
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.
1349 */
1350 if ((cpu_type_t)(header.mach_header.cputype & CPU_ARCH_MASK) != archbits)
1351 return(LOAD_BADARCH);
1352
1353 *file_offset = 0;
1354 *macho_size = fsize;
1355 }
1356
1357 *mach_header = header.mach_header;
1358 *vpp = vp;
1359
1360 ubc_setsize(vp, fsize);
1361
1362 return (error);
1363
1364 bad2:
1365 err2 = VNOP_CLOSE(vp, FREAD, &context);
1366 vnode_put(vp);
1367 return (error);
1368
1369 bad1:
1370 vnode_put(vp);
1371 return(error);
1372 }