Skip to content

Commit aafeb14

Browse files
author
Peter Zijlstra
committed
objtool: Support stack-swizzle
Natively support the stack swizzle pattern: mov %rsp, (%[tos]) mov %[tos], %rsp ... pop %rsp It uses the vals[] array to link the first two stack-ops, and detect the SP to SP_INDIRECT swizzle. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com>
1 parent 2a51282 commit aafeb14

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

‎tools/objtool/check.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,38 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
19451945
cfa->offset = -cfi->vals[op->src.reg].offset;
19461946
cfi->stack_size = cfa->offset;
19471947

1948+
} else if (cfa->base == CFI_SP &&
1949+
cfi->vals[op->src.reg].base == CFI_SP_INDIRECT &&
1950+
cfi->vals[op->src.reg].offset == cfa->offset) {
1951+
1952+
/*
1953+
* Stack swizzle:
1954+
*
1955+
* 1: mov %rsp, (%[tos])
1956+
* 2: mov %[tos], %rsp
1957+
* ...
1958+
* 3: pop %rsp
1959+
*
1960+
* Where:
1961+
*
1962+
* 1 - places a pointer to the previous
1963+
* stack at the Top-of-Stack of the
1964+
* new stack.
1965+
*
1966+
* 2 - switches to the new stack.
1967+
*
1968+
* 3 - pops the Top-of-Stack to restore
1969+
* the original stack.
1970+
*
1971+
* Note: we set base to SP_INDIRECT
1972+
* here and preserve offset. Therefore
1973+
* when the unwinder reaches ToS it
1974+
* will dereference SP and then add the
1975+
* offset to find the next frame, IOW:
1976+
* (%rsp) + offset.
1977+
*/
1978+
cfa->base = CFI_SP_INDIRECT;
1979+
19481980
} else {
19491981
cfa->base = CFI_UNDEFINED;
19501982
cfa->offset = 0;
@@ -2047,6 +2079,13 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
20472079

20482080
case OP_SRC_POP:
20492081
case OP_SRC_POPF:
2082+
if (op->dest.reg == CFI_SP && cfa->base == CFI_SP_INDIRECT) {
2083+
2084+
/* pop %rsp; # restore from a stack swizzle */
2085+
cfa->base = CFI_SP;
2086+
break;
2087+
}
2088+
20502089
if (!cfi->drap && op->dest.reg == cfa->base) {
20512090

20522091
/* pop %rbp */
@@ -2193,6 +2232,12 @@ static int update_cfi_state(struct instruction *insn, struct cfi_state *cfi,
21932232
/* mov reg, disp(%rsp) */
21942233
save_reg(cfi, op->src.reg, CFI_CFA,
21952234
op->dest.offset - cfi->stack_size);
2235+
2236+
} else if (op->src.reg == CFI_SP && op->dest.offset == 0) {
2237+
2238+
/* mov %rsp, (%reg); # setup a stack swizzle. */
2239+
cfi->vals[op->dest.reg].base = CFI_SP_INDIRECT;
2240+
cfi->vals[op->dest.reg].offset = cfa->offset;
21962241
}
21972242

21982243
break;

0 commit comments

Comments
 (0)