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