1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _LINUX_EXPORT_H
3 #define _LINUX_EXPORT_H
5 #include <linux/stringify.h>
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.
12 * Try not to add #includes here. It slows compilation and makes kernel
13 * hackers place grumpy comments in header files.
17 * This comment block is used by fixdep. Please do not remove.
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.
26 extern struct module __this_module;
27 #define THIS_MODULE (&__this_module)
29 #define THIS_MODULE ((struct module *)0)
32 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
33 #include <linux/compiler.h>
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
40 #define __KSYMTAB_ENTRY(sym, sec) \
42 asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \
44 "__ksymtab_" #sym ": \n" \
45 " .long " #sym "- . \n" \
46 " .long __kstrtab_" #sym "- . \n" \
47 " .long __kstrtabns_" #sym "- . \n" \
50 struct kernel_symbol {
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 }
62 struct kernel_symbol {
65 const char *namespace;
71 #define ___EXPORT_SYMBOL(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
76 * For every exported symbol, do the following:
78 * - Put the name of the symbol and namespace (empty string "" for none) in
80 * - Place a struct kernel_symbol entry in the __ksymtab section.
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.
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" \
96 __KSYMTAB_ENTRY(sym, sec)
100 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
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
107 #define __EXPORT_SYMBOL(sym, sec, ns)
109 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
111 #include <generated/autoksyms.h>
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.
120 #define __ksym_marker(sym) \
121 static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
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)
133 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
135 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
140 #define __EXPORT_SYMBOL(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
142 #endif /* CONFIG_MODULES */
144 #ifdef DEFAULT_SYMBOL_NAMESPACE
145 #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
147 #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, "")
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))
155 #endif /* !__ASSEMBLY__ */
157 #endif /* _LINUX_EXPORT_H */