bfd_[fs]printf_vma() truncating values

Jan Beulich jbeulich@suse.com
Mon May 3 14:11:11 GMT 2021


When putting together 9a8041fd94b7 ("gas: drop sprint_value()") I failed
to pay attention to bfd_sprintf_vma() silently truncating values for
32-bit binaries. This isn't helpful in particular in diagnostics. I came
up with the change below, but at least arm, cris, mips, riscv, and rx
utilize the truncation (i.e. there are now testsuite failures in cases
where negative values get printed as huge positive ones, which now get 8
more 'f' prefixed).

One alternative might be to avoid using the functions for diagnostic
output (where out-of-range values are relevant to report), and instead
have callers use BFD_VMA_FMT (or [fs]printf_vma()) directly. However,
the limiting to 8 characters by the functions here (for small enough
values) seems desirable to me for consumption by humans.

As a result, I'd like to seek input towards possible other options first.

Thanks, Jan

--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -2208,9 +2208,9 @@ void
 bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
 {
 #ifdef BFD64
-  if (is32bit (abfd))
+  if (is32bit (abfd) && value == (value & 0xffffffff))
     {
-      sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
+      sprintf (buf, "%08lx", (unsigned long) value);
       return;
     }
 #endif
@@ -2221,9 +2221,9 @@ void
 bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
 {
 #ifdef BFD64
-  if (is32bit (abfd))
+  if (is32bit (abfd) && value == (value & 0xffffffff))
     {
-      fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
+      fprintf ((FILE *) stream, "%08lx", (unsigned long) value);
       return;
     }
 #endif
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -4497,7 +4497,7 @@ dump_reloc_set (bfd *abfd, asection *sec
       {
 	char buf[30];
 
-	bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
+	bfd_sprintf_vma (abfd, buf, 0);
 	width = strlen (buf) - 7;
       }
     printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");


More information about the Binutils mailing list