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.
18 extern struct module __this_module;
19 #define THIS_MODULE (&__this_module)
21 #define THIS_MODULE ((struct module *)0)
24 #ifdef CONFIG_MODVERSIONS
25 /* Mark the CRC weak since genksyms apparently decides not to
26 * generate a checksums for some symbols */
27 #if defined(CONFIG_MODULE_REL_CRCS)
28 #define __CRC_SYMBOL(sym, sec) \
29 asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
30 " .weak __crc_" #sym " \n" \
31 " .long __crc_" #sym " - . \n" \
34 #define __CRC_SYMBOL(sym, sec) \
35 asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
36 " .weak __crc_" #sym " \n" \
37 " .long __crc_" #sym " \n" \
41 #define __CRC_SYMBOL(sym, sec)
44 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
45 #include <linux/compiler.h>
47 * Emit the ksymtab entry as a pair of relative references: this reduces
48 * the size by half on 64-bit architectures, and eliminates the need for
49 * absolute relocations that require runtime processing on relocatable
52 #define __KSYMTAB_ENTRY(sym, sec) \
54 asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \
56 "__ksymtab_" #sym ": \n" \
57 " .long " #sym "- . \n" \
58 " .long __kstrtab_" #sym "- . \n" \
59 " .long __kstrtabns_" #sym "- . \n" \
62 struct kernel_symbol {
68 #define __KSYMTAB_ENTRY(sym, sec) \
69 static const struct kernel_symbol __ksymtab_##sym \
70 __attribute__((section("___ksymtab" sec "+" #sym), used)) \
71 __aligned(sizeof(void *)) \
72 = { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
74 struct kernel_symbol {
77 const char *namespace;
83 #define ___EXPORT_SYMBOL(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
88 * For every exported symbol, do the following:
90 * - If applicable, place a CRC entry in the __kcrctab section.
91 * - Put the name of the symbol and namespace (empty string "" for none) in
93 * - Place a struct kernel_symbol entry in the __ksymtab section.
95 * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
96 * section flag requires it. Use '%progbits' instead of '@progbits' since the
97 * former apparently works on all arches according to the binutils source.
99 #define ___EXPORT_SYMBOL(sym, sec, ns) \
100 extern typeof(sym) sym; \
101 extern const char __kstrtab_##sym[]; \
102 extern const char __kstrtabns_##sym[]; \
103 __CRC_SYMBOL(sym, sec); \
104 asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1 \n" \
105 "__kstrtab_" #sym ": \n" \
106 " .asciz \"" #sym "\" \n" \
107 "__kstrtabns_" #sym ": \n" \
108 " .asciz \"" ns "\" \n" \
110 __KSYMTAB_ENTRY(sym, sec)
114 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
117 * Allow symbol exports to be disabled completely so that C code may
118 * be reused in other execution contexts such as the UEFI stub or the
121 #define __EXPORT_SYMBOL(sym, sec, ns)
123 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
125 #include <generated/autoksyms.h>
128 * For fine grained build dependencies, we want to tell the build system
129 * about each possible exported symbol even if they're not actually exported.
130 * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
131 * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
132 * discarded in the final link stage.
134 #define __ksym_marker(sym) \
135 static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
137 #define __EXPORT_SYMBOL(sym, sec, ns) \
138 __ksym_marker(sym); \
139 __cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
140 #define __cond_export_sym(sym, sec, ns, conf) \
141 ___cond_export_sym(sym, sec, ns, conf)
142 #define ___cond_export_sym(sym, sec, ns, enabled) \
143 __cond_export_sym_##enabled(sym, sec, ns)
144 #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
147 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
149 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
154 #define __EXPORT_SYMBOL(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
156 #endif /* CONFIG_MODULES */
158 #ifdef DEFAULT_SYMBOL_NAMESPACE
159 #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
161 #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, "")
164 #define EXPORT_SYMBOL(sym) _EXPORT_SYMBOL(sym, "")
165 #define EXPORT_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym, "_gpl")
166 #define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", __stringify(ns))
167 #define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "_gpl", __stringify(ns))
169 #endif /* !__ASSEMBLY__ */
171 #endif /* _LINUX_EXPORT_H */