Skip to content

Commit 65ea47d

Browse files
Julien Thierryingomolnar
authored andcommitted
objtool: Support multiple stack_op per instruction
Instruction sets can include more or less complex operations which might not fit the currently defined set of stack_ops. Combining more than one stack_op provides more flexibility to describe the behaviour of an instruction. This also reduces the need to define new stack_ops specific to a single instruction set. Allow instruction decoders to generate multiple stack_op per instruction. Signed-off-by: Julien Thierry <jthierry@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Alexandre Chartre <alexandre.chartre@oracle.com> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Link: https://lkml.kernel.org/r/20200327152847.15294-11-jthierry@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 1ee4447 commit 65ea47d

4 files changed

Lines changed: 62 additions & 31 deletions

File tree

‎tools/objtool/arch.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct op_src {
6464
struct stack_op {
6565
struct op_dest dest;
6666
struct op_src src;
67+
struct list_head list;
6768
};
6869

6970
struct instruction;
@@ -73,7 +74,8 @@ void arch_initial_func_cfi_state(struct cfi_state *state);
7374
int arch_decode_instruction(struct elf *elf, struct section *sec,
7475
unsigned long offset, unsigned int maxlen,
7576
unsigned int *len, enum insn_type *type,
76-
unsigned long *immediate, struct stack_op *op);
77+
unsigned long *immediate,
78+
struct list_head *ops_list);
7779

7880
bool arch_callee_saved_reg(unsigned char reg);
7981

‎tools/objtool/arch/x86/decode.c‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ unsigned long arch_jump_destination(struct instruction *insn)
8080
int arch_decode_instruction(struct elf *elf, struct section *sec,
8181
unsigned long offset, unsigned int maxlen,
8282
unsigned int *len, enum insn_type *type,
83-
unsigned long *immediate, struct stack_op *op)
83+
unsigned long *immediate,
84+
struct list_head *ops_list)
8485
{
8586
struct insn insn;
8687
int x86_64, sign;
8788
unsigned char op1, op2, rex = 0, rex_b = 0, rex_r = 0, rex_w = 0,
8889
rex_x = 0, modrm = 0, modrm_mod = 0, modrm_rm = 0,
8990
modrm_reg = 0, sib = 0;
91+
struct stack_op *op;
9092

9193
x86_64 = is_x86_64(elf);
9294
if (x86_64 == -1)
@@ -127,6 +129,10 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
127129
if (insn.sib.nbytes)
128130
sib = insn.sib.bytes[0];
129131

132+
op = calloc(1, sizeof(*op));
133+
if (!op)
134+
return -1;
135+
130136
switch (op1) {
131137

132138
case 0x1:
@@ -488,6 +494,11 @@ int arch_decode_instruction(struct elf *elf, struct section *sec,
488494

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

497+
if (*type == INSN_STACK)
498+
list_add_tail(&op->list, ops_list);
499+
else
500+
free(op);
501+
491502
return 0;
492503
}
493504

‎tools/objtool/check.c‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ static int decode_instructions(struct objtool_file *file)
260260
}
261261
memset(insn, 0, sizeof(*insn));
262262
INIT_LIST_HEAD(&insn->alts);
263+
INIT_LIST_HEAD(&insn->stack_ops);
263264
clear_insn_state(&insn->state);
264265

265266
insn->sec = sec;
@@ -269,7 +270,7 @@ static int decode_instructions(struct objtool_file *file)
269270
sec->len - offset,
270271
&insn->len, &insn->type,
271272
&insn->immediate,
272-
&insn->stack_op);
273+
&insn->stack_ops);
273274
if (ret)
274275
goto err;
275276

@@ -754,6 +755,7 @@ static int handle_group_alt(struct objtool_file *file,
754755
}
755756
memset(fake_jump, 0, sizeof(*fake_jump));
756757
INIT_LIST_HEAD(&fake_jump->alts);
758+
INIT_LIST_HEAD(&fake_jump->stack_ops);
757759
clear_insn_state(&fake_jump->state);
758760

759761
fake_jump->sec = special_alt->new_sec;
@@ -1452,10 +1454,11 @@ static bool has_valid_stack_frame(struct insn_state *state)
14521454
return false;
14531455
}
14541456

1455-
static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1457+
static int update_insn_state_regs(struct instruction *insn,
1458+
struct insn_state *state,
1459+
struct stack_op *op)
14561460
{
14571461
struct cfi_reg *cfa = &state->cfa;
1458-
struct stack_op *op = &insn->stack_op;
14591462

14601463
if (cfa->base != CFI_SP)
14611464
return 0;
@@ -1545,9 +1548,9 @@ static void restore_reg(struct insn_state *state, unsigned char reg)
15451548
* 41 5d pop %r13
15461549
* c3 retq
15471550
*/
1548-
static int update_insn_state(struct instruction *insn, struct insn_state *state)
1551+
static int update_insn_state(struct instruction *insn, struct insn_state *state,
1552+
struct stack_op *op)
15491553
{
1550-
struct stack_op *op = &insn->stack_op;
15511554
struct cfi_reg *cfa = &state->cfa;
15521555
struct cfi_reg *regs = state->regs;
15531556

@@ -1561,7 +1564,7 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
15611564
}
15621565

15631566
if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1564-
return update_insn_state_regs(insn, state);
1567+
return update_insn_state_regs(insn, state, op);
15651568

15661569
switch (op->dest.type) {
15671570

@@ -1898,6 +1901,42 @@ static int update_insn_state(struct instruction *insn, struct insn_state *state)
18981901
return 0;
18991902
}
19001903

1904+
static int handle_insn_ops(struct instruction *insn, struct insn_state *state)
1905+
{
1906+
struct stack_op *op;
1907+
1908+
list_for_each_entry(op, &insn->stack_ops, list) {
1909+
int res;
1910+
1911+
res = update_insn_state(insn, state, op);
1912+
if (res)
1913+
return res;
1914+
1915+
if (op->dest.type == OP_DEST_PUSHF) {
1916+
if (!state->uaccess_stack) {
1917+
state->uaccess_stack = 1;
1918+
} else if (state->uaccess_stack >> 31) {
1919+
WARN_FUNC("PUSHF stack exhausted",
1920+
insn->sec, insn->offset);
1921+
return 1;
1922+
}
1923+
state->uaccess_stack <<= 1;
1924+
state->uaccess_stack |= state->uaccess;
1925+
}
1926+
1927+
if (op->src.type == OP_SRC_POPF) {
1928+
if (state->uaccess_stack) {
1929+
state->uaccess = state->uaccess_stack & 1;
1930+
state->uaccess_stack >>= 1;
1931+
if (state->uaccess_stack == 1)
1932+
state->uaccess_stack = 0;
1933+
}
1934+
}
1935+
}
1936+
1937+
return 0;
1938+
}
1939+
19011940
static bool insn_state_match(struct instruction *insn, struct insn_state *state)
19021941
{
19031942
struct insn_state *state1 = &insn->state, *state2 = state;
@@ -2198,29 +2237,8 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
21982237
return 0;
21992238

22002239
case INSN_STACK:
2201-
if (update_insn_state(insn, &state))
2240+
if (handle_insn_ops(insn, &state))
22022241
return 1;
2203-
2204-
if (insn->stack_op.dest.type == OP_DEST_PUSHF) {
2205-
if (!state.uaccess_stack) {
2206-
state.uaccess_stack = 1;
2207-
} else if (state.uaccess_stack >> 31) {
2208-
WARN_FUNC("PUSHF stack exhausted", sec, insn->offset);
2209-
return 1;
2210-
}
2211-
state.uaccess_stack <<= 1;
2212-
state.uaccess_stack |= state.uaccess;
2213-
}
2214-
2215-
if (insn->stack_op.src.type == OP_SRC_POPF) {
2216-
if (state.uaccess_stack) {
2217-
state.uaccess = state.uaccess_stack & 1;
2218-
state.uaccess_stack >>= 1;
2219-
if (state.uaccess_stack == 1)
2220-
state.uaccess_stack = 0;
2221-
}
2222-
}
2223-
22242242
break;
22252243

22262244
case INSN_STAC:

‎tools/objtool/check.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct instruction {
4242
struct rela *jump_table;
4343
struct list_head alts;
4444
struct symbol *func;
45-
struct stack_op stack_op;
45+
struct list_head stack_ops;
4646
struct insn_state state;
4747
struct orc_entry orc;
4848
};

0 commit comments

Comments
 (0)