Skip to contentSkip to conversation

Disassembly with external objdump

PATCH Superseded Richard Henderson 7 participants 14 messages Aug 9, 2013 – Sep 3, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Disassembly with external objdump

We have one host platform (aarch64), and three target platforms (openrisc, unicore32, xtensa) with no built-in disassembly support, thanks largely to gplv3 silliness.

Here's a first-cut at handling these cases with an external tool. The qemu-produced dump file contains just a hex dump of bytes, and a perl script is provided to pass those bytes through objdump.

I've lightly tested this with aarch64 host running on Foundation. Feedback appreciated.

r~

Richard Henderson (2):
disas: Implement fallback to dump object code as hex
disas: Add disas-objdump.pl

 disas.c                  | 46 +++++++++++++++++++------
 scripts/disas-objdump.pl | 87 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 10 deletions(-)
 create mode 100755 scripts/disas-objdump.pl
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 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 | 46 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 10 deletions(-)
disas.c +36 -10
index 71007fb..bd74e85 100644
@@ -158,6 +158,34 @@ 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]);
+    }
+
+    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 +199,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 +291,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 +330,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 +374,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.3.1
#2 lore raw

PATCH 2/2 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.3.1
#5 lore raw
In reply to Richard Henderson

Hi Richard,

Quoted 3 lines · Richard Henderson

We have one host platform (aarch64), and three target platforms (openrisc, unicore32, xtensa) with no built-in disassembly support, thanks largely to gplv3 silliness.

Thank you for doing this for or32.

Quoted 16 lines

Here's a first-cut at handling these cases with an external tool. The qemu-produced dump file contains just a hex dump of bytes, and a perl script is provided to pass those bytes through objdump.

I've lightly tested this with aarch64 host running on Foundation. Feedback appreciated.

r~

Richard Henderson (2):
disas: Implement fallback to dump object code as hex
disas: Add disas-objdump.pl

Quoted 8 lines
 disas.c                  | 46 +++++++++++++++++++------
 scripts/disas-objdump.pl | 87 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 10 deletions(-)
 create mode 100755 scripts/disas-objdump.pl

--
1.8.3.1

Regards,
Jia

#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] disas: Add disas-objdump.pl

In reply to Richard Henderson
Quoted 3 lines · Richard Henderson

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

I'd add something like the following to get disassembled hunks with correct addresses:

scripts/disas-objdump.pl +7 -3
index c66a629..b6824b4 100755
@@ -38,15 +38,17 @@ $objdump[2] = mkobjcommand($tobjdump, $tmachine);
 # Zero-initialize current dumping state.
 my $mem = "";
 my $inobjd = 0;
+my $vma = "";
 
-sub objcommand {
+sub objcommand ($) {
+    my ($vma) = @_;
     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;
+    return $ret . " --adjust-vma=" . $vma;
 }
 
 while (<>) {
@@ -69,7 +71,8 @@ while (<>) {
         syswrite $outh, $mem;
 
         # Pipe from objdump...
-        open IN, "-|", objcommand();
+        $vma =~ s/:.*//;
+        open IN, "-|", objcommand($vma);
 
         # ... copying all but the first 7 lines of boilerplate to our stdout.
        my $i = 0;
@@ -82,6 +85,7 @@ while (<>) {
         $mem = "";
         $inobjd = 0;
     } else {
+        $vma = $_;
         print;
     }
 }
Signature · 2 lines
Thanks.
-- Max
#6 lore raw
In reply to Richard Henderson
Quoted 3 lines · Richard Henderson

We have one host platform (aarch64), and three target platforms (openrisc, unicore32, xtensa) with no built-in disassembly support, thanks largely to gplv3 silliness.

FWIW, there's an aarch64 BSD disassembler available as part of vixl: https://github.com/armvixl/vixl

but this is still going to be useful for other platforms anyway.

-- PMM

#7 lore raw
Quoted 11 lines · Peter Maydell
Quoted 3 lines · Richard Henderson

We have one host platform (aarch64), and three target platforms (openrisc, unicore32, xtensa) with no built-in disassembly support, thanks largely to gplv3 silliness.

FWIW, there's an aarch64 BSD disassembler available as part of vixl: https://github.com/armvixl/vixl

but this is still going to be useful for other platforms anyway.

-- PMM

I was integrating vixl into qemu, but I like Richard's idea better. I think that using objdump is better even for AArch64, since objdump will see more use and be more tested.

I don't have anywhere to test until September, but I generally

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] disas: Add disas-objdump.pl

In reply to Max Filippov
Quoted 1 line · Max Filippov

+ return $ret . " --adjust-vma=" . $vma;

Interesting. I didn't consider this, as that option doesn't appear in objdump --help.

Thanks,

r~

#8 lore raw
In reply to Claudio Fontana
Quoted 11 lines · Claudio Fontana
Quoted 7 lines · Peter Maydell
Quoted 3 lines · Richard Henderson

We have one host platform (aarch64), and three target platforms (openrisc, unicore32, xtensa) with no built-in disassembly support, thanks largely to gplv3 silliness.

FWIW, there's an aarch64 BSD disassembler available as part of vixl: https://github.com/armvixl/vixl

I was integrating vixl into qemu, but I like Richard's idea better.

Well, it depends. If we're going to dump all the in-tree disassemblers and always use an external objdump, that's fine. If we aren't I'd rather aarch64 not be in the "second class citizen" camp if we have a disassembler available.

-- PMM

#9 lore raw
Quoted 2 lines · Peter Maydell

Well, it depends. If we're going to dump all the in-tree disassemblers and always use an external objdump, that's fine.

It's tempting, given that the only internal disassemblers that are not missing opcodes are for the extinct cpus.

r~

#10 lore raw
Quoted 6 lines · Richard Henderson
Quoted 2 lines · Peter Maydell

Well, it depends. If we're going to dump all the in-tree disassemblers and always use an external objdump, that's fine.

It's tempting, given that the only internal disassemblers that are not missing opcodes are for the extinct cpus.

Will that leave those CPUs without x/i support from monitor? Does anybody use it?

Signature · 2 lines
Thanks.
-- Max
#11 lore raw
Quoted 9 lines · Max Filippov
Quoted 6 lines · Richard Henderson
Quoted 2 lines · Peter Maydell

Well, it depends. If we're going to dump all the in-tree disassemblers and always use an external objdump, that's fine.

It's tempting, given that the only internal disassemblers that are not missing opcodes are for the extinct cpus.

Will that leave those CPUs without x/i support from monitor?

Yes.

One could always have qemu fork to objdump too. Though finding the right one could be tricky without help.

Quoted 1 line

Does anybody use it?

That I can't answer.

r~

#13 lore raw
In reply to Richard Henderson
Quoted 10 lines · Richard Henderson

We have one host platform (aarch64), and three target platforms (openrisc, unicore32, xtensa) with no built-in disassembly support, thanks largely to gplv3 silliness.

Here's a first-cut at handling these cases with an external tool. The qemu-produced dump file contains just a hex dump of bytes, and a perl script is provided to pass those bytes through objdump.

I've lightly tested this with aarch64 host running on Foundation. Feedback appreciated.

Nice idea, now that QEMU is now more easily portable to new host platforms.

Quoted 18 lines

r~

Richard Henderson (2):
disas: Implement fallback to dump object code as hex
disas: Add disas-objdump.pl

 disas.c                  | 46 +++++++++++++++++++------
 scripts/disas-objdump.pl | 87 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 10 deletions(-)
 create mode 100755 scripts/disas-objdump.pl

--
1.8.3.1

#12 lore raw
In reply to Richard Henderson

Hi, resuming this conversation about external objdump,

Quoted 9 lines · Richard Henderson
Quoted 8 lines · Max Filippov
Quoted 6 lines · Richard Henderson
Quoted 2 lines · Peter Maydell

Well, it depends. If we're going to dump all the in-tree disassemblers and always use an external objdump, that's fine.

It's tempting, given that the only internal disassemblers that are not missing opcodes are for the extinct cpus.

Quoted 6 lines
Quoted 1 line

Will that leave those CPUs without x/i support from monitor?

Yes.

One could always have qemu fork to objdump too. Though finding the right one could be tricky without help.

I lost track of what you mean here.. that we could implement x/i support also using an external objdump?

I got my first actual run of a qemu with integrated libvixl from ARM for disassembly output of aarch64 code, and the results are substandard. Many instructions are missing, in particular all PC relative instructions, and the README warns about multiple other problems. It also introduces dependencies from scons, C++, and can only build and run on 64bit LP64 hosts.
Using external objdump would be very much better for aarch64.

What about making the external objdump the first class citizen here, and removing all the old stuff? Or just keeping it as fallback?

Quoted 4 lines
Quoted 2 lines

Does anybody use it?

That I can't answer.

As I started the aarch64 tcg task I just used gdb's disassembly feature for achieving that goal. However, since the feature is already there and people rely on it, maybe it could be done with external objdump as well?

Quoted 3 lines

r~

Ciao,

Claudio