* [Qemu-devel] [PATCH v2 1/3] disas: Implement fallback to dump object code as hex
2013-08-17 6:29 [Qemu-devel] [PATCH v2 0/3] Disassembly with external objdump Richard Henderson
@ 2013-08-17 6:29 ` Richard Henderson
2013-08-17 6:29 ` [Qemu-devel] [PATCH v2 2/3] disas: Add disas-objdump.pl Richard Henderson
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Richard Henderson @ 2013-08-17 6:29 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori
The OBJD-[HT] tags will be used by a script to run the hex blob
through objdump --disassemble.
Signed-off-by: Richard Henderson <rth@twiddle.net>
---
disas.c | 47 +++++++++++++++++++++++++++++++++++++----------
1 file changed, 37 insertions(+), 10 deletions(-)
diff --git a/disas.c b/disas.c
index 71007fb..0203ef2 100644
--- a/disas.c
+++ b/disas.c
@@ -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);
--
1.8.1.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [Qemu-devel] [PATCH v2 2/3] disas: Add disas-objdump.pl
2013-08-17 6:29 [Qemu-devel] [PATCH v2 0/3] Disassembly with external objdump Richard Henderson
2013-08-17 6:29 ` [Qemu-devel] [PATCH v2 1/3] disas: Implement fallback to dump object code as hex Richard Henderson
@ 2013-08-17 6:29 ` Richard Henderson
2013-08-17 6:29 ` [Qemu-devel] [PATCH v2 3/3] disas-objdump: Pass --adjust-vma to objdump Richard Henderson
2013-08-24 5:32 ` [Qemu-devel] [PATCH v2 0/3] Disassembly with external objdump Edgar E. Iglesias
3 siblings, 0 replies; 5+ messages in thread
From: Richard Henderson @ 2013-08-17 6:29 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori
The script massages the output produced for architectures that are
not supported internally by qemu though an external objdump program
for disassembly.
Signed-off-by: Richard Henderson <rth@twiddle.net>
---
scripts/disas-objdump.pl | 87 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 87 insertions(+)
create mode 100755 scripts/disas-objdump.pl
diff --git a/scripts/disas-objdump.pl b/scripts/disas-objdump.pl
new file mode 100755
index 0000000..c66a629
--- /dev/null
+++ b/scripts/disas-objdump.pl
@@ -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;
+ }
+}
--
1.8.1.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [Qemu-devel] [PATCH v2 3/3] disas-objdump: Pass --adjust-vma to objdump
2013-08-17 6:29 [Qemu-devel] [PATCH v2 0/3] Disassembly with external objdump Richard Henderson
2013-08-17 6:29 ` [Qemu-devel] [PATCH v2 1/3] disas: Implement fallback to dump object code as hex Richard Henderson
2013-08-17 6:29 ` [Qemu-devel] [PATCH v2 2/3] disas: Add disas-objdump.pl Richard Henderson
@ 2013-08-17 6:29 ` Richard Henderson
2013-08-24 5:32 ` [Qemu-devel] [PATCH v2 0/3] Disassembly with external objdump Edgar E. Iglesias
3 siblings, 0 replies; 5+ messages in thread
From: Richard Henderson @ 2013-08-17 6:29 UTC (permalink / raw)
To: qemu-devel; +Cc: aliguori
This gives the dumped blob its correct address during disassembly,
which makes pc-relative insns much easier to interpret.
Signed-off-by: Richard Henderson <rth@twiddle.net>
---
scripts/disas-objdump.pl | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/scripts/disas-objdump.pl b/scripts/disas-objdump.pl
index c66a629..8f7e818 100755
--- a/scripts/disas-objdump.pl
+++ b/scripts/disas-objdump.pl
@@ -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;
}
--
1.8.1.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH v2 0/3] Disassembly with external objdump
2013-08-17 6:29 [Qemu-devel] [PATCH v2 0/3] Disassembly with external objdump Richard Henderson
` (2 preceding siblings ...)
2013-08-17 6:29 ` [Qemu-devel] [PATCH v2 3/3] disas-objdump: Pass --adjust-vma to objdump Richard Henderson
@ 2013-08-24 5:32 ` Edgar E. Iglesias
3 siblings, 0 replies; 5+ messages in thread
From: Edgar E. Iglesias @ 2013-08-24 5:32 UTC (permalink / raw)
To: Richard Henderson; +Cc: aliguori, qemu-devel
On Fri, Aug 16, 2013 at 11:29:44PM -0700, Richard Henderson wrote:
> V2 adds the --adjust-vma idea from Max Filippov.
Thanks Richard, I've applied this
Cheers,
Edgar
>
>
> 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
>
> --
> 1.8.1.4
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread