export.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef _LINUX_EXPORT_H
  3. #define _LINUX_EXPORT_H
  4. #include <linux/stringify.h>
  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. * This comment block is used by fixdep. Please do not remove.
  15. *
  16. * When CONFIG_MODVERSIONS is changed from n to y, all source files having
  17. * EXPORT_SYMBOL variants must be re-compiled because genksyms is run as a
  18. * side effect of the *.o build rule.
  19. */
  20. #ifndef __ASSEMBLY__
  21. #ifdef MODULE
  22. extern struct module __this_module;
  23. #define THIS_MODULE (&__this_module)
  24. #else
  25. #define THIS_MODULE ((struct module *)0)
  26. #endif
  27. #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
  28. #include <linux/compiler.h>
  29. /*
  30. * Emit the ksymtab entry as a pair of relative references: this reduces
  31. * the size by half on 64-bit architectures, and eliminates the need for
  32. * absolute relocations that require runtime processing on relocatable
  33. * kernels.
  34. */
  35. #define __KSYMTAB_ENTRY(sym, sec) \
  36. __ADDRESSABLE(sym) \
  37. asm(" .section \"___ksymtab" sec "+" #sym "\", \"a\" \n" \
  38. " .balign 4 \n" \
  39. "__ksymtab_" #sym ": \n" \
  40. " .long " #sym "- . \n" \
  41. " .long __kstrtab_" #sym "- . \n" \
  42. " .long __kstrtabns_" #sym "- . \n" \
  43. " .previous \n")
  44. struct kernel_symbol {
  45. int value_offset;
  46. int name_offset;
  47. int namespace_offset;
  48. };
  49. #else
  50. #define __KSYMTAB_ENTRY(sym, sec) \
  51. static const struct kernel_symbol __ksymtab_##sym \
  52. __attribute__((section("___ksymtab" sec "+" #sym), used)) \
  53. __aligned(sizeof(void *)) \
  54. = { (unsigned long)&sym, __kstrtab_##sym, __kstrtabns_##sym }
  55. struct kernel_symbol {
  56. unsigned long value;
  57. const char *name;
  58. const char *namespace;
  59. };
  60. #endif
  61. #ifdef __GENKSYMS__
  62. #define ___EXPORT_SYMBOL(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
  63. #else
  64. /*
  65. * For every exported symbol, do the following:
  66. *
  67. * - Put the name of the symbol and namespace (empty string "" for none) in
  68. * __ksymtab_strings.
  69. * - Place a struct kernel_symbol entry in the __ksymtab section.
  70. *
  71. * note on .section use: we specify progbits since usage of the "M" (SHF_MERGE)
  72. * section flag requires it. Use '%progbits' instead of '@progbits' since the
  73. * former apparently works on all arches according to the binutils source.
  74. */
  75. #define ___EXPORT_SYMBOL(sym, sec, ns) \
  76. extern typeof(sym) sym; \
  77. extern const char __kstrtab_##sym[]; \
  78. extern const char __kstrtabns_##sym[]; \
  79. asm(" .section \"__ksymtab_strings\",\"aMS\",%progbits,1 \n" \
  80. "__kstrtab_" #sym ": \n" \
  81. " .asciz \"" #sym "\" \n" \
  82. "__kstrtabns_" #sym ": \n" \
  83. " .asciz \"" ns "\" \n" \
  84. " .previous \n"); \
  85. __KSYMTAB_ENTRY(sym, sec)
  86. #endif
  87. #if !defined(CONFIG_MODULES) || defined(__DISABLE_EXPORTS)
  88. /*
  89. * Allow symbol exports to be disabled completely so that C code may
  90. * be reused in other execution contexts such as the UEFI stub or the
  91. * decompressor.
  92. */
  93. #define __EXPORT_SYMBOL(sym, sec, ns)
  94. #elif defined(CONFIG_TRIM_UNUSED_KSYMS) && !defined(MODULE)
  95. #include <generated/autoksyms.h>
  96. /*
  97. * For fine grained build dependencies, we want to tell the build system
  98. * about each possible exported symbol even if they're not actually exported.
  99. * We use a symbol pattern __ksym_marker_<symbol> that the build system filters
  100. * from the $(NM) output (see scripts/gen_ksymdeps.sh). These symbols are
  101. * discarded in the final link stage.
  102. */
  103. #define __ksym_marker(sym) \
  104. static int __ksym_marker_##sym[0] __section(".discard.ksym") __used
  105. #define __EXPORT_SYMBOL(sym, sec, ns) \
  106. __ksym_marker(sym); \
  107. __cond_export_sym(sym, sec, ns, __is_defined(__KSYM_##sym))
  108. #define __cond_export_sym(sym, sec, ns, conf) \
  109. ___cond_export_sym(sym, sec, ns, conf)
  110. #define ___cond_export_sym(sym, sec, ns, enabled) \
  111. __cond_export_sym_##enabled(sym, sec, ns)
  112. #define __cond_export_sym_1(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
  113. #ifdef __GENKSYMS__
  114. #define __cond_export_sym_0(sym, sec, ns) __GENKSYMS_EXPORT_SYMBOL(sym)
  115. #else
  116. #define __cond_export_sym_0(sym, sec, ns) /* nothing */
  117. #endif
  118. #else
  119. #define __EXPORT_SYMBOL(sym, sec, ns) ___EXPORT_SYMBOL(sym, sec, ns)
  120. #endif /* CONFIG_MODULES */
  121. #ifdef DEFAULT_SYMBOL_NAMESPACE
  122. #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, __stringify(DEFAULT_SYMBOL_NAMESPACE))
  123. #else
  124. #define _EXPORT_SYMBOL(sym, sec) __EXPORT_SYMBOL(sym, sec, "")
  125. #endif
  126. #define EXPORT_SYMBOL(sym) _EXPORT_SYMBOL(sym, "")
  127. #if IS_ENABLED(CONFIG_SEC_KUNIT) && IS_ENABLED(CONFIG_KUNIT)
  128. #define EXPORT_SYMBOL_KUNIT(sym) _EXPORT_SYMBOL(sym, "")
  129. #else
  130. #define EXPORT_SYMBOL_KUNIT(sym) /* nothing */
  131. #endif
  132. #define EXPORT_SYMBOL_GPL(sym) _EXPORT_SYMBOL(sym, "_gpl")
  133. #define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", __stringify(ns))
  134. #define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "_gpl", __stringify(ns))
  135. #endif /* !__ASSEMBLY__ */
  136. #endif /* _LINUX_EXPORT_H */