export.h 6.5 KB

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