decode.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
  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 "insn/insn.h"
  21. #include "insn/inat.c"
  22. #include "insn/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. modrm = 0, modrm_mod = 0, modrm_rm = 0, modrm_reg = 0,
  82. 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_b = X86_REX_B(rex);
  103. }
  104. if (insn.modrm.nbytes) {
  105. modrm = insn.modrm.bytes[0];
  106. modrm_mod = X86_MODRM_MOD(modrm);
  107. modrm_reg = X86_MODRM_REG(modrm);
  108. modrm_rm = X86_MODRM_RM(modrm);
  109. }
  110. if (insn.sib.nbytes)
  111. sib = insn.sib.bytes[0];
  112. switch (op1) {
  113. case 0x1:
  114. case 0x29:
  115. if (rex_w && !rex_b && modrm_mod == 3 && modrm_rm == 4) {
  116. /* add/sub reg, %rsp */
  117. *type = INSN_STACK;
  118. op->src.type = OP_SRC_ADD;
  119. op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
  120. op->dest.type = OP_SRC_REG;
  121. op->dest.reg = CFI_SP;
  122. }
  123. break;
  124. case 0x50 ... 0x57:
  125. /* push reg */
  126. *type = INSN_STACK;
  127. op->src.type = OP_SRC_REG;
  128. op->src.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
  129. op->dest.type = OP_DEST_PUSH;
  130. break;
  131. case 0x58 ... 0x5f:
  132. /* pop reg */
  133. *type = INSN_STACK;
  134. op->src.type = OP_SRC_POP;
  135. op->dest.type = OP_DEST_REG;
  136. op->dest.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
  137. break;
  138. case 0x68:
  139. case 0x6a:
  140. /* push immediate */
  141. *type = INSN_STACK;
  142. op->src.type = OP_SRC_CONST;
  143. op->dest.type = OP_DEST_PUSH;
  144. break;
  145. case 0x70 ... 0x7f:
  146. *type = INSN_JUMP_CONDITIONAL;
  147. break;
  148. case 0x81:
  149. case 0x83:
  150. if (rex != 0x48)
  151. break;
  152. if (modrm == 0xe4) {
  153. /* and imm, %rsp */
  154. *type = INSN_STACK;
  155. op->src.type = OP_SRC_AND;
  156. op->src.reg = CFI_SP;
  157. op->src.offset = insn.immediate.value;
  158. op->dest.type = OP_DEST_REG;
  159. op->dest.reg = CFI_SP;
  160. break;
  161. }
  162. if (modrm == 0xc4)
  163. sign = 1;
  164. else if (modrm == 0xec)
  165. sign = -1;
  166. else
  167. break;
  168. /* add/sub imm, %rsp */
  169. *type = INSN_STACK;
  170. op->src.type = OP_SRC_ADD;
  171. op->src.reg = CFI_SP;
  172. op->src.offset = insn.immediate.value * sign;
  173. op->dest.type = OP_DEST_REG;
  174. op->dest.reg = CFI_SP;
  175. break;
  176. case 0x89:
  177. if (rex == 0x48 && modrm == 0xe5) {
  178. /* mov %rsp, %rbp */
  179. *type = INSN_STACK;
  180. op->src.type = OP_SRC_REG;
  181. op->src.reg = CFI_SP;
  182. op->dest.type = OP_DEST_REG;
  183. op->dest.reg = CFI_BP;
  184. break;
  185. }
  186. /* fallthrough */
  187. case 0x88:
  188. if (!rex_b &&
  189. (modrm_mod == 1 || modrm_mod == 2) && modrm_rm == 5) {
  190. /* mov reg, disp(%rbp) */
  191. *type = INSN_STACK;
  192. op->src.type = OP_SRC_REG;
  193. op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
  194. op->dest.type = OP_DEST_REG_INDIRECT;
  195. op->dest.reg = CFI_BP;
  196. op->dest.offset = insn.displacement.value;
  197. } else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
  198. /* mov reg, disp(%rsp) */
  199. *type = INSN_STACK;
  200. op->src.type = OP_SRC_REG;
  201. op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
  202. op->dest.type = OP_DEST_REG_INDIRECT;
  203. op->dest.reg = CFI_SP;
  204. op->dest.offset = insn.displacement.value;
  205. }
  206. break;
  207. case 0x8b:
  208. if (rex_w && !rex_b && modrm_mod == 1 && modrm_rm == 5) {
  209. /* mov disp(%rbp), reg */
  210. *type = INSN_STACK;
  211. op->src.type = OP_SRC_REG_INDIRECT;
  212. op->src.reg = CFI_BP;
  213. op->src.offset = insn.displacement.value;
  214. op->dest.type = OP_DEST_REG;
  215. op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
  216. } else if (rex_w && !rex_b && sib == 0x24 &&
  217. modrm_mod != 3 && modrm_rm == 4) {
  218. /* mov disp(%rsp), reg */
  219. *type = INSN_STACK;
  220. op->src.type = OP_SRC_REG_INDIRECT;
  221. op->src.reg = CFI_SP;
  222. op->src.offset = insn.displacement.value;
  223. op->dest.type = OP_DEST_REG;
  224. op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
  225. }
  226. break;
  227. case 0x8d:
  228. if (rex == 0x48 && modrm == 0x65) {
  229. /* lea -disp(%rbp), %rsp */
  230. *type = INSN_STACK;
  231. op->src.type = OP_SRC_ADD;
  232. op->src.reg = CFI_BP;
  233. op->src.offset = insn.displacement.value;
  234. op->dest.type = OP_DEST_REG;
  235. op->dest.reg = CFI_SP;
  236. break;
  237. }
  238. if (rex == 0x4c && modrm == 0x54 && sib == 0x24 &&
  239. insn.displacement.value == 8) {
  240. /*
  241. * lea 0x8(%rsp), %r10
  242. *
  243. * Here r10 is the "drap" pointer, used as a stack
  244. * pointer helper when the stack gets realigned.
  245. */
  246. *type = INSN_STACK;
  247. op->src.type = OP_SRC_ADD;
  248. op->src.reg = CFI_SP;
  249. op->src.offset = 8;
  250. op->dest.type = OP_DEST_REG;
  251. op->dest.reg = CFI_R10;
  252. break;
  253. }
  254. if (rex == 0x4c && modrm == 0x6c && sib == 0x24 &&
  255. insn.displacement.value == 16) {
  256. /*
  257. * lea 0x10(%rsp), %r13
  258. *
  259. * Here r13 is the "drap" pointer, used as a stack
  260. * pointer helper when the stack gets realigned.
  261. */
  262. *type = INSN_STACK;
  263. op->src.type = OP_SRC_ADD;
  264. op->src.reg = CFI_SP;
  265. op->src.offset = 16;
  266. op->dest.type = OP_DEST_REG;
  267. op->dest.reg = CFI_R13;
  268. break;
  269. }
  270. if (rex == 0x49 && modrm == 0x62 &&
  271. insn.displacement.value == -8) {
  272. /*
  273. * lea -0x8(%r10), %rsp
  274. *
  275. * Restoring rsp back to its original value after a
  276. * stack realignment.
  277. */
  278. *type = INSN_STACK;
  279. op->src.type = OP_SRC_ADD;
  280. op->src.reg = CFI_R10;
  281. op->src.offset = -8;
  282. op->dest.type = OP_DEST_REG;
  283. op->dest.reg = CFI_SP;
  284. break;
  285. }
  286. if (rex == 0x49 && modrm == 0x65 &&
  287. insn.displacement.value == -16) {
  288. /*
  289. * lea -0x10(%r13), %rsp
  290. *
  291. * Restoring rsp back to its original value after a
  292. * stack realignment.
  293. */
  294. *type = INSN_STACK;
  295. op->src.type = OP_SRC_ADD;
  296. op->src.reg = CFI_R13;
  297. op->src.offset = -16;
  298. op->dest.type = OP_DEST_REG;
  299. op->dest.reg = CFI_SP;
  300. break;
  301. }
  302. break;
  303. case 0x8f:
  304. /* pop to mem */
  305. *type = INSN_STACK;
  306. op->src.type = OP_SRC_POP;
  307. op->dest.type = OP_DEST_MEM;
  308. break;
  309. case 0x90:
  310. *type = INSN_NOP;
  311. break;
  312. case 0x9c:
  313. /* pushf */
  314. *type = INSN_STACK;
  315. op->src.type = OP_SRC_CONST;
  316. op->dest.type = OP_DEST_PUSH;
  317. break;
  318. case 0x9d:
  319. /* popf */
  320. *type = INSN_STACK;
  321. op->src.type = OP_SRC_POP;
  322. op->dest.type = OP_DEST_MEM;
  323. break;
  324. case 0x0f:
  325. if (op2 >= 0x80 && op2 <= 0x8f)
  326. *type = INSN_JUMP_CONDITIONAL;
  327. else if (op2 == 0x05 || op2 == 0x07 || op2 == 0x34 ||
  328. op2 == 0x35)
  329. /* sysenter, sysret */
  330. *type = INSN_CONTEXT_SWITCH;
  331. else if (op2 == 0x0d || op2 == 0x1f)
  332. /* nopl/nopw */
  333. *type = INSN_NOP;
  334. else if (op2 == 0xa0 || op2 == 0xa8) {
  335. /* push fs/gs */
  336. *type = INSN_STACK;
  337. op->src.type = OP_SRC_CONST;
  338. op->dest.type = OP_DEST_PUSH;
  339. } else if (op2 == 0xa1 || op2 == 0xa9) {
  340. /* pop fs/gs */
  341. *type = INSN_STACK;
  342. op->src.type = OP_SRC_POP;
  343. op->dest.type = OP_DEST_MEM;
  344. }
  345. break;
  346. case 0xc9:
  347. /*
  348. * leave
  349. *
  350. * equivalent to:
  351. * mov bp, sp
  352. * pop bp
  353. */
  354. *type = INSN_STACK;
  355. op->dest.type = OP_DEST_LEAVE;
  356. break;
  357. case 0xe3:
  358. /* jecxz/jrcxz */
  359. *type = INSN_JUMP_CONDITIONAL;
  360. break;
  361. case 0xe9:
  362. case 0xeb:
  363. *type = INSN_JUMP_UNCONDITIONAL;
  364. break;
  365. case 0xc2:
  366. case 0xc3:
  367. *type = INSN_RETURN;
  368. break;
  369. case 0xca: /* retf */
  370. case 0xcb: /* retf */
  371. case 0xcf: /* iret */
  372. *type = INSN_CONTEXT_SWITCH;
  373. break;
  374. case 0xe8:
  375. *type = INSN_CALL;
  376. break;
  377. case 0xff:
  378. if (modrm_reg == 2 || modrm_reg == 3)
  379. *type = INSN_CALL_DYNAMIC;
  380. else if (modrm_reg == 4)
  381. *type = INSN_JUMP_DYNAMIC;
  382. else if (modrm_reg == 5)
  383. /* jmpf */
  384. *type = INSN_CONTEXT_SWITCH;
  385. else if (modrm_reg == 6) {
  386. /* push from mem */
  387. *type = INSN_STACK;
  388. op->src.type = OP_SRC_CONST;
  389. op->dest.type = OP_DEST_PUSH;
  390. }
  391. break;
  392. default:
  393. break;
  394. }
  395. *immediate = insn.immediate.nbytes ? insn.immediate.value : 0;
  396. return 0;
  397. }
  398. void arch_initial_func_cfi_state(struct cfi_state *state)
  399. {
  400. int i;
  401. for (i = 0; i < CFI_NUM_REGS; i++) {
  402. state->regs[i].base = CFI_UNDEFINED;
  403. state->regs[i].offset = 0;
  404. }
  405. /* initial CFA (call frame address) */
  406. state->cfa.base = CFI_SP;
  407. state->cfa.offset = 8;
  408. /* initial RA (return address) */
  409. state->regs[16].base = CFI_CFA;
  410. state->regs[16].offset = -8;
  411. }