Objcopy seems to consider tbss part of PTLOAD

2024-03-15 Thread vijay
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=0x8010 --defsym LMA_START=0x480
-T l.ld reprod.img -o reprod.img
objcopy --change-section-lma .*-0x8000 reprod.img
objcopy --adjust-start -0x8000 reprod.img

lld dosen't seem to consider tbss section to have proper lma

of lld
  TLS0x2058 0x80100060 (vma) 0x
80100060 (lma)
 0x 0x0004  R  0x4
   03 .tbss
Ld Linker
  TLS0x00100098 0x801000a0 (vma)
0xc81000a0 (lma)
 0x 0x0004  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.


Objcopy seems to consider tbss part of PTLOAD

2024-03-15 Thread vijay Shankar
Take the following examplereprod.cint __thread a;
int b,c,k=100,n=400;
int my_start()
{
b = 500;
return a*a*b*k*n;
}
 l.ldENTRY(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.imgld.lld --defsym VMA_START=0x8010 --defsym LMA_START=0x480 -T l.ld reprod.img -o reprod.imgobjcopy --change-section-lma .*-0x8000 reprod.imgobjcopy --adjust-start -0x8000 reprod.img lld dosen't seem to consider tbss section to have proper lma of lld  TLS    0x2058 0x80100060 (vma) 0x80100060 (lma) 0x 0x0004  R  0x4   03 .tbssLd Linker  TLS    0x00100098 0x801000a0 (vma) 0xc81000a0 (lma) 0x 0x0004  R  0x4 Since this is my first time looking at bfd code i might be misunderstanding thingsLooks like the macro ELF_SECTION_IN_SEGMENT_1 checks if a section belongs to a segment based on vmaand 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. 

Re: Objcopy seems to consider tbss part of PTLOAD

2024-03-19 Thread vijay Shankar
 Hi Vijay,   In the future, it helps if report bugs, or potential bugs,   via the binutils bugzilla system. This allows us to track   bugs individually and also add comments in the code that   refer back to the bug report. https://sourceware.org/bugzilla/Thanks, will keep this in mind. clang reprod.c -ffreestanding -c -o reprod.imgWhich architecture is this for ? Which versions of clang andthe binutils are you using ?I'm using clang-17 and binutils 2.40 I have gotten this issue with x86_64 but this issue seems target agnostic.  ld.lld --defsym VMA_START=0x8010 --defsym LMA_START=0x480 -T l.ld reprod.img -o reprod.img objcopy --change-section-lma .*-0x8000 reprod.img objcopy --adjust-start -0x8000 reprod.img lld dosen't seem to consider tbss section to have proper lma of lld    TLS    0x2058 0x80100060 (vma) 0x80100060 (lma)   0x 0x0004  R  0x4 03 .tbss Ld Linker    TLS    0x00100098 0x801000a0 (vma) 0xc81000a0 (lma)   0x 0x0004  R  0x4I am sorry, but I did not understand what is wrong with the LMA.Please could you explain some more.Also, you say that objcopy does not consider the .tbss sectionto be part of a PT_LOAD segment. This is correct. It is partof a PT_TLS segment. PT_TLS segments are considered to beinherently loadable, so it is OK to use PT_TLS for thread localdata rather than PT_LOAD.Cheers   NickI am sorry for not being clear.to clarify it looks like when PT_TLS segment is considered part of PT_LOAD which makes sense considering .tdata is part of LOAD.but Im unsure about PT_TLS segment  consisting only of .tbss section like in above case.when objcopy does consider such a segment to be part of LOAD, when assigning offsets things seem to be going wrongFirst issue we encounter.objcopy: stq0GBq2: section `.data' can't be allocated in segment 0LOAD: .tbss .data .bss .sdataobjcopy: stq0GBq2: section `.bss' can't be allocated in segment 0LOAD: .tbss .data .bss .sdatasecond issue the image sizes are overblowndu -sh a.out34G a.outall of this because lld seems to not emit LMA with a proper offset ie its LMA is just VMA not LMA+Offset.segments are sorted and based on LMA ,based upon this the respective file-offsets are assigned and this is when things seem to be going wrong.Is it expected that objcopy considers PT_TLS segment consisting only of tbss to be part of LOAD, since tbss is a special section I assumed it should have been dealt with differently ie not part of LOAD. if so could you explain reasoning behind this.Thank you,~vijay

Re: Objcopy seems to consider tbss part of PTLOAD

2024-04-15 Thread vijay Shankar
  Raised bugzilla for the same :https://sourceware.org/bugzilla/show_bug.cgi?id=31540updated with suggested patch.Thank you.