On 1/28/26 8:38 AM, H.J. Lu wrote:
On Mon, Jan 26, 2026 at 8:49 PM Jason Merrill <[email protected]> wrote:

On 12/14/25 8:10 AM, H.J. Lu wrote:
On Sat, Dec 13, 2025 at 5:33 PM Stefan Schulze Frielinghaus
<[email protected]> wrote:

On Sun, Oct 26, 2025 at 10:29:30AM +0800, H.J. Lu wrote:
On Sat, Oct 25, 2025 at 4:42 PM H.J. Lu <[email protected]> wrote:
...
Here is the v4 patch.

Add a new target hook, stack_protect_guard_symbol, to support the user
provided stack protection guard as an internal symbol.  If the hook is
true,

1. Make __stack_chk_guard an internal C/C++ symbol.
2. Declare __stack_chk_guard as a size_t variable if size_t has the same
size as pointer so that it can be initialized as an integer.
3. If the user declared variable matches __stack_chk_guard, merge it
with __stack_chk_guard, including its visibility attribute.
4. Define the __stack_protection_guard_is_internal_symbol__ macro to
indicate that __stack_chk_guard is an internal symbol.

gcc/

PR c/121911
* target.def (stack_protect_guard_symbol): New target hook.
* targhooks.cc (default_stack_protect_guard): If
targetm.stack_protect_guard_symbol is true, make it an internal
symbol and use size_type_node if it has the same size as
ptr_type_node.
* tree.cc (build_common_tree_nodes): If stack protector is
enabled and targetm.stack_protect_guard_symbol is true, call
targetm.stack_protect_guard.
* config/i386/i386.cc (TARGET_STACK_PROTECT_GUARD_SYMBOL): New.
* doc/tm.texi: Regenerated.
* doc/tm.texi.in (TARGET_STACK_PROTECT_GUARD_SYMBOL): New.

gcc/c-family/

PR c/121911
* c-common.cc (matching_stack_protect_guard_decl_p): New.
* c-common.h (matching_stack_protect_guard_decl_p): Likewise.
* c-cppbuiltin.cc (c_cpp_builtins): Define the
__stack_protection_guard_is_internal_symbol__ macro if
targetm.stack_protect_guard_symbol is true.

gcc/c/

PR c/121911
* c-decl.cc: Include "c-family/c-common.h".
(merge_decls): Add a bool argument to keep the old type and copy
the old type if true.
(duplicate_decls): If the old decl is the stack protect guard and
the new decl matches the old decl, keep the type of the old decl.

gcc/cp/

PR c/121911
* decl.cc: Include "c-family/c-common.h".
(duplicate_decls): 2 types match if the old decl is the stack
protect guard and the new decl matches the old decl.

gcc/testsuite/

PR c/121911
* g++.target/i386/ssp-global-1.C: New test.
* g++.target/i386/ssp-global-2.C: Likewise.
* g++.target/i386/ssp-global-3.C: Likewise.
* g++.target/i386/ssp-global-4.C: Likewise.
* g++.target/i386/ssp-global-hidden-1.C: Likewise.
* g++.target/i386/ssp-global-hidden-2.C: Likewise.
* g++.target/i386/ssp-global-hidden-3.C: Likewise.
* gcc.target/i386/ssp-global-2.c: Likewise.
* gcc.target/i386/ssp-global-3.c: Likewise.
* gcc.target/i386/ssp-global-4.c: Likewise.
* gcc.target/i386/ssp-global-hidden-1.c: Likewise.
* gcc.target/i386/ssp-global-hidden-2.c: Likewise.
* gcc.target/i386/ssp-global-hidden-3.c: Likewise.


Here is the v5 patch.  The main difference is that __stack_chk_guard
is an internal symbol with C linkage now.


Here is the v6 patch to rename the target hook, stack_protect_guard_symbol,
to stack_protect_guard_symbol_p.

Add a new target hook, stack_protect_guard_symbol_p, to support the user
provided stack protection guard as an internal symbol.  If the hook
returns true,

1. Make __stack_chk_guard an internal C/C++ symbol.
2. Declare __stack_chk_guard as a size_t variable if size_t has the same
size as pointer so that it can be initialized as an integer.
3. If the user declared variable matches __stack_chk_guard, merge it
with __stack_chk_guard, including its visibility attribute.
4. Define the __stack_protection_guard_is_internal_symbol__ macro to
indicate that __stack_chk_guard is an internal symbol.

gcc/

PR c/121911
* target.def (stack_protect_guard_symbol_p): New target hook.
* targhooks.cc (default_stack_protect_guard): If the
stack_protect_guard_symbol_p hook returns true, use size_type_node
if it has the same size as ptr_type_node.
* config/i386/i386.cc (ix86_stack_protect_guard_symbol_p): New.
(TARGET_STACK_PROTECT_GUARD_SYMBOL_P): Likewise.
* doc/tm.texi: Regenerated.
* doc/tm.texi.in (TARGET_STACK_PROTECT_GUARD_SYMBOL_P): New.

gcc/c-family/

PR c/121911
* c-common.cc (stack_protect_guard_decl): New.
(pushing_stack_protect_guard_decl): Likewise.
(c_stack_protect_guard_decl_p): Likewise.
(duplicate_stack_protect_guard_decl_p): Likewise.
(c_common_nodes_and_builtins): If the stack_protect_guard_symbol_p
hook returns true, call targetm.stack_protect_guard.
* c-common.h (c_stack_protect_guard_decl_p): New.
(duplicate_stack_protect_guard_decl_p): Likewise.
* c-cppbuiltin.cc (c_cpp_builtins): Define the
__stack_protection_guard_is_internal_symbol__ macro if the
stack_protect_guard_symbol_p hook returns.

gcc/c/

PR c/121911
* c-decl.cc: Include "c-family/c-common.h".
(merge_decls): Add a bool argument to keep the old type and copy
the old type if true.
(duplicate_decls): If the old decl is the stack protect guard and
the new decl matches the old decl, keep the type of the old decl.

gcc/cp/

PR c/121911
* cp-objcp-common.cc (cp_pushdecl): Change the stack protection
guard symbol to C linkage.
* decl.cc: Include "c-family/c-common.h".
(duplicate_decls): 2 types match if the old decl is the stack
protect guard and the new decl matches the old decl.

gcc/testsuite/

PR c/121911
* g++.target/i386/ssp-global-1.C: New test.
* g++.target/i386/ssp-global-2.C: Likewise.
* g++.target/i386/ssp-global-3.C: Likewise.
* g++.target/i386/ssp-global-4.C: Likewise.
* g++.target/i386/ssp-global-hidden-1.C: Likewise.
* g++.target/i386/ssp-global-hidden-2.C: Likewise.
* g++.target/i386/ssp-global-hidden-3.C: Likewise.
* gcc.target/i386/ssp-global-2.c: Likewise.
* gcc.target/i386/ssp-global-3.c: Likewise.
* gcc.target/i386/ssp-global-4.c: Likewise.
* gcc.target/i386/ssp-global-hidden-1.c: Likewise.
* gcc.target/i386/ssp-global-hidden-2.c: Likewise.
* gcc.target/i386/ssp-global-hidden-3.c: Likewise.

+/* The stack protection guard.  */
+static tree stack_protect_guard_decl;

I don't see why we need a local copy of targetm.stack_protect_guard()?

I will drop it.

+/* True if the stack protection guard is being pushed.  */
+static bool pushing_stack_protect_guard_decl;

...or a flag in addition to comparing with stack_protect_guard().

It seems like a lot of the complication of this patch is to allow
declarations with a different type, contrary to every other declaration.

   This is quite novel, and it's not clear

bool
duplicate_stack_protect_guard_decl_p (tree newdecl, tree olddecl)
{
   if (newdecl != error_mark_node
       && c_stack_protect_guard_decl_p (olddecl))
     {
       /* Allow different integer types with the same size.  */
       tree oldtype = TREE_TYPE (olddecl);
       tree newtype = TREE_TYPE (newdecl);
       if (TYPE_CANONICAL (newtype) == TYPE_CANONICAL (oldtype)
           || (TREE_CODE (newtype) == INTEGER_TYPE
               && TREE_CODE (oldtype) == INTEGER_TYPE
               && (TYPE_PRECISION (newtype)
                   == TYPE_PRECISION (oldtype))))
         return true;
     }

   return false;
}

is to allow

const unsigned long int __stack_chk_guard;
unsigned long int __stack_chk_guard;

and

const unsigned int __stack_chk_guard;
unsigned long int __stack_chk_guard;

to me what the motivation is.
Why can't we just say it needs to be uintptr_t?

uintptr_type_node isn't available in targhooks.cc.  Can I use
size_type_node instead?

How about lang_hooks.types.type_for_size (POINTER_SIZE, 1)

Jason

Reply via email to