When .modinfo has no "name=" tag, __find_modname() falls back to
converting the object's build-tree path to a runtime module name by
stripping directory components, converting '-' to '_' and truncating the
file extension.

It does all that in a single pass over the entire path, so the first dot
anywhere in the path ends the name.  For an object built in a directory
whose name contains a dot, e.g. "drivers/foo-1.0/bar.o", the result is a
bogus module name.

Strip the directory components up front so only the basename is scanned
for the extension separator.

Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing 
object files")
Reported-by: Sashiko <[email protected]>
Signed-off-by: Josh Poimboeuf <[email protected]>
---
 tools/objtool/klp-diff.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index f8787d7d1454..aeb99d572300 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1140,7 +1140,7 @@ static struct export *find_export(struct symbol *sym)
 static const char *__find_modname(struct elfs *e)
 {
        struct section *sec;
-       char *name;
+       char *name, *slash;
 
        sec = find_section_by_name(e->orig, ".modinfo");
        if (!sec) {
@@ -1158,10 +1158,12 @@ static const char *__find_modname(struct elfs *e)
                return NULL;
        }
 
+       slash = strrchr(name, '/');
+       if (slash)
+               name = slash + 1;
+
        for (char *c = name; *c; c++) {
-               if (*c == '/')
-                       name = c + 1;
-               else if (*c == '-')
+               if (*c == '-')
                        *c = '_';
                else if (*c == '.') {
                        *c = '\0';
-- 
2.54.0


Reply via email to