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

From: Josh Poimboeuf
Date: Mon Mar 27 2023 - 12:01:44 EST


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@xxxxxxxxxx>
---
tools/objtool/Documentation/objtool.txt | 7 +++++++
tools/objtool/check.c | 16 ++++++++++++++--
2 files changed, 21 insertions(+), 2 deletions(-)

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;
--
2.39.2