blob: ea7df303d68d93f597ef7456d446ba5738a1dd6a [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 -040021struct kernel_symbol
22{
23 unsigned long value;
24 const char *name;
25};
26
27#ifdef MODULE
28extern struct module __this_module;
29#define THIS_MODULE (&__this_module)
30#else
31#define THIS_MODULE ((struct module *)0)
32#endif
33
34#ifdef CONFIG_MODULES
35
Nicolas Pitref2355412016-01-22 01:32:26 -050036#if defined(__KERNEL__) && !defined(__GENKSYMS__)
Paul Gortmakerf5016932011-05-23 14:11:39 -040037#ifdef CONFIG_MODVERSIONS
38/* Mark the CRC weak since genksyms apparently decides not to
39 * generate a checksums for some symbols */
Ard Biesheuvel71810db2017-02-03 09:54:06 +000040#if defined(CONFIG_MODULE_REL_CRCS)
41#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");
Paul Gortmakerf5016932011-05-23 14:11:39 -040046#else
Ard Biesheuvel71810db2017-02-03 09:54:06 +000047#define __CRC_SYMBOL(sym, sec) \
48 asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
Masahiro Yamada94e58e02018-05-09 16:23:49 +090049 " .weak __crc_" #sym " \n" \
50 " .long __crc_" #sym " \n" \
Ard Biesheuvel71810db2017-02-03 09:54:06 +000051 " .previous \n");
52#endif
53#else
Paul Gortmakerf5016932011-05-23 14:11:39 -040054#define __CRC_SYMBOL(sym, sec)
55#endif
56
57/* For every exported symbol, place a struct in the __ksymtab section */
Nicholas Pigginb67067f2016-08-24 22:29:20 +100058#define ___EXPORT_SYMBOL(sym, sec) \
59 extern typeof(sym) sym; \
60 __CRC_SYMBOL(sym, sec) \
61 static const char __kstrtab_##sym[] \
62 __attribute__((section("__ksymtab_strings"), aligned(1))) \
Masahiro Yamada94e58e02018-05-09 16:23:49 +090063 = #sym; \
Nicholas Pigginb67067f2016-08-24 22:29:20 +100064 static const struct kernel_symbol __ksymtab_##sym \
65 __used \
66 __attribute__((section("___ksymtab" sec "+" #sym), used)) \
Paul Gortmakerf5016932011-05-23 14:11:39 -040067 = { (unsigned long)&sym, __kstrtab_##sym }
68
Ard Biesheuvelf922c4a2018-08-21 21:56:04 -070069#if defined(__DISABLE_EXPORTS)
70
71/*
72 * Allow symbol exports to be disabled completely so that C code may
73 * be reused in other execution contexts such as the UEFI stub or the
74 * decompressor.
75 */
76#define __EXPORT_SYMBOL(sym, sec)
77
78#elif defined(__KSYM_DEPS__)
Nicolas Pitrec1a95fd2016-01-22 13:41:57 -050079
80/*
81 * For fine grained build dependencies, we want to tell the build system
82 * about each possible exported symbol even if they're not actually exported.
83 * We use a string pattern that is unlikely to be valid code that the build
84 * system filters out from the preprocessor output (see ksym_dep_filter
85 * in scripts/Kbuild.include).
86 */
87#define __EXPORT_SYMBOL(sym, sec) === __KSYM_##sym ===
88
89#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
Nicolas Pitref2355412016-01-22 01:32:26 -050090
Nicolas Pitref2355412016-01-22 01:32:26 -050091#include <generated/autoksyms.h>
92
93#define __EXPORT_SYMBOL(sym, sec) \
Masahiro Yamada6023d232016-06-14 14:58:55 +090094 __cond_export_sym(sym, sec, __is_defined(__KSYM_##sym))
Nicolas Pitref2355412016-01-22 01:32:26 -050095#define __cond_export_sym(sym, sec, conf) \
96 ___cond_export_sym(sym, sec, conf)
97#define ___cond_export_sym(sym, sec, enabled) \
98 __cond_export_sym_##enabled(sym, sec)
99#define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
100#define __cond_export_sym_0(sym, sec) /* nothing */
101
102#else
103#define __EXPORT_SYMBOL ___EXPORT_SYMBOL
104#endif
105
Paul Gortmakerf5016932011-05-23 14:11:39 -0400106#define EXPORT_SYMBOL(sym) \
107 __EXPORT_SYMBOL(sym, "")
108
109#define EXPORT_SYMBOL_GPL(sym) \
110 __EXPORT_SYMBOL(sym, "_gpl")
111
112#define EXPORT_SYMBOL_GPL_FUTURE(sym) \
113 __EXPORT_SYMBOL(sym, "_gpl_future")
114
115#ifdef CONFIG_UNUSED_SYMBOLS
116#define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
117#define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
118#else
119#define EXPORT_UNUSED_SYMBOL(sym)
120#define EXPORT_UNUSED_SYMBOL_GPL(sym)
121#endif
122
123#endif /* __GENKSYMS__ */
124
125#else /* !CONFIG_MODULES... */
126
127#define EXPORT_SYMBOL(sym)
128#define EXPORT_SYMBOL_GPL(sym)
129#define EXPORT_SYMBOL_GPL_FUTURE(sym)
130#define EXPORT_UNUSED_SYMBOL(sym)
131#define EXPORT_UNUSED_SYMBOL_GPL(sym)
132
133#endif /* CONFIG_MODULES */
Rusty Russellb92021b2013-03-15 15:04:17 +1030134#endif /* !__ASSEMBLY__ */
Paul Gortmakerf5016932011-05-23 14:11:39 -0400135
136#endif /* _LINUX_EXPORT_H */