From 4ce6622a51c1bcbe501289b856f1fb1bb59c082e Mon Sep 17 00:00:00 2001 From: jbeulich Date: Fri, 1 Jul 2016 14:23:24 +0000 Subject: [PATCH] check initializer to be zero in .bss-like sections Just like gas, which has recently learned to reject such initializers, gcc shouldn't accept such either. gcc/ 2016-07-01 Jan Beulich * varasm.c (get_variable_section): Validate initializer in named .bss-like sections. gcc/testsuite/ 2016-07-01 Jan Beulich * gcc.dg/bss.c: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@237913 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 5 +++++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/bss.c | 8 ++++++++ gcc/varasm.c | 13 ++++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/bss.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 3295534da35..4020a4e37d4 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2016-07-01 Jan Beulich + + * varasm.c (get_variable_section): Validate initializer in + named .bss-like sections. + 2016-07-01 Kelvin Nilsen * config/rs6000/altivec.md (*altivec_vpermr__internal): diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1eca9b2bcdf..382c172c778 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2016-07-01 Jan Beulich + + * gcc.dg/bss.c: New. + 2016-07-01 Peter Bergner PR target/71698 diff --git a/gcc/testsuite/gcc.dg/bss.c b/gcc/testsuite/gcc.dg/bss.c new file mode 100644 index 00000000000..af8d0c634fe --- /dev/null +++ b/gcc/testsuite/gcc.dg/bss.c @@ -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; diff --git a/gcc/varasm.c b/gcc/varasm.c index de8bcd6f20c..6a8fb81e41f 100644 --- a/gcc/varasm.c +++ b/gcc/varasm.c @@ -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) -- 2.11.4.GIT