1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) IBM Corporation, 2009
14 #define unlikely(cond) (cond)
21 * Test of instruction analysis in general and insn_get_length() in
22 * particular. See if insn_get_length() and the disassembler agree
23 * on the length of each instruction in an elf disassembly.
25 * Usage: objdump -d a.out | awk -f objdump_reformat.awk | ./insn_decoder_test
32 static void usage(void)
34 fprintf(stderr
, "Usage: objdump -d a.out | awk -f objdump_reformat.awk"
35 " | %s [-y|-n] [-v]\n", prog
);
36 fprintf(stderr
, "\t-y 64bit mode\n");
37 fprintf(stderr
, "\t-n 32bit mode\n");
38 fprintf(stderr
, "\t-v verbose mode\n");
42 static void malformed_line(const char *line
, int line_nr
)
44 fprintf(stderr
, "%s: error: malformed line %d:\n%s",
49 static void pr_warn(const char *fmt
, ...)
53 fprintf(stderr
, "%s: warning: ", prog
);
55 vfprintf(stderr
, fmt
, ap
);
59 static void dump_field(FILE *fp
, const char *name
, const char *indent
,
60 struct insn_field
*field
)
62 fprintf(fp
, "%s.%s = {\n", indent
, name
);
63 fprintf(fp
, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
64 indent
, field
->value
, field
->bytes
[0], field
->bytes
[1],
65 field
->bytes
[2], field
->bytes
[3]);
66 fprintf(fp
, "%s\t.got = %d, .nbytes = %d},\n", indent
,
67 field
->got
, field
->nbytes
);
70 static void dump_insn(FILE *fp
, struct insn
*insn
)
72 fprintf(fp
, "Instruction = {\n");
73 dump_field(fp
, "prefixes", "\t", &insn
->prefixes
);
74 dump_field(fp
, "rex_prefix", "\t", &insn
->rex_prefix
);
75 dump_field(fp
, "vex_prefix", "\t", &insn
->vex_prefix
);
76 dump_field(fp
, "opcode", "\t", &insn
->opcode
);
77 dump_field(fp
, "modrm", "\t", &insn
->modrm
);
78 dump_field(fp
, "sib", "\t", &insn
->sib
);
79 dump_field(fp
, "displacement", "\t", &insn
->displacement
);
80 dump_field(fp
, "immediate1", "\t", &insn
->immediate1
);
81 dump_field(fp
, "immediate2", "\t", &insn
->immediate2
);
82 fprintf(fp
, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
83 insn
->attr
, insn
->opnd_bytes
, insn
->addr_bytes
);
84 fprintf(fp
, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
85 insn
->length
, insn
->x86_64
, insn
->kaddr
);
88 static void parse_args(int argc
, char **argv
)
92 while ((c
= getopt(argc
, argv
, "ynv")) != -1) {
111 int main(int argc
, char **argv
)
113 char line
[BUFSIZE
], sym
[BUFSIZE
] = "<unknown>";
114 unsigned char insn_buff
[16];
119 parse_args(argc
, argv
);
121 while (fgets(line
, BUFSIZE
, stdin
)) {
122 char copy
[BUFSIZE
], *s
, *tab1
, *tab2
;
126 if (line
[0] == '<') {
133 memset(insn_buff
, 0, 16);
135 tab1
= strchr(copy
, '\t');
137 malformed_line(line
, insns
);
140 tab2
= strchr(s
, '\t');
142 malformed_line(line
, insns
);
143 *tab2
= '\0'; /* Characters beyond tab2 aren't examined */
145 if (sscanf(s
, "%x", &b
) == 1) {
146 insn_buff
[nb
++] = (unsigned char) b
;
152 /* Decode an instruction */
153 ret
= insn_decode(&insn
, insn_buff
, sizeof(insn_buff
),
154 x86_64
? INSN_MODE_64
: INSN_MODE_32
);
156 if (ret
< 0 || insn
.length
!= nb
) {
158 pr_warn("Found an x86 instruction decoder bug, "
159 "please report this.\n", sym
);
161 pr_warn("objdump says %d bytes, but insn_get_length() "
162 "says %d\n", nb
, insn
.length
);
164 dump_insn(stderr
, &insn
);
168 pr_warn("Decoded and checked %d instructions with %d "
169 "failures\n", insns
, warnings
);
171 fprintf(stdout
, "%s: success: Decoded and checked %d"
172 " instructions\n", prog
, insns
);