public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Feature request: spare program headers
@ 2023-11-17  3:23 Matheus Afonso Martins Moreira
  2023-11-17  8:08 ` Simon Richter
  0 siblings, 1 reply; 3+ messages in thread
From: Matheus Afonso Martins Moreira @ 2023-11-17  3:23 UTC (permalink / raw)
  To: binutils

Hello. I recently emailed this list about appending PHDRs to programs:

https://sourceware.org/pipermail/binutils/2023-November/130532.html

There turned out to be a simple and effective solution to my problem:
the linker appends a number of empty PT_NULL entries to the PHDRs table.
This allows post processing tools to overwrite those null headers
in any way they want without impacting normal linker operations.

The mold linker has already integrated this feature.
I'd like to formally request that it be included in GNU ld as well.
I have also filed an LLVM lld issue but there was no interest so far.

Relevant links:

mold
https://github.com/rui314/mold/issues/1148
https://github.com/rui314/mold/commit/eb6c213f2a9aa8a101b2b52a791be369d165e6a9

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

* Re: Feature request: spare program headers
  2023-11-17  3:23 Feature request: spare program headers Matheus Afonso Martins Moreira
@ 2023-11-17  8:08 ` Simon Richter
  2023-11-18  0:18   ` Matheus Afonso Martins Moreira
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Richter @ 2023-11-17  8:08 UTC (permalink / raw)
  To: binutils

Hi,

On 11/17/23 12:23, Matheus Afonso Martins Moreira wrote:

> There turned out to be a simple and effective solution to my problem:
> the linker appends a number of empty PT_NULL entries to the PHDRs table.
> This allows post processing tools to overwrite those null headers
> in any way they want without impacting normal linker operations.

You can specify PHDRs in your linker script. For example,

PHDRS {
     one 0;
     two 0;
}

$ ld -o test.elf -x test.x
ld: warning: cannot find entry symbol _start; not setting start address
$ objdump -p test.elf

test.elf:     file format elf64-x86-64

Program Header:
     NULL off    0x0000000000000000 vaddr 0x0000000000000000 paddr 
0x0000000000000000 align 2**3
          filesz 0x0000000000000000 memsz 0x0000000000000000 flags ---
     NULL off    0x0000000000000000 vaddr 0x0000000000000000 paddr 
0x0000000000000000 align 2**3
          filesz 0x0000000000000000 memsz 0x0000000000000000 flags ---

    Simon

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

* Feature request: spare program headers
  2023-11-17  8:08 ` Simon Richter
@ 2023-11-18  0:18   ` Matheus Afonso Martins Moreira
  0 siblings, 0 replies; 3+ messages in thread
From: Matheus Afonso Martins Moreira @ 2023-11-18  0:18 UTC (permalink / raw)
  To: binutils

> You can specify PHDRs in your linker script.

That's true but I ran into complications
when I tried to use the PHDRS directive.
I described my attempt in a previous email:

https://sourceware.org/pipermail/binutils/2023-November/130561.html

> I copied ld's internal linker script
> and customized it with a PHDRS command:
>
>     PHDRS
>     {
>         headers PT_PHDR FILEHDR PHDRS;
>         text PT_LOAD;
>         rodata PT_LOAD;
>         data PT_LOAD;
>         bss PT_LOAD;
>         extra PT_LOAD; /* meant to be empty */
>                        /* size/offset will be set later */
>     }
>
> I was not successful. I keep getting the following error:
>
>     error: could not allocate headers

Perhaps it is possible to do it via linker script
but I couldn't get it to work no matter what I tried.

It's also not possible to do *only* this.
Using PHDRS disables the linker's defaults
and forces users to specify all the segments.

https://sourceware.org/binutils/docs-2.41/ld/PHDRS.html

> The linker will create reasonable program headers by default.
> When the linker sees the PHDRS command in the linker script,
> it will not create any program headers other than the ones specified.

As a result of inhibiting those defaults, it also requires us to map
all the sections to all the custom segments we created instead of
simply letting the linker do it. The result is we have to maintain
customized copies of the linker's default scripts in our repositories.

An option that adds PT_NULL segments would be much easier to use.
Custom post processing tools can later customize those segments
with much greater flexibility after the link. I just implemented
such a tool for my project.

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

end of thread, other threads:[~2023-11-18  0:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-17  3:23 Feature request: spare program headers Matheus Afonso Martins Moreira
2023-11-17  8:08 ` Simon Richter
2023-11-18  0:18   ` Matheus Afonso Martins Moreira

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).