Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | #ifndef _LINUX_EXPORT_H |
| 3 | #define _LINUX_EXPORT_H |
| 4 | |
| 5 | /* |
| 6 | * Export symbols from the kernel to modules. Forked from module.h |
| 7 | * to reduce the amount of pointless cruft we feed to gcc when only |
| 8 | * exporting a simple symbol or two. |
| 9 | * |
| 10 | * Try not to add #includes here. It slows compilation and makes kernel |
| 11 | * hackers place grumpy comments in header files. |
| 12 | */ |
| 13 | |
| 14 | #ifndef __ASSEMBLY__ |
| 15 | #ifdef MODULE |
| 16 | extern struct module __this_module; |
| 17 | #define THIS_MODULE (&__this_module) |
| 18 | #else |
| 19 | #define THIS_MODULE ((struct module *)0) |
| 20 | #endif |
| 21 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | #ifdef CONFIG_MODVERSIONS |
| 23 | /* Mark the CRC weak since genksyms apparently decides not to |
| 24 | * generate a checksums for some symbols */ |
| 25 | #if defined(CONFIG_MODULE_REL_CRCS) |
| 26 | #define __CRC_SYMBOL(sym, sec) \ |
| 27 | asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \ |
| 28 | " .weak __crc_" #sym " \n" \ |
| 29 | " .long __crc_" #sym " - . \n" \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 30 | " .previous \n") |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 31 | #else |
| 32 | #define __CRC_SYMBOL(sym, sec) \ |
| 33 | asm(" .section \"___kcrctab" sec "+" #sym "\", \"a\" \n" \ |
| 34 | " .weak __crc_" #sym " \n" \ |
| 35 | " .long __crc_" #sym " \n" \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 36 | " .previous \n") |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 37 | #endif |
| 38 | #else |
| 39 | #define __CRC_SYMBOL(sym, sec) |
| 40 | #endif |
| 41 | |
| 42 | #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS |
| 43 | #include <linux/compiler.h> |
| 44 | /* |
| 45 | * Emit the ksymtab entry as a pair of relative references: this reduces |
| 46 | * the size by half on 64-bit architectures, and eliminates the need for |
| 47 | * absolute relocations that require runtime processing on relocatable |
| 48 | * kernels. |
| 49 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 50 | #define __KSYMTAB_ENTRY(sym, sec) \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 51 | __ADDRESSABLE(sym) \ |
| 52 | asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 53 | " .balign 4 \n" \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 54 | "__ksymtab_" #sym ": \n" \ |
| 55 | " .long " #sym "- . \n" \ |
| 56 | " .long __kstrtab_" #sym "- . \n" \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 57 | " .long __kstrtabns_" #sym "- . \n" \ |
| 58 | " .previous \n") |
| 59 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | struct kernel_symbol { |
| 61 | int value_offset; |
| 62 | int name_offset; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 63 | int namespace_offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | }; |
| 65 | #else |
| 66 | #define __KSYMTAB_ENTRY(sym, sec) \ |
| 67 | static const struct kernel_symbol __ksymtab_##sym \ |
| 68 | __attribute__((section("___ksymtab" sec "+" #sym), used)) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 69 | __aligned(sizeof(void *)) \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 70 | = { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | |
| 72 | struct kernel_symbol { |
| 73 | unsigned long value; |
| 74 | const char *name; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 75 | const char *namespace; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 76 | }; |
| 77 | #endif |
| 78 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 79 | #ifdef __GENKSYMS__ |
| 80 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 81 | #define ___EXPORT_SYMBOL(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 82 | |
| 83 | #else |
| 84 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 85 | /* |
| 86 | * For every exported symbol, do the following: |
| 87 | * |
| 88 | * - If applicable, place a CRC entry in the __kcrctab section. |
| 89 | * - Put the name of the symbol and namespace (empty string "" for none) in |
| 90 | * __ksymtab_strings. |
| 91 | * - Place a struct kernel_symbol entry in the __ksymtab section. |
| 92 | * |
| 93 | * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE) |
| 94 | * section flag requires it. Use '%progbits' instead of '@progbits' since the |
| 95 | * former apparently works on all arches according to the binutils source. |
| 96 | */ |
| 97 | #define ___EXPORT_SYMBOL(sym, sec, ns) \ |
| 98 | extern typeof(sym) sym; \ |
| 99 | extern const char __kstrtab_##sym[]; \ |
| 100 | extern const char __kstrtabns_##sym[]; \ |
| 101 | __CRC_SYMBOL(sym, sec); \ |
| 102 | asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1 \n" \ |
| 103 | "__kstrtab_" #sym ": \n" \ |
| 104 | " .asciz \"" #sym "\" \n" \ |
| 105 | "__kstrtabns_" #sym ": \n" \ |
| 106 | " .asciz \"" ns "\" \n" \ |
| 107 | " .previous \n"); \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 108 | __KSYMTAB_ENTRY(sym, sec) |
| 109 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Allow symbol exports to be disabled completely so that C code may |
| 116 | * be reused in other execution contexts such as the UEFI stub or the |
| 117 | * decompressor. |
| 118 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 119 | #define __EXPORT_SYMBOL(sym, sec, ns) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 120 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 121 | #elif defined(CONFIG_TRIM_UNUSED_KSYMS) |
| 122 | |
| 123 | #include <generated/autoksyms.h> |
| 124 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 125 | /* |
| 126 | * For fine grained build dependencies, we want to tell the build system |
| 127 | * about each possible exported symbol even if they're not actually exported. |
| 128 | * We use a symbol pattern __ksym_marker_<symbol> that the build system filters |
| 129 | * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are |
| 130 | * discarded in the final link stage. |
| 131 | */ |
| 132 | #define __ksym_marker(sym) \ |
| 133 | static int __ksym_marker_##sym[0] __section(".discard.ksym") __used |
| 134 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 135 | #define __EXPORT_SYMBOL(sym, sec, ns) \ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 136 | __ksym_marker(sym); \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 137 | __cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym)) |
| 138 | #define __cond_export_sym(sym, sec, ns, conf) \ |
| 139 | ___cond_export_sym(sym, sec, ns, conf) |
| 140 | #define ___cond_export_sym(sym, sec, ns, enabled) \ |
| 141 | __cond_export_sym_##enabled(sym, sec, ns) |
| 142 | #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns) |
| 143 | #define __cond_export_sym_0(sym, sec, ns) /* nothing */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 144 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 145 | #else |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 146 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 147 | #define __EXPORT_SYMBOL(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 148 | |
| 149 | #endif /* CONFIG_MODULES */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 150 | |
| 151 | #ifdef DEFAULT_SYMBOL_NAMESPACE |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 152 | #include <linux/stringify.h> |
| 153 | #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE)) |
| 154 | #else |
| 155 | #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, "") |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 156 | #endif |
| 157 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 158 | #define EXPORT_SYMBOL(sym) _EXPORT_SYMBOL(sym, "") |
| 159 | #define EXPORT_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym, "_gpl") |
| 160 | #define EXPORT_SYMBOL_GPL_FUTURE(sym) _EXPORT_SYMBOL(sym, "_gpl_future") |
| 161 | #define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", #ns) |
| 162 | #define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "_gpl", #ns) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 163 | |
| 164 | #ifdef CONFIG_UNUSED_SYMBOLS |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 165 | #define EXPORT_UNUSED_SYMBOL(sym) _EXPORT_SYMBOL(sym, "_unused") |
| 166 | #define EXPORT_UNUSED_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym, "_unused_gpl") |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 167 | #else |
| 168 | #define EXPORT_UNUSED_SYMBOL(sym) |
| 169 | #define EXPORT_UNUSED_SYMBOL_GPL(sym) |
| 170 | #endif |
| 171 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 172 | #endif /* !__ASSEMBLY__ */ |
| 173 | |
| 174 | #endif /* _LINUX_EXPORT_H */ |