[edk2-devel] [PATCH v4 01/10] BaseTools: add BASETOOLS define

Ard Biesheuvel ardb at kernel.org
Tue Apr 18 11:59:43 UTC 2023


On Tue, 18 Apr 2023 at 13:52, Gerd Hoffmann <kraxel at redhat.com> wrote:
>
>   Hi,
>
> > > Underlying problem is that x64 ProcessorBind.h sets visibility to
> > > hidden (anyone knows why?).
> >
> > Yes. Toolchains tend to assume that you are generating code for a
> > shared library once you enabled -fpic, and this results in lots of
> > pointless indirections via the GOT. (This has to do with the
> > copy-on-write footprint of shared library pages, as a GOT tends to
> > gather all relocated quantities in a small region of memory. It is
> > also related to ELF symbol preemption, which is another thing we don't
> > give a ^$& about).
> >
> > *However*, recent linkers do a better job getting rid of these GOT
> > references at link time, once you pass the --relax option. On X64 in
> > particular, there are special, relaxable GOTPCREL relocations now that
> > will not result in to symbol references via the GOT if the linker
> > notices that the symbol in question is never preempted and not
> > provided externally.
> >
> > So it is worth an experiment whether we still need this to begin with.
>
> Seems to work fine on fedora 37, even without adding --relax, maybe this
> is enabled by default (there is a --no-relax switch after all).  I'll go
> try older distros / compilers / binutils too.
>
> What would be the failure mode?  Errors on ELF -> PE conversion because
> a GOT is present?  Or will things break at runtime?
>

The problem here is that we rely on --emit-relocs to get at the
relocations in the binary, in order to convert the absolute ones into
PE/COFF relocations.

However, --emit-relocs did not use to cover the GOT, as those are
added at the end by the linker and not by the compiler. So if the GOT
is non-empty, the resulting PE executable will be corrupt.

Of course, the answer here is to dump GenFw altogether for ELF to PE
conversion, and implement something that consumes the dynamic
relocations generated when linking in PIE mode.

> > >  Seems to be fine for freestanding binaries,
> > > but when linking host tools against shared libraries things break.  For
> > > some reason on ubuntu only, fedora works fine (both gcc and clang).
> > >
> > > Automatically detecting freestanding vs. hosted would be even better,
> > > anyone knows whenever there is a define which can be used for this?  At
> > > least the gcc man page doesn't mention any ...
> >
> > ard at gambale:~/linux-build-x86$ gcc -dM -E -o - -xc - <<< ""|grep HOSTED
> > #define __STDC_HOSTED__ 1
> > ard at gambale:~/linux-build-x86$ gcc -ffreestanding -dM -E -o - -xc -
> > <<< ""|grep HOSTED
> > #define __STDC_HOSTED__ 0
>
> Nice.  So we have a plan b in case just dropping it doesn't work out.
>
> take care,
>   Gerd
>


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#103161): https://edk2.groups.io/g/devel/message/103161
Mute This Topic: https://groups.io/mt/98257896/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-




More information about the edk2-devel-archive mailing list