Skip to content

Commit f56dae8

Browse files
author
Peter Zijlstra
committed
objtool: Handle __sanitize_cov*() tail calls
Turns out the compilers also generate tail calls to __sanitize_cov*(), make sure to also patch those out in noinstr code. Fixes: 0f1441b ("objtool: Fix noinstr vs KCOV") Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Marco Elver <elver@google.com> Link: https://lore.kernel.org/r/20210624095147.818783799@infradead.org
1 parent 8b946cc commit f56dae8

3 files changed

Lines changed: 105 additions & 74 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,26 @@ const char *arch_nop_insn(int len)
659659
return nops[len-1];
660660
}
661661

662+
#define BYTE_RET 0xC3
663+
664+
const char *arch_ret_insn(int len)
665+
{
666+
static const char ret[5][5] = {
667+
{ BYTE_RET },
668+
{ BYTE_RET, BYTES_NOP1 },
669+
{ BYTE_RET, BYTES_NOP2 },
670+
{ BYTE_RET, BYTES_NOP3 },
671+
{ BYTE_RET, BYTES_NOP4 },
672+
};
673+
674+
if (len < 1 || len > 5) {
675+
WARN("invalid RET size: %d\n", len);
676+
return NULL;
677+
}
678+
679+
return ret[len-1];
680+
}
681+
662682
/* asm/alternative.h ? */
663683

664684
#define ALTINSTR_FLAG_INV (1 << 15)

‎tools/objtool/check.c‎

Lines changed: 84 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,79 @@ static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *i
904904
return insn->reloc;
905905
}
906906

907+
static void remove_insn_ops(struct instruction *insn)
908+
{
909+
struct stack_op *op, *tmp;
910+
911+
list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
912+
list_del(&op->list);
913+
free(op);
914+
}
915+
}
916+
917+
static void add_call_dest(struct objtool_file *file, struct instruction *insn,
918+
struct symbol *dest, bool sibling)
919+
{
920+
struct reloc *reloc = insn_reloc(file, insn);
921+
922+
insn->call_dest = dest;
923+
if (!dest)
924+
return;
925+
926+
if (insn->call_dest->static_call_tramp) {
927+
list_add_tail(&insn->call_node,
928+
&file->static_call_list);
929+
}
930+
931+
/*
932+
* Many compilers cannot disable KCOV with a function attribute
933+
* so they need a little help, NOP out any KCOV calls from noinstr
934+
* text.
935+
*/
936+
if (insn->sec->noinstr &&
937+
!strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
938+
if (reloc) {
939+
reloc->type = R_NONE;
940+
elf_write_reloc(file->elf, reloc);
941+
}
942+
943+
elf_write_insn(file->elf, insn->sec,
944+
insn->offset, insn->len,
945+
sibling ? arch_ret_insn(insn->len)
946+
: arch_nop_insn(insn->len));
947+
948+
insn->type = sibling ? INSN_RETURN : INSN_NOP;
949+
}
950+
951+
if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
952+
if (sibling)
953+
WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
954+
955+
if (reloc) {
956+
reloc->type = R_NONE;
957+
elf_write_reloc(file->elf, reloc);
958+
}
959+
960+
elf_write_insn(file->elf, insn->sec,
961+
insn->offset, insn->len,
962+
arch_nop_insn(insn->len));
963+
964+
insn->type = INSN_NOP;
965+
966+
list_add_tail(&insn->mcount_loc_node,
967+
&file->mcount_loc_list);
968+
}
969+
970+
/*
971+
* Whatever stack impact regular CALLs have, should be undone
972+
* by the RETURN of the called function.
973+
*
974+
* Annotated intra-function calls retain the stack_ops but
975+
* are converted to JUMP, see read_intra_function_calls().
976+
*/
977+
remove_insn_ops(insn);
978+
}
979+
907980
/*
908981
* Find the destination instructions for all jumps.
909982
*/
@@ -942,11 +1015,7 @@ static int add_jump_destinations(struct objtool_file *file)
9421015
continue;
9431016
} else if (insn->func) {
9441017
/* internal or external sibling call (with reloc) */
945-
insn->call_dest = reloc->sym;
946-
if (insn->call_dest->static_call_tramp) {
947-
list_add_tail(&insn->call_node,
948-
&file->static_call_list);
949-
}
1018+
add_call_dest(file, insn, reloc->sym, true);
9501019
continue;
9511020
} else if (reloc->sym->sec->idx) {
9521021
dest_sec = reloc->sym->sec;
@@ -1002,30 +1071,15 @@ static int add_jump_destinations(struct objtool_file *file)
10021071

10031072
} else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
10041073
insn->jump_dest->offset == insn->jump_dest->func->offset) {
1005-
10061074
/* internal sibling call (without reloc) */
1007-
insn->call_dest = insn->jump_dest->func;
1008-
if (insn->call_dest->static_call_tramp) {
1009-
list_add_tail(&insn->call_node,
1010-
&file->static_call_list);
1011-
}
1075+
add_call_dest(file, insn, insn->jump_dest->func, true);
10121076
}
10131077
}
10141078
}
10151079

10161080
return 0;
10171081
}
10181082

1019-
static void remove_insn_ops(struct instruction *insn)
1020-
{
1021-
struct stack_op *op, *tmp;
1022-
1023-
list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
1024-
list_del(&op->list);
1025-
free(op);
1026-
}
1027-
}
1028-
10291083
static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
10301084
{
10311085
struct symbol *call_dest;
@@ -1044,6 +1098,7 @@ static int add_call_destinations(struct objtool_file *file)
10441098
{
10451099
struct instruction *insn;
10461100
unsigned long dest_off;
1101+
struct symbol *dest;
10471102
struct reloc *reloc;
10481103

10491104
for_each_insn(file, insn) {
@@ -1053,7 +1108,9 @@ static int add_call_destinations(struct objtool_file *file)
10531108
reloc = insn_reloc(file, insn);
10541109
if (!reloc) {
10551110
dest_off = arch_jump_destination(insn);
1056-
insn->call_dest = find_call_destination(insn->sec, dest_off);
1111+
dest = find_call_destination(insn->sec, dest_off);
1112+
1113+
add_call_dest(file, insn, dest, false);
10571114

10581115
if (insn->ignore)
10591116
continue;
@@ -1071,16 +1128,17 @@ static int add_call_destinations(struct objtool_file *file)
10711128

10721129
} else if (reloc->sym->type == STT_SECTION) {
10731130
dest_off = arch_dest_reloc_offset(reloc->addend);
1074-
insn->call_dest = find_call_destination(reloc->sym->sec,
1075-
dest_off);
1076-
if (!insn->call_dest) {
1131+
dest = find_call_destination(reloc->sym->sec, dest_off);
1132+
if (!dest) {
10771133
WARN_FUNC("can't find call dest symbol at %s+0x%lx",
10781134
insn->sec, insn->offset,
10791135
reloc->sym->sec->name,
10801136
dest_off);
10811137
return -1;
10821138
}
10831139

1140+
add_call_dest(file, insn, dest, false);
1141+
10841142
} else if (arch_is_retpoline(reloc->sym)) {
10851143
/*
10861144
* Retpoline calls are really dynamic calls in
@@ -1096,55 +1154,7 @@ static int add_call_destinations(struct objtool_file *file)
10961154
continue;
10971155

10981156
} else
1099-
insn->call_dest = reloc->sym;
1100-
1101-
if (insn->call_dest && insn->call_dest->static_call_tramp) {
1102-
list_add_tail(&insn->call_node,
1103-
&file->static_call_list);
1104-
}
1105-
1106-
/*
1107-
* Many compilers cannot disable KCOV with a function attribute
1108-
* so they need a little help, NOP out any KCOV calls from noinstr
1109-
* text.
1110-
*/
1111-
if (insn->sec->noinstr &&
1112-
!strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
1113-
if (reloc) {
1114-
reloc->type = R_NONE;
1115-
elf_write_reloc(file->elf, reloc);
1116-
}
1117-
1118-
elf_write_insn(file->elf, insn->sec,
1119-
insn->offset, insn->len,
1120-
arch_nop_insn(insn->len));
1121-
insn->type = INSN_NOP;
1122-
}
1123-
1124-
if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
1125-
if (reloc) {
1126-
reloc->type = R_NONE;
1127-
elf_write_reloc(file->elf, reloc);
1128-
}
1129-
1130-
elf_write_insn(file->elf, insn->sec,
1131-
insn->offset, insn->len,
1132-
arch_nop_insn(insn->len));
1133-
1134-
insn->type = INSN_NOP;
1135-
1136-
list_add_tail(&insn->mcount_loc_node,
1137-
&file->mcount_loc_list);
1138-
}
1139-
1140-
/*
1141-
* Whatever stack impact regular CALLs have, should be undone
1142-
* by the RETURN of the called function.
1143-
*
1144-
* Annotated intra-function calls retain the stack_ops but
1145-
* are converted to JUMP, see read_intra_function_calls().
1146-
*/
1147-
remove_insn_ops(insn);
1157+
add_call_dest(file, insn, reloc->sym, false);
11481158
}
11491159

11501160
return 0;

‎tools/objtool/include/objtool/arch.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ unsigned long arch_jump_destination(struct instruction *insn);
8282
unsigned long arch_dest_reloc_offset(int addend);
8383

8484
const char *arch_nop_insn(int len);
85+
const char *arch_ret_insn(int len);
8586

8687
int arch_decode_hint_reg(u8 sp_reg, int *base);
8788

0 commit comments

Comments
 (0)