[binutils-gdb] bfd: don't silently wrap or truncate PE image section RVAs

Jan Beulich jbeulich@sourceware.org
Tue Mar 9 07:54:46 GMT 2021


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=87fa7d568ddd804fb73bb78e33ae1c22d9fe343c

commit 87fa7d568ddd804fb73bb78e33ae1c22d9fe343c
Author: Jan Beulich <jbeulich@suse.com>
Date:   Tue Mar 9 08:52:32 2021 +0100

    bfd: don't silently wrap or truncate PE image section RVAs
    
    In PE images section addresses get expressed as addresses relative to
    the image base. Therefore the VA of a section must be no less than the
    image base, and after subtraction of the image base the resulting value
    should fit in 32 bits. (The issue is particularly obvious to notice when
    sections, perhaps because of ELF assumptions, get placed at VA 0 by
    default. Debugging info sections as well as .comment, when input files
    are ELF, are a good example. All such sections need proper mentioning in
    the linker script to avoid this warning.)
    
    There are a number of test cases which previously produced bogus images,
    yet still declared the test a success. Like done for other tests
    already, force a zero image base for these. This then also allows (and
    requires) dropping again xfail-s which 39a7b38fac0e ("Fix linker tests
    to work with 16-bit targets") had added to ld-scripts/default-script*.d
    (originally as skip-s). This also depends on similar adjustments to
    testsuite/ld-scripts/map-address.* made by an earlier patch.
    
    For ld-scripts/print-memory-usage.* I suppose xcoff could be dropped
    from the exclusion list by suppressing garbage collection, just like
    already done in e.g. (as seen in the diff here) ld-scripts/data.*, but I
    didn't want to make unrelated adjustments.

Diff:
---
 bfd/ChangeLog                                  |  4 ++++
 bfd/peXXigen.c                                 | 12 +++++++-----
 ld/ChangeLog                                   | 15 +++++++++++++++
 ld/testsuite/ld-scripts/alignof.exp            |  9 ++++++++-
 ld/testsuite/ld-scripts/data.exp               |  4 +++-
 ld/testsuite/ld-scripts/default-script.exp     |  2 ++
 ld/testsuite/ld-scripts/default-script1.d      |  2 --
 ld/testsuite/ld-scripts/default-script2.d      |  2 --
 ld/testsuite/ld-scripts/default-script3.d      |  2 --
 ld/testsuite/ld-scripts/default-script4.d      |  2 --
 ld/testsuite/ld-scripts/log2.exp               |  9 ++++++++-
 ld/testsuite/ld-scripts/print-memory-usage.exp |  6 ++++++
 ld/testsuite/ld-scripts/sizeof.exp             |  9 ++++++++-
 ld/testsuite/ld-undefined/weak-undef.exp       | 10 ++++++++--
 14 files changed, 69 insertions(+), 19 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index a43a3c21745..6c633ebe0b6 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,7 @@
+2021-03-09  Jan Beulich  <jbeulich@suse.com>
+
+	* peXXigen.c (_bfd_XXi_swap_scnhdr_out): Diagnose out of range RVA.
+
 2021-03-05  H.J. Lu  <hongjiu.lu@intel.com>
 
 	PR ld/27425
diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c
index 513b5931659..83bbac51af7 100644
--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -933,11 +933,13 @@ _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
 
   memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
 
-  PUT_SCNHDR_VADDR (abfd,
-		    ((scnhdr_int->s_vaddr
-		      - pe_data (abfd)->pe_opthdr.ImageBase)
-		     & 0xffffffff),
-		    scnhdr_ext->s_vaddr);
+  ss = scnhdr_int->s_vaddr - pe_data (abfd)->pe_opthdr.ImageBase;
+  if (scnhdr_int->s_vaddr < pe_data (abfd)->pe_opthdr.ImageBase)
+    _bfd_error_handler ("%pB:%.8s: section below image base",
+                        abfd, scnhdr_int->s_name);
+  else if(ss != (ss & 0xffffffff))
+    _bfd_error_handler ("%pB:%.8s: RVA truncated", abfd, scnhdr_int->s_name);
+  PUT_SCNHDR_VADDR (abfd, ss & 0xffffffff, scnhdr_ext->s_vaddr);
 
   /* NT wants the size data to be rounded up to the next
      NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
diff --git a/ld/ChangeLog b/ld/ChangeLog
index 367aa7b2422..1b98c81a6a8 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,18 @@
+2021-03-09  Jan Beulich  <jbeulich@suse.com>
+
+	* testsuite/ld-scripts/alignof.exp,
+	testsuite/ld-scripts/data.exp,
+	testsuite/ld-scripts/default-script.exp,
+	testsuite/ld-scripts/log2.exp,
+	testsuite/ld-scripts/print-memory-usage.exp,
+	testsuite/ld-scripts/sizeof.exp,
+	testsuite/ld-undefined/weak-undef.exp: Set image base to zero
+	for PE/COFF.
+	* testsuite/ld-scripts/default-script1.d,
+	testsuite/ld-scripts/default-script2.d,
+	testsuite/ld-scripts/default-script3.d,
+	testsuite/ld-scripts/default-script4.d: Drop xfail and comment.
+
 2021-03-05  H.J. Lu  <hongjiu.lu@intel.com>
 
 	PR ld/27425
diff --git a/ld/testsuite/ld-scripts/alignof.exp b/ld/testsuite/ld-scripts/alignof.exp
index b94f22762ad..44aa93146c4 100644
--- a/ld/testsuite/ld-scripts/alignof.exp
+++ b/ld/testsuite/ld-scripts/alignof.exp
@@ -32,7 +32,14 @@ if ![ld_assemble $as $srcdir/$subdir/alignof.s tmpdir/alignof.o] {
     return
 }
 
-if ![ld_link $ld tmpdir/alignof "-T $srcdir/$subdir/alignof.t tmpdir/alignof.o"] {
+if { [is_pecoff_format] } {
+    set IMAGE_BASE "--image-base 0"
+} else {
+    set IMAGE_BASE ""
+}
+
+if ![ld_link $ld tmpdir/alignof "-T $srcdir/$subdir/alignof.t \
+	$IMAGE_BASE tmpdir/alignof.o"] {
     fail $testname
     return
 }
diff --git a/ld/testsuite/ld-scripts/data.exp b/ld/testsuite/ld-scripts/data.exp
index 772be15415d..8f7fe2dafac 100644
--- a/ld/testsuite/ld-scripts/data.exp
+++ b/ld/testsuite/ld-scripts/data.exp
@@ -20,7 +20,9 @@
 # MA 02110-1301, USA.
 
 set old_LDFLAGS $LDFLAGS
-if { [is_xcoff_format] } then {
+if { [is_pecoff_format] } then {
+    set LDFLAGS "$LDFLAGS --image-base 0"
+} elseif { [is_xcoff_format] } then {
     set LDFLAGS "$LDFLAGS -bnogc"
 }
 
diff --git a/ld/testsuite/ld-scripts/default-script.exp b/ld/testsuite/ld-scripts/default-script.exp
index 9894239ad9c..753085e4364 100644
--- a/ld/testsuite/ld-scripts/default-script.exp
+++ b/ld/testsuite/ld-scripts/default-script.exp
@@ -21,6 +21,8 @@
 set old_ldflags $LDFLAGS
 if { [istarget spu*-*-*] } {
     set LDFLAGS "$LDFLAGS --local-store 0:0"
+} elseif { [is_pecoff_format] } {
+    set LDFLAGS "$LDFLAGS --image-base 0"
 } elseif { [is_xcoff_format] } {
     set LDFLAGS "$LDFLAGS -bnogc"
 }
diff --git a/ld/testsuite/ld-scripts/default-script1.d b/ld/testsuite/ld-scripts/default-script1.d
index ec88067f438..97758084640 100644
--- a/ld/testsuite/ld-scripts/default-script1.d
+++ b/ld/testsuite/ld-scripts/default-script1.d
@@ -1,8 +1,6 @@
 #source: default-script.s
 #ld: -defsym _START=0x800 -T default-script.t
 #nm: -n
-#xfail: {[is_pecoff_format x86_64-*]}
-# Skipped on Mingw64 and Cygwin because the image base defaults to 0x100000000
 
 #...
 0*800 . _START
diff --git a/ld/testsuite/ld-scripts/default-script2.d b/ld/testsuite/ld-scripts/default-script2.d
index b10ac96288e..39c24e00148 100644
--- a/ld/testsuite/ld-scripts/default-script2.d
+++ b/ld/testsuite/ld-scripts/default-script2.d
@@ -1,8 +1,6 @@
 #source: default-script.s
 #ld: -T default-script.t -defsym _START=0x800
 #nm: -n
-#xfail: {[is_pecoff_format x86_64-*]}
-# Skipped on Mingw64 and Cygwin because the image base defaults to 0x100000000
 
 #...
 0*800 . _START
diff --git a/ld/testsuite/ld-scripts/default-script3.d b/ld/testsuite/ld-scripts/default-script3.d
index 4742bc31e4f..7a7dda65130 100644
--- a/ld/testsuite/ld-scripts/default-script3.d
+++ b/ld/testsuite/ld-scripts/default-script3.d
@@ -1,8 +1,6 @@
 #source: default-script.s
 #ld: -defsym _START=0x800 -dT default-script.t
 #nm: -n
-#xfail: {[is_pecoff_format x86_64-*]}
-# Skipped on Mingw64 and Cygwin because the image base defaults to 0x100000000
 
 #...
 0*800 . _START
diff --git a/ld/testsuite/ld-scripts/default-script4.d b/ld/testsuite/ld-scripts/default-script4.d
index 09b6dbf08ba..7f905b3f36e 100644
--- a/ld/testsuite/ld-scripts/default-script4.d
+++ b/ld/testsuite/ld-scripts/default-script4.d
@@ -1,8 +1,6 @@
 #source: default-script.s
 #ld: --default-script default-script.t -defsym _START=0x800
 #nm: -n
-#xfail: {[is_pecoff_format x86_64-*]}
-# Skipped on Mingw64 and Cygwin because the image base defaults to 0x100000000
 
 #...
 0*800 . _START
diff --git a/ld/testsuite/ld-scripts/log2.exp b/ld/testsuite/ld-scripts/log2.exp
index c91dcc1a408..ba00d95aa9e 100644
--- a/ld/testsuite/ld-scripts/log2.exp
+++ b/ld/testsuite/ld-scripts/log2.exp
@@ -26,7 +26,14 @@ if {![ld_assemble $as $srcdir/$subdir/log2.s tmpdir/log2.o]} {
     return
 }
 
-if {![ld_link $ld tmpdir/log2 "$LDFLAGS -T $srcdir/$subdir/log2.t tmpdir/log2.o"]} {
+if { [is_pecoff_format] } {
+    set IMAGE_BASE "--image-base 0"
+} else {
+    set IMAGE_BASE ""
+}
+
+if {![ld_link $ld tmpdir/log2 "$LDFLAGS -T $srcdir/$subdir/log2.t \
+	$IMAGE_BASE tmpdir/log2.o"]} {
     fail $testname
 } else {
     pass $testname
diff --git a/ld/testsuite/ld-scripts/print-memory-usage.exp b/ld/testsuite/ld-scripts/print-memory-usage.exp
index 74087bf28f6..a84c525882d 100644
--- a/ld/testsuite/ld-scripts/print-memory-usage.exp
+++ b/ld/testsuite/ld-scripts/print-memory-usage.exp
@@ -33,6 +33,11 @@ if { [istarget mips*-*-*]
     return
 }
 
+set old_LDFLAGS $LDFLAGS
+if { [is_pecoff_format] } {
+    set LDFLAGS "$LDFLAGS --image-base 0"
+}
+
 run_ld_link_tests {
     {
 	"print-memory-usage-1"
@@ -66,3 +71,4 @@ run_ld_link_tests {
 
 }
 
+set LDFLAGS $old_LDFLAGS
diff --git a/ld/testsuite/ld-scripts/sizeof.exp b/ld/testsuite/ld-scripts/sizeof.exp
index 6d306c8dcf7..4624fa1cdca 100644
--- a/ld/testsuite/ld-scripts/sizeof.exp
+++ b/ld/testsuite/ld-scripts/sizeof.exp
@@ -27,7 +27,14 @@ if ![ld_assemble $as $srcdir/$subdir/sizeof.s tmpdir/sizeof.o] {
     return
 }
 
-if ![ld_link $ld tmpdir/sizeof "$LDFLAGS -T $srcdir/$subdir/sizeof.t tmpdir/sizeof.o"] {
+if { [is_pecoff_format] } {
+    set IMAGE_BASE "--image-base 0"
+} else {
+    set IMAGE_BASE ""
+}
+
+if ![ld_link $ld tmpdir/sizeof "$LDFLAGS -T $srcdir/$subdir/sizeof.t \
+	$IMAGE_BASE tmpdir/sizeof.o"] {
     fail $testname
     return
 }
diff --git a/ld/testsuite/ld-undefined/weak-undef.exp b/ld/testsuite/ld-undefined/weak-undef.exp
index 4f470e59241..443dce2fbfe 100644
--- a/ld/testsuite/ld-undefined/weak-undef.exp
+++ b/ld/testsuite/ld-undefined/weak-undef.exp
@@ -23,14 +23,20 @@
 # some a.out targets too.
 set testname "weak undefined data symbols"
 
+if { [is_pecoff_format] } then {
+    set IMAGE_BASE "--image-base 0"
+} else {
+    set IMAGE_BASE ""
+}
+
 if { ![is_elf_format] && ![is_pecoff_format] } then {
     unsupported $testname
 } elseif {![ld_assemble $as $srcdir/$subdir/weak-undef.s \
 	    tmpdir/weak-undef.o]} then {
     # It's OK if .weak doesn't work on this target.
     unsupported $testname
-} elseif {![ld_link $ld tmpdir/weak-undef \
-		"tmpdir/weak-undef.o -T $srcdir/$subdir/weak-undef.t"]} then {
+} elseif {![ld_link $ld tmpdir/weak-undef "tmpdir/weak-undef.o \
+		-T $srcdir/$subdir/weak-undef.t $IMAGE_BASE"]} then {
     # Weak symbols are broken for non-i386 PE targets.
     if {! [istarget i?86-*-*]} {
 	setup_xfail *-*-pe*


More information about the Binutils-cvs mailing list