blob: 7473fba6a60c5022fc404c041d11e58c9ba49bc0 [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
13/* Some toolchains use a `_' prefix for all user symbols. */
Rusty Russellb92021b2013-03-15 15:04:17 +103014#ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX
15#define __VMLINUX_SYMBOL(x) _##x
16#define __VMLINUX_SYMBOL_STR(x) "_" #x
Paul Gortmakerf5016932011-05-23 14:11:39 -040017#else
Rusty Russellb92021b2013-03-15 15:04:17 +103018#define __VMLINUX_SYMBOL(x) x
19#define __VMLINUX_SYMBOL_STR(x) #x
Paul Gortmakerf5016932011-05-23 14:11:39 -040020#endif
21
Rusty Russellb92021b2013-03-15 15:04:17 +103022/* Indirect, so macros are expanded before pasting. */
23#define VMLINUX_SYMBOL(x) __VMLINUX_SYMBOL(x)
24#define VMLINUX_SYMBOL_STR(x) __VMLINUX_SYMBOL_STR(x)
25
26#ifndef __ASSEMBLY__
Paul Gortmakerf5016932011-05-23 14:11:39 -040027struct kernel_symbol
28{
29 unsigned long value;
30 const char *name;
31};
32
33#ifdef MODULE
34extern struct module __this_module;
35#define THIS_MODULE (&__this_module)
36#else
37#define THIS_MODULE ((struct module *)0)
38#endif
39
40#ifdef CONFIG_MODULES
41
Nicolas Pitref2355412016-01-22 01:32:26 -050042#if defined(__KERNEL__) && !defined(__GENKSYMS__)
Paul Gortmakerf5016932011-05-23 14:11:39 -040043#ifdef CONFIG_MODVERSIONS
44/* Mark the CRC weak since genksyms apparently decides not to
45 * generate a checksums for some symbols */
Ard Biesheuvel71810db2017-02-03 09:54:06 +000046#if defined(CONFIG_MODULE_REL_CRCS)
47#define __CRC_SYMBOL(sym, sec) \
48 asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
49 " .weak " VMLINUX_SYMBOL_STR(__crc_##sym) " \n" \
50 " .long " VMLINUX_SYMBOL_STR(__crc_##sym) " - . \n" \
51 " .previous \n");
52#elif !defined(CONFIG_64BIT)
Nicholas Pigginb67067f2016-08-24 22:29:20 +100053#define __CRC_SYMBOL(sym, sec) \
54 extern __visible void *__crc_##sym __attribute__((weak)); \
55 static const unsigned long __kcrctab_##sym \
56 __used \
57 __attribute__((section("___kcrctab" sec "+" #sym), used)) \
Paul Gortmakerf5016932011-05-23 14:11:39 -040058 = (unsigned long) &__crc_##sym;
59#else
Ard Biesheuvel71810db2017-02-03 09:54:06 +000060#define __CRC_SYMBOL(sym, sec) \
61 asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \
62 " .weak " VMLINUX_SYMBOL_STR(__crc_##sym) " \n" \
63 " .long " VMLINUX_SYMBOL_STR(__crc_##sym) " \n" \
64 " .previous \n");
65#endif
66#else
Paul Gortmakerf5016932011-05-23 14:11:39 -040067#define __CRC_SYMBOL(sym, sec)
68#endif
69
70/* For every exported symbol, place a struct in the __ksymtab section */
Nicholas Pigginb67067f2016-08-24 22:29:20 +100071#define ___EXPORT_SYMBOL(sym, sec) \
72 extern typeof(sym) sym; \
73 __CRC_SYMBOL(sym, sec) \
74 static const char __kstrtab_##sym[] \
75 __attribute__((section("__ksymtab_strings"), aligned(1))) \
76 = VMLINUX_SYMBOL_STR(sym); \
77 static const struct kernel_symbol __ksymtab_##sym \
78 __used \
79 __attribute__((section("___ksymtab" sec "+" #sym), used)) \
Paul Gortmakerf5016932011-05-23 14:11:39 -040080 = { (unsigned long)&sym, __kstrtab_##sym }
81
Nicolas Pitrec1a95fd2016-01-22 13:41:57 -050082#if defined(__KSYM_DEPS__)
83
84/*
85 * For fine grained build dependencies, we want to tell the build system
86 * about each possible exported symbol even if they're not actually exported.
87 * We use a string pattern that is unlikely to be valid code that the build
88 * system filters out from the preprocessor output (see ksym_dep_filter
89 * in scripts/Kbuild.include).
90 */
91#define __EXPORT_SYMBOL(sym, sec) === __KSYM_##sym ===
92
93#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
Nicolas Pitref2355412016-01-22 01:32:26 -050094
Nicolas Pitref2355412016-01-22 01:32:26 -050095#include <generated/autoksyms.h>
96
97#define __EXPORT_SYMBOL(sym, sec) \
Masahiro Yamada6023d232016-06-14 14:58:55 +090098 __cond_export_sym(sym, sec, __is_defined(__KSYM_##sym))
Nicolas Pitref2355412016-01-22 01:32:26 -050099#define __cond_export_sym(sym, sec, conf) \
100 ___cond_export_sym(sym, sec, conf)
101#define ___cond_export_sym(sym, sec, enabled) \
102 __cond_export_sym_##enabled(sym, sec)
103#define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
104#define __cond_export_sym_0(sym, sec) /* nothing */
105
106#else
107#define __EXPORT_SYMBOL ___EXPORT_SYMBOL
108#endif
109
Paul Gortmakerf5016932011-05-23 14:11:39 -0400110#define EXPORT_SYMBOL(sym) \
111 __EXPORT_SYMBOL(sym, "")
112
113#define EXPORT_SYMBOL_GPL(sym) \
114 __EXPORT_SYMBOL(sym, "_gpl")
115
116#define EXPORT_SYMBOL_GPL_FUTURE(sym) \
117 __EXPORT_SYMBOL(sym, "_gpl_future")
118
119#ifdef CONFIG_UNUSED_SYMBOLS
120#define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
121#define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
122#else
123#define EXPORT_UNUSED_SYMBOL(sym)
124#define EXPORT_UNUSED_SYMBOL_GPL(sym)
125#endif
126
127#endif /* __GENKSYMS__ */
128
129#else /* !CONFIG_MODULES... */
130
131#define EXPORT_SYMBOL(sym)
132#define EXPORT_SYMBOL_GPL(sym)
133#define EXPORT_SYMBOL_GPL_FUTURE(sym)
134#define EXPORT_UNUSED_SYMBOL(sym)
135#define EXPORT_UNUSED_SYMBOL_GPL(sym)
136
137#endif /* CONFIG_MODULES */
Rusty Russellb92021b2013-03-15 15:04:17 +1030138#endif /* !__ASSEMBLY__ */
Paul Gortmakerf5016932011-05-23 14:11:39 -0400139
140#endif /* _LINUX_EXPORT_H */