libc-symbols.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /* Support macros for making weak and strong aliases for symbols,
  2. and for using symbol sets and linker warnings with GNU ld.
  3. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #ifndef _LIBC_SYMBOLS_H
  17. #define _LIBC_SYMBOLS_H 1
  18. /* This file is included implicitly in the compilation of every source file,
  19. using -include. It includes config.h. */
  20. /* Enable declarations of GNU extensions, since we are compiling them. */
  21. #define _GNU_SOURCE 1
  22. #ifdef MODULE_NAME
  23. /* Use `#if IS_IN (module)` to detect what component is being compiled. */
  24. #define PASTE_NAME1(a,b) a##b
  25. #define PASTE_NAME(a,b) PASTE_NAME1 (a,b)
  26. #define IN_MODULE PASTE_NAME (MODULE_, MODULE_NAME)
  27. #define IS_IN(lib) (IN_MODULE == MODULE_##lib)
  28. /* True if the current module is a versioned library. Versioned
  29. library names culled from shlib-versions files are assigned a
  30. MODULE_* value greater than MODULE_LIBS_BEGIN. */
  31. #define IS_IN_LIB (IN_MODULE > MODULE_LIBS_BEGIN)
  32. /* The testsuite, and some other ancillary code, should be compiled against
  33. as close an approximation to the installed headers as possible.
  34. Defining this symbol disables most internal-use-only declarations
  35. provided by this header, and all those provided by other internal
  36. wrapper headers. */
  37. #if IS_IN (testsuite) || defined IS_IN_build || defined __cplusplus
  38. # define _ISOMAC 1
  39. #endif
  40. #else
  41. /* The generation process for a few files created very early in the
  42. build (notably libc-modules.h itself) involves preprocessing this
  43. header without defining MODULE_NAME. Under these conditions,
  44. internal declarations (especially from config.h) must be visible,
  45. but IS_IN should always evaluate as false. */
  46. # define IS_IN(lib) 0
  47. # define IS_IN_LIB 0
  48. # define IN_MODULE (-1)
  49. #endif
  50. #include <libc-misc.h>
  51. #ifndef _ISOMAC
  52. /* This is defined for the compilation of all C library code. features.h
  53. tests this to avoid inclusion of stubs.h while compiling the library,
  54. before stubs.h has been generated. Some library code that is shared
  55. with other packages also tests this symbol to see if it is being
  56. compiled as part of the C library. We must define this before including
  57. config.h, because it makes some definitions conditional on whether libc
  58. itself is being compiled, or just some generator program. */
  59. #define _LIBC 1
  60. /* Some files must be compiled with optimization on. */
  61. #if !defined __ASSEMBLER__ && !defined __OPTIMIZE__
  62. # error "glibc cannot be compiled without optimization"
  63. #endif
  64. /* -ffast-math cannot be applied to the C library, as it alters the ABI.
  65. Some test components that use -ffast-math are currently not part of
  66. IS_IN (testsuite) for technical reasons, so we have a secondary override. */
  67. #if defined __FAST_MATH__ && !defined TEST_FAST_MATH
  68. # error "glibc must not be compiled with -ffast-math"
  69. #endif
  70. /* Obtain the definition of symbol_version_reference. */
  71. #include <libc-symver.h>
  72. #include <libc-diag.h>
  73. /* When PIC is defined and SHARED isn't defined, we are building PIE
  74. by default. */
  75. #if defined PIC && !defined SHARED
  76. # define BUILD_PIE_DEFAULT 1
  77. #else
  78. # define BUILD_PIE_DEFAULT 0
  79. #endif
  80. /* Define this for the benefit of portable GNU code that wants to check it.
  81. Code that checks with #if will not #include <config.h> again, since we've
  82. already done it (and this file is implicitly included in every compile,
  83. via -include). Code that checks with #ifdef will #include <config.h>,
  84. but that file should always be idempotent (i.e., it's just #define/#undef
  85. and nothing else anywhere should be changing the macro state it touches),
  86. so it's harmless. */
  87. #define HAVE_CONFIG_H 0
  88. /* Define these macros for the benefit of portable GNU code that wants to check
  89. them. Of course, STDC_HEADERS is never false when building libc! */
  90. #define STDC_HEADERS 1
  91. #define HAVE_MBSTATE_T 1
  92. #define HAVE_MBSRTOWCS 1
  93. #define HAVE_LIBINTL_H 1
  94. #define HAVE_WCTYPE_H 1
  95. #define HAVE_ISWCTYPE 1
  96. #define ENABLE_NLS 1
  97. /* The symbols in all the user (non-_) macros are C symbols. */
  98. #ifndef __SYMBOL_PREFIX
  99. # define __SYMBOL_PREFIX
  100. #endif
  101. #ifndef C_SYMBOL_NAME
  102. # define C_SYMBOL_NAME(name) name
  103. #endif
  104. #ifndef ASM_LINE_SEP
  105. # define ASM_LINE_SEP ;
  106. #endif
  107. #ifndef __attribute_copy__
  108. /* Provide an empty definition when cdefs.h is not included. */
  109. # define __attribute_copy__(arg)
  110. #endif
  111. #ifndef __ASSEMBLER__
  112. /* GCC understands weak symbols and aliases; use its interface where
  113. possible, instead of embedded assembly language. */
  114. /* Define ALIASNAME as a strong alias for NAME. */
  115. # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
  116. # define _strong_alias(name, aliasname) \
  117. extern __typeof (name) aliasname __attribute__ ((alias (#name))) \
  118. __attribute_copy__ (name);
  119. /* This comes between the return type and function name in
  120. a function definition to make that definition weak. */
  121. # define weak_function __attribute__ ((weak))
  122. # define weak_const_function __attribute__ ((weak, __const__))
  123. /* Define ALIASNAME as a weak alias for NAME.
  124. If weak aliases are not available, this defines a strong alias. */
  125. # define weak_alias(name, aliasname) _weak_alias (name, aliasname)
  126. # define _weak_alias(name, aliasname) \
  127. extern __typeof (name) aliasname __attribute__ ((weak, alias (#name))) \
  128. __attribute_copy__ (name);
  129. /* Define a strong_alias for SHARED, or weak_alias otherwise. It is used to
  130. avoid potential compiler warnings for weak alias indirection (when a weak
  131. alias is always resolved to a symbol even if a weak definition also
  132. exists). */
  133. # ifdef SHARED
  134. # define static_weak_alias(name, aliasname) strong_alias (name, aliasname)
  135. # else
  136. # define static_weak_alias(name, aliasname) weak_alias (name, aliasname)
  137. # endif
  138. /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
  139. # define weak_extern(symbol) _weak_extern (weak symbol)
  140. # define _weak_extern(expr) _Pragma (#expr)
  141. /* In shared builds, the expression call_function_static_weak
  142. (FUNCTION-SYMBOL, ARGUMENTS) invokes FUNCTION-SYMBOL (an
  143. identifier) unconditionally, with the (potentially empty) argument
  144. list ARGUMENTS. In static builds, if FUNCTION-SYMBOL has a
  145. definition, the function is invoked as before; if FUNCTION-SYMBOL
  146. is NULL, no call is performed. */
  147. # ifdef SHARED
  148. # define call_function_static_weak(func, ...) func (__VA_ARGS__)
  149. # else /* !SHARED */
  150. # define call_function_static_weak(func, ...) \
  151. ({ \
  152. extern __typeof__ (func) func weak_function; \
  153. (func != NULL ? func (__VA_ARGS__) : (void)0); \
  154. })
  155. # endif
  156. #else /* __ASSEMBLER__ */
  157. # ifdef HAVE_ASM_SET_DIRECTIVE
  158. # define strong_alias(original, alias) \
  159. .globl C_SYMBOL_NAME (alias) ASM_LINE_SEP \
  160. .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
  161. # define strong_data_alias(original, alias) strong_alias(original, alias)
  162. # else
  163. # define strong_alias(original, alias) \
  164. .globl C_SYMBOL_NAME (alias) ASM_LINE_SEP \
  165. C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
  166. # define strong_data_alias(original, alias) strong_alias(original, alias)
  167. # endif
  168. # define weak_alias(original, alias) \
  169. .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
  170. C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
  171. # define weak_extern(symbol) \
  172. .weak C_SYMBOL_NAME (symbol)
  173. #endif /* __ASSEMBLER__ */
  174. /* Determine the return address. */
  175. #define RETURN_ADDRESS(nr) \
  176. __builtin_extract_return_addr (__builtin_return_address (nr))
  177. /* When a reference to SYMBOL is encountered, the linker will emit a
  178. warning message MSG. */
  179. /* We want the .gnu.warning.SYMBOL section to be unallocated. */
  180. #define __make_section_unallocated(section_string) \
  181. asm (".section " section_string "\n\t.previous");
  182. /* Tacking on "\n\t#" to the section name makes gcc put its bogus
  183. section attributes on what looks like a comment to the assembler. */
  184. #ifdef HAVE_SECTION_QUOTES
  185. # define __sec_comment "\"\n\t#\""
  186. #else
  187. # define __sec_comment "\n\t#"
  188. #endif
  189. #define link_warning(symbol, msg) \
  190. __make_section_unallocated (".gnu.warning." #symbol) \
  191. static const char __evoke_link_warning_##symbol[] \
  192. __attribute__ ((used, section (".gnu.warning." #symbol __sec_comment))) \
  193. = msg;
  194. /* A canned warning for sysdeps/stub functions. */
  195. #define stub_warning(name) \
  196. __make_section_unallocated (".gnu.glibc-stub." #name) \
  197. link_warning (name, #name " is not implemented and will always fail")
  198. /* Warning for linking functions calling dlopen into static binaries. */
  199. #ifdef SHARED
  200. #define static_link_warning(name)
  201. #else
  202. #define static_link_warning(name) static_link_warning1(name)
  203. #define static_link_warning1(name) \
  204. link_warning(name, "Using '" #name "' in statically linked applications \
  205. requires at runtime the shared libraries from the glibc version used \
  206. for linking")
  207. #endif
  208. /* Declare SYMBOL to be TYPE (`function' or `object') of SIZE bytes
  209. alias to ORIGINAL, when the assembler supports such declarations
  210. (such as in ELF).
  211. This is only necessary when defining something in assembly, or playing
  212. funny alias games where the size should be other than what the compiler
  213. thinks it is. */
  214. #define declare_object_symbol_alias(symbol, original, size) \
  215. declare_object_symbol_alias_1 (symbol, original, size)
  216. #ifdef __ASSEMBLER__
  217. # define declare_object_symbol_alias_1(symbol, original, s_size) \
  218. strong_alias (original, symbol) ASM_LINE_SEP \
  219. .type C_SYMBOL_NAME (symbol), %object ASM_LINE_SEP \
  220. .size C_SYMBOL_NAME (symbol), s_size ASM_LINE_SEP
  221. #else /* Not __ASSEMBLER__. */
  222. # ifdef HAVE_ASM_SET_DIRECTIVE
  223. # define declare_object_symbol_alias_1(symbol, original, size) \
  224. asm (".global " __SYMBOL_PREFIX # symbol "\n" \
  225. ".type " __SYMBOL_PREFIX # symbol ", %object\n" \
  226. ".set " __SYMBOL_PREFIX #symbol ", " __SYMBOL_PREFIX original "\n" \
  227. ".size " __SYMBOL_PREFIX #symbol ", " #size "\n");
  228. # else
  229. # define declare_object_symbol_alias_1(symbol, original, size) \
  230. asm (".global " __SYMBOL_PREFIX # symbol "\n" \
  231. ".type " __SYMBOL_PREFIX # symbol ", %object\n" \
  232. __SYMBOL_PREFIX #symbol " = " __SYMBOL_PREFIX original "\n" \
  233. ".size " __SYMBOL_PREFIX #symbol ", " #size "\n");
  234. # endif /* HAVE_ASM_SET_DIRECTIVE */
  235. #endif /* __ASSEMBLER__ */
  236. /*
  237. */
  238. #ifdef HAVE_GNU_RETAIN
  239. # define attribute_used_retain __attribute__ ((__used__, __retain__))
  240. #else
  241. # define attribute_used_retain __attribute__ ((__used__))
  242. #endif
  243. /* Symbol set support macros. */
  244. /* Make SYMBOL, which is in the text segment, an element of SET. */
  245. #define text_set_element(set, symbol) _elf_set_element(set, symbol)
  246. /* Make SYMBOL, which is in the data segment, an element of SET. */
  247. #define data_set_element(set, symbol) _elf_set_element(set, symbol)
  248. /* Make SYMBOL, which is in the bss segment, an element of SET. */
  249. #define bss_set_element(set, symbol) _elf_set_element(set, symbol)
  250. /* These are all done the same way in ELF.
  251. There is a new section created for each set. */
  252. #ifdef SHARED
  253. /* When building a shared library, make the set section writable,
  254. because it will need to be relocated at run time anyway. */
  255. # define _elf_set_element(set, symbol) \
  256. static const void *__elf_set_##set##_element_##symbol##__ \
  257. attribute_used_retain __attribute__ ((section (#set))) = &(symbol)
  258. #else
  259. # define _elf_set_element(set, symbol) \
  260. static const void *const __elf_set_##set##_element_##symbol##__ \
  261. attribute_used_retain __attribute__ ((section (#set))) = &(symbol)
  262. #endif
  263. /* Define SET as a symbol set. This may be required (it is in a.out) to
  264. be able to use the set's contents. */
  265. #define symbol_set_define(set) symbol_set_declare(set)
  266. /* Declare SET for use in this module, if defined in another module.
  267. In a shared library, this is always local to that shared object.
  268. For static linking, the set might be wholly absent and so we use
  269. weak references. */
  270. #define symbol_set_declare(set) \
  271. extern char const __start_##set[] __symbol_set_attribute; \
  272. extern char const __stop_##set[] __symbol_set_attribute;
  273. #ifdef SHARED
  274. # define __symbol_set_attribute attribute_hidden
  275. #else
  276. # define __symbol_set_attribute __attribute__ ((weak))
  277. #endif
  278. /* Return a pointer (void *const *) to the first element of SET. */
  279. #define symbol_set_first_element(set) ((void *const *) (&__start_##set))
  280. /* Return true iff PTR (a void *const *) has been incremented
  281. past the last element in SET. */
  282. #define symbol_set_end_p(set, ptr) ((ptr) >= (void *const *) &__stop_##set)
  283. #ifdef SHARED
  284. # define symbol_version(real, name, version) \
  285. symbol_version_reference(real, name, version)
  286. # define default_symbol_version(real, name, version) \
  287. _default_symbol_version(real, name, version)
  288. /* See <libc-symver.h>. */
  289. # ifdef __ASSEMBLER__
  290. # define _default_symbol_version(real, name, version) \
  291. _set_symbol_version (real, name@@version)
  292. # else
  293. # define _default_symbol_version(real, name, version) \
  294. _set_symbol_version (real, #name "@@" #version)
  295. # endif
  296. /* Evaluates to a string literal for VERSION in LIB. */
  297. # define symbol_version_string(lib, version) \
  298. _symbol_version_stringify_1 (VERSION_##lib##_##version)
  299. # define _symbol_version_stringify_1(arg) _symbol_version_stringify_2 (arg)
  300. # define _symbol_version_stringify_2(arg) #arg
  301. #else /* !SHARED */
  302. # define symbol_version(real, name, version)
  303. # define default_symbol_version(real, name, version) \
  304. strong_alias(real, name)
  305. #endif
  306. #if defined SHARED || defined LIBC_NONSHARED \
  307. || (BUILD_PIE_DEFAULT && IS_IN (libc))
  308. # define attribute_hidden __attribute__ ((visibility ("hidden")))
  309. #else
  310. # define attribute_hidden
  311. #endif
  312. #define attribute_tls_model_ie __attribute__ ((tls_model ("initial-exec")))
  313. #define attribute_relro __attribute__ ((section (".data.rel.ro")))
  314. /* The following macros are used for PLT bypassing within libc.so
  315. (and if needed other libraries similarly).
  316. First of all, you need to have the function prototyped somewhere,
  317. say in foo/foo.h:
  318. int foo (int __bar);
  319. If calls to foo within libc.so should always go to foo defined in libc.so,
  320. then in include/foo.h you add:
  321. libc_hidden_proto (foo)
  322. line and after the foo function definition:
  323. int foo (int __bar)
  324. {
  325. return __bar;
  326. }
  327. libc_hidden_def (foo)
  328. or
  329. int foo (int __bar)
  330. {
  331. return __bar;
  332. }
  333. libc_hidden_weak (foo)
  334. Similarly for global data. If references to foo within libc.so should
  335. always go to foo defined in libc.so, then in include/foo.h you add:
  336. libc_hidden_proto (foo)
  337. line and after foo's definition:
  338. int foo = INITIAL_FOO_VALUE;
  339. libc_hidden_data_def (foo)
  340. or
  341. int foo = INITIAL_FOO_VALUE;
  342. libc_hidden_data_weak (foo)
  343. If foo is normally just an alias (strong or weak) to some other function,
  344. you should use the normal strong_alias first, then add libc_hidden_def
  345. or libc_hidden_weak:
  346. int baz (int __bar)
  347. {
  348. return __bar;
  349. }
  350. strong_alias (baz, foo)
  351. libc_hidden_weak (foo)
  352. If the function should be internal to multiple objects, say ld.so and
  353. libc.so, the best way is to use:
  354. #if IS_IN (libc) || IS_IN (rtld)
  355. hidden_proto (foo)
  356. #endif
  357. in include/foo.h and the normal macros at all function definitions
  358. depending on what DSO they belong to.
  359. If versioned_symbol macro is used to define foo,
  360. libc_hidden_ver macro should be used, as in:
  361. int __real_foo (int __bar)
  362. {
  363. return __bar;
  364. }
  365. versioned_symbol (libc, __real_foo, foo, GLIBC_2_1);
  366. libc_hidden_ver (__real_foo, foo) */
  367. #if defined SHARED && !defined NO_HIDDEN
  368. # ifndef __ASSEMBLER__
  369. # define __hidden_proto_hiddenattr(attrs...) \
  370. __attribute__ ((visibility ("hidden"), ##attrs))
  371. # define hidden_proto(name, attrs...) \
  372. __hidden_proto (name, , __GI_##name, ##attrs)
  373. # define hidden_proto_alias(name, alias, attrs...) \
  374. __hidden_proto_alias (name, , alias, ##attrs)
  375. # define hidden_tls_proto(name, attrs...) \
  376. __hidden_proto (name, __thread, __GI_##name, ##attrs)
  377. # define __hidden_proto(name, thread, internal, attrs...) \
  378. extern thread __typeof (name) name __asm__ (__hidden_asmname (#internal)) \
  379. __hidden_proto_hiddenattr (attrs);
  380. # define __hidden_proto_alias(name, thread, internal, attrs...) \
  381. extern thread __typeof (name) internal __hidden_proto_hiddenattr (attrs);
  382. # define __hidden_asmname(name) \
  383. __hidden_asmname1 (__USER_LABEL_PREFIX__, name)
  384. # define __hidden_asmname1(prefix, name) __hidden_asmname2(prefix, name)
  385. # define __hidden_asmname2(prefix, name) #prefix name
  386. # define __hidden_ver1(local, internal, name) \
  387. __hidden_ver2 (, local, internal, name)
  388. # define __hidden_ver2(thread, local, internal, name) \
  389. extern thread __typeof (name) __EI_##name \
  390. __asm__(__hidden_asmname (#internal)); \
  391. extern thread __typeof (name) __EI_##name \
  392. __attribute__((alias (__hidden_asmname (#local)))) \
  393. __attribute_copy__ (name)
  394. # define hidden_ver(local, name) __hidden_ver1(local, __GI_##name, name);
  395. # define hidden_def(name) __hidden_ver1(__GI_##name, name, name);
  396. # define hidden_def_alias(name, internal) \
  397. strong_alias (name, internal)
  398. # define hidden_data_def(name) hidden_def(name)
  399. # define hidden_data_def_alias(name, alias) hidden_def_alias(name, alias)
  400. # define hidden_tls_def(name) \
  401. __hidden_ver2 (__thread, __GI_##name, name, name);
  402. # define hidden_weak(name) \
  403. __hidden_ver1(__GI_##name, name, name) __attribute__((weak));
  404. # define hidden_data_weak(name) hidden_weak(name)
  405. # define hidden_nolink(name, lib, version) \
  406. __hidden_nolink1 (__GI_##name, __EI_##name, name, VERSION_##lib##_##version)
  407. # define __hidden_nolink1(local, internal, name, version) \
  408. __hidden_nolink2 (local, internal, name, version)
  409. # define __hidden_nolink2(local, internal, name, version) \
  410. extern __typeof (name) internal __attribute__ ((alias (#local))) \
  411. __attribute_copy__ (name); \
  412. __hidden_nolink3 (local, internal, #name "@" #version)
  413. # define __hidden_nolink3(local, internal, vername) \
  414. __asm__ (".symver " #internal ", " vername);
  415. # else
  416. /* For assembly, we need to do the opposite of what we do in C:
  417. in assembly gcc __REDIRECT stuff is not in place, so functions
  418. are defined by its normal name and we need to create the
  419. __GI_* alias to it, in C __REDIRECT causes the function definition
  420. to use __GI_* name and we need to add alias to the real name.
  421. There is no reason to use hidden_weak over hidden_def in assembly,
  422. but we provide it for consistency with the C usage.
  423. hidden_proto doesn't make sense for assembly but the equivalent
  424. is to call via the HIDDEN_JUMPTARGET macro instead of JUMPTARGET. */
  425. # define hidden_def(name) strong_alias (name, __GI_##name)
  426. # define hidden_def_alias(name, alias) strong_alias (name, alias)
  427. # define hidden_weak(name) hidden_def (name)
  428. # define hidden_ver(local, name) strong_alias (local, __GI_##name)
  429. # define hidden_data_def(name) strong_data_alias (name, __GI_##name)
  430. # define hidden_data_def_alias(name, alias) strong_data_alias (name, alias)
  431. # define hidden_tls_def(name) hidden_data_def (name)
  432. # define hidden_data_weak(name) hidden_data_def (name)
  433. # define HIDDEN_JUMPTARGET(name) __GI_##name
  434. # endif
  435. #else
  436. # ifndef __ASSEMBLER__
  437. # if !defined SHARED && IS_IN (libc) && !defined LIBC_NONSHARED \
  438. && (!defined PIC || !defined NO_HIDDEN_EXTERN_FUNC_IN_PIE) \
  439. && !defined NO_HIDDEN
  440. # define __hidden_proto_hiddenattr(attrs...) \
  441. __attribute__ ((visibility ("hidden"), ##attrs))
  442. # define hidden_proto(name, attrs...) \
  443. __hidden_proto (name, , name, ##attrs)
  444. # define hidden_proto_alias(name, alias, attrs...) \
  445. __hidden_proto_alias (name, , alias, ##attrs)
  446. # define hidden_tls_proto(name, attrs...) \
  447. __hidden_proto (name, __thread, name, ##attrs)
  448. # define __hidden_proto(name, thread, internal, attrs...) \
  449. extern thread __typeof (name) name __hidden_proto_hiddenattr (attrs);
  450. # define __hidden_proto_alias(name, thread, internal, attrs...) \
  451. extern thread __typeof (name) internal __hidden_proto_hiddenattr (attrs);
  452. # else
  453. # define hidden_proto(name, attrs...)
  454. # define hidden_proto_alias(name, alias, attrs...)
  455. # define hidden_tls_proto(name, attrs...)
  456. # endif
  457. # else
  458. # define HIDDEN_JUMPTARGET(name) JUMPTARGET(name)
  459. # endif /* Not __ASSEMBLER__ */
  460. # define hidden_weak(name)
  461. # define hidden_def(name)
  462. # define hidden_def_alias(name, alias)
  463. # define hidden_ver(local, name)
  464. # define hidden_data_weak(name)
  465. # define hidden_data_def(name)
  466. # define hidden_data_def_alias(name, alias)
  467. # define hidden_tls_def(name)
  468. # define hidden_nolink(name, lib, version)
  469. #endif
  470. #if IS_IN (libc)
  471. # define libc_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  472. # define libc_hidden_proto_alias(name, alias, attrs...) \
  473. hidden_proto_alias (name, alias, ##attrs)
  474. # define libc_hidden_tls_proto(name, attrs...) hidden_tls_proto (name, ##attrs)
  475. # define libc_hidden_def(name) hidden_def (name)
  476. # define libc_hidden_weak(name) hidden_weak (name)
  477. # define libc_hidden_nolink_sunrpc(name, version) hidden_nolink (name, libc, version)
  478. # define libc_hidden_ver(local, name) hidden_ver (local, name)
  479. # define libc_hidden_data_def(name) hidden_data_def (name)
  480. # define libc_hidden_data_def_alias(name, alias) hidden_data_def_alias (name, alias)
  481. # define libc_hidden_tls_def(name) hidden_tls_def (name)
  482. # define libc_hidden_data_weak(name) hidden_data_weak (name)
  483. #else
  484. # define libc_hidden_proto(name, attrs...)
  485. # define libc_hidden_proto_alias(name, alias, attrs...)
  486. # define libc_hidden_tls_proto(name, attrs...)
  487. # define libc_hidden_def(name)
  488. # define libc_hidden_weak(name)
  489. # define libc_hidden_ver(local, name)
  490. # define libc_hidden_data_def(name)
  491. # define libc_hidden_data_def_alias(name, alias)
  492. # define libc_hidden_tls_def(name)
  493. # define libc_hidden_data_weak(name)
  494. #endif
  495. #if IS_IN (rtld)
  496. # define rtld_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  497. # define rtld_hidden_def(name) hidden_def (name)
  498. # define rtld_hidden_weak(name) hidden_weak (name)
  499. # define rtld_hidden_data_def(name) hidden_data_def (name)
  500. #else
  501. # define rtld_hidden_proto(name, attrs...)
  502. # define rtld_hidden_def(name)
  503. # define rtld_hidden_weak(name)
  504. # define rtld_hidden_data_def(name)
  505. #endif
  506. #if IS_IN (libm)
  507. # define libm_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  508. # define libm_hidden_def(name) hidden_def (name)
  509. # define libm_hidden_weak(name) hidden_weak (name)
  510. # define libm_hidden_ver(local, name) hidden_ver (local, name)
  511. #else
  512. # define libm_hidden_proto(name, attrs...)
  513. # define libm_hidden_def(name)
  514. # define libm_hidden_weak(name)
  515. # define libm_hidden_ver(local, name)
  516. #endif
  517. #if IS_IN (libmvec)
  518. # define libmvec_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  519. # define libmvec_hidden_def(name) hidden_def (name)
  520. #else
  521. # define libmvec_hidden_proto(name, attrs...)
  522. # define libmvec_hidden_def(name)
  523. #endif
  524. #if IS_IN (libresolv)
  525. # define libresolv_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  526. # define libresolv_hidden_def(name) hidden_def (name)
  527. # define libresolv_hidden_data_def(name) hidden_data_def (name)
  528. #else
  529. # define libresolv_hidden_proto(name, attrs...)
  530. # define libresolv_hidden_def(name)
  531. # define libresolv_hidden_data_def(name)
  532. #endif
  533. #if IS_IN (libpthread)
  534. # define libpthread_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  535. # define libpthread_hidden_def(name) hidden_def (name)
  536. #else
  537. # define libpthread_hidden_proto(name, attrs...)
  538. # define libpthread_hidden_def(name)
  539. #endif
  540. #if IS_IN (librt)
  541. # define librt_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  542. # define librt_hidden_ver(local, name) hidden_ver (local, name)
  543. #else
  544. # define librt_hidden_proto(name, attrs...)
  545. # define librt_hidden_ver(local, name)
  546. #endif
  547. #if IS_IN (libnsl)
  548. # define libnsl_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  549. # define libnsl_hidden_nolink_def(name, version) hidden_nolink (name, libnsl, version)
  550. #else
  551. # define libnsl_hidden_proto(name, attrs...)
  552. #endif
  553. #define libc_hidden_builtin_proto(name, attrs...) libc_hidden_proto (name, ##attrs)
  554. #define libc_hidden_builtin_def(name) libc_hidden_def (name)
  555. #define libc_hidden_ldbl_proto(name, attrs...) libc_hidden_proto (name, ##attrs)
  556. #ifdef __ASSEMBLER__
  557. # define HIDDEN_BUILTIN_JUMPTARGET(name) HIDDEN_JUMPTARGET(name)
  558. #endif
  559. #if IS_IN (libanl)
  560. # define libanl_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
  561. #else
  562. # define libanl_hidden_proto(name, attrs...)
  563. #endif
  564. /* Get some dirty hacks. */
  565. #include <symbol-hacks.h>
  566. /* Move compatibility symbols out of the way by placing them all in a
  567. special section. */
  568. #ifndef __ASSEMBLER__
  569. # define attribute_compat_text_section \
  570. __attribute__ ((section (".text.compat")))
  571. #else
  572. # define compat_text_section .section ".text.compat", "ax";
  573. #endif
  574. /* Helper / base macros for indirect function symbols. */
  575. #define __ifunc_resolver(type_name, name, expr, init, classifier, ...) \
  576. classifier inhibit_stack_protector \
  577. __typeof (type_name) *name##_ifunc (__VA_ARGS__) \
  578. { \
  579. init (); \
  580. __typeof (type_name) *res = expr; \
  581. return res; \
  582. }
  583. #ifdef HAVE_GCC_IFUNC
  584. # define __ifunc_args(type_name, name, expr, init, ...) \
  585. extern __typeof (type_name) name __attribute__ \
  586. ((ifunc (#name "_ifunc"))); \
  587. DIAG_PUSH_NEEDS_COMMENT_CLANG; \
  588. DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wunused-function"); \
  589. __ifunc_resolver (type_name, name, expr, init, static, __VA_ARGS__); \
  590. DIAG_POP_NEEDS_COMMENT_CLANG;
  591. # define __ifunc_args_hidden(type_name, name, expr, init, ...) \
  592. __ifunc_args (type_name, name, expr, init, __VA_ARGS__)
  593. #else
  594. /* Gcc does not support __attribute__ ((ifunc (...))). Use the old behaviour
  595. as fallback. But keep in mind that the debug information for the ifunc
  596. resolver functions is not correct. It contains the ifunc'ed function as
  597. DW_AT_linkage_name. E.g. lldb uses this field and an inferior function
  598. call of the ifunc'ed function will fail due to "no matching function for
  599. call to ..." because the ifunc'ed function and the resolver function have
  600. different signatures. (Gcc support is disabled at least on a ppc64le
  601. Ubuntu 14.04 system.) */
  602. # define __ifunc_args(type_name, name, expr, init, ...) \
  603. extern __typeof (type_name) name; \
  604. __typeof (type_name) *name##_ifunc (__VA_ARGS__) __asm__ (#name); \
  605. __ifunc_resolver (type_name, name, expr, init, , __VA_ARGS__) \
  606. __asm__ (".type " #name ", %gnu_indirect_function");
  607. # define __ifunc_args_hidden(type_name, name, expr, init, ...) \
  608. extern __typeof (type_name) __libc_##name; \
  609. __ifunc (type_name, __libc_##name, expr, __VA_ARGS__, init) \
  610. strong_alias (__libc_##name, name);
  611. #endif /* !HAVE_GCC_IFUNC */
  612. #define __ifunc(type_name, name, expr, arg, init) \
  613. __ifunc_args (type_name, name, expr, init, arg)
  614. #define __ifunc_hidden(type_name, name, expr, arg, init) \
  615. __ifunc_args_hidden (type_name, name, expr, init, arg)
  616. /* The following macros are used for indirect function symbols in libc.so.
  617. First of all, you need to have the function prototyped somewhere,
  618. say in foo.h:
  619. int foo (int __bar);
  620. If you have an implementation for foo which e.g. uses a special hardware
  621. feature which isn't available on all machines where this libc.so will be
  622. used but decidable if available at runtime e.g. via hwcaps, you can provide
  623. two or multiple implementations of foo:
  624. int __foo_default (int __bar)
  625. {
  626. return __bar;
  627. }
  628. int __foo_special (int __bar)
  629. {
  630. return __bar;
  631. }
  632. If your function foo has no libc_hidden_proto (foo) defined for PLT
  633. bypassing, you can use:
  634. #define INIT_ARCH() unsigned long int hwcap = __GLRO(dl_hwcap);
  635. libc_ifunc (foo, (hwcap & HWCAP_SPECIAL) ? __foo_special : __foo_default);
  636. This will define a resolver function for foo which returns __foo_special or
  637. __foo_default depending on your specified expression. Please note that you
  638. have to define a macro function INIT_ARCH before using libc_ifunc macro as
  639. it is called by the resolver function before evaluating the specified
  640. expression. In this example it is used to prepare the hwcap variable.
  641. The resolver function is assigned to an ifunc'ed symbol foo. Calls to foo
  642. from inside or outside of libc.so will be indirected by a PLT call.
  643. If your function foo has a libc_hidden_proto (foo) defined for PLT bypassing
  644. and calls to foo within libc.so should always go to one specific
  645. implementation of foo e.g. __foo_default then you have to add:
  646. __hidden_ver1 (__foo_default, __GI_foo, __foo_default);
  647. or a tweaked definition of libc_hidden_def macro after the __foo_default
  648. function definition. Calls to foo within libc.so will always go directly to
  649. __foo_default. Calls to foo from outside libc.so will be indirected by a
  650. PLT call to ifunc'ed symbol foo which you have to define in a separate
  651. compile unit:
  652. #define foo __redirect_foo
  653. #include <foo.h>
  654. #undef foo
  655. extern __typeof (__redirect_foo) __foo_default attribute_hidden;
  656. extern __typeof (__redirect_foo) __foo_special attribute_hidden;
  657. libc_ifunc_redirected (__redirect_foo, foo,
  658. (hwcap & HWCAP_SPECIAL)
  659. ? __foo_special
  660. : __foo_default);
  661. This will define the ifunc'ed symbol foo like above. The redirection of foo
  662. in header file is needed to omit an additional definition of __GI_foo which
  663. would end in a linker error while linking libc.so. You have to specify
  664. __redirect_foo as first parameter which is used within libc_ifunc_redirected
  665. macro in conjunction with typeof to define the ifunc'ed symbol foo.
  666. If your function foo has a libc_hidden_proto (foo) defined and calls to foo
  667. within or from outside libc.so should go via ifunc'ed symbol, then you have
  668. to use:
  669. libc_ifunc_hidden (foo, foo,
  670. (hwcap & HWCAP_SPECIAL)
  671. ? __foo_special
  672. : __foo_default);
  673. libc_hidden_def (foo)
  674. The first parameter foo of libc_ifunc_hidden macro is used in the same way
  675. as for libc_ifunc_redirected macro. */
  676. #define libc_ifunc(name, expr) __ifunc (name, name, expr, void, INIT_ARCH)
  677. #define libc_ifunc_redirected(redirected_name, name, expr) \
  678. __ifunc (redirected_name, name, expr, void, INIT_ARCH)
  679. #define libc_ifunc_hidden(redirected_name, name, expr) \
  680. __ifunc_hidden (redirected_name, name, expr, void, INIT_ARCH)
  681. /* The body of the function is supposed to use __get_cpu_features
  682. which will, if necessary, initialize the data first. */
  683. #define libm_ifunc_init()
  684. #define libm_ifunc(name, expr) \
  685. __ifunc (name, name, expr, void, libm_ifunc_init)
  686. /* These macros facilitate sharing source files with gnulib.
  687. They are here instead of sys/cdefs.h because they should not be
  688. used in public header files.
  689. Their definitions should be kept consistent with the definitions in
  690. gnulib-common.m4, but it is not necessary to cater to old non-GCC
  691. compilers, since they will only be used while building glibc itself.
  692. (Note that _GNUC_PREREQ cannot be used in this file.) */
  693. /* Define as a marker that can be attached to declarations that might not
  694. be used. This helps to reduce warnings, such as from
  695. GCC -Wunused-parameter. */
  696. #if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
  697. # define _GL_UNUSED __attribute__ ((__unused__))
  698. #else
  699. # define _GL_UNUSED
  700. #endif
  701. /* gcc supports the "unused" attribute on possibly unused labels, and
  702. g++ has since version 4.5. Note to support C++ as well as C,
  703. _GL_UNUSED_LABEL should be used with a trailing ; */
  704. #if !defined __cplusplus || __GNUC__ > 4 \
  705. || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
  706. # define _GL_UNUSED_LABEL _GL_UNUSED
  707. #else
  708. # define _GL_UNUSED_LABEL
  709. #endif
  710. /* The __pure__ attribute was added in gcc 2.96. */
  711. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
  712. # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
  713. #else
  714. # define _GL_ATTRIBUTE_PURE /* empty */
  715. #endif
  716. /* The __const__ attribute was added in gcc 2.95. */
  717. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
  718. # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
  719. #else
  720. # define _GL_ATTRIBUTE_CONST /* empty */
  721. #endif
  722. #endif /* !_ISOMAC */
  723. #endif /* libc-symbols.h */