varasm.c (bss_initializer_p): Do not put constants into .bss
authorJoerg Sonnenberger <joerg@bec.de>
Fri, 1 Sep 2017 16:26:00 +0000 (10:26 -0600)
committerJeff Law <law@gcc.gnu.org>
Fri, 1 Sep 2017 16:26:00 +0000 (10:26 -0600)
* varasm.c (bss_initializer_p): Do not put constants into .bss
(categorize_decl_for_section): Handle bss_initializer_p returning
false when DECL_INITIAL is NULL.

* gcc.target/i386/const-in-bss.c: New test.

From-SVN: r251602

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/i386/const-in-bss.c [new file with mode: 0644]
gcc/varasm.c

index e71380e5c9dd3a54afe64c44e405412c1c012255..f9d9eb74a3aa28b2d01b3a324d62d19be106d48e 100644 (file)
@@ -1,3 +1,10 @@
+2017-09-01  Joerg Sonnenberger  <joerg@bec.de>
+           Jeff Law  <law@redhat.com>
+
+       * varasm.c (bss_initializer_p): Do not put constants into .bss
+       (categorize_decl_for_section): Handle bss_initializer_p returning
+       false when DECL_INITIAL is NULL.
+
 2017-09-01  Andreas Krebbel  <krebbel@linux.vnet.ibm.com>
 
        PR target/82012
index fe6e4301bff99bff080f92def994e52cefd18bc5..e82751438d65691971f65aba6903679652b9a29a 100644 (file)
@@ -5,6 +5,8 @@
 
 2017-09-01  Jeff Law  <law@redhat.com>
 
+       * gcc.target/i386/const-in-bss.c: New test.
+
        PR tree-optimization/82052
        * gcc.c-torture/compile/pr82052.c: New test.
 
diff --git a/gcc/testsuite/gcc.target/i386/const-in-bss.c b/gcc/testsuite/gcc.target/i386/const-in-bss.c
new file mode 100644 (file)
index 0000000..c70aa0b
--- /dev/null
@@ -0,0 +1,6 @@
+/* { dg-do compile { target *-*-linux* } } */
+
+__attribute__((section("readonly1"))) const int foo1c;
+
+/* { dg-final { scan-assembler "readonly1,\"a\"" } } */
+/* { dg-final { scan-assembler-not "readonly1,\"aw\"" } } */
index e5393377a4337d9224ebe0591003b6db2b59b37a..d38d2c2721b1f44927fbdbe966b7fe3b4b5911bc 100644 (file)
@@ -976,16 +976,16 @@ decode_reg_name (const char *name)
 bool
 bss_initializer_p (const_tree decl)
 {
-  return (DECL_INITIAL (decl) == NULL
-         /* In LTO we have no errors in program; error_mark_node is used
-            to mark offlined constructors.  */
-         || (DECL_INITIAL (decl) == error_mark_node
-             && !in_lto_p)
-         || (flag_zero_initialized_in_bss
-             /* Leave constant zeroes in .rodata so they
-                can be shared.  */
-             && !TREE_READONLY (decl)
-             && initializer_zerop (DECL_INITIAL (decl))));
+  /* Do not put constants into the .bss section, they belong in a readonly
+     section.  */
+  return (!TREE_READONLY (decl)
+         && (DECL_INITIAL (decl) == NULL
+             /* In LTO we have no errors in program; error_mark_node is used
+                to mark offlined constructors.  */
+             || (DECL_INITIAL (decl) == error_mark_node
+                 && !in_lto_p)
+             || (flag_zero_initialized_in_bss
+                 && initializer_zerop (DECL_INITIAL (decl)))));
 }
 
 /* Compute the alignment of variable specified by DECL.
@@ -6517,7 +6517,8 @@ categorize_decl_for_section (const_tree decl, int reloc)
        ret = SECCAT_BSS;
       else if (! TREE_READONLY (decl)
               || TREE_SIDE_EFFECTS (decl)
-              || ! TREE_CONSTANT (DECL_INITIAL (decl)))
+              || (DECL_INITIAL (decl)
+                  && ! TREE_CONSTANT (DECL_INITIAL (decl))))
        {
          /* Here the reloc_rw_mask is not testing whether the section should
             be read-only or not, but whether the dynamic link will have to
@@ -6537,7 +6538,8 @@ categorize_decl_for_section (const_tree decl, int reloc)
           location.  -fmerge-all-constants allows even that (at the
           expense of not conforming).  */
        ret = SECCAT_RODATA;
-      else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
+      else if (DECL_INITIAL (decl)
+              && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
        ret = SECCAT_RODATA_MERGE_STR_INIT;
       else
        ret = SECCAT_RODATA_MERGE_CONST;