check initializer to be zero in .bss-like sections
authorJan Beulich <jbeulich@suse.com>
Fri, 1 Jul 2016 14:23:24 +0000 (14:23 +0000)
committerJan Beulich <jbeulich@gcc.gnu.org>
Fri, 1 Jul 2016 14:23:24 +0000 (14:23 +0000)
Just like gas, which has recently learned to reject such initializers,
gcc shouldn't accept such either.

gcc/
2016-07-01  Jan Beulich  <jbeulich@suse.com>

* varasm.c (get_variable_section): Validate initializer in
named .bss-like sections.

gcc/testsuite/
2016-07-01  Jan Beulich  <jbeulich@suse.com>

* gcc.dg/bss.c: New.

From-SVN: r237913

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/bss.c [new file with mode: 0644]
gcc/varasm.c

index 3295534da350b7c842f0a3db2634874a7b42ee6a..4020a4e37d4046d68f990eaf166088ace9f15db2 100644 (file)
@@ -1,3 +1,8 @@
+2016-07-01  Jan Beulich  <jbeulich@suse.com>
+
+       * varasm.c (get_variable_section): Validate initializer in
+       named .bss-like sections.
+
 2016-07-01  Kelvin Nilsen  <kelvin@gcc.gnu.org>
 
        * config/rs6000/altivec.md (*altivec_vpermr_<mode>_internal):
index 1eca9b2bcdfd47b3deaeb3737a17a370e322c704..382c172c77800f3aede4bcf3805822e96fd7ebb9 100644 (file)
@@ -1,3 +1,7 @@
+2016-07-01  Jan Beulich  <jbeulich@suse.com>
+
+       * gcc.dg/bss.c: New.
+
 2016-07-01  Peter Bergner  <bergner@vnet.ibm.com>
 
        PR target/71698
diff --git a/gcc/testsuite/gcc.dg/bss.c b/gcc/testsuite/gcc.dg/bss.c
new file mode 100644 (file)
index 0000000..af8d0c6
--- /dev/null
@@ -0,0 +1,8 @@
+/* Test non-zero initializers in .bss-like sections get properly refused.  */
+/* { dg-do compile } */
+/* { dg-require-named-sections "" } */
+
+int __attribute__((section(".bss.local"))) x = 1; /* { dg-error "" "zero init" } */
+int *__attribute__((section(".bss.local"))) px = &x; /* { dg-error "" "zero init" } */
+int __attribute__((section(".bss.local"))) y = 0;
+int *__attribute__((section(".bss.local"))) py = (void*)0;
index de8bcd6f20c823acd03991f813da3521b80547ff..6a8fb81e41f95a5815e6c017caa31d2728a92183 100644 (file)
@@ -1150,7 +1150,18 @@ get_variable_section (tree decl, bool prefer_noswitch_p)
 
   resolve_unique_section (decl, reloc, flag_data_sections);
   if (IN_NAMED_SECTION (decl))
-    return get_named_section (decl, NULL, reloc);
+    {
+      section *sect = get_named_section (decl, NULL, reloc);
+
+      if ((sect->common.flags & SECTION_BSS) && !bss_initializer_p (decl))
+       {
+         error_at (DECL_SOURCE_LOCATION (decl),
+                   "only zero initializers are allowed in section %qs",
+                   sect->named.name);
+         DECL_INITIAL (decl) = error_mark_node;
+       }
+      return sect;
+    }
 
   if (ADDR_SPACE_GENERIC_P (as)
       && !DECL_THREAD_LOCAL_P (decl)