kbuild: preprocess module linker script
authorMasahiro Yamada <masahiroy@kernel.org>
Tue, 8 Sep 2020 04:27:08 +0000 (13:27 +0900)
committerMasahiro Yamada <masahiroy@kernel.org>
Thu, 24 Sep 2020 15:36:41 +0000 (00:36 +0900)
There was a request to preprocess the module linker script like we
do for the vmlinux one. (https://lkml.org/lkml/2020/8/21/512)

The difference between vmlinux.lds and module.lds is that the latter
is needed for external module builds, thus must be cleaned up by
'make mrproper' instead of 'make clean'. Also, it must be created
by 'make modules_prepare'.

You cannot put it in arch/$(SRCARCH)/kernel/, which is cleaned up by
'make clean'. I moved arch/$(SRCARCH)/kernel/module.lds to
arch/$(SRCARCH)/include/asm/module.lds.h, which is included from
scripts/module.lds.S.

scripts/module.lds is fine because 'make clean' keeps all the
build artifacts under scripts/.

You can add arch-specific sections in <asm/module.lds.h>.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Tested-by: Jessica Yu <jeyu@kernel.org>
Acked-by: Will Deacon <will@kernel.org>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Acked-by: Palmer Dabbelt <palmerdabbelt@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Acked-by: Jessica Yu <jeyu@kernel.org>
28 files changed:
Makefile
arch/arm/Makefile
arch/arm/include/asm/module.lds.h [new file with mode: 0644]
arch/arm/kernel/module.lds [deleted file]
arch/arm64/Makefile
arch/arm64/include/asm/module.lds.h [new file with mode: 0644]
arch/arm64/kernel/module.lds [deleted file]
arch/ia64/Makefile
arch/ia64/include/asm/module.lds.h [new file with mode: 0644]
arch/ia64/module.lds [deleted file]
arch/m68k/Makefile
arch/m68k/include/asm/module.lds.h [new file with mode: 0644]
arch/m68k/kernel/module.lds [deleted file]
arch/powerpc/Makefile
arch/powerpc/include/asm/module.lds.h [new file with mode: 0644]
arch/powerpc/kernel/module.lds [deleted file]
arch/riscv/Makefile
arch/riscv/include/asm/module.lds.h [new file with mode: 0644]
arch/riscv/kernel/module.lds [deleted file]
arch/um/include/asm/Kbuild
include/asm-generic/Kbuild
include/asm-generic/module.lds.h [new file with mode: 0644]
scripts/.gitignore
scripts/Makefile
scripts/Makefile.modfinal
scripts/module-common.lds [deleted file]
scripts/module.lds.S [new file with mode: 0644]
scripts/package/builddeb

index 37739ee..97b1dae 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -505,7 +505,6 @@ KBUILD_CFLAGS_KERNEL :=
 KBUILD_AFLAGS_MODULE  := -DMODULE
 KBUILD_CFLAGS_MODULE  := -DMODULE
 KBUILD_LDFLAGS_MODULE :=
 KBUILD_AFLAGS_MODULE  := -DMODULE
 KBUILD_CFLAGS_MODULE  := -DMODULE
 KBUILD_LDFLAGS_MODULE :=
-export KBUILD_LDS_MODULE := $(srctree)/scripts/module-common.lds
 KBUILD_LDFLAGS :=
 CLANG_FLAGS :=
 
 KBUILD_LDFLAGS :=
 CLANG_FLAGS :=
 
@@ -1395,7 +1394,7 @@ endif
 # using awk while concatenating to the final file.
 
 PHONY += modules
 # using awk while concatenating to the final file.
 
 PHONY += modules
-modules: $(if $(KBUILD_BUILTIN),vmlinux) modules_check
+modules: $(if $(KBUILD_BUILTIN),vmlinux) modules_check modules_prepare
        $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
 
 PHONY += modules_check
        $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
 
 PHONY += modules_check
@@ -1412,6 +1411,7 @@ targets += modules.order
 # Target to prepare building external modules
 PHONY += modules_prepare
 modules_prepare: prepare
 # Target to prepare building external modules
 PHONY += modules_prepare
 modules_prepare: prepare
+       $(Q)$(MAKE) $(build)=scripts scripts/module.lds
 
 # Target to install modules
 PHONY += modules_install
 
 # Target to install modules
 PHONY += modules_install
@@ -1743,7 +1743,9 @@ help:
        @echo  '  clean           - remove generated files in module directory only'
        @echo  ''
 
        @echo  '  clean           - remove generated files in module directory only'
        @echo  ''
 
-PHONY += prepare
+# no-op for external module builds
+PHONY += prepare modules_prepare
+
 endif # KBUILD_EXTMOD
 
 # Single targets
 endif # KBUILD_EXTMOD
 
 # Single targets
@@ -1776,7 +1778,7 @@ MODORDER := .modules.tmp
 endif
 
 PHONY += single_modpost
 endif
 
 PHONY += single_modpost
-single_modpost: $(single-no-ko)
+single_modpost: $(single-no-ko) modules_prepare
        $(Q){ $(foreach m, $(single-ko), echo $(extmod-prefix)$m;) } > $(MODORDER)
        $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
 
        $(Q){ $(foreach m, $(single-ko), echo $(extmod-prefix)$m;) } > $(MODORDER)
        $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
 
index 4e87735..a0cb15d 100644 (file)
@@ -16,10 +16,6 @@ LDFLAGS_vmlinux      += --be8
 KBUILD_LDFLAGS_MODULE  += --be8
 endif
 
 KBUILD_LDFLAGS_MODULE  += --be8
 endif
 
-ifeq ($(CONFIG_ARM_MODULE_PLTS),y)
-KBUILD_LDS_MODULE      += $(srctree)/arch/arm/kernel/module.lds
-endif
-
 GZFLAGS                :=-9
 #KBUILD_CFLAGS +=-pipe
 
 GZFLAGS                :=-9
 #KBUILD_CFLAGS +=-pipe
 
diff --git a/arch/arm/include/asm/module.lds.h b/arch/arm/include/asm/module.lds.h
new file mode 100644 (file)
index 0000000..0e7cb4e
--- /dev/null
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifdef CONFIG_ARM_MODULE_PLTS
+SECTIONS {
+       .plt : { BYTE(0) }
+       .init.plt : { BYTE(0) }
+}
+#endif
diff --git a/arch/arm/kernel/module.lds b/arch/arm/kernel/module.lds
deleted file mode 100644 (file)
index 79cb6af..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-SECTIONS {
-       .plt : { BYTE(0) }
-       .init.plt : { BYTE(0) }
-}
index b45f012..76667ad 100644 (file)
@@ -115,10 +115,6 @@ endif
 
 CHECKFLAGS     += -D__aarch64__
 
 
 CHECKFLAGS     += -D__aarch64__
 
-ifeq ($(CONFIG_ARM64_MODULE_PLTS),y)
-KBUILD_LDS_MODULE      += $(srctree)/arch/arm64/kernel/module.lds
-endif
-
 ifeq ($(CONFIG_DYNAMIC_FTRACE_WITH_REGS),y)
   KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
   CC_FLAGS_FTRACE := -fpatchable-function-entry=2
 ifeq ($(CONFIG_DYNAMIC_FTRACE_WITH_REGS),y)
   KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
   CC_FLAGS_FTRACE := -fpatchable-function-entry=2
diff --git a/arch/arm64/include/asm/module.lds.h b/arch/arm64/include/asm/module.lds.h
new file mode 100644 (file)
index 0000000..691f15a
--- /dev/null
@@ -0,0 +1,7 @@
+#ifdef CONFIG_ARM64_MODULE_PLTS
+SECTIONS {
+       .plt (NOLOAD) : { BYTE(0) }
+       .init.plt (NOLOAD) : { BYTE(0) }
+       .text.ftrace_trampoline (NOLOAD) : { BYTE(0) }
+}
+#endif
diff --git a/arch/arm64/kernel/module.lds b/arch/arm64/kernel/module.lds
deleted file mode 100644 (file)
index 22e36a2..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-SECTIONS {
-       .plt (NOLOAD) : { BYTE(0) }
-       .init.plt (NOLOAD) : { BYTE(0) }
-       .text.ftrace_trampoline (NOLOAD) : { BYTE(0) }
-}
index 2876a7d..703b1c4 100644 (file)
@@ -20,7 +20,6 @@ CHECKFLAGS    += -D__ia64=1 -D__ia64__=1 -D_LP64 -D__LP64__
 
 OBJCOPYFLAGS   := --strip-all
 LDFLAGS_vmlinux        := -static
 
 OBJCOPYFLAGS   := --strip-all
 LDFLAGS_vmlinux        := -static
-KBUILD_LDS_MODULE += $(srctree)/arch/ia64/module.lds
 KBUILD_AFLAGS_KERNEL := -mconstant-gp
 EXTRA          :=
 
 KBUILD_AFLAGS_KERNEL := -mconstant-gp
 EXTRA          :=
 
diff --git a/arch/ia64/include/asm/module.lds.h b/arch/ia64/include/asm/module.lds.h
new file mode 100644 (file)
index 0000000..eff68f3
--- /dev/null
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+SECTIONS {
+       /* Group unwind sections into a single section: */
+       .IA_64.unwind_info : { *(.IA_64.unwind_info*) }
+       .IA_64.unwind : { *(.IA_64.unwind*) }
+       /*
+        * Create place-holder sections to hold the PLTs, GOT, and
+        * official procedure-descriptors (.opd).
+        */
+       .core.plt : { BYTE(0) }
+       .init.plt : { BYTE(0) }
+       .got : { BYTE(0) }
+       .opd : { BYTE(0) }
+}
diff --git a/arch/ia64/module.lds b/arch/ia64/module.lds
deleted file mode 100644 (file)
index eff68f3..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-SECTIONS {
-       /* Group unwind sections into a single section: */
-       .IA_64.unwind_info : { *(.IA_64.unwind_info*) }
-       .IA_64.unwind : { *(.IA_64.unwind*) }
-       /*
-        * Create place-holder sections to hold the PLTs, GOT, and
-        * official procedure-descriptors (.opd).
-        */
-       .core.plt : { BYTE(0) }
-       .init.plt : { BYTE(0) }
-       .got : { BYTE(0) }
-       .opd : { BYTE(0) }
-}
index 4438ffb..ea14f20 100644 (file)
@@ -75,7 +75,6 @@ KBUILD_CPPFLAGS += -D__uClinux__
 endif
 
 KBUILD_LDFLAGS := -m m68kelf
 endif
 
 KBUILD_LDFLAGS := -m m68kelf
-KBUILD_LDS_MODULE += $(srctree)/arch/m68k/kernel/module.lds
 
 ifdef CONFIG_SUN3
 LDFLAGS_vmlinux = -N
 
 ifdef CONFIG_SUN3
 LDFLAGS_vmlinux = -N
diff --git a/arch/m68k/include/asm/module.lds.h b/arch/m68k/include/asm/module.lds.h
new file mode 100644 (file)
index 0000000..fda94fa
--- /dev/null
@@ -0,0 +1,7 @@
+SECTIONS {
+       .m68k_fixup : {
+               __start_fixup = .;
+               *(.m68k_fixup)
+               __stop_fixup = .;
+       }
+}
diff --git a/arch/m68k/kernel/module.lds b/arch/m68k/kernel/module.lds
deleted file mode 100644 (file)
index fda94fa..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-SECTIONS {
-       .m68k_fixup : {
-               __start_fixup = .;
-               *(.m68k_fixup)
-               __stop_fixup = .;
-       }
-}
index 3e8da9c..8935658 100644 (file)
@@ -65,7 +65,6 @@ UTS_MACHINE := $(subst $(space),,$(machine-y))
 ifdef CONFIG_PPC32
 KBUILD_LDFLAGS_MODULE += arch/powerpc/lib/crtsavres.o
 else
 ifdef CONFIG_PPC32
 KBUILD_LDFLAGS_MODULE += arch/powerpc/lib/crtsavres.o
 else
-KBUILD_LDS_MODULE += $(srctree)/arch/powerpc/kernel/module.lds
 ifeq ($(call ld-ifversion, -ge, 225000000, y),y)
 # Have the linker provide sfpr if possible.
 # There is a corresponding test in arch/powerpc/lib/Makefile
 ifeq ($(call ld-ifversion, -ge, 225000000, y),y)
 # Have the linker provide sfpr if possible.
 # There is a corresponding test in arch/powerpc/lib/Makefile
diff --git a/arch/powerpc/include/asm/module.lds.h b/arch/powerpc/include/asm/module.lds.h
new file mode 100644 (file)
index 0000000..cea5dc1
--- /dev/null
@@ -0,0 +1,8 @@
+/* Force alignment of .toc section.  */
+SECTIONS
+{
+       .toc 0 : ALIGN(256)
+       {
+               *(.got .toc)
+       }
+}
diff --git a/arch/powerpc/kernel/module.lds b/arch/powerpc/kernel/module.lds
deleted file mode 100644 (file)
index cea5dc1..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-/* Force alignment of .toc section.  */
-SECTIONS
-{
-       .toc 0 : ALIGN(256)
-       {
-               *(.got .toc)
-       }
-}
index fb6e37d..8edaa8b 100644 (file)
@@ -53,9 +53,6 @@ endif
 ifeq ($(CONFIG_CMODEL_MEDANY),y)
        KBUILD_CFLAGS += -mcmodel=medany
 endif
 ifeq ($(CONFIG_CMODEL_MEDANY),y)
        KBUILD_CFLAGS += -mcmodel=medany
 endif
-ifeq ($(CONFIG_MODULE_SECTIONS),y)
-       KBUILD_LDS_MODULE += $(srctree)/arch/riscv/kernel/module.lds
-endif
 ifeq ($(CONFIG_PERF_EVENTS),y)
         KBUILD_CFLAGS += -fno-omit-frame-pointer
 endif
 ifeq ($(CONFIG_PERF_EVENTS),y)
         KBUILD_CFLAGS += -fno-omit-frame-pointer
 endif
diff --git a/arch/riscv/include/asm/module.lds.h b/arch/riscv/include/asm/module.lds.h
new file mode 100644 (file)
index 0000000..4254ff2
--- /dev/null
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2017 Andes Technology Corporation */
+#ifdef CONFIG_MODULE_SECTIONS
+SECTIONS {
+       .plt (NOLOAD) : { BYTE(0) }
+       .got (NOLOAD) : { BYTE(0) }
+       .got.plt (NOLOAD) : { BYTE(0) }
+}
+#endif
diff --git a/arch/riscv/kernel/module.lds b/arch/riscv/kernel/module.lds
deleted file mode 100644 (file)
index 295ecfb..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/* Copyright (C) 2017 Andes Technology Corporation */
-
-SECTIONS {
-       .plt (NOLOAD) : { BYTE(0) }
-       .got (NOLOAD) : { BYTE(0) }
-       .got.plt (NOLOAD) : { BYTE(0) }
-}
index 8d435f8..1c63b26 100644 (file)
@@ -16,6 +16,7 @@ generic-y += kdebug.h
 generic-y += mcs_spinlock.h
 generic-y += mm-arch-hooks.h
 generic-y += mmiowb.h
 generic-y += mcs_spinlock.h
 generic-y += mm-arch-hooks.h
 generic-y += mmiowb.h
+generic-y += module.lds.h
 generic-y += param.h
 generic-y += pci.h
 generic-y += percpu.h
 generic-y += param.h
 generic-y += pci.h
 generic-y += percpu.h
index 74b0612..7cd4e62 100644 (file)
@@ -40,6 +40,7 @@ mandatory-y += mmiowb.h
 mandatory-y += mmu.h
 mandatory-y += mmu_context.h
 mandatory-y += module.h
 mandatory-y += mmu.h
 mandatory-y += mmu_context.h
 mandatory-y += module.h
+mandatory-y += module.lds.h
 mandatory-y += msi.h
 mandatory-y += pci.h
 mandatory-y += percpu.h
 mandatory-y += msi.h
 mandatory-y += pci.h
 mandatory-y += percpu.h
diff --git a/include/asm-generic/module.lds.h b/include/asm-generic/module.lds.h
new file mode 100644 (file)
index 0000000..f210d5c
--- /dev/null
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __ASM_GENERIC_MODULE_LDS_H
+#define __ASM_GENERIC_MODULE_LDS_H
+
+/*
+ * <asm/module.lds.h> can specify arch-specific sections for linking modules.
+ * Empty for the asm-generic header.
+ */
+
+#endif /* __ASM_GENERIC_MODULE_LDS_H */
index 0d1c8e2..a6c1131 100644 (file)
@@ -8,3 +8,4 @@ asn1_compiler
 extract-cert
 sign-file
 insert-sys-cert
 extract-cert
 sign-file
 insert-sys-cert
+/module.lds
index bc018e4..b5418ec 100644 (file)
@@ -29,6 +29,9 @@ endif
 # The following programs are only built on demand
 hostprogs += unifdef
 
 # The following programs are only built on demand
 hostprogs += unifdef
 
+# The module linker script is preprocessed on demand
+targets += module.lds
+
 subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
 subdir-$(CONFIG_MODVERSIONS) += genksyms
 subdir-$(CONFIG_SECURITY_SELINUX) += selinux
 subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
 subdir-$(CONFIG_MODVERSIONS) += genksyms
 subdir-$(CONFIG_SECURITY_SELINUX) += selinux
index 411c1e6..ae01baf 100644 (file)
@@ -33,11 +33,10 @@ quiet_cmd_ld_ko_o = LD [M]  $@
       cmd_ld_ko_o =                                                     \
        $(LD) -r $(KBUILD_LDFLAGS)                                      \
                $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE)              \
       cmd_ld_ko_o =                                                     \
        $(LD) -r $(KBUILD_LDFLAGS)                                      \
                $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE)              \
-               $(addprefix -T , $(KBUILD_LDS_MODULE))                  \
-               -o $@ $(filter %.o, $^);                                \
+               -T scripts/module.lds -o $@ $(filter %.o, $^);          \
        $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
 
        $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
 
-$(modules): %.ko: %.o %.mod.o $(KBUILD_LDS_MODULE) FORCE
+$(modules): %.ko: %.o %.mod.o scripts/module.lds FORCE
        +$(call if_changed,ld_ko_o)
 
 targets += $(modules) $(modules:.ko=.mod.o)
        +$(call if_changed,ld_ko_o)
 
 targets += $(modules) $(modules:.ko=.mod.o)
diff --git a/scripts/module-common.lds b/scripts/module-common.lds
deleted file mode 100644 (file)
index d61b9e8..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Common module linker script, always used when linking a module.
- * Archs are free to supply their own linker scripts.  ld will
- * combine them automatically.
- */
-SECTIONS {
-       /DISCARD/ : {
-               *(.discard)
-               *(.discard.*)
-       }
-
-       __ksymtab               0 : { *(SORT(___ksymtab+*)) }
-       __ksymtab_gpl           0 : { *(SORT(___ksymtab_gpl+*)) }
-       __ksymtab_unused        0 : { *(SORT(___ksymtab_unused+*)) }
-       __ksymtab_unused_gpl    0 : { *(SORT(___ksymtab_unused_gpl+*)) }
-       __ksymtab_gpl_future    0 : { *(SORT(___ksymtab_gpl_future+*)) }
-       __kcrctab               0 : { *(SORT(___kcrctab+*)) }
-       __kcrctab_gpl           0 : { *(SORT(___kcrctab_gpl+*)) }
-       __kcrctab_unused        0 : { *(SORT(___kcrctab_unused+*)) }
-       __kcrctab_unused_gpl    0 : { *(SORT(___kcrctab_unused_gpl+*)) }
-       __kcrctab_gpl_future    0 : { *(SORT(___kcrctab_gpl_future+*)) }
-
-       .init_array             0 : ALIGN(8) { *(SORT(.init_array.*)) *(.init_array) }
-
-       __jump_table            0 : ALIGN(8) { KEEP(*(__jump_table)) }
-}
diff --git a/scripts/module.lds.S b/scripts/module.lds.S
new file mode 100644 (file)
index 0000000..69b9b71
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Common module linker script, always used when linking a module.
+ * Archs are free to supply their own linker scripts.  ld will
+ * combine them automatically.
+ */
+SECTIONS {
+       /DISCARD/ : {
+               *(.discard)
+               *(.discard.*)
+       }
+
+       __ksymtab               0 : { *(SORT(___ksymtab+*)) }
+       __ksymtab_gpl           0 : { *(SORT(___ksymtab_gpl+*)) }
+       __ksymtab_unused        0 : { *(SORT(___ksymtab_unused+*)) }
+       __ksymtab_unused_gpl    0 : { *(SORT(___ksymtab_unused_gpl+*)) }
+       __ksymtab_gpl_future    0 : { *(SORT(___ksymtab_gpl_future+*)) }
+       __kcrctab               0 : { *(SORT(___kcrctab+*)) }
+       __kcrctab_gpl           0 : { *(SORT(___kcrctab_gpl+*)) }
+       __kcrctab_unused        0 : { *(SORT(___kcrctab_unused+*)) }
+       __kcrctab_unused_gpl    0 : { *(SORT(___kcrctab_unused_gpl+*)) }
+       __kcrctab_gpl_future    0 : { *(SORT(___kcrctab_gpl_future+*)) }
+
+       .init_array             0 : ALIGN(8) { *(SORT(.init_array.*)) *(.init_array) }
+
+       __jump_table            0 : ALIGN(8) { KEEP(*(__jump_table)) }
+}
+
+/* bring in arch-specific sections */
+#include <asm/module.lds.h>
index 6df3c9f..44f212e 100755 (executable)
@@ -55,7 +55,7 @@ deploy_kernel_headers () {
                cd $srctree
                find . arch/$SRCARCH -maxdepth 1 -name Makefile\*
                find include scripts -type f -o -type l
                cd $srctree
                find . arch/$SRCARCH -maxdepth 1 -name Makefile\*
                find include scripts -type f -o -type l
-               find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform
+               find arch/$SRCARCH -name Kbuild.platforms -o -name Platform
                find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
        ) > debian/hdrsrcfiles
 
                find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
        ) > debian/hdrsrcfiles