xref: /linux-6.15/include/linux/export.h (revision ddb5cdba)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_EXPORT_H
3 #define _LINUX_EXPORT_H
4 
5 #include <linux/compiler.h>
6 #include <linux/linkage.h>
7 #include <linux/stringify.h>
8 
9 /*
10  * Export symbols from the kernel to modules.  Forked from module.h
11  * to reduce the amount of pointless cruft we feed to gcc when only
12  * exporting a simple symbol or two.
13  *
14  * Try not to add #includes here.  It slows compilation and makes kernel
15  * hackers place grumpy comments in header files.
16  */
17 
18 /*
19  * This comment block is used by fixdep. Please do not remove.
20  *
21  * When CONFIG_MODVERSIONS is changed from n to y, all source files having
22  * EXPORT_SYMBOL variants must be re-compiled because genksyms is run as a
23  * side effect of the *.o build rule.
24  */
25 
26 #ifndef __ASSEMBLY__
27 #ifdef MODULE
28 extern struct module __this_module;
29 #define THIS_MODULE (&__this_module)
30 #else
31 #define THIS_MODULE ((struct module *)0)
32 #endif
33 #endif /* __ASSEMBLY__ */
34 
35 #ifdef CONFIG_64BIT
36 #define __EXPORT_SYMBOL_REF(sym)			\
37 	.balign 8				ASM_NL	\
38 	.quad sym
39 #else
40 #define __EXPORT_SYMBOL_REF(sym)			\
41 	.balign 4				ASM_NL	\
42 	.long sym
43 #endif
44 
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 
53 #ifdef __GENKSYMS__
54 
55 #define ___EXPORT_SYMBOL(sym, sec, ns)	__GENKSYMS_EXPORT_SYMBOL(sym)
56 
57 #elif defined(__ASSEMBLY__)
58 
59 #define ___EXPORT_SYMBOL(sym, license, ns) \
60 	____EXPORT_SYMBOL(sym, license, ns)
61 
62 #else
63 
64 #define ___EXPORT_SYMBOL(sym, license, ns)			\
65 	extern typeof(sym) sym;					\
66 	__ADDRESSABLE(sym)					\
67 	asm(__stringify(____EXPORT_SYMBOL(sym, license, ns)))
68 
69 #endif
70 
71 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
72 
73 /*
74  * Allow symbol exports to be disabled completely so that C code may
75  * be reused in other execution contexts such as the UEFI stub or the
76  * decompressor.
77  */
78 #define __EXPORT_SYMBOL(sym, sec, ns)
79 
80 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
81 
82 #include <generated/autoksyms.h>
83 
84 /*
85  * For fine grained build dependencies, we want to tell the build system
86  * about each possible exported symbol even if they're not actually exported.
87  * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
88  * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
89  * discarded in the final link stage.
90  */
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 
101 #define __ksym_marker(sym)	\
102 	static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
103 
104 #endif
105 
106 #define __EXPORT_SYMBOL(sym, sec, ns)					\
107 	__ksym_marker(sym);						\
108 	__cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
109 #define __cond_export_sym(sym, sec, ns, conf)				\
110 	___cond_export_sym(sym, sec, ns, conf)
111 #define ___cond_export_sym(sym, sec, ns, enabled)			\
112 	__cond_export_sym_##enabled(sym, sec, ns)
113 #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
114 
115 #ifdef __GENKSYMS__
116 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
117 #else
118 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
119 #endif
120 
121 #else
122 
123 #define __EXPORT_SYMBOL(sym, sec, ns)	___EXPORT_SYMBOL(sym, sec, ns)
124 
125 #endif /* CONFIG_MODULES */
126 
127 #ifdef DEFAULT_SYMBOL_NAMESPACE
128 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
129 #else
130 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, "")
131 #endif
132 
133 #define EXPORT_SYMBOL(sym)		_EXPORT_SYMBOL(sym, "")
134 #define EXPORT_SYMBOL_GPL(sym)		_EXPORT_SYMBOL(sym, "GPL")
135 #define EXPORT_SYMBOL_NS(sym, ns)	__EXPORT_SYMBOL(sym, "", __stringify(ns))
136 #define EXPORT_SYMBOL_NS_GPL(sym, ns)	__EXPORT_SYMBOL(sym, "GPL", __stringify(ns))
137 
138 #endif /* _LINUX_EXPORT_H */
139