[lld][ELF] vma gets bumped with MEMORY command / TBSS sections
The below test when built using lld, bumps the VMA by the size of the tbss section.
` cat > 1.c << ! __thread int a = 0; int b = 10; int data = 100; int foo() { return a + b; } !
cat > script.t << ! MEMORY { MYMEM (rwx) : ORIGIN = 0x1000, LENGTH = 0x80000 }
SECTIONS { .foo : { *(.text.foo) } > MYMEM .tbss.a : { *(.tbss.a) } > MYMEM .bss.b : { *(.data.b) } > MYMEM .data : { (.data) } > MYMEM } !
CC=clang CCOPTS="-target riscv32" LD=ld.lld $CC $CCOPTS -c 1.c -ffunction-sections -fdata-sections -G0 -fno-asynchronous-unwind-tables $LD -m elf32lriscv 1.o -T script.t `
Section dump :-
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .foo PROGBITS 00001000 001000 00001e 00 AX 0 0 2 [ 2] .text PROGBITS 0000101e 00101e 000000 00 AX 0 0 2 [ 3] .tbss.a NOBITS 00001020 001020 000004 00 WAT 0 0 4 [ 4] .bss.b PROGBITS 00001024 001024 000004 00 WA 0 0 4 [ 5] .data PROGBITS 00001028 001028 000004 00 WA 0 0 4 [ 6] .comment PROGBITS 00000000 00102c 000038 01 MS 0 0 1 [ 7] .riscv.attributes RISCV_ATTRIBUTES 00000000 001064 00002b 00 0 0 1 [ 8] .symtab SYMTAB 00000000 001090 0000c0 10 10 8 4 [ 9] .shstrtab STRTAB 00000000 001150 000056 00 0 0 1 [10] .strtab STRTAB 00000000 0011a6 000030 00 0 0 1
If you see the VMA address of .bss.b, it is bumped by the size of the .tbss.a section.
This happens only when users use the MEMORY command.
GNU linker layout
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .foo PROGBITS 00001000 001000 00001c 00 AX 0 0 2 [ 2] .tbss.a NOBITS 0000101c 00101c 000004 00 WAT 0 0 4 [ 3] .bss.b PROGBITS 0000101c 00101c 000004 00 WA 0 0 4 [ 4] .data PROGBITS 00001020 001020 000004 00 WA 0 0 4 [ 5] .comment PROGBITS 00000000 001024 000024 01 MS 0 0 1 [ 6] .riscv.attributes RISCV_ATTRIBUTES 00000000 001048 00002b 00 0 0 1 [ 7] .symtab SYMTAB 00000000 001074 000120 10 8 14 4 [ 8] .strtab STRTAB 00000000 001194 00002e 00 0 0 1 [ 9] .shstrtab STRTAB 00000000 0011c2 000050 00 0 0 1
@llvm/issue-subscribers-lld-elf
Author: None (shankarke)
` cat > 1.c << ! __thread int a = 0; int b = 10; int data = 100; int foo() { return a + b; } !
cat > script.t << ! MEMORY { MYMEM (rwx) : ORIGIN = 0x1000, LENGTH = 0x80000 }
SECTIONS { .foo : { *(.text.foo) } > MYMEM .tbss.a : { *(.tbss.a) } > MYMEM .bss.b : { *(.data.b) } > MYMEM .data : { (.data) } > MYMEM } !
CC=clang CCOPTS="-target riscv32" LD=ld.lld $CC $CCOPTS -c 1.c -ffunction-sections -fdata-sections -G0 -fno-asynchronous-unwind-tables $LD -m elf32lriscv 1.o -T script.t `
Section dump :-
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .foo PROGBITS 00001000 001000 00001e 00 AX 0 0 2 [ 2] .text PROGBITS 0000101e 00101e 000000 00 AX 0 0 2 [ 3] .tbss.a NOBITS 00001020 001020 000004 00 WAT 0 0 4 [ 4] .bss.b PROGBITS 00001024 001024 000004 00 WA 0 0 4 [ 5] .data PROGBITS 00001028 001028 000004 00 WA 0 0 4 [ 6] .comment PROGBITS 00000000 00102c 000038 01 MS 0 0 1 [ 7] .riscv.attributes RISCV_ATTRIBUTES 00000000 001064 00002b 00 0 0 1 [ 8] .symtab SYMTAB 00000000 001090 0000c0 10 10 8 4 [ 9] .shstrtab STRTAB 00000000 001150 000056 00 0 0 1 [10] .strtab STRTAB 00000000 0011a6 000030 00 0 0 1
If you see the VMA address of .bss.b, it is bumped by the size of the .tbss.a section.
This happens only when users use the MEMORY command.
GNU linker layout
Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .foo PROGBITS 00001000 001000 00001c 00 AX 0 0 2 [ 2] .tbss.a NOBITS 0000101c 00101c 000004 00 WAT 0 0 4 [ 3] .bss.b PROGBITS 0000101c 00101c 000004 00 WA 0 0 4 [ 4] .data PROGBITS 00001020 001020 000004 00 WA 0 0 4 [ 5] .comment PROGBITS 00000000 001024 000024 01 MS 0 0 1 [ 6] .riscv.attributes RISCV_ATTRIBUTES 00000000 001048 00002b 00 0 0 1 [ 7] .symtab SYMTAB 00000000 001074 000120 10 8 14 4 [ 8] .strtab STRTAB 00000000 001194 00002e 00 0 0 1 [ 9] .shstrtab STRTAB 00000000 0011c2 000050 00 0 0 1