1#!/usr/bin/perl
2# test_x86dis.pl
3# ... actually this is a general test utility for libdisasm; however, it
4# is also useful for comparing the AT&T syntax output of x86dis (which is
5# generated by libdisasm) against what GNU as(1) expects.
6# NOTE: this script expects to be run from $LIBDISASM/libdisasm/test
7# and uses the instructions in ia32_test_insn.S to test disassembly.
8use warnings;
9use strict;
10
11# assembler settings
12my $asm_file = $ARGV[0] || 'ia32_test_insn.S';
13my $obj_file = $ARGV[1] || 'ia32.o';
14my $disasm_format = $ARGV[2] || "att";
15
16# relative path, ld_library_path for running x86dis from compile dir
17my $x86dis = '../x86dis/x86dis';
18# path is not needed now that libtool is being used
19#my $ldpath = 'LD_LIBRARY_PATH="../libdisasm"';
20my $ldpath = '';
21
22# uninitialized stuff
23my ($line, $output);
24
25system "as --32 -o $obj_file $asm_file";
26exit(1) if ($? != 0);
27
28$output = (grep(/\.text/,`objdump -h $obj_file`))[0];
29$output =~ s/^\s+//g;
30my ($idx,$name,$size,$vma,$lma,$offset,$align)=split(/\s+/,$output);
31$size = hex($size);
32$offset = hex($offset);
33
34$output = `$ldpath $x86dis -f $obj_file -s $disasm_format -r $offset $size`;
35exit(1) if ($? != 0);
36
37foreach ( split /\n/, $output ) {
38	chomp;
39
40	# skip comment lines
41	next if /^#/;
42
43	$line = $_;
44	$line =~ s/\s+/ /g;
45	$line =~ s/\s+$//;
46	$line =~ s/#.*//;
47	$line = lc $line;
48	if ($line =~ s/^[0-9][0-9a-f]+\s+(([0-9a-f]{2} )+)//)
49	{
50		my $byte_string = $1;
51		$byte_string =~ s/ +$//g;
52
53		printf("%s # %s\n",$line,$byte_string);
54	}
55	else {
56		print $line,"\n";
57	}
58}
59
60exit(0);
61