ELF program header in baremetal

Alexander Fedotov <[email protected]>
Newsgroups gmane.comp.gdb.devel,gmane.comp.gnu.binutils
Message-ID <CAN8C2CrxTvj4mgiTQjEiYo9icdRJvk8kCWa=n0fF0WnRmFUx1Q@mail.gmail.com>
Hello Binutils and GDB folks.

There is a question regarding p_align in ELF Program Header.
Whilst this parameter is understandable for loading application in OS,
there is no clear picture for baremetal - must debugger to follow ELF
specification or not.
Seems like we’ve been lucky in most cases until alignment being increased
to 64kB [1] since it was small enough. Now it is not.

Below is a real case on the MCU with Arm core onboard and small memory amount.

Linker script is:
  m_interrupts  (RX) : ORIGIN = 0x1FFF8000, LENGTH = 0x00000400
  m_text        (RX) : ORIGIN = 0x1FFF8400, LENGTH = 0x00007C00

Program Header:
    LOAD off    0x00000000 vaddr 0x1fff0000 paddr 0x1fff0000 align 2**16
         filesz 0x00008400 memsz 0x00008400 flags r--
    LOAD off    0x00008400 vaddr 0x1fff8400 paddr 0x1fff8400 align 2**16
         filesz 0x00000570 memsz 0x00000570 flags rwx
    LOAD off    0x00010000 vaddr 0x20000000 paddr 0x20000000 align 2**16
         filesz 0x00000424 memsz 0x00007000 flags rw-
private flags = 5000200: [Version5 EABI] [soft-float ABI]

With the given Program Header non-GDB debuggers loads non-existent memory
and throws an error. Another ones loads this ELF with warning but with
Verification error after that. I haven’t analyzed how GDB address this,
but somehow it is not affected. Maybe GDB just ignore PH and loads sections.
Anyway such a different behavior is odd.

Another case is multicore wen we have multiple separate ELF files. Loading
one-by-one can overwrite previously loaded text/data.
Similar case with bootloader overwriting [2].


My proposal is to set ELF_MAXPAGESIZE to smallest value for baremetal
configuration.

gABI states:
  Values 0 and 1 mean no alignment is required. Otherwise, p_align should
  be a positive, integral power of 2, and p_vaddr should equal p_offset,
  modulo p_align.

Forcing -max-page-size to 0/1 doesn't work unfortunately. It still generates
non-existent segments.


Best regards,
Alex

---------------
[1] https://patches.linaro.org/patch/32565/
[2] https://www.tablix.org/~avian/blog/archives/2012/11/gnu_linker_and_elf_program_header/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.