modpost: lto: strip .lto from module names
authorSami Tolvanen <samitolvanen@google.com>
Fri, 11 Dec 2020 18:46:27 +0000 (10:46 -0800)
committerKees Cook <keescook@chromium.org>
Thu, 14 Jan 2021 16:21:09 +0000 (08:21 -0800)
With LTO, everything is compiled into LLVM bitcode, so we have to link
each module into native code before modpost. Kbuild uses the .lto.o
suffix for these files, which also ends up in module information. This
change strips the unnecessary .lto suffix from the module name.

Suggested-by: Bill Wendling <morbo@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20201211184633.3213045-11-samitolvanen@google.com
scripts/mod/modpost.c
scripts/mod/modpost.h
scripts/mod/sumversion.c

index d6c81657d69550def54333df9f08c32e55b0913c..be0642126f535609ad717d02faa16393242fb668 100644 (file)
@@ -17,7 +17,6 @@
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
-#include <stdbool.h>
 #include <errno.h>
 #include "modpost.h"
 #include "../../include/linux/license.h"
@@ -84,14 +83,6 @@ modpost_log(enum loglevel loglevel, const char *fmt, ...)
                error_occurred = true;
 }
 
-static inline bool strends(const char *str, const char *postfix)
-{
-       if (strlen(str) < strlen(postfix))
-               return false;
-
-       return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
-}
-
 void *do_nofail(void *ptr, const char *expr)
 {
        if (!ptr)
@@ -1988,6 +1979,10 @@ static char *remove_dot(char *s)
                size_t m = strspn(s + n + 1, "0123456789");
                if (m && (s[n + m] == '.' || s[n + m] == 0))
                        s[n] = 0;
+
+               /* strip trailing .lto */
+               if (strends(s, ".lto"))
+                       s[strlen(s) - 4] = '\0';
        }
        return s;
 }
@@ -2011,6 +2006,9 @@ static void read_symbols(const char *modname)
                /* strip trailing .o */
                tmp = NOFAIL(strdup(modname));
                tmp[strlen(tmp) - 2] = '\0';
+               /* strip trailing .lto */
+               if (strends(tmp, ".lto"))
+                       tmp[strlen(tmp) - 4] = '\0';
                mod = new_module(tmp);
                free(tmp);
        }
index e6f46eee0af02f5a62222177a77847f0b639e956..1f76eea7cfdee73ca03ca5125d24f4f8a5545748 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <stdbool.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -180,6 +181,14 @@ static inline unsigned int get_secindex(const struct elf_info *info,
        return info->symtab_shndx_start[sym - info->symtab_start];
 }
 
+static inline bool strends(const char *str, const char *postfix)
+{
+       if (strlen(str) < strlen(postfix))
+               return false;
+
+       return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
+}
+
 /* file2alias.c */
 extern unsigned int cross_build;
 void handle_moddevtable(struct module *mod, struct elf_info *info,
index d587f40f11177ddb81f724757809c633b77195e5..760e6baa7eda0d2583e25db792caf2053fad222f 100644 (file)
@@ -391,10 +391,14 @@ void get_src_version(const char *modname, char sum[], unsigned sumlen)
        struct md4_ctx md;
        char *fname;
        char filelist[PATH_MAX + 1];
+       int postfix_len = 1;
+
+       if (strends(modname, ".lto.o"))
+               postfix_len = 5;
 
        /* objects for a module are listed in the first line of *.mod file. */
        snprintf(filelist, sizeof(filelist), "%.*smod",
-                (int)strlen(modname) - 1, modname);
+                (int)strlen(modname) - postfix_len, modname);
 
        buf = read_text_file(filelist);