c: Don't warn the builtin declaration of stack protection guard

Message ID CAMe9rOqC+mmFmq5FgogZ9peBNac5LWckmtVHvGw_sRBh8A_pfQ@mail.gmail.com
State New
Headers
Series c: Don't warn the builtin declaration of stack protection guard |

Commit Message

H.J. Lu May 6, 2026, 1:07 p.m. UTC
  On Wed, May 6, 2026 at 2:00 PM Iain Sandoe <idsandoe@googlemail.com> wrote:
>
> Hi H.J.
>
> How was your original patch actually tested?

I tested it on Linux.  Since -mstack-protector-guard=global isn't the default,
gcc.dg/Wnested-externs-1.c doesn't fail on Linux.

> It seems that it makes changes that affect all the sub-ports of X86.
>
> > On 6 May 2026, at 04:15, Rainer Orth <ro@cebitec.uni-bielefeld.de> wrote:
> >
> > Hi H.J.,
> >> On Wed, May 6, 2026 at 6:05 AM Jakub Jelinek <jakub@redhat.com> wrote:
> >>>
> >>> On Tue, May 05, 2026 at 11:20:34PM +0200, Jakub Jelinek wrote:
> >>>> On Tue, May 05, 2026 at 02:15:54PM -0700, Andrew Pinski wrote:
> >>>>>> If __stack_chk_guard is user provided, it needs to be changed to uintptr_t.
> >>>>
> >>>>>> commit c05b5e5d8cb660ed43159d66fd669c20746d6bea
> >>>>>> Author: H.J. Lu <hjl.tools@gmail.com>
> >>>>>> Date:   Fri Sep 12 18:52:39 2025 -0700
> >>>>>>
> >>>>>>    c/c++: Declare stack protection guard as a global symbol
> >>>>>>
> >>>>>> declared __stack_chk_guard as uintptr_t if it is an internal global
> >>>>>> symbol.  Change __stack_chk_guard in libssp to match the internal
> >>>>>> symbol type.
> >>>>>>
> >>>>>> PR c/121911
> >>>>>> * ssp.c: Include <stdint.h>.  Change __stack_chk_guard to
> >>>>>> uintptr_t.
> >>>>>
> >>>>> Ok.
> >>>>
> >>>> This looks wrong to me.
> >>>> __stack_chk_guard is void * on all targets but x86 and on it only in case of
> >>>> one command line option.
> >>>> So, this change would break all targets but some x86 cases?
> >>>>
> >>>>      if (targetm.stack_protect_guard_symbol_p ())
> >>>>        t = lang_hooks.types.type_for_mode (ptr_mode, 1);
> >>>>      else
> >>>>        t = ptr_type_node;
> >>>>      t = build_decl (UNKNOWN_LOCATION,
> >>>>                      VAR_DECL, get_identifier ("__stack_chk_guard"), t);
> >>>>
> >>>> Why was this even changed when GCC 15 always used void *?
> >>>
> >>> Not to mention that lang_hooks.types.type_for_mode (ptr_mode, 1)
> >>> is not really uintptr_t.
> >>> It is the first of unsigned int, unsigned char, unsigned short, unsigned
> >>> long, unsigned long long, unsigned __intXX which has the same mode
> >>> as pointer.
> >>> While uintptr_t is whatever type the target/ABI decided to be that type,
> >>> of course C mandates that it effectively has at least the precision of a
> >>> pointer, but which of the types with the same precision it is or if it isn't
> >>> some larger type can vary.
> >>>
> >>
> >> Since __stack_chk_guard is defined in C, maybe C++, we only need
> >> lang_hooks.types.type_for_mode to return uintptr_t in cc1 or cc1plus.
> >> What should be passed to ang_hooks.types.type_for_mode to get
> >> uintptr_t?
> >
> > while your patch fixes Solaris/x86 bootstrap, it introduces a new
> > failures and two regressions:
> >
> > +FAIL: g++.target/i386/ssp-global-hidden-3.C  -std=gnu++20 (test for excess errors)
> > +UNRESOLVED: g++.target/i386/ssp-global-hidden-3.C  -std=gnu++20 compilation failed to produce executable
> >
> > 32-bit only with Solaris ld (gld build still running), same with gnu++26
> > and gnu++98:
> >
> > Excess errors:
> > Undefined                       first referenced
> > symbol                             in file
> > __stack_chk_fail_local              ./ssp-global-hidden-3.o  (symbol scope specifies local binding)
> >
> > +FAIL: gcc.target/i386/ssp-global-hidden-3.c (test for excess errors)
> > +UNRESOLVED: gcc.target/i386/ssp-global-hidden-3.c compilation failed to produce executable
> >
> > Same.
> >
> > +FAIL: gcc.dg/Wnested-externs-1.c (test for excess errors)
> > +FAIL: gcc.dg/Wnested-externs-2.c (test for excess errors)
> >
> > Excess errors:
> > cc1: warning: nested extern declaration of '__stack_chk_guard' [-Wnested-externs]
>
> These also failed on my overnight test of r17-344-g2dde6c91d0f0 on x86_64-darwin (64b) with:
>
> FAIL: gcc.dg/Wnested-externs-1.c (test for excess errors)
> Excess errors:
> cc1: warning: nested extern declaration of '__stack_chk_guard' [-Wnested-externs]
>

__stack_chk_guard is the builtin declaration of stack protection guard.
Since its DECL_CONTEXT is set to current_function_decl, -Wnested-externs
warns:

cc1: warning: nested extern declaration of '__stack_chk_guard'
[-Wnested-externs]

Update pushdecl not to warn it.

gcc/

PR c/121911
* c/c-decl.cc (pushdecl): Don't warn the builtin declaration of
stack protection guard.

gcc/testsuite/

PR c/121911
* gcc.dg/Wnested-externs-1.c: Add -mstack-protector-guard=global
for x86.
  

Patch

From 33bf7d81b77f37e7681d280949f25d0c1e11c928 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 6 May 2026 20:58:47 +0800
Subject: [PATCH] c: Don't warn the builtin declaration of stack protection
 guard

__stack_chk_guard is the builtin declaration of stack protection guard.
Since its DECL_CONTEXT is set to current_function_decl, -Wnested-externs
warns:

cc1: warning: nested extern declaration of '__stack_chk_guard' [-Wnested-externs]

Update pushdecl not to warn it.

gcc/

	PR c/121911
	* c/c-decl.cc (pushdecl): Don't warn the builtin declaration of
	stack protection guard.

gcc/testsuite/

	PR c/121911
	* gcc.dg/Wnested-externs-1.c: Add -mstack-protector-guard=global
	for x86.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 gcc/c/c-decl.cc                          | 5 ++++-
 gcc/testsuite/gcc.dg/Wnested-externs-1.c | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index cc07e05e336..a4f13dd26fe 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -3551,8 +3551,11 @@  pushdecl (tree x)
 	  visdecl = b->decl;
 	  vistype = TREE_TYPE (visdecl);
 	}
+      /* Don't warn the builtin declaration of stack protection guard.  */
       if (scope != file_scope
-	  && !DECL_IN_SYSTEM_HEADER (x))
+	  && !DECL_IN_SYSTEM_HEADER (x)
+	  && (!targetm.stack_protect_guard_symbol_p ()
+	      || x != targetm.stack_protect_guard ()))
 	warning_at (locus, OPT_Wnested_externs,
 		    "nested extern declaration of %qD", x);
 
diff --git a/gcc/testsuite/gcc.dg/Wnested-externs-1.c b/gcc/testsuite/gcc.dg/Wnested-externs-1.c
index 5c4b5ddd8e2..a93c6f6006a 100644
--- a/gcc/testsuite/gcc.dg/Wnested-externs-1.c
+++ b/gcc/testsuite/gcc.dg/Wnested-externs-1.c
@@ -2,6 +2,7 @@ 
 /* Origin: Joseph Myers <joseph@codesourcery.com> */
 /* { dg-do compile } */
 /* { dg-options "-Wnested-externs" } */
+/* { dg-additional-options "-mstack-protector-guard=global" { target x86_64-*-* i?86-*-* } } */
 
 int a;
 static int b;
-- 
2.54.0