1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_EXPORT_H
3 #define _LINUX_EXPORT_H
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.
10 * Try not to add #includes here. It slows compilation and makes kernel
11 * hackers place grumpy comments in header files.
15 * This comment block is used by fixdep. Please do not remove.
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.
24 extern struct module __this_module;
25 #define THIS_MODULE (&__this_module)
27 #define THIS_MODULE ((struct module *)0)
30 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
31 #include <linux/compiler.h>
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
38 #define __KSYMTAB_ENTRY(sym, sec) \
40 asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \
42 "__ksymtab_" #sym ": \n" \
43 " .long " #sym "- . \n" \
44 " .long __kstrtab_" #sym "- . \n" \
45 " .long __kstrtabns_" #sym "- . \n" \
48 struct kernel_symbol {
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 }
60 struct kernel_symbol {
63 const char *namespace;
69 #define ___EXPORT_SYMBOL(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
74 * For every exported symbol, do the following:
76 * - Put the name of the symbol and namespace (empty string "" for none) in
78 * - Place a struct kernel_symbol entry in the __ksymtab section.
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.
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" \
94 __KSYMTAB_ENTRY(sym, sec)
98 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
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
105 #define __EXPORT_SYMBOL(sym, sec, ns)
107 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
109 #include <generated/autoksyms.h>
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.
118 #define __ksym_marker(sym) \
119 static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
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)
131 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
133 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
138 #define __EXPORT_SYMBOL(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
140 #endif /* CONFIG_MODULES */
142 #ifdef DEFAULT_SYMBOL_NAMESPACE
143 #include <linux/stringify.h>
144 #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
146 #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, "")
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)
154 #endif /* !__ASSEMBLY__ */
156 #endif /* _LINUX_EXPORT_H */