decode.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (C) 2015 Josh Poimboeuf <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #define unlikely(cond) (cond)
  20. #include <asm/insn.h>
  21. #include "lib/inat.c"
  22. #include "lib/insn.c"
  23. #include "../../elf.h"
  24. #include "../../arch.h"
  25. #include "../../warn.h"
  26. static unsigned char op_to_cfi_reg[][2] = {
  27. {CFI_AX, CFI_R8},
  28. {CFI_CX, CFI_R9},
  29. {CFI_DX, CFI_R10},
  30. {CFI_BX, CFI_R11},
  31. {CFI_SP, CFI_R12},
  32. {CFI_BP, CFI_R13},
  33. {CFI_SI, CFI_R14},
  34. {CFI_DI, CFI_R15},
  35. };
  36. static int is_x86_64(struct elf *elf)
  37. {
  38. switch (elf->ehdr.e_machine) {
  39. case EM_X86_64:
  40. return 1;
  41. case EM_386:
  42. return 0;
  43. default:
  44. WARN("unexpected ELF machine type %d", elf->ehdr.e_machine);
  45. return -1;
  46. }
  47. }
  48. bool arch_callee_saved_reg(unsigned char reg)
  49. {
  50. switch (reg) {
  51. case CFI_BP:
  52. case CFI_BX:
  53. case CFI_R12:
  54. case CFI_R13:
  55. case CFI_R14:
  56. case CFI_R15:
  57. return true;
  58. case CFI_AX:
  59. case CFI_CX:
  60. case CFI_DX:
  61. case CFI_SI:
  62. case CFI_DI:
  63. case CFI_SP:
  64. case CFI_R8:
  65. case CFI_R9:
  66. case CFI_R10:
  67. case CFI_R11:
  68. case CFI_RA:
  69. default:
  70. return false;
  71. }
  72. }
  73. int arch_decode_instruction(struct elf *elf, struct section *sec,
  74. unsigned long offset, unsigned int maxlen,
  75. unsigned int *len, unsigned char *type,
  76. unsigned long *immediate, struct stack_op *op)
  77. {
  78. struct insn insn;
  79. int x86_64, sign;
  80. unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
  81. rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
  82. modrm_reg = 0, sib = 0;
  83. x86_64 = is_x86_64(elf);
  84. if (x86_64 == -1)
  85. return -1;
  86. insn_init(&insn, sec->data->d_buf + offset, maxlen, x86_64);
  87. insn_get_length(&insn);
  88. if (!insn_complete(&insn)) {
  89. WARN_FUNC("can't decode instruction", sec, offset);
  90. return -1;
  91. }
  92. *len = insn.length;
  93. *type = INSN_OTHER;
  94. if (insn.vex_prefix.nbytes)
  95. return 0;
  96. op1 = insn.opcode.bytes[0];
  97. op2 = insn.opcode.bytes[1];
  98. if (insn.rex_prefix.nbytes) {
  99. rex = insn.rex_prefix.bytes[0];
  100. rex_w = X86_REX_W(rex) >> 3;
  101. rex_r = X86_REX_R(rex) >> 2;
  102. rex_x = X86_REX_X(rex) >> 1;
  103. rex_b = X86_REX_B(rex);
  104. }
  105. if (insn.modrm.nbytes) {
  106. modrm = insn.modrm.bytes[0];
  107. modrm_mod = X86_MODRM_MOD(modrm);
  108. modrm_reg = X86_MODRM_REG(modrm);
  109. modrm_rm = X86_MODRM_RM(modrm);
  110. }
  111. if (insn.sib.nbytes)
  112. sib = insn.sib.bytes[0];
  113. switch (op1) {
  114. case 0x1:
  115. case 0x29:
  116. if (rex_w && !rex_b && modrm_mod == 3 && modrm_rm == 4) {
  117. /* add/sub reg, %rsp */
  118. *type = INSN_STACK;
  119. op->src.type = OP_SRC_ADD;
  120. op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
  121. op->dest.type = OP_DEST_REG;
  122. op->dest.reg = CFI_SP;
  123. }
  124. break;
  125. case 0x50 ... 0x57:
  126. /* push reg */
  127. *type = INSN_STACK;
  128. op->src.type = OP_SRC_REG;
  129. op->src.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
  130. op->dest.type = OP_DEST_PUSH;
  131. break;
  132. case 0x58 ... 0x5f:
  133. /* pop reg */
  134. *type = INSN_STACK;
  135. op->src.type = OP_SRC_POP;
  136. op->dest.type = OP_DEST_REG;
  137. op->dest.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
  138. break;
  139. case 0x68:
  140. case 0x6a:
  141. /* push immediate */
  142. *type = INSN_STACK;
  143. op->src.type = OP_SRC_CONST;
  144. op->dest.type = OP_DEST_PUSH;
  145. break;
  146. case 0x70 ... 0x7f:
  147. *type = INSN_JUMP_CONDITIONAL;
  148. break;
  149. case 0x81:
  150. case 0x83:
  151. if (rex != 0x48)
  152. break;
  153. if (modrm == 0xe4) {
  154. /* and imm, %rsp */
  155. *type = INSN_STACK;
  156. op->src.type = OP_SRC_AND;
  157. op->src.reg = CFI_SP;
  158. op->src.offset = insn.immediate.value;
  159. op->dest.type = OP_DEST_REG;
  160. op->dest.reg = CFI_SP;
  161. break;
  162. }
  163. if (modrm == 0xc4)
  164. sign = 1;
  165. else if (modrm == 0xec)
  166. sign = -1;
  167. else
  168. break;
  169. /* add/sub imm, %rsp */
  170. *type = INSN_STACK;
  171. op->src.type = OP_SRC_ADD;
  172. op->src.reg = CFI_SP;
  173. op->src.offset = insn.immediate.value * sign;
  174. op->dest.type = OP_DEST_REG;
  175. op->dest.reg = CFI_SP;
  176. break;
  177. case 0x89:
  178. if (rex_w && !rex_r && modrm_mod == 3 && modrm_reg == 4) {
  179. /* mov %rsp, reg */
  180. *type = INSN_STACK;
  181. op->src.type = OP_SRC_REG;
  182. op->src.reg = CFI_SP;
  183. op->dest.type = OP_DEST_REG;
  184. op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
  185. break;
  186. }
  187. if (rex_w && !rex_b && modrm_mod == 3 && modrm_rm == 4) {
  188. /* mov reg, %rsp */
  189. *type = INSN_STACK;
  190. op->src.type = OP_SRC_REG;
  191. op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
  192. op->dest.type = OP_DEST_REG;
  193. op->dest.reg = CFI_SP;
  194. break;
  195. }
  196. /* fallthrough */
  197. case 0x88:
  198. if (!rex_b &&
  199. (modrm_mod == 1 || modrm_mod == 2) && modrm_rm == 5) {
  200. /* mov reg, disp(%rbp) */
  201. *type = INSN_STACK;
  202. op->src.type = OP_SRC_REG;
  203. op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
  204. op->dest.type = OP_DEST_REG_INDIRECT;
  205. op->dest.reg = CFI_BP;
  206. op->dest.offset = insn.displacement.value;
  207. } else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
  208. /* mov reg, disp(%rsp) */
  209. *type = INSN_STACK;
  210. op->src.type = OP_SRC_REG;
  211. op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
  212. op->dest.type = OP_DEST_REG_INDIRECT;
  213. op->dest.reg = CFI_SP;
  214. op->dest.offset = insn.displacement.value;
  215. }
  216. break;
  217. case 0x8b:
  218. if (rex_w && !rex_b && modrm_mod == 1 && modrm_rm == 5) {
  219. /* mov disp(%rbp), reg */
  220. *type = INSN_STACK;
  221. op->src.type = OP_SRC_REG_INDIRECT;
  222. op->src.reg = CFI_BP;
  223. op->src.offset = insn.displacement.value;
  224. op->dest.type = OP_DEST_REG;
  225. op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
  226. } else if (rex_w && !rex_b && sib == 0x24 &&
  227. modrm_mod != 3 && modrm_rm == 4) {
  228. /* mov disp(%rsp), reg */
  229. *type = INSN_STACK;
  230. op->src.type = OP_SRC_REG_INDIRECT;
  231. op->src.reg = CFI_SP;
  232. op->src.offset = insn.displacement.value;
  233. op->dest.type = OP_DEST_REG;
  234. op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
  235. }
  236. break;
  237. case 0x8d:
  238. if (sib == 0x24 && rex_w && !rex_b && !rex_x) {
  239. *type = INSN_STACK;
  240. if (!insn.displacement.value) {
  241. /* lea (%rsp), reg */
  242. op->src.type = OP_SRC_REG;
  243. } else {
  244. /* lea disp(%rsp), reg */
  245. op->src.type = OP_SRC_ADD;
  246. op->src.offset = insn.displacement.value;
  247. }
  248. op->src.reg = CFI_SP;
  249. op->dest.type = OP_DEST_REG;
  250. op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
  251. } else if (rex == 0x48 && modrm == 0x65) {
  252. /* lea disp(%rbp), %rsp */
  253. *type = INSN_STACK;
  254. op->src.type = OP_SRC_ADD;
  255. op->src.reg = CFI_BP;
  256. op->src.offset = insn.displacement.value;
  257. op->dest.type = OP_DEST_REG;
  258. op->dest.reg = CFI_SP;
  259. } else if (rex == 0x49 && modrm == 0x62 &&
  260. insn.displacement.value == -8) {
  261. /*
  262. * lea -0x8(%r10), %rsp
  263. *
  264. * Restoring rsp back to its original value after a
  265. * stack realignment.
  266. */
  267. *type = INSN_STACK;
  268. op->src.type = OP_SRC_ADD;
  269. op->src.reg = CFI_R10;
  270. op->src.offset = -8;
  271. op->dest.type = OP_DEST_REG;
  272. op->dest.reg = CFI_SP;
  273. } else if (rex == 0x49 && modrm == 0x65 &&
  274. insn.displacement.value == -16) {
  275. /*
  276. * lea -0x10(%r13), %rsp
  277. *
  278. * Restoring rsp back to its original value after a
  279. * stack realignment.
  280. */
  281. *type = INSN_STACK;
  282. op->src.type = OP_SRC_ADD;
  283. op->src.reg = CFI_R13;
  284. op->src.offset = -16;
  285. op->dest.type = OP_DEST_REG;
  286. op->dest.reg = CFI_SP;
  287. }
  288. break;
  289. case 0x8f:
  290. /* pop to mem */
  291. *type = INSN_STACK;
  292. op->src.type = OP_SRC_POP;
  293. op->dest.type = OP_DEST_MEM;
  294. break;
  295. case 0x90:
  296. *type = INSN_NOP;
  297. break;
  298. case 0x9c:
  299. /* pushf */
  300. *type = INSN_STACK;
  301. op->src.type = OP_SRC_CONST;
  302. op->dest.type = OP_DEST_PUSH;
  303. break;
  304. case 0x9d:
  305. /* popf */
  306. *type = INSN_STACK;
  307. op->src.type = OP_SRC_POP;
  308. op->dest.type = OP_DEST_MEM;
  309. break;
  310. case 0x0f:
  311. if (op2 >= 0x80 && op2 <= 0x8f) {
  312. *type = INSN_JUMP_CONDITIONAL;
  313. } else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 ||
  314. op2 == 0x35) {
  315. /* sysenter, sysret */
  316. *type = INSN_CONTEXT_SWITCH;
  317. } else if (op2 == 0x0b || op2 == 0xb9) {
  318. /* ud2 */
  319. *type = INSN_BUG;
  320. } else if (op2 == 0x0d || op2 == 0x1f) {
  321. /* nopl/nopw */
  322. *type = INSN_NOP;
  323. } else if (op2 == 0xa0 || op2 == 0xa8) {
  324. /* push fs/gs */
  325. *type = INSN_STACK;
  326. op->src.type = OP_SRC_CONST;
  327. op->dest.type = OP_DEST_PUSH;
  328. } else if (op2 == 0xa1 || op2 == 0xa9) {
  329. /* pop fs/gs */
  330. *type = INSN_STACK;
  331. op->src.type = OP_SRC_POP;
  332. op->dest.type = OP_DEST_MEM;
  333. }
  334. break;
  335. case 0xc9:
  336. /*
  337. * leave
  338. *
  339. * equivalent to:
  340. * mov bp, sp
  341. * pop bp
  342. */
  343. *type = INSN_STACK;
  344. op->dest.type = OP_DEST_LEAVE;
  345. break;
  346. case 0xe3:
  347. /* jecxz/jrcxz */
  348. *type = INSN_JUMP_CONDITIONAL;
  349. break;
  350. case 0xe9:
  351. case 0xeb:
  352. *type = INSN_JUMP_UNCONDITIONAL;
  353. break;
  354. case 0xc2:
  355. case 0xc3:
  356. *type = INSN_RETURN;
  357. break;
  358. case 0xca: /* retf */
  359. case 0xcb: /* retf */
  360. case 0xcf: /* iret */
  361. *type = INSN_CONTEXT_SWITCH;
  362. break;
  363. case 0xe8:
  364. *type = INSN_CALL;
  365. break;
  366. case 0xff:
  367. if (modrm_reg == 2 || modrm_reg == 3)
  368. *type = INSN_CALL_DYNAMIC;
  369. else if (modrm_reg == 4)
  370. *type = INSN_JUMP_DYNAMIC;
  371. else if (modrm_reg == 5)
  372. /* jmpf */
  373. *type = INSN_CONTEXT_SWITCH;
  374. else if (modrm_reg == 6) {
  375. /* push from mem */
  376. *type = INSN_STACK;
  377. op->src.type = OP_SRC_CONST;
  378. op->dest.type = OP_DEST_PUSH;
  379. }
  380. break;
  381. default:
  382. break;
  383. }
  384. *immediate = insn.immediate.nbytes ? insn.immediate.value : 0;
  385. return 0;
  386. }
  387. void arch_initial_func_cfi_state(struct cfi_state *state)
  388. {
  389. int i;
  390. for (i = 0; i < CFI_NUM_REGS; i++) {
  391. state->regs[i].base = CFI_UNDEFINED;
  392. state->regs[i].offset = 0;
  393. }
  394. /* initial CFA (call frame address) */
  395. state->cfa.base = CFI_SP;
  396. state->cfa.offset = 8;
  397. /* initial RA (return address) */
  398. state->regs[16].base = CFI_CFA;
  399. state->regs[16].offset = -8;
  400. }