bfd: don't silently wrap or truncate PE image section RVAs
authorJan Beulich <jbeulich@suse.com>
Tue, 9 Mar 2021 07:52:32 +0000 (08:52 +0100)
committerJan Beulich <jbeulich@suse.com>
Tue, 9 Mar 2021 07:52:32 +0000 (08:52 +0100)
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.

14 files changed:
bfd/ChangeLog
bfd/peXXigen.c
ld/ChangeLog
ld/testsuite/ld-scripts/alignof.exp
ld/testsuite/ld-scripts/data.exp
ld/testsuite/ld-scripts/default-script.exp
ld/testsuite/ld-scripts/default-script1.d
ld/testsuite/ld-scripts/default-script2.d
ld/testsuite/ld-scripts/default-script3.d
ld/testsuite/ld-scripts/default-script4.d
ld/testsuite/ld-scripts/log2.exp
ld/testsuite/ld-scripts/print-memory-usage.exp
ld/testsuite/ld-scripts/sizeof.exp
ld/testsuite/ld-undefined/weak-undef.exp

index a43a3c2174551861ccd008673403c712ddb7abaf..6c633ebe0b64e10cc25f1bcff5f3a979d5faf0c1 100644 (file)
@@ -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
index 513b59316591afe6d01dc255e19660bd81378826..83bbac51af78653bc612232596ab1ca378edfa86 100644 (file)
@@ -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,
index 367aa7b242228fe77227708cc6ebda793b9fffc2..1b98c81a6a8a3e3330f9c494f28bc213b7bb49c5 100644 (file)
@@ -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
index b94f22762ad968558957bd6822a0786f4eb80b7f..44aa93146c4e47a50effe223cc439d02f7b39065 100644 (file)
@@ -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
 }
index 772be15415dcaf8a57d316976b84725545338968..8f7fe2dafac60977a71d1650b92e87ee9b395fce 100644 (file)
@@ -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"
 }
 
index 9894239ad9c92ce0cda05d96b45d863c04012722..753085e4364332185f187ed60e7b83de96603556 100644 (file)
@@ -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"
 }
index ec88067f4383eabad22c2330f8376e63494a3c28..97758084640e78add929949aab038a29189b9f33 100644 (file)
@@ -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
index b10ac96288eb3f349dfdfbd58e7f1c069d58fa1b..39c24e001484d1259d2b09c7aabe866818656c2f 100644 (file)
@@ -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
index 4742bc31e4f570f1770ad89d87d2d7549bbe0575..7a7dda651307e1142061e8603e358119d7241632 100644 (file)
@@ -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
index 09b6dbf08bad4beef2dfb787337874050040527b..7f905b3f36e3ccc26dd10eac3849c9a8ca10811b 100644 (file)
@@ -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
index c91dcc1a4087d3b58860bc2a916f78ba337010c1..ba00d95aa9e1318701b761c0faef1f3159edfa19 100644 (file)
@@ -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
index 74087bf28f6d884e1d8c8b8a0145d68caf6bbf3d..a84c525882d1f737746772ea31f307df78b9f5dc 100644 (file)
@@ -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
index 6d306c8dcf7685aac8843b03a25b907f4c0aa4a5..4624fa1cdcab3be694812af0e9d58332a9370288 100644 (file)
@@ -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
 }
index 4f470e5924174c1ce5996828bfb82710c37ad9aa..443dce2fbfec2c6dd185951ebf8dc02376275869 100644 (file)
 # 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*
This page took 0.042009 seconds and 4 git commands to generate.