• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_EXPORT_H
3 #define _LINUX_EXPORT_H
4 
5 /*
6  * Export symbols from the kernel to modules.  Forked from module.h
7  * to reduce the amount of pointless cruft we feed to gcc when only
8  * exporting a simple symbol or two.
9  *
10  * Try not to add #includes here.  It slows compilation and makes kernel
11  * hackers place grumpy comments in header files.
12  */
13 
14 #ifndef __ASSEMBLY__
15 #ifdef MODULE
16 extern struct module __this_module;
17 #define THIS_MODULE (&__this_module)
18 #else
19 #define THIS_MODULE ((struct module *)0)
20 #endif
21 
22 #ifdef CONFIG_MODVERSIONS
23 /* Mark the CRC weak since genksyms apparently decides not to
24  * generate a checksums for some symbols */
25 #if defined(CONFIG_MODULE_REL_CRCS)
26 #define __CRC_SYMBOL(sym, sec)						\
27 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
28 	    "	.weak	__crc_" #sym "				\n"	\
29 	    "	.long	__crc_" #sym " - .			\n"	\
30 	    "	.previous					\n")
31 #else
32 #define __CRC_SYMBOL(sym, sec)						\
33 	asm("	.section \"___kcrctab" sec "+" #sym "\", \"a\"	\n"	\
34 	    "	.weak	__crc_" #sym "				\n"	\
35 	    "	.long	__crc_" #sym "				\n"	\
36 	    "	.previous					\n")
37 #endif
38 #else
39 #define __CRC_SYMBOL(sym, sec)
40 #endif
41 
42 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
43 #include <linux/compiler.h>
44 /*
45  * Emit the ksymtab entry as a pair of relative references: this reduces
46  * the size by half on 64-bit architectures, and eliminates the need for
47  * absolute relocations that require runtime processing on relocatable
48  * kernels.
49  */
50 #define __KSYMTAB_ENTRY(sym, sec)					\
51 	__ADDRESSABLE(sym)						\
52 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
53 	    "	.balign	4					\n"	\
54 	    "__ksymtab_" #sym ":				\n"	\
55 	    "	.long	" #sym "- .				\n"	\
56 	    "	.long	__kstrtab_" #sym "- .			\n"	\
57 	    "	.long	__kstrtabns_" #sym "- .			\n"	\
58 	    "	.previous					\n")
59 
60 struct kernel_symbol {
61 	int value_offset;
62 	int name_offset;
63 	int namespace_offset;
64 };
65 #else
66 #ifdef CONFIG_CFI_CLANG
67 #include <linux/compiler.h>
68 /*
69  * With -fno-sanitize-cfi-canonical-jump-tables, the compiler replaces
70  * references to functions with jump table addresses. Use inline assembly
71  * to emit ksymtab entries that point to the actual function instead.
72  */
73 #define ___KSYMTAB_ENTRY(sym, sec, size)				\
74 	__ADDRESSABLE(sym)						\
75 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
76 	    "	.balign " #size "				\n"	\
77 	    "__ksymtab_" #sym ":				\n"	\
78 	    "	." #size "byte	" #sym "			\n"	\
79 	    "	." #size "byte	__kstrtab_" #sym "		\n"	\
80 	    "	." #size "byte	__kstrtabns_" #sym "		\n"	\
81 	    "	.previous					\n")
82 
83 #ifdef CONFIG_64BIT
84 #define __KSYMTAB_ENTRY(sym, sec)	___KSYMTAB_ENTRY(sym, sec, 8)
85 #else
86 #define __KSYMTAB_ENTRY(sym, sec)	___KSYMTAB_ENTRY(sym, sec, 4)
87 #endif
88 
89 #else /* !CONFIG_CFI_CLANG */
90 
91 #define __KSYMTAB_ENTRY(sym, sec)					\
92 	static const struct kernel_symbol __ksymtab_##sym		\
93 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
94 	__aligned(sizeof(void *))					\
95 	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
96 
97 #endif
98 
99 struct kernel_symbol {
100 	unsigned long value;
101 	const char *name;
102 	const char *namespace;
103 };
104 #endif
105 
106 #ifdef __GENKSYMS__
107 
108 #define ___EXPORT_SYMBOL(sym, sec, ns)	__GENKSYMS_EXPORT_SYMBOL(sym)
109 
110 #else
111 
112 /*
113  * For every exported symbol, do the following:
114  *
115  * - If applicable, place a CRC entry in the __kcrctab section.
116  * - Put the name of the symbol and namespace (empty string "" for none) in
117  *   __ksymtab_strings.
118  * - Place a struct kernel_symbol entry in the __ksymtab section.
119  *
120  * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
121  * section flag requires it. Use '%progbits' instead of '@progbits' since the
122  * former apparently works on all arches according to the binutils source.
123  */
124 #define ___EXPORT_SYMBOL(sym, sec, ns)						\
125 	extern typeof(sym) sym;							\
126 	extern const char __kstrtab_##sym[];					\
127 	extern const char __kstrtabns_##sym[];					\
128 	__CRC_SYMBOL(sym, sec);							\
129 	asm("	.section \"__ksymtab_strings\",\"aMS\",%progbits,1	\n"	\
130 	    "__kstrtab_" #sym ":					\n"	\
131 	    "	.asciz 	\"" #sym "\"					\n"	\
132 	    "__kstrtabns_" #sym ":					\n"	\
133 	    "	.asciz 	\"" ns "\"					\n"	\
134 	    "	.previous						\n");	\
135 	__KSYMTAB_ENTRY(sym, sec)
136 
137 #endif
138 
139 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
140 
141 /*
142  * Allow symbol exports to be disabled completely so that C code may
143  * be reused in other execution contexts such as the UEFI stub or the
144  * decompressor.
145  */
146 #define __EXPORT_SYMBOL(sym, sec, ns)
147 
148 #elif defined(CONFIG_TRIM_UNUSED_KSYMS) && !defined(MODULE)
149 
150 #include <generated/autoksyms.h>
151 
152 /*
153  * For fine grained build dependencies, we want to tell the build system
154  * about each possible exported symbol even if they're not actually exported.
155  * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
156  * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
157  * discarded in the final link stage.
158  */
159 #define __ksym_marker(sym)	\
160 	static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
161 
162 #define __EXPORT_SYMBOL(sym, sec, ns)					\
163 	__ksym_marker(sym);						\
164 	__cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
165 #define __cond_export_sym(sym, sec, ns, conf)				\
166 	___cond_export_sym(sym, sec, ns, conf)
167 #define ___cond_export_sym(sym, sec, ns, enabled)			\
168 	__cond_export_sym_##enabled(sym, sec, ns)
169 #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
170 
171 #ifdef __GENKSYMS__
172 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
173 #else
174 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
175 #endif
176 
177 #else
178 
179 #define __EXPORT_SYMBOL(sym, sec, ns)	___EXPORT_SYMBOL(sym, sec, ns)
180 
181 #endif /* CONFIG_MODULES */
182 
183 #ifdef DEFAULT_SYMBOL_NAMESPACE
184 #include <linux/stringify.h>
185 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
186 #else
187 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, "")
188 #endif
189 
190 #define EXPORT_SYMBOL(sym)		_EXPORT_SYMBOL(sym, "")
191 #define EXPORT_SYMBOL_GPL(sym)		_EXPORT_SYMBOL(sym, "_gpl")
192 #define EXPORT_SYMBOL_GPL_FUTURE(sym)	_EXPORT_SYMBOL(sym, "_gpl_future")
193 #define _EXPORT_SYMBOL_NS(sym, ns)	__EXPORT_SYMBOL(sym, "", #ns)
194 #define _EXPORT_SYMBOL_NS_GPL(sym, ns)	__EXPORT_SYMBOL(sym, "_gpl", #ns)
195 #define EXPORT_SYMBOL_NS(sym, ns)	_EXPORT_SYMBOL_NS(sym, ns)
196 #define EXPORT_SYMBOL_NS_GPL(sym, ns)	_EXPORT_SYMBOL_NS_GPL(sym, ns)
197 
198 #ifdef CONFIG_UNUSED_SYMBOLS
199 #define EXPORT_UNUSED_SYMBOL(sym)	_EXPORT_SYMBOL(sym, "_unused")
200 #define EXPORT_UNUSED_SYMBOL_GPL(sym)	_EXPORT_SYMBOL(sym, "_unused_gpl")
201 #else
202 #define EXPORT_UNUSED_SYMBOL(sym)
203 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
204 #endif
205 
206 #endif /* !__ASSEMBLY__ */
207 
208 #endif /* _LINUX_EXPORT_H */
209