shlib-compat.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Macros for managing ABI-compatibility definitions using ELF symbol versions.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _SHLIB_COMPAT_H
  16. #define _SHLIB_COMPAT_H 1
  17. # include <abi-versions.h>
  18. /* Obtain the definition of symbol_version_reference. */
  19. #include <libc-symver.h>
  20. /* The file abi-versions.h (generated by scripts/abi-versions.awk) defines
  21. symbols like `ABI_libm_GLIBC_2_0' for each version set in the source
  22. code for each library. For a version set that is subsumed by a later
  23. version set, the definition gives the subsuming set, i.e. if GLIBC_2_0
  24. is subsumed by GLIBC_2_1, then ABI_libm_GLIBC_2_0 == ABI_libm_GLIBC_2_1.
  25. Each version set that is to be distinctly defined in the output has an
  26. unique positive integer value, increasing with newer versions. Thus,
  27. evaluating two ABI_* symbols reduces to integer values that differ only
  28. when the two version sets named are in fact two different ABIs we are
  29. supporting. If these do not differ, then there is no need to compile in
  30. extra code to support this version set where it has been superseded by a
  31. newer version. */
  32. #define LIB_COMPAT(lib, introduced, obsoleted) \
  33. _LIB_COMPAT (lib, introduced, obsoleted)
  34. #define _LIB_COMPAT(lib, introduced, obsoleted) \
  35. (IS_IN (lib) \
  36. && (!(ABI_##lib##_##obsoleted - 0) \
  37. || ((ABI_##lib##_##introduced - 0) < (ABI_##lib##_##obsoleted - 0))))
  38. #ifdef SHARED
  39. /* Similar to LIB_COMPAT, but evaluate to 0 for static build. The
  40. compatibility code should be conditionalized with e.g.
  41. `#if SHLIB_COMPAT (libm, GLIBC_2_0, GLIBC_2_2)' for code introduced
  42. in the GLIBC_2.0 version and obsoleted in the GLIBC_2.2 version. */
  43. # define SHLIB_COMPAT(lib, introduced, obsoleted) \
  44. _LIB_COMPAT (lib, introduced, obsoleted)
  45. /* Like SHLIB_COMPAT, but it can check versions in other libraries. It is
  46. not always false for !IS_IN (LIB). */
  47. #define OTHER_SHLIB_COMPAT(lib, introduced, obsoleted) \
  48. _OTHER_SHLIB_COMPAT (lib, introduced, obsoleted)
  49. #define _OTHER_SHLIB_COMPAT(lib, introduced, obsoleted) \
  50. (!(ABI_##lib##_##obsoleted - 0) \
  51. || ((ABI_##lib##_##introduced - 0) < (ABI_##lib##_##obsoleted - 0)))
  52. /* That header also defines symbols like `VERSION_libm_GLIBC_2_1' to
  53. the version set name to use for e.g. symbols first introduced into
  54. libm in the GLIBC_2.1 version. Definitions of symbols with explicit
  55. versions should look like:
  56. versioned_symbol (libm, new_foo, foo, GLIBC_2_1);
  57. This will define the symbol `foo' with the appropriate default version,
  58. i.e. either GLIBC_2.1 or the "earliest version" specified in
  59. shlib-versions if that is newer. */
  60. /* versioned_symbol (LIB, LOCAL, SYMBOL, VERSION) emits a definition
  61. of SYMBOL with a default (@@) VERSION appropriate for LIB. (The
  62. actually emitted symbol version is adjusted according to the
  63. baseline symbol version for LIB.) The address of the symbol is
  64. taken from LOCAL. Properties of LOCAL are copied to the exported
  65. symbol. In particular, LOCAL itself should be global. It is
  66. unspecified whether SYMBOL@VERSION is associated with LOCAL, or if
  67. an intermediate alias is created. If LOCAL and SYMBOL are
  68. distinct, and LOCAL is also intended for export, its version should
  69. be specified explicitly with versioned_symbol, too. */
  70. # define versioned_symbol(lib, local, symbol, version) \
  71. versioned_symbol_1 (lib, local, symbol, version)
  72. # define versioned_symbol_1(lib, local, symbol, version) \
  73. versioned_symbol_2 (local, symbol, VERSION_##lib##_##version)
  74. # define versioned_symbol_2(local, symbol, name) \
  75. default_symbol_version (local, symbol, name)
  76. /* compat_symbol is like versioned_symbol, but emits a compatibility
  77. version (with @ instead of @@). The same issue related to
  78. intermediate aliases applies, so LOCAL should not be listed in the
  79. Versions file, or otherwise it can be exported with an undesired
  80. default symbol version. */
  81. # define compat_symbol(lib, local, symbol, version) \
  82. compat_symbol_1 (lib, local, symbol, version)
  83. # define compat_symbol_1(lib, local, symbol, version) \
  84. compat_symbol_2 (local, symbol, VERSION_##lib##_##version)
  85. /* See <libc-symver.h>. */
  86. # ifdef __ASSEMBLER__
  87. #define compat_symbol_2(local, symbol, name) \
  88. _set_symbol_version (local, symbol@name)
  89. # else
  90. # define compat_symbol_2(local, symbol, name) \
  91. compat_symbol_3 (local, symbol, name)
  92. # define compat_symbol_3(local, symbol, name) \
  93. _set_symbol_version (local, #symbol "@" #name)
  94. # endif
  95. #else
  96. /* Not compiling ELF shared libraries at all, so never any old versions. */
  97. # define SHLIB_COMPAT(lib, introduced, obsoleted) 0
  98. # define OTHER_SHLIB_COMPAT(lib, introduced, obsoleted) 0
  99. /* No versions to worry about, just make this the global definition. */
  100. # define versioned_symbol(lib, local, symbol, version) \
  101. weak_alias (local, symbol)
  102. /* This should not appear outside `#if SHLIB_COMPAT (...)'. */
  103. # define compat_symbol(lib, local, symbol, version) ...
  104. #endif
  105. /* Use compat_symbol_reference for a reference *or* definition of a
  106. specific version of a symbol. compat_symbol_reference does not
  107. create intermediate aliases. Definitions are primarily used to
  108. ensure tests reference the exact compat symbol required, or define
  109. an interposing symbol of the right version e.g.,
  110. __malloc_initialize_hook in mcheck-init.c. Use compat_symbol to
  111. define such a symbol within the shared libraries that are built for
  112. users. */
  113. #define compat_symbol_reference(lib, local, symbol, version) \
  114. compat_symbol_reference_1 (lib, local, symbol, version)
  115. #define compat_symbol_reference_1(lib, local, symbol, version) \
  116. compat_symbol_reference_2 (local, symbol, VERSION_##lib##_##version)
  117. #define compat_symbol_reference_2(local, symbol, name) \
  118. symbol_version_reference (local, symbol, name)
  119. /* Export the symbol only for shared-library compatibility. */
  120. #define libc_sunrpc_symbol(name, aliasname, version) \
  121. compat_symbol (libc, name, aliasname, version);
  122. /* The TEST_COMPAT macro acts just like the SHLIB_COMPAT macro except
  123. that it does not check IS_IN. It is used by tests that are testing
  124. functionality that is only available in specific GLIBC versions. */
  125. # define TEST_COMPAT(lib, introduced, obsoleted) \
  126. _TEST_COMPAT (lib, introduced, obsoleted)
  127. # define _TEST_COMPAT(lib, introduced, obsoleted) \
  128. (!(ABI_##lib##_##obsoleted - 0) \
  129. || ((ABI_##lib##_##introduced - 0) < (ABI_##lib##_##obsoleted - 0)))
  130. #endif /* shlib-compat.h */