I have a program that mmaps memory at higher addresses using MAP_FIXED
at TASK_SIZE - PAGE_SIZE
.
This program runs fine if I execute it, but if I run it with gdb
, it segfaults just after the mmap
. Also at this point, the gdb state seems to be completely corrupted and it appears that the execution reaches an address range filled with 0's
(could be from the new mappings just created).
Does gdb
use this address range in the running process? Have I cleared out some of gdb's state? Is this address range documented somewhere?
Following is my call to mmap
and the address calculation -
#define TASK_SIZE64 (0x800000000000UL - 4096)
#define TASK_SIZE TASK_SIZE64
#define PAGE_OFFSET (void*)TASK_SIZE
...
char *load_address = PAGE_OFFSET - file_size_aligned;
if(load_address != mmap(load_address, file_size_aligned, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0)){
err("Failed to allocate memory for raw_binary with: %d\n", errno);
return -1;
}
file_size_aligned
comes to a PAGE_SIZE
. This is one of the allocations. There is one more that starts from load_address and allocates few more pages backwards (with PROT_READ
and PROT_WRITE
only).