]> Git Repo - linux.git/commitdiff
init: lto: fix PREL32 relocations
authorSami Tolvanen <[email protected]>
Fri, 11 Dec 2020 18:46:25 +0000 (10:46 -0800)
committerKees Cook <[email protected]>
Thu, 14 Jan 2021 16:21:09 +0000 (08:21 -0800)
With LTO, the compiler can rename static functions to avoid global
naming collisions. As initcall functions are typically static,
renaming can break references to them in inline assembly. This
change adds a global stub with a stable name for each initcall to
fix the issue when PREL32 relocations are used.

Signed-off-by: Sami Tolvanen <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
include/linux/init.h

index a57bf0dfd75f879a50361a72eb747e523c7e9493..a01f01c1a5c5414ba6ba6ccf7fc68230b35d0c31 100644 (file)
@@ -209,26 +209,49 @@ extern bool initcall_debug;
  */
 #define __initcall_section(__sec, __iid)                       \
        #__sec ".init.." #__iid
+
+/*
+ * With LTO, the compiler can rename static functions to avoid
+ * global naming collisions. We use a global stub function for
+ * initcalls to create a stable symbol name whose address can be
+ * taken in inline assembly when PREL32 relocations are used.
+ */
+#define __initcall_stub(fn, __iid, id)                         \
+       __initcall_name(initstub, __iid, id)
+
+#define __define_initcall_stub(__stub, fn)                     \
+       int __init __stub(void);                                \
+       int __init __stub(void)                                 \
+       {                                                       \
+               return fn();                                    \
+       }                                                       \
+       __ADDRESSABLE(__stub)
 #else
 #define __initcall_section(__sec, __iid)                       \
        #__sec ".init"
+
+#define __initcall_stub(fn, __iid, id) fn
+
+#define __define_initcall_stub(__stub, fn)                     \
+       __ADDRESSABLE(fn)
 #endif
 
 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
-#define ____define_initcall(fn, __name, __sec)                 \
-       __ADDRESSABLE(fn)                                       \
+#define ____define_initcall(fn, __stub, __name, __sec)         \
+       __define_initcall_stub(__stub, fn)                      \
        asm(".section   \"" __sec "\", \"a\"            \n"     \
            __stringify(__name) ":                      \n"     \
-           ".long      " #fn " - .                     \n"     \
+           ".long      " __stringify(__stub) " - .     \n"     \
            ".previous                                  \n");
 #else
-#define ____define_initcall(fn, __name, __sec)                 \
+#define ____define_initcall(fn, __unused, __name, __sec)       \
        static initcall_t __name __used                         \
                __attribute__((__section__(__sec))) = fn;
 #endif
 
 #define __unique_initcall(fn, id, __sec, __iid)                        \
        ____define_initcall(fn,                                 \
+               __initcall_stub(fn, __iid, id),                 \
                __initcall_name(initcall, __iid, id),           \
                __initcall_section(__sec, __iid))
 
This page took 0.046816 seconds and 4 git commands to generate.