]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
ca0e9bad | 2 | /* |
ca0e9bad | 3 | * |
ca0e9bad MH |
4 | * Copyright (C) IBM Corporation, 2009 |
5 | */ | |
6 | ||
7 | #include <stdlib.h> | |
8 | #include <stdio.h> | |
9 | #include <string.h> | |
10 | #include <assert.h> | |
d65ff75f | 11 | #include <unistd.h> |
10c91577 | 12 | #include <stdarg.h> |
ca0e9bad | 13 | |
ca0e9bad MH |
14 | #define unlikely(cond) (cond) |
15 | ||
16 | #include <asm/insn.h> | |
17 | #include <inat.c> | |
18 | #include <insn.c> | |
19 | ||
20 | /* | |
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. | |
24 | * | |
98fe07fc | 25 | * Usage: objdump -d a.out | awk -f objdump_reformat.awk | ./insn_decoder_test |
ca0e9bad MH |
26 | */ |
27 | ||
28 | const char *prog; | |
d65ff75f MH |
29 | static int verbose; |
30 | static int x86_64; | |
ca0e9bad MH |
31 | |
32 | static void usage(void) | |
33 | { | |
98fe07fc MH |
34 | fprintf(stderr, "Usage: objdump -d a.out | awk -f objdump_reformat.awk" |
35 | " | %s [-y|-n] [-v]\n", prog); | |
d65ff75f MH |
36 | fprintf(stderr, "\t-y 64bit mode\n"); |
37 | fprintf(stderr, "\t-n 32bit mode\n"); | |
38 | fprintf(stderr, "\t-v verbose mode\n"); | |
ca0e9bad MH |
39 | exit(1); |
40 | } | |
41 | ||
42 | static void malformed_line(const char *line, int line_nr) | |
43 | { | |
10c91577 MH |
44 | fprintf(stderr, "%s: error: malformed line %d:\n%s", |
45 | prog, line_nr, line); | |
ca0e9bad MH |
46 | exit(3); |
47 | } | |
48 | ||
10c91577 MH |
49 | static void pr_warn(const char *fmt, ...) |
50 | { | |
51 | va_list ap; | |
52 | ||
53 | fprintf(stderr, "%s: warning: ", prog); | |
54 | va_start(ap, fmt); | |
55 | vfprintf(stderr, fmt, ap); | |
56 | va_end(ap); | |
57 | } | |
58 | ||
d65ff75f MH |
59 | static void dump_field(FILE *fp, const char *name, const char *indent, |
60 | struct insn_field *field) | |
61 | { | |
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); | |
68 | } | |
69 | ||
70 | static void dump_insn(FILE *fp, struct insn *insn) | |
71 | { | |
3235dc3f | 72 | fprintf(fp, "Instruction = {\n"); |
d65ff75f MH |
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); | |
86 | } | |
87 | ||
88 | static void parse_args(int argc, char **argv) | |
89 | { | |
90 | int c; | |
91 | prog = argv[0]; | |
92 | while ((c = getopt(argc, argv, "ynv")) != -1) { | |
93 | switch (c) { | |
94 | case 'y': | |
95 | x86_64 = 1; | |
96 | break; | |
97 | case 'n': | |
98 | x86_64 = 0; | |
99 | break; | |
100 | case 'v': | |
101 | verbose = 1; | |
102 | break; | |
103 | default: | |
104 | usage(); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
ca0e9bad MH |
109 | #define BUFSIZE 256 |
110 | ||
111 | int main(int argc, char **argv) | |
112 | { | |
35039eb6 | 113 | char line[BUFSIZE], sym[BUFSIZE] = "<unknown>"; |
ca0e9bad MH |
114 | unsigned char insn_buf[16]; |
115 | struct insn insn; | |
be2bf0a2 | 116 | int insns = 0; |
ce64c620 | 117 | int warnings = 0; |
ca0e9bad | 118 | |
d65ff75f | 119 | parse_args(argc, argv); |
50a482fb | 120 | |
ca0e9bad MH |
121 | while (fgets(line, BUFSIZE, stdin)) { |
122 | char copy[BUFSIZE], *s, *tab1, *tab2; | |
123 | int nb = 0; | |
124 | unsigned int b; | |
125 | ||
35039eb6 MH |
126 | if (line[0] == '<') { |
127 | /* Symbol line */ | |
128 | strcpy(sym, line); | |
129 | continue; | |
130 | } | |
131 | ||
ca0e9bad MH |
132 | insns++; |
133 | memset(insn_buf, 0, 16); | |
134 | strcpy(copy, line); | |
135 | tab1 = strchr(copy, '\t'); | |
136 | if (!tab1) | |
137 | malformed_line(line, insns); | |
138 | s = tab1 + 1; | |
139 | s += strspn(s, " "); | |
140 | tab2 = strchr(s, '\t'); | |
141 | if (!tab2) | |
142 | malformed_line(line, insns); | |
143 | *tab2 = '\0'; /* Characters beyond tab2 aren't examined */ | |
144 | while (s < tab2) { | |
145 | if (sscanf(s, "%x", &b) == 1) { | |
146 | insn_buf[nb++] = (unsigned char) b; | |
147 | s += 3; | |
148 | } else | |
149 | break; | |
150 | } | |
151 | /* Decode an instruction */ | |
6ba48ff4 | 152 | insn_init(&insn, insn_buf, sizeof(insn_buf), x86_64); |
ca0e9bad MH |
153 | insn_get_length(&insn); |
154 | if (insn.length != nb) { | |
ce64c620 | 155 | warnings++; |
10c91577 MH |
156 | pr_warn("Found an x86 instruction decoder bug, " |
157 | "please report this.\n", sym); | |
158 | pr_warn("%s", line); | |
159 | pr_warn("objdump says %d bytes, but insn_get_length() " | |
160 | "says %d\n", nb, insn.length); | |
d65ff75f MH |
161 | if (verbose) |
162 | dump_insn(stderr, &insn); | |
ca0e9bad MH |
163 | } |
164 | } | |
ce64c620 | 165 | if (warnings) |
10c91577 MH |
166 | pr_warn("Decoded and checked %d instructions with %d " |
167 | "failures\n", insns, warnings); | |
ce64c620 | 168 | else |
10c91577 MH |
169 | fprintf(stdout, "%s: success: Decoded and checked %d" |
170 | " instructions\n", prog, insns); | |
ca0e9bad MH |
171 | return 0; |
172 | } |