diff options
Diffstat (limited to 'arch/x86/tools/test_get_len.c')
| -rw-r--r-- | arch/x86/tools/test_get_len.c | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/arch/x86/tools/test_get_len.c b/arch/x86/tools/test_get_len.c new file mode 100644 index 000000000000..af75e07217ba --- /dev/null +++ b/arch/x86/tools/test_get_len.c | |||
| @@ -0,0 +1,168 @@ | |||
| 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 | * | ||
| 12 | * You should have received a copy of the GNU General Public License | ||
| 13 | * along with this program; if not, write to the Free Software | ||
| 14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 15 | * | ||
| 16 | * Copyright (C) IBM Corporation, 2009 | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <stdlib.h> | ||
| 20 | #include <stdio.h> | ||
| 21 | #include <string.h> | ||
| 22 | #include <assert.h> | ||
| 23 | #include <unistd.h> | ||
| 24 | |||
| 25 | #define unlikely(cond) (cond) | ||
| 26 | |||
| 27 | #include <asm/insn.h> | ||
| 28 | #include <inat.c> | ||
| 29 | #include <insn.c> | ||
| 30 | |||
| 31 | /* | ||
| 32 | * Test of instruction analysis in general and insn_get_length() in | ||
| 33 | * particular. See if insn_get_length() and the disassembler agree | ||
| 34 | * on the length of each instruction in an elf disassembly. | ||
| 35 | * | ||
| 36 | * Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len | ||
| 37 | */ | ||
| 38 | |||
| 39 | const char *prog; | ||
| 40 | static int verbose; | ||
| 41 | static int x86_64; | ||
| 42 | |||
| 43 | static void usage(void) | ||
| 44 | { | ||
| 45 | fprintf(stderr, "Usage: objdump -d a.out | awk -f distill.awk |" | ||
| 46 | " %s [-y|-n] [-v] \n", prog); | ||
| 47 | fprintf(stderr, "\t-y 64bit mode\n"); | ||
| 48 | fprintf(stderr, "\t-n 32bit mode\n"); | ||
| 49 | fprintf(stderr, "\t-v verbose mode\n"); | ||
| 50 | exit(1); | ||
| 51 | } | ||
| 52 | |||
| 53 | static void malformed_line(const char *line, int line_nr) | ||
| 54 | { | ||
| 55 | fprintf(stderr, "%s: malformed line %d:\n%s", prog, line_nr, line); | ||
| 56 | exit(3); | ||
| 57 | } | ||
| 58 | |||
| 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 | { | ||
| 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); | ||
| 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 | |||
| 109 | #define BUFSIZE 256 | ||
| 110 | |||
| 111 | int main(int argc, char **argv) | ||
| 112 | { | ||
| 113 | char line[BUFSIZE], sym[BUFSIZE] = "<unknown>"; | ||
| 114 | unsigned char insn_buf[16]; | ||
| 115 | struct insn insn; | ||
| 116 | int insns = 0, c; | ||
| 117 | |||
| 118 | parse_args(argc, argv); | ||
| 119 | |||
| 120 | while (fgets(line, BUFSIZE, stdin)) { | ||
| 121 | char copy[BUFSIZE], *s, *tab1, *tab2; | ||
| 122 | int nb = 0; | ||
| 123 | unsigned int b; | ||
| 124 | |||
| 125 | if (line[0] == '<') { | ||
| 126 | /* Symbol line */ | ||
| 127 | strcpy(sym, line); | ||
| 128 | continue; | ||
| 129 | } | ||
| 130 | |||
| 131 | insns++; | ||
| 132 | memset(insn_buf, 0, 16); | ||
| 133 | strcpy(copy, line); | ||
| 134 | tab1 = strchr(copy, '\t'); | ||
| 135 | if (!tab1) | ||
| 136 | malformed_line(line, insns); | ||
| 137 | s = tab1 + 1; | ||
| 138 | s += strspn(s, " "); | ||
| 139 | tab2 = strchr(s, '\t'); | ||
| 140 | if (!tab2) | ||
| 141 | malformed_line(line, insns); | ||
| 142 | *tab2 = '\0'; /* Characters beyond tab2 aren't examined */ | ||
| 143 | while (s < tab2) { | ||
| 144 | if (sscanf(s, "%x", &b) == 1) { | ||
| 145 | insn_buf[nb++] = (unsigned char) b; | ||
| 146 | s += 3; | ||
| 147 | } else | ||
| 148 | break; | ||
| 149 | } | ||
| 150 | /* Decode an instruction */ | ||
| 151 | insn_init(&insn, insn_buf, x86_64); | ||
| 152 | insn_get_length(&insn); | ||
| 153 | if (insn.length != nb) { | ||
| 154 | fprintf(stderr, "Error: %s found a difference at %s\n", | ||
| 155 | prog, sym); | ||
| 156 | fprintf(stderr, "Error: %s", line); | ||
| 157 | fprintf(stderr, "Error: objdump says %d bytes, but " | ||
| 158 | "insn_get_length() says %d\n", nb, | ||
| 159 | insn.length); | ||
| 160 | if (verbose) | ||
| 161 | dump_insn(stderr, &insn); | ||
| 162 | exit(2); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | fprintf(stderr, "Succeed: decoded and checked %d instructions\n", | ||
| 166 | insns); | ||
| 167 | return 0; | ||
| 168 | } | ||
