blob: 286d2e3b9d57489046b473f2aa8a5e9e9bd94bde [file] [log] [blame]
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -04001/*
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.
6 *
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.
11 *
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -040012 * Copyright (C) IBM Corporation, 2009
13 */
14
15#include <stdlib.h>
16#include <stdio.h>
17#include <string.h>
18#include <assert.h>
Masami Hiramatsud65ff752009-11-16 18:06:18 -050019#include <unistd.h>
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -040020
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -040021#define unlikely(cond) (cond)
22
23#include <asm/insn.h>
24#include <inat.c>
25#include <insn.c>
26
27/*
28 * Test of instruction analysis in general and insn_get_length() in
29 * particular. See if insn_get_length() and the disassembler agree
30 * on the length of each instruction in an elf disassembly.
31 *
Masami Hiramatsu98fe07f2017-11-25 00:10:54 +090032 * Usage: objdump -d a.out | awk -f objdump_reformat.awk | ./insn_decoder_test
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -040033 */
34
35const char *prog;
Masami Hiramatsud65ff752009-11-16 18:06:18 -050036static int verbose;
37static int x86_64;
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -040038
39static void usage(void)
40{
Masami Hiramatsu98fe07f2017-11-25 00:10:54 +090041 fprintf(stderr, "Usage: objdump -d a.out | awk -f objdump_reformat.awk"
42 " | %s [-y|-n] [-v]\n", prog);
Masami Hiramatsud65ff752009-11-16 18:06:18 -050043 fprintf(stderr, "\t-y 64bit mode\n");
44 fprintf(stderr, "\t-n 32bit mode\n");
45 fprintf(stderr, "\t-v verbose mode\n");
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -040046 exit(1);
47}
48
49static void malformed_line(const char *line, int line_nr)
50{
51 fprintf(stderr, "%s: malformed line %d:\n%s", prog, line_nr, line);
52 exit(3);
53}
54
Masami Hiramatsud65ff752009-11-16 18:06:18 -050055static void dump_field(FILE *fp, const char *name, const char *indent,
56 struct insn_field *field)
57{
58 fprintf(fp, "%s.%s = {\n", indent, name);
59 fprintf(fp, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
60 indent, field->value, field->bytes[0], field->bytes[1],
61 field->bytes[2], field->bytes[3]);
62 fprintf(fp, "%s\t.got = %d, .nbytes = %d},\n", indent,
63 field->got, field->nbytes);
64}
65
66static void dump_insn(FILE *fp, struct insn *insn)
67{
Frans Pop3235dc32010-02-06 18:47:17 +010068 fprintf(fp, "Instruction = {\n");
Masami Hiramatsud65ff752009-11-16 18:06:18 -050069 dump_field(fp, "prefixes", "\t", &insn->prefixes);
70 dump_field(fp, "rex_prefix", "\t", &insn->rex_prefix);
71 dump_field(fp, "vex_prefix", "\t", &insn->vex_prefix);
72 dump_field(fp, "opcode", "\t", &insn->opcode);
73 dump_field(fp, "modrm", "\t", &insn->modrm);
74 dump_field(fp, "sib", "\t", &insn->sib);
75 dump_field(fp, "displacement", "\t", &insn->displacement);
76 dump_field(fp, "immediate1", "\t", &insn->immediate1);
77 dump_field(fp, "immediate2", "\t", &insn->immediate2);
78 fprintf(fp, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
79 insn->attr, insn->opnd_bytes, insn->addr_bytes);
80 fprintf(fp, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
81 insn->length, insn->x86_64, insn->kaddr);
82}
83
84static void parse_args(int argc, char **argv)
85{
86 int c;
87 prog = argv[0];
88 while ((c = getopt(argc, argv, "ynv")) != -1) {
89 switch (c) {
90 case 'y':
91 x86_64 = 1;
92 break;
93 case 'n':
94 x86_64 = 0;
95 break;
96 case 'v':
97 verbose = 1;
98 break;
99 default:
100 usage();
101 }
102 }
103}
104
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400105#define BUFSIZE 256
106
107int main(int argc, char **argv)
108{
Masami Hiramatsu35039eb2009-11-16 18:06:24 -0500109 char line[BUFSIZE], sym[BUFSIZE] = "<unknown>";
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400110 unsigned char insn_buf[16];
111 struct insn insn;
Jean Delvarebe2bf0a2009-12-06 12:40:59 +0100112 int insns = 0;
Masami Hiramatsuce64c6202009-11-16 18:06:31 -0500113 int warnings = 0;
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400114
Masami Hiramatsud65ff752009-11-16 18:06:18 -0500115 parse_args(argc, argv);
Masami Hiramatsu50a482f2009-08-28 18:13:19 -0400116
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400117 while (fgets(line, BUFSIZE, stdin)) {
118 char copy[BUFSIZE], *s, *tab1, *tab2;
119 int nb = 0;
120 unsigned int b;
121
Masami Hiramatsu35039eb2009-11-16 18:06:24 -0500122 if (line[0] == '<') {
123 /* Symbol line */
124 strcpy(sym, line);
125 continue;
126 }
127
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400128 insns++;
129 memset(insn_buf, 0, 16);
130 strcpy(copy, line);
131 tab1 = strchr(copy, '\t');
132 if (!tab1)
133 malformed_line(line, insns);
134 s = tab1 + 1;
135 s += strspn(s, " ");
136 tab2 = strchr(s, '\t');
137 if (!tab2)
138 malformed_line(line, insns);
139 *tab2 = '\0'; /* Characters beyond tab2 aren't examined */
140 while (s < tab2) {
141 if (sscanf(s, "%x", &b) == 1) {
142 insn_buf[nb++] = (unsigned char) b;
143 s += 3;
144 } else
145 break;
146 }
147 /* Decode an instruction */
Dave Hansen6ba48ff2014-11-14 07:39:57 -0800148 insn_init(&insn, insn_buf, sizeof(insn_buf), x86_64);
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400149 insn_get_length(&insn);
150 if (insn.length != nb) {
Masami Hiramatsuce64c6202009-11-16 18:06:31 -0500151 warnings++;
152 fprintf(stderr, "Warning: %s found difference at %s\n",
Masami Hiramatsu35039eb2009-11-16 18:06:24 -0500153 prog, sym);
Masami Hiramatsuce64c6202009-11-16 18:06:31 -0500154 fprintf(stderr, "Warning: %s", line);
155 fprintf(stderr, "Warning: objdump says %d bytes, but "
Masami Hiramatsud65ff752009-11-16 18:06:18 -0500156 "insn_get_length() says %d\n", nb,
157 insn.length);
158 if (verbose)
159 dump_insn(stderr, &insn);
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400160 }
161 }
Masami Hiramatsuce64c6202009-11-16 18:06:31 -0500162 if (warnings)
163 fprintf(stderr, "Warning: decoded and checked %d"
164 " instructions with %d warnings\n", insns, warnings);
165 else
Paul Bollebdcc18b2016-10-25 22:56:04 +0200166 fprintf(stdout, "Success: decoded and checked %d"
Masami Hiramatsuce64c6202009-11-16 18:06:31 -0500167 " instructions\n", insns);
Masami Hiramatsuca0e9ba2009-08-13 16:34:21 -0400168 return 0;
169}