test_get_len.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #define unlikely(cond) (cond)
  24. #include <asm/insn.h>
  25. #include <inat.c>
  26. #include <insn.c>
  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. *
  32. * Usage: objdump -d a.out | awk -f distill.awk | ./test_get_len
  33. */
  34. const char *prog;
  35. static int verbose;
  36. static int x86_64;
  37. static void usage(void)
  38. {
  39. fprintf(stderr, "Usage: objdump -d a.out | awk -f distill.awk |"
  40. " %s [-y|-n] [-v]\n", prog);
  41. fprintf(stderr, "\t-y 64bit mode\n");
  42. fprintf(stderr, "\t-n 32bit mode\n");
  43. fprintf(stderr, "\t-v verbose mode\n");
  44. exit(1);
  45. }
  46. static void malformed_line(const char *line, int line_nr)
  47. {
  48. fprintf(stderr, "%s: malformed line %d:\n%s", prog, line_nr, line);
  49. exit(3);
  50. }
  51. static void dump_field(FILE *fp, const char *name, const char *indent,
  52. struct insn_field *field)
  53. {
  54. fprintf(fp, "%s.%s = {\n", indent, name);
  55. fprintf(fp, "%s\t.value = %d, bytes[] = {%x, %x, %x, %x},\n",
  56. indent, field->value, field->bytes[0], field->bytes[1],
  57. field->bytes[2], field->bytes[3]);
  58. fprintf(fp, "%s\t.got = %d, .nbytes = %d},\n", indent,
  59. field->got, field->nbytes);
  60. }
  61. static void dump_insn(FILE *fp, struct insn *insn)
  62. {
  63. fprintf(fp, "Instruction = {\n");
  64. dump_field(fp, "prefixes", "\t", &insn->prefixes);
  65. dump_field(fp, "rex_prefix", "\t", &insn->rex_prefix);
  66. dump_field(fp, "vex_prefix", "\t", &insn->vex_prefix);
  67. dump_field(fp, "opcode", "\t", &insn->opcode);
  68. dump_field(fp, "modrm", "\t", &insn->modrm);
  69. dump_field(fp, "sib", "\t", &insn->sib);
  70. dump_field(fp, "displacement", "\t", &insn->displacement);
  71. dump_field(fp, "immediate1", "\t", &insn->immediate1);
  72. dump_field(fp, "immediate2", "\t", &insn->immediate2);
  73. fprintf(fp, "\t.attr = %x, .opnd_bytes = %d, .addr_bytes = %d,\n",
  74. insn->attr, insn->opnd_bytes, insn->addr_bytes);
  75. fprintf(fp, "\t.length = %d, .x86_64 = %d, .kaddr = %p}\n",
  76. insn->length, insn->x86_64, insn->kaddr);
  77. }
  78. static void parse_args(int argc, char **argv)
  79. {
  80. int c;
  81. prog = argv[0];
  82. while ((c = getopt(argc, argv, "ynv")) != -1) {
  83. switch (c) {
  84. case 'y':
  85. x86_64 = 1;
  86. break;
  87. case 'n':
  88. x86_64 = 0;
  89. break;
  90. case 'v':
  91. verbose = 1;
  92. break;
  93. default:
  94. usage();
  95. }
  96. }
  97. }
  98. #define BUFSIZE 256
  99. int main(int argc, char **argv)
  100. {
  101. char line[BUFSIZE], sym[BUFSIZE] = "<unknown>";
  102. unsigned char insn_buf[16];
  103. struct insn insn;
  104. int insns = 0;
  105. int warnings = 0;
  106. parse_args(argc, argv);
  107. while (fgets(line, BUFSIZE, stdin)) {
  108. char copy[BUFSIZE], *s, *tab1, *tab2;
  109. int nb = 0;
  110. unsigned int b;
  111. if (line[0] == '<') {
  112. /* Symbol line */
  113. strcpy(sym, line);
  114. continue;
  115. }
  116. insns++;
  117. memset(insn_buf, 0, 16);
  118. strcpy(copy, line);
  119. tab1 = strchr(copy, '\t');
  120. if (!tab1)
  121. malformed_line(line, insns);
  122. s = tab1 + 1;
  123. s += strspn(s, " ");
  124. tab2 = strchr(s, '\t');
  125. if (!tab2)
  126. malformed_line(line, insns);
  127. *tab2 = '\0'; /* Characters beyond tab2 aren't examined */
  128. while (s < tab2) {
  129. if (sscanf(s, "%x", &b) == 1) {
  130. insn_buf[nb++] = (unsigned char) b;
  131. s += 3;
  132. } else
  133. break;
  134. }
  135. /* Decode an instruction */
  136. insn_init(&insn, insn_buf, sizeof(insn_buf), x86_64);
  137. insn_get_length(&insn);
  138. if (insn.length != nb) {
  139. warnings++;
  140. fprintf(stderr, "Warning: %s found difference at %s\n",
  141. prog, sym);
  142. fprintf(stderr, "Warning: %s", line);
  143. fprintf(stderr, "Warning: objdump says %d bytes, but "
  144. "insn_get_length() says %d\n", nb,
  145. insn.length);
  146. if (verbose)
  147. dump_insn(stderr, &insn);
  148. }
  149. }
  150. if (warnings)
  151. fprintf(stderr, "Warning: decoded and checked %d"
  152. " instructions with %d warnings\n", insns, warnings);
  153. else
  154. fprintf(stderr, "Succeed: decoded and checked %d"
  155. " instructions\n", insns);
  156. return 0;
  157. }