Skip to content

Commit ddb5cdb

Browse files
committed
kbuild: generate KSYMTAB entries by modpost
Commit 7b45371 ("kbuild: link symbol CRCs at final link, removing CONFIG_MODULE_REL_CRCS") made modpost output CRCs in the same way whether the EXPORT_SYMBOL() is placed in *.c or *.S. For further cleanups, this commit applies a similar approach to the entire data structure of EXPORT_SYMBOL(). The EXPORT_SYMBOL() compilation is split into two stages. When a source file is compiled, EXPORT_SYMBOL() will be converted into a dummy symbol in the .export_symbol section. For example, EXPORT_SYMBOL(foo); EXPORT_SYMBOL_NS_GPL(bar, BAR_NAMESPACE); will be encoded into the following assembly code: .section ".export_symbol","a" __export_symbol_foo: .asciz "" /* license */ .asciz "" /* name space */ .balign 8 .quad foo /* symbol reference */ .previous .section ".export_symbol","a" __export_symbol_bar: .asciz "GPL" /* license */ .asciz "BAR_NAMESPACE" /* name space */ .balign 8 .quad bar /* symbol reference */ .previous They are mere markers to tell modpost the name, license, and namespace of the symbols. They will be dropped from the final vmlinux and modules because the *(.export_symbol) will go into /DISCARD/ in the linker script. Then, modpost extracts all the information about EXPORT_SYMBOL() from the .export_symbol section, and generates the final C code: KSYMTAB_FUNC(foo, "", ""); KSYMTAB_FUNC(bar, "_gpl", "BAR_NAMESPACE"); KSYMTAB_FUNC() (or KSYMTAB_DATA() if it is data) is expanded to struct kernel_symbol that will be linked to the vmlinux or a module. With this change, EXPORT_SYMBOL() works in the same way for *.c and *.S files, providing the following benefits. [1] Deprecate EXPORT_DATA_SYMBOL() In the old days, EXPORT_SYMBOL() was only available in C files. To export a symbol in *.S, EXPORT_SYMBOL() was placed in a separate *.c file. arch/arm/kernel/armksyms.c is one example written in the classic manner. Commit 22823ab ("EXPORT_SYMBOL() for asm") removed this limitation. Since then, EXPORT_SYMBOL() can be placed close to the symbol definition in *.S files. It was a nice improvement. However, as that commit mentioned, you need to use EXPORT_DATA_SYMBOL() for data objects on some architectures. In the new approach, modpost checks symbol's type (STT_FUNC or not), and outputs KSYMTAB_FUNC() or KSYMTAB_DATA() accordingly. There are only two users of EXPORT_DATA_SYMBOL: EXPORT_DATA_SYMBOL_GPL(empty_zero_page) (arch/ia64/kernel/head.S) EXPORT_DATA_SYMBOL(ia64_ivt) (arch/ia64/kernel/ivt.S) They are transformed as follows and output into .vmlinux.export.c KSYMTAB_DATA(empty_zero_page, "_gpl", ""); KSYMTAB_DATA(ia64_ivt, "", ""); The other EXPORT_SYMBOL users in ia64 assembly are output as KSYMTAB_FUNC(). EXPORT_DATA_SYMBOL() is now deprecated. [2] merge <linux/export.h> and <asm-generic/export.h> There are two similar header implementations: include/linux/export.h for .c files include/asm-generic/export.h for .S files Ideally, the functionality should be consistent between them, but they tend to diverge. Commit 8651ec0 ("module: add support for symbol namespaces.") did not support the namespace for *.S files. This commit shifts the essential implementation part to C, which supports EXPORT_SYMBOL_NS() for *.S files. <asm/export.h> and <asm-generic/export.h> will remain as a wrapper of <linux/export.h> for a while. They will be removed after #include <asm/export.h> directives are all replaced with #include <linux/export.h>. [3] Implement CONFIG_TRIM_UNUSED_KSYMS in one-pass algorithm (by a later commit) When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses the directory tree to determine which EXPORT_SYMBOL to trim. If an EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the second traverse, where some source files are recompiled with their EXPORT_SYMBOL() tuned into a no-op. We can do this better now; modpost can selectively emit KSYMTAB entries that are really used by modules. Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]>
1 parent 94d6cb6 commit ddb5cdb

File tree

12 files changed

+190
-184
lines changed

12 files changed

+190
-184
lines changed

arch/ia64/include/asm/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22
generated-y += syscall_table.h
33
generic-y += agp.h
4+
generic-y += export.h
45
generic-y += kvm_para.h
56
generic-y += mcs_spinlock.h
67
generic-y += vtime.h

arch/ia64/include/asm/export.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

include/asm-generic/export.h

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,12 @@
33
#define __ASM_GENERIC_EXPORT_H
44

55
/*
6-
* This comment block is used by fixdep. Please do not remove.
7-
*
8-
* When CONFIG_MODVERSIONS is changed from n to y, all source files having
9-
* EXPORT_SYMBOL variants must be re-compiled because genksyms is run as a
10-
* side effect of the *.o build rule.
6+
* <asm/export.h> and <asm-generic/export.h> are deprecated.
7+
* Please include <linux/export.h> directly.
118
*/
9+
#include <linux/export.h>
1210

13-
#ifndef KSYM_FUNC
14-
#define KSYM_FUNC(x) x
15-
#endif
16-
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
17-
#define KSYM_ALIGN 4
18-
#elif defined(CONFIG_64BIT)
19-
#define KSYM_ALIGN 8
20-
#else
21-
#define KSYM_ALIGN 4
22-
#endif
23-
24-
.macro __put, val, name
25-
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
26-
.long \val - ., \name - ., 0
27-
#elif defined(CONFIG_64BIT)
28-
.quad \val, \name, 0
29-
#else
30-
.long \val, \name, 0
31-
#endif
32-
.endm
33-
34-
/*
35-
* note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
36-
* section flag requires it. Use '%progbits' instead of '@progbits' since the
37-
* former apparently works on all arches according to the binutils source.
38-
*/
39-
40-
.macro ___EXPORT_SYMBOL name,val,sec
41-
#if defined(CONFIG_MODULES) && !defined(__DISABLE_EXPORTS)
42-
.section ___ksymtab\sec+\name,"a"
43-
.balign KSYM_ALIGN
44-
__ksymtab_\name:
45-
__put \val, __kstrtab_\name
46-
.previous
47-
.section __ksymtab_strings,"aMS",%progbits,1
48-
__kstrtab_\name:
49-
.asciz "\name"
50-
.previous
51-
#endif
52-
.endm
53-
54-
#if defined(CONFIG_TRIM_UNUSED_KSYMS)
55-
56-
#include <linux/kconfig.h>
57-
#include <generated/autoksyms.h>
58-
59-
.macro __ksym_marker sym
60-
.section ".discard.ksym","a"
61-
__ksym_marker_\sym:
62-
.previous
63-
.endm
64-
65-
#define __EXPORT_SYMBOL(sym, val, sec) \
66-
__ksym_marker sym; \
67-
__cond_export_sym(sym, val, sec, __is_defined(__KSYM_##sym))
68-
#define __cond_export_sym(sym, val, sec, conf) \
69-
___cond_export_sym(sym, val, sec, conf)
70-
#define ___cond_export_sym(sym, val, sec, enabled) \
71-
__cond_export_sym_##enabled(sym, val, sec)
72-
#define __cond_export_sym_1(sym, val, sec) ___EXPORT_SYMBOL sym, val, sec
73-
#define __cond_export_sym_0(sym, val, sec) /* nothing */
74-
75-
#else
76-
#define __EXPORT_SYMBOL(sym, val, sec) ___EXPORT_SYMBOL sym, val, sec
77-
#endif
78-
79-
#define EXPORT_SYMBOL(name) \
80-
__EXPORT_SYMBOL(name, KSYM_FUNC(name),)
81-
#define EXPORT_SYMBOL_GPL(name) \
82-
__EXPORT_SYMBOL(name, KSYM_FUNC(name), _gpl)
83-
#define EXPORT_DATA_SYMBOL(name) \
84-
__EXPORT_SYMBOL(name, name,)
85-
#define EXPORT_DATA_SYMBOL_GPL(name) \
86-
__EXPORT_SYMBOL(name, name,_gpl)
11+
#define EXPORT_DATA_SYMBOL(name) EXPORT_SYMBOL(name)
12+
#define EXPORT_DATA_SYMBOL_GPL(name) EXPORT_SYMBOL_GPL(name)
8713

8814
#endif

include/asm-generic/vmlinux.lds.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@
10061006
PATCHABLE_DISCARDS \
10071007
*(.discard) \
10081008
*(.discard.*) \
1009+
*(.export_symbol) \
10091010
*(.modinfo) \
10101011
/* ld.bfd warns about .gnu.version* even when not emitted */ \
10111012
*(.gnu.version*) \

include/linux/export-internal.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,55 @@
1010
#include <linux/compiler.h>
1111
#include <linux/types.h>
1212

13+
#if defined(CONFIG_HAVE_ARCH_PREL32_RELOCATIONS)
14+
/*
15+
* relative reference: this reduces the size by half on 64-bit architectures,
16+
* and eliminates the need for absolute relocations that require runtime
17+
* processing on relocatable kernels.
18+
*/
19+
#define __KSYM_REF(sym) ".long " #sym "- ."
20+
#elif defined(CONFIG_64BIT)
21+
#define __KSYM_REF(sym) ".quad " #sym
22+
#else
23+
#define __KSYM_REF(sym) ".long " #sym
24+
#endif
25+
26+
/*
27+
* For every exported symbol, do the following:
28+
*
29+
* - Put the name of the symbol and namespace (empty string "" for none) in
30+
* __ksymtab_strings.
31+
* - Place a struct kernel_symbol entry in the __ksymtab section.
32+
*
33+
* Note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
34+
* section flag requires it. Use '%progbits' instead of '@progbits' since the
35+
* former apparently works on all arches according to the binutils source.
36+
*/
37+
#define __KSYMTAB(name, sym, sec, ns) \
38+
asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1" "\n" \
39+
"__kstrtab_" #name ":" "\n" \
40+
" .asciz \"" #name "\"" "\n" \
41+
"__kstrtabns_" #name ":" "\n" \
42+
" .asciz \"" ns "\"" "\n" \
43+
" .previous" "\n" \
44+
" .section \"___ksymtab" sec "+" #name "\", \"a\"" "\n" \
45+
" .balign 4" "\n" \
46+
"__ksymtab_" #name ":" "\n" \
47+
__KSYM_REF(sym) "\n" \
48+
__KSYM_REF(__kstrtab_ ##name) "\n" \
49+
__KSYM_REF(__kstrtabns_ ##name) "\n" \
50+
" .previous" "\n" \
51+
)
52+
53+
#ifdef CONFIG_IA64
54+
#define KSYM_FUNC(name) @fptr(name)
55+
#else
56+
#define KSYM_FUNC(name) name
57+
#endif
58+
59+
#define KSYMTAB_FUNC(name, sec, ns) __KSYMTAB(name, KSYM_FUNC(name), sec, ns)
60+
#define KSYMTAB_DATA(name, sec, ns) __KSYMTAB(name, name, sec, ns)
61+
1362
#define SYMBOL_CRC(sym, crc, sec) \
1463
asm(".section \"___kcrctab" sec "+" #sym "\",\"a\"" "\n" \
1564
"__crc_" #sym ":" "\n" \

include/linux/export.h

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef _LINUX_EXPORT_H
33
#define _LINUX_EXPORT_H
44

5+
#include <linux/compiler.h>
6+
#include <linux/linkage.h>
57
#include <linux/stringify.h>
68

79
/*
@@ -28,72 +30,41 @@ extern struct module __this_module;
2830
#else
2931
#define THIS_MODULE ((struct module *)0)
3032
#endif
33+
#endif /* __ASSEMBLY__ */
3134

32-
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
33-
#include <linux/compiler.h>
34-
/*
35-
* Emit the ksymtab entry as a pair of relative references: this reduces
36-
* the size by half on 64-bit architectures, and eliminates the need for
37-
* absolute relocations that require runtime processing on relocatable
38-
* kernels.
39-
*/
40-
#define __KSYMTAB_ENTRY(sym, sec) \
41-
__ADDRESSABLE(sym) \
42-
asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \
43-
" .balign 4 \n" \
44-
"__ksymtab_" #sym ": \n" \
45-
" .long " #sym "- . \n" \
46-
" .long __kstrtab_" #sym "- . \n" \
47-
" .long __kstrtabns_" #sym "- . \n" \
48-
" .previous \n")
49-
50-
struct kernel_symbol {
51-
int value_offset;
52-
int name_offset;
53-
int namespace_offset;
54-
};
35+
#ifdef CONFIG_64BIT
36+
#define __EXPORT_SYMBOL_REF(sym) \
37+
.balign 8 ASM_NL \
38+
.quad sym
5539
#else
56-
#define __KSYMTAB_ENTRY(sym, sec) \
57-
static const struct kernel_symbol __ksymtab_##sym \
58-
__attribute__((section("___ksymtab" sec "+" #sym), used)) \
59-
__aligned(sizeof(void *)) \
60-
= { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
61-
62-
struct kernel_symbol {
63-
unsigned long value;
64-
const char *name;
65-
const char *namespace;
66-
};
40+
#define __EXPORT_SYMBOL_REF(sym) \
41+
.balign 4 ASM_NL \
42+
.long sym
6743
#endif
6844

45+
#define ____EXPORT_SYMBOL(sym, license, ns) \
46+
.section ".export_symbol","a" ASM_NL \
47+
__export_symbol_##sym: ASM_NL \
48+
.asciz license ASM_NL \
49+
.asciz ns ASM_NL \
50+
__EXPORT_SYMBOL_REF(sym) ASM_NL \
51+
.previous
52+
6953
#ifdef __GENKSYMS__
7054

7155
#define ___EXPORT_SYMBOL(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
7256

57+
#elif defined(__ASSEMBLY__)
58+
59+
#define ___EXPORT_SYMBOL(sym, license, ns) \
60+
____EXPORT_SYMBOL(sym, license, ns)
61+
7362
#else
7463

75-
/*
76-
* For every exported symbol, do the following:
77-
*
78-
* - Put the name of the symbol and namespace (empty string "" for none) in
79-
* __ksymtab_strings.
80-
* - Place a struct kernel_symbol entry in the __ksymtab section.
81-
*
82-
* note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
83-
* section flag requires it. Use '%progbits' instead of '@progbits' since the
84-
* former apparently works on all arches according to the binutils source.
85-
*/
86-
#define ___EXPORT_SYMBOL(sym, sec, ns) \
87-
extern typeof(sym) sym; \
88-
extern const char __kstrtab_##sym[]; \
89-
extern const char __kstrtabns_##sym[]; \
90-
asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1 \n" \
91-
"__kstrtab_" #sym ": \n" \
92-
" .asciz \"" #sym "\" \n" \
93-
"__kstrtabns_" #sym ": \n" \
94-
" .asciz \"" ns "\" \n" \
95-
" .previous \n"); \
96-
__KSYMTAB_ENTRY(sym, sec)
64+
#define ___EXPORT_SYMBOL(sym, license, ns) \
65+
extern typeof(sym) sym; \
66+
__ADDRESSABLE(sym) \
67+
asm(__stringify(____EXPORT_SYMBOL(sym, license, ns)))
9768

9869
#endif
9970

@@ -117,9 +88,21 @@ struct kernel_symbol {
11788
* from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
11889
* discarded in the final link stage.
11990
*/
91+
92+
#ifdef __ASSEMBLY__
93+
94+
#define __ksym_marker(sym) \
95+
.section ".discard.ksym","a" ; \
96+
__ksym_marker_##sym: ; \
97+
.previous
98+
99+
#else
100+
120101
#define __ksym_marker(sym) \
121102
static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
122103

104+
#endif
105+
123106
#define __EXPORT_SYMBOL(sym, sec, ns) \
124107
__ksym_marker(sym); \
125108
__cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
@@ -148,10 +131,8 @@ struct kernel_symbol {
148131
#endif
149132

150133
#define EXPORT_SYMBOL(sym) _EXPORT_SYMBOL(sym, "")
151-
#define EXPORT_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym, "_gpl")
134+
#define EXPORT_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym, "GPL")
152135
#define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", __stringify(ns))
153-
#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "_gpl", __stringify(ns))
154-
155-
#endif /* !__ASSEMBLY__ */
136+
#define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "GPL", __stringify(ns))
156137

157138
#endif /* _LINUX_EXPORT_H */

include/linux/pm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ const struct dev_pm_ops name = { \
389389
#endif
390390

391391
#define EXPORT_DEV_PM_OPS(name) _EXPORT_DEV_PM_OPS(name, "", "")
392-
#define EXPORT_GPL_DEV_PM_OPS(name) _EXPORT_DEV_PM_OPS(name, "_gpl", "")
392+
#define EXPORT_GPL_DEV_PM_OPS(name) _EXPORT_DEV_PM_OPS(name, "GPL", "")
393393
#define EXPORT_NS_DEV_PM_OPS(name, ns) _EXPORT_DEV_PM_OPS(name, "", #ns)
394-
#define EXPORT_NS_GPL_DEV_PM_OPS(name, ns) _EXPORT_DEV_PM_OPS(name, "_gpl", #ns)
394+
#define EXPORT_NS_GPL_DEV_PM_OPS(name, ns) _EXPORT_DEV_PM_OPS(name, "GPL", #ns)
395395

396396
/*
397397
* Use this if you want to use the same suspend and resume callbacks for suspend

kernel/module/internal.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
/* Maximum number of characters written by module_flags() */
3333
#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4)
3434

35+
struct kernel_symbol {
36+
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
37+
int value_offset;
38+
int name_offset;
39+
int namespace_offset;
40+
#else
41+
unsigned long value;
42+
const char *name;
43+
const char *namespace;
44+
#endif
45+
};
46+
3547
extern struct mutex module_mutex;
3648
extern struct list_head modules;
3749

scripts/Makefile.build

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
163163
ifdef CONFIG_MODVERSIONS
164164
# When module versioning is enabled the following steps are executed:
165165
# o compile a <file>.o from <file>.c
166-
# o if <file>.o doesn't contain a __ksymtab version, i.e. does
166+
# o if <file>.o doesn't contain a __export_symbol_*, i.e. does
167167
# not export symbols, it's done.
168168
# o otherwise, we calculate symbol versions using the good old
169169
# genksyms on the preprocessed source and dump them into the .cmd file.
170170
# o modpost will extract versions from that file and create *.c files that will
171171
# be compiled and linked to the kernel and/or modules.
172172

173173
gen_symversions = \
174-
if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then \
174+
if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then \
175175
$(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
176176
>> $(dot-target).cmd; \
177177
fi
@@ -342,9 +342,7 @@ $(obj)/%.ll: $(src)/%.rs FORCE
342342
cmd_gensymtypes_S = \
343343
{ echo "\#include <linux/kernel.h>" ; \
344344
echo "\#include <asm/asm-prototypes.h>" ; \
345-
$(CPP) $(a_flags) $< | \
346-
grep "\<___EXPORT_SYMBOL\>" | \
347-
sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ; } | \
345+
$(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \
348346
$(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
349347

350348
quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@

scripts/check-local-export

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ BEGIN {
4646
{ symbol_types[$3]=$2 }
4747
4848
# append the exported symbol to the array
49-
($3 ~ /^__ksymtab_/) {
49+
($3 ~ /^__export_symbol_.*/) {
5050
export_symbols[i] = $3
51-
sub(/^__ksymtab_/, "", export_symbols[i])
51+
sub(/^__export_symbol_/, "", export_symbols[i])
5252
i++
5353
}
5454

0 commit comments

Comments
 (0)