xref: /openbmc/linux/include/linux/export.h (revision 7cffcade)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_EXPORT_H
3 #define _LINUX_EXPORT_H
4 
5 #include <linux/stringify.h>
6 
7 /*
8  * Export symbols from the kernel to modules.  Forked from module.h
9  * to reduce the amount of pointless cruft we feed to gcc when only
10  * exporting a simple symbol or two.
11  *
12  * Try not to add #includes here.  It slows compilation and makes kernel
13  * hackers place grumpy comments in header files.
14  */
15 
16 /*
17  * This comment block is used by fixdep. Please do not remove.
18  *
19  * When CONFIG_MODVERSIONS is changed from n to y, all source files having
20  * EXPORT_SYMBOL variants must be re-compiled because genksyms is run as a
21  * side effect of the *.o build rule.
22  */
23 
24 #ifndef __ASSEMBLY__
25 #ifdef MODULE
26 extern struct module __this_module;
27 #define THIS_MODULE (&__this_module)
28 #else
29 #define THIS_MODULE ((struct module *)0)
30 #endif
31 
32 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
33 #include <linux/compiler.h>
34 /*
35  * Emit the ksymtab entry as a pair of relative references: this reduces
36  * the size by half on 64-bit architectures, and eliminates the need for
37  * absolute relocations that require runtime processing on relocatable
38  * kernels.
39  */
40 #define __KSYMTAB_ENTRY(sym, sec)					\
41 	__ADDRESSABLE(sym)						\
42 	asm("	.section \"___ksymtab" sec "+" #sym "\", \"a\"	\n"	\
43 	    "	.balign	4					\n"	\
44 	    "__ksymtab_" #sym ":				\n"	\
45 	    "	.long	" #sym "- .				\n"	\
46 	    "	.long	__kstrtab_" #sym "- .			\n"	\
47 	    "	.long	__kstrtabns_" #sym "- .			\n"	\
48 	    "	.previous					\n")
49 
50 struct kernel_symbol {
51 	int value_offset;
52 	int name_offset;
53 	int namespace_offset;
54 };
55 #else
56 #define __KSYMTAB_ENTRY(sym, sec)					\
57 	static const struct kernel_symbol __ksymtab_##sym		\
58 	__attribute__((section("___ksymtab" sec "+" #sym), used))	\
59 	__aligned(sizeof(void *))					\
60 	= { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
61 
62 struct kernel_symbol {
63 	unsigned long value;
64 	const char *name;
65 	const char *namespace;
66 };
67 #endif
68 
69 #ifdef __GENKSYMS__
70 
71 #define ___EXPORT_SYMBOL(sym, sec, ns)	__GENKSYMS_EXPORT_SYMBOL(sym)
72 
73 #else
74 
75 /*
76  * For every exported symbol, do the following:
77  *
78  * - Put the name of the symbol and namespace (empty string "" for none) in
79  *   __ksymtab_strings.
80  * - Place a struct kernel_symbol entry in the __ksymtab section.
81  *
82  * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
83  * section flag requires it. Use '%progbits' instead of '@progbits' since the
84  * former apparently works on all arches according to the binutils source.
85  */
86 #define ___EXPORT_SYMBOL(sym, sec, ns)						\
87 	extern typeof(sym) sym;							\
88 	extern const char __kstrtab_##sym[];					\
89 	extern const char __kstrtabns_##sym[];					\
90 	asm("	.section \"__ksymtab_strings\",\"aMS\",%progbits,1	\n"	\
91 	    "__kstrtab_" #sym ":					\n"	\
92 	    "	.asciz 	\"" #sym "\"					\n"	\
93 	    "__kstrtabns_" #sym ":					\n"	\
94 	    "	.asciz 	\"" ns "\"					\n"	\
95 	    "	.previous						\n");	\
96 	__KSYMTAB_ENTRY(sym, sec)
97 
98 #endif
99 
100 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
101 
102 /*
103  * Allow symbol exports to be disabled completely so that C code may
104  * be reused in other execution contexts such as the UEFI stub or the
105  * decompressor.
106  */
107 #define __EXPORT_SYMBOL(sym, sec, ns)
108 
109 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
110 
111 #include <generated/autoksyms.h>
112 
113 /*
114  * For fine grained build dependencies, we want to tell the build system
115  * about each possible exported symbol even if they're not actually exported.
116  * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
117  * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
118  * discarded in the final link stage.
119  */
120 #define __ksym_marker(sym)	\
121 	static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
122 
123 #define __EXPORT_SYMBOL(sym, sec, ns)					\
124 	__ksym_marker(sym);						\
125 	__cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
126 #define __cond_export_sym(sym, sec, ns, conf)				\
127 	___cond_export_sym(sym, sec, ns, conf)
128 #define ___cond_export_sym(sym, sec, ns, enabled)			\
129 	__cond_export_sym_##enabled(sym, sec, ns)
130 #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
131 
132 #ifdef __GENKSYMS__
133 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
134 #else
135 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
136 #endif
137 
138 #else
139 
140 #define __EXPORT_SYMBOL(sym, sec, ns)	___EXPORT_SYMBOL(sym, sec, ns)
141 
142 #endif /* CONFIG_MODULES */
143 
144 #ifdef DEFAULT_SYMBOL_NAMESPACE
145 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
146 #else
147 #define _EXPORT_SYMBOL(sym, sec)	__EXPORT_SYMBOL(sym, sec, "")
148 #endif
149 
150 #define EXPORT_SYMBOL(sym)		_EXPORT_SYMBOL(sym, "")
151 #define EXPORT_SYMBOL_GPL(sym)		_EXPORT_SYMBOL(sym, "_gpl")
152 #define EXPORT_SYMBOL_NS(sym, ns)	__EXPORT_SYMBOL(sym, "", __stringify(ns))
153 #define EXPORT_SYMBOL_NS_GPL(sym, ns)	__EXPORT_SYMBOL(sym, "_gpl", __stringify(ns))
154 
155 #endif /* !__ASSEMBLY__ */
156 
157 #endif /* _LINUX_EXPORT_H */
158