xref: /linux/include/linux/export.h (revision 52338415)
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_NS(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 #define __KSYMTAB_ENTRY(sym, sec)					\
60 	__ADDRESSABLE(sym)						\
61 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
62 	    "	.balign 4					\n"	\
63 	    "__ksymtab_" #sym ":				\n"	\
64 	    "	.long	" #sym "- .				\n"	\
65 	    "	.long	__kstrtab_" #sym "- .			\n"	\
66 	    "	.long	0					\n"	\
67 	    "	.previous					\n")
68 
69 struct kernel_symbol {
70 	int value_offset;
71 	int name_offset;
72 	int namespace_offset;
73 };
74 #else
75 #define __KSYMTAB_ENTRY_NS(sym, sec)					\
76 	static const struct kernel_symbol __ksymtab_##sym		\
77 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
78 	__aligned(sizeof(void *))					\
79 	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
80 
81 #define __KSYMTAB_ENTRY(sym, sec)					\
82 	static const struct kernel_symbol __ksymtab_##sym		\
83 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
84 	__aligned(sizeof(void *))					\
85 	= { (unsigned long)&sym, __kstrtab_##sym, NULL }
86 
87 struct kernel_symbol {
88 	unsigned long value;
89 	const char *name;
90 	const char *namespace;
91 };
92 #endif
93 
94 #ifdef __GENKSYMS__
95 
96 #define ___EXPORT_SYMBOL(sym,sec)	__GENKSYMS_EXPORT_SYMBOL(sym)
97 #define ___EXPORT_SYMBOL_NS(sym,sec,ns)	__GENKSYMS_EXPORT_SYMBOL(sym)
98 
99 #else
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 __kstrtabns_##sym[]				\
112 	__attribute__((section("__ksymtab_strings"), used, aligned(1)))	\
113 	= #ns;								\
114 	__KSYMTAB_ENTRY_NS(sym, sec)
115 
116 #define ___EXPORT_SYMBOL(sym, sec)					\
117 	___export_symbol_common(sym, sec);				\
118 	__KSYMTAB_ENTRY(sym, sec)
119 
120 #endif
121 
122 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
123 
124 /*
125  * Allow symbol exports to be disabled completely so that C code may
126  * be reused in other execution contexts such as the UEFI stub or the
127  * decompressor.
128  */
129 #define __EXPORT_SYMBOL_NS(sym, sec, ns)
130 #define __EXPORT_SYMBOL(sym, sec)
131 
132 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
133 
134 #include <generated/autoksyms.h>
135 
136 /*
137  * For fine grained build dependencies, we want to tell the build system
138  * about each possible exported symbol even if they're not actually exported.
139  * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
140  * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
141  * discarded in the final link stage.
142  */
143 #define __ksym_marker(sym)	\
144 	static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
145 
146 #define __EXPORT_SYMBOL(sym, sec)				\
147 	__ksym_marker(sym);					\
148 	__cond_export_sym(sym, sec, __is_defined(__KSYM_##sym))
149 #define __cond_export_sym(sym, sec, conf)			\
150 	___cond_export_sym(sym, sec, conf)
151 #define ___cond_export_sym(sym, sec, enabled)			\
152 	__cond_export_sym_##enabled(sym, sec)
153 #define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
154 #define __cond_export_sym_0(sym, sec) /* nothing */
155 
156 #define __EXPORT_SYMBOL_NS(sym, sec, ns)				\
157 	__ksym_marker(sym);						\
158 	__cond_export_ns_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
159 #define __cond_export_ns_sym(sym, sec, ns, conf)			\
160 	___cond_export_ns_sym(sym, sec, ns, conf)
161 #define ___cond_export_ns_sym(sym, sec, ns, enabled)			\
162 	__cond_export_ns_sym_##enabled(sym, sec, ns)
163 #define __cond_export_ns_sym_1(sym, sec, ns) ___EXPORT_SYMBOL_NS(sym, sec, ns)
164 #define __cond_export_ns_sym_0(sym, sec, ns) /* nothing */
165 
166 #else
167 
168 #define __EXPORT_SYMBOL_NS(sym,sec,ns)	___EXPORT_SYMBOL_NS(sym,sec,ns)
169 #define __EXPORT_SYMBOL(sym,sec)	___EXPORT_SYMBOL(sym,sec)
170 
171 #endif /* CONFIG_MODULES */
172 
173 #ifdef DEFAULT_SYMBOL_NAMESPACE
174 #undef __EXPORT_SYMBOL
175 #define __EXPORT_SYMBOL(sym, sec)				\
176 	__EXPORT_SYMBOL_NS(sym, sec, DEFAULT_SYMBOL_NAMESPACE)
177 #endif
178 
179 #define EXPORT_SYMBOL(sym)		__EXPORT_SYMBOL(sym, "")
180 #define EXPORT_SYMBOL_GPL(sym)		__EXPORT_SYMBOL(sym, "_gpl")
181 #define EXPORT_SYMBOL_GPL_FUTURE(sym)	__EXPORT_SYMBOL(sym, "_gpl_future")
182 #define EXPORT_SYMBOL_NS(sym, ns)	__EXPORT_SYMBOL_NS(sym, "", ns)
183 #define EXPORT_SYMBOL_NS_GPL(sym, ns)	__EXPORT_SYMBOL_NS(sym, "_gpl", ns)
184 
185 #ifdef CONFIG_UNUSED_SYMBOLS
186 #define EXPORT_UNUSED_SYMBOL(sym)	__EXPORT_SYMBOL(sym, "_unused")
187 #define EXPORT_UNUSED_SYMBOL_GPL(sym)	__EXPORT_SYMBOL(sym, "_unused_gpl")
188 #else
189 #define EXPORT_UNUSED_SYMBOL(sym)
190 #define EXPORT_UNUSED_SYMBOL_GPL(sym)
191 #endif
192 
193 #endif /* !__ASSEMBLY__ */
194 
195 #endif /* _LINUX_EXPORT_H */
196