Skip to contentSkip to conversation

objtool: cope with pre-4.5 gcc (and non-gcc)

PATCH In review Jan Beulich 2 participants 2 messages May 13, 2016 – May 16, 2016 objtool

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 objtool: cope with pre-4.5 gcc (and non-gcc)

The kernel's unreachable() translates to __builtin_unreachable() only for gcc 4.5 and newer, and else expands to an infinite loop. Avoid "function has unreachable instruction" warnings for this case by inspecting the instructions immediately following the UD2. This cuts down the number of files getting such warnings by about 99% for me.

---
 tools/objtool/builtin-check.c |   37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
tools/objtool/builtin-check.c +36 -1
@@ -1033,6 +1033,39 @@ static bool is_ubsan_insn(struct instruc
 			"__ubsan_handle_builtin_unreachable"));
 }
 
+/*
+ * BUG() uses unreachable(), which on older gcc translates to an infinite
+ * loop, i.e. a branch targeting itself (or a group of immediately preceding
+ * NOPs). Avoid that branch (and the NOPs) getting considered unreachable,
+ * despite it really being so.
+ */
+static void mark_post_bug_insns(struct objtool_file *file,
+				struct instruction *insn)
+{
+	const struct instruction *first_nop = NULL;
+
+	if (insn->type != INSN_BUG)
+		return;
+
+	while ((insn = next_insn_same_sec(file, insn)) != NULL) {
+		switch (insn->type) {
+		case INSN_NOP:
+			insn->visited = true;
+			if (!first_nop)
+				first_nop = insn;
+			continue;
+		case INSN_JUMP_UNCONDITIONAL:
+			if (insn->jump_dest == insn ||
+			    (first_nop && insn->jump_dest == first_nop))
+				insn->visited = true;
+			break;
+		default:
+			break;
+		}
+		break;
+	}
+}
+
 static bool ignore_unreachable_insn(struct symbol *func,
 				    struct instruction *insn)
 {
@@ -1095,8 +1128,10 @@ static int validate_functions(struct obj
 				continue;
 
 			func_for_each_insn(file, func, insn) {
-				if (insn->visited)
+				if (insn->visited) {
+					mark_post_bug_insns(file, insn);
 					continue;
+				}
 
 				insn->visited = true;
 
#1 lore raw
Quoted 7 lines · Jan Beulich

The kernel's unreachable() translates to __builtin_unreachable() only for gcc 4.5 and newer, and else expands to an infinite loop. Avoid "function has unreachable instruction" warnings for this case by inspecting the instructions immediately following the UD2. This cuts down the number of files getting such warnings by about 99% for me.

Thanks for the patch. I'm wondering if we can detect this situation more precisely, maybe by adding some kind of annotation to the unreachable() macro. Will need to think about it a little more...

Signature · 1 line
Josh