]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ff6e181a A |
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. | |
1c79356b | 12 | * |
ff6e181a A |
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 | |
1c79356b A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ff6e181a A |
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. | |
1c79356b A |
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 | */ | |
91447636 | 34 | |
1c79356b | 35 | #include <sys/param.h> |
91447636 | 36 | #include <sys/vnode_internal.h> |
1c79356b A |
37 | #include <sys/uio.h> |
38 | #include <sys/namei.h> | |
91447636 A |
39 | #include <sys/proc_internal.h> |
40 | #include <sys/kauth.h> | |
1c79356b A |
41 | #include <sys/stat.h> |
42 | #include <sys/malloc.h> | |
91447636 | 43 | #include <sys/mount_internal.h> |
1c79356b | 44 | #include <sys/fcntl.h> |
91447636 A |
45 | #include <sys/ubc_internal.h> |
46 | #include <sys/imgact.h> | |
1c79356b | 47 | |
1c79356b | 48 | #include <mach/mach_types.h> |
91447636 A |
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> | |
1c79356b | 57 | |
91447636 A |
58 | #include <kern/kern_types.h> |
59 | #include <kern/cpu_number.h> | |
1c79356b | 60 | #include <kern/mach_loader.h> |
91447636 | 61 | #include <kern/kalloc.h> |
55e303ae | 62 | #include <kern/task.h> |
91447636 | 63 | #include <kern/thread.h> |
1c79356b A |
64 | |
65 | #include <mach-o/fat.h> | |
66 | #include <mach-o/loader.h> | |
67 | ||
91447636 | 68 | #include <vm/pmap.h> |
1c79356b A |
69 | #include <vm/vm_map.h> |
70 | #include <vm/vm_kern.h> | |
71 | #include <vm/vm_pager.h> | |
72 | #include <vm/vnode_pager.h> | |
9bccf70c | 73 | #include <vm/vm_shared_memory_server.h> |
91447636 | 74 | #include <vm/vm_protos.h> |
9bccf70c | 75 | |
91447636 A |
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 | }; | |
9bccf70c | 114 | |
1c79356b A |
115 | /* |
116 | * Prototypes of static functions. | |
117 | */ | |
91447636 | 118 | static load_return_t |
1c79356b A |
119 | parse_machfile( |
120 | struct vnode *vp, | |
91447636 A |
121 | vm_map_t map, |
122 | thread_t thr_act, | |
1c79356b | 123 | struct mach_header *header, |
91447636 A |
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 | |
1c79356b A |
133 | load_segment( |
134 | struct segment_command *scp, | |
0b4e3aa0 | 135 | void * pager, |
91447636 A |
136 | off_t pager_offset, |
137 | off_t macho_size, | |
138 | off_t end_of_file, | |
0b4e3aa0 A |
139 | vm_map_t map, |
140 | load_result_t *result | |
91447636 A |
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 | |
1c79356b A |
155 | load_unixthread( |
156 | struct thread_command *tcp, | |
91447636 | 157 | thread_t thr_act, |
0b4e3aa0 | 158 | load_result_t *result |
91447636 A |
159 | ); |
160 | ||
161 | static load_return_t | |
1c79356b A |
162 | load_thread( |
163 | struct thread_command *tcp, | |
91447636 | 164 | thread_t thr_act, |
0b4e3aa0 | 165 | load_result_t *result |
91447636 A |
166 | ); |
167 | ||
168 | static load_return_t | |
1c79356b | 169 | load_threadstate( |
0b4e3aa0 | 170 | thread_t thread, |
1c79356b A |
171 | unsigned long *ts, |
172 | unsigned long total_size | |
91447636 A |
173 | ); |
174 | ||
175 | static load_return_t | |
1c79356b | 176 | load_threadstack( |
0b4e3aa0 | 177 | thread_t thread, |
1c79356b A |
178 | unsigned long *ts, |
179 | unsigned long total_size, | |
91447636 | 180 | mach_vm_offset_t *user_stack, |
0b4e3aa0 | 181 | int *customstack |
91447636 A |
182 | ); |
183 | ||
184 | static load_return_t | |
1c79356b | 185 | load_threadentry( |
0b4e3aa0 | 186 | thread_t thread, |
1c79356b A |
187 | unsigned long *ts, |
188 | unsigned long total_size, | |
91447636 A |
189 | mach_vm_offset_t *entry_point |
190 | ); | |
191 | ||
192 | static load_return_t | |
1c79356b A |
193 | load_dylinker( |
194 | struct dylinker_command *lcp, | |
91447636 | 195 | integer_t archbits, |
0b4e3aa0 | 196 | vm_map_t map, |
91447636 | 197 | thread_t thr_act, |
0b4e3aa0 | 198 | int depth, |
55e303ae A |
199 | load_result_t *result, |
200 | boolean_t clean_regions | |
91447636 A |
201 | ); |
202 | ||
203 | static load_return_t | |
1c79356b | 204 | get_macho_vnode( |
0b4e3aa0 | 205 | char *path, |
91447636 | 206 | integer_t archbits, |
1c79356b | 207 | struct mach_header *mach_header, |
91447636 A |
208 | off_t *file_offset, |
209 | off_t *macho_size, | |
1c79356b A |
210 | struct vnode **vpp |
211 | ); | |
212 | ||
213 | load_return_t | |
214 | load_machfile( | |
91447636 | 215 | struct image_params *imgp, |
1c79356b | 216 | struct mach_header *header, |
91447636 | 217 | thread_t thr_act, |
55e303ae | 218 | vm_map_t new_map, |
91447636 A |
219 | boolean_t clean_regions, |
220 | load_result_t *result | |
1c79356b A |
221 | ) |
222 | { | |
91447636 A |
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 */ | |
1c79356b A |
228 | vm_map_t map; |
229 | vm_map_t old_map; | |
230 | load_result_t myresult; | |
1c79356b | 231 | load_return_t lret; |
0b4e3aa0 A |
232 | boolean_t create_map = TRUE; |
233 | ||
234 | if (new_map != VM_MAP_NULL) { | |
235 | create_map = FALSE; | |
236 | } | |
1c79356b | 237 | |
0b4e3aa0 A |
238 | if (create_map) { |
239 | old_map = current_map(); | |
1c79356b | 240 | #ifdef i386 |
0b4e3aa0 A |
241 | pmap = get_task_pmap(current_task()); |
242 | pmap_reference(pmap); | |
1c79356b | 243 | #else |
91447636 | 244 | pmap = pmap_create((vm_map_size_t) 0); |
1c79356b | 245 | #endif |
0b4e3aa0 A |
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; | |
55e303ae | 252 | |
1c79356b A |
253 | if (!result) |
254 | result = &myresult; | |
255 | ||
91447636 | 256 | *result = load_result_null; |
1c79356b | 257 | |
0b4e3aa0 | 258 | lret = parse_machfile(vp, map, thr_act, header, file_offset, macho_size, |
91447636 A |
259 | ((imgp->ip_flags & IMGPF_IS_64BIT) == 0), /* shared regions? */ |
260 | clean_regions, 0, result); | |
1c79356b A |
261 | |
262 | if (lret != LOAD_SUCCESS) { | |
55e303ae | 263 | if (create_map) { |
0b4e3aa0 | 264 | vm_map_deallocate(map); /* will lose pmap reference too */ |
55e303ae | 265 | } |
1c79356b A |
266 | return(lret); |
267 | } | |
55e303ae | 268 | |
1c79356b A |
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 | * | |
55e303ae A |
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. | |
1c79356b A |
277 | * That lets us get off the pmap associated with it, and |
278 | * then we can release it. | |
279 | */ | |
0b4e3aa0 A |
280 | if (create_map) { |
281 | task_halt(current_task()); | |
1c79356b | 282 | |
0b4e3aa0 | 283 | old_map = swap_task_map(current_task(), map); |
1c79356b | 284 | #ifndef i386 |
0b4e3aa0 | 285 | pmap_switch(pmap); /* Make sure we are using the new pmap */ |
1c79356b | 286 | #endif |
0b4e3aa0 A |
287 | vm_map_deallocate(old_map); |
288 | } | |
1c79356b A |
289 | return(LOAD_SUCCESS); |
290 | } | |
291 | ||
292 | int dylink_test = 1; | |
1c79356b | 293 | |
91447636 A |
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 | */ | |
1c79356b A |
303 | static |
304 | load_return_t | |
305 | parse_machfile( | |
91447636 | 306 | struct vnode *vp, |
1c79356b | 307 | vm_map_t map, |
91447636 | 308 | thread_t thr_act, |
1c79356b | 309 | struct mach_header *header, |
91447636 A |
310 | off_t file_offset, |
311 | off_t macho_size, | |
312 | boolean_t shared_regions, | |
313 | boolean_t clean_regions, | |
1c79356b | 314 | int depth, |
91447636 | 315 | load_result_t *result |
1c79356b A |
316 | ) |
317 | { | |
a3d08fcd | 318 | uint32_t ncmds; |
91447636 | 319 | struct load_command *lcp; |
1c79356b | 320 | struct dylinker_command *dlp = 0; |
91447636 | 321 | integer_t dlarchbits = 0; |
1c79356b | 322 | void * pager; |
55e303ae | 323 | load_return_t ret = LOAD_SUCCESS; |
91447636 A |
324 | caddr_t addr; |
325 | void * kl_addr; | |
1c79356b | 326 | vm_size_t size,kl_size; |
a3d08fcd A |
327 | size_t offset; |
328 | size_t oldoffset; /* for overflow check */ | |
1c79356b A |
329 | int pass; |
330 | struct proc *p = current_proc(); /* XXXX */ | |
331 | int error; | |
332 | int resid=0; | |
0b4e3aa0 | 333 | task_t task; |
91447636 A |
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 | } | |
1c79356b A |
341 | |
342 | /* | |
343 | * Break infinite recursion | |
344 | */ | |
345 | if (depth > 6) | |
346 | return(LOAD_FAILURE); | |
0b4e3aa0 A |
347 | |
348 | task = (task_t)get_threadtask(thr_act); | |
349 | ||
1c79356b A |
350 | depth++; |
351 | ||
352 | /* | |
353 | * Check to see if right machine type. | |
354 | */ | |
91447636 A |
355 | if (((cpu_type_t)(header->cputype & ~CPU_ARCH_MASK) != cpu_type()) || |
356 | !grade_binary(header->cputype, header->cpusubtype)) | |
1c79356b A |
357 | return(LOAD_BADARCH); |
358 | ||
91447636 A |
359 | abi64 = ((header->cputype & CPU_ARCH_ABI64) == CPU_ARCH_ABI64); |
360 | ||
1c79356b A |
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 | */ | |
91447636 | 395 | if ((mach_header_sz + header->sizeofcmds) > macho_size) |
1c79356b A |
396 | return(LOAD_BADMACHO); |
397 | ||
398 | /* | |
399 | * Round size of Mach-O commands up to page boundry. | |
400 | */ | |
91447636 | 401 | size = round_page(mach_header_sz + header->sizeofcmds); |
1c79356b A |
402 | if (size <= 0) |
403 | return(LOAD_BADMACHO); | |
404 | ||
405 | /* | |
406 | * Map the load commands into kernel memory. | |
407 | */ | |
408 | addr = 0; | |
1c79356b A |
409 | kl_size = size; |
410 | kl_addr = kalloc(size); | |
91447636 | 411 | addr = (caddr_t)kl_addr; |
0b4e3aa0 | 412 | if (addr == NULL) |
1c79356b | 413 | return(LOAD_NOSPACE); |
0b4e3aa0 | 414 | |
91447636 A |
415 | error = vn_rdwr(UIO_READ, vp, addr, size, file_offset, |
416 | UIO_SYSSPACE32, 0, kauth_cred_get(), &resid, p); | |
417 | if (error) { | |
0b4e3aa0 A |
418 | if (kl_addr ) |
419 | kfree(kl_addr, kl_size); | |
55e303ae | 420 | return(LOAD_IOERROR); |
1c79356b | 421 | } |
91447636 | 422 | /* (void)ubc_map(vp, PROT_EXEC); */ /* NOT HERE */ |
1c79356b | 423 | |
1c79356b A |
424 | /* |
425 | * Scan through the commands, processing each one as necessary. | |
426 | */ | |
427 | for (pass = 1; pass <= 2; pass++) { | |
a3d08fcd A |
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 | */ | |
91447636 | 434 | offset = mach_header_sz; |
1c79356b A |
435 | ncmds = header->ncmds; |
436 | while (ncmds--) { | |
437 | /* | |
438 | * Get a pointer to the command. | |
439 | */ | |
440 | lcp = (struct load_command *)(addr + offset); | |
a3d08fcd | 441 | oldoffset = offset; |
1c79356b A |
442 | offset += lcp->cmdsize; |
443 | ||
444 | /* | |
a3d08fcd A |
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. | |
1c79356b | 451 | */ |
a3d08fcd A |
452 | if (oldoffset > offset || |
453 | lcp->cmdsize < sizeof(struct load_command) || | |
91447636 A |
454 | offset > header->sizeofcmds + mach_header_sz) { |
455 | ret = LOAD_BADMACHO; | |
a3d08fcd | 456 | break; |
1c79356b A |
457 | } |
458 | ||
459 | /* | |
a3d08fcd A |
460 | * Act on struct load_command's for which kernel |
461 | * intervention is required. | |
1c79356b A |
462 | */ |
463 | switch(lcp->cmd) { | |
91447636 A |
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; | |
1c79356b A |
476 | case LC_SEGMENT: |
477 | if (pass != 1) | |
478 | break; | |
479 | ret = load_segment( | |
480 | (struct segment_command *) lcp, | |
91447636 A |
481 | pager, |
482 | file_offset, | |
1c79356b | 483 | macho_size, |
91447636 | 484 | ubc_getsize(vp), |
1c79356b A |
485 | map, |
486 | result); | |
487 | break; | |
488 | case LC_THREAD: | |
489 | if (pass != 2) | |
490 | break; | |
91447636 A |
491 | ret = load_thread((struct thread_command *)lcp, |
492 | thr_act, | |
1c79356b A |
493 | result); |
494 | break; | |
495 | case LC_UNIXTHREAD: | |
496 | if (pass != 2) | |
497 | break; | |
498 | ret = load_unixthread( | |
91447636 A |
499 | (struct thread_command *) lcp, |
500 | thr_act, | |
1c79356b A |
501 | result); |
502 | break; | |
1c79356b A |
503 | case LC_LOAD_DYLINKER: |
504 | if (pass != 2) | |
505 | break; | |
91447636 | 506 | if ((depth == 1) && (dlp == 0)) { |
1c79356b | 507 | dlp = (struct dylinker_command *)lcp; |
91447636 A |
508 | dlarchbits = (header->cputype & CPU_ARCH_MASK); |
509 | } else { | |
1c79356b | 510 | ret = LOAD_FAILURE; |
91447636 | 511 | } |
1c79356b A |
512 | break; |
513 | default: | |
a3d08fcd A |
514 | /* Other commands are ignored by the kernel */ |
515 | ret = LOAD_SUCCESS; | |
91447636 | 516 | break; |
1c79356b A |
517 | } |
518 | if (ret != LOAD_SUCCESS) | |
519 | break; | |
520 | } | |
521 | if (ret != LOAD_SUCCESS) | |
522 | break; | |
523 | } | |
91447636 A |
524 | if (ret == LOAD_SUCCESS) { |
525 | ||
526 | if (shared_regions) { | |
527 | vm_offset_t vmaddr; | |
1c79356b A |
528 | shared_region_mapping_t shared_region; |
529 | struct shared_region_task_mappings map_info; | |
530 | shared_region_mapping_t next; | |
531 | ||
532 | RedoLookup: | |
0b4e3aa0 | 533 | vm_get_shared_region(task, &shared_region); |
1c79356b A |
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), | |
55e303ae A |
544 | &(map_info.fs_base), |
545 | &(map_info.system), | |
1c79356b A |
546 | &(map_info.flags), &next); |
547 | ||
55e303ae A |
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( | |
1c79356b | 562 | (shared_region_mapping_t)map_info.self); |
55e303ae A |
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 | } | |
1c79356b A |
573 | } |
574 | ||
1c79356b | 575 | if (dylink_test) { |
9bccf70c | 576 | p->p_flag |= P_NOSHLIB; /* no shlibs in use */ |
91447636 | 577 | vmaddr = map_info.client_base; |
55e303ae | 578 | if(clean_regions) { |
91447636 A |
579 | vm_map(map, &vmaddr, map_info.text_size, |
580 | 0, SHARED_LIB_ALIAS|VM_FLAGS_FIXED, | |
55e303ae A |
581 | map_info.text_region, 0, FALSE, |
582 | VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE); | |
583 | } else { | |
91447636 | 584 | vm_map(map, &vmaddr, map_info.text_size, 0, |
1c79356b | 585 | (VM_MEMORY_SHARED_PMAP << 24) |
91447636 | 586 | | SHARED_LIB_ALIAS | VM_FLAGS_FIXED, |
1c79356b A |
587 | map_info.text_region, 0, FALSE, |
588 | VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE); | |
55e303ae | 589 | } |
91447636 A |
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, | |
1c79356b A |
593 | map_info.data_region, 0, TRUE, |
594 | VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE); | |
55e303ae A |
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 | ||
91447636 A |
617 | vmaddr = map_info.client_base; |
618 | vm_map(map, &vmaddr, map_info.text_size, | |
619 | 0, SHARED_LIB_ALIAS | VM_FLAGS_FIXED, | |
55e303ae A |
620 | map_info.text_region, 0, FALSE, |
621 | VM_PROT_READ, VM_PROT_READ, VM_INHERIT_SHARE); | |
622 | } | |
1c79356b | 623 | } |
91447636 A |
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 | } | |
1c79356b A |
640 | } |
641 | ||
0b4e3aa0 A |
642 | if (kl_addr ) |
643 | kfree(kl_addr, kl_size); | |
644 | ||
1c79356b | 645 | if (ret == LOAD_SUCCESS) |
91447636 | 646 | (void)ubc_map(vp, PROT_EXEC); |
1c79356b A |
647 | |
648 | return(ret); | |
649 | } | |
650 | ||
651 | static | |
652 | load_return_t | |
653 | load_segment( | |
654 | struct segment_command *scp, | |
655 | void * pager, | |
91447636 A |
656 | off_t pager_offset, |
657 | off_t macho_size, | |
658 | __unused off_t end_of_file, | |
1c79356b A |
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; | |
1c79356b A |
666 | vm_prot_t initprot; |
667 | vm_prot_t maxprot; | |
1c79356b A |
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 | ||
91447636 | 676 | seg_size = round_page(scp->vmsize); |
1c79356b A |
677 | if (seg_size == 0) |
678 | return(KERN_SUCCESS); | |
679 | ||
680 | /* | |
681 | * Round sizes to page size. | |
682 | */ | |
91447636 A |
683 | map_size = round_page(scp->filesize); |
684 | map_addr = trunc_page(scp->vmaddr); | |
1c79356b A |
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, | |
91447636 A |
695 | &map_addr, map_size, (vm_offset_t)0, |
696 | VM_FLAGS_FIXED, pager, map_offset, TRUE, | |
1c79356b A |
697 | initprot, maxprot, |
698 | VM_INHERIT_DEFAULT); | |
699 | if (ret != KERN_SUCCESS) | |
700 | return(LOAD_NOSPACE); | |
701 | ||
1c79356b A |
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 | ||
91447636 | 711 | ret = vm_allocate(kernel_map, &tmp, delta_size, VM_FLAGS_ANYWHERE); |
1c79356b A |
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 | ||
91447636 | 736 | ret = vm_allocate(map, &tmp, delta_size, VM_FLAGS_FIXED); |
1c79356b A |
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 | |
91447636 A |
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, | |
1c79356b A |
769 | load_result_t *result |
770 | ) | |
771 | { | |
91447636 A |
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; | |
1c79356b | 777 | |
91447636 A |
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); | |
1c79356b | 828 | return (LOAD_FAILURE); |
91447636 | 829 | } |
1c79356b | 830 | |
91447636 A |
831 | (void) vm_deallocate(kernel_map, tmp, delta_size); |
832 | } | |
833 | #endif /* FIXME */ | |
834 | } | |
1c79356b | 835 | |
91447636 A |
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; | |
1c79356b | 844 | |
91447636 A |
845 | ret = mach_vm_allocate(map, &tmp, delta_size, VM_FLAGS_FIXED); |
846 | if (ret != KERN_SUCCESS) | |
847 | return(LOAD_NOSPACE); | |
848 | } | |
1c79356b | 849 | |
91447636 A |
850 | /* |
851 | * Set protection values. (Note: ignore errors!) | |
852 | */ | |
1c79356b | 853 | |
91447636 A |
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; | |
1c79356b A |
866 | return(LOAD_SUCCESS); |
867 | } | |
868 | ||
869 | static | |
870 | load_return_t | |
871 | load_thread( | |
872 | struct thread_command *tcp, | |
91447636 | 873 | thread_t thread, |
1c79356b A |
874 | load_result_t *result |
875 | ) | |
876 | { | |
1c79356b A |
877 | kern_return_t kret; |
878 | load_return_t lret; | |
0b4e3aa0 A |
879 | task_t task; |
880 | int customstack=0; | |
1c79356b | 881 | |
55e303ae | 882 | task = get_threadtask(thread); |
0b4e3aa0 A |
883 | |
884 | /* if count is 0; same as thr_act */ | |
885 | if (result->thread_count != 0) { | |
886 | kret = thread_create(task, &thread); | |
1c79356b A |
887 | if (kret != KERN_SUCCESS) |
888 | return(LOAD_RESOURCE); | |
91447636 | 889 | thread_deallocate(thread); |
1c79356b A |
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) { | |
0b4e3aa0 | 900 | lret = load_threadstack(thread, |
1c79356b A |
901 | (unsigned long *)(((vm_offset_t)tcp) + |
902 | sizeof(struct thread_command)), | |
903 | tcp->cmdsize - sizeof(struct thread_command), | |
0b4e3aa0 A |
904 | &result->user_stack, |
905 | &customstack); | |
906 | if (customstack) | |
907 | result->customstack = 1; | |
908 | else | |
909 | result->customstack = 0; | |
910 | ||
1c79356b A |
911 | if (lret != LOAD_SUCCESS) |
912 | return(lret); | |
913 | ||
0b4e3aa0 | 914 | lret = load_threadentry(thread, |
1c79356b A |
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 | ||
91447636 A |
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 | ||
1c79356b A |
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; | |
91447636 | 994 | unsigned long thread_size; |
1c79356b | 995 | |
91447636 A |
996 | ret = thread_state_initialize( thread ); |
997 | if (ret != KERN_SUCCESS) | |
998 | return(LOAD_FAILURE); | |
999 | ||
1c79356b | 1000 | /* |
91447636 A |
1001 | * Set the new thread state; iterate through the state flavors in |
1002 | * the mach-o file. | |
1c79356b | 1003 | */ |
1c79356b A |
1004 | while (total_size > 0) { |
1005 | flavor = *ts++; | |
1006 | size = *ts++; | |
91447636 A |
1007 | thread_size = (size+2)*sizeof(unsigned long); |
1008 | if (thread_size > total_size) | |
1c79356b | 1009 | return(LOAD_BADMACHO); |
91447636 A |
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); | |
1c79356b A |
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, | |
91447636 | 1030 | user_addr_t *user_stack, |
0b4e3aa0 | 1031 | int *customstack |
1c79356b A |
1032 | ) |
1033 | { | |
1034 | kern_return_t ret; | |
1035 | unsigned long size; | |
1036 | int flavor; | |
91447636 | 1037 | unsigned long stack_size; |
1c79356b | 1038 | |
1c79356b A |
1039 | while (total_size > 0) { |
1040 | flavor = *ts++; | |
1041 | size = *ts++; | |
91447636 A |
1042 | stack_size = (size+2)*sizeof(unsigned long); |
1043 | if (stack_size > total_size) | |
1c79356b | 1044 | return(LOAD_BADMACHO); |
91447636 A |
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); | |
1c79356b A |
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, | |
91447636 | 1066 | mach_vm_offset_t *entry_point |
1c79356b A |
1067 | ) |
1068 | { | |
1069 | kern_return_t ret; | |
1070 | unsigned long size; | |
1071 | int flavor; | |
91447636 | 1072 | unsigned long entry_size; |
1c79356b A |
1073 | |
1074 | /* | |
1075 | * Set the thread state. | |
1076 | */ | |
91447636 | 1077 | *entry_point = MACH_VM_MIN_ADDRESS; |
1c79356b A |
1078 | while (total_size > 0) { |
1079 | flavor = *ts++; | |
1080 | size = *ts++; | |
91447636 A |
1081 | entry_size = (size+2)*sizeof(unsigned long); |
1082 | if (entry_size > total_size) | |
1c79356b | 1083 | return(LOAD_BADMACHO); |
91447636 A |
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); | |
1c79356b A |
1091 | if (ret != KERN_SUCCESS) |
1092 | return(LOAD_FAILURE); | |
1093 | ts += size; /* ts is a (unsigned long *) */ | |
1094 | } | |
1095 | return(LOAD_SUCCESS); | |
1096 | } | |
1097 | ||
1c79356b A |
1098 | |
1099 | static | |
1100 | load_return_t | |
1101 | load_dylinker( | |
1102 | struct dylinker_command *lcp, | |
91447636 | 1103 | integer_t archbits, |
1c79356b | 1104 | vm_map_t map, |
91447636 | 1105 | thread_t thr_act, |
1c79356b | 1106 | int depth, |
55e303ae A |
1107 | load_result_t *result, |
1108 | boolean_t clean_regions | |
1c79356b A |
1109 | ) |
1110 | { | |
1111 | char *name; | |
1112 | char *p; | |
1113 | struct vnode *vp; | |
1114 | struct mach_header header; | |
91447636 A |
1115 | off_t file_offset; |
1116 | off_t macho_size; | |
1c79356b A |
1117 | vm_map_t copy_map; |
1118 | load_result_t myresult; | |
1119 | kern_return_t ret; | |
1120 | vm_map_copy_t tmp; | |
91447636 A |
1121 | mach_vm_offset_t dyl_start, map_addr; |
1122 | mach_vm_size_t dyl_length; | |
1c79356b A |
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 | ||
91447636 | 1134 | ret = get_macho_vnode(name, archbits, &header, &file_offset, &macho_size, &vp); |
1c79356b A |
1135 | if (ret) |
1136 | return (ret); | |
1137 | ||
1c79356b A |
1138 | /* |
1139 | * Load the Mach-O. | |
91447636 | 1140 | * Use a temporary map to do the work. |
1c79356b | 1141 | */ |
91447636 A |
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; | |
1c79356b | 1150 | |
0b4e3aa0 | 1151 | ret = parse_machfile(vp, copy_map, thr_act, &header, |
1c79356b | 1152 | file_offset, macho_size, |
91447636 | 1153 | FALSE, clean_regions, depth, &myresult); |
1c79356b A |
1154 | |
1155 | if (ret) | |
1156 | goto out; | |
1157 | ||
1158 | if (get_map_nentries(copy_map) > 0) { | |
1159 | ||
91447636 A |
1160 | dyl_start = mach_get_vm_start(copy_map); |
1161 | dyl_length = mach_get_vm_end(copy_map) - dyl_start; | |
1c79356b A |
1162 | |
1163 | map_addr = dyl_start; | |
91447636 | 1164 | ret = mach_vm_allocate(map, &map_addr, dyl_length, VM_FLAGS_FIXED); |
1c79356b | 1165 | if (ret != KERN_SUCCESS) { |
91447636 | 1166 | ret = mach_vm_allocate(map, &map_addr, dyl_length, VM_FLAGS_ANYWHERE); |
1c79356b A |
1167 | } |
1168 | ||
1169 | if (ret != KERN_SUCCESS) { | |
1170 | ret = LOAD_NOSPACE; | |
1171 | goto out; | |
1172 | ||
1173 | } | |
91447636 A |
1174 | ret = vm_map_copyin(copy_map, |
1175 | (vm_map_address_t)dyl_start, | |
1176 | (vm_map_size_t)dyl_length, | |
1177 | TRUE, &tmp); | |
1c79356b A |
1178 | if (ret != KERN_SUCCESS) { |
1179 | (void) vm_map_remove(map, | |
91447636 A |
1180 | vm_map_trunc_page(map_addr), |
1181 | vm_map_round_page(map_addr + dyl_length), | |
1182 | VM_MAP_NO_FLAGS); | |
1c79356b A |
1183 | goto out; |
1184 | } | |
1185 | ||
91447636 A |
1186 | ret = vm_map_copy_overwrite(map, |
1187 | (vm_map_address_t)map_addr, | |
1188 | tmp, FALSE); | |
1c79356b | 1189 | if (ret != KERN_SUCCESS) { |
91447636 A |
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 | } | |
1c79356b A |
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; | |
91447636 | 1206 | (void)ubc_map(vp, PROT_EXEC); |
1c79356b A |
1207 | } |
1208 | out: | |
1209 | vm_map_deallocate(copy_map); | |
1210 | ||
91447636 | 1211 | vnode_put(vp); |
1c79356b A |
1212 | return (ret); |
1213 | ||
1214 | } | |
1215 | ||
91447636 A |
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 | */ | |
1c79356b A |
1222 | static |
1223 | load_return_t | |
1224 | get_macho_vnode( | |
1225 | char *path, | |
91447636 | 1226 | integer_t archbits, |
1c79356b | 1227 | struct mach_header *mach_header, |
91447636 A |
1228 | off_t *file_offset, |
1229 | off_t *macho_size, | |
1c79356b A |
1230 | struct vnode **vpp |
1231 | ) | |
1232 | { | |
1233 | struct vnode *vp; | |
91447636 | 1234 | struct vfs_context context; |
1c79356b A |
1235 | struct nameidata nid, *ndp; |
1236 | struct proc *p = current_proc(); /* XXXX */ | |
1237 | boolean_t is_fat; | |
1238 | struct fat_arch fat_arch; | |
55e303ae | 1239 | int error = LOAD_SUCCESS; |
1c79356b A |
1240 | int resid; |
1241 | union { | |
1242 | struct mach_header mach_header; | |
1243 | struct fat_header fat_header; | |
1244 | char pad[512]; | |
1245 | } header; | |
0b4e3aa0 | 1246 | off_t fsize = (off_t)0; |
91447636 | 1247 | struct ucred *cred = kauth_cred_get(); |
55e303ae | 1248 | int err2; |
1c79356b | 1249 | |
91447636 A |
1250 | context.vc_proc = p; |
1251 | context.vc_ucred = cred; | |
1252 | ||
1c79356b | 1253 | ndp = &nid; |
1c79356b A |
1254 | |
1255 | /* init the namei data to point the file user's program name */ | |
91447636 | 1256 | NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE32, CAST_USER_ADDR_T(path), &context); |
1c79356b | 1257 | |
91447636 | 1258 | if ((error = namei(ndp)) != 0) { |
55e303ae A |
1259 | if (error == ENOENT) |
1260 | error = LOAD_ENOENT; | |
1261 | else | |
1262 | error = LOAD_FAILURE; | |
1c79356b | 1263 | return(error); |
55e303ae | 1264 | } |
91447636 | 1265 | nameidone(ndp); |
1c79356b A |
1266 | vp = ndp->ni_vp; |
1267 | ||
1268 | /* check for regular file */ | |
1269 | if (vp->v_type != VREG) { | |
55e303ae | 1270 | error = LOAD_PROTECT; |
1c79356b A |
1271 | goto bad1; |
1272 | } | |
1273 | ||
91447636 A |
1274 | /* get size */ |
1275 | if ((error = vnode_size(vp, &fsize, &context)) != 0) { | |
55e303ae | 1276 | error = LOAD_FAILURE; |
1c79356b | 1277 | goto bad1; |
55e303ae | 1278 | } |
1c79356b A |
1279 | |
1280 | /* Check mount point */ | |
1281 | if (vp->v_mount->mnt_flag & MNT_NOEXEC) { | |
55e303ae | 1282 | error = LOAD_PROTECT; |
1c79356b A |
1283 | goto bad1; |
1284 | } | |
1285 | ||
91447636 A |
1286 | /* check access */ |
1287 | if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_EXECUTE, &context)) != 0) { | |
55e303ae | 1288 | error = LOAD_PROTECT; |
1c79356b | 1289 | goto bad1; |
55e303ae | 1290 | } |
0b4e3aa0 | 1291 | |
1c79356b | 1292 | /* try to open it */ |
91447636 | 1293 | if ((error = VNOP_OPEN(vp, FREAD, &context)) != 0) { |
55e303ae | 1294 | error = LOAD_PROTECT; |
1c79356b | 1295 | goto bad1; |
0b4e3aa0 A |
1296 | } |
1297 | ||
91447636 A |
1298 | if ((error = vn_rdwr(UIO_READ, vp, (caddr_t)&header, sizeof(header), 0, |
1299 | UIO_SYSSPACE32, IO_NODELOCKED, cred, &resid, p)) != 0) { | |
55e303ae | 1300 | error = LOAD_IOERROR; |
1c79356b | 1301 | goto bad2; |
55e303ae | 1302 | } |
1c79356b | 1303 | |
91447636 A |
1304 | if (header.mach_header.magic == MH_MAGIC || |
1305 | header.mach_header.magic == MH_MAGIC_64) | |
1c79356b A |
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) { | |
0b4e3aa0 | 1316 | /* Look up our architecture in the fat file. */ |
91447636 | 1317 | error = fatfile_getarch_with_bits(vp, archbits, (vm_offset_t)(&header.fat_header), &fat_arch); |
0b4e3aa0 | 1318 | if (error != LOAD_SUCCESS) |
1c79356b | 1319 | goto bad2; |
0b4e3aa0 A |
1320 | |
1321 | /* Read the Mach-O header out of it */ | |
55e303ae | 1322 | error = vn_rdwr(UIO_READ, vp, (caddr_t)&header.mach_header, |
1c79356b | 1323 | sizeof(header.mach_header), fat_arch.offset, |
91447636 | 1324 | UIO_SYSSPACE32, IO_NODELOCKED, cred, &resid, p); |
1c79356b | 1325 | if (error) { |
55e303ae | 1326 | error = LOAD_IOERROR; |
1c79356b A |
1327 | goto bad2; |
1328 | } | |
1329 | ||
0b4e3aa0 | 1330 | /* Is this really a Mach-O? */ |
91447636 A |
1331 | if (header.mach_header.magic != MH_MAGIC && |
1332 | header.mach_header.magic != MH_MAGIC_64) { | |
1c79356b A |
1333 | error = LOAD_BADMACHO; |
1334 | goto bad2; | |
1335 | } | |
0b4e3aa0 | 1336 | |
1c79356b | 1337 | *file_offset = fat_arch.offset; |
0b4e3aa0 | 1338 | *macho_size = fsize = fat_arch.size; |
1c79356b | 1339 | } else { |
91447636 A |
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); | |
0b4e3aa0 | 1352 | |
1c79356b | 1353 | *file_offset = 0; |
91447636 | 1354 | *macho_size = fsize; |
1c79356b A |
1355 | } |
1356 | ||
0b4e3aa0 A |
1357 | *mach_header = header.mach_header; |
1358 | *vpp = vp; | |
91447636 A |
1359 | |
1360 | ubc_setsize(vp, fsize); | |
0b4e3aa0 | 1361 | |
0b4e3aa0 A |
1362 | return (error); |
1363 | ||
1c79356b | 1364 | bad2: |
91447636 A |
1365 | err2 = VNOP_CLOSE(vp, FREAD, &context); |
1366 | vnode_put(vp); | |
1c79356b | 1367 | return (error); |
0b4e3aa0 | 1368 | |
1c79356b | 1369 | bad1: |
91447636 | 1370 | vnode_put(vp); |
1c79356b A |
1371 | return(error); |
1372 | } |