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-2022 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
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. |
10 | |
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. |
15 | |
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 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #ifndef _LIBC_SYMBOLS_H |
21 | #define _LIBC_SYMBOLS_H 1 |
22 | |
23 | /* This file is included implicitly in the compilation of every source file, |
24 | using -include. It includes config.h. */ |
25 | |
26 | /* Enable declarations of GNU extensions, since we are compiling them. */ |
27 | #define _GNU_SOURCE 1 |
28 | |
29 | #ifdef MODULE_NAME |
30 | |
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) |
36 | |
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) |
41 | |
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 |
50 | |
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 |
61 | |
62 | #ifndef _ISOMAC |
63 | |
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 |
72 | |
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 |
77 | |
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 |
84 | |
85 | #include <config.h> |
86 | |
87 | /* Obtain the definition of symbol_version_reference. */ |
88 | #include <libc-symver.h> |
89 | |
90 | /* When PIC is defined and SHARED isn't defined, we are building PIE |
91 | by default. */ |
92 | #if defined PIC && !defined SHARED |
93 | # define BUILD_PIE_DEFAULT 1 |
94 | #else |
95 | # define BUILD_PIE_DEFAULT 0 |
96 | #endif |
97 | |
98 | /* Define this for the benefit of portable GNU code that wants to check it. |
99 | Code that checks with #if will not #include <config.h> again, since we've |
100 | already done it (and this file is implicitly included in every compile, |
101 | via -include). Code that checks with #ifdef will #include <config.h>, |
102 | but that file should always be idempotent (i.e., it's just #define/#undef |
103 | and nothing else anywhere should be changing the macro state it touches), |
104 | so it's harmless. */ |
105 | #define HAVE_CONFIG_H 0 |
106 | |
107 | /* Define these macros for the benefit of portable GNU code that wants to check |
108 | them. Of course, STDC_HEADERS is never false when building libc! */ |
109 | #define 1 |
110 | #define HAVE_MBSTATE_T 1 |
111 | #define HAVE_MBSRTOWCS 1 |
112 | #define HAVE_LIBINTL_H 1 |
113 | #define HAVE_WCTYPE_H 1 |
114 | #define HAVE_ISWCTYPE 1 |
115 | #define ENABLE_NLS 1 |
116 | |
117 | /* The symbols in all the user (non-_) macros are C symbols. */ |
118 | |
119 | #ifndef __SYMBOL_PREFIX |
120 | # define __SYMBOL_PREFIX |
121 | #endif |
122 | |
123 | #ifndef C_SYMBOL_NAME |
124 | # define C_SYMBOL_NAME(name) name |
125 | #endif |
126 | |
127 | #ifndef ASM_LINE_SEP |
128 | # define ASM_LINE_SEP ; |
129 | #endif |
130 | |
131 | #ifndef __attribute_copy__ |
132 | /* Provide an empty definition when cdefs.h is not included. */ |
133 | # define __attribute_copy__(arg) |
134 | #endif |
135 | |
136 | #ifndef __ASSEMBLER__ |
137 | /* GCC understands weak symbols and aliases; use its interface where |
138 | possible, instead of embedded assembly language. */ |
139 | |
140 | /* Define ALIASNAME as a strong alias for NAME. */ |
141 | # define strong_alias(name, aliasname) _strong_alias(name, aliasname) |
142 | # define _strong_alias(name, aliasname) \ |
143 | extern __typeof (name) aliasname __attribute__ ((alias (#name))) \ |
144 | __attribute_copy__ (name); |
145 | |
146 | /* This comes between the return type and function name in |
147 | a function definition to make that definition weak. */ |
148 | # define weak_function __attribute__ ((weak)) |
149 | # define weak_const_function __attribute__ ((weak, __const__)) |
150 | |
151 | /* Define ALIASNAME as a weak alias for NAME. |
152 | If weak aliases are not available, this defines a strong alias. */ |
153 | # define weak_alias(name, aliasname) _weak_alias (name, aliasname) |
154 | # define _weak_alias(name, aliasname) \ |
155 | extern __typeof (name) aliasname __attribute__ ((weak, alias (#name))) \ |
156 | __attribute_copy__ (name); |
157 | |
158 | /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */ |
159 | # define weak_extern(symbol) _weak_extern (weak symbol) |
160 | # define _weak_extern(expr) _Pragma (#expr) |
161 | |
162 | /* In shared builds, the expression call_function_static_weak |
163 | (FUNCTION-SYMBOL, ARGUMENTS) invokes FUNCTION-SYMBOL (an |
164 | identifier) unconditionally, with the (potentially empty) argument |
165 | list ARGUMENTS. In static builds, if FUNCTION-SYMBOL has a |
166 | definition, the function is invoked as before; if FUNCTION-SYMBOL |
167 | is NULL, no call is performed. */ |
168 | # ifdef SHARED |
169 | # define call_function_static_weak(func, ...) func (__VA_ARGS__) |
170 | # else /* !SHARED */ |
171 | # define call_function_static_weak(func, ...) \ |
172 | ({ \ |
173 | extern __typeof__ (func) func weak_function; \ |
174 | (func != NULL ? func (__VA_ARGS__) : (void)0); \ |
175 | }) |
176 | # endif |
177 | |
178 | #else /* __ASSEMBLER__ */ |
179 | |
180 | # ifdef HAVE_ASM_SET_DIRECTIVE |
181 | # define strong_alias(original, alias) \ |
182 | .globl C_SYMBOL_NAME (alias) ASM_LINE_SEP \ |
183 | .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original) |
184 | # define strong_data_alias(original, alias) strong_alias(original, alias) |
185 | # else |
186 | # define strong_alias(original, alias) \ |
187 | .globl C_SYMBOL_NAME (alias) ASM_LINE_SEP \ |
188 | C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) |
189 | # define strong_data_alias(original, alias) strong_alias(original, alias) |
190 | # endif |
191 | |
192 | # define weak_alias(original, alias) \ |
193 | .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \ |
194 | C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) |
195 | |
196 | # define weak_extern(symbol) \ |
197 | .weak C_SYMBOL_NAME (symbol) |
198 | |
199 | #endif /* __ASSEMBLER__ */ |
200 | |
201 | /* Determine the return address. */ |
202 | #define RETURN_ADDRESS(nr) \ |
203 | __builtin_extract_return_addr (__builtin_return_address (nr)) |
204 | |
205 | /* When a reference to SYMBOL is encountered, the linker will emit a |
206 | warning message MSG. */ |
207 | /* We want the .gnu.warning.SYMBOL section to be unallocated. */ |
208 | #define __make_section_unallocated(section_string) \ |
209 | asm (".section " section_string "\n\t.previous"); |
210 | |
211 | /* Tacking on "\n\t#" to the section name makes gcc put it's bogus |
212 | section attributes on what looks like a comment to the assembler. */ |
213 | #ifdef HAVE_SECTION_QUOTES |
214 | # define __sec_comment "\"\n\t#\"" |
215 | #else |
216 | # define "\n\t#" |
217 | #endif |
218 | #define link_warning(symbol, msg) \ |
219 | __make_section_unallocated (".gnu.warning." #symbol) \ |
220 | static const char __evoke_link_warning_##symbol[] \ |
221 | __attribute__ ((used, section (".gnu.warning." #symbol __sec_comment))) \ |
222 | = msg; |
223 | |
224 | /* A canned warning for sysdeps/stub functions. */ |
225 | #define stub_warning(name) \ |
226 | __make_section_unallocated (".gnu.glibc-stub." #name) \ |
227 | link_warning (name, #name " is not implemented and will always fail") |
228 | |
229 | /* Warning for linking functions calling dlopen into static binaries. */ |
230 | #ifdef SHARED |
231 | #define static_link_warning(name) |
232 | #else |
233 | #define static_link_warning(name) static_link_warning1(name) |
234 | #define static_link_warning1(name) \ |
235 | link_warning(name, "Using '" #name "' in statically linked applications \ |
236 | requires at runtime the shared libraries from the glibc version used \ |
237 | for linking") |
238 | #endif |
239 | |
240 | /* Resource Freeing Hooks: |
241 | |
242 | Normally a process exits and the OS cleans up any allocated |
243 | memory. However, when tooling like mtrace or valgrind is monitoring |
244 | the process we need to free all resources that are part of the |
245 | process in order to provide the consistency required to track |
246 | memory leaks. |
247 | |
248 | A single public API exists and is __libc_freeres(), and this is used |
249 | by applications like valgrind to freee resouces. |
250 | |
251 | There are 3 cases: |
252 | |
253 | (a) __libc_freeres |
254 | |
255 | In this case all you need to do is define the freeing routine: |
256 | |
257 | foo.c: |
258 | libfoo_freeres_fn (foo_freeres) |
259 | { |
260 | complex_free (mem); |
261 | } |
262 | |
263 | This ensures the function is called at the right point to free |
264 | resources. |
265 | |
266 | (b) __libc_freeres_ptr |
267 | |
268 | The framework for (a) iterates over the list of pointers-to-free |
269 | in (b) and frees them. |
270 | |
271 | foo.c: |
272 | libc_freeres_ptr (static char *foo_buffer); |
273 | |
274 | Freeing these resources alaways happens last and is equivalent |
275 | to registering a function that does 'free (foo_buffer)'. |
276 | |
277 | (c) Explicit lists of free routines to call or objects to free. |
278 | |
279 | It is the intended goal to remove (a) and (b) which have some |
280 | non-determinism based on link order, and instead use explicit |
281 | lists of functions and frees to resolve cleanup ordering issues |
282 | and make it easy to debug and maintain. |
283 | |
284 | As of today the following subsystems use (c): |
285 | |
286 | Per-thread cleanup: |
287 | * malloc/thread-freeres.c |
288 | |
289 | libdl cleanup: |
290 | * dlfcn/dlfreeres.c |
291 | |
292 | libpthread cleanup: |
293 | * nptl/nptlfreeres.c |
294 | |
295 | So if you need any shutdown routines to run you should add them |
296 | directly to the appropriate subsystem's shutdown list. */ |
297 | |
298 | /* Resource pointers to free in libc.so. */ |
299 | #define libc_freeres_ptr(decl) \ |
300 | __make_section_unallocated ("__libc_freeres_ptrs, \"aw\", %nobits") \ |
301 | decl __attribute__ ((section ("__libc_freeres_ptrs" __sec_comment))) |
302 | |
303 | /* Resource freeing functions from libc.so go in this section. */ |
304 | #define __libc_freeres_fn_section \ |
305 | __attribute__ ((__used__, section ("__libc_freeres_fn"))) |
306 | |
307 | /* Resource freeing functions for libc.so. */ |
308 | #define libc_freeres_fn(name) \ |
309 | static void name (void) __attribute_used__ __libc_freeres_fn_section; \ |
310 | text_set_element (__libc_subfreeres, name); \ |
311 | static void name (void) |
312 | |
313 | /* Declare SYMBOL to be TYPE (`function' or `object') of SIZE bytes |
314 | alias to ORIGINAL, when the assembler supports such declarations |
315 | (such as in ELF). |
316 | This is only necessary when defining something in assembly, or playing |
317 | funny alias games where the size should be other than what the compiler |
318 | thinks it is. */ |
319 | #ifdef __ASSEMBLER__ |
320 | # define declare_object_symbol_alias(symbol, original, size) \ |
321 | declare_object_symbol_alias_1 (symbol, original, size) |
322 | # define declare_object_symbol_alias_1(symbol, original, s_size) \ |
323 | strong_alias (original, symbol) ASM_LINE_SEP \ |
324 | .type C_SYMBOL_NAME (symbol), %object ASM_LINE_SEP \ |
325 | .size C_SYMBOL_NAME (symbol), s_size ASM_LINE_SEP |
326 | #else /* Not __ASSEMBLER__. */ |
327 | # ifdef HAVE_ASM_SET_DIRECTIVE |
328 | # define declare_symbol_alias_1_alias(symbol, original) \ |
329 | ".set " __SYMBOL_PREFIX #symbol ", " __SYMBOL_PREFIX #original |
330 | # else |
331 | # define declare_symbol_alias_1_alias(symbol, original) \ |
332 | __SYMBOL_PREFIX #symbol " = " __SYMBOL_PREFIX #original |
333 | # endif /* HAVE_ASM_SET_DIRECTIVE */ |
334 | #endif /* __ASSEMBLER__ */ |
335 | |
336 | |
337 | /* |
338 | |
339 | */ |
340 | |
341 | #ifdef HAVE_GNU_RETAIN |
342 | # define attribute_used_retain __attribute__ ((__used__, __retain__)) |
343 | #else |
344 | # define attribute_used_retain __attribute__ ((__used__)) |
345 | #endif |
346 | |
347 | /* Symbol set support macros. */ |
348 | |
349 | /* Make SYMBOL, which is in the text segment, an element of SET. */ |
350 | #define text_set_element(set, symbol) _elf_set_element(set, symbol) |
351 | /* Make SYMBOL, which is in the data segment, an element of SET. */ |
352 | #define data_set_element(set, symbol) _elf_set_element(set, symbol) |
353 | /* Make SYMBOL, which is in the bss segment, an element of SET. */ |
354 | #define bss_set_element(set, symbol) _elf_set_element(set, symbol) |
355 | |
356 | /* These are all done the same way in ELF. |
357 | There is a new section created for each set. */ |
358 | #ifdef SHARED |
359 | /* When building a shared library, make the set section writable, |
360 | because it will need to be relocated at run time anyway. */ |
361 | # define _elf_set_element(set, symbol) \ |
362 | static const void *__elf_set_##set##_element_##symbol##__ \ |
363 | attribute_used_retain __attribute__ ((section (#set))) = &(symbol) |
364 | #else |
365 | # define _elf_set_element(set, symbol) \ |
366 | static const void *const __elf_set_##set##_element_##symbol##__ \ |
367 | attribute_used_retain __attribute__ ((section (#set))) = &(symbol) |
368 | #endif |
369 | |
370 | /* Define SET as a symbol set. This may be required (it is in a.out) to |
371 | be able to use the set's contents. */ |
372 | #define symbol_set_define(set) symbol_set_declare(set) |
373 | |
374 | /* Declare SET for use in this module, if defined in another module. |
375 | In a shared library, this is always local to that shared object. |
376 | For static linking, the set might be wholly absent and so we use |
377 | weak references. */ |
378 | #define symbol_set_declare(set) \ |
379 | extern char const __start_##set[] __symbol_set_attribute; \ |
380 | extern char const __stop_##set[] __symbol_set_attribute; |
381 | #ifdef SHARED |
382 | # define __symbol_set_attribute attribute_hidden |
383 | #else |
384 | # define __symbol_set_attribute __attribute__ ((weak)) |
385 | #endif |
386 | |
387 | /* Return a pointer (void *const *) to the first element of SET. */ |
388 | #define symbol_set_first_element(set) ((void *const *) (&__start_##set)) |
389 | |
390 | /* Return true iff PTR (a void *const *) has been incremented |
391 | past the last element in SET. */ |
392 | #define symbol_set_end_p(set, ptr) ((ptr) >= (void *const *) &__stop_##set) |
393 | |
394 | #ifdef SHARED |
395 | # define symbol_version(real, name, version) \ |
396 | symbol_version_reference(real, name, version) |
397 | # define default_symbol_version(real, name, version) \ |
398 | _default_symbol_version(real, name, version) |
399 | /* See <libc-symver.h>. */ |
400 | # ifdef __ASSEMBLER__ |
401 | # define _default_symbol_version(real, name, version) \ |
402 | _set_symbol_version (real, name@@version) |
403 | # else |
404 | # define _default_symbol_version(real, name, version) \ |
405 | _set_symbol_version (real, #name "@@" #version) |
406 | # endif |
407 | |
408 | /* Evalutes to a string literal for VERSION in LIB. */ |
409 | # define symbol_version_string(lib, version) \ |
410 | _symbol_version_stringify_1 (VERSION_##lib##_##version) |
411 | # define _symbol_version_stringify_1(arg) _symbol_version_stringify_2 (arg) |
412 | # define _symbol_version_stringify_2(arg) #arg |
413 | |
414 | #else /* !SHARED */ |
415 | # define symbol_version(real, name, version) |
416 | # define default_symbol_version(real, name, version) \ |
417 | strong_alias(real, name) |
418 | #endif |
419 | |
420 | #if defined SHARED || defined LIBC_NONSHARED \ |
421 | || (BUILD_PIE_DEFAULT && IS_IN (libc)) |
422 | # define attribute_hidden __attribute__ ((visibility ("hidden"))) |
423 | #else |
424 | # define attribute_hidden |
425 | #endif |
426 | |
427 | #define attribute_tls_model_ie __attribute__ ((tls_model ("initial-exec"))) |
428 | |
429 | #define attribute_relro __attribute__ ((section (".data.rel.ro"))) |
430 | |
431 | |
432 | /* Used to disable stack protection in sensitive places, like ifunc |
433 | resolvers and early static TLS init. */ |
434 | #ifdef HAVE_CC_NO_STACK_PROTECTOR |
435 | # define inhibit_stack_protector \ |
436 | __attribute__ ((__optimize__ ("-fno-stack-protector"))) |
437 | #else |
438 | # define inhibit_stack_protector |
439 | #endif |
440 | |
441 | /* The following macros are used for PLT bypassing within libc.so |
442 | (and if needed other libraries similarly). |
443 | First of all, you need to have the function prototyped somewhere, |
444 | say in foo/foo.h: |
445 | |
446 | int foo (int __bar); |
447 | |
448 | If calls to foo within libc.so should always go to foo defined in libc.so, |
449 | then in include/foo.h you add: |
450 | |
451 | libc_hidden_proto (foo) |
452 | |
453 | line and after the foo function definition: |
454 | |
455 | int foo (int __bar) |
456 | { |
457 | return __bar; |
458 | } |
459 | libc_hidden_def (foo) |
460 | |
461 | or |
462 | |
463 | int foo (int __bar) |
464 | { |
465 | return __bar; |
466 | } |
467 | libc_hidden_weak (foo) |
468 | |
469 | Similarly for global data. If references to foo within libc.so should |
470 | always go to foo defined in libc.so, then in include/foo.h you add: |
471 | |
472 | libc_hidden_proto (foo) |
473 | |
474 | line and after foo's definition: |
475 | |
476 | int foo = INITIAL_FOO_VALUE; |
477 | libc_hidden_data_def (foo) |
478 | |
479 | or |
480 | |
481 | int foo = INITIAL_FOO_VALUE; |
482 | libc_hidden_data_weak (foo) |
483 | |
484 | If foo is normally just an alias (strong or weak) to some other function, |
485 | you should use the normal strong_alias first, then add libc_hidden_def |
486 | or libc_hidden_weak: |
487 | |
488 | int baz (int __bar) |
489 | { |
490 | return __bar; |
491 | } |
492 | strong_alias (baz, foo) |
493 | libc_hidden_weak (foo) |
494 | |
495 | If the function should be internal to multiple objects, say ld.so and |
496 | libc.so, the best way is to use: |
497 | |
498 | #if IS_IN (libc) || IS_IN (rtld) |
499 | hidden_proto (foo) |
500 | #endif |
501 | |
502 | in include/foo.h and the normal macros at all function definitions |
503 | depending on what DSO they belong to. |
504 | |
505 | If versioned_symbol macro is used to define foo, |
506 | libc_hidden_ver macro should be used, as in: |
507 | |
508 | int __real_foo (int __bar) |
509 | { |
510 | return __bar; |
511 | } |
512 | versioned_symbol (libc, __real_foo, foo, GLIBC_2_1); |
513 | libc_hidden_ver (__real_foo, foo) */ |
514 | |
515 | #if defined SHARED && !defined NO_HIDDEN |
516 | # ifndef __ASSEMBLER__ |
517 | # define __hidden_proto_hiddenattr(attrs...) \ |
518 | __attribute__ ((visibility ("hidden"), ##attrs)) |
519 | # define hidden_proto(name, attrs...) \ |
520 | __hidden_proto (name, , __GI_##name, ##attrs) |
521 | # define hidden_proto_alias(name, alias, attrs...) \ |
522 | __hidden_proto_alias (name, , alias, ##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_proto_alias(name, thread, internal, attrs...) \ |
529 | extern thread __typeof (name) internal __hidden_proto_hiddenattr (attrs); |
530 | # define __hidden_asmname(name) \ |
531 | __hidden_asmname1 (__USER_LABEL_PREFIX__, name) |
532 | # define __hidden_asmname1(prefix, name) __hidden_asmname2(prefix, name) |
533 | # define __hidden_asmname2(prefix, name) #prefix name |
534 | # define __hidden_ver1(local, internal, name) \ |
535 | __hidden_ver2 (, local, internal, name) |
536 | # define __hidden_ver2(thread, local, internal, name) \ |
537 | extern thread __typeof (name) __EI_##name \ |
538 | __asm__(__hidden_asmname (#internal)); \ |
539 | extern thread __typeof (name) __EI_##name \ |
540 | __attribute__((alias (__hidden_asmname (#local)))) \ |
541 | __attribute_copy__ (name) |
542 | # define hidden_ver(local, name) __hidden_ver1(local, __GI_##name, name); |
543 | # define hidden_def(name) __hidden_ver1(__GI_##name, name, name); |
544 | # define hidden_def_alias(name, internal) \ |
545 | strong_alias (name, internal) |
546 | # define hidden_data_def(name) hidden_def(name) |
547 | # define hidden_data_def_alias(name, alias) hidden_def_alias(name, alias) |
548 | # define hidden_tls_def(name) \ |
549 | __hidden_ver2 (__thread, __GI_##name, name, name); |
550 | # define hidden_weak(name) \ |
551 | __hidden_ver1(__GI_##name, name, name) __attribute__((weak)); |
552 | # define hidden_data_weak(name) hidden_weak(name) |
553 | # define hidden_nolink(name, lib, version) \ |
554 | __hidden_nolink1 (__GI_##name, __EI_##name, name, VERSION_##lib##_##version) |
555 | # define __hidden_nolink1(local, internal, name, version) \ |
556 | __hidden_nolink2 (local, internal, name, version) |
557 | # define __hidden_nolink2(local, internal, name, version) \ |
558 | extern __typeof (name) internal __attribute__ ((alias (#local))) \ |
559 | __attribute_copy__ (name); \ |
560 | __hidden_nolink3 (local, internal, #name "@" #version) |
561 | # define __hidden_nolink3(local, internal, vername) \ |
562 | __asm__ (".symver " #internal ", " vername); |
563 | # else |
564 | /* For assembly, we need to do the opposite of what we do in C: |
565 | in assembly gcc __REDIRECT stuff is not in place, so functions |
566 | are defined by its normal name and we need to create the |
567 | __GI_* alias to it, in C __REDIRECT causes the function definition |
568 | to use __GI_* name and we need to add alias to the real name. |
569 | There is no reason to use hidden_weak over hidden_def in assembly, |
570 | but we provide it for consistency with the C usage. |
571 | hidden_proto doesn't make sense for assembly but the equivalent |
572 | is to call via the HIDDEN_JUMPTARGET macro instead of JUMPTARGET. */ |
573 | # define hidden_def(name) strong_alias (name, __GI_##name) |
574 | # define hidden_def_alias(name, alias) strong_alias (name, alias) |
575 | # define hidden_weak(name) hidden_def (name) |
576 | # define hidden_ver(local, name) strong_alias (local, __GI_##name) |
577 | # define hidden_data_def(name) strong_data_alias (name, __GI_##name) |
578 | # define hidden_data_def_alias(name, alias) strong_data_alias (name, alias) |
579 | # define hidden_tls_def(name) hidden_data_def (name) |
580 | # define hidden_data_weak(name) hidden_data_def (name) |
581 | # define HIDDEN_JUMPTARGET(name) __GI_##name |
582 | # endif |
583 | #else |
584 | # ifndef __ASSEMBLER__ |
585 | # if !defined SHARED && IS_IN (libc) && !defined LIBC_NONSHARED \ |
586 | && (!defined PIC || !defined NO_HIDDEN_EXTERN_FUNC_IN_PIE) \ |
587 | && !defined NO_HIDDEN |
588 | # define __hidden_proto_hiddenattr(attrs...) \ |
589 | __attribute__ ((visibility ("hidden"), ##attrs)) |
590 | # define hidden_proto(name, attrs...) \ |
591 | __hidden_proto (name, , name, ##attrs) |
592 | # define hidden_proto_alias(name, alias, attrs...) \ |
593 | __hidden_proto_alias (name, , alias, ##attrs) |
594 | # define hidden_tls_proto(name, attrs...) \ |
595 | __hidden_proto (name, __thread, name, ##attrs) |
596 | # define __hidden_proto(name, thread, internal, attrs...) \ |
597 | extern thread __typeof (name) name __hidden_proto_hiddenattr (attrs); |
598 | # define __hidden_proto_alias(name, thread, internal, attrs...) \ |
599 | extern thread __typeof (name) internal __hidden_proto_hiddenattr (attrs); |
600 | # else |
601 | # define hidden_proto(name, attrs...) |
602 | # define hidden_proto_alias(name, alias, attrs...) |
603 | # define hidden_tls_proto(name, attrs...) |
604 | # endif |
605 | # else |
606 | # define HIDDEN_JUMPTARGET(name) JUMPTARGET(name) |
607 | # endif /* Not __ASSEMBLER__ */ |
608 | # define hidden_weak(name) |
609 | # define hidden_def(name) |
610 | # define hidden_def_alias(name, alias) |
611 | # define hidden_ver(local, name) |
612 | # define hidden_data_weak(name) |
613 | # define hidden_data_def(name) |
614 | # define hidden_data_def_alias(name, alias) |
615 | # define hidden_tls_def(name) |
616 | # define hidden_nolink(name, lib, version) |
617 | #endif |
618 | |
619 | #if IS_IN (libc) |
620 | # define libc_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
621 | # define libc_hidden_proto_alias(name, alias, attrs...) \ |
622 | hidden_proto_alias (name, alias, ##attrs) |
623 | # define libc_hidden_tls_proto(name, attrs...) hidden_tls_proto (name, ##attrs) |
624 | # define libc_hidden_def(name) hidden_def (name) |
625 | # define libc_hidden_weak(name) hidden_weak (name) |
626 | # define libc_hidden_nolink_sunrpc(name, version) hidden_nolink (name, libc, version) |
627 | # define libc_hidden_ver(local, name) hidden_ver (local, name) |
628 | # define libc_hidden_data_def(name) hidden_data_def (name) |
629 | # define libc_hidden_data_def_alias(name, alias) hidden_data_def_alias (name, alias) |
630 | # define libc_hidden_tls_def(name) hidden_tls_def (name) |
631 | # define libc_hidden_data_weak(name) hidden_data_weak (name) |
632 | #else |
633 | # define libc_hidden_proto(name, attrs...) |
634 | # define libc_hidden_proto_alias(name, alias, attrs...) |
635 | # define libc_hidden_tls_proto(name, attrs...) |
636 | # define libc_hidden_def(name) |
637 | # define libc_hidden_weak(name) |
638 | # define libc_hidden_ver(local, name) |
639 | # define libc_hidden_data_def(name) |
640 | # define libc_hidden_data_def_alias(name, alias) |
641 | # define libc_hidden_tls_def(name) |
642 | # define libc_hidden_data_weak(name) |
643 | #endif |
644 | |
645 | #if IS_IN (rtld) |
646 | # define rtld_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
647 | # define rtld_hidden_def(name) hidden_def (name) |
648 | # define rtld_hidden_weak(name) hidden_weak (name) |
649 | # define rtld_hidden_data_def(name) hidden_data_def (name) |
650 | #else |
651 | # define rtld_hidden_proto(name, attrs...) |
652 | # define rtld_hidden_def(name) |
653 | # define rtld_hidden_weak(name) |
654 | # define rtld_hidden_data_def(name) |
655 | #endif |
656 | |
657 | #if IS_IN (libm) |
658 | # define libm_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
659 | # define libm_hidden_def(name) hidden_def (name) |
660 | # define libm_hidden_weak(name) hidden_weak (name) |
661 | # define libm_hidden_ver(local, name) hidden_ver (local, name) |
662 | #else |
663 | # define libm_hidden_proto(name, attrs...) |
664 | # define libm_hidden_def(name) |
665 | # define libm_hidden_weak(name) |
666 | # define libm_hidden_ver(local, name) |
667 | #endif |
668 | |
669 | #if IS_IN (libmvec) |
670 | # define libmvec_hidden_def(name) hidden_def (name) |
671 | #else |
672 | # define libmvec_hidden_def(name) |
673 | #endif |
674 | |
675 | #if IS_IN (libresolv) |
676 | # define libresolv_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
677 | # define libresolv_hidden_def(name) hidden_def (name) |
678 | # define libresolv_hidden_data_def(name) hidden_data_def (name) |
679 | #else |
680 | # define libresolv_hidden_proto(name, attrs...) |
681 | # define libresolv_hidden_def(name) |
682 | # define libresolv_hidden_data_def(name) |
683 | #endif |
684 | |
685 | #if IS_IN (libpthread) |
686 | # define libpthread_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
687 | # define libpthread_hidden_def(name) hidden_def (name) |
688 | #else |
689 | # define libpthread_hidden_proto(name, attrs...) |
690 | # define libpthread_hidden_def(name) |
691 | #endif |
692 | |
693 | #if IS_IN (librt) |
694 | # define librt_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
695 | # define librt_hidden_ver(local, name) hidden_ver (local, name) |
696 | #else |
697 | # define librt_hidden_proto(name, attrs...) |
698 | # define librt_hidden_ver(local, name) |
699 | #endif |
700 | |
701 | #if IS_IN (libnsl) |
702 | # define libnsl_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
703 | # define libnsl_hidden_nolink_def(name, version) hidden_nolink (name, libnsl, version) |
704 | #else |
705 | # define libnsl_hidden_proto(name, attrs...) |
706 | #endif |
707 | |
708 | #define libc_hidden_builtin_proto(name, attrs...) libc_hidden_proto (name, ##attrs) |
709 | #define libc_hidden_builtin_def(name) libc_hidden_def (name) |
710 | |
711 | #define libc_hidden_ldbl_proto(name, attrs...) libc_hidden_proto (name, ##attrs) |
712 | #ifdef __ASSEMBLER__ |
713 | # define HIDDEN_BUILTIN_JUMPTARGET(name) HIDDEN_JUMPTARGET(name) |
714 | #endif |
715 | |
716 | #if IS_IN (libanl) |
717 | # define libanl_hidden_proto(name, attrs...) hidden_proto (name, ##attrs) |
718 | #else |
719 | # define libanl_hidden_proto(name, attrs...) |
720 | #endif |
721 | |
722 | /* Get some dirty hacks. */ |
723 | #include <symbol-hacks.h> |
724 | |
725 | /* Move compatibility symbols out of the way by placing them all in a |
726 | special section. */ |
727 | #ifndef __ASSEMBLER__ |
728 | # define attribute_compat_text_section \ |
729 | __attribute__ ((section (".text.compat"))) |
730 | #else |
731 | # define compat_text_section .section ".text.compat", "ax"; |
732 | #endif |
733 | |
734 | /* Helper / base macros for indirect function symbols. */ |
735 | #define __ifunc_resolver(type_name, name, expr, arg, init, classifier) \ |
736 | classifier inhibit_stack_protector \ |
737 | __typeof (type_name) *name##_ifunc (arg) \ |
738 | { \ |
739 | init (); \ |
740 | __typeof (type_name) *res = expr; \ |
741 | return res; \ |
742 | } |
743 | |
744 | #ifdef HAVE_GCC_IFUNC |
745 | # define __ifunc(type_name, name, expr, arg, init) \ |
746 | extern __typeof (type_name) name __attribute__ \ |
747 | ((ifunc (#name "_ifunc"))); \ |
748 | __ifunc_resolver (type_name, name, expr, arg, init, static) |
749 | |
750 | # define __ifunc_hidden(type_name, name, expr, arg, init) \ |
751 | __ifunc (type_name, name, expr, arg, init) |
752 | #else |
753 | /* Gcc does not support __attribute__ ((ifunc (...))). Use the old behaviour |
754 | as fallback. But keep in mind that the debug information for the ifunc |
755 | resolver functions is not correct. It contains the ifunc'ed function as |
756 | DW_AT_linkage_name. E.g. lldb uses this field and an inferior function |
757 | call of the ifunc'ed function will fail due to "no matching function for |
758 | call to ..." because the ifunc'ed function and the resolver function have |
759 | different signatures. (Gcc support is disabled at least on a ppc64le |
760 | Ubuntu 14.04 system.) */ |
761 | |
762 | # define __ifunc(type_name, name, expr, arg, init) \ |
763 | extern __typeof (type_name) name; \ |
764 | __typeof (type_name) *name##_ifunc (arg) __asm__ (#name); \ |
765 | __ifunc_resolver (type_name, name, expr, arg, init,) \ |
766 | __asm__ (".type " #name ", %gnu_indirect_function"); |
767 | |
768 | # define __ifunc_hidden(type_name, name, expr, arg, init) \ |
769 | extern __typeof (type_name) __libc_##name; \ |
770 | __ifunc (type_name, __libc_##name, expr, arg, init) \ |
771 | strong_alias (__libc_##name, name); |
772 | #endif /* !HAVE_GCC_IFUNC */ |
773 | |
774 | /* The following macros are used for indirect function symbols in libc.so. |
775 | First of all, you need to have the function prototyped somewhere, |
776 | say in foo.h: |
777 | |
778 | int foo (int __bar); |
779 | |
780 | If you have an implementation for foo which e.g. uses a special hardware |
781 | feature which isn't available on all machines where this libc.so will be |
782 | used but decideable if available at runtime e.g. via hwcaps, you can provide |
783 | two or multiple implementations of foo: |
784 | |
785 | int __foo_default (int __bar) |
786 | { |
787 | return __bar; |
788 | } |
789 | |
790 | int __foo_special (int __bar) |
791 | { |
792 | return __bar; |
793 | } |
794 | |
795 | If your function foo has no libc_hidden_proto (foo) defined for PLT |
796 | bypassing, you can use: |
797 | |
798 | #define INIT_ARCH() unsigned long int hwcap = __GLRO(dl_hwcap); |
799 | |
800 | libc_ifunc (foo, (hwcap & HWCAP_SPECIAL) ? __foo_special : __foo_default); |
801 | |
802 | This will define a resolver function for foo which returns __foo_special or |
803 | __foo_default depending on your specified expression. Please note that you |
804 | have to define a macro function INIT_ARCH before using libc_ifunc macro as |
805 | it is called by the resolver function before evaluating the specified |
806 | expression. In this example it is used to prepare the hwcap variable. |
807 | The resolver function is assigned to an ifunc'ed symbol foo. Calls to foo |
808 | from inside or outside of libc.so will be indirected by a PLT call. |
809 | |
810 | If your function foo has a libc_hidden_proto (foo) defined for PLT bypassing |
811 | and calls to foo within libc.so should always go to one specific |
812 | implementation of foo e.g. __foo_default then you have to add: |
813 | |
814 | __hidden_ver1 (__foo_default, __GI_foo, __foo_default); |
815 | |
816 | or a tweaked definition of libc_hidden_def macro after the __foo_default |
817 | function definition. Calls to foo within libc.so will always go directly to |
818 | __foo_default. Calls to foo from outside libc.so will be indirected by a |
819 | PLT call to ifunc'ed symbol foo which you have to define in a separate |
820 | compile unit: |
821 | |
822 | #define foo __redirect_foo |
823 | #include <foo.h> |
824 | #undef foo |
825 | |
826 | extern __typeof (__redirect_foo) __foo_default attribute_hidden; |
827 | extern __typeof (__redirect_foo) __foo_special attribute_hidden; |
828 | |
829 | libc_ifunc_redirected (__redirect_foo, foo, |
830 | (hwcap & HWCAP_SPECIAL) |
831 | ? __foo_special |
832 | : __foo_default); |
833 | |
834 | This will define the ifunc'ed symbol foo like above. The redirection of foo |
835 | in header file is needed to omit an additional defintion of __GI_foo which |
836 | would end in a linker error while linking libc.so. You have to specify |
837 | __redirect_foo as first parameter which is used within libc_ifunc_redirected |
838 | macro in conjunction with typeof to define the ifunc'ed symbol foo. |
839 | |
840 | If your function foo has a libc_hidden_proto (foo) defined and calls to foo |
841 | within or from outside libc.so should go via ifunc'ed symbol, then you have |
842 | to use: |
843 | |
844 | libc_ifunc_hidden (foo, foo, |
845 | (hwcap & HWCAP_SPECIAL) |
846 | ? __foo_special |
847 | : __foo_default); |
848 | libc_hidden_def (foo) |
849 | |
850 | The first parameter foo of libc_ifunc_hidden macro is used in the same way |
851 | as for libc_ifunc_redirected macro. */ |
852 | |
853 | #define libc_ifunc(name, expr) __ifunc (name, name, expr, void, INIT_ARCH) |
854 | |
855 | #define libc_ifunc_redirected(redirected_name, name, expr) \ |
856 | __ifunc (redirected_name, name, expr, void, INIT_ARCH) |
857 | |
858 | #define libc_ifunc_hidden(redirected_name, name, expr) \ |
859 | __ifunc_hidden (redirected_name, name, expr, void, INIT_ARCH) |
860 | |
861 | /* The body of the function is supposed to use __get_cpu_features |
862 | which will, if necessary, initialize the data first. */ |
863 | #define libm_ifunc_init() |
864 | #define libm_ifunc(name, expr) \ |
865 | __ifunc (name, name, expr, void, libm_ifunc_init) |
866 | |
867 | /* Add the compiler optimization to inhibit loop transformation to library |
868 | calls. This is used to avoid recursive calls in memset and memmove |
869 | default implementations. */ |
870 | #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL |
871 | # define inhibit_loop_to_libcall \ |
872 | __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns"))) |
873 | #else |
874 | # define inhibit_loop_to_libcall |
875 | #endif |
876 | |
877 | /* These macros facilitate sharing source files with gnulib. |
878 | |
879 | They are here instead of sys/cdefs.h because they should not be |
880 | used in public header files. |
881 | |
882 | Their definitions should be kept consistent with the definitions in |
883 | gnulib-common.m4, but it is not necessary to cater to old non-GCC |
884 | compilers, since they will only be used while building glibc itself. |
885 | (Note that _GNUC_PREREQ cannot be used in this file.) */ |
886 | |
887 | /* Define as a marker that can be attached to declarations that might not |
888 | be used. This helps to reduce warnings, such as from |
889 | GCC -Wunused-parameter. */ |
890 | #if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) |
891 | # define _GL_UNUSED __attribute__ ((__unused__)) |
892 | #else |
893 | # define _GL_UNUSED |
894 | #endif |
895 | |
896 | /* gcc supports the "unused" attribute on possibly unused labels, and |
897 | g++ has since version 4.5. Note to support C++ as well as C, |
898 | _GL_UNUSED_LABEL should be used with a trailing ; */ |
899 | #if !defined __cplusplus || __GNUC__ > 4 \ |
900 | || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) |
901 | # define _GL_UNUSED_LABEL _GL_UNUSED |
902 | #else |
903 | # define _GL_UNUSED_LABEL |
904 | #endif |
905 | |
906 | /* The __pure__ attribute was added in gcc 2.96. */ |
907 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) |
908 | # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__)) |
909 | #else |
910 | # define _GL_ATTRIBUTE_PURE /* empty */ |
911 | #endif |
912 | |
913 | /* The __const__ attribute was added in gcc 2.95. */ |
914 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95) |
915 | # define _GL_ATTRIBUTE_CONST __attribute__ ((__const__)) |
916 | #else |
917 | # define _GL_ATTRIBUTE_CONST /* empty */ |
918 | #endif |
919 | |
920 | #endif /* !_ISOMAC */ |
921 | #endif /* libc-symbols.h */ |
922 | |