2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * Copyright (C) IBM Corporation, 2009
22 #define unlikely(cond) (cond)
29 * Test of instruction analysis in general and insn_get_length() in
30 * particular. See if insn_get_length() and the disassembler agree
31 * on the length of each instruction in an elf disassembly.
33 * Usage: objdump -d a.out | awk -f objdump_reformat.awk | ./insn_decoder_test
40 static void usage(void)
42 fprintf(stderr
, "Usage: objdump -d a.out | awk -f objdump_reformat.awk"
43 " | %s [-y|-n] [-v]\n", prog
);
44 fprintf(stderr
, "\t-y 64bit mode\n");
45 fprintf(stderr
, "\t-n 32bit mode\n");
46 fprintf(stderr
, "\t-v verbose mode\n");
50 static void malformed_line(const char *line
, int line_nr
)
52 fprintf(stderr
, "%s: error: malformed line %d:\n%s",
57 static void pr_warn(const char *fmt
, ...)
61 fprintf(stderr
, "%s: warning: ", prog
);
63 vfprintf(stderr
, fmt
, ap
);
67 static void dump_field(FILE *fp
, const char *name
, const char *indent
,
68 struct insn_field
*field
)
70 fprintf(fp
, "%s.%s = {\n", indent
, name
);
71 fprintf(fp
, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
72 indent
, field
->value
, field
->bytes
[0], field
->bytes
[1],
73 field
->bytes
[2], field
->bytes
[3]);
74 fprintf(fp
, "%s\t.got = %d, .nbytes = %d},\n", indent
,
75 field
->got
, field
->nbytes
);
78 static void dump_insn(FILE *fp
, struct insn
*insn
)
80 fprintf(fp
, "Instruction = {\n");
81 dump_field(fp
, "prefixes", "\t", &insn
->prefixes
);
82 dump_field(fp
, "rex_prefix", "\t", &insn
->rex_prefix
);
83 dump_field(fp
, "vex_prefix", "\t", &insn
->vex_prefix
);
84 dump_field(fp
, "opcode", "\t", &insn
->opcode
);
85 dump_field(fp
, "modrm", "\t", &insn
->modrm
);
86 dump_field(fp
, "sib", "\t", &insn
->sib
);
87 dump_field(fp
, "displacement", "\t", &insn
->displacement
);
88 dump_field(fp
, "immediate1", "\t", &insn
->immediate1
);
89 dump_field(fp
, "immediate2", "\t", &insn
->immediate2
);
90 fprintf(fp
, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
91 insn
->attr
, insn
->opnd_bytes
, insn
->addr_bytes
);
92 fprintf(fp
, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
93 insn
->length
, insn
->x86_64
, insn
->kaddr
);
96 static void parse_args(int argc
, char **argv
)
100 while ((c
= getopt(argc
, argv
, "ynv")) != -1) {
119 int main(int argc
, char **argv
)
121 char line
[BUFSIZE
], sym
[BUFSIZE
] = "<unknown>";
122 unsigned char insn_buf
[16];
127 parse_args(argc
, argv
);
129 while (fgets(line
, BUFSIZE
, stdin
)) {
130 char copy
[BUFSIZE
], *s
, *tab1
, *tab2
;
134 if (line
[0] == '<') {
141 memset(insn_buf
, 0, 16);
143 tab1
= strchr(copy
, '\t');
145 malformed_line(line
, insns
);
148 tab2
= strchr(s
, '\t');
150 malformed_line(line
, insns
);
151 *tab2
= '\0'; /* Characters beyond tab2 aren't examined */
153 if (sscanf(s
, "%x", &b
) == 1) {
154 insn_buf
[nb
++] = (unsigned char) b
;
159 /* Decode an instruction */
160 insn_init(&insn
, insn_buf
, sizeof(insn_buf
), x86_64
);
161 insn_get_length(&insn
);
162 if (insn
.length
!= nb
) {
164 pr_warn("Found an x86 instruction decoder bug, "
165 "please report this.\n", sym
);
167 pr_warn("objdump says %d bytes, but insn_get_length() "
168 "says %d\n", nb
, insn
.length
);
170 dump_insn(stderr
, &insn
);
174 pr_warn("Decoded and checked %d instructions with %d "
175 "failures\n", insns
, warnings
);
177 fprintf(stdout
, "%s: success: Decoded and checked %d"
178 " instructions\n", prog
, insns
);