blob: 2f9ccbe6a6390a4f2b2faea3cac96fd49ce48e1c [file] [log] [blame]
Paul Gortmakerf5016932011-05-23 14:11:39 -04001#ifndef _LINUX_EXPORT_H
2#define _LINUX_EXPORT_H
3/*
4 * Export symbols from the kernel to modules. Forked from module.h
5 * to reduce the amount of pointless cruft we feed to gcc when only
6 * exporting a simple symbol or two.
7 *
Rusty Russellb92021b2013-03-15 15:04:17 +10308 * Try not to add #includes here. It slows compilation and makes kernel
9 * hackers place grumpy comments in header files.
Paul Gortmakerf5016932011-05-23 14:11:39 -040010 */
11
12/* Some toolchains use a `_' prefix for all user symbols. */
Rusty Russellb92021b2013-03-15 15:04:17 +103013#ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX
14#define __VMLINUX_SYMBOL(x) _##x
15#define __VMLINUX_SYMBOL_STR(x) "_" #x
Paul Gortmakerf5016932011-05-23 14:11:39 -040016#else
Rusty Russellb92021b2013-03-15 15:04:17 +103017#define __VMLINUX_SYMBOL(x) x
18#define __VMLINUX_SYMBOL_STR(x) #x
Paul Gortmakerf5016932011-05-23 14:11:39 -040019#endif
20
Rusty Russellb92021b2013-03-15 15:04:17 +103021/* Indirect, so macros are expanded before pasting. */
22#define VMLINUX_SYMBOL(x) __VMLINUX_SYMBOL(x)
23#define VMLINUX_SYMBOL_STR(x) __VMLINUX_SYMBOL_STR(x)
24
25#ifndef __ASSEMBLY__
Paul Gortmakerf5016932011-05-23 14:11:39 -040026struct kernel_symbol
27{
28 unsigned long value;
29 const char *name;
30};
31
32#ifdef MODULE
33extern struct module __this_module;
34#define THIS_MODULE (&__this_module)
35#else
36#define THIS_MODULE ((struct module *)0)
37#endif
38
39#ifdef CONFIG_MODULES
40
Nicolas Pitref2355412016-01-22 01:32:26 -050041#if defined(__KERNEL__) && !defined(__GENKSYMS__)
Paul Gortmakerf5016932011-05-23 14:11:39 -040042#ifdef CONFIG_MODVERSIONS
43/* Mark the CRC weak since genksyms apparently decides not to
44 * generate a checksums for some symbols */
45#define __CRC_SYMBOL(sym, sec) \
Andi Kleene0f244c2013-10-23 10:57:58 +103046 extern __visible void *__crc_##sym __attribute__((weak)); \
Paul Gortmakerf5016932011-05-23 14:11:39 -040047 static const unsigned long __kcrctab_##sym \
48 __used \
49 __attribute__((section("___kcrctab" sec "+" #sym), unused)) \
50 = (unsigned long) &__crc_##sym;
51#else
52#define __CRC_SYMBOL(sym, sec)
53#endif
54
55/* For every exported symbol, place a struct in the __ksymtab section */
Nicolas Pitref2355412016-01-22 01:32:26 -050056#define ___EXPORT_SYMBOL(sym, sec) \
Paul Gortmakerf5016932011-05-23 14:11:39 -040057 extern typeof(sym) sym; \
58 __CRC_SYMBOL(sym, sec) \
59 static const char __kstrtab_##sym[] \
60 __attribute__((section("__ksymtab_strings"), aligned(1))) \
Rusty Russellb92021b2013-03-15 15:04:17 +103061 = VMLINUX_SYMBOL_STR(sym); \
Johannes Berg7b4ec8d2014-01-16 10:18:48 +103062 extern const struct kernel_symbol __ksymtab_##sym; \
Andi Kleene0f244c2013-10-23 10:57:58 +103063 __visible const struct kernel_symbol __ksymtab_##sym \
Paul Gortmakerf5016932011-05-23 14:11:39 -040064 __used \
65 __attribute__((section("___ksymtab" sec "+" #sym), unused)) \
66 = { (unsigned long)&sym, __kstrtab_##sym }
67
Nicolas Pitrec1a95fd2016-01-22 13:41:57 -050068#if defined(__KSYM_DEPS__)
69
70/*
71 * For fine grained build dependencies, we want to tell the build system
72 * about each possible exported symbol even if they're not actually exported.
73 * We use a string pattern that is unlikely to be valid code that the build
74 * system filters out from the preprocessor output (see ksym_dep_filter
75 * in scripts/Kbuild.include).
76 */
77#define __EXPORT_SYMBOL(sym, sec) === __KSYM_##sym ===
78
79#elif defined(CONFIG_TRIM_UNUSED_KSYMS)
Nicolas Pitref2355412016-01-22 01:32:26 -050080
81#include <linux/kconfig.h>
82#include <generated/autoksyms.h>
83
84#define __EXPORT_SYMBOL(sym, sec) \
85 __cond_export_sym(sym, sec, config_enabled(__KSYM_##sym))
86#define __cond_export_sym(sym, sec, conf) \
87 ___cond_export_sym(sym, sec, conf)
88#define ___cond_export_sym(sym, sec, enabled) \
89 __cond_export_sym_##enabled(sym, sec)
90#define __cond_export_sym_1(sym, sec) ___EXPORT_SYMBOL(sym, sec)
91#define __cond_export_sym_0(sym, sec) /* nothing */
92
93#else
94#define __EXPORT_SYMBOL ___EXPORT_SYMBOL
95#endif
96
Paul Gortmakerf5016932011-05-23 14:11:39 -040097#define EXPORT_SYMBOL(sym) \
98 __EXPORT_SYMBOL(sym, "")
99
100#define EXPORT_SYMBOL_GPL(sym) \
101 __EXPORT_SYMBOL(sym, "_gpl")
102
103#define EXPORT_SYMBOL_GPL_FUTURE(sym) \
104 __EXPORT_SYMBOL(sym, "_gpl_future")
105
106#ifdef CONFIG_UNUSED_SYMBOLS
107#define EXPORT_UNUSED_SYMBOL(sym) __EXPORT_SYMBOL(sym, "_unused")
108#define EXPORT_UNUSED_SYMBOL_GPL(sym) __EXPORT_SYMBOL(sym, "_unused_gpl")
109#else
110#define EXPORT_UNUSED_SYMBOL(sym)
111#define EXPORT_UNUSED_SYMBOL_GPL(sym)
112#endif
113
114#endif /* __GENKSYMS__ */
115
116#else /* !CONFIG_MODULES... */
117
118#define EXPORT_SYMBOL(sym)
119#define EXPORT_SYMBOL_GPL(sym)
120#define EXPORT_SYMBOL_GPL_FUTURE(sym)
121#define EXPORT_UNUSED_SYMBOL(sym)
122#define EXPORT_UNUSED_SYMBOL_GPL(sym)
123
124#endif /* CONFIG_MODULES */
Rusty Russellb92021b2013-03-15 15:04:17 +1030125#endif /* !__ASSEMBLY__ */
Paul Gortmakerf5016932011-05-23 14:11:39 -0400126
127#endif /* _LINUX_EXPORT_H */