[PATCH] D95198: [ELF] Fix program header alloc when first PT_LOAD is not at lowest VMA

Peter Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 30 08:22:14 PST 2021


psmith added a comment.

Apologies for the delay in responding. Is there any reason why the ELF Header and Program Header need to be covered by a PT_LOAD region at all? For example if the ELF file is on flash somewhere. The RTOS loader reads the ELF header and program header from the file in flash. If the program headers are not in the address range covered by any PT_LOAD then there is no need to copy the program headers into the TCM or SDRAM. As I understand it with -nmagic (and no PHDRS) neither LLD or ld.bfd will attempt to put the ELF Header and Program Header into a PT_LOAD segment.

Personally If we are to change LLD in this area I'd much prefer we did it with a good understanding of PHDRS and how it should interact with header placement and I'm not sure we (collectively) have that yet. May be worth asking on the binutils mailing list.

Reading the docs: https://sourceware.org/binutils/docs/ld/PHDRS.html

  You may use the FILEHDR and PHDRS keywords after the program header type to further describe the contents of the segment. The FILEHDR keyword means that the segment should include the ELF file header. The PHDRS keyword means that the segment should include the ELF program headers themselves. If applied to a loadable segment (PT_LOAD), all prior loadable segments must have one of these keywords.

There is test phdrs3a.t that may be of interest https://github.com/bminor/binutils-gdb/blob/master/ld/testsuite/ld-scripts/phdrs3a.t 
This has the PHDRS

  PHDRS
  {
    data PT_LOAD FILEHDR PHDRS FLAGS(4);
    text PT_LOAD FILEHDR PHDRS FLAGS(1);
  }
  
  SECTIONS
  {
    /* This test will fail on architectures where the startaddress below
       is less than the constant MAXPAGESIZE.  */
    . = 0x800000 + SIZEOF_HEADERS;
    .text : { *(.text) } :text
    .data : { *(.data) } :data
    /DISCARD/ : { *(.*) }
  }

The output is interesting. Both the data and text segment start at 0x800000, include the headers and program headers, with the output ELF file producing two overlapping PT_LOAD program headers essentially:

  0x800000 (data and text PHDR start)
  ELF Header
  Program Header
  .data
  (data PHDR end)
  .text
  (text PHDR end)

ld.bfd will give an error message if the FILEHDR and PHDR are removed from data.

To me, reversing the addresses so that the first PT_LOAD in the PHDRS command contains the FILEHDR and PHDR but is not the lowest program header is kind of a hack that seems to work than being intentional.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D95198/new/

https://reviews.llvm.org/D95198



More information about the llvm-commits mailing list