unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries
@ 2020-01-10 16:20 Siddhesh Poyarekar
  2020-01-10 16:55 ` Alexander Monakov
  2020-01-11 13:25 ` Segher Boessenkool
  0 siblings, 2 replies; 5+ messages in thread
From: Siddhesh Poyarekar @ 2020-01-10 16:20 UTC (permalink / raw)
  To: gcc, GLIBC Devel

Hello,

Ref: https://bugs.linaro.org/show_bug.cgi?id=5479

Statically built independent programs that implement their own program
entry points (i.e. -ffreestanding -nostartfiles) and call __builtin_*
functions break when the builtin function in question is implemented as
an IFUNC in glibc and the builtin results in a glibc call instead of
some inline code.

This happens because the startup code where ifuncs are resolved never
gets executed (since glibc's startup code is never executed) and hence
the PLT jumps fail.  The bug report talks about this as an aarch64
problem but I've been able to reproduce the problem on x86_64 as well.
One just needs to make sure that the __builtin_foo call results in a
glibc call.

I spent some time thinking about this and while it's trivial to fix by
disabling ifuncs for static glibc, I wanted a solution that wasn't such
a big hammer.  The other alternative I could think of is to have an
exported alias (called __builtin_strlen for example instead of strlen)
of a default implementation of the builtin function in glibc that gcc
generates a call to if freestanding && nostartfiles && static.

Any thoughts or other ideas on how this could be implemented?

Thanks,
Siddhesh

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

* Re: [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries
  2020-01-10 16:20 [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries Siddhesh Poyarekar
@ 2020-01-10 16:55 ` Alexander Monakov
  2020-01-11  3:52   ` Siddhesh Poyarekar
  2020-01-11 13:25 ` Segher Boessenkool
  1 sibling, 1 reply; 5+ messages in thread
From: Alexander Monakov @ 2020-01-10 16:55 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: gcc, GLIBC Devel

On Fri, 10 Jan 2020, Siddhesh Poyarekar wrote:

> I spent some time thinking about this and while it's trivial to fix by
> disabling ifuncs for static glibc, I wanted a solution that wasn't such
> a big hammer.  The other alternative I could think of is to have an
> exported alias (called __builtin_strlen for example instead of strlen)
> of a default implementation of the builtin function in glibc that gcc
> generates a call to if freestanding && nostartfiles && static.

In the Linaro bugreport you mention,

> Basically, IFUNCs and freestanding don't mix.

but really any libc (Glibc included) and -nostartfiles don't mix: stdio
won't be initialized, TLS won't be setup, and pretty much all other
libc-internal datastructures won't be properly setup. Almost no libc functions
are callable, because for example if they try to access 'errno', they crash.

Looking at the opening comment of the failing kselftest source:

 * This program tries to be as small as possible itself, to
 * avoid perturbing the system memory utilization with its
 * own execution.  It also attempts to have as few dependencies
 * on kernel features as possible.
 *
 * It should be statically linked, with startup libs avoided.
 * It uses no library calls, and only the following 3 syscalls:
 *   sysinfo(), write(), and _exit()

so in fact allowing it to link with libc strlen would be contrary to its intent.
The fix is simple: add -nodefaultlibs next to -nostartfiles in its Makefile, and
write a trivial loop in place of __builtin_strlen.

Alexander

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

* Re: [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries
  2020-01-10 16:55 ` Alexander Monakov
@ 2020-01-11  3:52   ` Siddhesh Poyarekar
  0 siblings, 0 replies; 5+ messages in thread
From: Siddhesh Poyarekar @ 2020-01-11  3:52 UTC (permalink / raw)
  To: Alexander Monakov; +Cc: gcc, GLIBC Devel

On 10/01/20 10:25 pm, Alexander Monakov wrote:
> On Fri, 10 Jan 2020, Siddhesh Poyarekar wrote:
> 
>> I spent some time thinking about this and while it's trivial to fix by
>> disabling ifuncs for static glibc, I wanted a solution that wasn't such
>> a big hammer.  The other alternative I could think of is to have an
>> exported alias (called __builtin_strlen for example instead of strlen)
>> of a default implementation of the builtin function in glibc that gcc
>> generates a call to if freestanding && nostartfiles && static.
> 
> In the Linaro bugreport you mention,
> 
>> Basically, IFUNCs and freestanding don't mix.
> 
> but really any libc (Glibc included) and -nostartfiles don't mix: stdio
> won't be initialized, TLS won't be setup, and pretty much all other
> libc-internal datastructures won't be properly setup. Almost no libc functions
> are callable, because for example if they try to access 'errno', they crash.
> 
> Looking at the opening comment of the failing kselftest source:
> 
>  * This program tries to be as small as possible itself, to
>  * avoid perturbing the system memory utilization with its
>  * own execution.  It also attempts to have as few dependencies
>  * on kernel features as possible.
>  *
>  * It should be statically linked, with startup libs avoided.
>  * It uses no library calls, and only the following 3 syscalls:
>  *   sysinfo(), write(), and _exit()
> 
> so in fact allowing it to link with libc strlen would be contrary to its intent.
> The fix is simple: add -nodefaultlibs next to -nostartfiles in its Makefile, and
> write a trivial loop in place of __builtin_strlen.

That's a valid point.  I'll recommend dropping __builtin_strlen from
that test.

Thanks,
Siddhesh

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

* Re: [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries
  2020-01-10 16:20 [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries Siddhesh Poyarekar
  2020-01-10 16:55 ` Alexander Monakov
@ 2020-01-11 13:25 ` Segher Boessenkool
  2020-01-11 13:31   ` Siddhesh Poyarekar
  1 sibling, 1 reply; 5+ messages in thread
From: Segher Boessenkool @ 2020-01-11 13:25 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: gcc, GLIBC Devel

Hi!

On Fri, Jan 10, 2020 at 09:50:48PM +0530, Siddhesh Poyarekar wrote:
> Statically built independent programs that implement their own program
> entry points (i.e. -ffreestanding -nostartfiles) and call __builtin_*
> functions break when the builtin function in question is implemented as
> an IFUNC in glibc and the builtin results in a glibc call instead of
> some inline code.
> 
> This happens because the startup code where ifuncs are resolved never
> gets executed (since glibc's startup code is never executed) and hence
> the PLT jumps fail.  The bug report talks about this as an aarch64
> problem but I've been able to reproduce the problem on x86_64 as well.
> One just needs to make sure that the __builtin_foo call results in a
> glibc call.

-ffreestanding means you might not have any of the C standard library,
and -nostartfiles means you do not do any of the standard initialisation.
Why then would you expect any ifunc to work?

> I spent some time thinking about this and while it's trivial to fix by
> disabling ifuncs for static glibc, I wanted a solution that wasn't such
> a big hammer.  The other alternative I could think of is to have an
> exported alias (called __builtin_strlen for example instead of strlen)
> of a default implementation of the builtin function in glibc that gcc
> generates a call to if freestanding && nostartfiles && static.
> 
> Any thoughts or other ideas on how this could be implemented?

Why do you not want the startfiles, but do want their effects?


Segher

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

* Re: [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries
  2020-01-11 13:25 ` Segher Boessenkool
@ 2020-01-11 13:31   ` Siddhesh Poyarekar
  0 siblings, 0 replies; 5+ messages in thread
From: Siddhesh Poyarekar @ 2020-01-11 13:31 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: gcc, GLIBC Devel

On 11/01/20 6:55 pm, Segher Boessenkool wrote:
> -ffreestanding means you might not have any of the C standard library,
> and -nostartfiles means you do not do any of the standard initialisation.
> Why then would you expect any ifunc to work?
> 

Agreed, based on Alexander's feedback I have suggested changing the
tests instead.

Siddhesh

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

end of thread, other threads:[~2020-01-11 13:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-10 16:20 [RFC] builtin functions and `-ffreestanding -nostartfies` with static binaries Siddhesh Poyarekar
2020-01-10 16:55 ` Alexander Monakov
2020-01-11  3:52   ` Siddhesh Poyarekar
2020-01-11 13:25 ` Segher Boessenkool
2020-01-11 13:31   ` Siddhesh Poyarekar

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