Sign-extend start and stop address inputs to objdump

Message ID 20190504012302.125913-1-fshahbazker@wavecomp.com
State New
Headers show
Series
  • Sign-extend start and stop address inputs to objdump
Related show

Commit Message

Faraz Shahbazker May 4, 2019, 1:21 a.m.
For targets that treat addresses as signed (MIPS/SH64), user-specified
start/stop address limits cannot be compared directly to section VMAs.
We must sign-extend user-specified 32-bit address limits which have
bit 31 set for such targets.

binutils/
	* objdump.c (sign_extend_address): New function.
	(dump_bfd): Sign-extend user-specified start/stop addresses
	for targets that need it.
	* testsuite/binutils-all/objdump.exp: Add tests for objdump
	with start and stop addresses in higher address ranges.
---
 binutils/objdump.c                          | 26 ++++++++++++++
 binutils/testsuite/binutils-all/objdump.exp | 54 +++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+)

-- 
2.9.5

Comments

Nick Clifton May 8, 2019, 12:38 p.m. | #1
Hi Faraz,

> binutils/

> 	* objdump.c (sign_extend_address): New function.

> 	(dump_bfd): Sign-extend user-specified start/stop addresses

> 	for targets that need it.

> 	* testsuite/binutils-all/objdump.exp: Add tests for objdump

> 	with start and stop addresses in higher address ranges.


Approved - please apply.

Cheers
  Nick
Alan Modra May 10, 2019, 2:01 p.m. | #2
git commit 2379f9c475 introduced an rx-elf test failure.  This fixes it.

	* testsuite/binutils-all/objdump.exp (test_objdump_disas_limited),
	(test_objdump_content_limited): Add text arg, use in place of .text.
	(bintest_signed.o): Call get_standard_section_names for name of
	text section.

diff --git a/binutils/testsuite/binutils-all/objdump.exp b/binutils/testsuite/binutils-all/objdump.exp
index f25946ca08..beaf44f954 100644
--- a/binutils/testsuite/binutils-all/objdump.exp
+++ b/binutils/testsuite/binutils-all/objdump.exp
@@ -690,15 +690,15 @@ proc test_objdump_limited { testfile dopt want start stop } {
 
 # Test objdump -d --start-address M --stop-address N
 
-proc test_objdump_disas_limited { testfile start stop } {
-    set want "$testfile:\[ 	\]*file format.*Disassembly of section .text:\n.*\[ \]*$start:.*"
+proc test_objdump_disas_limited { testfile text start stop } {
+    set want "$testfile:\[ 	\]*file format.*Disassembly of section $text:\n.*\[ \]*$start:.*"
     test_objdump_limited $testfile -d $want $start $stop
 }
 
 # Test objdump -s --start-address M --stop-address N
 
-proc test_objdump_content_limited { testfile start stop } {
-    set want "$testfile:\[ 	\]*file format.*Contents of section .text:\n\[ \]*$start .*"
+proc test_objdump_content_limited { testfile text start stop } {
+    set want "$testfile:\[ 	\]*file format.*Contents of section $text:\n\[ \]*$start .*"
     test_objdump_limited $testfile -s $want $start $stop
 }
 
@@ -709,9 +709,15 @@ proc test_objdump_content_limited { testfile start stop } {
 # start/stop limits.
 
 if {[is_elf_format]} then {
+    set sect_names [get_standard_section_names]
+    if { $sect_names != "" } {
+	set text [lindex $sect_names 0]
+    } else {
+	set text ".text"
+    }
     # generate a copy of the test object with .text repositioned
-    if { [binutils_run $OBJCOPY "--change-section-address .text=0x80000000 tmpdir/bintest.o tmpdir/bintest_signed.o"] != "" } {
-	fail "Failed to reposition .text to 0x80000000 (tmpdir/bintest.o -> tmpdir/bintest_signed.o)"
+    if { [binutils_run $OBJCOPY "--change-section-address $text=0x80000000 tmpdir/bintest.o tmpdir/bintest_signed.o"] != "" } {
+	fail "Failed to reposition $text to 0x80000000 (tmpdir/bintest.o -> tmpdir/bintest_signed.o)"
 	return
     }
 
@@ -721,8 +727,8 @@ if {[is_elf_format]} then {
 	set testfile3 tmpdir/bintest_signed.o
     }
 
-    test_objdump_content_limited $testfile3 "80000004" "80000008"
-    test_objdump_disas_limited $testfile3 "80000004" "80000008"
+    test_objdump_content_limited $testfile3 $text "80000004" "80000008"
+    test_objdump_disas_limited $testfile3 $text "80000004" "80000008"
     remote_file host delete $testfile3
 }
 

-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/binutils/objdump.c b/binutils/objdump.c
index 644638c..05d503e 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -3749,11 +3749,25 @@  adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
     }
 }
 
+/* Return the sign-extended form of an ARCH_SIZE sized VMA.  */
+
+static bfd_vma
+sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
+		     bfd_vma vma,
+		     unsigned arch_size)
+{
+  bfd_vma mask;
+  mask = (bfd_vma) 1 << (arch_size - 1);
+  return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
+}
+
 /* Dump selected contents of ABFD.  */
 
 static void
 dump_bfd (bfd *abfd, bfd_boolean is_mainfile)
 {
+  const struct elf_backend_data * bed;
+
   if (bfd_big_endian (abfd))
     byte_get = byte_get_big_endian;
   else if (bfd_little_endian (abfd))
@@ -3784,6 +3798,18 @@  dump_bfd (bfd *abfd, bfd_boolean is_mainfile)
 	}
     }
 
+  /* Adjust user-specified start and stop limits for targets that use
+     signed addresses.  */
+  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+      && (bed = get_elf_backend_data (abfd)) != NULL
+      && bed->sign_extend_vma)
+    {
+      start_address = sign_extend_address (abfd, start_address,
+					   bed->s->arch_size);
+      stop_address = sign_extend_address (abfd, stop_address,
+					  bed->s->arch_size);
+    }
+
   /* If we are adjusting section VMA's, change them all now.  Changing
      the BFD information is a hack.  However, we must do it, or
      bfd_find_nearest_line will not do the right thing.  */
diff --git a/binutils/testsuite/binutils-all/objdump.exp b/binutils/testsuite/binutils-all/objdump.exp
index 94ff015..f25946c 100644
--- a/binutils/testsuite/binutils-all/objdump.exp
+++ b/binutils/testsuite/binutils-all/objdump.exp
@@ -672,6 +672,60 @@  if {[is_elf_format]} then {
     test_follow_debuglink "--dwarf=follow-links --headers --wide" objdump.WK3
 }
 
+# Test objdump output with start and stop address limits for the specified
+# dump option
+
+proc test_objdump_limited { testfile dopt want start stop } {
+    global OBJDUMP
+    global OBJDUMPFLAGS
+
+    set got [binutils_run $OBJDUMP "$OBJDUMPFLAGS $dopt --start-address 0x$start --stop-address 0x$stop $testfile"]
+
+    if [regexp $want $got] then {
+	pass "objdump $dopt --start-address 0x$start --stop-address 0x$stop ($testfile)"
+    } else {
+	fail "objdump $dopt --start-address 0x$start --stop-address 0x$stop ($testfile)"
+    }
+}
+
+# Test objdump -d --start-address M --stop-address N
+
+proc test_objdump_disas_limited { testfile start stop } {
+    set want "$testfile:\[ 	\]*file format.*Disassembly of section .text:\n.*\[ \]*$start:.*"
+    test_objdump_limited $testfile -d $want $start $stop
+}
+
+# Test objdump -s --start-address M --stop-address N
+
+proc test_objdump_content_limited { testfile start stop } {
+    set want "$testfile:\[ 	\]*file format.*Contents of section .text:\n\[ \]*$start .*"
+    test_objdump_limited $testfile -s $want $start $stop
+}
+
+# Test objdump with --start-address and --stop-address options for higher
+# address ranges which may be sign-extended on targets that treat addresses
+# as signed.  We only check that objdump produces some dump output at the
+# specified start address as a proxy for correct enforcement of the
+# start/stop limits.
+
+if {[is_elf_format]} then {
+    # generate a copy of the test object with .text repositioned
+    if { [binutils_run $OBJCOPY "--change-section-address .text=0x80000000 tmpdir/bintest.o tmpdir/bintest_signed.o"] != "" } {
+	fail "Failed to reposition .text to 0x80000000 (tmpdir/bintest.o -> tmpdir/bintest_signed.o)"
+	return
+    }
+
+    if [is_remote host] {
+	set testfile3 [remote_download host tmpdir/bintest_signed.o]
+    } else {
+	set testfile3 tmpdir/bintest_signed.o
+    }
+
+    test_objdump_content_limited $testfile3 "80000004" "80000008"
+    test_objdump_disas_limited $testfile3 "80000004" "80000008"
+    remote_file host delete $testfile3
+}
+
 # Options which are not tested: -a -D -R -T -x -l --stabs
 # I don't see any generic way to test any of these other than -a.
 # Tests could be written for specific targets, and that should be done