GNU Linux-libre 5.15.54-gnu
[releases.git] / include / linux / export.h
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 #ifndef __ASSEMBLY__
17 #ifdef MODULE
18 extern struct module __this_module;
19 #define THIS_MODULE (&__this_module)
20 #else
21 #define THIS_MODULE ((struct module *)0)
22 #endif
23
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"     \
32             "   .previous                                       \n")
33 #else
34 #define __CRC_SYMBOL(sym, sec)                                          \
35         asm("   .section \"___kcrctab" sec "+" #sym "\", \"a\"  \n"     \
36             "   .weak   __crc_" #sym "                          \n"     \
37             "   .long   __crc_" #sym "                          \n"     \
38             "   .previous                                       \n")
39 #endif
40 #else
41 #define __CRC_SYMBOL(sym, sec)
42 #endif
43
44 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
45 #include <linux/compiler.h>
46 /*
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
50  * kernels.
51  */
52 #define __KSYMTAB_ENTRY(sym, sec)                                       \
53         __ADDRESSABLE(sym)                                              \
54         asm("   .section \"___ksymtab" sec "+" #sym "\", \"a\"  \n"     \
55             "   .balign 4                                       \n"     \
56             "__ksymtab_" #sym ":                                \n"     \
57             "   .long   " #sym "- .                             \n"     \
58             "   .long   __kstrtab_" #sym "- .                   \n"     \
59             "   .long   __kstrtabns_" #sym "- .                 \n"     \
60             "   .previous                                       \n")
61
62 struct kernel_symbol {
63         int value_offset;
64         int name_offset;
65         int namespace_offset;
66 };
67 #else
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 }
73
74 struct kernel_symbol {
75         unsigned long value;
76         const char *name;
77         const char *namespace;
78 };
79 #endif
80
81 #ifdef __GENKSYMS__
82
83 #define ___EXPORT_SYMBOL(sym, sec, ns)  __GENKSYMS_EXPORT_SYMBOL(sym)
84
85 #else
86
87 /*
88  * For every exported symbol, do the following:
89  *
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
92  *   __ksymtab_strings.
93  * - Place a struct kernel_symbol entry in the __ksymtab section.
94  *
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.
98  */
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"     \
109             "   .previous                                               \n");   \
110         __KSYMTAB_ENTRY(sym, sec)
111
112 #endif
113
114 #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
115
116 /*
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
119  * decompressor.
120  */
121 #define __EXPORT_SYMBOL(sym, sec, ns)
122
123 #elif defined(CONFIG_TRIM_UNUSED_KSYMS)
124
125 #include <generated/autoksyms.h>
126
127 /*
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.
133  */
134 #define __ksym_marker(sym)      \
135         static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
136
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)
145
146 #ifdef __GENKSYMS__
147 #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
148 #else
149 #define __cond_export_sym_0(sym, sec, ns) /* nothing */
150 #endif
151
152 #else
153
154 #define __EXPORT_SYMBOL(sym, sec, ns)   ___EXPORT_SYMBOL(sym, sec, ns)
155
156 #endif /* CONFIG_MODULES */
157
158 #ifdef DEFAULT_SYMBOL_NAMESPACE
159 #define _EXPORT_SYMBOL(sym, sec)        __EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
160 #else
161 #define _EXPORT_SYMBOL(sym, sec)        __EXPORT_SYMBOL(sym, sec, "")
162 #endif
163
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))
168
169 #endif /* !__ASSEMBLY__ */
170
171 #endif /* _LINUX_EXPORT_H */