Does the stack in this 'ld' linker script overwrite the stored executable?

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?

gnu
ld
riscv
asked on Stack Overflow Apr 7, 2019 by twilco • edited Apr 8, 2019 by twilco

1 Answer

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.

answered on Stack Overflow Apr 8, 2019 by Frant • edited Apr 8, 2019 by Frant

User contributions licensed under CC BY-SA 3.0