default_stack_protect_guard calls

  lang_hooks.types.type_for_mode (ptr_mode, 1);

to get an integer type for __stack_chk_guard which is declared as a
global symbol of type uintptr_t.  For 32-bit systems, uintptr_t may
be either unsigned int or unsigned long int.  On 32-bit Darwin, we get

$ cat /tmp/x.c
__UINTPTR_TYPE__ __stack_chk_guard = 0x1000;
$ ./xgcc -B./ -S /tmp/x.c -m32
/tmp/x.c:1:18: error: conflicting types for ‘__stack_chk_guard’; have
‘long unsigned int’
    1 | __UINTPTR_TYPE__ __stack_chk_guard = 0x1000;
      |                  ^~~~~~~~~~~~~~~~~
cc1: note: previous declaration of ‘__stack_chk_guard’ with type ‘unsigned int’
$

since lang_hooks.types.type_for_mode returns unsigned int while Darwin's
uintptr_t is unsigned long int.  Update default_stack_protect_guard to
check UINTPTR_TYPE to get unsigned integer type for uintptr_t instead.

gcc/c-family/

PR c/125226
* targhooks.cc (default_stack_protect_guard): Check UINTPTR_TYPE
to get unsigned integer type for uintptr_t.

OK for mastter?

Thanks.

-- 
H.J.
From 08f9ffa7967418feffdd905d5f9617a3abdcddd2 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <[email protected]>
Date: Fri, 8 May 2026 12:20:02 +0800
Subject: [PATCH] SSP: Check UINTPTR_TYPE to get uintptr_t type
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

default_stack_protect_guard calls

  lang_hooks.types.type_for_mode (ptr_mode, 1);

to get an integer type for __stack_chk_guard which is declared as a
global symbol of type uintptr_t.  For 32-bit systems, uintptr_t may
be either unsigned int or unsigned long int.  On 32-bit Darwin, we get

$ cat /tmp/x.c
__UINTPTR_TYPE__ __stack_chk_guard = 0x1000;
$ ./xgcc -B./ -S /tmp/x.c -m32
/tmp/x.c:1:18: error: conflicting types for ‘__stack_chk_guard’; have ‘long unsigned int’
    1 | __UINTPTR_TYPE__ __stack_chk_guard = 0x1000;
      |                  ^~~~~~~~~~~~~~~~~
cc1: note: previous declaration of ‘__stack_chk_guard’ with type ‘unsigned int’
$

since lang_hooks.types.type_for_mode returns unsigned int while Darwin's
uintptr_t is unsigned long int.  Update default_stack_protect_guard to
check UINTPTR_TYPE to get unsigned integer type for uintptr_t instead.

gcc/c-family/

	PR c/125226
	* targhooks.cc (default_stack_protect_guard): Check UINTPTR_TYPE
	to get unsigned integer type for uintptr_t.

Signed-off-by: H.J. Lu <[email protected]>
---
 gcc/targhooks.cc | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/gcc/targhooks.cc b/gcc/targhooks.cc
index 295f70cb3f5..aae7eb49182 100644
--- a/gcc/targhooks.cc
+++ b/gcc/targhooks.cc
@@ -942,7 +942,36 @@ default_stack_protect_guard (void)
       rtx x;
 
       if (targetm.stack_protect_guard_symbol_p ())
-	t = lang_hooks.types.type_for_mode (ptr_mode, 1);
+	{
+	  /* Get unsigned integer type for uintptr_t.  */
+	  if (strcmp (UINTPTR_TYPE, "unsigned int") == 0)
+	    t = unsigned_type_node;
+	  else if (strcmp (UINTPTR_TYPE, "long unsigned int") == 0)
+	    t = long_unsigned_type_node;
+	  else if (strcmp (UINTPTR_TYPE, "long long unsigned int") == 0)
+	    t = long_long_unsigned_type_node;
+	  else if (strcmp (UINTPTR_TYPE, "short unsigned int") == 0)
+	    t = short_unsigned_type_node;
+	  else
+	    {
+	      int i;
+
+	      t = nullptr;
+	      for (i = 0; i < NUM_INT_N_ENTS; i++)
+		if (int_n_enabled_p[i])
+		  {
+		    char name[50], altname[50];
+		    sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
+		    sprintf (altname, "__int%d__ unsigned", int_n_data[i].bitsize);
+
+		    if (strcmp (name, UINTPTR_TYPE) == 0
+			|| strcmp (altname, UINTPTR_TYPE) == 0)
+		      t = int_n_trees[i].unsigned_type;
+		  }
+	      if (!t)
+		gcc_unreachable ();
+	    }
+	}
       else
 	t = ptr_type_node;
       t = build_decl (UNKNOWN_LOCATION,
-- 
2.54.0

Reply via email to