@@ -82,15 +82,21 @@ unsigned long arch_jump_destination(struct instruction *insn)
8282 * 01 | [r/m + d8] |[S+d]| [r/m + d8] |
8383 * 10 | [r/m + d32] |[S+D]| [r/m + d32] |
8484 * 11 | r/ m |
85- *
8685 */
86+
87+ #define mod_is_mem () (modrm_mod != 3)
88+ #define mod_is_reg () (modrm_mod == 3)
89+
8790#define is_RIP () ((modrm_rm & 7) == CFI_BP && modrm_mod == 0)
88- #define have_SIB () ((modrm_rm & 7) == CFI_SP && modrm_mod != 3 )
91+ #define have_SIB () ((modrm_rm & 7) == CFI_SP && mod_is_mem() )
8992
9093#define rm_is (reg ) (have_SIB() ? \
9194 sib_base == (reg) && sib_index == CFI_SP : \
9295 modrm_rm == (reg))
9396
97+ #define rm_is_mem (reg ) (mod_is_mem() && !is_RIP() && rm_is(reg))
98+ #define rm_is_reg (reg ) (mod_is_reg() && modrm_rm == (reg))
99+
94100int arch_decode_instruction (const struct elf * elf , const struct section * sec ,
95101 unsigned long offset , unsigned int maxlen ,
96102 unsigned int * len , enum insn_type * type ,
@@ -154,7 +160,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
154160
155161 case 0x1 :
156162 case 0x29 :
157- if (rex_w && modrm_mod == 3 && modrm_rm == CFI_SP ) {
163+ if (rex_w && rm_is_reg ( CFI_SP ) ) {
158164
159165 /* add/sub reg, %rsp */
160166 ADD_OP (op ) {
@@ -219,7 +225,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
219225 break ;
220226
221227 /* %rsp target only */
222- if (!( modrm_mod == 3 && modrm_rm == CFI_SP ))
228+ if (!rm_is_reg ( CFI_SP ))
223229 break ;
224230
225231 imm = insn .immediate .value ;
@@ -272,7 +278,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
272278
273279 if (modrm_reg == CFI_SP ) {
274280
275- if (modrm_mod == 3 ) {
281+ if (mod_is_reg () ) {
276282 /* mov %rsp, reg */
277283 ADD_OP (op ) {
278284 op -> src .type = OP_SRC_REG ;
@@ -308,7 +314,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
308314 break ;
309315 }
310316
311- if (modrm_mod == 3 && modrm_rm == CFI_SP ) {
317+ if (rm_is_reg ( CFI_SP ) ) {
312318
313319 /* mov reg, %rsp */
314320 ADD_OP (op ) {
@@ -325,7 +331,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
325331 if (!rex_w )
326332 break ;
327333
328- if (( modrm_mod == 1 || modrm_mod == 2 ) && modrm_rm == CFI_BP ) {
334+ if (rm_is_mem ( CFI_BP ) ) {
329335
330336 /* mov reg, disp(%rbp) */
331337 ADD_OP (op ) {
@@ -338,7 +344,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
338344 break ;
339345 }
340346
341- if (modrm_mod != 3 && rm_is (CFI_SP )) {
347+ if (rm_is_mem (CFI_SP )) {
342348
343349 /* mov reg, disp(%rsp) */
344350 ADD_OP (op ) {
@@ -357,7 +363,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
357363 if (!rex_w )
358364 break ;
359365
360- if (( modrm_mod == 1 || modrm_mod == 2 ) && modrm_rm == CFI_BP ) {
366+ if (rm_is_mem ( CFI_BP ) ) {
361367
362368 /* mov disp(%rbp), reg */
363369 ADD_OP (op ) {
@@ -370,7 +376,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
370376 break ;
371377 }
372378
373- if (modrm_mod != 3 && rm_is (CFI_SP )) {
379+ if (rm_is_mem (CFI_SP )) {
374380
375381 /* mov disp(%rsp), reg */
376382 ADD_OP (op ) {
@@ -386,7 +392,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
386392 break ;
387393
388394 case 0x8d :
389- if (modrm_mod == 3 ) {
395+ if (mod_is_reg () ) {
390396 WARN ("invalid LEA encoding at %s:0x%lx" , sec -> name , offset );
391397 break ;
392398 }
0 commit comments