public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Matheus Afonso Martins Moreira <matheus.a.m.moreira@gmail.com>
To: binutils@sourceware.org
Subject: Re: Appending program headers to existing ELFs?
Date: Tue, 14 Nov 2023 22:26:28 -0300	[thread overview]
Message-ID: <c5b186e052b1ff9f24aeb8384bfaac1e.matheus.a.m.moreira@gmail.com> (raw)
In-Reply-To: <f3c40681-219d-4605-a925-564aed5b3806@redhat.com>

Hello Nick,

> Why can't you add the data at link time ?

I'd like to add a section to the executable
that users can freely modify after the binary
has been built and distributed without needing
to recompile or relink anything.

In concrete terms:

I'm building a small programming language interpreter
and would like to add support for embedding programs
into the executable. My intention is to treat the
embedded programs section as an additional load path
for libraries and modules as well as the main function.

> could you create a new symbol for the section
> and reference the data that way

That is indeed possible but as you noted
it would require access to the symbol table
at runtime and that's not guaranteed.
That information may have been stripped away
and the kernel does not load it to begin with.
I'm also not using libc so I don't have access
to dlopen and dlsym.

> At the moment this can only be done by the linker

I see. I suspected as much but hoped there would be a way.
Thanks for clarifying that.

> With a linker script you can layout a PHDRS structure

Yes. It's one of the solutions I tried.

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

I was unable to determine the cause and fix it.

For ease of maintenance, I don't intend to mess with
the sections and segments generated by the GNU tools.
I would have liked to merely append a custom header
to the ones generated by ld. Unfortunately, merely
including a PHDRS command disables ld's program header
generation logic which sensibly maps the sections
to the headers.

A linker script directive that applies defaults
while allowing further customization would be
a great new feature.

In the meantime, I'm trying to work around the problem
by placing a statically allocated packed structure
into an isolated section so that its entire contents
can be overwritten by objcopy:

    objcopy --update-section .structure=./file
            lang lang+file

This reduces the problem to a binary file format problem
and seems like the way forward for me at the moment.
Would have been nice to be able to reuse the ELF machinery though...

Thanks for the guidance,

    Matheus

  reply	other threads:[~2023-11-15  1:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-13 14:19 Matheus Afonso Martins Moreira
2023-11-14 12:47 ` Nick Clifton
2023-11-15  1:26   ` Matheus Afonso Martins Moreira [this message]
2023-11-16  8:51     ` Nick Clifton
2023-11-17  8:05       ` Fangrui Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c5b186e052b1ff9f24aeb8384bfaac1e.matheus.a.m.moreira@gmail.com \
    --to=matheus.a.m.moreira@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).