xref: /linux-6.15/include/linux/export.h (revision 69a94abb)
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	8					\n"	\
53 	    "__ksymtab_" #sym ":				\n"	\
54 	    "	.long	" #sym "- .				\n"	\
55 	    "	.long	__kstrtab_" #sym "- .			\n"	\
56 	    "	.previous					\n")
57 
58 struct kernel_symbol {
59 	int value_offset;
60 	int name_offset;
61 };
62 #else
63 #define __KSYMTAB_ENTRY(sym, sec)					\
64 	static const struct kernel_symbol __ksymtab_##sym		\
65 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
66 	= { (unsigned long)&sym, __kstrtab_##sym }
67 
68 struct kernel_symbol {
69 	unsigned long value;
70 	const char *name;
71 };
72 #endif
73 
74 #ifdef __GENKSYMS__
75 
76 #define ___EXPORT_SYMBOL(sym, sec)	__GENKSYMS_EXPORT_SYMBOL(sym)
77 
78 #else
79 
80 /* For every exported symbol, place a struct in the __ksymtab section */
81 #define ___EXPORT_SYMBOL(sym, sec)					\
82 	extern typeof(sym) sym;						\
83 	__CRC_SYMBOL(sym, sec)						\
84 	static const char __kstrtab_##sym[]				\
85 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
86 	= #sym;								\
87 	__KSYMTAB_ENTRY(sym, sec)
88 
89 #endif
90 
91 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
92 
93 /*
94  * Allow symbol exports to be disabled completely so that C code may
95  * be reused in other execution contexts such as the UEFI stub or the
96  * decompressor.
97  */
98 #define __EXPORT_SYMBOL(sym, sec)
99 
100 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
101 
102 #include <generated/autoksyms.h>
103 
104 /*
105  * For fine grained build dependencies, we want to tell the build system
106  * about each possible exported symbol even if they're not actually exported.
107  * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
108  * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
109  * discarded in the final link stage.
110  */
111 #define __ksym_marker(sym)	\
112 	static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
113 
114 #define __EXPORT_SYMBOL(sym, sec)				\
115 	__ksym_marker(sym);					\
116 	__cond_export_sym(sym, sec, __is_defined(__KSYM_##sym))
117 #define __cond_export_sym(sym, sec, conf)			\
118 	___cond_export_sym(sym, sec, conf)
119 #define ___cond_export_sym(sym, sec, enabled)			\
120 	__cond_export_sym_##enabled(sym, sec)
121 #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
122 #define __cond_export_sym_0(sym, sec) /* nothing */
123 
124 #else
125 
126 #define __EXPORT_SYMBOL(sym, sec)	___EXPORT_SYMBOL(sym, sec)
127 
128 #endif /* CONFIG_MODULES */
129 
130 #define EXPORT_SYMBOL(sym)		__EXPORT_SYMBOL(sym, "")
131 #define EXPORT_SYMBOL_GPL(sym)		__EXPORT_SYMBOL(sym, "_gpl")
132 #define EXPORT_SYMBOL_GPL_FUTURE(sym)	__EXPORT_SYMBOL(sym, "_gpl_future")
133 #ifdef CONFIG_UNUSED_SYMBOLS
134 #define EXPORT_UNUSED_SYMBOL(sym)	__EXPORT_SYMBOL(sym, "_unused")
135 #define EXPORT_UNUSED_SYMBOL_GPL(sym)	__EXPORT_SYMBOL(sym, "_unused_gpl")
136 #else
137 #define EXPORT_UNUSED_SYMBOL(sym)
138 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
139 #endif
140 
141 #endif /* !__ASSEMBLY__ */
142 
143 #endif /* _LINUX_EXPORT_H */
144