2022-04-16 01:36:50

by Josh Poimboeuf

[permalink] [raw]
Subject: [PATCH 03/18] objtool: Add sec+offset to warnings

Change this:

vmlinux.o: warning: objtool: fixup_exception()+0x2d1: unreachable instruction

to this:

vmlinux.o: warning: objtool: fixup_exception()+0x2d1: .text+0x76c51: unreachable instruction

It's noisier, but it makes our lives considerably easier.

Make this new 'verbose' mode optional, which will come in handy soon.

Suggested-by: Nick Desaulniers <[email protected]>
Signed-off-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/include/objtool/warn.h | 39 +++++++++++++---------------
1 file changed, 18 insertions(+), 21 deletions(-)

diff --git a/tools/objtool/include/objtool/warn.h b/tools/objtool/include/objtool/warn.h
index dab0dda7c617..a4c6b52a58c9 100644
--- a/tools/objtool/include/objtool/warn.h
+++ b/tools/objtool/include/objtool/warn.h
@@ -15,12 +15,13 @@

extern const char *objname;

-static inline char *offstr(struct section *sec, unsigned long offset)
+static inline char *offstr(struct section *sec, unsigned long offset,
+ bool verbose)
{
bool is_text = (sec->sh.sh_flags & SHF_EXECINSTR);
struct symbol *sym = NULL;
- unsigned long name_off;
- char *name, *str;
+ char *str;
+ int len;

if (is_text)
sym = find_func_containing(sec, offset);
@@ -28,20 +29,16 @@ static inline char *offstr(struct section *sec, unsigned long offset)
sym = find_symbol_containing(sec, offset);

if (sym) {
- name = sym->name;
- name_off = offset - sym->offset;
+ str = malloc(strlen(sym->name) + strlen(sec->name) + 40);
+ len = sprintf(str, "%s%s+0x%lx",
+ sym->name, is_text ? "()" : "", offset - sym->offset);
+ if (verbose)
+ sprintf(str+len, ": %s+0x%lx", sec->name, offset);
} else {
- name = sec->name;
- name_off = offset;
+ str = malloc(strlen(sec->name) + 20);
+ sprintf(str, "%s+0x%lx", sec->name, offset);
}

- str = malloc(strlen(name) + 20);
-
- if (sym)
- sprintf(str, "%s%s+0x%lx", name, is_text ? "()" : "", name_off);
- else
- sprintf(str, "%s+0x%lx", name, name_off);
-
return str;
}

@@ -52,17 +49,17 @@ static inline char *offstr(struct section *sec, unsigned long offset)

#define WARN_FUNC(format, sec, offset, ...) \
({ \
- char *_str = offstr(sec, offset); \
+ char *_str = offstr(sec, offset, true); \
WARN("%s: " format, _str, ##__VA_ARGS__); \
free(_str); \
})

-#define BT_FUNC(format, insn, ...) \
-({ \
- struct instruction *_insn = (insn); \
- char *_str = offstr(_insn->sec, _insn->offset); \
- WARN(" %s: " format, _str, ##__VA_ARGS__); \
- free(_str); \
+#define BT_FUNC(format, insn, ...) \
+({ \
+ struct instruction *_insn = (insn); \
+ char *_str = offstr(_insn->sec, _insn->offset, true); \
+ WARN(" %s: " format, _str, ##__VA_ARGS__); \
+ free(_str); \
})

#define WARN_ELF(format, ...) \
--
2.34.1