Skip to content

Commit d9b17a0

Browse files
Peter Zijlstragregkh
authored andcommitted
objtool: Handle __sanitize_cov*() tail calls
[ Upstream commit f56dae8 ] 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 Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c8a2b96 commit d9b17a0

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
@@ -829,6 +829,79 @@ static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *i
829829
return insn->reloc;
830830
}
831831

832+
static void remove_insn_ops(struct instruction *insn)
833+
{
834+
struct stack_op *op, *tmp;
835+
836+
list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
837+
list_del(&op->list);
838+
free(op);
839+
}
840+
}
841+
842+
static void add_call_dest(struct objtool_file *file, struct instruction *insn,
843+
struct symbol *dest, bool sibling)
844+
{
845+
struct reloc *reloc = insn_reloc(file, insn);
846+
847+
insn->call_dest = dest;
848+
if (!dest)
849+
return;
850+
851+
if (insn->call_dest->static_call_tramp) {
852+
list_add_tail(&insn->call_node,
853+
&file->static_call_list);
854+
}
855+
856+
/*
857+
* Many compilers cannot disable KCOV with a function attribute
858+
* so they need a little help, NOP out any KCOV calls from noinstr
859+
* text.
860+
*/
861+
if (insn->sec->noinstr &&
862+
!strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
863+
if (reloc) {
864+
reloc->type = R_NONE;
865+
elf_write_reloc(file->elf, reloc);
866+
}
867+
868+
elf_write_insn(file->elf, insn->sec,
869+
insn->offset, insn->len,
870+
sibling ? arch_ret_insn(insn->len)
871+
: arch_nop_insn(insn->len));
872+
873+
insn->type = sibling ? INSN_RETURN : INSN_NOP;
874+
}
875+
876+
if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
877+
if (sibling)
878+
WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, insn->offset);
879+
880+
if (reloc) {
881+
reloc->type = R_NONE;
882+
elf_write_reloc(file->elf, reloc);
883+
}
884+
885+
elf_write_insn(file->elf, insn->sec,
886+
insn->offset, insn->len,
887+
arch_nop_insn(insn->len));
888+
889+
insn->type = INSN_NOP;
890+
891+
list_add_tail(&insn->mcount_loc_node,
892+
&file->mcount_loc_list);
893+
}
894+
895+
/*
896+
* Whatever stack impact regular CALLs have, should be undone
897+
* by the RETURN of the called function.
898+
*
899+
* Annotated intra-function calls retain the stack_ops but
900+
* are converted to JUMP, see read_intra_function_calls().
901+
*/
902+
remove_insn_ops(insn);
903+
}
904+
832905
/*
833906
* Find the destination instructions for all jumps.
834907
*/
@@ -867,11 +940,7 @@ static int add_jump_destinations(struct objtool_file *file)
867940
continue;
868941
} else if (insn->func) {
869942
/* internal or external sibling call (with reloc) */
870-
insn->call_dest = reloc->sym;
871-
if (insn->call_dest->static_call_tramp) {
872-
list_add_tail(&insn->call_node,
873-
&file->static_call_list);
874-
}
943+
add_call_dest(file, insn, reloc->sym, true);
875944
continue;
876945
} else if (reloc->sym->sec->idx) {
877946
dest_sec = reloc->sym->sec;
@@ -927,30 +996,15 @@ static int add_jump_destinations(struct objtool_file *file)
927996

928997
} else if (insn->jump_dest->func->pfunc != insn->func->pfunc &&
929998
insn->jump_dest->offset == insn->jump_dest->func->offset) {
930-
931999
/* internal sibling call (without reloc) */
932-
insn->call_dest = insn->jump_dest->func;
933-
if (insn->call_dest->static_call_tramp) {
934-
list_add_tail(&insn->call_node,
935-
&file->static_call_list);
936-
}
1000+
add_call_dest(file, insn, insn->jump_dest->func, true);
9371001
}
9381002
}
9391003
}
9401004

9411005
return 0;
9421006
}
9431007

944-
static void remove_insn_ops(struct instruction *insn)
945-
{
946-
struct stack_op *op, *tmp;
947-
948-
list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) {
949-
list_del(&op->list);
950-
free(op);
951-
}
952-
}
953-
9541008
static struct symbol *find_call_destination(struct section *sec, unsigned long offset)
9551009
{
9561010
struct symbol *call_dest;
@@ -969,6 +1023,7 @@ static int add_call_destinations(struct objtool_file *file)
9691023
{
9701024
struct instruction *insn;
9711025
unsigned long dest_off;
1026+
struct symbol *dest;
9721027
struct reloc *reloc;
9731028

9741029
for_each_insn(file, insn) {
@@ -978,7 +1033,9 @@ static int add_call_destinations(struct objtool_file *file)
9781033
reloc = insn_reloc(file, insn);
9791034
if (!reloc) {
9801035
dest_off = arch_jump_destination(insn);
981-
insn->call_dest = find_call_destination(insn->sec, dest_off);
1036+
dest = find_call_destination(insn->sec, dest_off);
1037+
1038+
add_call_dest(file, insn, dest, false);
9821039

9831040
if (insn->ignore)
9841041
continue;
@@ -996,16 +1053,17 @@ static int add_call_destinations(struct objtool_file *file)
9961053

9971054
} else if (reloc->sym->type == STT_SECTION) {
9981055
dest_off = arch_dest_reloc_offset(reloc->addend);
999-
insn->call_dest = find_call_destination(reloc->sym->sec,
1000-
dest_off);
1001-
if (!insn->call_dest) {
1056+
dest = find_call_destination(reloc->sym->sec, dest_off);
1057+
if (!dest) {
10021058
WARN_FUNC("can't find call dest symbol at %s+0x%lx",
10031059
insn->sec, insn->offset,
10041060
reloc->sym->sec->name,
10051061
dest_off);
10061062
return -1;
10071063
}
10081064

1065+
add_call_dest(file, insn, dest, false);
1066+
10091067
} else if (arch_is_retpoline(reloc->sym)) {
10101068
/*
10111069
* Retpoline calls are really dynamic calls in
@@ -1021,55 +1079,7 @@ static int add_call_destinations(struct objtool_file *file)
10211079
continue;
10221080

10231081
} else
1024-
insn->call_dest = reloc->sym;
1025-
1026-
if (insn->call_dest && insn->call_dest->static_call_tramp) {
1027-
list_add_tail(&insn->call_node,
1028-
&file->static_call_list);
1029-
}
1030-
1031-
/*
1032-
* Many compilers cannot disable KCOV with a function attribute
1033-
* so they need a little help, NOP out any KCOV calls from noinstr
1034-
* text.
1035-
*/
1036-
if (insn->sec->noinstr &&
1037-
!strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
1038-
if (reloc) {
1039-
reloc->type = R_NONE;
1040-
elf_write_reloc(file->elf, reloc);
1041-
}
1042-
1043-
elf_write_insn(file->elf, insn->sec,
1044-
insn->offset, insn->len,
1045-
arch_nop_insn(insn->len));
1046-
insn->type = INSN_NOP;
1047-
}
1048-
1049-
if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
1050-
if (reloc) {
1051-
reloc->type = R_NONE;
1052-
elf_write_reloc(file->elf, reloc);
1053-
}
1054-
1055-
elf_write_insn(file->elf, insn->sec,
1056-
insn->offset, insn->len,
1057-
arch_nop_insn(insn->len));
1058-
1059-
insn->type = INSN_NOP;
1060-
1061-
list_add_tail(&insn->mcount_loc_node,
1062-
&file->mcount_loc_list);
1063-
}
1064-
1065-
/*
1066-
* Whatever stack impact regular CALLs have, should be undone
1067-
* by the RETURN of the called function.
1068-
*
1069-
* Annotated intra-function calls retain the stack_ops but
1070-
* are converted to JUMP, see read_intra_function_calls().
1071-
*/
1072-
remove_insn_ops(insn);
1082+
add_call_dest(file, insn, reloc->sym, false);
10731083
}
10741084

10751085
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(struct instruction *insn, u8 sp_reg);
8788

0 commit comments

Comments
 (0)