7
votes

Is there a way to set the heap start address in GCC compiled C program in linux? In x86_64 system,my test program sets the heap address to 4 byte referenced address ( less than FFFFFFFF). I want to set this to 8 byte referenced address for some testing ( > FFFFFFFF). Does GCC provide any way to set the heap start address?

1
Please define what "heap start address" is to you. Most malloc-s are using mmap (and that is subject to ASLR...)Basile Starynkevitch
Why do you ask? Why do you care?Basile Starynkevitch
I am debugging a crash which seems to be happening when heap is more than 4 bytes, I am guessing this could be happening due to some pointer to integer assignment. But my test program always have heap in 4 bytes addressable space.kumar
Can this not be done with the linker?Martin James

1 Answers

6
votes

You can do this a bit indirectly using sbrk():

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
  sbrk(0xFFFFFFFF);
  printf("%p\n", malloc(1));
  return 0;
}

This works by "allocating" 0xFFFFFFFF bytes at the very start, so that the next thing malloc() can allocate is a higher address.