xref: /openbmc/linux/include/linux/export.h (revision df202b452fe6c6d6f1351bad485e2367ef1e644e)
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 /*
15  * This comment block is used by fixdep. Please do not remove.
16  *
17  * When CONFIG_MODVERSIONS is changed from n to y, all source files having
18  * EXPORT_SYMBOL variants must be re-compiled because genksyms is run as a
19  * side effect of the *.o build rule.
20  */
21 
22 #ifndef __ASSEMBLY__
23 #ifdef MODULE
24 extern struct module __this_module;
25 #define THIS_MODULE (&__this_module)
26 #else
27 #define THIS_MODULE ((struct module *)0)
28 #endif
29 
30 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
31 #include <linux/compiler.h>
32 /*
33  * Emit the ksymtab entry as a pair of relative references: this reduces
34  * the size by half on 64-bit architectures, and eliminates the need for
35  * absolute relocations that require runtime processing on relocatable
36  * kernels.
37  */
38 #define __KSYMTAB_ENTRY(sym, sec)					\
39 	__ADDRESSABLE(sym)						\
40 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
41 	    "	.balign	4					\n"	\
42 	    "__ksymtab_" #sym ":				\n"	\
43 	    "	.long	" #sym "- .				\n"	\
44 	    "	.long	__kstrtab_" #sym "- .			\n"	\
45 	    "	.long	__kstrtabns_" #sym "- .			\n"	\
46 	    "	.previous					\n")
47 
48 struct kernel_symbol {
49 	int value_offset;
50 	int name_offset;
51 	int namespace_offset;
52 };
53 #else
54 #define __KSYMTAB_ENTRY(sym, sec)					\
55 	static const struct kernel_symbol __ksymtab_##sym		\
56 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
57 	__aligned(sizeof(void *))					\
58 	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
59 
60 struct kernel_symbol {
61 	unsigned long value;
62 	const char *name;
63 	const char *namespace;
64 };
65 #endif
66 
67 #ifdef __GENKSYMS__
68 
69 #define ___EXPORT_SYMBOL(sym, sec, ns)	__GENKSYMS_EXPORT_SYMBOL(sym)
70 
71 #else
72 
73 /*
74  * For every exported symbol, do the following:
75  *
76  * - Put the name of the symbol and namespace (empty string "" for none) in
77  *   __ksymtab_strings.
78  * - Place a struct kernel_symbol entry in the __ksymtab section.
79  *
80  * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
81  * section flag requires it. Use '%progbits' instead of '@progbits' since the
82  * former apparently works on all arches according to the binutils source.
83  */
84 #define ___EXPORT_SYMBOL(sym, sec, ns)						\
85 	extern typeof(sym) sym;							\
86 	extern const char __kstrtab_##sym[];					\
87 	extern const char __kstrtabns_##sym[];					\
88 	asm("	.section \"__ksymtab_strings\",\"aMS\",%progbits,1	\n"	\
89 	    "__kstrtab_" #sym ":					\n"	\
90 	    "	.asciz 	\"" #sym "\"					\n"	\
91 	    "__kstrtabns_" #sym ":					\n"	\
92 	    "	.asciz 	\"" ns "\"					\n"	\
93 	    "	.previous						\n");	\
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 
130 #ifdef __GENKSYMS__
131 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
132 #else
133 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
134 #endif
135 
136 #else
137 
138 #define __EXPORT_SYMBOL(sym, sec, ns)	___EXPORT_SYMBOL(sym, sec, ns)
139 
140 #endif /* CONFIG_MODULES */
141 
142 #ifdef DEFAULT_SYMBOL_NAMESPACE
143 #include <linux/stringify.h>
144 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
145 #else
146 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, "")
147 #endif
148 
149 #define EXPORT_SYMBOL(sym)		_EXPORT_SYMBOL(sym, "")
150 #define EXPORT_SYMBOL_GPL(sym)		_EXPORT_SYMBOL(sym, "_gpl")
151 #define EXPORT_SYMBOL_NS(sym, ns)	__EXPORT_SYMBOL(sym, "", #ns)
152 #define EXPORT_SYMBOL_NS_GPL(sym, ns)	__EXPORT_SYMBOL(sym, "_gpl", #ns)
153 
154 #endif /* !__ASSEMBLY__ */
155 
156 #endif /* _LINUX_EXPORT_H */
157