Commit 36d92e43 authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Ingo Molnar

objtool,x86: More ModRM sugar

Better helpers to decode ModRM.
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Link: https://lkml.kernel.org/r/YCZB/ljatFXqQbm8@hirez.programming.kicks-ass.net
parent 961d83b9
...@@ -82,15 +82,21 @@ unsigned long arch_jump_destination(struct instruction *insn) ...@@ -82,15 +82,21 @@ unsigned long arch_jump_destination(struct instruction *insn)
* 01 | [r/m + d8] |[S+d]| [r/m + d8] | * 01 | [r/m + d8] |[S+d]| [r/m + d8] |
* 10 | [r/m + d32] |[S+D]| [r/m + d32] | * 10 | [r/m + d32] |[S+D]| [r/m + d32] |
* 11 | r/ m | * 11 | r/ m |
*
*/ */
#define mod_is_mem() (modrm_mod != 3)
#define mod_is_reg() (modrm_mod == 3)
#define is_RIP() ((modrm_rm & 7) == CFI_BP && modrm_mod == 0) #define is_RIP() ((modrm_rm & 7) == CFI_BP && modrm_mod == 0)
#define have_SIB() ((modrm_rm & 7) == CFI_SP && modrm_mod != 3) #define have_SIB() ((modrm_rm & 7) == CFI_SP && mod_is_mem())
#define rm_is(reg) (have_SIB() ? \ #define rm_is(reg) (have_SIB() ? \
sib_base == (reg) && sib_index == CFI_SP : \ sib_base == (reg) && sib_index == CFI_SP : \
modrm_rm == (reg)) modrm_rm == (reg))
#define rm_is_mem(reg) (mod_is_mem() && !is_RIP() && rm_is(reg))
#define rm_is_reg(reg) (mod_is_reg() && modrm_rm == (reg))
int arch_decode_instruction(const struct elf *elf, const struct section *sec, int arch_decode_instruction(const struct elf *elf, const struct section *sec,
unsigned long offset, unsigned int maxlen, unsigned long offset, unsigned int maxlen,
unsigned int *len, enum insn_type *type, unsigned int *len, enum insn_type *type,
...@@ -154,7 +160,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -154,7 +160,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
case 0x1: case 0x1:
case 0x29: case 0x29:
if (rex_w && modrm_mod == 3 && modrm_rm == CFI_SP) { if (rex_w && rm_is_reg(CFI_SP)) {
/* add/sub reg, %rsp */ /* add/sub reg, %rsp */
ADD_OP(op) { ADD_OP(op) {
...@@ -219,7 +225,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -219,7 +225,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
break; break;
/* %rsp target only */ /* %rsp target only */
if (!(modrm_mod == 3 && modrm_rm == CFI_SP)) if (!rm_is_reg(CFI_SP))
break; break;
imm = insn.immediate.value; imm = insn.immediate.value;
...@@ -272,7 +278,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -272,7 +278,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
if (modrm_reg == CFI_SP) { if (modrm_reg == CFI_SP) {
if (modrm_mod == 3) { if (mod_is_reg()) {
/* mov %rsp, reg */ /* mov %rsp, reg */
ADD_OP(op) { ADD_OP(op) {
op->src.type = OP_SRC_REG; op->src.type = OP_SRC_REG;
...@@ -308,7 +314,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -308,7 +314,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
break; break;
} }
if (modrm_mod == 3 && modrm_rm == CFI_SP) { if (rm_is_reg(CFI_SP)) {
/* mov reg, %rsp */ /* mov reg, %rsp */
ADD_OP(op) { ADD_OP(op) {
...@@ -325,7 +331,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -325,7 +331,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
if (!rex_w) if (!rex_w)
break; break;
if ((modrm_mod == 1 || modrm_mod == 2) && modrm_rm == CFI_BP) { if (rm_is_mem(CFI_BP)) {
/* mov reg, disp(%rbp) */ /* mov reg, disp(%rbp) */
ADD_OP(op) { ADD_OP(op) {
...@@ -338,7 +344,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -338,7 +344,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
break; break;
} }
if (modrm_mod != 3 && rm_is(CFI_SP)) { if (rm_is_mem(CFI_SP)) {
/* mov reg, disp(%rsp) */ /* mov reg, disp(%rsp) */
ADD_OP(op) { ADD_OP(op) {
...@@ -357,7 +363,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -357,7 +363,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
if (!rex_w) if (!rex_w)
break; break;
if ((modrm_mod == 1 || modrm_mod == 2) && modrm_rm == CFI_BP) { if (rm_is_mem(CFI_BP)) {
/* mov disp(%rbp), reg */ /* mov disp(%rbp), reg */
ADD_OP(op) { ADD_OP(op) {
...@@ -370,7 +376,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -370,7 +376,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
break; break;
} }
if (modrm_mod != 3 && rm_is(CFI_SP)) { if (rm_is_mem(CFI_SP)) {
/* mov disp(%rsp), reg */ /* mov disp(%rsp), reg */
ADD_OP(op) { ADD_OP(op) {
...@@ -386,7 +392,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec, ...@@ -386,7 +392,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
break; break;
case 0x8d: case 0x8d:
if (modrm_mod == 3) { if (mod_is_reg()) {
WARN("invalid LEA encoding at %s:0x%lx", sec->name, offset); WARN("invalid LEA encoding at %s:0x%lx", sec->name, offset);
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment