Skip to content

Commit 0f1441b

Browse files
author
Peter Zijlstra
committed
objtool: Fix noinstr vs KCOV
Since many compilers cannot disable KCOV with a function attribute, help it to NOP out any __sanitizer_cov_*() calls injected in noinstr code. This turns: 12: e8 00 00 00 00 callq 17 <lockdep_hardirqs_on+0x17> 13: R_X86_64_PLT32 __sanitizer_cov_trace_pc-0x4 into: 12: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 13: R_X86_64_NONE __sanitizer_cov_trace_pc-0x4 Just like recordmcount does. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Dmitry Vyukov <[email protected]>
1 parent fdabdd0 commit 0f1441b

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ config X86
6767
select ARCH_HAS_FILTER_PGPROT
6868
select ARCH_HAS_FORTIFY_SOURCE
6969
select ARCH_HAS_GCOV_PROFILE_ALL
70-
select ARCH_HAS_KCOV if X86_64
70+
select ARCH_HAS_KCOV if X86_64 && STACK_VALIDATION
7171
select ARCH_HAS_MEM_ENCRYPT
7272
select ARCH_HAS_MEMBARRIER_SYNC_CORE
7373
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE

tools/objtool/arch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ unsigned long arch_jump_destination(struct instruction *insn);
8484

8585
unsigned long arch_dest_rela_offset(int addend);
8686

87+
const char *arch_nop_insn(int len);
88+
8789
#endif /* _ARCH_H */

tools/objtool/arch/x86/decode.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,21 @@ void arch_initial_func_cfi_state(struct cfi_init_state *state)
565565
state->regs[16].base = CFI_CFA;
566566
state->regs[16].offset = -8;
567567
}
568+
569+
const char *arch_nop_insn(int len)
570+
{
571+
static const char nops[5][5] = {
572+
/* 1 */ { 0x90 },
573+
/* 2 */ { 0x66, 0x90 },
574+
/* 3 */ { 0x0f, 0x1f, 0x00 },
575+
/* 4 */ { 0x0f, 0x1f, 0x40, 0x00 },
576+
/* 5 */ { 0x0f, 0x1f, 0x44, 0x00, 0x00 },
577+
};
578+
579+
if (len < 1 || len > 5) {
580+
WARN("invalid NOP size: %d\n", len);
581+
return NULL;
582+
}
583+
584+
return nops[len-1];
585+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _OBJTOOL_ARCH_ELF
2+
#define _OBJTOOL_ARCH_ELF
3+
4+
#define R_NONE R_X86_64_NONE
5+
6+
#endif /* _OBJTOOL_ARCH_ELF */

tools/objtool/check.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "check.h"
1313
#include "special.h"
1414
#include "warn.h"
15+
#include "arch_elf.h"
1516

1617
#include <linux/hashtable.h>
1718
#include <linux/kernel.h>
@@ -765,6 +766,24 @@ static int add_call_destinations(struct objtool_file *file)
765766
} else
766767
insn->call_dest = rela->sym;
767768

769+
/*
770+
* Many compilers cannot disable KCOV with a function attribute
771+
* so they need a little help, NOP out any KCOV calls from noinstr
772+
* text.
773+
*/
774+
if (insn->sec->noinstr &&
775+
!strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
776+
if (rela) {
777+
rela->type = R_NONE;
778+
elf_write_rela(file->elf, rela);
779+
}
780+
781+
elf_write_insn(file->elf, insn->sec,
782+
insn->offset, insn->len,
783+
arch_nop_insn(insn->len));
784+
insn->type = INSN_NOP;
785+
}
786+
768787
/*
769788
* Whatever stack impact regular CALLs have, should be undone
770789
* by the RETURN of the called function.

0 commit comments

Comments
 (0)