Take the following example
reprod.c
int __thread a;
int b,c,k=100,n=400;
int my_start()
{
b = 500;
return a*a*b*k*n;
}
l.ld
ENTRY(my_start)
SECTIONS
{
.text (VMA_START) : AT(VMA_START + LMA_START)
{
*(.text.*)
}
. = ALIGN(16);
.data :
{
*(.data)
}
. = ALIGN(16);
.tdata : {
*(.tdata) *(.tdata.*)
}
. = ALIGN(16);
.tbss : {
*(.tbss) *(.tbss.*)
}
. = ALIGN(16);
.sdata :
{BYTE(0x00)
*(.sdata) *(.sdata.*)
}
. = ALIGN(16);
}
clang reprod.c -ffreestanding -c -o reprod.img
ld.lld --defsym VMA_START=0xffffffff80100000 --defsym LMA_START=0x4800000
-T l.ld reprod.img -o reprod.img
objcopy --change-section-lma .*-0xffffffff80000000 reprod.img
objcopy --adjust-start -0xffffffff80000000 reprod.img
lld dosen't seem to consider tbss section to have proper lma
of lld
TLS 0x0000000000002058 0xffffffff80100060 (vma) 0xffffffff
80100060 (lma)
0x0000000000000000 0x0000000000000004 R 0x4
03 .tbss
Ld Linker
TLS 0x0000000000100098 0xffffffff801000a0 (vma)
0xffffffffc81000a0 (lma)
0x0000000000000000 0x0000000000000004 R 0x4
Since this is my first time looking at bfd code i might be misunderstanding
things
Looks like the macro ELF_SECTION_IN_SEGMENT_1 checks if a section belongs
to a segment based on vma
and considers tbss part of LOAD segment.
&& (!(check_vma) \
|| ((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \
|| ((sec_hdr)->sh_addr >= (segment)->p_vaddr \
&& (!(strict) \
|| ((sec_hdr)->sh_addr - (segment)->p_vaddr \
<= (segment)->p_memsz - 1)) \
&& (((sec_hdr)->sh_addr - (segment)->p_vaddr \
+ ELF_SECTION_SIZE(sec_hdr, segment)) \
<= (segment)->p_memsz)))
and later when it assigns file offsets, it seems to fail after sorting
segments.
qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
elf_sort_segments);
sorted segment order:
Im not sure if the fix needs to go into objcopy ie add appropriate checks
to ELF_SECTION_IN_SEGMENT_1 or make lld emit lma proper. either of both Im
ready to do the needful, please guide me on this.
Thank you.