[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
Sun Jan 31 22:44:58 PST 2021


pattop added a comment.

I can think of a few times I've encountered this on nommu targets. Some examples:

One product has a first stage bootloader which loads an ELF image from eMMC and then branches to its entry point. In this case PHDRS need to be loaded so that the program (a small RTOS) has them available. The RTOS uses its own PHDRS while initialising its page allocator to reserve regions where it itself is loaded, effectively a form of introspection. There's an internal SRAM in the thing at 0x20000000 and DRAM (where the headers go) is at 0x80000000.

Another product loads DSP routines into the TCMs of a Cortex-M7 as part of a larger program. This is effectively a normal ELF file with a few extra sections linked to absolute addresses. As stated earlier ITCM is at address 0. PHDRs definitely need to be loaded here as the C runtime reads them as part of its initialisation.

Regardless of whether these are good design decisions, BFD ld has this capability and it is definitely used in the wild.

On the topic of PHDRs and header placement I think the idea that headers should be part of the first load segment follows on from the fact that the file header is always at the beginning of the file. It's then quite natural for the first load segment to start at file offset 0.

I have also seen phdrs3a.t. It originates from a discussion here https://sourceware.org/legacy-ml/binutils/2009-10/msg00023.html which led to this bug https://sourceware.org/bugzilla/show_bug.cgi?id=10744 which resulted in the test case.

I think BFD ld intentionally does not place any restrictions on the VMAs of the load segments so I disagree that this is a hack or workaround.

You can specify the exact address of an output segment using the AT(ADDRESS) syntax without any restrictions. https://sourceware.org/binutils/docs/ld/PHDRS.html says:

  You can specify that a segment should be loaded at a particular address in memory by using an AT expression. This is identical to the AT command used as an output section attribute (see Output Section LMA). The AT command for a program header overrides the output section attribute.


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