xref: /linux-6.15/include/linux/export.h (revision c3a6cf19)
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_MODVERSIONS
22 /* Mark the CRC weak since genksyms apparently decides not to
23  * generate a checksums for some symbols */
24 #if defined(CONFIG_MODULE_REL_CRCS)
25 #define __CRC_SYMBOL(sym, sec)						\
26 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
27 	    "	.weak	__crc_" #sym "				\n"	\
28 	    "	.long	__crc_" #sym " - .			\n"	\
29 	    "	.previous					\n")
30 #else
31 #define __CRC_SYMBOL(sym, sec)						\
32 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
33 	    "	.weak	__crc_" #sym "				\n"	\
34 	    "	.long	__crc_" #sym "				\n"	\
35 	    "	.previous					\n")
36 #endif
37 #else
38 #define __CRC_SYMBOL(sym, sec)
39 #endif
40 
41 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
42 #include <linux/compiler.h>
43 /*
44  * Emit the ksymtab entry as a pair of relative references: this reduces
45  * the size by half on 64-bit architectures, and eliminates the need for
46  * absolute relocations that require runtime processing on relocatable
47  * kernels.
48  */
49 #define __KSYMTAB_ENTRY(sym, sec)					\
50 	__ADDRESSABLE(sym)						\
51 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
52 	    "	.balign	4					\n"	\
53 	    "__ksymtab_" #sym ":				\n"	\
54 	    "	.long	" #sym "- .				\n"	\
55 	    "	.long	__kstrtab_" #sym "- .			\n"	\
56 	    "	.long	__kstrtabns_" #sym "- .			\n"	\
57 	    "	.previous					\n")
58 
59 struct kernel_symbol {
60 	int value_offset;
61 	int name_offset;
62 	int namespace_offset;
63 };
64 #else
65 #define __KSYMTAB_ENTRY(sym, sec)					\
66 	static const struct kernel_symbol __ksymtab_##sym		\
67 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
68 	__aligned(sizeof(void *))					\
69 	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
70 
71 struct kernel_symbol {
72 	unsigned long value;
73 	const char *name;
74 	const char *namespace;
75 };
76 #endif
77 
78 #ifdef __GENKSYMS__
79 
80 #define ___EXPORT_SYMBOL(sym, sec, ns)	__GENKSYMS_EXPORT_SYMBOL(sym)
81 
82 #else
83 
84 /* For every exported symbol, place a struct in the __ksymtab section */
85 #define ___EXPORT_SYMBOL(sym, sec, ns)					\
86 	extern typeof(sym) sym;						\
87 	__CRC_SYMBOL(sym, sec);						\
88 	static const char __kstrtab_##sym[]				\
89 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
90 	= #sym;								\
91 	static const char __kstrtabns_##sym[]				\
92 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
93 	= ns;								\
94 	__KSYMTAB_ENTRY(sym, sec)
95 
96 #endif
97 
98 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
99 
100 /*
101  * Allow symbol exports to be disabled completely so that C code may
102  * be reused in other execution contexts such as the UEFI stub or the
103  * decompressor.
104  */
105 #define __EXPORT_SYMBOL(sym, sec, ns)
106 
107 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
108 
109 #include <generated/autoksyms.h>
110 
111 /*
112  * For fine grained build dependencies, we want to tell the build system
113  * about each possible exported symbol even if they're not actually exported.
114  * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
115  * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
116  * discarded in the final link stage.
117  */
118 #define __ksym_marker(sym)	\
119 	static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
120 
121 #define __EXPORT_SYMBOL(sym, sec, ns)					\
122 	__ksym_marker(sym);						\
123 	__cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
124 #define __cond_export_sym(sym, sec, ns, conf)				\
125 	___cond_export_sym(sym, sec, ns, conf)
126 #define ___cond_export_sym(sym, sec, ns, enabled)			\
127 	__cond_export_sym_##enabled(sym, sec, ns)
128 #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
129 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
130 
131 #else
132 
133 #define __EXPORT_SYMBOL(sym, sec, ns)	___EXPORT_SYMBOL(sym, sec, ns)
134 
135 #endif /* CONFIG_MODULES */
136 
137 #ifdef DEFAULT_SYMBOL_NAMESPACE
138 #include <linux/stringify.h>
139 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
140 #else
141 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, "")
142 #endif
143 
144 #define EXPORT_SYMBOL(sym)		_EXPORT_SYMBOL(sym, "")
145 #define EXPORT_SYMBOL_GPL(sym)		_EXPORT_SYMBOL(sym, "_gpl")
146 #define EXPORT_SYMBOL_GPL_FUTURE(sym)	_EXPORT_SYMBOL(sym, "_gpl_future")
147 #define EXPORT_SYMBOL_NS(sym, ns)	__EXPORT_SYMBOL(sym, "", #ns)
148 #define EXPORT_SYMBOL_NS_GPL(sym, ns)	__EXPORT_SYMBOL(sym, "_gpl", #ns)
149 
150 #ifdef CONFIG_UNUSED_SYMBOLS
151 #define EXPORT_UNUSED_SYMBOL(sym)	_EXPORT_SYMBOL(sym, "_unused")
152 #define EXPORT_UNUSED_SYMBOL_GPL(sym)	_EXPORT_SYMBOL(sym, "_unused_gpl")
153 #else
154 #define EXPORT_UNUSED_SYMBOL(sym)
155 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
156 #endif
157 
158 #endif /* !__ASSEMBLY__ */
159 
160 #endif /* _LINUX_EXPORT_H */
161