[COMMITTED] readelf: Avoid may be used uninitialized warning from GCC 14

Message ID CAMe9rOqobP0weYWRB-5Fp5Dns5wy87NN8FjPsdx2aDsfqjbfHw@mail.gmail.com
State New
Headers
Series [COMMITTED] readelf: Avoid may be used uninitialized warning from GCC 14 |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply

Commit Message

H.J. Lu July 1, 2026, 11:13 p.m. UTC
  On Thu, Jul 2, 2026 at 6:21 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Wed, Jul 1, 2026 at 2:21 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > PR binutils/34324
> > * readelf.c (process_relocs): Return error on missing DT_REL and
> > DT_RELA dynamic tags.
> >
>
> I am checking it in as an obvious fix.

GCC 14 fails to recognize that rel_entsz and entsz_name are unused when
process_relocs returns early for error:

.../binutils/readelf.c:10127:18: error: ‘rel_entsz’ may be used
uninitialized [-Werror=maybe-uninitialized]
10127 |               if (rel_entsz == 0)
      |                  ^
...
.../binutils/readelf.c:10129:19: error: ‘entsz_name’ may be used
uninitialized [-Werror=maybe-uninitialized]
10129 |                   printf (_("<missing or corrupt dynamic tag: %s>\n"),
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10130 |                           entsz_name);

Clear them before returning error.  Also replace reltype_unknown with
default since GCC 14 fails to see that reltype_unknown is the only
unhandled value for relocation_type enum.

PR binutils/34324
* readelf.c (process_relocs): Clear rel_entsz and entsz_name
before returning error on missing DT_REL and DT_RELA dynamic
tags.
  

Patch

From c1c4795df872f48b7f3659cae5974a6ae4f917ea Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 2 Jul 2026 07:02:48 +0800
Subject: [PATCH] readelf: Avoid may be used uninitialized warning from GCC 14
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GCC 14 fails to recognize that rel_entsz and entsz_name are unused when
process_relocs returns early for error:

.../binutils/readelf.c:10127:18: error: ‘rel_entsz’ may be used uninitialized [-Werror=maybe-uninitialized]
10127 |               if (rel_entsz == 0)
      |                  ^
...
.../binutils/readelf.c:10129:19: error: ‘entsz_name’ may be used uninitialized [-Werror=maybe-uninitialized]
10129 |                   printf (_("<missing or corrupt dynamic tag: %s>\n"),
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10130 |                           entsz_name);

Clear them before returning error.  Also replace reltype_unknown with
default since GCC 14 fails to see that reltype_unknown is the only
unhandled value for relocation_type enum.

	PR binutils/34324
	* readelf.c (process_relocs): Clear rel_entsz and entsz_name
	before returning error on missing DT_REL and DT_RELA dynamic
	tags.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 binutils/readelf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 33b95da8b7e..59b9d906f08 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -10080,8 +10080,11 @@  process_relocs (Filedata * filedata)
 
 	  switch (rel_type)
 	    {
-	    case reltype_unknown:
+	    default:
 	      error (_("missing DT_REL and DT_RELA dynamic tags\n"));
+	      /* Avoid may be used uninitialized warning from GCC 14.  */
+	      rel_entsz = 0;
+	      entsz_name = NULL;
 	      return false;
 	    case reltype_rel:
 	      rel_entsz = filedata->dynamic_info[DT_RELENT];
-- 
2.54.0