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-1998,2000,2001,2002 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #ifndef _LIBC_SYMBOLS_H
22 #define _LIBC_SYMBOLS_H 1
24 /* This file's macros are included implicitly in the compilation of every
25 file in the C library by -imacros.
27 We include config.h which is generated by configure.
28 It should define for us the following symbols:
30 * HAVE_ASM_SET_DIRECTIVE if we have `.set B, A' instead of `A = B'.
31 * ASM_GLOBAL_DIRECTIVE with `.globl' or `.global'.
32 * ASM_TYPE_DIRECTIVE_PREFIX with `@' or `#' or whatever for .type,
33 or leave it undefined if there is no .type directive.
34 * HAVE_GNU_LD if using GNU ld, with support for weak symbols in a.out,
35 and for symbol set and warning messages extensions in a.out and ELF.
36 * HAVE_ELF if using ELF, which supports weak symbols using `.weak'.
37 * HAVE_ASM_WEAK_DIRECTIVE if we have weak symbols using `.weak'.
38 * HAVE_ASM_WEAKEXT_DIRECTIVE if we have weak symbols using `.weakext'.
42 /* This is defined for the compilation of all C library code. features.h
43 tests this to avoid inclusion of stubs.h while compiling the library,
44 before stubs.h has been generated. Some library code that is shared
45 with other packages also tests this symbol to see if it is being
46 compiled as part of the C library. We must define this before including
47 config.h, because it makes some definitions conditional on whether libc
48 itself is being compiled, or just some generator program. */
51 /* Enable declarations of GNU extensions, since we are compiling them. */
53 /* And we also need the data for the reentrant functions. */
58 /* The symbols in all the user (non-_) macros are C symbols.
59 HAVE_GNU_LD without HAVE_ELF implies a.out. */
61 #if defined HAVE_ASM_WEAK_DIRECTIVE || defined HAVE_ASM_WEAKEXT_DIRECTIVE
62 # define HAVE_WEAK_SYMBOLS
65 #ifndef __SYMBOL_PREFIX
66 # ifdef NO_UNDERSCORES
67 # define __SYMBOL_PREFIX
69 # define __SYMBOL_PREFIX "_"
74 # ifdef NO_UNDERSCORES
75 # define C_SYMBOL_NAME(name) name
77 # define C_SYMBOL_NAME(name) _##name
82 # define ASM_LINE_SEP ;
85 #ifndef C_SYMBOL_DOT_NAME
86 # define C_SYMBOL_DOT_NAME(name) .##name
90 /* GCC understands weak symbols and aliases; use its interface where
91 possible, instead of embedded assembly language. */
93 /* Define ALIASNAME as a strong alias for NAME. */
94 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
95 # define _strong_alias(name, aliasname) \
96 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
98 /* This comes between the return type and function name in
99 a function definition to make that definition weak. */
100 # define weak_function __attribute__ ((weak))
101 # define weak_const_function __attribute__ ((weak, __const__))
103 # ifdef HAVE_WEAK_SYMBOLS
105 /* Define ALIASNAME as a weak alias for NAME.
106 If weak aliases are not available, this defines a strong alias. */
107 # define weak_alias(name, aliasname) _weak_alias (name, aliasname)
108 # define _weak_alias(name, aliasname) \
109 extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
111 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
112 # define weak_extern(symbol) _weak_extern (symbol)
113 # ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
114 # define _weak_extern(symbol) asm (".weakext " __SYMBOL_PREFIX #symbol);
116 # define _weak_extern(symbol) asm (".weak " __SYMBOL_PREFIX #symbol);
121 # define weak_alias(name, aliasname) strong_alias(name, aliasname)
122 # define weak_extern(symbol) /* Nothing. */
126 #else /* __ASSEMBLER__ */
128 # ifdef HAVE_ASM_SET_DIRECTIVE
129 # define strong_alias(original, alias) \
130 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
131 .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
133 # ifdef HAVE_ASM_GLOBAL_DOT_NAME
134 # define strong_alias(original, alias) \
135 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
136 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP \
137 ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP \
138 C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
140 # define strong_alias(original, alias) \
141 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
142 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
146 # ifdef HAVE_WEAK_SYMBOLS
147 # ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
148 # define weak_alias(original, alias) \
149 .weakext C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
150 # define weak_extern(symbol) \
151 .weakext C_SYMBOL_NAME (symbol)
153 # else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
155 # ifdef HAVE_ASM_GLOBAL_DOT_NAME
156 # define weak_alias(original, alias) \
157 .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
158 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP \
159 ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP \
160 C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
162 # define weak_alias(original, alias) \
163 .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
164 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
167 # define weak_extern(symbol) \
168 .weak C_SYMBOL_NAME (symbol)
170 # endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
172 # else /* ! HAVE_WEAK_SYMBOLS */
174 # define weak_alias(original, alias) strong_alias(original, alias)
175 # define weak_extern(symbol) /* Nothing */
176 # endif /* ! HAVE_WEAK_SYMBOLS */
178 #endif /* __ASSEMBLER__ */
180 /* On some platforms we can make internal function calls (i.e., calls of
181 functions not exported) a bit faster by using a different calling
183 #ifndef internal_function
184 # define internal_function /* empty */
187 /* Prepare for the case that `__builtin_expect' is not available. */
188 #ifndef HAVE_BUILTIN_EXPECT
189 # define __builtin_expect(expr, val) (expr)
192 /* Determine the return address. */
193 #define RETURN_ADDRESS(nr) \
194 __builtin_extract_return_addr (__builtin_return_address (nr))
196 /* When a reference to SYMBOL is encountered, the linker will emit a
197 warning message MSG. */
201 /* We want the .gnu.warning.SYMBOL section to be unallocated. */
202 # ifdef HAVE_ASM_PREVIOUS_DIRECTIVE
203 # define __make_section_unallocated(section_string) \
204 asm (".section " section_string "\n\t.previous");
205 # elif defined HAVE_ASM_POPSECTION_DIRECTIVE
206 # define __make_section_unallocated(section_string) \
207 asm (".pushsection " section_string "\n\t.popsection");
209 # define __make_section_unallocated(section_string)
212 /* Tacking on "\n\t#" to the section name makes gcc put it's bogus
213 section attributes on what looks like a comment to the assembler. */
214 # ifdef HAVE_SECTION_QUOTES
215 # define link_warning(symbol, msg) \
216 __make_section_unallocated (".gnu.warning." #symbol) \
217 static const char __evoke_link_warning_##symbol[] \
218 __attribute__ ((unused, section (".gnu.warning." #symbol "\"\n\t#\""))) \
221 # define link_warning(symbol, msg) \
222 __make_section_unallocated (".gnu.warning." #symbol) \
223 static const char __evoke_link_warning_##symbol[] \
224 __attribute__ ((unused, section (".gnu.warning." #symbol "\n\t#"))) = msg;
226 # else /* Not ELF: a.out */
228 /* XCOFF does not support .stabs.
229 The native aix linker will remove the .stab and .stabstr sections
230 The gnu linker will have a fatal error if there is a relocation for
231 symbol in the .stab section. Silently disable this macro. */
232 # define link_warning(symbol, msg)
234 # define link_warning(symbol, msg) \
235 asm (".stabs \"" msg "\",30,0,0,0\n\t" \
236 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
240 /* We will never be heard; they will all die horribly. */
241 # define link_warning(symbol, msg)
244 /* A canned warning for sysdeps/stub functions. */
245 #define stub_warning(name) \
246 link_warning (name, \
247 "warning: " #name " is not implemented and will always fail")
250 /* Declare SYMBOL to be TYPE (`function' or `object') and of SIZE bytes,
251 when the assembler supports such declarations (such as in ELF).
252 This is only necessary when defining something in assembly, or playing
253 funny alias games where the size should be other than what the compiler
255 #define declare_symbol(symbol, type, size) \
256 declare_symbol_1 (symbol, type, size)
257 #ifdef ASM_TYPE_DIRECTIVE_PREFIX
258 # ifdef __ASSEMBLER__
259 # define declare_symbol_1(symbol, type, size) \
260 .type C_SYMBOL_NAME (symbol), \
261 declare_symbol_1_paste (ASM_TYPE_DIRECTIVE_PREFIX, type), size
262 # define declare_symbol_1_paste(a, b) declare_symbol_1_paste_1 (a,b)
263 # define declare_symbol_1_paste_1(a,b) a##b
264 # else /* Not __ASSEMBLER__. */
265 # define declare_symbol_1(symbol, type, size) \
266 asm (".type " __SYMBOL_PREFIX #symbol \
267 declare_symbol_1_stringify (ASM_TYPE_DIRECTIVE_PREFIX) #type \
268 "\n\t.size " __SYMBOL_PREFIX #symbol ", " #size);
269 # define declare_symbol_1_stringify(x) declare_symbol_1_stringify_1 (x)
270 # define declare_symbol_1_stringify_1(x) #x
271 # endif /* __ASSEMBLER__ */
273 # define declare_symbol_1(symbol, type, size) /* Nothing. */
283 /* Symbol set support macros. */
287 /* Make SYMBOL, which is in the text segment, an element of SET. */
288 # define text_set_element(set, symbol) _elf_set_element(set, symbol)
289 /* Make SYMBOL, which is in the data segment, an element of SET. */
290 # define data_set_element(set, symbol) _elf_set_element(set, symbol)
291 /* Make SYMBOL, which is in the bss segment, an element of SET. */
292 # define bss_set_element(set, symbol) _elf_set_element(set, symbol)
294 /* These are all done the same way in ELF.
295 There is a new section created for each set. */
297 /* When building a shared library, make the set section writable,
298 because it will need to be relocated at run time anyway. */
299 # define _elf_set_element(set, symbol) \
300 static const void *__elf_set_##set##_element_##symbol##__ \
301 __attribute__ ((unused, section (#set))) = &(symbol)
303 # define _elf_set_element(set, symbol) \
304 static const void *const __elf_set_##set##_element_##symbol##__ \
305 __attribute__ ((unused, section (#set))) = &(symbol)
308 /* Define SET as a symbol set. This may be required (it is in a.out) to
309 be able to use the set's contents. */
310 # define symbol_set_define(set) symbol_set_declare(set)
312 /* Declare SET for use in this module, if defined in another module. */
313 # define symbol_set_declare(set) \
314 extern void *const __start_##set __attribute__ ((__weak__)); \
315 extern void *const __stop_##set __attribute__ ((__weak__)); \
316 weak_extern (__start_##set) weak_extern (__stop_##set)
318 /* Return a pointer (void *const *) to the first element of SET. */
319 # define symbol_set_first_element(set) (&__start_##set)
321 /* Return true iff PTR (a void *const *) has been incremented
322 past the last element in SET. */
323 # define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
325 # else /* Not ELF: a.out. */
328 /* XCOFF does not support .stabs.
329 The native aix linker will remove the .stab and .stabstr sections
330 The gnu linker will have a fatal error if there is a relocation for
331 symbol in the .stab section. Silently disable these macros. */
332 # define text_set_element(set, symbol)
333 # define data_set_element(set, symbol)
334 # define bss_set_element(set, symbol)
336 # define text_set_element(set, symbol) \
337 asm (".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
338 # define data_set_element(set, symbol) \
339 asm (".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
340 # define bss_set_element(set, symbol) ?error Must use initialized data.
342 # define symbol_set_define(set) void *const (set)[1];
343 # define symbol_set_declare(set) extern void *const (set)[1];
345 # define symbol_set_first_element(set) &(set)[1]
346 # define symbol_set_end_p(set, ptr) (*(ptr) == 0)
350 /* We cannot do anything in generial. */
351 # define text_set_element(set, symbol) asm ("")
352 # define data_set_element(set, symbol) asm ("")
353 # define bss_set_element(set, symbol) asm ("")
354 # define symbol_set_define(set) void *const (set)[1];
355 # define symbol_set_declare(set) extern void *const (set)[1];
357 # define symbol_set_first_element(set) &(set)[1]
358 # define symbol_set_end_p(set, ptr) (*(ptr) == 0)
359 #endif /* Have GNU ld. */
362 # define symbol_version(real, name, version) \
363 _symbol_version(real, name, version)
364 # define default_symbol_version(real, name, version) \
365 _default_symbol_version(real, name, version)
366 # ifdef __ASSEMBLER__
367 # define _symbol_version(real, name, version) \
368 .symver real, name##@##version
369 # define _default_symbol_version(real, name, version) \
370 .symver real, name##@##@##version
372 # define _symbol_version(real, name, version) \
373 __asm__ (".symver " #real "," #name "@" #version)
374 # define _default_symbol_version(real, name, version) \
375 __asm__ (".symver " #real "," #name "@@" #version)
378 # define symbol_version(real, name, version)
379 # define default_symbol_version(real, name, version) \
380 strong_alias(real, name)
383 #if defined HAVE_VISIBILITY_ATTRIBUTE && defined SHARED
384 # define attribute_hidden __attribute__ ((visibility ("hidden")))
386 # define attribute_hidden
389 /* Handling on non-exported internal names. We have to do this only
392 # define INTUSE(name) name##_internal
393 # define INTDEF(name) strong_alias (name, name##_internal)
394 # define INTVARDEF(name) \
395 _INTVARDEF (name, name##_internal)
396 # if defined HAVE_VISIBILITY_ATTRIBUTE
397 # define _INTVARDEF(name, aliasname) \
398 extern __typeof (name) aliasname __attribute__ ((alias (#name), \
399 visibility ("hidden")));
401 # define _INTVARDEF(name, aliasname) \
402 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
404 # define INTDEF2(name, newname) strong_alias (name, newname##_internal)
405 # define INTVARDEF2(name, newname) _INTVARDEF (name, newname##_internal)
407 # define INTUSE(name) name
408 # define INTDEF(name)
409 # define INTVARDEF(name)
410 # define INTDEF2(name, newname)
411 # define INTVARDEF2(name, newname)
414 /* The following macros are used for PLT bypassing within libc.so
415 (and if needed other libraries similarly).
416 First of all, you need to have the function prototyped somewhere,
421 If calls to foo within libc.so should always go to foo defined in libc.so,
422 then in include/foo.h you add:
424 libc_hidden_proto (foo)
426 line and after the foo function definition:
432 libc_hidden_def (foo)
440 libc_hidden_weak (foo)
442 If foo is normally just an alias (strong or weak) of some other function,
443 you should use the normal strong_alias first, then add libc_hidden_def
450 strong_alias (baz, foo)
451 libc_hidden_weak (foo)
453 If the function should be internal to multiple objects, say ld.so and
454 libc.so, the best way is to use:
456 #if !defined NOT_IN_libc || defined IS_IN_rtld
460 in include/foo.h and the normal macros at all function definitions
461 depending on what DSO they belong to.
463 If versioned_symbol macro is used to define foo,
464 libc_hidden_ver macro should be used, as in:
466 int __real_foo (int __bar)
470 versioned_symbol (libc, __real_foo, foo, GLIBC_2_1);
471 libc_hidden_ver (__real_foo, foo) */
473 #if defined SHARED && defined DO_VERSIONING \
474 && !defined HAVE_BROKEN_ALIAS_ATTRIBUTE
475 # ifndef __ASSEMBLER__
476 # ifdef HAVE_BROKEN_VISIBILITY_ATTRIBUTE
477 # define __hidden_proto_hiddenattr
479 # define __hidden_proto_hiddenattr attribute_hidden
481 # define hidden_proto(name) __hidden_proto (name, __GI_##name)
482 # define __hidden_proto(name, internal) \
483 extern __typeof (name) internal; \
484 extern __typeof (name) name __asm__ (__hidden_asmname (#internal)) \
485 __hidden_proto_hiddenattr;
486 # define __hidden_asmname(name) \
487 __hidden_asmname1 (__USER_LABEL_PREFIX__, name)
488 # define __hidden_asmname1(prefix, name) __hidden_asmname2(prefix, name)
489 # define __hidden_asmname2(prefix, name) #prefix name
490 # ifdef HAVE_ASM_SET_DIRECTIVE
491 # define __hidden_def1(original, alias) \
492 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
493 .set C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
495 # ifdef HAVE_ASM_GLOBAL_DOT_NAME
496 # define __hidden_def1(original, alias) \
497 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
498 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP \
499 ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP \
500 C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
502 # define __hidden_def1(original, alias) \
503 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
504 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
507 # define __hidden_def2(...) #__VA_ARGS__
508 # define __hidden_def3(...) __hidden_def2 (__VA_ARGS__)
509 # define hidden_def(name) \
510 __asm__ (__hidden_def3 (__hidden_def1 (__GI_##name, name)));
511 # define hidden_ver(local, name) \
512 __asm__ (__hidden_def3 (__hidden_def1 (local, __GI_##name)));
513 # ifdef HAVE_WEAK_SYMBOLS
514 # ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
515 # define __hidden_weak1(original, alias) \
516 .weakext C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
517 # else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
518 # ifdef HAVE_ASM_GLOBAL_DOT_NAME
519 # define __hidden_weak1(original, alias) \
520 .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
521 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP \
522 ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP \
523 C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
525 # define __hidden_weak1(original, alias) \
526 .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
527 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
530 # define hidden_weak(name) \
531 __asm__ (__hidden_def3 (__hidden_weak1 (__GI_##name, name)));
533 # define hidden_weak(name) hidden_def (name)
536 /* For assembly, we need to do the opposite of what we do in C:
537 in assembly gcc __REDIRECT stuff is not in place, so functions
538 are defined by its normal name and we need to create the
539 __GI_* alias to it, in C __REDIRECT causes the function definition
540 to use __GI_* name and we need to add alias to the real name.
541 There is no reason to use hidden_weak over hidden_def in assembly,
542 but we provide it for consistency with the C usage.
543 hidden_proto doesn't make sense for assembly but the equivalent
544 is to call via the HIDDEN_JUMPTARGET macro einstead of JUMPTARGET. */
545 # define hidden_def(name) strong_alias (name, __GI_##name)
546 # define hidden_weak(name) hidden_def (name)
547 # define hidden_ver(local, name) strong_alias (local, __GI_##name)
548 # define HIDDEN_JUMPTARGET(name) __GI_##name
551 # ifndef __ASSEMBLER__
552 # define hidden_proto(name)
554 # define HIDDEN_JUMPTARGET(name) JUMPTARGET(name)
555 # endif /* Not __ASSEMBLER__ */
556 # define hidden_weak(name)
557 # define hidden_def(name)
558 # define hidden_ver(local, name)
561 #if !defined NOT_IN_libc
562 # define libc_hidden_proto(name) hidden_proto (name)
563 # define libc_hidden_def(name) hidden_def (name)
564 # define libc_hidden_weak(name) hidden_weak (name)
565 # define libc_hidden_ver(local, name) hidden_ver (local, name)
567 # define libc_hidden_proto(name)
568 # define libc_hidden_def(name)
569 # define libc_hidden_weak(name)
570 # define libc_hidden_ver(local, name)
573 #if defined NOT_IN_libc && defined IS_IN_rtld
574 # define rtld_hidden_proto(name) hidden_proto (name)
575 # define rtld_hidden_def(name) hidden_def (name)
576 # define rtld_hidden_weak(name) hidden_weak (name)
577 # define rtld_hidden_ver(local, name) hidden_ver (local, name)
579 # define rtld_hidden_proto(name)
580 # define rtld_hidden_def(name)
581 # define rtld_hidden_weak(name)
582 # define rtld_hidden_ver(local, name)
585 #if defined NOT_IN_libc && defined IS_IN_libm
586 # define libm_hidden_proto(name) hidden_proto (name)
587 # define libm_hidden_def(name) hidden_def (name)
588 # define libm_hidden_weak(name) hidden_weak (name)
589 # define libm_hidden_ver(local, name) hidden_ver (local, name)
591 # define libm_hidden_proto(name)
592 # define libm_hidden_def(name)
593 # define libm_hidden_weak(name)
594 # define libm_hidden_ver(local, name)
597 #endif /* libc-symbols.h */