Skip to contentSkip to conversation

Disassembly with external objdump

PATCH In review Richard Henderson 2 participants 5 messages Aug 17, 2013 – Aug 24, 2013

lore
Share & tools
lore
#0 lore raw

v2 cover Disassembly with external objdump

V2 adds the --adjust-vma idea from Max Filippov.

r~

Richard Henderson (3):
disas: Implement fallback to dump object code as hex
disas: Add disas-objdump.pl
disas-objdump: Pass --adjust-vma to objdump

 disas.c                  | 47 ++++++++++++++++++-----
 scripts/disas-objdump.pl | 99 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+), 10 deletions(-)
 create mode 100755 scripts/disas-objdump.pl
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v2 1/3 disas: Implement fallback to dump object code as hex

The OBJD-[HT] tags will be used by a script to run the hex blob through objdump --disassemble.

---
 disas.c | 47 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 10 deletions(-)
disas.c +37 -10
index 71007fb..0203ef2 100644
@@ -158,6 +158,35 @@ print_insn_thumb1(bfd_vma pc, disassemble_info *info)
 }
 #endif
 
+static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
+                              const char *prefix)
+{
+    int i, n = info->buffer_length;
+    uint8_t *buf = g_malloc(n);
+
+    info->read_memory_func(pc, buf, n, info);
+
+    for (i = 0; i < n; ++i) {
+        if (i % 32 == 0) {
+            info->fprintf_func(info->stream, "\n%s: ", prefix);
+        }
+        info->fprintf_func(info->stream, "%02x", buf[i]);
+    }
+
+    g_free(buf);
+    return n;
+}
+
+static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
+{
+    return print_insn_objdump(pc, info, "OBJD-H");
+}
+
+static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
+{
+    return print_insn_objdump(pc, info, "OBJD-T");
+}
+
 /* Disassemble this for me please... (debugging). 'flags' has the following
    values:
     i386 - 1 means 16 bit code, 2 means 64 bit code
@@ -171,7 +200,7 @@ void target_disas(FILE *out, CPUArchState *env, target_ulong code,
     target_ulong pc;
     int count;
     CPUDebug s;
-    int (*print_insn)(bfd_vma pc, disassemble_info *info);
+    int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL;
 
     INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
 
@@ -263,11 +292,10 @@ void target_disas(FILE *out, CPUArchState *env, target_ulong code,
 #elif defined(TARGET_LM32)
     s.info.mach = bfd_mach_lm32;
     print_insn = print_insn_lm32;
-#else
-    fprintf(out, "0x" TARGET_FMT_lx
-	    ": Asm output not supported on this arch\n", code);
-    return;
 #endif
+    if (print_insn == NULL) {
+        print_insn = print_insn_od_target;
+    }
 
     for (pc = code; size > 0; pc += count, size -= count) {
 	fprintf(out, "0x" TARGET_FMT_lx ":  ", pc);
@@ -303,7 +331,7 @@ void disas(FILE *out, void *code, unsigned long size)
     uintptr_t pc;
     int count;
     CPUDebug s;
-    int (*print_insn)(bfd_vma pc, disassemble_info *info);
+    int (*print_insn)(bfd_vma pc, disassemble_info *info) = NULL;
 
     INIT_DISASSEMBLE_INFO(s.info, out, fprintf);
     s.info.print_address_func = generic_print_host_address;
@@ -347,11 +375,10 @@ void disas(FILE *out, void *code, unsigned long size)
     print_insn = print_insn_hppa;
 #elif defined(__ia64__)
     print_insn = print_insn_ia64;
-#else
-    fprintf(out, "0x%lx: Asm output not supported on this arch\n",
-	    (long) code);
-    return;
 #endif
+    if (print_insn == NULL) {
+        print_insn = print_insn_od_host;
+    }
     for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
         fprintf(out, "0x%08" PRIxPTR ":  ", pc);
         count = print_insn(pc, &s.info);
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v2 2/3 disas: Add disas-objdump.pl

In reply to Richard Henderson

The script massages the output produced for architectures that are not supported internally by qemu though an external objdump program for disassembly.

---
 scripts/disas-objdump.pl | 87 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)
 create mode 100755 scripts/disas-objdump.pl
scripts/disas-objdump.pl new file +87 -0
new file mode 100755
index 0000000..c66a629
@@ -0,0 +1,87 @@
+#!/usr/bin/perl -w
+
+use File::Temp qw/ tempfile /;
+use Getopt::Long;
+
+# Default to the system objdump if a cross-compiler edition not given.
+my $aobjdump = "objdump";
+my $hobjdump = "";
+my $tobjdump = "";
+my $hmachine = "";
+my $tmachine = "";
+
+GetOptions ('O|objdump=s' => \$aobjdump,
+            'host-objdump=s' => \$hobjdump,
+            'target-objdump=s' => \$tobjdump,
+            'h|host-machine=s' => \$hmachine,
+            't|target-machine=s' => \$tmachine);
+
+# But we can't default the machines.  Sanity check that we've at least one.
+die "No host or target machine type" if !$hmachine && !$tmachine;
+
+# Reuse one temp file for all of the hunks.
+my ($outh, $outname) = tempfile();
+binmode($outh);
+END { unlink $outname; }
+
+# Pre-construct the command-lines for executing the dump.
+sub mkobjcommand ($$) {
+    my ($cmd, $mach) = @_;
+    return 0 if !$mach;
+    $cmd = $aobjdump if !$cmd;
+    return "$cmd -m $mach --disassemble-all -b binary $outname";
+}
+
+$objdump[1] = mkobjcommand($hobjdump, $hmachine);
+$objdump[2] = mkobjcommand($tobjdump, $tmachine);
+
+# Zero-initialize current dumping state.
+my $mem = "";
+my $inobjd = 0;
+
+sub objcommand {
+    my $ret = $objdump[$inobjd];
+    if (!$ret) {
+        die "Host machine type not specified" if $inobjd == 1;
+        die "Target machine type not specified" if $inobjd == 2;
+        die "Internal error";
+    }
+    return $ret;
+}
+
+while (<>) {
+    # Collect the data from the relevant OBJD-* lines.
+    if (/^OBJD-H: /) {
+        die "Internal error" if $inobjd == 2;
+        $mem = $mem . pack("H*", substr($_, 8, -1));
+        $inobjd = 1;
+    } elsif (/^OBJD-T: /) {
+        die "Internal error" if $inobjd == 1;
+        $mem = $mem . pack("H*", substr($_, 8, -1));
+        $inobjd = 2;
+    }
+    # ... which will always be followed by a blank line,
+    # at which point we should produce our dump.
+    elsif ($inobjd) {
+        # Rewrite the temp file in one go; it will usually be small.
+        sysseek $outh, 0, 0;
+        truncate $outh, 0;
+        syswrite $outh, $mem;
+
+        # Pipe from objdump...
+        open IN, "-|", objcommand();
+
+        # ... copying all but the first 7 lines of boilerplate to our stdout.
+	my $i = 0;
+	while (<IN>) {
+	    print if (++$i > 7);
+        }
+        close IN;
+        print "\n";
+
+        $mem = "";
+        $inobjd = 0;
+    } else {
+        print;
+    }
+}
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH v2 3/3 disas-objdump: Pass --adjust-vma to objdump

In reply to Richard Henderson

This gives the dumped blob its correct address during disassembly, which makes pc-relative insns much easier to interpret.

---
 scripts/disas-objdump.pl | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
scripts/disas-objdump.pl +15 -3
index c66a629..8f7e818 100755
@@ -29,7 +29,7 @@ sub mkobjcommand ($$) {
     my ($cmd, $mach) = @_;
     return 0 if !$mach;
     $cmd = $aobjdump if !$cmd;
-    return "$cmd -m $mach --disassemble-all -b binary $outname";
+    return "$cmd -m $mach --disassemble-all -b binary";
 }
 
 $objdump[1] = mkobjcommand($hobjdump, $hmachine);
@@ -38,6 +38,7 @@ $objdump[2] = mkobjcommand($tobjdump, $tmachine);
 # Zero-initialize current dumping state.
 my $mem = "";
 my $inobjd = 0;
+my $vma = 0;
 
 sub objcommand {
     my $ret = $objdump[$inobjd];
@@ -50,7 +51,7 @@ sub objcommand {
 }
 
 while (<>) {
-    # Collect the data from the relevant OBJD-* lines.
+    # Collect the data from the relevant OBJD-* lines ...
     if (/^OBJD-H: /) {
         die "Internal error" if $inobjd == 2;
         $mem = $mem . pack("H*", substr($_, 8, -1));
@@ -68,8 +69,12 @@ while (<>) {
         truncate $outh, 0;
         syswrite $outh, $mem;
 
+        my $cmd = objcommand();
+        $cmd = $cmd . " --adjust-vma=" . $vma if $vma;
+        $cmd = $cmd . " " . $outname;
+
         # Pipe from objdump...
-        open IN, "-|", objcommand();
+        open IN, "-|", $cmd;
 
         # ... copying all but the first 7 lines of boilerplate to our stdout.
 	my $i = 0;
@@ -81,6 +86,13 @@ while (<>) {
 
         $mem = "";
         $inobjd = 0;
+        $vma = 0;
+    }
+    # The line before "OBJD-*" will be of the form "0x<hex>+: +\n".
+    # Extract the value for passing to --adjust-vma.
+    elsif (/^(0x[0-9a-fA-F]+):\s*$/) {
+        $vma = $1;
+        print;
     } else {
         print;
     }
Signature · 1 line
1.8.1.4
#4 lore raw
In reply to Richard Henderson
Quoted 1 line · Richard Henderson

V2 adds the --adjust-vma idea from Max Filippov.

Thanks Richard, I've applied this

Cheers,
Edgar

Quoted 19 lines

r~

Richard Henderson (3):
disas: Implement fallback to dump object code as hex
disas: Add disas-objdump.pl
disas-objdump: Pass --adjust-vma to objdump

 disas.c                  | 47 ++++++++++++++++++-----
 scripts/disas-objdump.pl | 99 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+), 10 deletions(-)
 create mode 100755 scripts/disas-objdump.pl
Signature · 1 line
1.8.1.4