libc: Extend __libc_freeres framework (Bug 23329).
[glibc.git] / include / libc-symbols.h
blob8b9273c13a19f2658105c7997267d9086adae716
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-2018 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef _LIBC_SYMBOLS_H
21 #define _LIBC_SYMBOLS_H 1
23 /* This file is included implicitly in the compilation of every source file,
24 using -include. It includes config.h. */
26 /* Enable declarations of GNU extensions, since we are compiling them. */
27 #define _GNU_SOURCE 1
29 #ifdef MODULE_NAME
31 /* Use `#if IS_IN (module)` to detect what component is being compiled. */
32 #define PASTE_NAME1(a,b) a##b
33 #define PASTE_NAME(a,b) PASTE_NAME1 (a,b)
34 #define IN_MODULE PASTE_NAME (MODULE_, MODULE_NAME)
35 #define IS_IN(lib) (IN_MODULE == MODULE_##lib)
37 /* True if the current module is a versioned library. Versioned
38 library names culled from shlib-versions files are assigned a
39 MODULE_* value greater than MODULE_LIBS_BEGIN. */
40 #define IS_IN_LIB (IN_MODULE > MODULE_LIBS_BEGIN)
42 /* The testsuite, and some other ancillary code, should be compiled against
43 as close an approximation to the installed headers as possible.
44 Defining this symbol disables most internal-use-only declarations
45 provided by this header, and all those provided by other internal
46 wrapper headers. */
47 #if IS_IN (testsuite) || defined IS_IN_build || defined __cplusplus
48 # define _ISOMAC 1
49 #endif
51 #else
52 /* The generation process for a few files created very early in the
53 build (notably libc-modules.h itself) involves preprocessing this
54 header without defining MODULE_NAME. Under these conditions,
55 internal declarations (especially from config.h) must be visible,
56 but IS_IN should always evaluate as false. */
57 # define IS_IN(lib) 0
58 # define IS_IN_LIB 0
59 # define IN_MODULE (-1)
60 #endif
62 #ifndef _ISOMAC
64 /* This is defined for the compilation of all C library code. features.h
65 tests this to avoid inclusion of stubs.h while compiling the library,
66 before stubs.h has been generated. Some library code that is shared
67 with other packages also tests this symbol to see if it is being
68 compiled as part of the C library. We must define this before including
69 config.h, because it makes some definitions conditional on whether libc
70 itself is being compiled, or just some generator program. */
71 #define _LIBC 1
73 /* Some files must be compiled with optimization on. */
74 #if !defined __ASSEMBLER__ && !defined __OPTIMIZE__
75 # error "glibc cannot be compiled without optimization"
76 #endif
78 /* -ffast-math cannot be applied to the C library, as it alters the ABI.
79 Some test components that use -ffast-math are currently not part of
80 IS_IN (testsuite) for technical reasons, so we have a secondary override. */
81 #if defined __FAST_MATH__ && !defined TEST_FAST_MATH
82 # error "glibc must not be compiled with -ffast-math"
83 #endif
85 #include <config.h>
87 /* When PIC is defined and SHARED isn't defined, we are building PIE
88 by default. */
89 #if defined PIC && !defined SHARED
90 # define BUILD_PIE_DEFAULT 1
91 #else
92 # define BUILD_PIE_DEFAULT 0
93 #endif
95 /* Define this for the benefit of portable GNU code that wants to check it.
96 Code that checks with #if will not #include <config.h> again, since we've
97 already done it (and this file is implicitly included in every compile,
98 via -include). Code that checks with #ifdef will #include <config.h>,
99 but that file should always be idempotent (i.e., it's just #define/#undef
100 and nothing else anywhere should be changing the macro state it touches),
101 so it's harmless. */
102 #define HAVE_CONFIG_H 0
104 /* Define these macros for the benefit of portable GNU code that wants to check
105 them. Of course, STDC_HEADERS is never false when building libc! */
106 #define STDC_HEADERS 1
107 #define HAVE_MBSTATE_T 1
108 #define HAVE_MBSRTOWCS 1
109 #define HAVE_LIBINTL_H 1
110 #define HAVE_WCTYPE_H 1
111 #define HAVE_ISWCTYPE 1
112 #define ENABLE_NLS 1
114 /* The symbols in all the user (non-_) macros are C symbols. */
116 #ifndef __SYMBOL_PREFIX
117 # define __SYMBOL_PREFIX
118 #endif
120 #ifndef C_SYMBOL_NAME
121 # define C_SYMBOL_NAME(name) name
122 #endif
124 #ifndef ASM_LINE_SEP
125 # define ASM_LINE_SEP ;
126 #endif
128 #ifndef __ASSEMBLER__
129 /* GCC understands weak symbols and aliases; use its interface where
130 possible, instead of embedded assembly language. */
132 /* Define ALIASNAME as a strong alias for NAME. */
133 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
134 # define _strong_alias(name, aliasname) \
135 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
137 /* This comes between the return type and function name in
138 a function definition to make that definition weak. */
139 # define weak_function __attribute__ ((weak))
140 # define weak_const_function __attribute__ ((weak, __const__))
142 /* Define ALIASNAME as a weak alias for NAME.
143 If weak aliases are not available, this defines a strong alias. */
144 # define weak_alias(name, aliasname) _weak_alias (name, aliasname)
145 # define _weak_alias(name, aliasname) \
146 extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
148 /* Same as WEAK_ALIAS, but mark symbol as hidden. */
149 # define weak_hidden_alias(name, aliasname) \
150 _weak_hidden_alias (name, aliasname)
151 # define _weak_hidden_alias(name, aliasname) \
152 extern __typeof (name) aliasname \
153 __attribute__ ((weak, alias (#name), __visibility__ ("hidden")));
155 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
156 # define weak_extern(symbol) _weak_extern (weak symbol)
157 # define _weak_extern(expr) _Pragma (#expr)
159 /* In shared builds, the expression call_function_static_weak
160 (FUNCTION-SYMBOL, ARGUMENTS) invokes FUNCTION-SYMBOL (an
161 identifier) unconditionally, with the (potentially empty) argument
162 list ARGUMENTS. In static builds, if FUNCTION-SYMBOL has a
163 definition, the function is invoked as before; if FUNCTION-SYMBOL
164 is NULL, no call is performed. */
165 # ifdef SHARED
166 # define call_function_static_weak(func, ...) func (__VA_ARGS__)
167 # else /* !SHARED */
168 # define call_function_static_weak(func, ...) \
169 ({ \
170 extern __typeof__ (func) func weak_function; \
171 (func != NULL ? func (__VA_ARGS__) : (void)0); \
173 # endif
175 #else /* __ASSEMBLER__ */
177 # ifdef HAVE_ASM_SET_DIRECTIVE
178 # define strong_alias(original, alias) \
179 .globl C_SYMBOL_NAME (alias) ASM_LINE_SEP \
180 .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
181 # define strong_data_alias(original, alias) strong_alias(original, alias)
182 # else
183 # define strong_alias(original, alias) \
184 .globl C_SYMBOL_NAME (alias) ASM_LINE_SEP \
185 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
186 # define strong_data_alias(original, alias) strong_alias(original, alias)
187 # endif
189 # define weak_alias(original, alias) \
190 .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
191 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
193 # define weak_extern(symbol) \
194 .weak C_SYMBOL_NAME (symbol)
196 #endif /* __ASSEMBLER__ */
198 /* Determine the return address. */
199 #define RETURN_ADDRESS(nr) \
200 __builtin_extract_return_addr (__builtin_return_address (nr))
202 /* When a reference to SYMBOL is encountered, the linker will emit a
203 warning message MSG. */
204 /* We want the .gnu.warning.SYMBOL section to be unallocated. */
205 #define __make_section_unallocated(section_string) \
206 asm (".section " section_string "\n\t.previous");
208 /* Tacking on "\n\t#" to the section name makes gcc put it's bogus
209 section attributes on what looks like a comment to the assembler. */
210 #ifdef HAVE_SECTION_QUOTES
211 # define __sec_comment "\"\n\t#\""
212 #else
213 # define __sec_comment "\n\t#"
214 #endif
215 #define link_warning(symbol, msg) \
216 __make_section_unallocated (".gnu.warning." #symbol) \
217 static const char __evoke_link_warning_##symbol[] \
218 __attribute__ ((used, section (".gnu.warning." #symbol __sec_comment))) \
219 = msg;
221 /* A canned warning for sysdeps/stub functions. */
222 #define stub_warning(name) \
223 __make_section_unallocated (".gnu.glibc-stub." #name) \
224 link_warning (name, #name " is not implemented and will always fail")
226 /* Warning for linking functions calling dlopen into static binaries. */
227 #ifdef SHARED
228 #define static_link_warning(name)
229 #else
230 #define static_link_warning(name) static_link_warning1(name)
231 #define static_link_warning1(name) \
232 link_warning(name, "Using '" #name "' in statically linked applications \
233 requires at runtime the shared libraries from the glibc version used \
234 for linking")
235 #endif
237 /* Resource Freeing Hooks:
239 Normally a process exits and the OS cleans up any allocated
240 memory. However, when tooling like mtrace or valgrind is monitoring
241 the process we need to free all resources that are part of the
242 process in order to provide the consistency required to track
243 memory leaks.
245 A single public API exists and is __libc_freeres(), and this is used
246 by applications like valgrind to freee resouces.
248 There are 3 cases:
250 (a) __libc_freeres
252 In this case all you need to do is define the freeing routine:
254 foo.c:
255 libfoo_freeres_fn (foo_freeres)
257 complex_free (mem);
260 This ensures the function is called at the right point to free
261 resources.
263 (b) __libc_freeres_ptr
265 The framework for (a) iterates over the list of pointers-to-free
266 in (b) and frees them.
268 foo.c:
269 libc_freeres_ptr (static char *foo_buffer);
271 Freeing these resources alaways happens last and is equivalent
272 to registering a function that does 'free (foo_buffer)'.
274 (c) Explicit lists of free routines to call or objects to free.
276 It is the intended goal to remove (a) and (b) which have some
277 non-determinism based on link order, and instead use explicit
278 lists of functions and frees to resolve cleanup ordering issues
279 and make it easy to debug and maintain.
281 As of today the following subsystems use (c):
283 Per-thread cleanup:
284 * malloc/thread-freeres.c
286 libdl cleanup:
287 * dlfcn/dlfreeres.c
289 libpthread cleanup:
290 * nptl/nptlfreeres.c
292 So if you need any shutdown routines to run you should add them
293 directly to the appropriate subsystem's shutdown list. */
295 /* Resource pointers to free in libc.so. */
296 #define libc_freeres_ptr(decl) \
297 __make_section_unallocated ("__libc_freeres_ptrs, \"aw\", %nobits") \
298 decl __attribute__ ((section ("__libc_freeres_ptrs" __sec_comment)))
300 /* Resource freeing functions from libc.so go in this section. */
301 #define __libc_freeres_fn_section \
302 __attribute__ ((section ("__libc_freeres_fn")))
304 /* Resource freeing functions for libc.so. */
305 #define libc_freeres_fn(name) \
306 static void name (void) __attribute_used__ __libc_freeres_fn_section; \
307 text_set_element (__libc_subfreeres, name); \
308 static void name (void)
310 /* Declare SYMBOL to be TYPE (`function' or `object') of SIZE bytes
311 alias to ORIGINAL, when the assembler supports such declarations
312 (such as in ELF).
313 This is only necessary when defining something in assembly, or playing
314 funny alias games where the size should be other than what the compiler
315 thinks it is. */
316 #define declare_symbol_alias(symbol, original, type, size) \
317 declare_symbol_alias_1 (symbol, original, type, size)
318 #ifdef __ASSEMBLER__
319 # define declare_symbol_alias_1(symbol, original, type, size) \
320 strong_alias (original, symbol); \
321 .type C_SYMBOL_NAME (symbol), %##type; \
322 .size C_SYMBOL_NAME (symbol), size
323 #else /* Not __ASSEMBLER__. */
324 # define declare_symbol_alias_1(symbol, original, type, size) \
325 asm (".globl " __SYMBOL_PREFIX #symbol \
326 "\n\t" declare_symbol_alias_1_alias (symbol, original) \
327 "\n\t.type " __SYMBOL_PREFIX #symbol ", " \
328 "%" #type \
329 "\n\t.size " __SYMBOL_PREFIX #symbol ", " #size);
330 # ifdef HAVE_ASM_SET_DIRECTIVE
331 # define declare_symbol_alias_1_alias(symbol, original) \
332 ".set " __SYMBOL_PREFIX #symbol ", " __SYMBOL_PREFIX #original
333 # else
334 # define declare_symbol_alias_1_alias(symbol, original) \
335 __SYMBOL_PREFIX #symbol " = " __SYMBOL_PREFIX #original
336 # endif /* HAVE_ASM_SET_DIRECTIVE */
337 #endif /* __ASSEMBLER__ */
344 /* Symbol set support macros. */
346 /* Make SYMBOL, which is in the text segment, an element of SET. */
347 #define text_set_element(set, symbol) _elf_set_element(set, symbol)
348 /* Make SYMBOL, which is in the data segment, an element of SET. */
349 #define data_set_element(set, symbol) _elf_set_element(set, symbol)
350 /* Make SYMBOL, which is in the bss segment, an element of SET. */
351 #define bss_set_element(set, symbol) _elf_set_element(set, symbol)
353 /* These are all done the same way in ELF.
354 There is a new section created for each set. */
355 #ifdef SHARED
356 /* When building a shared library, make the set section writable,
357 because it will need to be relocated at run time anyway. */
358 # define _elf_set_element(set, symbol) \
359 static const void *__elf_set_##set##_element_##symbol##__ \
360 __attribute__ ((used, section (#set))) = &(symbol)
361 #else
362 # define _elf_set_element(set, symbol) \
363 static const void *const __elf_set_##set##_element_##symbol##__ \
364 __attribute__ ((used, section (#set))) = &(symbol)
365 #endif
367 /* Define SET as a symbol set. This may be required (it is in a.out) to
368 be able to use the set's contents. */
369 #define symbol_set_define(set) symbol_set_declare(set)
371 /* Declare SET for use in this module, if defined in another module.
372 In a shared library, this is always local to that shared object.
373 For static linking, the set might be wholly absent and so we use
374 weak references. */
375 #define symbol_set_declare(set) \
376 extern char const __start_##set[] __symbol_set_attribute; \
377 extern char const __stop_##set[] __symbol_set_attribute;
378 #ifdef SHARED
379 # define __symbol_set_attribute attribute_hidden
380 #else
381 # define __symbol_set_attribute __attribute__ ((weak))
382 #endif
384 /* Return a pointer (void *const *) to the first element of SET. */
385 #define symbol_set_first_element(set) ((void *const *) (&__start_##set))
387 /* Return true iff PTR (a void *const *) has been incremented
388 past the last element in SET. */
389 #define symbol_set_end_p(set, ptr) ((ptr) >= (void *const *) &__stop_##set)
391 /* Use symbol_version_reference to specify the version a symbol
392 reference should link to. Use symbol_version or
393 default_symbol_version for the definition of a versioned symbol.
394 The difference is that the latter is a no-op in non-shared
395 builds. */
396 #ifdef __ASSEMBLER__
397 # define symbol_version_reference(real, name, version) \
398 .symver real, name##@##version
399 #else /* !__ASSEMBLER__ */
400 # define symbol_version_reference(real, name, version) \
401 __asm__ (".symver " #real "," #name "@" #version)
402 #endif
404 #ifdef SHARED
405 # define symbol_version(real, name, version) \
406 symbol_version_reference(real, name, version)
407 # define default_symbol_version(real, name, version) \
408 _default_symbol_version(real, name, version)
409 # ifdef __ASSEMBLER__
410 # define _default_symbol_version(real, name, version) \
411 .symver real, name##@##@##version
412 # else
413 # define _default_symbol_version(real, name, version) \
414 __asm__ (".symver " #real "," #name "@@" #version)
415 # endif
416 #else
417 # define symbol_version(real, name, version)
418 # define default_symbol_version(real, name, version) \
419 strong_alias(real, name)
420 #endif
422 #if defined SHARED || defined LIBC_NONSHARED \
423 || (BUILD_PIE_DEFAULT && IS_IN (libc))
424 # define attribute_hidden __attribute__ ((visibility ("hidden")))
425 #else
426 # define attribute_hidden
427 #endif
429 #define attribute_tls_model_ie __attribute__ ((tls_model ("initial-exec")))
431 #define attribute_relro __attribute__ ((section (".data.rel.ro")))
434 /* Used to disable stack protection in sensitive places, like ifunc
435 resolvers and early static TLS init. */
436 #ifdef HAVE_CC_NO_STACK_PROTECTOR
437 # define inhibit_stack_protector \
438 __attribute__ ((__optimize__ ("-fno-stack-protector")))
439 #else
440 # define inhibit_stack_protector
441 #endif
443 /* The following macros are used for PLT bypassing within libc.so
444 (and if needed other libraries similarly).
445 First of all, you need to have the function prototyped somewhere,
446 say in foo/foo.h:
448 int foo (int __bar);
450 If calls to foo within libc.so should always go to foo defined in libc.so,
451 then in include/foo.h you add:
453 libc_hidden_proto (foo)
455 line and after the foo function definition:
457 int foo (int __bar)
459 return __bar;
461 libc_hidden_def (foo)
465 int foo (int __bar)
467 return __bar;
469 libc_hidden_weak (foo)
471 Similarly for global data. If references to foo within libc.so should
472 always go to foo defined in libc.so, then in include/foo.h you add:
474 libc_hidden_proto (foo)
476 line and after foo's definition:
478 int foo = INITIAL_FOO_VALUE;
479 libc_hidden_data_def (foo)
483 int foo = INITIAL_FOO_VALUE;
484 libc_hidden_data_weak (foo)
486 If foo is normally just an alias (strong or weak) to some other function,
487 you should use the normal strong_alias first, then add libc_hidden_def
488 or libc_hidden_weak:
490 int baz (int __bar)
492 return __bar;
494 strong_alias (baz, foo)
495 libc_hidden_weak (foo)
497 If the function should be internal to multiple objects, say ld.so and
498 libc.so, the best way is to use:
500 #if IS_IN (libc) || IS_IN (rtld)
501 hidden_proto (foo)
502 #endif
504 in include/foo.h and the normal macros at all function definitions
505 depending on what DSO they belong to.
507 If versioned_symbol macro is used to define foo,
508 libc_hidden_ver macro should be used, as in:
510 int __real_foo (int __bar)
512 return __bar;
514 versioned_symbol (libc, __real_foo, foo, GLIBC_2_1);
515 libc_hidden_ver (__real_foo, foo) */
517 #if defined SHARED && !defined NO_HIDDEN
518 # ifndef __ASSEMBLER__
519 # define __hidden_proto_hiddenattr(attrs...) \
520 __attribute__ ((visibility ("hidden"), ##attrs))
521 # define hidden_proto(name, attrs...) \
522 __hidden_proto (name, , __GI_##name, ##attrs)
523 # define hidden_tls_proto(name, attrs...) \
524 __hidden_proto (name, __thread, __GI_##name, ##attrs)
525 # define __hidden_proto(name, thread, internal, attrs...) \
526 extern thread __typeof (name) name __asm__ (__hidden_asmname (#internal)) \
527 __hidden_proto_hiddenattr (attrs);
528 # define __hidden_asmname(name) \
529 __hidden_asmname1 (__USER_LABEL_PREFIX__, name)
530 # define __hidden_asmname1(prefix, name) __hidden_asmname2(prefix, name)
531 # define __hidden_asmname2(prefix, name) #prefix name
532 # define __hidden_ver1(local, internal, name) \
533 extern __typeof (name) __EI_##name __asm__(__hidden_asmname (#internal)); \
534 extern __typeof (name) __EI_##name \
535 __attribute__((alias (__hidden_asmname (#local))))
536 # define hidden_ver(local, name) __hidden_ver1(local, __GI_##name, name);
537 # define hidden_data_ver(local, name) hidden_ver(local, name)
538 # define hidden_def(name) __hidden_ver1(__GI_##name, name, name);
539 # define hidden_data_def(name) hidden_def(name)
540 # define hidden_weak(name) \
541 __hidden_ver1(__GI_##name, name, name) __attribute__((weak));
542 # define hidden_data_weak(name) hidden_weak(name)
543 # define hidden_nolink(name, lib, version) \
544 __hidden_nolink1 (__GI_##name, __EI_##name, name, VERSION_##lib##_##version)
545 # define __hidden_nolink1(local, internal, name, version) \
546 __hidden_nolink2 (local, internal, name, version)
547 # define __hidden_nolink2(local, internal, name, version) \
548 extern __typeof (name) internal __attribute__ ((alias (#local))); \
549 __hidden_nolink3 (local, internal, #name "@" #version)
550 # define __hidden_nolink3(local, internal, vername) \
551 __asm__ (".symver " #internal ", " vername);
552 # else
553 /* For assembly, we need to do the opposite of what we do in C:
554 in assembly gcc __REDIRECT stuff is not in place, so functions
555 are defined by its normal name and we need to create the
556 __GI_* alias to it, in C __REDIRECT causes the function definition
557 to use __GI_* name and we need to add alias to the real name.
558 There is no reason to use hidden_weak over hidden_def in assembly,
559 but we provide it for consistency with the C usage.
560 hidden_proto doesn't make sense for assembly but the equivalent
561 is to call via the HIDDEN_JUMPTARGET macro instead of JUMPTARGET. */
562 # define hidden_def(name) strong_alias (name, __GI_##name)
563 # define hidden_weak(name) hidden_def (name)
564 # define hidden_ver(local, name) strong_alias (local, __GI_##name)
565 # define hidden_data_def(name) strong_data_alias (name, __GI_##name)
566 # define hidden_data_weak(name) hidden_data_def (name)
567 # define hidden_data_ver(local, name) strong_data_alias (local, __GI_##name)
568 # define HIDDEN_JUMPTARGET(name) __GI_##name
569 # endif
570 #else
571 # ifndef __ASSEMBLER__
572 # if !defined SHARED && IS_IN (libc) && !defined LIBC_NONSHARED \
573 && (!defined PIC || !defined NO_HIDDEN_EXTERN_FUNC_IN_PIE) \
574 && !defined NO_HIDDEN
575 # define __hidden_proto_hiddenattr(attrs...) \
576 __attribute__ ((visibility ("hidden"), ##attrs))
577 # define hidden_proto(name, attrs...) \
578 __hidden_proto (name, , name, ##attrs)
579 # define hidden_tls_proto(name, attrs...) \
580 __hidden_proto (name, __thread, name, ##attrs)
581 # define __hidden_proto(name, thread, internal, attrs...) \
582 extern thread __typeof (name) name __hidden_proto_hiddenattr (attrs);
583 # else
584 # define hidden_proto(name, attrs...)
585 # define hidden_tls_proto(name, attrs...)
586 # endif
587 # else
588 # define HIDDEN_JUMPTARGET(name) JUMPTARGET(name)
589 # endif /* Not __ASSEMBLER__ */
590 # define hidden_weak(name)
591 # define hidden_def(name)
592 # define hidden_ver(local, name)
593 # define hidden_data_weak(name)
594 # define hidden_data_def(name)
595 # define hidden_data_ver(local, name)
596 # define hidden_nolink(name, lib, version)
597 #endif
599 #if IS_IN (libc)
600 # define libc_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
601 # define libc_hidden_tls_proto(name, attrs...) hidden_tls_proto (name, ##attrs)
602 # define libc_hidden_def(name) hidden_def (name)
603 # define libc_hidden_weak(name) hidden_weak (name)
604 # ifdef LINK_OBSOLETE_RPC
605 /* libc_hidden_nolink_sunrpc should only get used in sunrpc code. */
606 # define libc_hidden_nolink_sunrpc(name, version) hidden_def (name)
607 # else
608 # define libc_hidden_nolink_sunrpc(name, version) hidden_nolink (name, libc, version)
609 # endif
610 # define libc_hidden_ver(local, name) hidden_ver (local, name)
611 # define libc_hidden_data_def(name) hidden_data_def (name)
612 # define libc_hidden_data_weak(name) hidden_data_weak (name)
613 # define libc_hidden_data_ver(local, name) hidden_data_ver (local, name)
614 #else
615 # define libc_hidden_proto(name, attrs...)
616 # define libc_hidden_tls_proto(name, attrs...)
617 # define libc_hidden_def(name)
618 # define libc_hidden_weak(name)
619 # define libc_hidden_ver(local, name)
620 # define libc_hidden_data_def(name)
621 # define libc_hidden_data_weak(name)
622 # define libc_hidden_data_ver(local, name)
623 #endif
625 #if IS_IN (rtld) && !defined NO_RTLD_HIDDEN
626 # define rtld_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
627 # define rtld_hidden_tls_proto(name, attrs...) hidden_tls_proto (name, ##attrs)
628 # define rtld_hidden_def(name) hidden_def (name)
629 # define rtld_hidden_weak(name) hidden_weak (name)
630 # define rtld_hidden_ver(local, name) hidden_ver (local, name)
631 # define rtld_hidden_data_def(name) hidden_data_def (name)
632 # define rtld_hidden_data_weak(name) hidden_data_weak (name)
633 # define rtld_hidden_data_ver(local, name) hidden_data_ver (local, name)
634 #else
635 # define rtld_hidden_proto(name, attrs...)
636 # define rtld_hidden_tls_proto(name, attrs...)
637 # define rtld_hidden_def(name)
638 # define rtld_hidden_weak(name)
639 # define rtld_hidden_ver(local, name)
640 # define rtld_hidden_data_def(name)
641 # define rtld_hidden_data_weak(name)
642 # define rtld_hidden_data_ver(local, name)
643 #endif
645 #if IS_IN (libm)
646 # define libm_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
647 # define libm_hidden_tls_proto(name, attrs...) hidden_tls_proto (name, ##attrs)
648 # define libm_hidden_def(name) hidden_def (name)
649 # define libm_hidden_weak(name) hidden_weak (name)
650 # define libm_hidden_ver(local, name) hidden_ver (local, name)
651 # define libm_hidden_data_def(name) hidden_data_def (name)
652 # define libm_hidden_data_weak(name) hidden_data_weak (name)
653 # define libm_hidden_data_ver(local, name) hidden_data_ver (local, name)
654 #else
655 # define libm_hidden_proto(name, attrs...)
656 # define libm_hidden_tls_proto(name, attrs...)
657 # define libm_hidden_def(name)
658 # define libm_hidden_weak(name)
659 # define libm_hidden_ver(local, name)
660 # define libm_hidden_data_def(name)
661 # define libm_hidden_data_weak(name)
662 # define libm_hidden_data_ver(local, name)
663 #endif
665 #if IS_IN (libmvec)
666 # define libmvec_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
667 # define libmvec_hidden_tls_proto(name, attrs...) hidden_tls_proto (name, ##attrs)
668 # define libmvec_hidden_def(name) hidden_def (name)
669 # define libmvec_hidden_weak(name) hidden_weak (name)
670 # define libmvec_hidden_ver(local, name) hidden_ver (local, name)
671 # define libmvec_hidden_data_def(name) hidden_data_def (name)
672 # define libmvec_hidden_data_weak(name) hidden_data_weak (name)
673 # define libmvec_hidden_data_ver(local, name) hidden_data_ver (local, name)
674 #else
675 # define libmvec_hidden_proto(name, attrs...)
676 # define libmvec_hidden_tls_proto(name, attrs...)
677 # define libmvec_hidden_def(name)
678 # define libmvec_hidden_weak(name)
679 # define libmvec_hidden_ver(local, name)
680 # define libmvec_hidden_data_def(name)
681 # define libmvec_hidden_data_weak(name)
682 # define libmvec_hidden_data_ver(local, name)
683 #endif
685 #if IS_IN (libresolv)
686 # define libresolv_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
687 # define libresolv_hidden_tls_proto(name, attrs...) \
688 hidden_tls_proto (name, ##attrs)
689 # define libresolv_hidden_def(name) hidden_def (name)
690 # define libresolv_hidden_weak(name) hidden_weak (name)
691 # define libresolv_hidden_ver(local, name) hidden_ver (local, name)
692 # define libresolv_hidden_data_def(name) hidden_data_def (name)
693 # define libresolv_hidden_data_weak(name) hidden_data_weak (name)
694 # define libresolv_hidden_data_ver(local, name) hidden_data_ver (local, name)
695 #else
696 # define libresolv_hidden_proto(name, attrs...)
697 # define libresolv_hidden_tls_proto(name, attrs...)
698 # define libresolv_hidden_def(name)
699 # define libresolv_hidden_weak(name)
700 # define libresolv_hidden_ver(local, name)
701 # define libresolv_hidden_data_def(name)
702 # define libresolv_hidden_data_weak(name)
703 # define libresolv_hidden_data_ver(local, name)
704 #endif
706 #if IS_IN (librt)
707 # define librt_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
708 # define librt_hidden_tls_proto(name, attrs...) \
709 hidden_tls_proto (name, ##attrs)
710 # define librt_hidden_def(name) hidden_def (name)
711 # define librt_hidden_weak(name) hidden_weak (name)
712 # define librt_hidden_ver(local, name) hidden_ver (local, name)
713 # define librt_hidden_data_def(name) hidden_data_def (name)
714 # define librt_hidden_data_weak(name) hidden_data_weak (name)
715 # define librt_hidden_data_ver(local, name) hidden_data_ver (local, name)
716 #else
717 # define librt_hidden_proto(name, attrs...)
718 # define librt_hidden_tls_proto(name, attrs...)
719 # define librt_hidden_def(name)
720 # define librt_hidden_weak(name)
721 # define librt_hidden_ver(local, name)
722 # define librt_hidden_data_def(name)
723 # define librt_hidden_data_weak(name)
724 # define librt_hidden_data_ver(local, name)
725 #endif
727 #if IS_IN (libdl)
728 # define libdl_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
729 # define libdl_hidden_tls_proto(name, attrs...) \
730 hidden_tls_proto (name, ##attrs)
731 # define libdl_hidden_def(name) hidden_def (name)
732 # define libdl_hidden_weak(name) hidden_weak (name)
733 # define libdl_hidden_ver(local, name) hidden_ver (local, name)
734 # define libdl_hidden_data_def(name) hidden_data_def (name)
735 # define libdl_hidden_data_weak(name) hidden_data_weak (name)
736 # define libdl_hidden_data_ver(local, name) hidden_data_ver (local, name)
737 #else
738 # define libdl_hidden_proto(name, attrs...)
739 # define libdl_hidden_tls_proto(name, attrs...)
740 # define libdl_hidden_def(name)
741 # define libdl_hidden_weak(name)
742 # define libdl_hidden_ver(local, name)
743 # define libdl_hidden_data_def(name)
744 # define libdl_hidden_data_weak(name)
745 # define libdl_hidden_data_ver(local, name)
746 #endif
748 #if IS_IN (libnss_files)
749 # define libnss_files_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
750 # define libnss_files_hidden_tls_proto(name, attrs...) \
751 hidden_tls_proto (name, ##attrs)
752 # define libnss_files_hidden_def(name) hidden_def (name)
753 # define libnss_files_hidden_weak(name) hidden_weak (name)
754 # define libnss_files_hidden_ver(local, name) hidden_ver (local, name)
755 # define libnss_files_hidden_data_def(name) hidden_data_def (name)
756 # define libnss_files_hidden_data_weak(name) hidden_data_weak (name)
757 # define libnss_files_hidden_data_ver(local, name) hidden_data_ver(local, name)
758 #else
759 # define libnss_files_hidden_proto(name, attrs...)
760 # define libnss_files_hidden_tls_proto(name, attrs...)
761 # define libnss_files_hidden_def(name)
762 # define libnss_files_hidden_weak(name)
763 # define libnss_files_hidden_ver(local, name)
764 # define libnss_files_hidden_data_def(name)
765 # define libnss_files_hidden_data_weak(name)
766 # define libnss_files_hidden_data_ver(local, name)
767 #endif
769 #if IS_IN (libnsl)
770 # define libnsl_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
771 # define libnsl_hidden_tls_proto(name, attrs...) \
772 hidden_tls_proto (name, ##attrs)
773 # ifdef LINK_OBSOLETE_NSL
774 /* libnsl_hidden_nolink should only get used in libnsl code. */
775 # define libnsl_hidden_nolink_def(name, version) libnsl_hidden_def (name)
776 # else
777 # define libnsl_hidden_nolink_def(name, version) hidden_nolink (name, libnsl, version)
778 # endif
779 # define libnsl_hidden_def(name) hidden_def (name)
780 # define libnsl_hidden_weak(name) hidden_weak (name)
781 # define libnsl_hidden_ver(local, name) hidden_ver (local, name)
782 # define libnsl_hidden_data_def(name) hidden_data_def (name)
783 # define libnsl_hidden_data_weak(name) hidden_data_weak (name)
784 # define libnsl_hidden_data_ver(local, name) hidden_data_ver (local, name)
785 #else
786 # define libnsl_hidden_proto(name, attrs...)
787 # define libnsl_hidden_tls_proto(name, attrs...)
788 # define libnsl_hidden_def(name)
789 # define libnsl_hidden_weak(name)
790 # define libnsl_hidden_ver(local, name)
791 # define libnsl_hidden_data_def(name)
792 # define libnsl_hidden_data_weak(name)
793 # define libnsl_hidden_data_ver(local, name)
794 #endif
796 #if IS_IN (libnss_nisplus)
797 # define libnss_nisplus_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
798 # define libnss_nisplus_hidden_tls_proto(name, attrs...) \
799 hidden_tls_proto (name, ##attrs)
800 # define libnss_nisplus_hidden_def(name) hidden_def (name)
801 # define libnss_nisplus_hidden_weak(name) hidden_weak (name)
802 # define libnss_nisplus_hidden_ver(local, name) hidden_ver (local, name)
803 # define libnss_nisplus_hidden_data_def(name) hidden_data_def (name)
804 # define libnss_nisplus_hidden_data_weak(name) hidden_data_weak (name)
805 # define libnss_nisplus_hidden_data_ver(local, name) hidden_data_ver (local, name)
806 #else
807 # define libnss_nisplus_hidden_proto(name, attrs...)
808 # define libnss_nisplus_hidden_tls_proto(name, attrs...)
809 # define libnss_nisplus_hidden_def(name)
810 # define libnss_nisplus_hidden_weak(name)
811 # define libnss_nisplus_hidden_ver(local, name)
812 # define libnss_nisplus_hidden_data_def(name)
813 # define libnss_nisplus_hidden_data_weak(name)
814 # define libnss_nisplus_hidden_data_ver(local, name)
815 #endif
817 #define libc_hidden_builtin_proto(name, attrs...) libc_hidden_proto (name, ##attrs)
818 #define libc_hidden_builtin_def(name) libc_hidden_def (name)
819 #define libc_hidden_builtin_weak(name) libc_hidden_weak (name)
820 #define libc_hidden_builtin_ver(local, name) libc_hidden_ver (local, name)
821 #ifdef __ASSEMBLER__
822 # define HIDDEN_BUILTIN_JUMPTARGET(name) HIDDEN_JUMPTARGET(name)
823 #endif
825 #if IS_IN (libutil)
826 # define libutil_hidden_proto(name, attrs...) hidden_proto (name, ##attrs)
827 # define libutil_hidden_tls_proto(name, attrs...) \
828 hidden_tls_proto (name, ##attrs)
829 # define libutil_hidden_def(name) hidden_def (name)
830 # define libutil_hidden_weak(name) hidden_weak (name)
831 # define libutil_hidden_ver(local, name) hidden_ver (local, name)
832 # define libutil_hidden_data_def(name) hidden_data_def (name)
833 # define libutil_hidden_data_weak(name) hidden_data_weak (name)
834 # define libutil_hidden_data_ver(local, name) hidden_data_ver (local, name)
835 #else
836 # define libutil_hidden_proto(name, attrs...)
837 # define libutil_hidden_tls_proto(name, attrs...)
838 # define libutil_hidden_def(name)
839 # define libutil_hidden_weak(name)
840 # define libutil_hidden_ver(local, name)
841 # define libutil_hidden_data_def(name)
842 # define libutil_hidden_data_weak(name)
843 # define libutil_hidden_data_ver(local, name)
844 #endif
846 /* Get some dirty hacks. */
847 #include <symbol-hacks.h>
849 /* Move compatibility symbols out of the way by placing them all in a
850 special section. */
851 #ifndef __ASSEMBLER__
852 # define attribute_compat_text_section \
853 __attribute__ ((section (".text.compat")))
854 # define attribute_compat_data_section \
855 __attribute__ ((section (".data.compat")))
856 #else
857 # define compat_text_section .section ".text.compat", "ax";
858 # define compat_data_section .section ".data.compat", "aw";
859 #endif
861 /* Helper / base macros for indirect function symbols. */
862 #define __ifunc_resolver(type_name, name, expr, arg, init, classifier) \
863 classifier inhibit_stack_protector \
864 __typeof (type_name) *name##_ifunc (arg) \
866 init (); \
867 __typeof (type_name) *res = expr; \
868 return res; \
871 #ifdef HAVE_GCC_IFUNC
872 # define __ifunc(type_name, name, expr, arg, init) \
873 extern __typeof (type_name) name __attribute__ \
874 ((ifunc (#name "_ifunc"))); \
875 __ifunc_resolver (type_name, name, expr, arg, init, static)
877 # define __ifunc_hidden(type_name, name, expr, arg, init) \
878 __ifunc (type_name, name, expr, arg, init)
879 #else
880 /* Gcc does not support __attribute__ ((ifunc (...))). Use the old behaviour
881 as fallback. But keep in mind that the debug information for the ifunc
882 resolver functions is not correct. It contains the ifunc'ed function as
883 DW_AT_linkage_name. E.g. lldb uses this field and an inferior function
884 call of the ifunc'ed function will fail due to "no matching function for
885 call to ..." because the ifunc'ed function and the resolver function have
886 different signatures. (Gcc support is disabled at least on a ppc64le
887 Ubuntu 14.04 system.) */
889 # define __ifunc(type_name, name, expr, arg, init) \
890 extern __typeof (type_name) name; \
891 __typeof (type_name) *name##_ifunc (arg) __asm__ (#name); \
892 __ifunc_resolver (type_name, name, expr, arg, init,) \
893 __asm__ (".type " #name ", %gnu_indirect_function");
895 # define __ifunc_hidden(type_name, name, expr, arg, init) \
896 extern __typeof (type_name) __libc_##name; \
897 __ifunc (type_name, __libc_##name, expr, arg, init) \
898 strong_alias (__libc_##name, name);
899 #endif /* !HAVE_GCC_IFUNC */
901 /* The following macros are used for indirect function symbols in libc.so.
902 First of all, you need to have the function prototyped somewhere,
903 say in foo.h:
905 int foo (int __bar);
907 If you have an implementation for foo which e.g. uses a special hardware
908 feature which isn't available on all machines where this libc.so will be
909 used but decideable if available at runtime e.g. via hwcaps, you can provide
910 two or multiple implementations of foo:
912 int __foo_default (int __bar)
914 return __bar;
917 int __foo_special (int __bar)
919 return __bar;
922 If your function foo has no libc_hidden_proto (foo) defined for PLT
923 bypassing, you can use:
925 #define INIT_ARCH() unsigned long int hwcap = __GLRO(dl_hwcap);
927 libc_ifunc (foo, (hwcap & HWCAP_SPECIAL) ? __foo_special : __foo_default);
929 This will define a resolver function for foo which returns __foo_special or
930 __foo_default depending on your specified expression. Please note that you
931 have to define a macro function INIT_ARCH before using libc_ifunc macro as
932 it is called by the resolver function before evaluating the specified
933 expression. In this example it is used to prepare the hwcap variable.
934 The resolver function is assigned to an ifunc'ed symbol foo. Calls to foo
935 from inside or outside of libc.so will be indirected by a PLT call.
937 If your function foo has a libc_hidden_proto (foo) defined for PLT bypassing
938 and calls to foo within libc.so should always go to one specific
939 implementation of foo e.g. __foo_default then you have to add:
941 __hidden_ver1 (__foo_default, __GI_foo, __foo_default);
943 or a tweaked definition of libc_hidden_def macro after the __foo_default
944 function definition. Calls to foo within libc.so will always go directly to
945 __foo_default. Calls to foo from outside libc.so will be indirected by a
946 PLT call to ifunc'ed symbol foo which you have to define in a separate
947 compile unit:
949 #define foo __redirect_foo
950 #include <foo.h>
951 #undef foo
953 extern __typeof (__redirect_foo) __foo_default attribute_hidden;
954 extern __typeof (__redirect_foo) __foo_special attribute_hidden;
956 libc_ifunc_redirected (__redirect_foo, foo,
957 (hwcap & HWCAP_SPECIAL)
958 ? __foo_special
959 : __foo_default);
961 This will define the ifunc'ed symbol foo like above. The redirection of foo
962 in header file is needed to omit an additional defintion of __GI_foo which
963 would end in a linker error while linking libc.so. You have to specify
964 __redirect_foo as first parameter which is used within libc_ifunc_redirected
965 macro in conjunction with typeof to define the ifunc'ed symbol foo.
967 If your function foo has a libc_hidden_proto (foo) defined and calls to foo
968 within or from outside libc.so should go via ifunc'ed symbol, then you have
969 to use:
971 libc_ifunc_hidden (foo, foo,
972 (hwcap & HWCAP_SPECIAL)
973 ? __foo_special
974 : __foo_default);
975 libc_hidden_def (foo)
977 The first parameter foo of libc_ifunc_hidden macro is used in the same way
978 as for libc_ifunc_redirected macro. */
980 #define libc_ifunc(name, expr) __ifunc (name, name, expr, void, INIT_ARCH)
982 #define libc_ifunc_redirected(redirected_name, name, expr) \
983 __ifunc (redirected_name, name, expr, void, INIT_ARCH)
985 #define libc_ifunc_hidden(redirected_name, name, expr) \
986 __ifunc_hidden (redirected_name, name, expr, void, INIT_ARCH)
988 /* The body of the function is supposed to use __get_cpu_features
989 which will, if necessary, initialize the data first. */
990 #define libm_ifunc_init()
991 #define libm_ifunc(name, expr) \
992 __ifunc (name, name, expr, void, libm_ifunc_init)
994 /* Add the compiler optimization to inhibit loop transformation to library
995 calls. This is used to avoid recursive calls in memset and memmove
996 default implementations. */
997 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
998 # define inhibit_loop_to_libcall \
999 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
1000 #else
1001 # define inhibit_loop_to_libcall
1002 #endif
1004 /* These macros facilitate sharing source files with gnulib.
1006 They are here instead of sys/cdefs.h because they should not be
1007 used in public header files.
1009 Their definitions should be kept consistent with the definitions in
1010 gnulib-common.m4, but it is not necessary to cater to old non-GCC
1011 compilers, since they will only be used while building glibc itself.
1012 (Note that _GNUC_PREREQ cannot be used in this file.) */
1014 /* Define as a marker that can be attached to declarations that might not
1015 be used. This helps to reduce warnings, such as from
1016 GCC -Wunused-parameter. */
1017 #if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
1018 # define _GL_UNUSED __attribute__ ((__unused__))
1019 #else
1020 # define _GL_UNUSED
1021 #endif
1023 /* gcc supports the "unused" attribute on possibly unused labels, and
1024 g++ has since version 4.5. Note to support C++ as well as C,
1025 _GL_UNUSED_LABEL should be used with a trailing ; */
1026 #if !defined __cplusplus || __GNUC__ > 4 \
1027 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
1028 # define _GL_UNUSED_LABEL _GL_UNUSED
1029 #else
1030 # define _GL_UNUSED_LABEL
1031 #endif
1033 /* The __pure__ attribute was added in gcc 2.96. */
1034 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
1035 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
1036 #else
1037 # define _GL_ATTRIBUTE_PURE /* empty */
1038 #endif
1040 /* The __const__ attribute was added in gcc 2.95. */
1041 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
1042 # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__))
1043 #else
1044 # define _GL_ATTRIBUTE_CONST /* empty */
1045 #endif
1047 #endif /* !_ISOMAC */
1048 #endif /* libc-symbols.h */