linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] objtool: clang-related fixes
@ 2020-02-18  3:41 Josh Poimboeuf
  2020-02-18  3:41 ` [PATCH 1/2] objtool: Fix clang switch table edge case Josh Poimboeuf
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Josh Poimboeuf @ 2020-02-18  3:41 UTC (permalink / raw)
  To: x86; +Cc: linux-kernel, Peter Zijlstra, Nick Desaulniers

Fix a couple of issues which were discovered after clang CI was broken
by 644592d32837 ("objtool: Fail the kernel build on fatal errors").

Josh Poimboeuf (2):
  objtool: Fix clang switch table edge case
  objtool: Improve call destination function detection

 tools/objtool/check.c | 38 +++++++++++++++++++++++++++-----------
 tools/objtool/elf.c   | 14 ++++++++++++--
 tools/objtool/elf.h   |  1 +
 3 files changed, 40 insertions(+), 13 deletions(-)

-- 
2.21.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/2] objtool: Fix clang switch table edge case
  2020-02-18  3:41 [PATCH 0/2] objtool: clang-related fixes Josh Poimboeuf
@ 2020-02-18  3:41 ` Josh Poimboeuf
  2020-02-18 19:57   ` Nathan Chancellor
  2020-02-21  9:50   ` [tip: core/objtool] " tip-bot2 for Josh Poimboeuf
  2020-02-18  3:41 ` [PATCH 2/2] objtool: Improve call destination function detection Josh Poimboeuf
  2020-02-20 19:06 ` [PATCH 0/2] objtool: clang-related fixes Nick Desaulniers
  2 siblings, 2 replies; 10+ messages in thread
From: Josh Poimboeuf @ 2020-02-18  3:41 UTC (permalink / raw)
  To: x86; +Cc: linux-kernel, Peter Zijlstra, Nick Desaulniers

Clang has the ability to create a switch table which is not a jump
table, but is rather a table of string pointers.  This confuses objtool,
because it sees the relocations for the string pointers and assumes
they're part of a jump table:

  drivers/ata/sata_dwc_460ex.o: warning: objtool: sata_dwc_bmdma_start_by_tag()+0x3a2: can't find switch jump table
  net/ceph/messenger.o: warning: objtool: ceph_con_workfn()+0x47c: can't find switch jump table

Make objtool's find_jump_table() smart enough to distinguish between a
switch jump table (which has relocations to text addresses in the same
function as the original instruction) and other anonymous rodata (which
may have relocations to elsewhere).

Link: https://github.com/ClangBuiltLinux/linux/issues/485
Reported-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
 tools/objtool/check.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index b6da413bcbd6..e7227649bac7 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1019,7 +1019,7 @@ static struct rela *find_jump_table(struct objtool_file *file,
 				      struct instruction *insn)
 {
 	struct rela *text_rela, *table_rela;
-	struct instruction *orig_insn = insn;
+	struct instruction *dest_insn, *orig_insn = insn;
 	struct section *table_sec;
 	unsigned long table_offset;
 
@@ -1071,10 +1071,17 @@ static struct rela *find_jump_table(struct objtool_file *file,
 		    strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
 			continue;
 
-		/* Each table entry has a rela associated with it. */
+		/*
+		 * Each table entry has a rela associated with it.  The rela
+		 * should reference text in the same function as the original
+		 * instruction.
+		 */
 		table_rela = find_rela_by_dest(table_sec, table_offset);
 		if (!table_rela)
 			continue;
+		dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
+		if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
+			continue;
 
 		/*
 		 * Use of RIP-relative switch jumps is quite rare, and
-- 
2.21.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/2] objtool: Improve call destination function detection
  2020-02-18  3:41 [PATCH 0/2] objtool: clang-related fixes Josh Poimboeuf
  2020-02-18  3:41 ` [PATCH 1/2] objtool: Fix clang switch table edge case Josh Poimboeuf
@ 2020-02-18  3:41 ` Josh Poimboeuf
  2020-02-18 18:13   ` Nick Desaulniers
                     ` (2 more replies)
  2020-02-20 19:06 ` [PATCH 0/2] objtool: clang-related fixes Nick Desaulniers
  2 siblings, 3 replies; 10+ messages in thread
From: Josh Poimboeuf @ 2020-02-18  3:41 UTC (permalink / raw)
  To: x86; +Cc: linux-kernel, Peter Zijlstra, Nick Desaulniers

A recent clang change, combined with a binutils bug, can trigger a
situation where a ".Lprintk$local" STT_NOTYPE symbol gets created at the
same offset as the "printk" STT_FUNC symbol.  This confuses objtool:

  kernel/printk/printk.o: warning: objtool: ignore_loglevel_setup()+0x10: can't find call dest symbol at .text+0xc67

Improve the call destination detection by looking specifically for an
STT_FUNC symbol.

Link: https://github.com/ClangBuiltLinux/linux/issues/872
Link: https://sourceware.org/bugzilla/show_bug.cgi?id=25551
Reported-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
---
 tools/objtool/check.c | 27 ++++++++++++++++++---------
 tools/objtool/elf.c   | 14 ++++++++++++--
 tools/objtool/elf.h   |  1 +
 3 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index e7227649bac7..da0767128f61 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -415,8 +415,8 @@ static void add_ignores(struct objtool_file *file)
 			break;
 
 		case STT_SECTION:
-			func = find_symbol_by_offset(rela->sym->sec, rela->addend);
-			if (!func || func->type != STT_FUNC)
+			func = find_func_by_offset(rela->sym->sec, rela->addend);
+			if (!func)
 				continue;
 			break;
 
@@ -679,10 +679,14 @@ static int add_call_destinations(struct objtool_file *file)
 					       insn->len);
 		if (!rela) {
 			dest_off = insn->offset + insn->len + insn->immediate;
-			insn->call_dest = find_symbol_by_offset(insn->sec,
-								dest_off);
+			insn->call_dest = find_func_by_offset(insn->sec, dest_off);
+			if (!insn->call_dest)
+				insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
 
-			if (!insn->call_dest && !insn->ignore) {
+			if (insn->ignore)
+				continue;
+
+			if (!insn->call_dest) {
 				WARN_FUNC("unsupported intra-function call",
 					  insn->sec, insn->offset);
 				if (retpoline)
@@ -690,11 +694,16 @@ static int add_call_destinations(struct objtool_file *file)
 				return -1;
 			}
 
+			if (insn->func && insn->call_dest->type != STT_FUNC) {
+				WARN_FUNC("unsupported call to non-function",
+					  insn->sec, insn->offset);
+				return -1;
+			}
+
 		} else if (rela->sym->type == STT_SECTION) {
-			insn->call_dest = find_symbol_by_offset(rela->sym->sec,
-								rela->addend+4);
-			if (!insn->call_dest ||
-			    insn->call_dest->type != STT_FUNC) {
+			insn->call_dest = find_func_by_offset(rela->sym->sec,
+							      rela->addend+4);
+			if (!insn->call_dest) {
 				WARN_FUNC("can't find call dest symbol at %s+0x%x",
 					  insn->sec, insn->offset,
 					  rela->sym->sec->name,
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index edba4745f25a..cc4601c879ce 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -62,8 +62,18 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
 	struct symbol *sym;
 
 	list_for_each_entry(sym, &sec->symbol_list, list)
-		if (sym->type != STT_SECTION &&
-		    sym->offset == offset)
+		if (sym->type != STT_SECTION && sym->offset == offset)
+			return sym;
+
+	return NULL;
+}
+
+struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
+{
+	struct symbol *sym;
+
+	list_for_each_entry(sym, &sec->symbol_list, list)
+		if (sym->type == STT_FUNC && sym->offset == offset)
 			return sym;
 
 	return NULL;
diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
index 44150204db4d..a1963259b930 100644
--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -77,6 +77,7 @@ struct elf {
 
 struct elf *elf_read(const char *name, int flags);
 struct section *find_section_by_name(struct elf *elf, const char *name);
+struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
-- 
2.21.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/2] objtool: Improve call destination function detection
  2020-02-18  3:41 ` [PATCH 2/2] objtool: Improve call destination function detection Josh Poimboeuf
@ 2020-02-18 18:13   ` Nick Desaulniers
  2020-02-18 19:58   ` Nathan Chancellor
  2020-02-21  9:50   ` [tip: core/objtool] " tip-bot2 for Josh Poimboeuf
  2 siblings, 0 replies; 10+ messages in thread
From: Nick Desaulniers @ 2020-02-18 18:13 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT), LKML, Peter Zijlstra

On Mon, Feb 17, 2020 at 7:42 PM Josh Poimboeuf <jpoimboe@redhat.com> wrote:
>
> A recent clang change, combined with a binutils bug, can trigger a
> situation where a ".Lprintk$local" STT_NOTYPE symbol gets created at the
> same offset as the "printk" STT_FUNC symbol.  This confuses objtool:
>
>   kernel/printk/printk.o: warning: objtool: ignore_loglevel_setup()+0x10: can't find call dest symbol at .text+0xc67
>
> Improve the call destination detection by looking specifically for an
> STT_FUNC symbol.
>
> Link: https://github.com/ClangBuiltLinux/linux/issues/872
> Link: https://sourceware.org/bugzilla/show_bug.cgi?id=25551
> Reported-by: Nick Desaulniers <ndesaulniers@google.com>
> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>

Thanks Josh.
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Tested defconfig and allyesconfig builds with the series.

> ---
>  tools/objtool/check.c | 27 ++++++++++++++++++---------
>  tools/objtool/elf.c   | 14 ++++++++++++--
>  tools/objtool/elf.h   |  1 +
>  3 files changed, 31 insertions(+), 11 deletions(-)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index e7227649bac7..da0767128f61 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -415,8 +415,8 @@ static void add_ignores(struct objtool_file *file)
>                         break;
>
>                 case STT_SECTION:
> -                       func = find_symbol_by_offset(rela->sym->sec, rela->addend);
> -                       if (!func || func->type != STT_FUNC)
> +                       func = find_func_by_offset(rela->sym->sec, rela->addend);
> +                       if (!func)
>                                 continue;
>                         break;
>
> @@ -679,10 +679,14 @@ static int add_call_destinations(struct objtool_file *file)
>                                                insn->len);
>                 if (!rela) {
>                         dest_off = insn->offset + insn->len + insn->immediate;
> -                       insn->call_dest = find_symbol_by_offset(insn->sec,
> -                                                               dest_off);
> +                       insn->call_dest = find_func_by_offset(insn->sec, dest_off);
> +                       if (!insn->call_dest)
> +                               insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
>
> -                       if (!insn->call_dest && !insn->ignore) {
> +                       if (insn->ignore)
> +                               continue;
> +
> +                       if (!insn->call_dest) {
>                                 WARN_FUNC("unsupported intra-function call",
>                                           insn->sec, insn->offset);
>                                 if (retpoline)
> @@ -690,11 +694,16 @@ static int add_call_destinations(struct objtool_file *file)
>                                 return -1;
>                         }
>
> +                       if (insn->func && insn->call_dest->type != STT_FUNC) {
> +                               WARN_FUNC("unsupported call to non-function",
> +                                         insn->sec, insn->offset);
> +                               return -1;
> +                       }
> +
>                 } else if (rela->sym->type == STT_SECTION) {
> -                       insn->call_dest = find_symbol_by_offset(rela->sym->sec,
> -                                                               rela->addend+4);
> -                       if (!insn->call_dest ||
> -                           insn->call_dest->type != STT_FUNC) {
> +                       insn->call_dest = find_func_by_offset(rela->sym->sec,
> +                                                             rela->addend+4);
> +                       if (!insn->call_dest) {
>                                 WARN_FUNC("can't find call dest symbol at %s+0x%x",
>                                           insn->sec, insn->offset,
>                                           rela->sym->sec->name,
> diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
> index edba4745f25a..cc4601c879ce 100644
> --- a/tools/objtool/elf.c
> +++ b/tools/objtool/elf.c
> @@ -62,8 +62,18 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
>         struct symbol *sym;
>
>         list_for_each_entry(sym, &sec->symbol_list, list)
> -               if (sym->type != STT_SECTION &&
> -                   sym->offset == offset)
> +               if (sym->type != STT_SECTION && sym->offset == offset)
> +                       return sym;
> +
> +       return NULL;
> +}
> +
> +struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
> +{
> +       struct symbol *sym;
> +
> +       list_for_each_entry(sym, &sec->symbol_list, list)
> +               if (sym->type == STT_FUNC && sym->offset == offset)
>                         return sym;
>
>         return NULL;
> diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
> index 44150204db4d..a1963259b930 100644
> --- a/tools/objtool/elf.h
> +++ b/tools/objtool/elf.h
> @@ -77,6 +77,7 @@ struct elf {
>
>  struct elf *elf_read(const char *name, int flags);
>  struct section *find_section_by_name(struct elf *elf, const char *name);
> +struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
>  struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
>  struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
>  struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);
> --
> 2.21.1
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/2] objtool: Fix clang switch table edge case
  2020-02-18  3:41 ` [PATCH 1/2] objtool: Fix clang switch table edge case Josh Poimboeuf
@ 2020-02-18 19:57   ` Nathan Chancellor
  2020-02-21  9:50   ` [tip: core/objtool] " tip-bot2 for Josh Poimboeuf
  1 sibling, 0 replies; 10+ messages in thread
From: Nathan Chancellor @ 2020-02-18 19:57 UTC (permalink / raw)
  To: Josh Poimboeuf; +Cc: x86, linux-kernel, Peter Zijlstra, Nick Desaulniers

On Mon, Feb 17, 2020 at 09:41:53PM -0600, Josh Poimboeuf wrote:
> Clang has the ability to create a switch table which is not a jump
> table, but is rather a table of string pointers.  This confuses objtool,
> because it sees the relocations for the string pointers and assumes
> they're part of a jump table:
> 
>   drivers/ata/sata_dwc_460ex.o: warning: objtool: sata_dwc_bmdma_start_by_tag()+0x3a2: can't find switch jump table
>   net/ceph/messenger.o: warning: objtool: ceph_con_workfn()+0x47c: can't find switch jump table
> 
> Make objtool's find_jump_table() smart enough to distinguish between a
> switch jump table (which has relocations to text addresses in the same
> function as the original instruction) and other anonymous rodata (which
> may have relocations to elsewhere).
> 
> Link: https://github.com/ClangBuiltLinux/linux/issues/485
> Reported-by: Nick Desaulniers <ndesaulniers@google.com>
> Tested-by: Nick Desaulniers <ndesaulniers@google.com>
> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>

Tested with clang-11 and binutils 2.34, a combo that was previously
broken and I no longer see these warnings on either defconfig or
allyesconfig.

Tested-by: Nathan Chancellor <natechancellor@gmail.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/2] objtool: Improve call destination function detection
  2020-02-18  3:41 ` [PATCH 2/2] objtool: Improve call destination function detection Josh Poimboeuf
  2020-02-18 18:13   ` Nick Desaulniers
@ 2020-02-18 19:58   ` Nathan Chancellor
  2020-02-21  9:50   ` [tip: core/objtool] " tip-bot2 for Josh Poimboeuf
  2 siblings, 0 replies; 10+ messages in thread
From: Nathan Chancellor @ 2020-02-18 19:58 UTC (permalink / raw)
  To: Josh Poimboeuf; +Cc: x86, linux-kernel, Peter Zijlstra, Nick Desaulniers

On Mon, Feb 17, 2020 at 09:41:54PM -0600, Josh Poimboeuf wrote:
> A recent clang change, combined with a binutils bug, can trigger a
> situation where a ".Lprintk$local" STT_NOTYPE symbol gets created at the
> same offset as the "printk" STT_FUNC symbol.  This confuses objtool:
> 
>   kernel/printk/printk.o: warning: objtool: ignore_loglevel_setup()+0x10: can't find call dest symbol at .text+0xc67
> 
> Improve the call destination detection by looking specifically for an
> STT_FUNC symbol.
> 
> Link: https://github.com/ClangBuiltLinux/linux/issues/872
> Link: https://sourceware.org/bugzilla/show_bug.cgi?id=25551
> Reported-by: Nick Desaulniers <ndesaulniers@google.com>
> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>

Tested with clang-11 and binutils 2.34, a combo that was previously
broken and I no longer see these warnings on either defconfig or
allyesconfig.

Tested-by: Nathan Chancellor <natechancellor@gmail.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/2] objtool: clang-related fixes
  2020-02-18  3:41 [PATCH 0/2] objtool: clang-related fixes Josh Poimboeuf
  2020-02-18  3:41 ` [PATCH 1/2] objtool: Fix clang switch table edge case Josh Poimboeuf
  2020-02-18  3:41 ` [PATCH 2/2] objtool: Improve call destination function detection Josh Poimboeuf
@ 2020-02-20 19:06 ` Nick Desaulniers
  2020-02-20 19:27   ` Borislav Petkov
  2 siblings, 1 reply; 10+ messages in thread
From: Nick Desaulniers @ 2020-02-20 19:06 UTC (permalink / raw)
  To: Thomas Gleixner, Ingo Molnar, Borislav Petkov
  Cc: maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
	LKML, Peter Zijlstra, Josh Poimboeuf

Hello, can one of the x86 maintainers please pick up this series? Our
CI has been red *for days* now without it.

On Mon, Feb 17, 2020 at 7:42 PM Josh Poimboeuf <jpoimboe@redhat.com> wrote:
>
> Fix a couple of issues which were discovered after clang CI was broken
> by 644592d32837 ("objtool: Fail the kernel build on fatal errors").
>
> Josh Poimboeuf (2):
>   objtool: Fix clang switch table edge case
>   objtool: Improve call destination function detection
>
>  tools/objtool/check.c | 38 +++++++++++++++++++++++++++-----------
>  tools/objtool/elf.c   | 14 ++++++++++++--
>  tools/objtool/elf.h   |  1 +
>  3 files changed, 40 insertions(+), 13 deletions(-)
>
> --
> 2.21.1
>


-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 0/2] objtool: clang-related fixes
  2020-02-20 19:06 ` [PATCH 0/2] objtool: clang-related fixes Nick Desaulniers
@ 2020-02-20 19:27   ` Borislav Petkov
  0 siblings, 0 replies; 10+ messages in thread
From: Borislav Petkov @ 2020-02-20 19:27 UTC (permalink / raw)
  To: Nick Desaulniers
  Cc: Thomas Gleixner, Ingo Molnar,
	maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT),
	LKML, Peter Zijlstra, Josh Poimboeuf

On Thu, Feb 20, 2020 at 11:06:30AM -0800, Nick Desaulniers wrote:
> Hello, can one of the x86 maintainers please pick up this series? Our
> CI has been red *for days* now without it.

I'll queue them tomorrow.

Thx.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tip: core/objtool] objtool: Fix clang switch table edge case
  2020-02-18  3:41 ` [PATCH 1/2] objtool: Fix clang switch table edge case Josh Poimboeuf
  2020-02-18 19:57   ` Nathan Chancellor
@ 2020-02-21  9:50   ` tip-bot2 for Josh Poimboeuf
  1 sibling, 0 replies; 10+ messages in thread
From: tip-bot2 for Josh Poimboeuf @ 2020-02-21  9:50 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Nick Desaulniers, Josh Poimboeuf, Borislav Petkov,
	Nathan Chancellor, x86, LKML

The following commit has been merged into the core/objtool branch of tip:

Commit-ID:     113d4bc9048336ba7c3d2ad972dbad4aef6e148a
Gitweb:        https://git.kernel.org/tip/113d4bc9048336ba7c3d2ad972dbad4aef6e148a
Author:        Josh Poimboeuf <jpoimboe@redhat.com>
AuthorDate:    Mon, 17 Feb 2020 21:41:53 -06:00
Committer:     Borislav Petkov <bp@suse.de>
CommitterDate: Fri, 21 Feb 2020 10:12:52 +01:00

objtool: Fix clang switch table edge case

Clang has the ability to create a switch table which is not a jump
table, but is rather a table of string pointers.  This confuses objtool,
because it sees the relocations for the string pointers and assumes
they're part of a jump table:

  drivers/ata/sata_dwc_460ex.o: warning: objtool: sata_dwc_bmdma_start_by_tag()+0x3a2: can't find switch jump table
  net/ceph/messenger.o: warning: objtool: ceph_con_workfn()+0x47c: can't find switch jump table

Make objtool's find_jump_table() smart enough to distinguish between a
switch jump table (which has relocations to text addresses in the same
function as the original instruction) and other anonymous rodata (which
may have relocations to elsewhere).

Reported-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nathan Chancellor <natechancellor@gmail.com>
Link: https://github.com/ClangBuiltLinux/linux/issues/485
Link: https://lkml.kernel.org/r/263f6aae46d33da0b86d7030ced878cb5cab1788.1581997059.git.jpoimboe@redhat.com
---
 tools/objtool/check.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index b038de2..4d6e283 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1025,7 +1025,7 @@ static struct rela *find_jump_table(struct objtool_file *file,
 				      struct instruction *insn)
 {
 	struct rela *text_rela, *table_rela;
-	struct instruction *orig_insn = insn;
+	struct instruction *dest_insn, *orig_insn = insn;
 	struct section *table_sec;
 	unsigned long table_offset;
 
@@ -1077,10 +1077,17 @@ static struct rela *find_jump_table(struct objtool_file *file,
 		    strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
 			continue;
 
-		/* Each table entry has a rela associated with it. */
+		/*
+		 * Each table entry has a rela associated with it.  The rela
+		 * should reference text in the same function as the original
+		 * instruction.
+		 */
 		table_rela = find_rela_by_dest(table_sec, table_offset);
 		if (!table_rela)
 			continue;
+		dest_insn = find_insn(file, table_rela->sym->sec, table_rela->addend);
+		if (!dest_insn || !dest_insn->func || dest_insn->func->pfunc != func)
+			continue;
 
 		/*
 		 * Use of RIP-relative switch jumps is quite rare, and

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [tip: core/objtool] objtool: Improve call destination function detection
  2020-02-18  3:41 ` [PATCH 2/2] objtool: Improve call destination function detection Josh Poimboeuf
  2020-02-18 18:13   ` Nick Desaulniers
  2020-02-18 19:58   ` Nathan Chancellor
@ 2020-02-21  9:50   ` tip-bot2 for Josh Poimboeuf
  2 siblings, 0 replies; 10+ messages in thread
From: tip-bot2 for Josh Poimboeuf @ 2020-02-21  9:50 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Nick Desaulniers, Josh Poimboeuf, Borislav Petkov,
	Nathan Chancellor, x86, LKML

The following commit has been merged into the core/objtool branch of tip:

Commit-ID:     7acfe5315312fc56c2a94c9216448087b38ae909
Gitweb:        https://git.kernel.org/tip/7acfe5315312fc56c2a94c9216448087b38ae909
Author:        Josh Poimboeuf <jpoimboe@redhat.com>
AuthorDate:    Mon, 17 Feb 2020 21:41:54 -06:00
Committer:     Borislav Petkov <bp@suse.de>
CommitterDate: Fri, 21 Feb 2020 10:20:34 +01:00

objtool: Improve call destination function detection

A recent clang change, combined with a binutils bug, can trigger a
situation where a ".Lprintk$local" STT_NOTYPE symbol gets created at the
same offset as the "printk" STT_FUNC symbol.  This confuses objtool:

  kernel/printk/printk.o: warning: objtool: ignore_loglevel_setup()+0x10: can't find call dest symbol at .text+0xc67

Improve the call destination detection by looking specifically for an
STT_FUNC symbol.

Reported-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Tested-by: Nick Desaulniers <ndesaulniers@google.com>
Tested-by: Nathan Chancellor <natechancellor@gmail.com>
Link: https://github.com/ClangBuiltLinux/linux/issues/872
Link: https://sourceware.org/bugzilla/show_bug.cgi?id=25551
Link: https://lkml.kernel.org/r/0a7ee320bc0ea4469bd3dc450a7b4725669e0ea9.1581997059.git.jpoimboe@redhat.com
---
 tools/objtool/check.c | 27 ++++++++++++++++++---------
 tools/objtool/elf.c   | 14 ++++++++++++--
 tools/objtool/elf.h   |  1 +
 3 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 4d6e283..6b6178e 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -420,8 +420,8 @@ static void add_ignores(struct objtool_file *file)
 			break;
 
 		case STT_SECTION:
-			func = find_symbol_by_offset(rela->sym->sec, rela->addend);
-			if (!func || func->type != STT_FUNC)
+			func = find_func_by_offset(rela->sym->sec, rela->addend);
+			if (!func)
 				continue;
 			break;
 
@@ -665,10 +665,14 @@ static int add_call_destinations(struct objtool_file *file)
 					       insn->len);
 		if (!rela) {
 			dest_off = insn->offset + insn->len + insn->immediate;
-			insn->call_dest = find_symbol_by_offset(insn->sec,
-								dest_off);
+			insn->call_dest = find_func_by_offset(insn->sec, dest_off);
+			if (!insn->call_dest)
+				insn->call_dest = find_symbol_by_offset(insn->sec, dest_off);
 
-			if (!insn->call_dest && !insn->ignore) {
+			if (insn->ignore)
+				continue;
+
+			if (!insn->call_dest) {
 				WARN_FUNC("unsupported intra-function call",
 					  insn->sec, insn->offset);
 				if (retpoline)
@@ -676,11 +680,16 @@ static int add_call_destinations(struct objtool_file *file)
 				return -1;
 			}
 
+			if (insn->func && insn->call_dest->type != STT_FUNC) {
+				WARN_FUNC("unsupported call to non-function",
+					  insn->sec, insn->offset);
+				return -1;
+			}
+
 		} else if (rela->sym->type == STT_SECTION) {
-			insn->call_dest = find_symbol_by_offset(rela->sym->sec,
-								rela->addend+4);
-			if (!insn->call_dest ||
-			    insn->call_dest->type != STT_FUNC) {
+			insn->call_dest = find_func_by_offset(rela->sym->sec,
+							      rela->addend+4);
+			if (!insn->call_dest) {
 				WARN_FUNC("can't find call dest symbol at %s+0x%x",
 					  insn->sec, insn->offset,
 					  rela->sym->sec->name,
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index edba474..cc4601c 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -62,8 +62,18 @@ struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
 	struct symbol *sym;
 
 	list_for_each_entry(sym, &sec->symbol_list, list)
-		if (sym->type != STT_SECTION &&
-		    sym->offset == offset)
+		if (sym->type != STT_SECTION && sym->offset == offset)
+			return sym;
+
+	return NULL;
+}
+
+struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
+{
+	struct symbol *sym;
+
+	list_for_each_entry(sym, &sec->symbol_list, list)
+		if (sym->type == STT_FUNC && sym->offset == offset)
 			return sym;
 
 	return NULL;
diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
index 4415020..a196325 100644
--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -77,6 +77,7 @@ struct elf {
 
 struct elf *elf_read(const char *name, int flags);
 struct section *find_section_by_name(struct elf *elf, const char *name);
+struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
 struct symbol *find_symbol_by_name(struct elf *elf, const char *name);
 struct symbol *find_symbol_containing(struct section *sec, unsigned long offset);

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-02-21  9:51 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-18  3:41 [PATCH 0/2] objtool: clang-related fixes Josh Poimboeuf
2020-02-18  3:41 ` [PATCH 1/2] objtool: Fix clang switch table edge case Josh Poimboeuf
2020-02-18 19:57   ` Nathan Chancellor
2020-02-21  9:50   ` [tip: core/objtool] " tip-bot2 for Josh Poimboeuf
2020-02-18  3:41 ` [PATCH 2/2] objtool: Improve call destination function detection Josh Poimboeuf
2020-02-18 18:13   ` Nick Desaulniers
2020-02-18 19:58   ` Nathan Chancellor
2020-02-21  9:50   ` [tip: core/objtool] " tip-bot2 for Josh Poimboeuf
2020-02-20 19:06 ` [PATCH 0/2] objtool: clang-related fixes Nick Desaulniers
2020-02-20 19:27   ` Borislav Petkov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).