Commit 7d989fca authored by Peter Zijlstra's avatar Peter Zijlstra
Browse files

objtool: Rework allocating stack_ops on decode



Wrap each stack_op in a macro that allocates and adds it to the list.
This simplifies trying to figure out what to do with the pre-allocated
stack_op at the end.

Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: default avatarAlexandre Chartre <alexandre.chartre@oracle.com>
Reviewed-by: default avatarMiroslav Benes <mbenes@suse.cz>
Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Link: https://lkml.kernel.org/r/20200428191659.736151601@infradead.org
parent c721b3f8
Loading
Loading
Loading
Loading
+147 −104
Original line number Original line Diff line number Diff line
@@ -77,6 +77,11 @@ unsigned long arch_jump_destination(struct instruction *insn)
	return insn->offset + insn->len + insn->immediate;
	return insn->offset + insn->len + insn->immediate;
}
}


#define ADD_OP(op) \
	if (!(op = calloc(1, sizeof(*op)))) \
		return -1; \
	else for (list_add_tail(&op->list, ops_list); op; op = NULL)

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,
@@ -88,7 +93,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
	unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
	unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
		      rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
		      rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
		      modrm_reg = 0, sib = 0;
		      modrm_reg = 0, sib = 0;
	struct stack_op *op;
	struct stack_op *op = NULL;


	x86_64 = is_x86_64(elf);
	x86_64 = is_x86_64(elf);
	if (x86_64 == -1)
	if (x86_64 == -1)
@@ -129,10 +134,6 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
	if (insn.sib.nbytes)
	if (insn.sib.nbytes)
		sib = insn.sib.bytes[0];
		sib = insn.sib.bytes[0];


	op = calloc(1, sizeof(*op));
	if (!op)
		return -1;

	switch (op1) {
	switch (op1) {


	case 0x1:
	case 0x1:
@@ -141,20 +142,24 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


			/* add/sub reg, %rsp */
			/* add/sub reg, %rsp */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_ADD;
				op->src.type = OP_SRC_ADD;
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = CFI_SP;
				op->dest.reg = CFI_SP;
			}
			}
		}
		break;
		break;


	case 0x50 ... 0x57:
	case 0x50 ... 0x57:


		/* push reg */
		/* push reg */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op) {
			op->src.type = OP_SRC_REG;
			op->src.type = OP_SRC_REG;
			op->src.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
			op->src.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
			op->dest.type = OP_DEST_PUSH;
			op->dest.type = OP_DEST_PUSH;
		}


		break;
		break;


@@ -162,9 +167,11 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


		/* pop reg */
		/* pop reg */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op) {
			op->src.type = OP_SRC_POP;
			op->src.type = OP_SRC_POP;
			op->dest.type = OP_DEST_REG;
			op->dest.type = OP_DEST_REG;
			op->dest.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
			op->dest.reg = op_to_cfi_reg[op1 & 0x7][rex_b];
		}


		break;
		break;


@@ -172,8 +179,10 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
	case 0x6a:
	case 0x6a:
		/* push immediate */
		/* push immediate */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op) {
			op->src.type = OP_SRC_CONST;
			op->src.type = OP_SRC_CONST;
			op->dest.type = OP_DEST_PUSH;
			op->dest.type = OP_DEST_PUSH;
		}
		break;
		break;


	case 0x70 ... 0x7f:
	case 0x70 ... 0x7f:
@@ -188,11 +197,13 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
		if (modrm == 0xe4) {
		if (modrm == 0xe4) {
			/* and imm, %rsp */
			/* and imm, %rsp */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_AND;
				op->src.type = OP_SRC_AND;
				op->src.reg = CFI_SP;
				op->src.reg = CFI_SP;
				op->src.offset = insn.immediate.value;
				op->src.offset = insn.immediate.value;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = CFI_SP;
				op->dest.reg = CFI_SP;
			}
			break;
			break;
		}
		}


@@ -205,11 +216,13 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


		/* add/sub imm, %rsp */
		/* add/sub imm, %rsp */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op) {
			op->src.type = OP_SRC_ADD;
			op->src.type = OP_SRC_ADD;
			op->src.reg = CFI_SP;
			op->src.reg = CFI_SP;
			op->src.offset = insn.immediate.value * sign;
			op->src.offset = insn.immediate.value * sign;
			op->dest.type = OP_DEST_REG;
			op->dest.type = OP_DEST_REG;
			op->dest.reg = CFI_SP;
			op->dest.reg = CFI_SP;
		}
		break;
		break;


	case 0x89:
	case 0x89:
@@ -217,10 +230,12 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


			/* mov %rsp, reg */
			/* mov %rsp, reg */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_REG;
				op->src.type = OP_SRC_REG;
				op->src.reg = CFI_SP;
				op->src.reg = CFI_SP;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
				op->dest.reg = op_to_cfi_reg[modrm_rm][rex_b];
			}
			break;
			break;
		}
		}


@@ -228,10 +243,12 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


			/* mov reg, %rsp */
			/* mov reg, %rsp */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_REG;
				op->src.type = OP_SRC_REG;
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = CFI_SP;
				op->dest.reg = CFI_SP;
			}
			break;
			break;
		}
		}


@@ -242,22 +259,26 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


			/* mov reg, disp(%rbp) */
			/* mov reg, disp(%rbp) */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_REG;
				op->src.type = OP_SRC_REG;
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->dest.type = OP_DEST_REG_INDIRECT;
				op->dest.type = OP_DEST_REG_INDIRECT;
				op->dest.reg = CFI_BP;
				op->dest.reg = CFI_BP;
				op->dest.offset = insn.displacement.value;
				op->dest.offset = insn.displacement.value;
			}


		} else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {
		} else if (rex_w && !rex_b && modrm_rm == 4 && sib == 0x24) {


			/* mov reg, disp(%rsp) */
			/* mov reg, disp(%rsp) */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_REG;
				op->src.type = OP_SRC_REG;
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->src.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->dest.type = OP_DEST_REG_INDIRECT;
				op->dest.type = OP_DEST_REG_INDIRECT;
				op->dest.reg = CFI_SP;
				op->dest.reg = CFI_SP;
				op->dest.offset = insn.displacement.value;
				op->dest.offset = insn.displacement.value;
			}
			}
		}


		break;
		break;


@@ -266,23 +287,27 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


			/* mov disp(%rbp), reg */
			/* mov disp(%rbp), reg */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_REG_INDIRECT;
				op->src.type = OP_SRC_REG_INDIRECT;
				op->src.reg = CFI_BP;
				op->src.reg = CFI_BP;
				op->src.offset = insn.displacement.value;
				op->src.offset = insn.displacement.value;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
			}


		} else if (rex_w && !rex_b && sib == 0x24 &&
		} else if (rex_w && !rex_b && sib == 0x24 &&
			   modrm_mod != 3 && modrm_rm == 4) {
			   modrm_mod != 3 && modrm_rm == 4) {


			/* mov disp(%rsp), reg */
			/* mov disp(%rsp), reg */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_REG_INDIRECT;
				op->src.type = OP_SRC_REG_INDIRECT;
				op->src.reg = CFI_SP;
				op->src.reg = CFI_SP;
				op->src.offset = insn.displacement.value;
				op->src.offset = insn.displacement.value;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
			}
			}
		}


		break;
		break;


@@ -290,6 +315,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
		if (sib == 0x24 && rex_w && !rex_b && !rex_x) {
		if (sib == 0x24 && rex_w && !rex_b && !rex_x) {


			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				if (!insn.displacement.value) {
				if (!insn.displacement.value) {
					/* lea (%rsp), reg */
					/* lea (%rsp), reg */
					op->src.type = OP_SRC_REG;
					op->src.type = OP_SRC_REG;
@@ -301,16 +327,19 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
				op->src.reg = CFI_SP;
				op->src.reg = CFI_SP;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
				op->dest.reg = op_to_cfi_reg[modrm_reg][rex_r];
			}


		} else if (rex == 0x48 && modrm == 0x65) {
		} else if (rex == 0x48 && modrm == 0x65) {


			/* lea disp(%rbp), %rsp */
			/* lea disp(%rbp), %rsp */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_ADD;
				op->src.type = OP_SRC_ADD;
				op->src.reg = CFI_BP;
				op->src.reg = CFI_BP;
				op->src.offset = insn.displacement.value;
				op->src.offset = insn.displacement.value;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = CFI_SP;
				op->dest.reg = CFI_SP;
			}


		} else if (rex == 0x49 && modrm == 0x62 &&
		} else if (rex == 0x49 && modrm == 0x62 &&
			   insn.displacement.value == -8) {
			   insn.displacement.value == -8) {
@@ -322,11 +351,13 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
			 * stack realignment.
			 * stack realignment.
			 */
			 */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_ADD;
				op->src.type = OP_SRC_ADD;
				op->src.reg = CFI_R10;
				op->src.reg = CFI_R10;
				op->src.offset = -8;
				op->src.offset = -8;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = CFI_SP;
				op->dest.reg = CFI_SP;
			}


		} else if (rex == 0x49 && modrm == 0x65 &&
		} else if (rex == 0x49 && modrm == 0x65 &&
			   insn.displacement.value == -16) {
			   insn.displacement.value == -16) {
@@ -338,20 +369,24 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
			 * stack realignment.
			 * stack realignment.
			 */
			 */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_ADD;
				op->src.type = OP_SRC_ADD;
				op->src.reg = CFI_R13;
				op->src.reg = CFI_R13;
				op->src.offset = -16;
				op->src.offset = -16;
				op->dest.type = OP_DEST_REG;
				op->dest.type = OP_DEST_REG;
				op->dest.reg = CFI_SP;
				op->dest.reg = CFI_SP;
			}
			}
		}


		break;
		break;


	case 0x8f:
	case 0x8f:
		/* pop to mem */
		/* pop to mem */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op) {
			op->src.type = OP_SRC_POP;
			op->src.type = OP_SRC_POP;
			op->dest.type = OP_DEST_MEM;
			op->dest.type = OP_DEST_MEM;
		}
		break;
		break;


	case 0x90:
	case 0x90:
@@ -361,15 +396,19 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
	case 0x9c:
	case 0x9c:
		/* pushf */
		/* pushf */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op) {
			op->src.type = OP_SRC_CONST;
			op->src.type = OP_SRC_CONST;
			op->dest.type = OP_DEST_PUSHF;
			op->dest.type = OP_DEST_PUSHF;
		}
		break;
		break;


	case 0x9d:
	case 0x9d:
		/* popf */
		/* popf */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op) {
			op->src.type = OP_SRC_POPF;
			op->src.type = OP_SRC_POPF;
			op->dest.type = OP_DEST_MEM;
			op->dest.type = OP_DEST_MEM;
		}
		break;
		break;


	case 0x0f:
	case 0x0f:
@@ -405,16 +444,20 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


			/* push fs/gs */
			/* push fs/gs */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_CONST;
				op->src.type = OP_SRC_CONST;
				op->dest.type = OP_DEST_PUSH;
				op->dest.type = OP_DEST_PUSH;
			}


		} else if (op2 == 0xa1 || op2 == 0xa9) {
		} else if (op2 == 0xa1 || op2 == 0xa9) {


			/* pop fs/gs */
			/* pop fs/gs */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_POP;
				op->src.type = OP_SRC_POP;
				op->dest.type = OP_DEST_MEM;
				op->dest.type = OP_DEST_MEM;
			}
			}
		}


		break;
		break;


@@ -427,6 +470,7 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
		 * pop bp
		 * pop bp
		 */
		 */
		*type = INSN_STACK;
		*type = INSN_STACK;
		ADD_OP(op)
			op->dest.type = OP_DEST_LEAVE;
			op->dest.type = OP_DEST_LEAVE;


		break;
		break;
@@ -449,12 +493,14 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,
	case 0xcf: /* iret */
	case 0xcf: /* iret */
		*type = INSN_EXCEPTION_RETURN;
		*type = INSN_EXCEPTION_RETURN;


		ADD_OP(op) {
			/* add $40, %rsp */
			/* add $40, %rsp */
			op->src.type = OP_SRC_ADD;
			op->src.type = OP_SRC_ADD;
			op->src.reg = CFI_SP;
			op->src.reg = CFI_SP;
			op->src.offset = 5*8;
			op->src.offset = 5*8;
			op->dest.type = OP_DEST_REG;
			op->dest.type = OP_DEST_REG;
			op->dest.reg = CFI_SP;
			op->dest.reg = CFI_SP;
		}
		break;
		break;


	case 0xca: /* retf */
	case 0xca: /* retf */
@@ -492,9 +538,11 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


			/* push from mem */
			/* push from mem */
			*type = INSN_STACK;
			*type = INSN_STACK;
			ADD_OP(op) {
				op->src.type = OP_SRC_CONST;
				op->src.type = OP_SRC_CONST;
				op->dest.type = OP_DEST_PUSH;
				op->dest.type = OP_DEST_PUSH;
			}
			}
		}


		break;
		break;


@@ -504,11 +552,6 @@ int arch_decode_instruction(const struct elf *elf, const struct section *sec,


	*immediate = insn.immediate.nbytes ? insn.immediate.value : 0;
	*immediate = insn.immediate.nbytes ? insn.immediate.value : 0;


	if (*type == INSN_STACK || *type == INSN_EXCEPTION_RETURN)
		list_add_tail(&op->list, ops_list);
	else
		free(op);

	return 0;
	return 0;
}
}