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

Patrick Oppenlander via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 26 15:04:17 PST 2021


pattop added a comment.

In D95198#2519261 <https://reviews.llvm.org/D95198#2519261>, @peter.smith wrote:

> I'm not clear on what the requirements are here.

The ability to control the segment in which the program headers reside.

BFD ld requires that they are in the first loadable segment. But it does not require that the first loadable segment is at the lowest address, or that the segments are in any particular order.

> I would be very interested to see a reference to placing headers in the first loadable program segment. That sounds like it could be a convention of some linker/platform, but I can't remember seeing that in any specification.
>
> The best reference I've found is in Levine's Linkers and Loaders book (from 2000) where it talks about
>
>   ELF files extend the "header in the address space" trick used in QMAGIC a.out files to make the executable file as compact as possible at the cost of some unused space in the address space."
>
> Looking up QMAGIC it says:
>
>    Compact pageable files consider the a.out header to be part of the text segment, because there's no particular reason that the code in the text segment has to start at location zero.
>   ...
>   The code actually starts immediately after the header and the whole page is mapped into the second page of the process, leaving the first page unmapped so that pointer references to location zero will fail. This has the harmless side effect of mapping the header into the process as well.

Thanks for the references. I also don't know where this originally comes from.

> My understanding of embedded systems (mostly deeply embedded with at most an RTOS) is that the loadable segments are extracted from the ELF file and burned into ROM/Flash. The program never refers to the ELF header and program headers. It sounds like you have a requirement to place the ELF header and program headers in an arbitrary segment?

In this particular system there is a small RTOS with a program loader which can load ELF images. The system has a small RAM at address 0 (ITCM on a Cortex-M7) into which some critical functions need to be loaded. The rest of the program is loaded into a DRAM at some other location.

LLD currently forces the program headers to reside at the lowest address (in this case, address 0) which means that the program no longer fits into the available space.

> If so, rather than alter the QMAGIC convention, I think it would be better to use the PHDRS linker script command https://sourceware.org/binutils/docs/ld/PHDRS.html I believe that permits the headers to be allocated to a user controlled segment. If LLD's PHDRS support isn't good enough (compared to BFD) I'd prefer we improved it.

Improving LLD's PHDRS support is what I was trying to do with this patch.

With the change in place program headers are allocated into the first loadable segment if it exists. This behaviour is close to BFD ld, but BFD ld can also put the headers into multiple segments (but each prior loadable segment must also include them).

This change alone is not enough to fully solve the problem. D95199 <https://reviews.llvm.org/D95199> is also required for the link to succeed.


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