public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix a warning when initializing symbol_flags
@ 2020-09-26  0:42 Saagar Jha
  2020-09-26 13:06 ` Alan Modra
  0 siblings, 1 reply; 6+ messages in thread
From: Saagar Jha @ 2020-09-26  0:42 UTC (permalink / raw)
  To: binutils; +Cc: Saagar Jha

Clang on certain platforms (such as arm64) will warn if a struct is
partially initialized:

symbols.c:199:35: error: missing field 'written' initializer [-Werror,-Wmissing-field-initializers]
2895  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };

If we use empty braces we have the same effect but avoid the warning.

gas/ChangeLog:

	* symbols.c: Shorten an initializer for symbol_flags to be empty.
---
 gas/ChangeLog | 4 ++++
 gas/symbols.c | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/gas/ChangeLog b/gas/ChangeLog
index 1c692dd931..6f9e842330 100644
--- a/gas/ChangeLog
+++ b/gas/ChangeLog
@@ -1,3 +1,7 @@
+2020-09-25  Saagar Jha  <saagar@saagarjha.com>
+
+	* symbols.c: Shorten an initializer for symbol_flags to be empty.
+
 2020-09-24  Jim Wilson  <jimw@sifive.com>
 
 	PR 26400
diff --git a/gas/symbols.c b/gas/symbols.c
index d6080886be..40d7ebc586 100644
--- a/gas/symbols.c
+++ b/gas/symbols.c
@@ -196,7 +196,7 @@ static void *
 symbol_entry_find (htab_t table, const char *name)
 {
   hashval_t hash = htab_hash_string (name);
-  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };
+  symbol_entry_t needle = { { { }, hash, name, 0, 0, 0 } };
   return htab_find_with_hash (table, &needle, hash);
 }
 
-- 
2.28.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix a warning when initializing symbol_flags
  2020-09-26  0:42 [PATCH] Fix a warning when initializing symbol_flags Saagar Jha
@ 2020-09-26 13:06 ` Alan Modra
  2020-09-26 19:21   ` Hans-Peter Nilsson
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Modra @ 2020-09-26 13:06 UTC (permalink / raw)
  To: Saagar Jha; +Cc: binutils

On Fri, Sep 25, 2020 at 05:42:24PM -0700, Saagar Jha wrote:
> Clang on certain platforms (such as arm64) will warn if a struct is
> partially initialized:
> 
> symbols.c:199:35: error: missing field 'written' initializer [-Werror,-Wmissing-field-initializers]
> 2895  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };
> 
> If we use empty braces we have the same effect but avoid the warning.

But on gcc-4.9 this results in:

symbols.c: In function ‘symbol_entry_find’:
symbols.c:199:3: error: missing initializer for field ‘local_symbol’ of ‘struct symbol_flags’ [-Werror=missing-field-initializers]
   symbol_entry_t needle = { { { }, hash, name, 0, 0, 0 } };
   ^
symbols.c:39:16: note: ‘local_symbol’ declared here
   unsigned int local_symbol : 1;
                ^

So I don't think we should apply your patch.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix a warning when initializing symbol_flags
  2020-09-26 13:06 ` Alan Modra
@ 2020-09-26 19:21   ` Hans-Peter Nilsson
  2020-09-26 22:30     ` Saagar Jha
  0 siblings, 1 reply; 6+ messages in thread
From: Hans-Peter Nilsson @ 2020-09-26 19:21 UTC (permalink / raw)
  To: Alan Modra; +Cc: Saagar Jha, binutils



On Sat, 26 Sep 2020, Alan Modra via Binutils wrote:

> On Fri, Sep 25, 2020 at 05:42:24PM -0700, Saagar Jha wrote:
> > Clang on certain platforms (such as arm64) will warn if a struct is
> > partially initialized:
> >
> > symbols.c:199:35: error: missing field 'written' initializer [-Werror,-Wmissing-field-initializers]
> > 2895  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };
> >
> > If we use empty braces we have the same effect but avoid the warning.
>
> But on gcc-4.9 this results in:
>
> symbols.c: In function ?symbol_entry_find?:
> symbols.c:199:3: error: missing initializer for field ?local_symbol? of ?struct symbol_flags? [-Werror=missing-field-initializers]
>    symbol_entry_t needle = { { { }, hash, name, 0, 0, 0 } };
>    ^
> symbols.c:39:16: note: ?local_symbol? declared here
>    unsigned int local_symbol : 1;
>                 ^
>
> So I don't think we should apply your patch.

How about a memset 0 and setting .hash and .name?

I had to do that locally for an older gcc, that had a similar
warning.  I prefered that over 11 (IIRC) "0," and having to
keep them straight. :)

brgds, H-P

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix a warning when initializing symbol_flags
  2020-09-26 19:21   ` Hans-Peter Nilsson
@ 2020-09-26 22:30     ` Saagar Jha
  2020-09-30  6:12       ` Alan Modra
  0 siblings, 1 reply; 6+ messages in thread
From: Saagar Jha @ 2020-09-26 22:30 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: Alan Modra, binutils

Ah, so I just realized that this file is actually compiled as C and not a C++ file in .c clothing, so even if this didn’t warn in Clang the patch was still incorrect as {} is an invalid initializer in C and GCC is correct in flagging it (though perhaps not for the right reasons). Newer versions of Clang do correctly identify { 0 } as the correct zero initialization idiom: https://github.com/llvm/llvm-project/commit/817a3bfcdd0bbac8d74fdfdb83a08484d8f63a30, although most distributions seem to carry a version older than this.

Eleven zeroes is, as far as I can tell, the only widely-compatible, guaranteed-to-work way to initialize this without warnings. memset would generally “work” but if we’re being pedantic I believe this has only been guaranteed as of a recent C standard (C11?) So I guess I’ll have to defer to whatever the decision is here on portability versus readability. (Third option: disable the warning around this code, perhaps after checking theg version.)

> On Sep 26, 2020, at 12:21, Hans-Peter Nilsson <hp@bitrange.com> wrote:
> 
> On Sat, 26 Sep 2020, Alan Modra via Binutils wrote:
> 
>> On Fri, Sep 25, 2020 at 05:42:24PM -0700, Saagar Jha wrote:
>>> Clang on certain platforms (such as arm64) will warn if a struct is
>>> partially initialized:
>>> 
>>> symbols.c:199:35: error: missing field 'written' initializer [-Werror,-Wmissing-field-initializers]
>>> 2895  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };
>>> 
>>> If we use empty braces we have the same effect but avoid the warning.
>> 
>> But on gcc-4.9 this results in:
>> 
>> symbols.c: In function ?symbol_entry_find?:
>> symbols.c:199:3: error: missing initializer for field ?local_symbol? of ?struct symbol_flags? [-Werror=missing-field-initializers]
>>   symbol_entry_t needle = { { { }, hash, name, 0, 0, 0 } };
>>   ^
>> symbols.c:39:16: note: ?local_symbol? declared here
>>   unsigned int local_symbol : 1;
>>                ^
>> 
>> So I don't think we should apply your patch.
> 
> How about a memset 0 and setting .hash and .name?
> 
> I had to do that locally for an older gcc, that had a similar
> warning.  I prefered that over 11 (IIRC) "0," and having to
> keep them straight. :)
> 
> brgds, H-P


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix a warning when initializing symbol_flags
  2020-09-26 22:30     ` Saagar Jha
@ 2020-09-30  6:12       ` Alan Modra
  2020-09-30  6:22         ` Saagar Jha
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Modra @ 2020-09-30  6:12 UTC (permalink / raw)
  To: Saagar Jha; +Cc: Hans-Peter Nilsson, binutils

On Sat, Sep 26, 2020 at 03:30:53PM -0700, Saagar Jha wrote:
> Eleven zeroes is, as far as I can tell, the only widely-compatible,
> guaranteed-to-work way to initialize this without warnings. 

Let's do that.  I had reason today to compile binutils on an older
system with gcc-4.4.7 installed, and that warned on the current
source.

	* config/obj-elf.c (obj_elf_change_section): Rename variable to
	avoid shadowing warning.
	* symbols.c (symbol_entry_find): Init all symbol_flags fields.

diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index b1c99020a3..cd457abe5e 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -762,8 +762,8 @@ obj_elf_change_section (const char *name,
 	  /* We could be repurposing an undefined symbol here: make sure we
 	     reset sy_value to look like other section symbols in order to avoid
 	     trying to incorrectly resolve this section symbol later on.  */
-	  static const expressionS expr = { .X_op = O_constant };
-	  symbol_set_value_expression (secsym, &expr);
+	  static const expressionS exp = { .X_op = O_constant };
+	  symbol_set_value_expression (secsym, &exp);
 	  symbol_set_bfdsym (secsym, sec->symbol);
 	}
       else
diff --git a/gas/symbols.c b/gas/symbols.c
index d6080886be..26dd84b126 100644
--- a/gas/symbols.c
+++ b/gas/symbols.c
@@ -196,7 +196,8 @@ static void *
 symbol_entry_find (htab_t table, const char *name)
 {
   hashval_t hash = htab_hash_string (name);
-  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };
+  symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
+			      hash, name, 0, 0, 0 } };
   return htab_find_with_hash (table, &needle, hash);
 }
 


-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Fix a warning when initializing symbol_flags
  2020-09-30  6:12       ` Alan Modra
@ 2020-09-30  6:22         ` Saagar Jha
  0 siblings, 0 replies; 6+ messages in thread
From: Saagar Jha @ 2020-09-30  6:22 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils

Actually, you just reminded me of one more way to do this, although it’s a bit cryptic:

static struct symbol_flags zero_flags;
symbol_entry_t needle = { { zero_flags, hash, name, 0, 0, 0 } };

I had made a patch for this over the weekend but alas I forgot to send it to the list. But if you’re fine with the zeroes, that’s OK too.

Saagar Jha

> On Sep 29, 2020, at 23:12, Alan Modra <amodra@gmail.com> wrote:
> 
> On Sat, Sep 26, 2020 at 03:30:53PM -0700, Saagar Jha wrote:
>> Eleven zeroes is, as far as I can tell, the only widely-compatible,
>> guaranteed-to-work way to initialize this without warnings. 
> 
> Let's do that.  I had reason today to compile binutils on an older
> system with gcc-4.4.7 installed, and that warned on the current
> source.
> 
> 	* config/obj-elf.c (obj_elf_change_section): Rename variable to
> 	avoid shadowing warning.
> 	* symbols.c (symbol_entry_find): Init all symbol_flags fields.
> 
> diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
> index b1c99020a3..cd457abe5e 100644
> --- a/gas/config/obj-elf.c
> +++ b/gas/config/obj-elf.c
> @@ -762,8 +762,8 @@ obj_elf_change_section (const char *name,
> 	  /* We could be repurposing an undefined symbol here: make sure we
> 	     reset sy_value to look like other section symbols in order to avoid
> 	     trying to incorrectly resolve this section symbol later on.  */
> -	  static const expressionS expr = { .X_op = O_constant };
> -	  symbol_set_value_expression (secsym, &expr);
> +	  static const expressionS exp = { .X_op = O_constant };
> +	  symbol_set_value_expression (secsym, &exp);
> 	  symbol_set_bfdsym (secsym, sec->symbol);
> 	}
>       else
> diff --git a/gas/symbols.c b/gas/symbols.c
> index d6080886be..26dd84b126 100644
> --- a/gas/symbols.c
> +++ b/gas/symbols.c
> @@ -196,7 +196,8 @@ static void *
> symbol_entry_find (htab_t table, const char *name)
> {
>   hashval_t hash = htab_hash_string (name);
> -  symbol_entry_t needle = { { { 0 }, hash, name, 0, 0, 0 } };
> +  symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
> +			      hash, name, 0, 0, 0 } };
>   return htab_find_with_hash (table, &needle, hash);
> }
> 
> 
> 
> -- 
> Alan Modra
> Australia Development Lab, IBM


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-09-30  6:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-26  0:42 [PATCH] Fix a warning when initializing symbol_flags Saagar Jha
2020-09-26 13:06 ` Alan Modra
2020-09-26 19:21   ` Hans-Peter Nilsson
2020-09-26 22:30     ` Saagar Jha
2020-09-30  6:12       ` Alan Modra
2020-09-30  6:22         ` Saagar Jha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).