Update.
[glibc.git] / include / libc-symbols.h
blob8073fca5755a1e5d11b8c50a4d9c00a55a948076
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 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 # define link_warning(symbol, msg) \
180 __make_section_unallocated (".gnu.warning." #symbol) \
181 static const char __evoke_link_warning_##symbol[] \
182 __attribute__ ((section (".gnu.warning." #symbol))) = msg;
183 # else
184 # define link_warning(symbol, msg) \
185 asm(".stabs \"" msg "\",30,0,0,0\n" \
186 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
187 # endif
188 #else
189 /* We will never be heard; they will all die horribly. */
190 # define link_warning(symbol, msg)
191 #endif
193 /* A canned warning for sysdeps/stub functions. */
194 #define stub_warning(name) \
195 link_warning (name, \
196 "warning: " #name " is not implemented and will always fail")
202 #ifdef HAVE_GNU_LD
204 /* Symbol set support macros. */
206 # ifdef HAVE_ELF
208 /* Make SYMBOL, which is in the text segment, an element of SET. */
209 # define text_set_element(set, symbol) _elf_set_element(set, symbol)
210 /* Make SYMBOL, which is in the data segment, an element of SET. */
211 # define data_set_element(set, symbol) _elf_set_element(set, symbol)
212 /* Make SYMBOL, which is in the bss segment, an element of SET. */
213 # define bss_set_element(set, symbol) _elf_set_element(set, symbol)
215 /* These are all done the same way in ELF.
216 There is a new section created for each set. */
217 # ifdef PIC
218 /* When building a shared library, make the set section writable,
219 because it will need to be relocated at run time anyway. */
220 # define _elf_set_element(set, symbol) \
221 static const void *__elf_set_##set##_element_##symbol##__ \
222 __attribute__ ((unused, section (#set))) = &(symbol)
223 # else
224 # define _elf_set_element(set, symbol) \
225 static const void *const __elf_set_##set##_element_##symbol##__ \
226 __attribute__ ((unused, section (#set))) = &(symbol)
227 # endif
229 /* Define SET as a symbol set. This may be required (it is in a.out) to
230 be able to use the set's contents. */
231 # define symbol_set_define(set) symbol_set_declare(set)
233 /* Declare SET for use in this module, if defined in another module. */
234 # define symbol_set_declare(set) \
235 extern void *const __start_##set __attribute__ ((__weak__)); \
236 extern void *const __stop_##set __attribute__ ((__weak__)); \
237 weak_extern (__start_##set) weak_extern (__stop_##set)
239 /* Return a pointer (void *const *) to the first element of SET. */
240 # define symbol_set_first_element(set) (&__start_##set)
242 /* Return true iff PTR (a void *const *) has been incremented
243 past the last element in SET. */
244 # define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
246 # else /* Not ELF: a.out. */
248 # define text_set_element(set, symbol) \
249 asm(".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
250 # define data_set_element(set, symbol) \
251 asm(".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
252 # define bss_set_element(set, symbol) ?error Must use initialized data.
253 # define symbol_set_define(set) void *const (set)[1];
254 # define symbol_set_declare(set) extern void *const (set)[1];
256 # define symbol_set_first_element(set) &(set)[1]
257 # define symbol_set_end_p(set, ptr) (*(ptr) == 0)
259 # endif /* ELF. */
260 #endif /* Have GNU ld. */
262 #if DO_VERSIONING
263 # ifdef __ASSEMBLER__
264 # define symbol_version(real, name, version) \
265 .symver real, name##@##version
266 # define default_symbol_version(real, name, version) \
267 .symver real, name##@##@##version
268 # else
269 # define symbol_version(real, name, version) \
270 __asm__ (".symver " #real "," #name "@" #version)
271 # define default_symbol_version(real, name, version) \
272 __asm__ (".symver " #real "," #name "@@" #version)
273 # endif
274 #else
275 # define symbol_version(real, name, version)
276 # define default_symbol_version(real, name, version) \
277 strong_alias(real, name)
278 #endif
280 #endif /* libc-symbols.h */