https://gcc.gnu.org/g:4db9571488eb9f39f0b06435a8642a4f11e82dcf

commit r16-3188-g4db9571488eb9f39f0b06435a8642a4f11e82dcf
Author: Iain Sandoe <i...@sandoe.co.uk>
Date:   Sat Aug 9 08:19:08 2025 +0100

    Darwin: Handle string constants specially when asan is enabled.
    
    The Darwin ABI uses a different section for string constants when
    address sanitizing is enabled.  This adds defintions of the asan-
    specific sections and switches string constants to the correct
    section.
    
    It also makes the string constant symbols linker-visible when
    asan is enabled, but not otherwise.
    
    gcc/ChangeLog:
    
            * config/darwin-sections.def (asan_string_section,
            asan_globals_section, asan_liveness_section): New.
            * config/darwin.cc (objc_method_decl): Use asan sections
            when asan is enabled.
            (darwin_encode_section_info): Alter string constant
            linker visibility depending on asan.
            (machopic_select_section): Use the asan sections when
            asan is enabled.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/torture/darwin-cfstring-3.c: Adjust for amended
            string labels.
            * g++.dg/torture/darwin-cfstring-3.C: Likewise.
    
    Signed-off-by: Iain Sandoe <i...@sandoe.co.uk>

Diff:
---
 gcc/config/darwin-sections.def                   |  7 +++++
 gcc/config/darwin.cc                             | 39 ++++++++++++++++++++----
 gcc/testsuite/g++.dg/torture/darwin-cfstring-3.C |  8 ++---
 gcc/testsuite/gcc.dg/torture/darwin-cfstring-3.c |  8 ++---
 4 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/gcc/config/darwin-sections.def b/gcc/config/darwin-sections.def
index 44adcc6062db..76587c268cef 100644
--- a/gcc/config/darwin-sections.def
+++ b/gcc/config/darwin-sections.def
@@ -215,3 +215,10 @@ DEF_SECTION (objc2_method_names_section, 0,
 
 DEF_SECTION (objc2_method_types_section, 0,
             ".section __TEXT, __objc_methtype, cstring_literals", 1)
+
+/* ASAN sections.  */
+
+DEF_SECTION (asan_string_section, 0, ".section __TEXT, __asan_cstring", 0)
+DEF_SECTION (asan_globals_section, 0, ".section __DATA, __asan_globals", 0)
+DEF_SECTION (asan_liveness_section, 0,
+            ".section __DATA,__asan_liveness,regular,live_support", 0)
diff --git a/gcc/config/darwin.cc b/gcc/config/darwin.cc
index 1724084b6287..75ac3560954b 100644
--- a/gcc/config/darwin.cc
+++ b/gcc/config/darwin.cc
@@ -49,6 +49,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "optabs.h"
 #include "flags.h"
 #include "opts.h"
+#include "asan.h"
 
 /* Fix and Continue.
 
@@ -1303,8 +1304,9 @@ darwin_encode_section_info (tree decl, rtx rtl, int first)
       bool is_str = TREE_CODE (decl) == STRING_CST;
       rtx sym_ref = XEXP (rtl, 0);
 
-      /* If this is a string cst or not anchored we have nothing to do.  */
-      if (is_str || !SYMBOL_REF_HAS_BLOCK_INFO_P (sym_ref))
+      /* Unless this is a string cst or we are in an anchored section we have
+        nothing more to do here.  */
+      if (!is_str && !SYMBOL_REF_HAS_BLOCK_INFO_P (sym_ref))
        return;
 
       tree sym_decl = SYMBOL_REF_DECL (sym_ref);
@@ -1312,9 +1314,18 @@ darwin_encode_section_info (tree decl, rtx rtl, int 
first)
       gcc_checking_assert (strncmp ("*lC", name, 3) == 0);
 
       char *buf;
-      /* Lets identify anchored constants with a different prefix, for the
-         sake of inspection only.  */
-      buf = xasprintf ("*LaC%s", &name[3]);
+      if (is_str)
+       {
+         bool for_asan = (flag_sanitize & SANITIZE_ADDRESS)
+                          && asan_protect_global (CONST_CAST_TREE (decl));
+         /* When we are generating code for sanitized strings, the string
+            internal symbols are made visible in the object.  */
+         buf = xasprintf ("*%c.str.%s", for_asan ? 'l' : 'L', &name[3]);
+       }
+      else
+       /* Lets identify anchored constants with a different prefix, for the
+          sake of inspection only.  */
+       buf = xasprintf ("*LaC%s", &name[3]);
       if (sym_decl)
        DECL_NAME (sym_decl) = get_identifier (buf);
       XSTR (sym_ref, 0) = ggc_strdup (buf);
@@ -1706,6 +1717,17 @@ machopic_select_section (tree decl,
 
   ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
 
+  /* Trump categorize_decl_for_section () for ASAN stuff - the Darwin
+     categorisations are special.  */
+  if (flag_sanitize & SANITIZE_ADDRESS)
+    {
+      if (TREE_CODE (decl) == STRING_CST
+         && asan_protect_global (CONST_CAST_TREE (decl)))
+       {
+         return darwin_sections[asan_string_section];
+       }
+    }
+
   switch (categorize_decl_for_section (decl, reloc))
     {
     case SECCAT_TEXT:
@@ -1722,7 +1744,12 @@ machopic_select_section (tree decl,
       break;
 
     case SECCAT_RODATA_MERGE_STR_INIT:
-      base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), 
align);
+      if ((flag_sanitize & SANITIZE_ADDRESS)
+          && asan_protect_global (CONST_CAST_TREE (decl)))
+       /* or !flag_merge_constants */
+       return darwin_sections[asan_string_section];
+      else
+       return darwin_mergeable_string_section (DECL_INITIAL (decl), align);
       break;
 
     case SECCAT_RODATA_MERGE_CONST:
diff --git a/gcc/testsuite/g++.dg/torture/darwin-cfstring-3.C 
b/gcc/testsuite/g++.dg/torture/darwin-cfstring-3.C
index ee4b385b17fa..4be3a2526c3e 100644
--- a/gcc/testsuite/g++.dg/torture/darwin-cfstring-3.C
+++ b/gcc/testsuite/g++.dg/torture/darwin-cfstring-3.C
@@ -24,7 +24,7 @@ void foo(void) {
   s0 = s1;
 }
 
-/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\t.*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } } */
-/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\t.*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\tL\.str\..*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } 
} */
+/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\tL\.str\..*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } 
} } */
diff --git a/gcc/testsuite/gcc.dg/torture/darwin-cfstring-3.c 
b/gcc/testsuite/gcc.dg/torture/darwin-cfstring-3.c
index ee4b385b17fa..4be3a2526c3e 100644
--- a/gcc/testsuite/gcc.dg/torture/darwin-cfstring-3.c
+++ b/gcc/testsuite/gcc.dg/torture/darwin-cfstring-3.c
@@ -24,7 +24,7 @@ void foo(void) {
   s0 = s1;
 }
 
-/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+\[lL\]C.*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
-/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\t.*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } } */
-/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\t.*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+4\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler "\\.long\[ 
\\t\]+___CFConstantStringClassReference\n\[ \\t\]*\\.long\[ \\t\]+1992\n\[ 
\\t\]*\\.long\[ \\t\]+L\.str\..*\n\[ \\t\]*\\.long\[ \\t\]+10\n" { target { 
*-*-darwin* && { ! lp64 } } } } } */
+/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\tL\.str\..*\n\t.quad\t4\n" { target { *-*-darwin* && {  lp64 } } } } 
} */
+/* { dg-final { scan-assembler 
".quad\t___CFConstantStringClassReference\n\t.long\t1992\n\t.space 
4\n\t.quad\tL\.str\..*\n\t.quad\t10\n" { target { *-*-darwin* && {  lp64 } } } 
} } */

Reply via email to