Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 1 | /* |
| 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 Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 12 | * Copyright (C) IBM Corporation, 2009 |
| 13 | */ |
| 14 | |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <assert.h> |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 19 | #include <unistd.h> |
Masami Hiramatsu | 10c9157 | 2017-11-25 00:11:22 +0900 | [diff] [blame^] | 20 | #include <stdarg.h> |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 21 | |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 22 | #define unlikely(cond) (cond) |
| 23 | |
| 24 | #include <asm/insn.h> |
| 25 | #include <inat.c> |
| 26 | #include <insn.c> |
| 27 | |
| 28 | /* |
| 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. |
| 32 | * |
Masami Hiramatsu | 98fe07f | 2017-11-25 00:10:54 +0900 | [diff] [blame] | 33 | * Usage: objdump -d a.out | awk -f objdump_reformat.awk | ./insn_decoder_test |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | const char *prog; |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 37 | static int verbose; |
| 38 | static int x86_64; |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 39 | |
| 40 | static void usage(void) |
| 41 | { |
Masami Hiramatsu | 98fe07f | 2017-11-25 00:10:54 +0900 | [diff] [blame] | 42 | fprintf(stderr, "Usage: objdump -d a.out | awk -f objdump_reformat.awk" |
| 43 | " | %s [-y|-n] [-v]\n", prog); |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 44 | fprintf(stderr, "\t-y 64bit mode\n"); |
| 45 | fprintf(stderr, "\t-n 32bit mode\n"); |
| 46 | fprintf(stderr, "\t-v verbose mode\n"); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | static void malformed_line(const char *line, int line_nr) |
| 51 | { |
Masami Hiramatsu | 10c9157 | 2017-11-25 00:11:22 +0900 | [diff] [blame^] | 52 | fprintf(stderr, "%s: error: malformed line %d:\n%s", |
| 53 | prog, line_nr, line); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 54 | exit(3); |
| 55 | } |
| 56 | |
Masami Hiramatsu | 10c9157 | 2017-11-25 00:11:22 +0900 | [diff] [blame^] | 57 | static void pr_warn(const char *fmt, ...) |
| 58 | { |
| 59 | va_list ap; |
| 60 | |
| 61 | fprintf(stderr, "%s: warning: ", prog); |
| 62 | va_start(ap, fmt); |
| 63 | vfprintf(stderr, fmt, ap); |
| 64 | va_end(ap); |
| 65 | } |
| 66 | |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 67 | static void dump_field(FILE *fp, const char *name, const char *indent, |
| 68 | struct insn_field *field) |
| 69 | { |
| 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); |
| 76 | } |
| 77 | |
| 78 | static void dump_insn(FILE *fp, struct insn *insn) |
| 79 | { |
Frans Pop | 3235dc3 | 2010-02-06 18:47:17 +0100 | [diff] [blame] | 80 | fprintf(fp, "Instruction = {\n"); |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 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); |
| 94 | } |
| 95 | |
| 96 | static void parse_args(int argc, char **argv) |
| 97 | { |
| 98 | int c; |
| 99 | prog = argv[0]; |
| 100 | while ((c = getopt(argc, argv, "ynv")) != -1) { |
| 101 | switch (c) { |
| 102 | case 'y': |
| 103 | x86_64 = 1; |
| 104 | break; |
| 105 | case 'n': |
| 106 | x86_64 = 0; |
| 107 | break; |
| 108 | case 'v': |
| 109 | verbose = 1; |
| 110 | break; |
| 111 | default: |
| 112 | usage(); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 117 | #define BUFSIZE 256 |
| 118 | |
| 119 | int main(int argc, char **argv) |
| 120 | { |
Masami Hiramatsu | 35039eb | 2009-11-16 18:06:24 -0500 | [diff] [blame] | 121 | char line[BUFSIZE], sym[BUFSIZE] = "<unknown>"; |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 122 | unsigned char insn_buf[16]; |
| 123 | struct insn insn; |
Jean Delvare | be2bf0a | 2009-12-06 12:40:59 +0100 | [diff] [blame] | 124 | int insns = 0; |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 125 | int warnings = 0; |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 126 | |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 127 | parse_args(argc, argv); |
Masami Hiramatsu | 50a482f | 2009-08-28 18:13:19 -0400 | [diff] [blame] | 128 | |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 129 | while (fgets(line, BUFSIZE, stdin)) { |
| 130 | char copy[BUFSIZE], *s, *tab1, *tab2; |
| 131 | int nb = 0; |
| 132 | unsigned int b; |
| 133 | |
Masami Hiramatsu | 35039eb | 2009-11-16 18:06:24 -0500 | [diff] [blame] | 134 | if (line[0] == '<') { |
| 135 | /* Symbol line */ |
| 136 | strcpy(sym, line); |
| 137 | continue; |
| 138 | } |
| 139 | |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 140 | insns++; |
| 141 | memset(insn_buf, 0, 16); |
| 142 | strcpy(copy, line); |
| 143 | tab1 = strchr(copy, '\t'); |
| 144 | if (!tab1) |
| 145 | malformed_line(line, insns); |
| 146 | s = tab1 + 1; |
| 147 | s += strspn(s, " "); |
| 148 | tab2 = strchr(s, '\t'); |
| 149 | if (!tab2) |
| 150 | malformed_line(line, insns); |
| 151 | *tab2 = '\0'; /* Characters beyond tab2 aren't examined */ |
| 152 | while (s < tab2) { |
| 153 | if (sscanf(s, "%x", &b) == 1) { |
| 154 | insn_buf[nb++] = (unsigned char) b; |
| 155 | s += 3; |
| 156 | } else |
| 157 | break; |
| 158 | } |
| 159 | /* Decode an instruction */ |
Dave Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 160 | insn_init(&insn, insn_buf, sizeof(insn_buf), x86_64); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 161 | insn_get_length(&insn); |
| 162 | if (insn.length != nb) { |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 163 | warnings++; |
Masami Hiramatsu | 10c9157 | 2017-11-25 00:11:22 +0900 | [diff] [blame^] | 164 | pr_warn("Found an x86 instruction decoder bug, " |
| 165 | "please report this.\n", sym); |
| 166 | pr_warn("%s", line); |
| 167 | pr_warn("objdump says %d bytes, but insn_get_length() " |
| 168 | "says %d\n", nb, insn.length); |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 169 | if (verbose) |
| 170 | dump_insn(stderr, &insn); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 171 | } |
| 172 | } |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 173 | if (warnings) |
Masami Hiramatsu | 10c9157 | 2017-11-25 00:11:22 +0900 | [diff] [blame^] | 174 | pr_warn("Decoded and checked %d instructions with %d " |
| 175 | "failures\n", insns, warnings); |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 176 | else |
Masami Hiramatsu | 10c9157 | 2017-11-25 00:11:22 +0900 | [diff] [blame^] | 177 | fprintf(stdout, "%s: success: Decoded and checked %d" |
| 178 | " instructions\n", prog, insns); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 179 | return 0; |
| 180 | } |