xref: /linux-6.15/include/linux/export.h (revision 8e2adc6a)
1 #ifndef _LINUX_EXPORT_H
2 #define _LINUX_EXPORT_H
3 
4 /*
5  * Export symbols from the kernel to modules.  Forked from module.h
6  * to reduce the amount of pointless cruft we feed to gcc when only
7  * exporting a simple symbol or two.
8  *
9  * Try not to add #includes here.  It slows compilation and makes kernel
10  * hackers place grumpy comments in header files.
11  */
12 
13 #ifndef __ASSEMBLY__
14 #ifdef MODULE
15 extern struct module __this_module;
16 #define THIS_MODULE (&__this_module)
17 #else
18 #define THIS_MODULE ((struct module *)0)
19 #endif
20 
21 #ifdef CONFIG_MODULES
22 
23 #define NS_SEPARATOR "."
24 
25 #if defined(__KERNEL__) && !defined(__GENKSYMS__)
26 #ifdef CONFIG_MODVERSIONS
27 /* Mark the CRC weak since genksyms apparently decides not to
28  * generate a checksums for some symbols */
29 #if defined(CONFIG_MODULE_REL_CRCS)
30 #define __CRC_SYMBOL(sym, sec)						\
31 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
32 	    "	.weak	__crc_" #sym "				\n"	\
33 	    "	.long	__crc_" #sym " - .			\n"	\
34 	    "	.previous					\n")
35 #else
36 #define __CRC_SYMBOL(sym, sec)						\
37 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
38 	    "	.weak	__crc_" #sym "				\n"	\
39 	    "	.long	__crc_" #sym "				\n"	\
40 	    "	.previous					\n")
41 #endif
42 #else
43 #define __CRC_SYMBOL(sym, sec)
44 #endif
45 
46 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
47 #include <linux/compiler.h>
48 /*
49  * Emit the ksymtab entry as a pair of relative references: this reduces
50  * the size by half on 64-bit architectures, and eliminates the need for
51  * absolute relocations that require runtime processing on relocatable
52  * kernels.
53  */
54 #define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
55 	__ADDRESSABLE(sym)						\
56 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
57 	    "	.balign	4					\n"	\
58 	    "__ksymtab_" #sym NS_SEPARATOR #ns ":		\n"	\
59 	    "	.long	" #sym "- .				\n"	\
60 	    "	.long	__kstrtab_" #sym "- .			\n"	\
61 	    "	.long	__kstrtab_ns_" #sym "- .		\n"	\
62 	    "	.previous					\n")
63 
64 #define __KSYMTAB_ENTRY(sym, sec)					\
65 	__ADDRESSABLE(sym)						\
66 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
67 	    "	.balign 4					\n"	\
68 	    "__ksymtab_" #sym ":				\n"	\
69 	    "	.long	" #sym "- .				\n"	\
70 	    "	.long	__kstrtab_" #sym "- .			\n"	\
71 	    "	.long	0 - .					\n"	\
72 	    "	.previous					\n")
73 
74 struct kernel_symbol {
75 	int value_offset;
76 	int name_offset;
77 	int namespace_offset;
78 };
79 #else
80 #define __KSYMTAB_ENTRY_NS(sym, sec, ns)				\
81 	static const struct kernel_symbol __ksymtab_##sym##__##ns	\
82 	asm("__ksymtab_" #sym NS_SEPARATOR #ns)				\
83 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
84 	__aligned(sizeof(void *))					\
85 	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtab_ns_##sym }
86 
87 #define __KSYMTAB_ENTRY(sym, sec)					\
88 	static const struct kernel_symbol __ksymtab_##sym		\
89 	asm("__ksymtab_" #sym)						\
90 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
91 	__aligned(sizeof(void *))					\
92 	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
93 
94 struct kernel_symbol {
95 	unsigned long value;
96 	const char *name;
97 	const char *namespace;
98 };
99 #endif
100 
101 #define ___export_symbol_common(sym, sec)				\
102 	extern typeof(sym) sym;						\
103 	__CRC_SYMBOL(sym, sec);						\
104 	static const char __kstrtab_##sym[]				\
105 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
106 	= #sym								\
107 
108 /* For every exported symbol, place a struct in the __ksymtab section */
109 #define ___EXPORT_SYMBOL_NS(sym, sec, ns)				\
110 	___export_symbol_common(sym, sec);				\
111 	static const char __kstrtab_ns_##sym[]				\
112 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
113 	= #ns;								\
114 	__KSYMTAB_ENTRY_NS(sym, sec, ns)
115 
116 #define ___EXPORT_SYMBOL(sym, sec)					\
117 	___export_symbol_common(sym, sec);				\
118 	__KSYMTAB_ENTRY(sym, sec)
119 
120 #if defined(__DISABLE_EXPORTS)
121 
122 /*
123  * Allow symbol exports to be disabled completely so that C code may
124  * be reused in other execution contexts such as the UEFI stub or the
125  * decompressor.
126  */
127 #define __EXPORT_SYMBOL_NS(sym, sec, ns)
128 #define __EXPORT_SYMBOL(sym, sec)
129 
130 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
131 
132 #include <generated/autoksyms.h>
133 
134 /*
135  * For fine grained build dependencies, we want to tell the build system
136  * about each possible exported symbol even if they're not actually exported.
137  * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
138  * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
139  * discarded in the final link stage.
140  */
141 #define __ksym_marker(sym)	\
142 	static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
143 
144 #define __EXPORT_SYMBOL(sym, sec)				\
145 	__ksym_marker(sym);					\
146 	__cond_export_sym(sym, sec, __is_defined(__KSYM_##sym))
147 #define __cond_export_sym(sym, sec, conf)			\
148 	___cond_export_sym(sym, sec, conf)
149 #define ___cond_export_sym(sym, sec, enabled)			\
150 	__cond_export_sym_##enabled(sym, sec)
151 #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
152 #define __cond_export_sym_0(sym, sec) /* nothing */
153 
154 #define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
155 	__ksym_marker(sym);						\
156 	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
157 #define __cond_export_ns_sym(sym, sec, ns, conf)			\
158 	___cond_export_ns_sym(sym, sec, ns, conf)
159 #define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
160 	__cond_export_ns_sym_##enabled(sym, sec, ns)
161 #define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
162 #define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
163 
164 #else
165 #define __EXPORT_SYMBOL_NS ___EXPORT_SYMBOL_NS
166 #define __EXPORT_SYMBOL ___EXPORT_SYMBOL
167 #endif
168 
169 #ifdef DEFAULT_SYMBOL_NAMESPACE
170 #undef __EXPORT_SYMBOL
171 #define __EXPORT_SYMBOL(sym, sec)				\
172 	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
173 #endif
174 
175 #define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "")
176 #define EXPORT_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_gpl")
177 #define EXPORT_SYMBOL_GPL_FUTURE(sym) __EXPORT_SYMBOL(sym, "_gpl_future")
178 #define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL_NS(sym, "", ns)
179 #define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL_NS(sym, "_gpl", ns)
180 
181 #ifdef CONFIG_UNUSED_SYMBOLS
182 #define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
183 #define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
184 #else
185 #define EXPORT_UNUSED_SYMBOL(sym)
186 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
187 #endif
188 
189 #endif	/* __KERNEL__ && !__GENKSYMS__ */
190 
191 #if defined(__GENKSYMS__)
192 /*
193  * When we're running genksyms, ignore the namespace and make the _NS
194  * variants look like the normal ones. There are two reasons for this:
195  * 1) In the normal definition of EXPORT_SYMBOL_NS, the 'ns' macro
196  *    argument is itself not expanded because it's always tokenized or
197  *    concatenated; but when running genksyms, a blank definition of the
198  *    macro does allow the argument to be expanded; if a namespace
199  *    happens to collide with a #define, this can cause issues.
200  * 2) There's no need to modify genksyms to deal with the _NS variants
201  */
202 #define EXPORT_SYMBOL_NS(sym, ns) EXPORT_SYMBOL(sym)
203 #define EXPORT_SYMBOL_NS_GPL(sym, ns) EXPORT_SYMBOL_GPL(sym)
204 #endif
205 
206 #else /* !CONFIG_MODULES... */
207 
208 #define EXPORT_SYMBOL(sym)
209 #define EXPORT_SYMBOL_NS(sym, ns)
210 #define EXPORT_SYMBOL_NS_GPL(sym, ns)
211 #define EXPORT_SYMBOL_GPL(sym)
212 #define EXPORT_SYMBOL_GPL_FUTURE(sym)
213 #define EXPORT_UNUSED_SYMBOL(sym)
214 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
215 
216 #endif /* CONFIG_MODULES */
217 #endif /* !__ASSEMBLY__ */
218 
219 #endif /* _LINUX_EXPORT_H */
220