cctools - Add map-fd support where needed.
[darwin-xtools.git] / cctools / ld / cctools_map_fd.c
blobf2e73926a66e66c5cef2ec842c2258459c02f9ea
1 /* Iain Sandoe, Oct 2015. */
3 #include "cctools_map_fd.h"
4 #include <sys/mman.h>
6 /* The interface is originally defined in libc.h */
8 /* The actual code for the map_fd syscall is in kern_mman.c in xnu.
9 - replace it with a call to mmap. */
11 kern_return_t
12 map_fd(int fd, vm_offset_t offset, vm_offset_t *addr,
13 boolean_t find_space, vm_size_t numbytes)
15 void *ret_addr = (void *)0; /* default, let the system pick the adddress. */
16 int prot = PROT_READ | PROT_WRITE;
17 int flags = MAP_FILE | MAP_PRIVATE;
19 if (! find_space) {
20 /* Use the address specified.
21 At present, this is not used by cctools - and presumably the code will
22 be updated on some future drop to remove it. */
23 ret_addr = (void *) *addr;
24 flags |= MAP_FIXED;
27 ret_addr = mmap(ret_addr, numbytes, prot, flags, fd, offset);
29 /* mmp either returns MAP_FAILED, or the result. */
30 if (ret_addr == MAP_FAILED)
31 return KERN_FAILURE;
33 *addr = (vm_offset_t)ret_addr;
34 return KERN_SUCCESS;