public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Overlapping program headers/segments
@ 2023-09-24 18:32 Max Larsson
  2023-09-25  6:14 ` Jan Beulich
  2023-09-25  7:24 ` Andreas Schwab
  0 siblings, 2 replies; 4+ messages in thread
From: Max Larsson @ 2023-09-24 18:32 UTC (permalink / raw)
  To: binutils


Hi everyone,
 
I'm about to (re-)port binutils (mainly the the linker ld) to the amigaos ppc architecture using
elf format.
 
Now I've stumble over a problem, where I need to help, how to fix it.
 
What I have done so far, is to put the .rodata section in its own segment via defining a target
specific method for 'elf_backend_modify_segment'. How to do it, I copied/looked/adapted from
elfxx-mips.c:_bfd_mips_elf_modify_segment_map That works as expected.
 
But The result is that the program headers overlap. Like this:
 
Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x10000000 0x10000000 0x02034 0x02034 R E 0x10000
  LOAD           0x020000 0x10010000 0x10010000 0x000f0 0x000f0 RW  0x10000
  DYNAMIC        0x020028 0x10010028 0x10010028 0x000b8 0x000b8 RW  0x4
  LOAD           0x012000 0x10002000 0x10002000 0x00031 0x00031 R   0x10000
 Section to Segment mapping:
  Segment Sections...
   00     .hash .dynsym .dynstr .rela.dyn .rela.got2 .rela.plt .text .plt
   01     .init_array .fini_array .got2 .dynamic .got
   02     .dynamic
   03     .rodata
 
You see, if you take the Virt-/PhysAddr and add the MemSize from the first
program header the result is 0x10002034, which is greater than the Virt-/PhysAddr
of the last program header   0x10002000.

So my question why do I get such a result, and how can I avoid it?

best regards

Max Larsson


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Overlapping program headers/segments
  2023-09-24 18:32 Overlapping program headers/segments Max Larsson
@ 2023-09-25  6:14 ` Jan Beulich
  2023-09-25  7:24 ` Andreas Schwab
  1 sibling, 0 replies; 4+ messages in thread
From: Jan Beulich @ 2023-09-25  6:14 UTC (permalink / raw)
  To: Max Larsson; +Cc: binutils

On 24.09.2023 20:32, Max Larsson via Binutils wrote:
> What I have done so far, is to put the .rodata section in its own segment via defining a target
> specific method for 'elf_backend_modify_segment'. How to do it, I copied/looked/adapted from
> elfxx-mips.c:_bfd_mips_elf_modify_segment_map That works as expected.
>  
> But The result is that the program headers overlap. Like this:
>  
> Program Headers:
>   Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
>   LOAD           0x000000 0x10000000 0x10000000 0x02034 0x02034 R E 0x10000
>   LOAD           0x020000 0x10010000 0x10010000 0x000f0 0x000f0 RW  0x10000
>   DYNAMIC        0x020028 0x10010028 0x10010028 0x000b8 0x000b8 RW  0x4
>   LOAD           0x012000 0x10002000 0x10002000 0x00031 0x00031 R   0x10000
>  Section to Segment mapping:
>   Segment Sections...
>    00     .hash .dynsym .dynstr .rela.dyn .rela.got2 .rela.plt .text .plt
>    01     .init_array .fini_array .got2 .dynamic .got
>    02     .dynamic
>    03     .rodata
>  
> You see, if you take the Virt-/PhysAddr and add the MemSize from the first
> program header the result is 0x10002034, which is greater than the Virt-/PhysAddr
> of the last program header   0x10002000.
> 
> So my question why do I get such a result, and how can I avoid it?

You didn't provide the linker script, which may have the observed effect together
with your code change: If it puts .text and .rodata together, then the overlap is
to be expected (as would frequently happen for NOTE segments and .note sections),
I think.

Jan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Overlapping program headers/segments
  2023-09-24 18:32 Overlapping program headers/segments Max Larsson
  2023-09-25  6:14 ` Jan Beulich
@ 2023-09-25  7:24 ` Andreas Schwab
  2023-09-28  7:25   ` Max Larsson
  1 sibling, 1 reply; 4+ messages in thread
From: Andreas Schwab @ 2023-09-25  7:24 UTC (permalink / raw)
  To: Max Larsson via Binutils; +Cc: Max Larsson

On Sep 24 2023, Max Larsson via Binutils wrote:

> So my question why do I get such a result, and how can I avoid it?

If you want to avoid the overlap you need to provide enough padding
between TEXT and RODATA.  See the handling of SEPARATE_CODE in
scripttempl/elf.sc.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Overlapping program headers/segments
  2023-09-25  7:24 ` Andreas Schwab
@ 2023-09-28  7:25   ` Max Larsson
  0 siblings, 0 replies; 4+ messages in thread
From: Max Larsson @ 2023-09-28  7:25 UTC (permalink / raw)
  To: Max Larsson via Binutils

> You didn't provide the linker script, which may have the observed effect together
> with your code change: If it puts .text and .rodata together, then the overlap is
> to be expected (as would frequently happen for NOTE segments and .note sections),
> I think.

Forget to mention. I using a slight modified elf.sc script, but even
using the plain elf.sc linker
script gives me overlapping  segments. But i think i found the
issue/solution. Read below.

>> So my question why do I get such a result, and how can I avoid it?
>
> If you want to avoid the overlap you need to provide enough padding
> between TEXT and RODATA.  See the handling of SEPARATE_CODE in
> scripttempl/elf.sc.

I tried that, but it doesn't give me the desired result. I need more a
result delivered
be enabling WRITABLE_RODATA. But a writable .rodata section isn't
liked as primer
solution. It's a possible the backup plan.

Nevertheless your hint with SEPARATE_CODE did lead to a solution, even
if I  cannot
reconstruct how i got there.
As mention I copied the solution from
elfxx-mips.c:_bfd_mips_elf_modify_segment_map to
put the .rodata section in its own segment. What I wondered about was
that the code
just looks for the section and just puts it in a new segment, but it
did not remove the section
from the origin segment.

Now I copied the code and adopted it from elf32-spu.c:
spu_elf_modify_segment_map, which not only put the section in its own
segment but
even removes it from the origin segment. Doing so gives me no
overlapping segments.

Thus the code I used in the first place may not have used the internal
data structures in the "correct"
way, leaving a section somehow in two segments, which gives wrong
calculated addresses and offsets?

Anyway thanks for the help.

kind regards

Max



On Mon, Sep 25, 2023 at 9:24 AM Andreas Schwab <schwab@suse.de> wrote:
>
> On Sep 24 2023, Max Larsson via Binutils wrote:
>
> > So my question why do I get such a result, and how can I avoid it?
>
> If you want to avoid the overlap you need to provide enough padding
> between TEXT and RODATA.  See the handling of SEPARATE_CODE in
> scripttempl/elf.sc.
>
> --
> Andreas Schwab, SUSE Labs, schwab@suse.de
> GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
> "And now for something completely different."

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-28  7:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-24 18:32 Overlapping program headers/segments Max Larsson
2023-09-25  6:14 ` Jan Beulich
2023-09-25  7:24 ` Andreas Schwab
2023-09-28  7:25   ` Max Larsson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).