[5/5] objtool: Add "missing __noreturn" warning

Message ID 4f22aa05702ca037630fa6c1f7fd54d13914a631.1679932620.git.jpoimboe@kernel.org
State New
Headers
Series objtool: warning improvements |

Commit Message

Josh Poimboeuf March 27, 2023, 4 p.m. UTC
  Most "unreachable instruction" warnings these days seem to actually be
the result of a missing __noreturn annotation.  Add an explicit check
for that.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 tools/objtool/Documentation/objtool.txt |  7 +++++++
 tools/objtool/check.c                   | 16 ++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)
  

Comments

Miroslav Benes March 28, 2023, 9:10 a.m. UTC | #1
>  		WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
> -		return 1;

I knew I should have read the whole set first...

Miroslav
  
Josh Poimboeuf March 29, 2023, 4:24 p.m. UTC | #2
On Tue, Mar 28, 2023 at 11:10:48AM +0200, Miroslav Benes wrote:
> >  		WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
> > -		return 1;
> 
> I knew I should have read the whole set first...

Oops, guess that is in the wrong patch.
  

Patch

diff --git a/tools/objtool/Documentation/objtool.txt b/tools/objtool/Documentation/objtool.txt
index ec6f82fb414c..e04d16490c3b 100644
--- a/tools/objtool/Documentation/objtool.txt
+++ b/tools/objtool/Documentation/objtool.txt
@@ -423,6 +423,13 @@  the objtool maintainers.
    names and does not use module_init() / module_exit() macros to create
    them.
 
+13. file.o: warning: func(): missing __noreturn
+
+   Objtool has detected that the function doesn't return, but is missing
+   the __noreturn annotation.  NOTE: In addition to adding the
+   __noreturn annotation, the function name also needs to be added to
+   'global_noreturns' in tools/objtool/check.c.
+
 
 If the error doesn't seem to make sense, it could be a bug in objtool.
 Feel free to ask the objtool maintainer for help.
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 67a684225702..1ed3024af2b1 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4548,7 +4548,8 @@  static int validate_sls(struct objtool_file *file)
 
 static int validate_reachable_instructions(struct objtool_file *file)
 {
-	struct instruction *insn;
+	struct instruction *insn, *prev_insn;
+	struct symbol *call_dest;
 
 	if (file->ignore_unreachables)
 		return 0;
@@ -4561,8 +4562,19 @@  static int validate_reachable_instructions(struct objtool_file *file)
 			continue;
 		insn->sym->warned = 1;
 
+		prev_insn = prev_insn_same_sec(file, insn);
+		if (prev_insn)
+			call_dest = insn_call_dest(prev_insn);
+		if (prev_insn && prev_insn->dead_end && call_dest) {
+			if (call_dest->warned)
+				continue;
+			call_dest->warned = 1;
+
+			WARN("%s(): missing __noreturn", call_dest->name);
+			continue;
+		}
+
 		WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
-		return 1;
 	}
 
 	return 0;