Tested on x86_64, i686 darwin, pushed to trunk, thanks
Iain

--- 8< ---

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>
---
 gcc/config/darwin-sections.def                |  7 ++++
 gcc/config/darwin.cc                          | 39 ++++++++++++++++---
 .../g++.dg/torture/darwin-cfstring-3.C        |  8 ++--
 .../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 44adcc6062d..76587c268ce 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 1724084b628..75ac3560954 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 ee4b385b17f..4be3a2526c3 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 ee4b385b17f..4be3a2526c3 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 } } } 
} } */
-- 
2.39.2 (Apple Git-143)

Reply via email to