Update.
[glibc.git] / include / libc-symbols.h
blob1a3e1c823998689a6e6d8f3467349bd572843e90
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, 1996, 1997, 1998, 2000 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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 * HAVE_GNU_LD if using GNU ld, with support for weak symbols in a.out,
33 and for symbol set and warning messages extensions in a.out and ELF.
34 * HAVE_ELF if using ELF, which supports weak symbols using `.weak'.
35 * HAVE_ASM_WEAK_DIRECTIVE if we have weak symbols using `.weak'.
36 * HAVE_ASM_WEAKEXT_DIRECTIVE if we have weak symbols using `.weakext'.
40 /* This is defined for the compilation of all C library code. features.h
41 tests this to avoid inclusion of stubs.h while compiling the library,
42 before stubs.h has been generated. Some library code that is shared
43 with other packages also tests this symbol to see if it is being
44 compiled as part of the C library. We must define this before including
45 config.h, because it makes some definitions conditional on whether libc
46 itself is being compiled, or just some generator program. */
47 #define _LIBC 1
49 /* Enable declarations of GNU extensions, since we are compiling them. */
50 #define _GNU_SOURCE 1
51 /* And we also need the data for the reentrant functions. */
52 #define _REENTRANT 1
54 #include <config.h>
56 /* The symbols in all the user (non-_) macros are C symbols.
57 HAVE_GNU_LD without HAVE_ELF implies a.out. */
59 #if defined HAVE_ASM_WEAK_DIRECTIVE || defined HAVE_ASM_WEAKEXT_DIRECTIVE
60 # define HAVE_WEAK_SYMBOLS
61 #endif
63 #ifndef __SYMBOL_PREFIX
64 # ifdef NO_UNDERSCORES
65 # define __SYMBOL_PREFIX
66 # else
67 # define __SYMBOL_PREFIX "_"
68 # endif
69 #endif
71 #ifndef C_SYMBOL_NAME
72 # ifdef NO_UNDERSCORES
73 # define C_SYMBOL_NAME(name) name
74 # else
75 # define C_SYMBOL_NAME(name) _##name
76 # endif
77 #endif
79 #ifndef __ASSEMBLER__
80 /* GCC understands weak symbols and aliases; use its interface where
81 possible, instead of embedded assembly language. */
83 /* Define ALIASNAME as a strong alias for NAME. */
84 # define strong_alias(name, aliasname) \
85 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
87 /* This comes between the return type and function name in
88 a function definition to make that definition weak. */
89 # define weak_function __attribute__ ((weak))
90 # define weak_const_function __attribute__ ((weak, __const__))
92 # ifdef HAVE_WEAK_SYMBOLS
94 /* Define ALIASNAME as a weak alias for NAME.
95 If weak aliases are not available, this defines a strong alias. */
96 # define weak_alias(name, aliasname) \
97 extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
99 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
100 # ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
101 # define weak_extern(symbol) asm (".weakext " __SYMBOL_PREFIX #symbol);
102 # else
103 # define weak_extern(symbol) asm (".weak " __SYMBOL_PREFIX #symbol);
104 # endif
106 # else
108 # define weak_alias(name, aliasname) strong_alias(name, aliasname)
109 # define weak_extern(symbol) /* Nothing. */
111 # endif
113 #else /* __ASSEMBLER__ */
115 # ifdef HAVE_ASM_SET_DIRECTIVE
116 # define strong_alias(original, alias) \
117 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
118 .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
119 # else
120 # define strong_alias(original, alias) \
121 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias); \
122 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
123 # endif
125 # ifdef HAVE_WEAK_SYMBOLS
126 # ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
127 # define weak_alias(original, alias) \
128 .weakext C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
129 # define weak_extern(symbol) \
130 .weakext C_SYMBOL_NAME (symbol)
132 # else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
134 # define weak_alias(original, alias) \
135 .weak C_SYMBOL_NAME (alias); \
136 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
138 # define weak_extern(symbol) \
139 .weak C_SYMBOL_NAME (symbol)
141 # endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
143 # else /* ! HAVE_WEAK_SYMBOLS */
145 # define weak_alias(original, alias) strong_alias(original, alias)
146 # define weak_extern(symbol) /* Nothing */
147 # endif /* ! HAVE_WEAK_SYMBOLS */
149 #endif /* __ASSEMBLER__ */
151 /* On some platforms we can make internal function calls (i.e., calls of
152 functions not exported) a bit faster by using a different calling
153 convention. */
154 #ifndef internal_function
155 # define internal_function /* empty */
156 #endif
158 /* Prepare for the case that `__builtin_expect' is not available. */
159 #ifndef HAVE_BUILTIN_EXPECT
160 # define __builtin_expect(expr, val) (expr)
161 #endif
163 /* When a reference to SYMBOL is encountered, the linker will emit a
164 warning message MSG. */
165 #ifdef HAVE_GNU_LD
166 # ifdef HAVE_ELF
168 /* We want the .gnu.warning.SYMBOL section to be unallocated. */
169 # ifdef HAVE_ASM_PREVIOUS_DIRECTIVE
170 # define __make_section_unallocated(section_string) \
171 asm(".section " section_string "; .previous");
172 # elif defined HAVE_ASM_POPSECTION_DIRECTIVE
173 # define __make_section_unallocated(section_string) \
174 asm(".pushsection " section_string "; .popsection");
175 # else
176 # define __make_section_unallocated(section_string)
177 # endif
179 # ifdef HAVE_SECTION_QUOTES
180 # define link_warning(symbol, msg) \
181 __make_section_unallocated (".gnu.warning." #symbol) \
182 static const char __evoke_link_warning_##symbol[] \
183 __attribute__ ((section (".gnu.warning." #symbol "\"\n\t#\""))) = msg;
184 # else
185 # define link_warning(symbol, msg) \
186 __make_section_unallocated (".gnu.warning." #symbol) \
187 static const char __evoke_link_warning_##symbol[] \
188 __attribute__ ((section (".gnu.warning." #symbol "\n\t#"))) = msg;
189 # endif
190 # else
191 # define link_warning(symbol, msg) \
192 asm(".stabs \"" msg "\",30,0,0,0\n" \
193 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
194 # endif
195 #else
196 /* We will never be heard; they will all die horribly. */
197 # define link_warning(symbol, msg)
198 #endif
200 /* A canned warning for sysdeps/stub functions. */
201 #define stub_warning(name) \
202 link_warning (name, \
203 "warning: " #name " is not implemented and will always fail")
209 #ifdef HAVE_GNU_LD
211 /* Symbol set support macros. */
213 # ifdef HAVE_ELF
215 /* Make SYMBOL, which is in the text segment, an element of SET. */
216 # define text_set_element(set, symbol) _elf_set_element(set, symbol)
217 /* Make SYMBOL, which is in the data segment, an element of SET. */
218 # define data_set_element(set, symbol) _elf_set_element(set, symbol)
219 /* Make SYMBOL, which is in the bss segment, an element of SET. */
220 # define bss_set_element(set, symbol) _elf_set_element(set, symbol)
222 /* These are all done the same way in ELF.
223 There is a new section created for each set. */
224 # ifdef SHARED
225 /* When building a shared library, make the set section writable,
226 because it will need to be relocated at run time anyway. */
227 # define _elf_set_element(set, symbol) \
228 static const void *__elf_set_##set##_element_##symbol##__ \
229 __attribute__ ((unused, section (#set))) = &(symbol)
230 # else
231 # define _elf_set_element(set, symbol) \
232 static const void *const __elf_set_##set##_element_##symbol##__ \
233 __attribute__ ((unused, section (#set))) = &(symbol)
234 # endif
236 /* Define SET as a symbol set. This may be required (it is in a.out) to
237 be able to use the set's contents. */
238 # define symbol_set_define(set) symbol_set_declare(set)
240 /* Declare SET for use in this module, if defined in another module. */
241 # define symbol_set_declare(set) \
242 extern void *const __start_##set __attribute__ ((__weak__)); \
243 extern void *const __stop_##set __attribute__ ((__weak__)); \
244 weak_extern (__start_##set) weak_extern (__stop_##set)
246 /* Return a pointer (void *const *) to the first element of SET. */
247 # define symbol_set_first_element(set) (&__start_##set)
249 /* Return true iff PTR (a void *const *) has been incremented
250 past the last element in SET. */
251 # define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
253 # else /* Not ELF: a.out. */
255 # define text_set_element(set, symbol) \
256 asm(".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
257 # define data_set_element(set, symbol) \
258 asm(".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
259 # define bss_set_element(set, symbol) ?error Must use initialized data.
260 # define symbol_set_define(set) void *const (set)[1];
261 # define symbol_set_declare(set) extern void *const (set)[1];
263 # define symbol_set_first_element(set) &(set)[1]
264 # define symbol_set_end_p(set, ptr) (*(ptr) == 0)
266 # endif /* ELF. */
267 #endif /* Have GNU ld. */
269 #if DO_VERSIONING
270 # ifdef __ASSEMBLER__
271 # define symbol_version(real, name, version) \
272 .symver real, name##@##version
273 # define default_symbol_version(real, name, version) \
274 .symver real, name##@##@##version
275 # else
276 # define symbol_version(real, name, version) \
277 __asm__ (".symver " #real "," #name "@" #version)
278 # define default_symbol_version(real, name, version) \
279 __asm__ (".symver " #real "," #name "@@" #version)
280 # endif
281 #else
282 # define symbol_version(real, name, version)
283 # define default_symbol_version(real, name, version) \
284 strong_alias(real, name)
285 #endif
287 #endif /* libc-symbols.h */