0

I have a question about the behavior of the linker script found in this question:

https://stackoverflow.com/a/55193198/2421349

To save you a click, the relavant portion is:

OUTPUT_ARCH(riscv)
MEMORY
{
   /* qemu-system-risc64 virt machine */
   RAM (rwx)  : ORIGIN = 0x80000000, LENGTH = 128M 
}
ENTRY(_start)

And in a later section:

 PROVIDE (__executable_start = SEGMENT_START("text-segment", ORIGIN(RAM)));
 . = SEGMENT_START("text-segment", ORIGIN(RAM)) + SIZEOF_HEADERS;
 PROVIDE(__stack_top = ORIGIN(RAM) + LENGTH(RAM));

We set __executable_start to begin at ORIGIN(RAM). Then we use the . command to move the linker output location SIZEOF_HEADERS bytes forward. And finally we set __stack_top = ORIGIN(RAM) + LENGTH(RAM).

Assuming the stack grows down towards ORIGIN(RAM), won't it eventually overwrite __executable_start and whatever SIZEOF_HEADERS is if the stack grows large enough?

kibowki
  • 4,206
  • 16
  • 48
  • 74

1 Answers1

0

Yes, if the stack grows large enough, it will eventually start overwriting parts of the memory it should not. But this is not specific to this linker script: Ultimately, the memory is a finite resource, and any stack growing too much because too large automatic variables beeing allocated, and/or out of control recursive calls will end up causing problems.

Frant
  • 5,382
  • 1
  • 16
  • 22