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. */
49 /* Enable declarations of GNU extensions, since we are compiling them. */
51 /* And we also need the data for the reentrant functions. */
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
63 #ifndef __SYMBOL_PREFIX
64 # ifdef NO_UNDERSCORES
65 # define __SYMBOL_PREFIX
67 # define __SYMBOL_PREFIX "_"
72 # ifdef NO_UNDERSCORES
73 # define C_SYMBOL_NAME(name) name
75 # define C_SYMBOL_NAME(name) _##name
80 # define ASM_LINE_SEP ;
83 #ifndef C_SYMBOL_DOT_NAME
84 # define C_SYMBOL_DOT_NAME(name) .##name
88 /* GCC understands weak symbols and aliases; use its interface where
89 possible, instead of embedded assembly language. */
91 /* Define ALIASNAME as a strong alias for NAME. */
92 # define strong_alias(name, aliasname) _strong_alias(name, aliasname)
93 # define _strong_alias(name, aliasname) \
94 extern __typeof (name) aliasname __attribute__ ((alias (#name)));
96 /* This comes between the return type and function name in
97 a function definition to make that definition weak. */
98 # define weak_function __attribute__ ((weak))
99 # define weak_const_function __attribute__ ((weak, __const__))
101 # ifdef HAVE_WEAK_SYMBOLS
103 /* Define ALIASNAME as a weak alias for NAME.
104 If weak aliases are not available, this defines a strong alias. */
105 # define weak_alias(name, aliasname) _weak_alias (name, aliasname)
106 # define _weak_alias(name, aliasname) \
107 extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));
109 /* Declare SYMBOL as weak undefined symbol (resolved to 0 if not defined). */
110 # define weak_extern(symbol) _weak_extern (symbol)
111 # ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
112 # define _weak_extern(symbol) asm (".weakext " __SYMBOL_PREFIX #symbol);
114 # define _weak_extern(symbol) asm (".weak " __SYMBOL_PREFIX #symbol);
119 # define weak_alias(name, aliasname) strong_alias(name, aliasname)
120 # define weak_extern(symbol) /* Nothing. */
124 #else /* __ASSEMBLER__ */
126 # ifdef HAVE_ASM_SET_DIRECTIVE
127 # define strong_alias(original, alias) \
128 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
129 .set C_SYMBOL_NAME (alias),C_SYMBOL_NAME (original)
131 # ifdef HAVE_ASM_GLOBAL_DOT_NAME
132 # define strong_alias(original, alias) \
133 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
134 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP \
135 ASM_GLOBAL_DIRECTIVE C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP \
136 C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
138 # define strong_alias(original, alias) \
139 ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME (alias) ASM_LINE_SEP \
140 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
144 # ifdef HAVE_WEAK_SYMBOLS
145 # ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
146 # define weak_alias(original, alias) \
147 .weakext C_SYMBOL_NAME (alias), C_SYMBOL_NAME (original)
148 # define weak_extern(symbol) \
149 .weakext C_SYMBOL_NAME (symbol)
151 # else /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
153 # ifdef HAVE_ASM_GLOBAL_DOT_NAME
154 # define weak_alias(original, alias) \
155 .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
156 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original) ASM_LINE_SEP \
157 .weak C_SYMBOL_DOT_NAME (alias) ASM_LINE_SEP \
158 C_SYMBOL_DOT_NAME (alias) = C_SYMBOL_DOT_NAME (original)
160 # define weak_alias(original, alias) \
161 .weak C_SYMBOL_NAME (alias) ASM_LINE_SEP \
162 C_SYMBOL_NAME (alias) = C_SYMBOL_NAME (original)
165 # define weak_extern(symbol) \
166 .weak C_SYMBOL_NAME (symbol)
168 # endif /* ! HAVE_ASM_WEAKEXT_DIRECTIVE */
170 # else /* ! HAVE_WEAK_SYMBOLS */
172 # define weak_alias(original, alias) strong_alias(original, alias)
173 # define weak_extern(symbol) /* Nothing */
174 # endif /* ! HAVE_WEAK_SYMBOLS */
176 #endif /* __ASSEMBLER__ */
178 /* On some platforms we can make internal function calls (i.e., calls of
179 functions not exported) a bit faster by using a different calling
181 #ifndef internal_function
182 # define internal_function /* empty */
185 /* Prepare for the case that `__builtin_expect' is not available. */
186 #ifndef HAVE_BUILTIN_EXPECT
187 # define __builtin_expect(expr, val) (expr)
190 /* Determine the return address. */
191 #define RETURN_ADDRESS(nr) \
192 __builtin_extract_return_addr (__builtin_return_address (nr))
194 /* When a reference to SYMBOL is encountered, the linker will emit a
195 warning message MSG. */
199 /* We want the .gnu.warning.SYMBOL section to be unallocated. */
200 # ifdef HAVE_ASM_PREVIOUS_DIRECTIVE
201 # define __make_section_unallocated(section_string) \
202 asm (".section " section_string "\n\t.previous");
203 # elif defined HAVE_ASM_POPSECTION_DIRECTIVE
204 # define __make_section_unallocated(section_string) \
205 asm (".pushsection " section_string "\n\t.popsection");
207 # define __make_section_unallocated(section_string)
210 /* Tacking on "\n\t#" to the section name makes gcc put it's bogus
211 section attributes on what looks like a comment to the assembler. */
212 # ifdef HAVE_SECTION_QUOTES
213 # define link_warning(symbol, msg) \
214 __make_section_unallocated (".gnu.warning." #symbol) \
215 static const char __evoke_link_warning_##symbol[] \
216 __attribute__ ((section (".gnu.warning." #symbol "\"\n\t#\""))) = msg;
218 # define link_warning(symbol, msg) \
219 __make_section_unallocated (".gnu.warning." #symbol) \
220 static const char __evoke_link_warning_##symbol[] \
221 __attribute__ ((section (".gnu.warning." #symbol "\n\t#"))) = msg;
224 # define link_warning(symbol, msg) \
225 asm (".stabs \"" msg "\",30,0,0,0\n\t" \
226 ".stabs \"" __SYMBOL_PREFIX #symbol "\",1,0,0,0\n");
229 /* We will never be heard; they will all die horribly. */
230 # define link_warning(symbol, msg)
233 /* A canned warning for sysdeps/stub functions. */
234 #define stub_warning(name) \
235 link_warning (name, \
236 "warning: " #name " is not implemented and will always fail")
244 /* Symbol set support macros. */
248 /* Make SYMBOL, which is in the text segment, an element of SET. */
249 # define text_set_element(set, symbol) _elf_set_element(set, symbol)
250 /* Make SYMBOL, which is in the data segment, an element of SET. */
251 # define data_set_element(set, symbol) _elf_set_element(set, symbol)
252 /* Make SYMBOL, which is in the bss segment, an element of SET. */
253 # define bss_set_element(set, symbol) _elf_set_element(set, symbol)
255 /* These are all done the same way in ELF.
256 There is a new section created for each set. */
258 /* When building a shared library, make the set section writable,
259 because it will need to be relocated at run time anyway. */
260 # define _elf_set_element(set, symbol) \
261 static const void *__elf_set_##set##_element_##symbol##__ \
262 __attribute__ ((unused, section (#set))) = &(symbol)
264 # define _elf_set_element(set, symbol) \
265 static const void *const __elf_set_##set##_element_##symbol##__ \
266 __attribute__ ((unused, section (#set))) = &(symbol)
269 /* Define SET as a symbol set. This may be required (it is in a.out) to
270 be able to use the set's contents. */
271 # define symbol_set_define(set) symbol_set_declare(set)
273 /* Declare SET for use in this module, if defined in another module. */
274 # define symbol_set_declare(set) \
275 extern void *const __start_##set __attribute__ ((__weak__)); \
276 extern void *const __stop_##set __attribute__ ((__weak__)); \
277 weak_extern (__start_##set) weak_extern (__stop_##set)
279 /* Return a pointer (void *const *) to the first element of SET. */
280 # define symbol_set_first_element(set) (&__start_##set)
282 /* Return true iff PTR (a void *const *) has been incremented
283 past the last element in SET. */
284 # define symbol_set_end_p(set, ptr) ((ptr) >= &__stop_##set)
286 # else /* Not ELF: a.out. */
288 # define text_set_element(set, symbol) \
289 asm (".stabs \"" __SYMBOL_PREFIX #set "\",23,0,0," __SYMBOL_PREFIX #symbol)
290 # define data_set_element(set, symbol) \
291 asm (".stabs \"" __SYMBOL_PREFIX #set "\",25,0,0," __SYMBOL_PREFIX #symbol)
292 # define bss_set_element(set, symbol) ?error Must use initialized data.
293 # define symbol_set_define(set) void *const (set)[1];
294 # define symbol_set_declare(set) extern void *const (set)[1];
296 # define symbol_set_first_element(set) &(set)[1]
297 # define symbol_set_end_p(set, ptr) (*(ptr) == 0)
300 #endif /* Have GNU ld. */
303 # define symbol_version(real, name, version) \
304 _symbol_version(real, name, version)
305 # define default_symbol_version(real, name, version) \
306 _default_symbol_version(real, name, version)
307 # ifdef __ASSEMBLER__
308 # define _symbol_version(real, name, version) \
309 .symver real, name##@##version
310 # define _default_symbol_version(real, name, version) \
311 .symver real, name##@##@##version
313 # define _symbol_version(real, name, version) \
314 __asm__ (".symver " #real "," #name "@" #version)
315 # define _default_symbol_version(real, name, version) \
316 __asm__ (".symver " #real "," #name "@@" #version)
319 # define symbol_version(real, name, version)
320 # define default_symbol_version(real, name, version) \
321 strong_alias(real, name)
324 #endif /* libc-symbols.h */