<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72386>72386</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `lld`: append ELF program header option
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          matheusmoreira
      </td>
    </tr>
</table>

<pre>
    It would be useful to have a command line option or plugin for the linker that appends an empty `PT_LOAD` program header table entry to ELF executables. This will greatly facillitate patching executables with new data after linking.

The Linux kernel automatically loads those segments onto memory and passes a pointer to the program header table via the auxiliary vector. This would be the perfect mechanism to allow executables to easily and efficiently access data embedded into the executable itself, even data patched in after the the binary has been compiled.

Current solutions are insufficient. `objcopy` can add new sections but they do not get loaded by the kernel without a `PT_LOAD` segment and those can only be created at link time since adding new program headers would change all offsets in the file. Linker scripts support a `PHDRS` command but using that disables the linker's default behavior and forces users to specify _all_ the segments and map all the sections to them instead of letting the linker do it.

A simple `--append-program-header` that just adds an empty program header to the end of the table would be ideal. With that feature in place, custom tools can be written to copy arbitrary data into the ELF and then edit the placeholder's offset and size to match.

Links:

[Related StackOverflow question][so]
[Binutils mailing list discussion][binutils]

[so]: https://stackoverflow.com/q/77468641
[binutils]: https://sourceware.org/pipermail/binutils/2023-November/130532.html
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVc2O20YMfhr5Qqwhy2vZe_BhN1ujBYKmSAL0GFAjyppkNKMMOXbcpy84kp112sOuDGlIfvx-JGS2R0-0LzYvxeZ1gUn6EPcDSk-JhxDJRlw0ob3s_xA4h-RaaAgSU5ccSIAeTwQIJgwD-hac9QRhFBs8hAijS0froQsRpCd9-o30JwrgOJJvGdADDaNcoKjLvz5_ef_h-bWoSxhjOEYcoCdstQQbR0Be4kWn_vb-APSDTMr3eQmfe8twts7BMRKKu0CHxjpnBYVgR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