This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2] Use DIAG_IGNORE_NEEDS_COMMENT to silence -Wstringop-truncation


On Wed, May 16, 2018 at 7:33 PM, Carlos O'Donell <carlos@redhat.com> wrote:
> On 05/16/2018 09:54 PM, Alan Modra wrote:
>> On Mon, May 14, 2018 at 10:09:53PM -0400, Carlos O'Donell wrote:
>>> On 05/07/2018 11:32 PM, Alan Modra wrote:
>>>> Also, not all binutils users have glibc installed.  We can't depend on
>>>> a macro defined in glibc's /usr/include/features.h, __GNUC_PREREQ.
>>>
>>> Just to be clear the patch doesn't depend on glibc being installed, it
>>> just copies the presently used macros from glibc for use in binutils.
>>>
>>> ... and then uses them to fix the gcc 8 issues that crop up with -Werror.
>>
>> I checked HJ's patch again, and stand by my comment about
>> __GNUC_PREREQ.  This macro is not defined anywhere in the binutils
>> sources, nor is it defined by HJ's patch.
>
> My apologies, I misread your original statement. I completely agree.
>
> Why do we need __GNUC_PREREQ macro?
>
> You can unconditionally use the DIAG_* macros, and if you're
> __GNUC__ >= 8 then they evaluate to meaningful things, otherwise
> nothing.

Here is the patch without __GNUC_PREREQ.  OK for master branch?


-- 
H.J.
From a09fe67e984810e4188ad01ed3fe3f483c0a5896 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 7 May 2018 05:42:25 -0700
Subject: [PATCH] Use DIAG_IGNORE_NEEDS_COMMENT to silence
 -Wstringop-truncation

GCC 8 warns about destination size with -Wstringop-truncation:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643

Copy DIAG_PUSH_NEEDS_COMMENT, DIAG_POP_NEEDS_COMMENT and
DIAG_IGNORE_NEEDS_COMMENT from glibc 2.27 to silence it.

	PR binutils/23146
	* bfd-in.h (DIAG_PUSH_NEEDS_COMMENT): New.
	(DIAG_POP_NEEDS_COMMENT): Likewse.
	(_DIAG_STR1): Likewse.
	(_DIAG_STR): Likewse.
	(DIAG_IGNORE_NEEDS_COMMENT): Likewse.
	* bfd-in2.h: Regenerated.
	* elf32-arm.c (elf32_arm_nabi_write_core_note): Use
	DIAG_PUSH_NEEDS_COMMENT, DIAG_IGNORE_NEEDS_COMMENT and
	DIAG_POP_NEEDS_COMMENT to silence GCC 8 warnings with
	-Wstringop-truncation.
	* elf32-ppc.c (ppc_elf_write_core_note): Likewse.
	* elf32-s390.c (elf_s390_write_core_note): Likewse.
	* elf64-ppc.c (ppc64_elf_write_core_note): Likewse.
	* elf64-s390.c (elf_s390_write_core_note): Likewse.
	* elfxx-aarch64.c (_bfd_aarch64_elf_write_core_note): Likewse.
---
 bfd/bfd-in.h        | 38 ++++++++++++++++++++++++++++++++++++++
 bfd/bfd-in2.h       | 38 ++++++++++++++++++++++++++++++++++++++
 bfd/elf32-arm.c     |  7 +++++++
 bfd/elf32-ppc.c     |  7 +++++++
 bfd/elf32-s390.c    |  7 +++++++
 bfd/elf64-ppc.c     |  7 +++++++
 bfd/elf64-s390.c    |  7 +++++++
 bfd/elfxx-aarch64.c |  7 +++++++
 8 files changed, 118 insertions(+)

diff --git a/bfd/bfd-in.h b/bfd/bfd-in.h
index 481587e458..7fb2fb157a 100644
--- a/bfd/bfd-in.h
+++ b/bfd/bfd-in.h
@@ -71,6 +71,44 @@ extern "C" {
 #define LITMEMCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2) - 1)
 #define LITSTRCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2))
 
+#if __GNUC__ >= 8
+/* These are copied from glibc 2.27.  */
+
+/* The macros to control diagnostics are structured like this, rather
+   than a single macro that both pushes and pops diagnostic state and
+   takes the affected code as an argument, because the GCC pragmas
+   work by disabling the diagnostic for a range of source locations
+   and do not work when all the pragmas and the affected code are in a
+   single macro expansion.  */
+
+/* Push diagnostic state.  */
+#define DIAG_PUSH_NEEDS_COMMENT _Pragma ("GCC diagnostic push")
+
+/* Pop diagnostic state.  */
+#define DIAG_POP_NEEDS_COMMENT _Pragma ("GCC diagnostic pop")
+
+#define _DIAG_STR1(s) #s
+#define _DIAG_STR(s) _DIAG_STR1(s)
+
+/* Ignore the diagnostic OPTION.  VERSION is the most recent GCC
+   version for which the diagnostic has been confirmed to appear in
+   the absence of the pragma (in the form MAJOR.MINOR for GCC 4.x,
+   just MAJOR for GCC 5 and later).  Uses of this pragma should be
+   reviewed when the GCC version given is no longer supported for
+   building glibc; the version number should always be on the same
+   source line as the macro name, so such uses can be found with grep.
+   Uses should come with a comment giving more details of the
+   diagnostic, and an architecture on which it is seen if possibly
+   optimization-related and not in architecture-specific code.  This
+   macro should only be used if the diagnostic seems hard to fix (for
+   example, optimization-related false positives).  */
+#define DIAG_IGNORE_NEEDS_COMMENT(version, option)     \
+  _Pragma (_DIAG_STR (GCC diagnostic ignored option))
+#else
+#define DIAG_PUSH_NEEDS_COMMENT
+#define DIAG_POP_NEEDS_COMMENT
+#define DIAG_IGNORE_NEEDS_COMMENT(version, option)
+#endif
 
 #define BFD_SUPPORTS_PLUGINS @supports_plugins@
 
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index c64eee1fe1..9d11dd0c39 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -78,6 +78,44 @@ extern "C" {
 #define LITMEMCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2) - 1)
 #define LITSTRCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2))
 
+#if __GNUC__ >= 8
+/* These are copied from glibc 2.27.  */
+
+/* The macros to control diagnostics are structured like this, rather
+   than a single macro that both pushes and pops diagnostic state and
+   takes the affected code as an argument, because the GCC pragmas
+   work by disabling the diagnostic for a range of source locations
+   and do not work when all the pragmas and the affected code are in a
+   single macro expansion.  */
+
+/* Push diagnostic state.  */
+#define DIAG_PUSH_NEEDS_COMMENT _Pragma ("GCC diagnostic push")
+
+/* Pop diagnostic state.  */
+#define DIAG_POP_NEEDS_COMMENT _Pragma ("GCC diagnostic pop")
+
+#define _DIAG_STR1(s) #s
+#define _DIAG_STR(s) _DIAG_STR1(s)
+
+/* Ignore the diagnostic OPTION.  VERSION is the most recent GCC
+   version for which the diagnostic has been confirmed to appear in
+   the absence of the pragma (in the form MAJOR.MINOR for GCC 4.x,
+   just MAJOR for GCC 5 and later).  Uses of this pragma should be
+   reviewed when the GCC version given is no longer supported for
+   building glibc; the version number should always be on the same
+   source line as the macro name, so such uses can be found with grep.
+   Uses should come with a comment giving more details of the
+   diagnostic, and an architecture on which it is seen if possibly
+   optimization-related and not in architecture-specific code.  This
+   macro should only be used if the diagnostic seems hard to fix (for
+   example, optimization-related false positives).  */
+#define DIAG_IGNORE_NEEDS_COMMENT(version, option)     \
+  _Pragma (_DIAG_STR (GCC diagnostic ignored option))
+#else
+#define DIAG_PUSH_NEEDS_COMMENT
+#define DIAG_POP_NEEDS_COMMENT
+#define DIAG_IGNORE_NEEDS_COMMENT(version, option)
+#endif
 
 #define BFD_SUPPORTS_PLUGINS @supports_plugins@
 
diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c
index 5a3b58ff0d..c1b3a7938b 100644
--- a/bfd/elf32-arm.c
+++ b/bfd/elf32-arm.c
@@ -2174,7 +2174,14 @@ elf32_arm_nabi_write_core_note (bfd *abfd, char *buf, int *bufsiz,
 	va_start (ap, note_type);
 	memset (data, 0, sizeof (data));
 	strncpy (data + 28, va_arg (ap, const char *), 16);
+	DIAG_PUSH_NEEDS_COMMENT;
+	/* GCC 8 warns about 80 equals destination size with
+	   -Wstringop-truncation:
+	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
+	 */
+	DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
 	strncpy (data + 44, va_arg (ap, const char *), 80);
+	DIAG_POP_NEEDS_COMMENT;
 	va_end (ap);
 
 	return elfcore_write_note (abfd, buf, bufsiz,
diff --git a/bfd/elf32-ppc.c b/bfd/elf32-ppc.c
index 462c8af311..7ca77f02a9 100644
--- a/bfd/elf32-ppc.c
+++ b/bfd/elf32-ppc.c
@@ -2411,7 +2411,14 @@ ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
 	va_start (ap, note_type);
 	memset (data, 0, sizeof (data));
 	strncpy (data + 32, va_arg (ap, const char *), 16);
+	DIAG_PUSH_NEEDS_COMMENT;
+	/* GCC 8 warns about 80 equals destination size with
+	   -Wstringop-truncation:
+	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
+	 */
+	DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
 	strncpy (data + 48, va_arg (ap, const char *), 80);
+	DIAG_POP_NEEDS_COMMENT;
 	va_end (ap);
 	return elfcore_write_note (abfd, buf, bufsiz,
 				   "CORE", note_type, data, sizeof (data));
diff --git a/bfd/elf32-s390.c b/bfd/elf32-s390.c
index b3603bd865..db5721ebbc 100644
--- a/bfd/elf32-s390.c
+++ b/bfd/elf32-s390.c
@@ -3951,7 +3951,14 @@ elf_s390_write_core_note (bfd *abfd, char *buf, int *bufsiz,
 	va_end (ap);
 
 	strncpy (data + 28, fname, 16);
+	DIAG_PUSH_NEEDS_COMMENT;
+	/* GCC 8 warns about 80 equals destination size with
+	   -Wstringop-truncation:
+	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
+	 */
+	DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
 	strncpy (data + 44, psargs, 80);
+	DIAG_POP_NEEDS_COMMENT;
 	return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
 				   &data, sizeof (data));
       }
diff --git a/bfd/elf64-ppc.c b/bfd/elf64-ppc.c
index b166558945..e22c553744 100644
--- a/bfd/elf64-ppc.c
+++ b/bfd/elf64-ppc.c
@@ -3041,7 +3041,14 @@ ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
 	va_start (ap, note_type);
 	memset (data, 0, sizeof (data));
 	strncpy (data + 40, va_arg (ap, const char *), 16);
+	DIAG_PUSH_NEEDS_COMMENT;
+	/* GCC 8 warns about 80 equals destination size with
+	   -Wstringop-truncation:
+	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
+	 */
+	DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
 	strncpy (data + 56, va_arg (ap, const char *), 80);
+	DIAG_POP_NEEDS_COMMENT;
 	va_end (ap);
 	return elfcore_write_note (abfd, buf, bufsiz,
 				   "CORE", note_type, data, sizeof (data));
diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index bfa02340ca..15228b439f 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -3760,7 +3760,14 @@ elf_s390_write_core_note (bfd *abfd, char *buf, int *bufsiz,
 	va_end (ap);
 
 	strncpy (data + 40, fname, 16);
+	DIAG_PUSH_NEEDS_COMMENT;
+	/* GCC 8 warns about 80 equals destination size with
+	   -Wstringop-truncation:
+	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
+	 */
+	DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
 	strncpy (data + 56, psargs, 80);
+	DIAG_POP_NEEDS_COMMENT;
 	return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
 				   &data, sizeof (data));
       }
diff --git a/bfd/elfxx-aarch64.c b/bfd/elfxx-aarch64.c
index 45a732db2b..73bd1e95ce 100644
--- a/bfd/elfxx-aarch64.c
+++ b/bfd/elfxx-aarch64.c
@@ -659,7 +659,14 @@ _bfd_aarch64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_ty
 	va_start (ap, note_type);
 	memset (data, 0, sizeof (data));
 	strncpy (data + 40, va_arg (ap, const char *), 16);
+	DIAG_PUSH_NEEDS_COMMENT;
+	/* GCC 8 warns about 80 equals destination size with
+	   -Wstringop-truncation:
+	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
+	 */
+	DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-truncation");
 	strncpy (data + 56, va_arg (ap, const char *), 80);
+	DIAG_POP_NEEDS_COMMENT;
 	va_end (ap);
 
 	return elfcore_write_note (abfd, buf, bufsiz, "CORE",
-- 
2.17.0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]