[PATCH] elf: Use dl-symbol-redir-ifunc.h instead _dl_strlen
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Wed Feb 11 18:44:45 GMT 2026
On 11/02/26 15:31, DJ Delorie wrote:
> Adhemerval Zanella <adhemerval.zanella@linaro.org> writes:
>> -/* The function might be called before the process is self-relocated. */
>> -static size_t
>> -__attribute_optimization_barrier__
>> -_dl_strlen (const char *s)
>> -{
>> - const char *p = s;
>> - for (; *s != '\0'; s++);
>> - return s - p;
>> -}
>
> This part is OK.
>
>> - size_t envvallen = _dl_strlen (envval);
>> + for (const char *p = envval; *p != '\0'; p++, envvallen++);
>
> I thought the problem was that clang turned this loop into an internal
> call to strlen, regardless of what "strlen()" would expand to in the
> current scope? Does dl-symbol-redir-ifunc.h guarantee the symbol
> "strlen" will be the right one? (looks like yes)
And you are right in your assessment. The 'asm' redirection trick instructs
the linker to alias all compiler generated symbols to another one controlled
by the TU.
>
> In fact, I see no reference to strlen in any of the
> dl-symbol-redir-ifunc.h so I'm wondering how (or if) it fixed this
> problem in the first place...
>
> Looks like on x86 the call to strlen might be faster as it uses sse2
> instead of the dumb loop...
>
>> asm ("memset = __memset_generic");
>> +asm ("strlen = __strlen_generic");
>
> Is this compatible with the hidden strlen reference clang uses? I think
> so.
Yes, in fact dl-symbol-redir-ifunc.h fixes similar issues for the case gcc
does similar optimizations.
>
>> diff --git a/sysdeps/aarch64/multiarch/strlen_generic.S b/sysdeps/aarch64/multiarch/strlen_generic.S
>> +
>> +#if IS_IN (rtld)
>> +strong_alias (strlen, __strlen_generic)
>> +#endif
>
> Ok
>
>> diff --git a/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h
>> index 04b6c1e6a3..b607e525f2 100644
>> --- a/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h
>> +++ b/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h
>> @@ -44,6 +44,16 @@ asm ("memset = " HAVE_MEMSET_IFUNC_GENERIC);
>>
>> asm ("memcmp = " HAVE_MEMCMP_IFUNC_GENERIC);
>>
>> +#if MINIMUM_X86_ISA_LEVEL >= 4
>> +# define HAVE_STRCMP_IFUNC_GENERIC "__strlen_evex"
>> +#elif MINIMUM_X86_ISA_LEVEL == 3
>> +# define HAVE_STRCMP_IFUNC_GENERIC "__strlen_avx2"
>> +#else
>> +# define HAVE_STRCMP_IFUNC_GENERIC "__strlen_sse2"
>> +#endif
>> +
>> +asm ("strlen = " HAVE_STRCMP_IFUNC_GENERIC);
>> +
>
> Ok.
>
> LGTM
> Reviewed-by: DJ Delorie <dj@redhat.com>
>
More information about the Libc-alpha
mailing list