Google's build system, gn, used for Chromium, et.al., has support for this which can provide some inspiration for most of the major platforms on which Emacs runs; qv https://gn.googlesource.com/gn/+/refs/heads/main/src/util/exe_path.cc

On Tue, Sep 17, 2024 at 1:51 PM Eli Zaretskii <eliz@gnu.org> wrote:
> Cc: Lars Ingebrigtsen <larsi@gnus.org>, Andrea Corallo <acorallo@gnu.org>
> Date: Tue, 17 Sep 2024 11:18:41 -0400
> From:  Spencer Baugh via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>
>
> 1. Compile and install Emacs with --with-native-compilation=aot, e.g.:
> prefix=~/prefix
> mkdir $prefix
> ./configure --with-native-compilation=aot --prefix=$prefix
> make -j64 && make install
> 2. Run emacs with "exec -a" to change its argv[0]:
> sh -c "exec -a emacs $prefix/bin/emacs -Q --batch"
> 3. Observe an error like:
> Error using execdir /usr/local/home/sbaugh/workspaces/24833141-bffb-3c99-a9d6-c366d37c4f5e/+share+/app/emacs/bin/:
> emacs: /usr/local/home/sbaugh/workspaces/24833141-bffb-3c99-a9d6-c366d37c4f5e/+share+/app/emacs/bin/../native-lisp/31.0.50-a88a37f5/preloaded/minibuffer-b2d9c221-284ab177.eln: cannot open shared object file: No such file or directory
>
> "exec -a emacs" works fine for with-native-compilation=yes or
> with-native-compilation=no.

Invocation via "exec -a" is not supported, if it messes with the
leading directories of the argv[0] value passed to Emacs.  That's
because the search for the preloaded *.eln files is based on the
directory in which the Emacs executable is installed, as passed via
argv[0], and breaks if "exec -a" messes with that.

IOW, "don't do that, it will hurt".

P.S. If someone knows how to teach Emacs how to find the absolute file
name of its executable without depending on argv[0], speak up.  We do
that on Windows, but not on Posix platforms, since (I'm being told)
there's no reliable way of having that on GNU/Linux and other Posix
platforms.