blob: ae072bc5aacf8365245bdbc50f6c19b9e3d5812c [file] [log] [blame]
Paul Gortmakerf5016932011-05-23 14:11:39 -04001#ifndef _LINUX_EXPORT_H
2#define _LINUX_EXPORT_H
Nicholas Pigginb67067f2016-08-24 22:29:20 +10003
Paul Gortmakerf5016932011-05-23 14:11:39 -04004/*
5 * Export symbols from the kernel to modules. Forked from module.h
6 * to reduce the amount of pointless cruft we feed to gcc when only
7 * exporting a simple symbol or two.
8 *
Rusty Russellb92021b2013-03-15 15:04:17 +10309 * Try not to add #includes here. It slows compilation and makes kernel
10 * hackers place grumpy comments in header files.
Paul Gortmakerf5016932011-05-23 14:11:39 -040011 */
12
Rusty Russellb92021b2013-03-15 15:04:17 +103013#define __VMLINUX_SYMBOL(x) x
14#define __VMLINUX_SYMBOL_STR(x) #x
Paul Gortmakerf5016932011-05-23 14:11:39 -040015
Rusty Russellb92021b2013-03-15 15:04:17 +103016/* Indirect, so macros are expanded before pasting. */
17#define VMLINUX_SYMBOL(x) __VMLINUX_SYMBOL(x)
18#define VMLINUX_SYMBOL_STR(x) __VMLINUX_SYMBOL_STR(x)
19
20#ifndef __ASSEMBLY__
Paul Gortmakerf5016932011-05-23 14:11:39 -040021#ifdef MODULE
22extern struct module __this_module;
23#define THIS_MODULE (&__this_module)
24#else
25#define THIS_MODULE ((struct module *)0)
26#endif
27
28#ifdef CONFIG_MODULES
29
Nicolas Pitref2355412016-01-22 01:32:26 -050030#if defined(__KERNEL__) && !defined(__GENKSYMS__)
Paul Gortmakerf5016932011-05-23 14:11:39 -040031#ifdef CONFIG_MODVERSIONS
32/* Mark the CRC weak since genksyms apparently decides not to
33 * generate a checksums for some symbols */
Ard Biesheuvel71810db2017-02-03 09:54:06 +000034#if defined(CONFIG_MODULE_REL_CRCS)
35#define __CRC_SYMBOL(sym, sec) \
36 asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
Masahiro Yamada94e58e02018-05-09 16:23:49 +090037 " .weak __crc_" #sym " \n" \
38 " .long __crc_" #sym " - . \n" \
Ard Biesheuvel71810db2017-02-03 09:54:06 +000039 " .previous \n");
Paul Gortmakerf5016932011-05-23 14:11:39 -040040#else
Ard Biesheuvel71810db2017-02-03 09:54:06 +000041#define __CRC_SYMBOL(sym, sec) \
42 asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
Masahiro Yamada94e58e02018-05-09 16:23:49 +090043 " .weak __crc_" #sym " \n" \
44 " .long __crc_" #sym " \n" \
Ard Biesheuvel71810db2017-02-03 09:54:06 +000045 " .previous \n");
46#endif
47#else
Paul Gortmakerf5016932011-05-23 14:11:39 -040048#define __CRC_SYMBOL(sym, sec)
49#endif
50
Ard Biesheuvel7290d582018-08-21 21:56:09 -070051#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
52#include <linux/compiler.h>
53/*
54 * Emit the ksymtab entry as a pair of relative references: this reduces
55 * the size by half on 64-bit architectures, and eliminates the need for
56 * absolute relocations that require runtime processing on relocatable
57 * kernels.
58 */
59#define __KSYMTAB_ENTRY(sym, sec) \
60 __ADDRESSABLE(sym) \
61 asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \
62 " .balign 8 \n" \
63 "__ksymtab_" #sym ": \n" \
64 " .long " #sym "- . \n" \
65 " .long __kstrtab_" #sym "- . \n" \
66 " .previous \n")
67
68struct kernel_symbol {
69 int value_offset;
70 int name_offset;
71};
72#else
73#define __KSYMTAB_ENTRY(sym, sec) \
74 static const struct kernel_symbol __ksymtab_##sym \
75 __attribute__((section("___ksymtab" sec "+" #sym), used)) \
76 = { (unsigned long)&sym, __kstrtab_##sym }
77
78struct kernel_symbol {
79 unsigned long value;
80 const char *name;
81};
82#endif
83
Paul Gortmakerf5016932011-05-23 14:11:39 -040084/* For every exported symbol, place a struct in the __ksymtab section */
Nicholas Pigginb67067f2016-08-24 22:29:20 +100085#define ___EXPORT_SYMBOL(sym, sec) \
86 extern typeof(sym) sym; \
87 __CRC_SYMBOL(sym, sec) \
88 static const char __kstrtab_##sym[] \
Ard Biesheuvel7290d582018-08-21 21:56:09 -070089 __attribute__((section("__ksymtab_strings"), used, aligned(1))) \
Masahiro Yamada94e58e02018-05-09 16:23:49 +090090 = #sym; \
Ard Biesheuvel7290d582018-08-21 21:56:09 -070091 __KSYMTAB_ENTRY(sym, sec)
Paul Gortmakerf5016932011-05-23 14:11:39 -040092
Ard Biesheuvelf922c4a2018-08-21 21:56:04 -070093#if defined(__DISABLE_EXPORTS)
94
95/*
96 * Allow symbol exports to be disabled completely so that C code may
97 * be reused in other execution contexts such as the UEFI stub or the
98 * decompressor.
99 */
100#define __EXPORT_SYMBOL(sym, sec)
101
102#elif defined(__KSYM_DEPS__)
Nicolas Pitrec1a95fd2016-01-22 13:41:57 -0500103
104/*
105 * For fine grained build dependencies, we want to tell the build system
106 * about each possible exported symbol even if they're not actually exported.
107 * We use a string pattern that is unlikely to be valid code that the build
108 * system filters out from the preprocessor output (see ksym_dep_filter
109 * in scripts/Kbuild.include).
110 */
111#define __EXPORT_SYMBOL(sym, sec) === __KSYM_##sym ===
112
113#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
Nicolas Pitref2355412016-01-22 01:32:26 -0500114
Nicolas Pitref2355412016-01-22 01:32:26 -0500115#include <generated/autoksyms.h>
116
117#define __EXPORT_SYMBOL(sym, sec) \
Masahiro Yamada6023d232016-06-14 14:58:55 +0900118 __cond_export_sym(sym, sec, __is_defined(__KSYM_##sym))
Nicolas Pitref2355412016-01-22 01:32:26 -0500119#define __cond_export_sym(sym, sec, conf) \
120 ___cond_export_sym(sym, sec, conf)
121#define ___cond_export_sym(sym, sec, enabled) \
122 __cond_export_sym_##enabled(sym, sec)
123#define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
124#define __cond_export_sym_0(sym, sec) /* nothing */
125
126#else
127#define __EXPORT_SYMBOL ___EXPORT_SYMBOL
128#endif
129
Paul Gortmakerf5016932011-05-23 14:11:39 -0400130#define EXPORT_SYMBOL(sym) \
131 __EXPORT_SYMBOL(sym, "")
132
133#define EXPORT_SYMBOL_GPL(sym) \
134 __EXPORT_SYMBOL(sym, "_gpl")
135
136#define EXPORT_SYMBOL_GPL_FUTURE(sym) \
137 __EXPORT_SYMBOL(sym, "_gpl_future")
138
139#ifdef CONFIG_UNUSED_SYMBOLS
140#define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
141#define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
142#else
143#define EXPORT_UNUSED_SYMBOL(sym)
144#define EXPORT_UNUSED_SYMBOL_GPL(sym)
145#endif
146
147#endif /* __GENKSYMS__ */
148
149#else /* !CONFIG_MODULES... */
150
151#define EXPORT_SYMBOL(sym)
152#define EXPORT_SYMBOL_GPL(sym)
153#define EXPORT_SYMBOL_GPL_FUTURE(sym)
154#define EXPORT_UNUSED_SYMBOL(sym)
155#define EXPORT_UNUSED_SYMBOL_GPL(sym)
156
157#endif /* CONFIG_MODULES */
Rusty Russellb92021b2013-03-15 15:04:17 +1030158#endif /* !__ASSEMBLY__ */
Paul Gortmakerf5016932011-05-23 14:11:39 -0400159
160#endif /* _LINUX_EXPORT_H */