vm_prot_t vmprot, max_vmprot;
memory_object_t memobj;
vm_address_t mapaddr, mask;
- boolean_t copy;
+ boolean_t copy, anywhere;
mapaddr = (vm_address_t) addr;
vmprot |= VM_PROT_EXECUTE;
copy = ! (flags & MAP_SHARED);
+ anywhere = ! (flags & MAP_FIXED);
#ifdef __LP64__
if ((addr == NULL) && (prot & PROT_EXEC)
if (copy)
max_vmprot = VM_PROT_ALL;
+ /* When ANYWHERE is true but the caller has provided a preferred address,
+ try mapping at that address with anywhere = 0 first. If this fails,
+ we'll retry with anywhere = 1 below. */
err = __vm_map (__mach_task_self (),
&mapaddr, (vm_size_t) len, mask,
- mapaddr == 0,
+ anywhere && (mapaddr == 0),
memobj, (vm_offset_t) offset,
copy, vmprot, max_vmprot,
copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE);
}
else
{
+ /* This mmap call is allowed to allocate anywhere, */
if (mapaddr != 0 && (err == KERN_NO_SPACE || err == KERN_INVALID_ADDRESS))
+ /* ...but above, we tried allocating at the specific address,
+ and failed to. Now try again, with anywhere = 1 this time. */
err = __vm_map (__mach_task_self (),
&mapaddr, (vm_size_t) len, mask,
1, memobj, (vm_offset_t) offset,