[11/11] nptl: Fix pthread_create.c build with clang

Message ID 20221028173532.876027-12-adhemerval.zanella@linaro.org
State Committed
Commit 3d8b5dde879c6e024548118914da5bfcbd5170a7
Headers
Series Initial fixes for clang build support |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Adhemerval Zanella Oct. 28, 2022, 5:35 p.m. UTC
  clang complains that libc_hidden_data_def (__nptl_threads_events)
creates an invalid alias:

  pthread_create.c:50:1: error: alias must point to a defined variable or function
  libc_hidden_data_def (__nptl_threads_events)
  ^
  ../include/libc-symbols.h:621:37: note: expanded from macro
  'libc_hidden_data_def'

It seems that clang requires that a proper prototype is defined prior
the hidden alias creation.
---
 nptl/pthread_create.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
  

Comments

Fangrui Song Oct. 29, 2022, 4:51 a.m. UTC | #1
On 2022-10-28, Adhemerval Zanella via Libc-alpha wrote:
>clang complains that libc_hidden_data_def (__nptl_threads_events)
>creates an invalid alias:
>
>  pthread_create.c:50:1: error: alias must point to a defined variable or function
>  libc_hidden_data_def (__nptl_threads_events)
>  ^
>  ../include/libc-symbols.h:621:37: note: expanded from macro
>  'libc_hidden_data_def'
>
>It seems that clang requires that a proper prototype is defined prior
>the hidden alias creation.

I investigated the issue last year: https://maskray.me/blog/2021-10-10-when-can-glibc-be-built-with-clang#aliasing-an-asm-label

The issue is that when the alias target (__GI___nptl_threads_events) is the asm label name of a declaration, Clang
cannot tell the alias target is actually defined. It conservatively (you may call it wrong) reports an error.

Reviewed-by: Fangrui Song <maskray@google.com>

>---
> nptl/pthread_create.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
>diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
>index 32ae2f4b2f..34a41a0fdf 100644
>--- a/nptl/pthread_create.c
>+++ b/nptl/pthread_create.c
>@@ -45,13 +45,15 @@
>
>
> /* Globally enabled events.  */
>-td_thr_events_t __nptl_threads_events;
>+extern td_thr_events_t __nptl_threads_events;
> libc_hidden_proto (__nptl_threads_events)
>+td_thr_events_t __nptl_threads_events;
> libc_hidden_data_def (__nptl_threads_events)
>
> /* Pointer to descriptor with the last event.  */
>-struct pthread *__nptl_last_event;
>+extern struct pthread *__nptl_last_event;
> libc_hidden_proto (__nptl_last_event)
>+struct pthread *__nptl_last_event;
> libc_hidden_data_def (__nptl_last_event)
>
> #ifdef SHARED
>-- 
>2.34.1
>
  
Adhemerval Zanella Oct. 31, 2022, 8:03 p.m. UTC | #2
On 29/10/22 01:51, Fangrui Song wrote:
> On 2022-10-28, Adhemerval Zanella via Libc-alpha wrote:
>> clang complains that libc_hidden_data_def (__nptl_threads_events)
>> creates an invalid alias:
>>
>>  pthread_create.c:50:1: error: alias must point to a defined variable or function
>>  libc_hidden_data_def (__nptl_threads_events)
>>  ^
>>  ../include/libc-symbols.h:621:37: note: expanded from macro
>>  'libc_hidden_data_def'
>>
>> It seems that clang requires that a proper prototype is defined prior
>> the hidden alias creation.
> 
> I investigated the issue last year: https://maskray.me/blog/2021-10-10-when-can-glibc-be-built-with-clang#aliasing-an-asm-label
> 
> The issue is that when the alias target (__GI___nptl_threads_events) is the asm label name of a declaration, Clang
> cannot tell the alias target is actually defined. It conservatively (you may call it wrong) reports an error.
> 
> Reviewed-by: Fangrui Song <maskray@google.com>

Thanks.

> 
>> ---
>> nptl/pthread_create.c | 6 ++++--
>> 1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
>> index 32ae2f4b2f..34a41a0fdf 100644
>> --- a/nptl/pthread_create.c
>> +++ b/nptl/pthread_create.c
>> @@ -45,13 +45,15 @@
>>
>>
>> /* Globally enabled events.  */
>> -td_thr_events_t __nptl_threads_events;
>> +extern td_thr_events_t __nptl_threads_events;
>> libc_hidden_proto (__nptl_threads_events)
>> +td_thr_events_t __nptl_threads_events;
>> libc_hidden_data_def (__nptl_threads_events)
>>
>> /* Pointer to descriptor with the last event.  */
>> -struct pthread *__nptl_last_event;
>> +extern struct pthread *__nptl_last_event;
>> libc_hidden_proto (__nptl_last_event)
>> +struct pthread *__nptl_last_event;
>> libc_hidden_data_def (__nptl_last_event)
>>
>> #ifdef SHARED
>> -- 
>> 2.34.1
>>
  

Patch

diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
index 32ae2f4b2f..34a41a0fdf 100644
--- a/nptl/pthread_create.c
+++ b/nptl/pthread_create.c
@@ -45,13 +45,15 @@ 
 
 
 /* Globally enabled events.  */
-td_thr_events_t __nptl_threads_events;
+extern td_thr_events_t __nptl_threads_events;
 libc_hidden_proto (__nptl_threads_events)
+td_thr_events_t __nptl_threads_events;
 libc_hidden_data_def (__nptl_threads_events)
 
 /* Pointer to descriptor with the last event.  */
-struct pthread *__nptl_last_event;
+extern struct pthread *__nptl_last_event;
 libc_hidden_proto (__nptl_last_event)
+struct pthread *__nptl_last_event;
 libc_hidden_data_def (__nptl_last_event)
 
 #ifdef SHARED