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 | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 20 | |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 21 | #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 Hiramatsu | 98fe07f | 2017-11-25 00:10:54 +0900 | [diff] [blame^] | 32 | * 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] | 33 | */ |
| 34 | |
| 35 | const char *prog; |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 36 | static int verbose; |
| 37 | static int x86_64; |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 38 | |
| 39 | static void usage(void) |
| 40 | { |
Masami Hiramatsu | 98fe07f | 2017-11-25 00:10:54 +0900 | [diff] [blame^] | 41 | fprintf(stderr, "Usage: objdump -d a.out | awk -f objdump_reformat.awk" |
| 42 | " | %s [-y|-n] [-v]\n", prog); |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 43 | fprintf(stderr, "\t-y 64bit mode\n"); |
| 44 | fprintf(stderr, "\t-n 32bit mode\n"); |
| 45 | fprintf(stderr, "\t-v verbose mode\n"); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 46 | exit(1); |
| 47 | } |
| 48 | |
| 49 | static 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 Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 55 | static 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 | |
| 66 | static void dump_insn(FILE *fp, struct insn *insn) |
| 67 | { |
Frans Pop | 3235dc3 | 2010-02-06 18:47:17 +0100 | [diff] [blame] | 68 | fprintf(fp, "Instruction = {\n"); |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 69 | 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 | |
| 84 | static 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 Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 105 | #define BUFSIZE 256 |
| 106 | |
| 107 | int main(int argc, char **argv) |
| 108 | { |
Masami Hiramatsu | 35039eb | 2009-11-16 18:06:24 -0500 | [diff] [blame] | 109 | char line[BUFSIZE], sym[BUFSIZE] = "<unknown>"; |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 110 | unsigned char insn_buf[16]; |
| 111 | struct insn insn; |
Jean Delvare | be2bf0a | 2009-12-06 12:40:59 +0100 | [diff] [blame] | 112 | int insns = 0; |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 113 | int warnings = 0; |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 114 | |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 115 | parse_args(argc, argv); |
Masami Hiramatsu | 50a482f | 2009-08-28 18:13:19 -0400 | [diff] [blame] | 116 | |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 117 | while (fgets(line, BUFSIZE, stdin)) { |
| 118 | char copy[BUFSIZE], *s, *tab1, *tab2; |
| 119 | int nb = 0; |
| 120 | unsigned int b; |
| 121 | |
Masami Hiramatsu | 35039eb | 2009-11-16 18:06:24 -0500 | [diff] [blame] | 122 | if (line[0] == '<') { |
| 123 | /* Symbol line */ |
| 124 | strcpy(sym, line); |
| 125 | continue; |
| 126 | } |
| 127 | |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 128 | 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 Hansen | 6ba48ff | 2014-11-14 07:39:57 -0800 | [diff] [blame] | 148 | insn_init(&insn, insn_buf, sizeof(insn_buf), x86_64); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 149 | insn_get_length(&insn); |
| 150 | if (insn.length != nb) { |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 151 | warnings++; |
| 152 | fprintf(stderr, "Warning: %s found difference at %s\n", |
Masami Hiramatsu | 35039eb | 2009-11-16 18:06:24 -0500 | [diff] [blame] | 153 | prog, sym); |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 154 | fprintf(stderr, "Warning: %s", line); |
| 155 | fprintf(stderr, "Warning: objdump says %d bytes, but " |
Masami Hiramatsu | d65ff75 | 2009-11-16 18:06:18 -0500 | [diff] [blame] | 156 | "insn_get_length() says %d\n", nb, |
| 157 | insn.length); |
| 158 | if (verbose) |
| 159 | dump_insn(stderr, &insn); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 160 | } |
| 161 | } |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 162 | if (warnings) |
| 163 | fprintf(stderr, "Warning: decoded and checked %d" |
| 164 | " instructions with %d warnings\n", insns, warnings); |
| 165 | else |
Paul Bolle | bdcc18b | 2016-10-25 22:56:04 +0200 | [diff] [blame] | 166 | fprintf(stdout, "Success: decoded and checked %d" |
Masami Hiramatsu | ce64c620 | 2009-11-16 18:06:31 -0500 | [diff] [blame] | 167 | " instructions\n", insns); |
Masami Hiramatsu | ca0e9ba | 2009-08-13 16:34:21 -0400 | [diff] [blame] | 168 | return 0; |
| 169 | } |