Not exactly kosher, but after digging through the source code for GNU ld it looks like you can specify the address of section after it is defined.
The following linker script should give you the desired behavior:
MEMORY {
ram (rwx) : ORIGIN = 0x10000, LENGTH = 0x0002000
}
SECTIONS {
.bss (NOLOAD) : {
. = ALIGN(4);
_sbss = . ;
_szero = .;
*(.bss .bss.*)
*(COMMON)
/* Note: Just for example - padding .bss by 0x100 bytes so its not empty */
. = 0x100;
. = ALIGN(4);
_ebss = . ;
_ezero = .;
} > ram
/* create .noinit section */
.noinit (NOLOAD): {
. = ALIGN(4);
__noinit_start = .;
/* Note: Just for example - padding .noinit by 0x100 bytes so its not empty */
. = 0x100;
*(.noinit)
__noinit_end = .;
} > ram
/* place .heap after .bss */
.heap _ebss (NOLOAD) : {
. = ALIGN(4);
_sheap = . ;
. = ABSOLUTE(ORIGIN(ram) + LENGTH(ram) - SIZEOF(.noinit));
_eheap = . ;
} > ram
/* update .noinit to be placed at end of heap */
.noinit _eheap (NOLOAD) : { } > ram
}
The output of an empty binary linked with the above script show the correct placement of sections and symbols:
echo | gcc -x c++ - -nostartfiles -T linkerScript.ld -Xlinker --orphan-handling=discard -fuse-ld=bfd && objdump -th a.out
a.out: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .bss 00000100 0000000000010000 0000000000010000 00001000 2**0
ALLOC
1 .noinit 00000100 0000000000011f00 0000000000011f00 00001000 2**0
ALLOC
2 .heap 00001e00 0000000000010100 0000000000010100 00001000 2**0
ALLOC
SYMBOL TABLE:
0000000000010000 l d .bss 0000000000000000 .bss
0000000000011f00 l d .noinit 0000000000000000 .noinit
0000000000010100 l d .heap 0000000000000000 .heap
0000000000010100 g .heap 0000000000000000 _sheap
0000000000010000 g .bss 0000000000000000 _sbss
0000000000010000 g .bss 0000000000000000 _szero
0000000000010100 g .bss 0000000000000000 _ebss
0000000000010100 g .bss 0000000000000000 _ezero
0000000000011f00 g .noinit 0000000000000000 __noinit_start
0000000000012000 g .noinit 0000000000000000 __noinit_end
0000000000011f00 g .heap 0000000000000000 _eheap
Note: I couldn’t find any documentation regarding this behavior so I can’t guarantee this trick will work in future revisions of ld.
Note: This trick doesn’t work for the gold linker.